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 1240 2006-10-26 12:12:44Z 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(HAVE_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 "filter.h" |
---|
38 | |
---|
39 | static void version(void); |
---|
40 | #if defined(HAVE_GETOPT_H) |
---|
41 | static void usage(void); |
---|
42 | #endif |
---|
43 | |
---|
44 | int 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, ret; |
---|
52 | |
---|
53 | int infocode = -1; |
---|
54 | |
---|
55 | cx->export = "utf8"; |
---|
56 | cx->font = "mono9"; |
---|
57 | cx->dir = "/usr/share/figlet/"; |
---|
58 | |
---|
59 | cx->term_width = 80; |
---|
60 | |
---|
61 | cx->filters = NULL; |
---|
62 | cx->nfilters = 0; |
---|
63 | |
---|
64 | #if defined(HAVE_GETOPT_H) |
---|
65 | for(;;) |
---|
66 | { |
---|
67 | # ifdef HAVE_GETOPT_LONG |
---|
68 | # define MOREINFO "Try `%s --help' for more information.\n" |
---|
69 | int option_index = 0; |
---|
70 | static struct option long_options[] = |
---|
71 | { |
---|
72 | /* Long option, needs arg, flag, short option */ |
---|
73 | { "font", 1, NULL, 'f' }, |
---|
74 | { "directory", 1, NULL, 'd' }, |
---|
75 | { "width", 1, NULL, 'w' }, |
---|
76 | { "termwidth", 0, NULL, 't' }, |
---|
77 | { "filter", 1, NULL, 'F' }, |
---|
78 | { "gay", 0, NULL, 130 }, |
---|
79 | { "metal", 0, NULL, 131 }, |
---|
80 | { "irc", 0, NULL, 140 }, |
---|
81 | { "html", 0, NULL, 141 }, |
---|
82 | { "tga", 0, NULL, 142 }, |
---|
83 | { "help", 0, NULL, 'h' }, |
---|
84 | { "infocode", 1, NULL, 'I' }, |
---|
85 | { "version", 0, NULL, 'v' }, |
---|
86 | { NULL, 0, NULL, 0 } |
---|
87 | }; |
---|
88 | |
---|
89 | int c = getopt_long(argc, argv, "f:d:w:tF:hI:v", |
---|
90 | long_options, &option_index); |
---|
91 | # else |
---|
92 | # define MOREINFO "Try `%s -h' for more information.\n" |
---|
93 | int c = getopt(argc, argv, "f:d:w:tF:hI:v"); |
---|
94 | # endif |
---|
95 | if(c == -1) |
---|
96 | break; |
---|
97 | |
---|
98 | switch(c) |
---|
99 | { |
---|
100 | case 'h': /* --help */ |
---|
101 | usage(); |
---|
102 | return 0; |
---|
103 | case 'I': /* --infocode */ |
---|
104 | infocode = atoi(optarg); |
---|
105 | break; |
---|
106 | case 'v': /* --version */ |
---|
107 | version(); |
---|
108 | return 0; |
---|
109 | case 'f': /* --font */ |
---|
110 | cx->font = optarg; |
---|
111 | break; |
---|
112 | case 'd': /* --directory */ |
---|
113 | cx->dir = optarg; |
---|
114 | break; |
---|
115 | case 'F': /* --filter */ |
---|
116 | if(filter_add(cx, optarg)) |
---|
117 | return -1; |
---|
118 | break; |
---|
119 | case 130: /* --gay */ |
---|
120 | filter_add(cx, "gay"); |
---|
121 | break; |
---|
122 | case 131: /* --metal */ |
---|
123 | filter_add(cx, "metal"); |
---|
124 | break; |
---|
125 | case 'w': /* --width */ |
---|
126 | cx->term_width = atoi(optarg); |
---|
127 | break; |
---|
128 | case 't': /* --termwidth */ |
---|
129 | { |
---|
130 | #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_TIOCGWINSZ) |
---|
131 | struct winsize ws; |
---|
132 | |
---|
133 | if((ioctl(1, TIOCGWINSZ, &ws) != -1 || |
---|
134 | ioctl(2, TIOCGWINSZ, &ws) != -1 || |
---|
135 | ioctl(0, TIOCGWINSZ, &ws) != -1) && ws.ws_col != 0) |
---|
136 | cx->term_width = ws.ws_col; |
---|
137 | #endif |
---|
138 | break; |
---|
139 | } |
---|
140 | case 140: /* --irc */ |
---|
141 | cx->export = "irc"; |
---|
142 | break; |
---|
143 | case 141: /* --html */ |
---|
144 | cx->export = "html"; |
---|
145 | break; |
---|
146 | case 142: /* --tga */ |
---|
147 | cx->export = "tga"; |
---|
148 | break; |
---|
149 | case '?': |
---|
150 | printf(MOREINFO, argv[0]); |
---|
151 | return 1; |
---|
152 | default: |
---|
153 | printf("%s: invalid option -- %i\n", argv[0], c); |
---|
154 | printf(MOREINFO, argv[0]); |
---|
155 | return 1; |
---|
156 | } |
---|
157 | } |
---|
158 | #else |
---|
159 | # define MOREINFO "Usage: %s message...\n" |
---|
160 | int optind = 1; |
---|
161 | #endif |
---|
162 | |
---|
163 | switch(infocode) |
---|
164 | { |
---|
165 | case -1: |
---|
166 | break; |
---|
167 | case 0: |
---|
168 | version(); |
---|
169 | return 0; |
---|
170 | case 1: |
---|
171 | printf("20201\n"); |
---|
172 | return 0; |
---|
173 | case 2: |
---|
174 | printf("%s\n", cx->dir); |
---|
175 | return 0; |
---|
176 | case 3: |
---|
177 | printf("%s\n", cx->font); |
---|
178 | return 0; |
---|
179 | case 4: |
---|
180 | printf("%u\n", cx->term_width); |
---|
181 | return 0; |
---|
182 | default: |
---|
183 | return 0; |
---|
184 | } |
---|
185 | |
---|
186 | if(!strcasecmp(cx->font, "mono9")) |
---|
187 | ret = init_big(cx); |
---|
188 | else if(!strcasecmp(cx->font, "term")) |
---|
189 | ret = init_tiny(cx); |
---|
190 | else |
---|
191 | ret = init_figlet(cx); |
---|
192 | |
---|
193 | if(ret) |
---|
194 | return -1; |
---|
195 | |
---|
196 | if(optind >= argc) |
---|
197 | { |
---|
198 | char buf[10]; |
---|
199 | unsigned int len; |
---|
200 | uint32_t ch; |
---|
201 | |
---|
202 | i = 0; |
---|
203 | |
---|
204 | /* Read from stdin */ |
---|
205 | while(!feof(stdin)) |
---|
206 | { |
---|
207 | buf[i++] = getchar(); |
---|
208 | buf[i] = '\0'; |
---|
209 | |
---|
210 | ch = cucul_utf8_to_utf32(buf, &len); |
---|
211 | |
---|
212 | if(!len) |
---|
213 | continue; |
---|
214 | |
---|
215 | cx->feed(cx, ch); |
---|
216 | i = 0; |
---|
217 | } |
---|
218 | } |
---|
219 | else for(i = optind; i < argc; i++) |
---|
220 | { |
---|
221 | /* Read from commandline */ |
---|
222 | unsigned int len; |
---|
223 | |
---|
224 | if(i != optind) |
---|
225 | cx->feed(cx, ' '); |
---|
226 | |
---|
227 | for(j = 0; argv[i][j];) |
---|
228 | { |
---|
229 | cx->feed(cx, cucul_utf8_to_utf32(argv[i] + j, &len)); |
---|
230 | j += len; |
---|
231 | } |
---|
232 | } |
---|
233 | |
---|
234 | cx->end(cx); |
---|
235 | |
---|
236 | /* Apply optional effects to our string */ |
---|
237 | filter_do(cx); |
---|
238 | |
---|
239 | /* Output char */ |
---|
240 | buffer = cucul_export_canvas(cx->cv, cx->export); |
---|
241 | fwrite(cucul_get_buffer_data(buffer), |
---|
242 | cucul_get_buffer_size(buffer), 1, stdout); |
---|
243 | cucul_free_buffer(buffer); |
---|
244 | |
---|
245 | cucul_free_canvas(cx->cv); |
---|
246 | |
---|
247 | return 0; |
---|
248 | } |
---|
249 | |
---|
250 | #if defined(HAVE_GETOPT_H) |
---|
251 | # define USAGE \ |
---|
252 | "Usage: toilet [ -htv ] [ -d fontdirectory ]\n" \ |
---|
253 | " [ -f fontfile ] [ -F filter ] [ -w outputwidth ]\n" \ |
---|
254 | " [ -I infocode ] [ message ]\n" |
---|
255 | #else |
---|
256 | # define USAGE "" |
---|
257 | #endif |
---|
258 | |
---|
259 | static void version(void) |
---|
260 | { |
---|
261 | printf( |
---|
262 | "TOIlet Copyright 2006 Sam Hocevar\n" |
---|
263 | "Internet: <sam@zoy.org> Version: %s, date: %s\n" |
---|
264 | "\n" |
---|
265 | "TOIlet, along with the various TOIlet fonts and documentation, may be\n" |
---|
266 | "freely copied and distributed.\n" |
---|
267 | "\n" |
---|
268 | "If you use TOIlet, please send an e-mail message to <sam@zoy.org>.\n" |
---|
269 | "\n" |
---|
270 | "The latest version of TOIlet is available from the web site,\n" |
---|
271 | " http://libcaca.zoy.org/toilet.html\n" |
---|
272 | "\n" |
---|
273 | USAGE, |
---|
274 | VERSION, DATE); |
---|
275 | } |
---|
276 | |
---|
277 | #if defined(HAVE_GETOPT_H) |
---|
278 | static void usage(void) |
---|
279 | { |
---|
280 | printf(USAGE); |
---|
281 | # ifdef HAVE_GETOPT_LONG |
---|
282 | printf(" -f, --font <name> select the font\n"); |
---|
283 | printf(" -d, --directory <dir> specify font directory\n"); |
---|
284 | printf(" -w, --width <width> set output width\n"); |
---|
285 | printf(" -t, --termwidth adapt to terminal's width\n"); |
---|
286 | printf(" -F, --filter <name> apply one or several filters to the text\n"); |
---|
287 | printf(" --gay rainbow filter (same as -F gay)\n"); |
---|
288 | printf(" --metal metal filter (same as -F metal)\n"); |
---|
289 | printf(" --irc output IRC colour codes\n"); |
---|
290 | printf(" --html output an HTML document\n"); |
---|
291 | printf(" --tga output a TGA image\n"); |
---|
292 | printf(" -h, --help display this help and exit\n"); |
---|
293 | printf(" -I, --infocode <code> print FIGlet-compatible infocode\n"); |
---|
294 | printf(" -v, --version output version information and exit\n"); |
---|
295 | # else |
---|
296 | printf(" -f <name> select the font\n"); |
---|
297 | printf(" -d <dir> specify font directory\n"); |
---|
298 | printf(" -w <width> set output width\n"); |
---|
299 | printf(" -t adapt to terminal's width\n"); |
---|
300 | printf(" -F <name> apply one or several filters to the text\n"); |
---|
301 | printf(" -h display this help and exit\n"); |
---|
302 | printf(" -I <code> print FIGlet-compatible infocode\n"); |
---|
303 | printf(" -v output version information and exit\n"); |
---|
304 | # endif |
---|
305 | } |
---|
306 | #endif |
---|
307 | |
---|