source: toilet/trunk/src/main.c @ 2263

Last change on this file since 2263 was 1461, checked in by Sam Hocevar, 16 years ago
  • Bwarf, typo in the no warranty clause.
  • Property svn:keywords set to Id
File size: 8.2 KB
Line 
1/*
2 *  TOIlet        The Other Implementation’s letters
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: main.c 1461 2006-12-12 01:50:48Z sam $
7 *
8 *  This program is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 * This is the main program entry point.
17 */
18
19#include "config.h"
20
21#if defined HAVE_INTTYPES_H
22#   include <inttypes.h>
23#endif
24#if defined HAVE_GETOPT_H
25#   include <getopt.h>
26#endif
27#if defined HAVE_SYS_IOCTL_H && defined HAVE_TIOCGWINSZ
28#   include <sys/ioctl.h>
29#endif
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <cucul.h>
34
35#include "toilet.h"
36#include "render.h"
37#include "filter.h"
38#include "export.h"
39
40static void version(void);
41#if defined HAVE_GETOPT_H
42static void usage(void);
43#endif
44
45int main(int argc, char *argv[])
46{
47    context_t struct_cx;
48    context_t *cx = &struct_cx;
49
50    int infocode = -1;
51
52    cx->export = "utf8";
53    cx->font = "smblock";
54    cx->dir = FONTDIR;
55
56    cx->term_width = 80;
57
58    cx->hmode = H_DEFAULT;
59
60    cx->filters = NULL;
61    cx->nfilters = 0;
62
63#if defined HAVE_GETOPT_H
64    for(;;)
65    {
66#   ifdef HAVE_GETOPT_LONG
67#       define MOREINFO "Try `%s --help' for more information.\n"
68        int option_index = 0;
69        static struct option long_options[] =
70        {
71            /* Long option, needs arg, flag, short option */
72            { "font", 1, NULL, 'f' },
73            { "directory", 1, NULL, 'd' },
74            { "width", 1, NULL, 'w' },
75            { "termwidth", 0, NULL, 't' },
76            { "filter", 1, NULL, 'F' },
77            { "gay", 0, NULL, 130 },
78            { "metal", 0, NULL, 131 },
79            { "export", 1, NULL, 'E' },
80            { "irc", 0, NULL, 140 },
81            { "html", 0, NULL, 141 },
82            { "help", 0, NULL, 'h' },
83            { "infocode", 1, NULL, 'I' },
84            { "version", 0, NULL, 'v' },
85            { NULL, 0, NULL, 0 }
86        };
87
88        int c = getopt_long(argc, argv, "f:d:w:tsSkWoF:E:hI:v",
89                            long_options, &option_index);
90#   else
91#       define MOREINFO "Try `%s -h' for more information.\n"
92        int c = getopt(argc, argv, "f:d:w:tsSkWoF:E:hI:v");
93#   endif
94        if(c == -1)
95            break;
96
97        switch(c)
98        {
99        case 'h': /* --help */
100            usage();
101            return 0;
102        case 'I': /* --infocode */
103            infocode = atoi(optarg);
104            break;
105        case 'v': /* --version */
106            version();
107            return 0;
108        case 'f': /* --font */
109            cx->font = optarg;
110            break;
111        case 'd': /* --directory */
112            cx->dir = optarg;
113            break;
114        case 'F': /* --filter */
115            if(!strcmp(optarg, "list"))
116                return filter_list();
117            if(filter_add(cx, optarg) < 0)
118                return -1;
119            break;
120        case 130: /* --gay */
121            filter_add(cx, "gay");
122            break;
123        case 131: /* --metal */
124            filter_add(cx, "metal");
125            break;
126        case 'w': /* --width */
127            cx->term_width = atoi(optarg);
128            break;
129        case 't': /* --termwidth */
130        {
131#if defined HAVE_SYS_IOCTL_H && defined HAVE_TIOCGWINSZ
132            struct winsize ws;
133
134            if((ioctl(1, TIOCGWINSZ, &ws) != -1 ||
135                ioctl(2, TIOCGWINSZ, &ws) != -1 ||
136                ioctl(0, TIOCGWINSZ, &ws) != -1) && ws.ws_col != 0)
137                cx->term_width = ws.ws_col;
138#endif
139            break;
140        }
141        case 's':
142            cx->hmode = H_DEFAULT;
143            break;
144        case 'S':
145            cx->hmode = H_SMUSH;
146            break;
147        case 'k':
148            cx->hmode = H_KERN;
149            break;
150        case 'W':
151            cx->hmode = H_NONE;
152            break;
153        case 'o':
154            cx->hmode = H_OVERLAP;
155            break;
156        case 'E': /* --export */
157            if(!strcmp(optarg, "list"))
158                return export_list();
159            if(export_set(cx, optarg) < 0)
160                return -1;
161            break;
162        case 140: /* --irc */
163            export_set(cx, "irc");
164            break;
165        case 141: /* --html */
166            export_set(cx, "html");
167            break;
168        case '?':
169            printf(MOREINFO, argv[0]);
170            return 1;
171        default:
172            printf("%s: invalid option -- %i\n", argv[0], c);
173            printf(MOREINFO, argv[0]);
174            return 1;
175        }
176    }
177#else
178#   define MOREINFO "Usage: %s message...\n"
179    int optind = 1;
180#endif
181
182    switch(infocode)
183    {
184        case -1:
185            break;
186        case 0:
187            version();
188            return 0;
189        case 1:
190            printf("20201\n");
191            return 0;
192        case 2:
193            printf("%s\n", cx->dir);
194            return 0;
195        case 3:
196            printf("%s\n", cx->font);
197            return 0;
198        case 4:
199            printf("%u\n", cx->term_width);
200            return 0;
201        default:
202            return 0;
203    }
204
205    if(render_init(cx) < 0)
206        return -1;
207
208    if(optind >= argc)
209        render_stdin(cx);
210    else
211        render_list(cx, argc - optind, argv + optind);
212
213    render_end(cx);
214    filter_end(cx);
215
216    return 0;
217}
218
219#if defined HAVE_GETOPT_H
220#   define USAGE \
221    "Usage: toilet [ -hkostvSW ] [ -d fontdirectory ]\n" \
222    "              [ -f fontfile ] [ -F filter ] [ -w outputwidth ]\n" \
223    "              [ -I infocode ] [ -E format ] [ message ]\n"
224#else
225#   define USAGE ""
226#endif
227
228#if defined HAVE_GETOPT_LONG
229#   define HELP \
230    "  -f, --font <name>        select the font\n" \
231    "  -d, --directory <dir>    specify font directory\n" \
232    "  -s, -S, -k, -W, -o       render mode (default, force smushing,\n" \
233    "                           kerning, full width, overlap)\n" \
234    "  -w, --width <width>      set output width\n" \
235    "  -t, --termwidth          adapt to terminal's width\n" \
236    "  -F, --filter <filters>   apply one or several filters to the text\n" \
237    "  -F, --filter list        list available filters\n" \
238    "      --gay                rainbow filter (same as -F gay)\n" \
239    "      --metal              metal filter (same as -F metal)\n" \
240    "  -E, --export <format>    select export format\n" \
241    "  -E, --export list        list available export formats\n" \
242    "      --irc                output IRC colour codes (same as -E irc)\n" \
243    "      --html               output an HTML document (same as -E html)\n" \
244    "  -h, --help               display this help and exit\n" \
245    "  -I, --infocode <code>    print FIGlet-compatible infocode\n" \
246    "  -v, --version            output version information and exit\n"
247#else
248#   define HELP \
249    "  -f <name>           select the font\n" \
250    "  -d <dir>            specify font directory\n" \
251    "  -s, -S, -k, -W, -o  render mode (default, force smushing,\n" \
252    "                      kerning, full width, overlap)\n" \
253    "  -w <width>          set output width\n" \
254    "  -t                  adapt to terminal's width\n" \
255    "  -F <filters>        apply one or several filters to the text\n" \
256    "  -F list             list available filters\n" \
257    "  -E <format>         select export format\n" \
258    "  -E list             list available export formats\n" \
259    "  -h                  display this help and exit\n" \
260    "  -I <code>           print FIGlet-compatible infocode\n" \
261    "  -v                  output version information and exit\n"
262#endif
263
264static void version(void)
265{
266    printf(
267    "TOIlet Copyright 2006 Sam Hocevar\n"
268    "Internet: <sam@zoy.org> Version: %s, date: %s\n"
269    "\n"
270    "TOIlet, along with the various TOIlet fonts and documentation, may be\n"
271    "freely copied and distributed.\n"
272    "\n"
273    "If you use TOIlet, please send an e-mail message to <sam@zoy.org>.\n"
274    "\n"
275    "The latest version of TOIlet is available from the web site,\n"
276    "        http://libcaca.zoy.org/toilet.html\n"
277    "\n"
278    "%s", VERSION, DATE, USAGE);
279}
280
281#if defined HAVE_GETOPT_H
282static void usage(void)
283{
284    printf("%s%s", HELP, USAGE);
285}
286#endif
287
Note: See TracBrowser for help on using the repository browser.