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 1293 2006-11-06 01:13:38Z 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 <stdlib.h> |
---|
31 | #include <cucul.h> |
---|
32 | |
---|
33 | #include "toilet.h" |
---|
34 | #include "render.h" |
---|
35 | #include "filter.h" |
---|
36 | |
---|
37 | static void version(void); |
---|
38 | #if defined(HAVE_GETOPT_H) |
---|
39 | static void usage(void); |
---|
40 | #endif |
---|
41 | |
---|
42 | int main(int argc, char *argv[]) |
---|
43 | { |
---|
44 | context_t struct_cx; |
---|
45 | context_t *cx = &struct_cx; |
---|
46 | |
---|
47 | int infocode = -1; |
---|
48 | |
---|
49 | cx->export = "utf8"; |
---|
50 | cx->font = "smblock"; |
---|
51 | cx->dir = "/usr/share/figlet/"; |
---|
52 | |
---|
53 | cx->term_width = 80; |
---|
54 | |
---|
55 | cx->filters = NULL; |
---|
56 | cx->nfilters = 0; |
---|
57 | |
---|
58 | #if defined(HAVE_GETOPT_H) |
---|
59 | for(;;) |
---|
60 | { |
---|
61 | # ifdef HAVE_GETOPT_LONG |
---|
62 | # define MOREINFO "Try `%s --help' for more information.\n" |
---|
63 | int option_index = 0; |
---|
64 | static struct option long_options[] = |
---|
65 | { |
---|
66 | /* Long option, needs arg, flag, short option */ |
---|
67 | { "font", 1, NULL, 'f' }, |
---|
68 | { "directory", 1, NULL, 'd' }, |
---|
69 | { "width", 1, NULL, 'w' }, |
---|
70 | { "termwidth", 0, NULL, 't' }, |
---|
71 | { "filter", 1, NULL, 'F' }, |
---|
72 | { "gay", 0, NULL, 130 }, |
---|
73 | { "metal", 0, NULL, 131 }, |
---|
74 | { "irc", 0, NULL, 140 }, |
---|
75 | { "html", 0, NULL, 141 }, |
---|
76 | { "tga", 0, NULL, 142 }, |
---|
77 | { "help", 0, NULL, 'h' }, |
---|
78 | { "infocode", 1, NULL, 'I' }, |
---|
79 | { "version", 0, NULL, 'v' }, |
---|
80 | { NULL, 0, NULL, 0 } |
---|
81 | }; |
---|
82 | |
---|
83 | int c = getopt_long(argc, argv, "f:d:w:tF:hI:v", |
---|
84 | long_options, &option_index); |
---|
85 | # else |
---|
86 | # define MOREINFO "Try `%s -h' for more information.\n" |
---|
87 | int c = getopt(argc, argv, "f:d:w:tF:hI:v"); |
---|
88 | # endif |
---|
89 | if(c == -1) |
---|
90 | break; |
---|
91 | |
---|
92 | switch(c) |
---|
93 | { |
---|
94 | case 'h': /* --help */ |
---|
95 | usage(); |
---|
96 | return 0; |
---|
97 | case 'I': /* --infocode */ |
---|
98 | infocode = atoi(optarg); |
---|
99 | break; |
---|
100 | case 'v': /* --version */ |
---|
101 | version(); |
---|
102 | return 0; |
---|
103 | case 'f': /* --font */ |
---|
104 | cx->font = optarg; |
---|
105 | break; |
---|
106 | case 'd': /* --directory */ |
---|
107 | cx->dir = optarg; |
---|
108 | break; |
---|
109 | case 'F': /* --filter */ |
---|
110 | if(filter_add(cx, optarg)) |
---|
111 | return -1; |
---|
112 | break; |
---|
113 | case 130: /* --gay */ |
---|
114 | filter_add(cx, "gay"); |
---|
115 | break; |
---|
116 | case 131: /* --metal */ |
---|
117 | filter_add(cx, "metal"); |
---|
118 | break; |
---|
119 | case 'w': /* --width */ |
---|
120 | cx->term_width = atoi(optarg); |
---|
121 | break; |
---|
122 | case 't': /* --termwidth */ |
---|
123 | { |
---|
124 | #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_TIOCGWINSZ) |
---|
125 | struct winsize ws; |
---|
126 | |
---|
127 | if((ioctl(1, TIOCGWINSZ, &ws) != -1 || |
---|
128 | ioctl(2, TIOCGWINSZ, &ws) != -1 || |
---|
129 | ioctl(0, TIOCGWINSZ, &ws) != -1) && ws.ws_col != 0) |
---|
130 | cx->term_width = ws.ws_col; |
---|
131 | #endif |
---|
132 | break; |
---|
133 | } |
---|
134 | case 140: /* --irc */ |
---|
135 | cx->export = "irc"; |
---|
136 | break; |
---|
137 | case 141: /* --html */ |
---|
138 | cx->export = "html"; |
---|
139 | break; |
---|
140 | case 142: /* --tga */ |
---|
141 | cx->export = "tga"; |
---|
142 | break; |
---|
143 | case '?': |
---|
144 | printf(MOREINFO, argv[0]); |
---|
145 | return 1; |
---|
146 | default: |
---|
147 | printf("%s: invalid option -- %i\n", argv[0], c); |
---|
148 | printf(MOREINFO, argv[0]); |
---|
149 | return 1; |
---|
150 | } |
---|
151 | } |
---|
152 | #else |
---|
153 | # define MOREINFO "Usage: %s message...\n" |
---|
154 | int optind = 1; |
---|
155 | #endif |
---|
156 | |
---|
157 | switch(infocode) |
---|
158 | { |
---|
159 | case -1: |
---|
160 | break; |
---|
161 | case 0: |
---|
162 | version(); |
---|
163 | return 0; |
---|
164 | case 1: |
---|
165 | printf("20201\n"); |
---|
166 | return 0; |
---|
167 | case 2: |
---|
168 | printf("%s\n", cx->dir); |
---|
169 | return 0; |
---|
170 | case 3: |
---|
171 | printf("%s\n", cx->font); |
---|
172 | return 0; |
---|
173 | case 4: |
---|
174 | printf("%u\n", cx->term_width); |
---|
175 | return 0; |
---|
176 | default: |
---|
177 | return 0; |
---|
178 | } |
---|
179 | |
---|
180 | if(render_init(cx) < 0) |
---|
181 | return -1; |
---|
182 | |
---|
183 | if(optind >= argc) |
---|
184 | render_stdin(cx); |
---|
185 | else |
---|
186 | render_list(cx, argc - optind, argv + optind); |
---|
187 | |
---|
188 | render_end(cx); |
---|
189 | filter_end(cx); |
---|
190 | |
---|
191 | return 0; |
---|
192 | } |
---|
193 | |
---|
194 | #if defined(HAVE_GETOPT_H) |
---|
195 | # define USAGE \ |
---|
196 | "Usage: toilet [ -htv ] [ -d fontdirectory ]\n" \ |
---|
197 | " [ -f fontfile ] [ -F filter ] [ -w outputwidth ]\n" \ |
---|
198 | " [ -I infocode ] [ message ]\n" |
---|
199 | #else |
---|
200 | # define USAGE "" |
---|
201 | #endif |
---|
202 | |
---|
203 | static void version(void) |
---|
204 | { |
---|
205 | printf( |
---|
206 | "TOIlet Copyright 2006 Sam Hocevar\n" |
---|
207 | "Internet: <sam@zoy.org> Version: %s, date: %s\n" |
---|
208 | "\n" |
---|
209 | "TOIlet, along with the various TOIlet fonts and documentation, may be\n" |
---|
210 | "freely copied and distributed.\n" |
---|
211 | "\n" |
---|
212 | "If you use TOIlet, please send an e-mail message to <sam@zoy.org>.\n" |
---|
213 | "\n" |
---|
214 | "The latest version of TOIlet is available from the web site,\n" |
---|
215 | " http://libcaca.zoy.org/toilet.html\n" |
---|
216 | "\n" |
---|
217 | USAGE, |
---|
218 | VERSION, DATE); |
---|
219 | } |
---|
220 | |
---|
221 | #if defined(HAVE_GETOPT_H) |
---|
222 | static void usage(void) |
---|
223 | { |
---|
224 | printf(USAGE); |
---|
225 | # ifdef HAVE_GETOPT_LONG |
---|
226 | printf(" -f, --font <name> select the font\n"); |
---|
227 | printf(" -d, --directory <dir> specify font directory\n"); |
---|
228 | printf(" -w, --width <width> set output width\n"); |
---|
229 | printf(" -t, --termwidth adapt to terminal's width\n"); |
---|
230 | printf(" -F, --filter <name> apply one or several filters to the text\n"); |
---|
231 | printf(" --gay rainbow filter (same as -F gay)\n"); |
---|
232 | printf(" --metal metal filter (same as -F metal)\n"); |
---|
233 | printf(" --irc output IRC colour codes\n"); |
---|
234 | printf(" --html output an HTML document\n"); |
---|
235 | printf(" --tga output a TGA image\n"); |
---|
236 | printf(" -h, --help display this help and exit\n"); |
---|
237 | printf(" -I, --infocode <code> print FIGlet-compatible infocode\n"); |
---|
238 | printf(" -v, --version output version information and exit\n"); |
---|
239 | # else |
---|
240 | printf(" -f <name> select the font\n"); |
---|
241 | printf(" -d <dir> specify font directory\n"); |
---|
242 | printf(" -w <width> set output width\n"); |
---|
243 | printf(" -t adapt to terminal's width\n"); |
---|
244 | printf(" -F <name> apply one or several filters to the text\n"); |
---|
245 | printf(" -h display this help and exit\n"); |
---|
246 | printf(" -I <code> print FIGlet-compatible infocode\n"); |
---|
247 | printf(" -v output version information and exit\n"); |
---|
248 | # endif |
---|
249 | } |
---|
250 | #endif |
---|
251 | |
---|