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