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 1101 2006-09-23 22:53:17Z 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 | #include "config.h" |
---|
15 | |
---|
16 | #if defined(HAVE_INTTYPES_H) |
---|
17 | # include <inttypes.h> |
---|
18 | #endif |
---|
19 | #if defined(HAVE_GETOPT_H) |
---|
20 | # include <getopt.h> |
---|
21 | #endif |
---|
22 | #include <stdio.h> |
---|
23 | #include <string.h> |
---|
24 | #include <stdlib.h> |
---|
25 | #include <cucul.h> |
---|
26 | |
---|
27 | #include "render.h" |
---|
28 | #include "figlet.h" |
---|
29 | #include "filters.h" |
---|
30 | |
---|
31 | int main(int argc, char *argv[]) |
---|
32 | { |
---|
33 | cucul_canvas_t *cv; |
---|
34 | cucul_buffer_t *buffer; |
---|
35 | |
---|
36 | uint32_t *string = NULL; |
---|
37 | unsigned int length; |
---|
38 | |
---|
39 | int i; |
---|
40 | |
---|
41 | char const *export = "utf8"; |
---|
42 | char const *font = "mono9"; |
---|
43 | unsigned flag_gay = 0; |
---|
44 | unsigned flag_metal = 0; |
---|
45 | |
---|
46 | #if defined(HAVE_GETOPT_H) |
---|
47 | for(;;) |
---|
48 | { |
---|
49 | # ifdef HAVE_GETOPT_LONG |
---|
50 | # define MOREINFO "Try `%s --help' for more information.\n" |
---|
51 | int option_index = 0; |
---|
52 | static struct option long_options[] = |
---|
53 | { |
---|
54 | /* Long option, needs arg, flag, short option */ |
---|
55 | { "font", 1, NULL, 'f' }, |
---|
56 | { "gay", 0, NULL, 'g' }, |
---|
57 | { "metal", 0, NULL, 'm' }, |
---|
58 | { "irc", 0, NULL, 'i' }, |
---|
59 | { "help", 0, NULL, 'h' }, |
---|
60 | { "version", 0, NULL, 'v' }, |
---|
61 | { NULL, 0, NULL, 0 } |
---|
62 | }; |
---|
63 | |
---|
64 | int c = getopt_long(argc, argv, "f:gmihv", long_options, &option_index); |
---|
65 | # else |
---|
66 | # define MOREINFO "Try `%s -h' for more information.\n" |
---|
67 | int c = getopt(argc, argv, "f:gmihv"); |
---|
68 | # endif |
---|
69 | if(c == -1) |
---|
70 | break; |
---|
71 | |
---|
72 | switch(c) |
---|
73 | { |
---|
74 | case 'h': /* --help */ |
---|
75 | printf("Usage: %s [ -f:gmihv ] [ message ]\n", argv[0]); |
---|
76 | # ifdef HAVE_GETOPT_LONG |
---|
77 | printf(" -f, --font <fontfile>\n"); |
---|
78 | printf(" select the font\n"); |
---|
79 | printf(" -g, --gay add a rainbow effect to the text\n"); |
---|
80 | printf(" -m, --metal add a metal effect to the text\n"); |
---|
81 | printf(" -i, --irc output IRC colour codes\n"); |
---|
82 | printf(" -h, --help display this help and exit\n"); |
---|
83 | printf(" -v, --version output version information and exit\n"); |
---|
84 | # else |
---|
85 | printf(" -f <fontfile>\n"); |
---|
86 | printf(" select the font\n"); |
---|
87 | printf(" -g add a rainbow effect to the text\n"); |
---|
88 | printf(" -m add a metal effect to the text\n"); |
---|
89 | printf(" -i output IRC colour codes\n"); |
---|
90 | printf(" -h display this help and exit\n"); |
---|
91 | printf(" -v output version information and exit\n"); |
---|
92 | # endif |
---|
93 | return 0; |
---|
94 | case 'v': /* --version */ |
---|
95 | printf("TOIlet Copyright 2006 Sam Hocevar %s\n", VERSION); |
---|
96 | printf("Internet: <sam@zoy.org> Version: 0, date: 21 Sep 2006\n"); |
---|
97 | printf("\n"); |
---|
98 | return 0; |
---|
99 | case 'f': /* --font */ |
---|
100 | font = optarg; |
---|
101 | break; |
---|
102 | case 'g': /* --gay */ |
---|
103 | flag_gay = 1; |
---|
104 | break; |
---|
105 | case 'm': /* --metal */ |
---|
106 | flag_metal = 1; |
---|
107 | break; |
---|
108 | case 'i': /* --irc */ |
---|
109 | export = "irc"; |
---|
110 | break; |
---|
111 | case '?': |
---|
112 | printf(MOREINFO, argv[0]); |
---|
113 | return 1; |
---|
114 | default: |
---|
115 | printf("%s: invalid option -- %i\n", argv[0], c); |
---|
116 | printf(MOREINFO, argv[0]); |
---|
117 | return 1; |
---|
118 | } |
---|
119 | } |
---|
120 | #else |
---|
121 | # define MOREINFO "Usage: %s message...\n" |
---|
122 | int optind = 1; |
---|
123 | #endif |
---|
124 | |
---|
125 | if(optind >= argc) |
---|
126 | { |
---|
127 | printf("%s: too few arguments\n", argv[0]); |
---|
128 | printf(MOREINFO, argv[0]); |
---|
129 | return 1; |
---|
130 | } |
---|
131 | |
---|
132 | /* Load rest of commandline into a UTF-32 string */ |
---|
133 | for(i = optind, length = 0; i < argc; i++) |
---|
134 | { |
---|
135 | unsigned int k, guessed_len, real_len; |
---|
136 | |
---|
137 | guessed_len = strlen(argv[i]); |
---|
138 | |
---|
139 | if(i > optind) |
---|
140 | string[length++] = (uint32_t)' '; |
---|
141 | |
---|
142 | string = realloc(string, (length + guessed_len + 1) * 4); |
---|
143 | |
---|
144 | for(k = 0, real_len = 0; k < guessed_len; real_len++) |
---|
145 | { |
---|
146 | unsigned int char_len; |
---|
147 | |
---|
148 | string[length + real_len] = |
---|
149 | cucul_utf8_to_utf32(argv[i] + k, &char_len); |
---|
150 | |
---|
151 | k += char_len; |
---|
152 | } |
---|
153 | |
---|
154 | length += real_len; |
---|
155 | } |
---|
156 | |
---|
157 | /* Render string to canvas */ |
---|
158 | if(!strcasecmp(font, "mono9")) |
---|
159 | cv = render_big(string, length); |
---|
160 | else if(!strcasecmp(font, "term")) |
---|
161 | cv = render_tiny(string, length); |
---|
162 | else |
---|
163 | cv = render_figlet(string, length, font); |
---|
164 | |
---|
165 | /* Crop output */ |
---|
166 | filter_autocrop(cv); |
---|
167 | |
---|
168 | /* Do gay stuff with our string (léopard) */ |
---|
169 | if(flag_metal) |
---|
170 | filter_metal(cv); |
---|
171 | if(flag_gay) |
---|
172 | filter_gay(cv); |
---|
173 | |
---|
174 | /* Output char */ |
---|
175 | buffer = cucul_export_canvas(cv, export); |
---|
176 | fwrite(cucul_get_buffer_data(buffer), |
---|
177 | cucul_get_buffer_size(buffer), 1, stdout); |
---|
178 | cucul_free_buffer(buffer); |
---|
179 | |
---|
180 | cucul_free_canvas(cv); |
---|
181 | |
---|
182 | return 0; |
---|
183 | } |
---|
184 | |
---|