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

Last change on this file since 1194 was 1194, checked in by Sam Hocevar, 16 years ago
  • The mono9 driver works again. With wrapping and stdin reading.
  • Property svn:keywords set to Id
File size: 7.6 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 1194 2006-10-09 23:54:40Z sam $
7 *
8 *  This program is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14/*
15 * This is the main program entry point.
16 */
17
18#include "config.h"
19
20#if defined(HAVE_INTTYPES_H)
21#   include <inttypes.h>
22#endif
23#if defined(HAVE_GETOPT_H)
24#   include <getopt.h>
25#endif
26#if defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ)
27#   include <sys/ioctl.h>
28#endif
29#include <stdio.h>
30#include <string.h>
31#include <stdlib.h>
32#include <cucul.h>
33
34#include "toilet.h"
35#include "render.h"
36#include "figlet.h"
37#include "filters.h"
38
39static void version(void);
40#if defined(HAVE_GETOPT_H)
41static void usage(void);
42#endif
43
44int main(int argc, char *argv[])
45{
46    context_t struct_cx;
47    context_t *cx = &struct_cx;
48
49    cucul_buffer_t *buffer;
50
51    int i, j;
52
53    unsigned int flag_gay = 0;
54    unsigned int flag_metal = 0;
55    int infocode = -1;
56
57    cx->export = "utf8";
58    cx->font = "mono9";
59    cx->dir = "/usr/share/figlet/";
60
61    cx->term_width = 80;
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            { "gay", 0, NULL, 'g' },
77            { "metal", 0, NULL, 'm' },
78            { "irc", 0, NULL, 'i' },
79            { "help", 0, NULL, 'h' },
80            { "infocode", 1, NULL, 'I' },
81            { "version", 0, NULL, 'v' },
82            { NULL, 0, NULL, 0 }
83        };
84
85        int c = getopt_long(argc, argv, "d:f:I:w:ghimtv",
86                            long_options, &option_index);
87#   else
88#       define MOREINFO "Try `%s -h' for more information.\n"
89        int c = getopt(argc, argv, "d:f:I:w:ghimtv");
90#   endif
91        if(c == -1)
92            break;
93
94        switch(c)
95        {
96        case 'h': /* --help */
97            usage();
98            return 0;
99        case 'I': /* --infocode */
100            infocode = atoi(optarg);
101            break;
102        case 'v': /* --version */
103            version();
104            return 0;
105        case 'f': /* --font */
106            cx->font = optarg;
107            break;
108        case 'd': /* --directory */
109            cx->dir = optarg;
110            break;
111        case 'g': /* --gay */
112            flag_gay = 1;
113            break;
114        case 'm': /* --metal */
115            flag_metal = 1;
116            break;
117        case 'w': /* --width */
118            cx->term_width = atoi(optarg);
119            break;
120        case 't': /* --termwidth */
121        {
122#if defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ)
123            struct winsize ws;
124
125            if((ioctl(1, TIOCGWINSZ, &ws) != -1 ||
126                ioctl(2, TIOCGWINSZ, &ws) != -1 ||
127                ioctl(0, TIOCGWINSZ, &ws) != -1) && ws.ws_col != 0)
128                cx->term_width = ws.ws_col;
129#endif
130            break;
131        }
132        case 'i': /* --irc */
133            cx->export = "irc";
134            break;
135        case '?':
136            printf(MOREINFO, argv[0]);
137            return 1;
138        default:
139            printf("%s: invalid option -- %i\n", argv[0], c);
140            printf(MOREINFO, argv[0]);
141            return 1;
142        }
143    }
144#else
145#   define MOREINFO "Usage: %s message...\n"
146    int optind = 1;
147#endif
148
149    switch(infocode)
150    {
151        case -1:
152            break;
153        case 0:
154            version();
155            return 0;
156        case 1:
157            printf("20201\n");
158            return 0;
159        case 2:
160            printf("%s\n", cx->dir);
161            return 0;
162        case 3:
163            printf("%s\n", cx->font);
164            return 0;
165        case 4:
166            printf("%u\n", cx->term_width);
167            return 0;
168        default:
169            return 0;
170    }
171
172    if(!strcasecmp(cx->font, "mono9"))
173        init_big(cx);
174    else /* if(!strcasecmp(cx->font, "term")) */
175        init_tiny(cx);
176
177    if(optind >= argc)
178    {
179        char buf[10];
180        unsigned int len;
181        uint32_t ch;
182
183        i = 0;
184
185        /* Read from stdin */
186        while(!feof(stdin))
187        {
188            buf[i++] = getchar();
189            buf[i] = '\0';
190
191            ch = cucul_utf8_to_utf32(buf, &len);
192
193            if(!len)
194                continue;
195
196            cx->feed(cx, ch);
197            i = 0;
198        }
199    }
200    else for(i = optind; i < argc; i++)
201    {
202        /* Read from commandline */
203        unsigned int len;
204
205        if(i != optind)
206            cx->feed(cx, ' ');
207
208        for(j = 0; argv[i][j];)
209        {
210            cx->feed(cx, cucul_utf8_to_utf32(argv[i] + j, &len));
211            j += len;
212        }
213    }
214
215    cx->end(cx);
216
217    /* Apply optional effects to our string */
218    if(!strcasecmp(cx->font, "mono9"))
219        filter_autocrop(cx->cv);
220    if(flag_metal)
221        filter_metal(cx->cv);
222    if(flag_gay)
223        filter_gay(cx->cv);
224
225    /* Output char */
226    buffer = cucul_export_canvas(cx->cv, cx->export);
227    fwrite(cucul_get_buffer_data(buffer),
228           cucul_get_buffer_size(buffer), 1, stdout);
229    cucul_free_buffer(buffer);
230
231    cucul_free_canvas(cx->cv);
232
233    return 0;
234}
235
236#if defined(HAVE_GETOPT_H)
237#   define USAGE \
238    "Usage: toilet [ -ghimtv ] [ -d fontdirectory ]\n" \
239    "              [ -f fontfile ] [ -w outputwidth ]\n" \
240    "              [ -I infocode ] [ message ]\n"
241#else
242#   define USAGE ""
243#endif
244
245static void version(void)
246{
247    printf(
248    "TOIlet Copyright 2006 Sam Hocevar\n"
249    "Internet: <sam@zoy.org> Version: %s, date: %s\n"
250    "\n"
251    "TOIlet, along with the various TOIlet fonts and documentation, may be\n"
252    "freely copied and distributed.\n"
253    "\n"
254    "If you use TOIlet, please send an e-mail message to <sam@zoy.org>.\n"
255    "\n"
256    "The latest version of TOIlet is available from the web site,\n"
257    "        http://libcaca.zoy.org/toilet.html\n"
258    "\n"
259    USAGE,
260    VERSION, DATE);
261}
262
263#if defined(HAVE_GETOPT_H)
264static void usage(void)
265{
266    printf(USAGE);
267#   ifdef HAVE_GETOPT_LONG
268    printf("  -f, --font <fontfile>    select the font\n");
269    printf("  -d, --directory <dir>    specify font directory\n");
270    printf("  -w, --width <width>      set output width\n");
271    printf("  -t, --termwidth          adapt to terminal's width\n");
272    printf("  -g, --gay                add a rainbow effect to the text\n");
273    printf("  -m, --metal              add a metal effect to the text\n");
274    printf("  -i, --irc                output IRC colour codes\n");
275    printf("  -h, --help               display this help and exit\n");
276    printf("  -I, --infocode           print FIGlet-compatible infocode\n");
277    printf("  -v, --version            output version information and exit\n");
278#   else
279    printf("  -f <fontfile>    select the font\n");
280    printf("  -d <dir>         specify font directory\n");
281    printf("  -w <width>       set output width\n");
282    printf("  -t               adapt to terminal's width\n");
283    printf("  -g               add a rainbow effect to the text\n");
284    printf("  -m               add a metal effect to the text\n");
285    printf("  -i               output IRC colour codes\n");
286    printf("  -h               display this help and exit\n");
287    printf("  -I               print FIGlet-compatible infocode\n");
288    printf("  -v               output version information and exit\n");
289#   endif
290}
291#endif
292
Note: See TracBrowser for help on using the repository browser.