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 1150 2006-09-30 17:53:59Z 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 | |
---|
39 | char const *toilet_export = "utf8"; |
---|
40 | char const *toilet_font = "mono9"; |
---|
41 | char const *toilet_dir = "/usr/share/figlet/"; |
---|
42 | |
---|
43 | static void version(void); |
---|
44 | #if defined(HAVE_GETOPT_H) |
---|
45 | static void usage(void); |
---|
46 | #endif |
---|
47 | |
---|
48 | int main(int argc, char *argv[]) |
---|
49 | { |
---|
50 | cucul_canvas_t *cv; |
---|
51 | cucul_buffer_t *buffer; |
---|
52 | |
---|
53 | uint32_t *string = NULL; |
---|
54 | unsigned int length; |
---|
55 | |
---|
56 | int i; |
---|
57 | |
---|
58 | unsigned int flag_gay = 0; |
---|
59 | unsigned int flag_metal = 0; |
---|
60 | unsigned int term_width = 80; |
---|
61 | int infocode = -1; |
---|
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 | toilet_font = optarg; |
---|
107 | break; |
---|
108 | case 'd': /* --directory */ |
---|
109 | toilet_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 | 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 | term_width = ws.ws_col; |
---|
129 | #endif |
---|
130 | break; |
---|
131 | } |
---|
132 | case 'i': /* --irc */ |
---|
133 | toilet_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", toilet_dir); |
---|
161 | return 0; |
---|
162 | case 3: |
---|
163 | printf("%s\n", toilet_font); |
---|
164 | return 0; |
---|
165 | case 4: |
---|
166 | printf("%u\n", term_width); |
---|
167 | return 0; |
---|
168 | default: |
---|
169 | return 0; |
---|
170 | } |
---|
171 | |
---|
172 | if(optind >= argc) |
---|
173 | { |
---|
174 | printf("%s: too few arguments\n", argv[0]); |
---|
175 | printf(MOREINFO, argv[0]); |
---|
176 | return 1; |
---|
177 | } |
---|
178 | |
---|
179 | /* Load rest of commandline into a UTF-32 string */ |
---|
180 | for(i = optind, length = 0; i < argc; i++) |
---|
181 | { |
---|
182 | unsigned int k, guessed_len, real_len; |
---|
183 | |
---|
184 | guessed_len = strlen(argv[i]); |
---|
185 | |
---|
186 | if(i > optind) |
---|
187 | string[length++] = (uint32_t)' '; |
---|
188 | |
---|
189 | string = realloc(string, (length + guessed_len + 1) * 4); |
---|
190 | |
---|
191 | for(k = 0, real_len = 0; k < guessed_len; real_len++) |
---|
192 | { |
---|
193 | unsigned int char_len; |
---|
194 | |
---|
195 | string[length + real_len] = |
---|
196 | cucul_utf8_to_utf32(argv[i] + k, &char_len); |
---|
197 | |
---|
198 | k += char_len; |
---|
199 | } |
---|
200 | |
---|
201 | length += real_len; |
---|
202 | } |
---|
203 | |
---|
204 | /* Render string to canvas */ |
---|
205 | if(!strcasecmp(toilet_font, "mono9")) |
---|
206 | { |
---|
207 | cv = render_big(string, length); |
---|
208 | filter_autocrop(cv); |
---|
209 | } |
---|
210 | else if(!strcasecmp(toilet_font, "term")) |
---|
211 | cv = render_tiny(string, length); |
---|
212 | else |
---|
213 | cv = render_figlet(string, length); |
---|
214 | |
---|
215 | free(string); |
---|
216 | |
---|
217 | if(!cv) |
---|
218 | return -1; |
---|
219 | |
---|
220 | /* Apply optional effects to our string */ |
---|
221 | if(flag_metal) |
---|
222 | filter_metal(cv); |
---|
223 | if(flag_gay) |
---|
224 | filter_gay(cv); |
---|
225 | |
---|
226 | /* Output char */ |
---|
227 | buffer = cucul_export_canvas(cv, toilet_export); |
---|
228 | fwrite(cucul_get_buffer_data(buffer), |
---|
229 | cucul_get_buffer_size(buffer), 1, stdout); |
---|
230 | cucul_free_buffer(buffer); |
---|
231 | |
---|
232 | cucul_free_canvas(cv); |
---|
233 | |
---|
234 | return 0; |
---|
235 | } |
---|
236 | |
---|
237 | static void version(void) |
---|
238 | { |
---|
239 | printf("TOIlet Copyright 2006 Sam Hocevar %s\n", VERSION); |
---|
240 | printf("Internet: <sam@zoy.org> Version: 0, date: 21 Sep 2006\n"); |
---|
241 | printf("\n"); |
---|
242 | } |
---|
243 | |
---|
244 | #if defined(HAVE_GETOPT_H) |
---|
245 | static void usage(void) |
---|
246 | { |
---|
247 | printf("Usage: toilet [ -ghimtv ] [ -d fontdirectory ]\n"); |
---|
248 | printf(" [ -f fontfile ] [ -w outputwidth ]\n"); |
---|
249 | printf(" [ -I infocode ] [ message ]\n"); |
---|
250 | # ifdef HAVE_GETOPT_LONG |
---|
251 | printf(" -f, --font <fontfile> select the font\n"); |
---|
252 | printf(" -d, --directory <dir> specify font directory\n"); |
---|
253 | printf(" -w, --width <width> set output width\n"); |
---|
254 | printf(" -t, --termwidth adapt to terminal's width\n"); |
---|
255 | printf(" -g, --gay add a rainbow effect to the text\n"); |
---|
256 | printf(" -m, --metal add a metal effect to the text\n"); |
---|
257 | printf(" -i, --irc output IRC colour codes\n"); |
---|
258 | printf(" -h, --help display this help and exit\n"); |
---|
259 | printf(" -I, --infocode print FIGlet-compatible infocode\n"); |
---|
260 | printf(" -v, --version output version information and exit\n"); |
---|
261 | # else |
---|
262 | printf(" -f <fontfile> select the font\n"); |
---|
263 | printf(" -d <dir> specify font directory\n"); |
---|
264 | printf(" -w <width> set output width\n"); |
---|
265 | printf(" -t adapt to terminal's width\n"); |
---|
266 | printf(" -g add a rainbow effect to the text\n"); |
---|
267 | printf(" -m add a metal effect to the text\n"); |
---|
268 | printf(" -i output IRC colour codes\n"); |
---|
269 | printf(" -h display this help and exit\n"); |
---|
270 | printf(" -I print FIGlet-compatible infocode\n"); |
---|
271 | printf(" -v output version information and exit\n"); |
---|
272 | # endif |
---|
273 | } |
---|
274 | #endif |
---|
275 | |
---|