[1084] | 1 | /* |
---|
[1087] | 2 | * TOIlet The Other Implementation’s letters |
---|
| 3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
---|
| 4 | * All Rights Reserved |
---|
[1084] | 5 | * |
---|
[1087] | 6 | * $Id: main.c 1319 2006-11-10 07:47:17Z sam $ |
---|
[1084] | 7 | * |
---|
[1087] | 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. |
---|
[1084] | 12 | */ |
---|
| 13 | |
---|
[1102] | 14 | /* |
---|
| 15 | * This is the main program entry point. |
---|
| 16 | */ |
---|
| 17 | |
---|
[1084] | 18 | #include "config.h" |
---|
| 19 | |
---|
[1087] | 20 | #if defined(HAVE_INTTYPES_H) |
---|
| 21 | # include <inttypes.h> |
---|
| 22 | #endif |
---|
| 23 | #if defined(HAVE_GETOPT_H) |
---|
| 24 | # include <getopt.h> |
---|
| 25 | #endif |
---|
[1197] | 26 | #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_TIOCGWINSZ) |
---|
[1116] | 27 | # include <sys/ioctl.h> |
---|
| 28 | #endif |
---|
[1087] | 29 | #include <stdio.h> |
---|
| 30 | #include <stdlib.h> |
---|
[1319] | 31 | #include <string.h> |
---|
[1100] | 32 | #include <cucul.h> |
---|
[1084] | 33 | |
---|
[1143] | 34 | #include "toilet.h" |
---|
[1100] | 35 | #include "render.h" |
---|
[1227] | 36 | #include "filter.h" |
---|
[1087] | 37 | |
---|
[1114] | 38 | static void version(void); |
---|
| 39 | #if defined(HAVE_GETOPT_H) |
---|
| 40 | static void usage(void); |
---|
| 41 | #endif |
---|
[1319] | 42 | static int export_list(void); |
---|
[1114] | 43 | |
---|
[1087] | 44 | int main(int argc, char *argv[]) |
---|
[1084] | 45 | { |
---|
[1193] | 46 | context_t struct_cx; |
---|
| 47 | context_t *cx = &struct_cx; |
---|
| 48 | |
---|
[1114] | 49 | int infocode = -1; |
---|
[1087] | 50 | |
---|
[1193] | 51 | cx->export = "utf8"; |
---|
[1293] | 52 | cx->font = "smblock"; |
---|
[1193] | 53 | cx->dir = "/usr/share/figlet/"; |
---|
| 54 | |
---|
| 55 | cx->term_width = 80; |
---|
| 56 | |
---|
[1228] | 57 | cx->filters = NULL; |
---|
| 58 | cx->nfilters = 0; |
---|
| 59 | |
---|
[1087] | 60 | #if defined(HAVE_GETOPT_H) |
---|
| 61 | for(;;) |
---|
| 62 | { |
---|
| 63 | # ifdef HAVE_GETOPT_LONG |
---|
| 64 | # define MOREINFO "Try `%s --help' for more information.\n" |
---|
| 65 | int option_index = 0; |
---|
| 66 | static struct option long_options[] = |
---|
| 67 | { |
---|
| 68 | /* Long option, needs arg, flag, short option */ |
---|
[1101] | 69 | { "font", 1, NULL, 'f' }, |
---|
[1115] | 70 | { "directory", 1, NULL, 'd' }, |
---|
[1114] | 71 | { "width", 1, NULL, 'w' }, |
---|
[1116] | 72 | { "termwidth", 0, NULL, 't' }, |
---|
[1228] | 73 | { "filter", 1, NULL, 'F' }, |
---|
[1240] | 74 | { "gay", 0, NULL, 130 }, |
---|
| 75 | { "metal", 0, NULL, 131 }, |
---|
[1319] | 76 | { "export", 1, NULL, 'E' }, |
---|
[1240] | 77 | { "irc", 0, NULL, 140 }, |
---|
| 78 | { "html", 0, NULL, 141 }, |
---|
[1087] | 79 | { "help", 0, NULL, 'h' }, |
---|
[1114] | 80 | { "infocode", 1, NULL, 'I' }, |
---|
[1087] | 81 | { "version", 0, NULL, 'v' }, |
---|
| 82 | { NULL, 0, NULL, 0 } |
---|
| 83 | }; |
---|
| 84 | |
---|
[1319] | 85 | int c = getopt_long(argc, argv, "f:d:w:tF:E:hI:v", |
---|
[1114] | 86 | long_options, &option_index); |
---|
[1087] | 87 | # else |
---|
| 88 | # define MOREINFO "Try `%s -h' for more information.\n" |
---|
[1319] | 89 | int c = getopt(argc, argv, "f:d:w:tF:E:hI:v"); |
---|
[1087] | 90 | # endif |
---|
| 91 | if(c == -1) |
---|
| 92 | break; |
---|
| 93 | |
---|
| 94 | switch(c) |
---|
| 95 | { |
---|
| 96 | case 'h': /* --help */ |
---|
[1114] | 97 | usage(); |
---|
[1087] | 98 | return 0; |
---|
[1114] | 99 | case 'I': /* --infocode */ |
---|
| 100 | infocode = atoi(optarg); |
---|
| 101 | break; |
---|
[1101] | 102 | case 'v': /* --version */ |
---|
[1114] | 103 | version(); |
---|
[1101] | 104 | return 0; |
---|
| 105 | case 'f': /* --font */ |
---|
[1193] | 106 | cx->font = optarg; |
---|
[1099] | 107 | break; |
---|
[1115] | 108 | case 'd': /* --directory */ |
---|
[1193] | 109 | cx->dir = optarg; |
---|
[1115] | 110 | break; |
---|
[1228] | 111 | case 'F': /* --filter */ |
---|
[1319] | 112 | if(!strcmp(optarg, "list")) |
---|
| 113 | return filter_list(); |
---|
| 114 | if(filter_add(cx, optarg) < 0) |
---|
[1228] | 115 | return -1; |
---|
| 116 | break; |
---|
[1240] | 117 | case 130: /* --gay */ |
---|
[1228] | 118 | filter_add(cx, "gay"); |
---|
[1087] | 119 | break; |
---|
[1240] | 120 | case 131: /* --metal */ |
---|
[1228] | 121 | filter_add(cx, "metal"); |
---|
[1101] | 122 | break; |
---|
[1114] | 123 | case 'w': /* --width */ |
---|
[1193] | 124 | cx->term_width = atoi(optarg); |
---|
[1114] | 125 | break; |
---|
[1116] | 126 | case 't': /* --termwidth */ |
---|
| 127 | { |
---|
[1197] | 128 | #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_TIOCGWINSZ) |
---|
[1116] | 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) |
---|
[1193] | 134 | cx->term_width = ws.ws_col; |
---|
[1116] | 135 | #endif |
---|
| 136 | break; |
---|
| 137 | } |
---|
[1319] | 138 | case 'E': /* --export */ |
---|
| 139 | if(!strcmp(optarg, "list")) |
---|
| 140 | return export_list(); |
---|
| 141 | cx->export = optarg; |
---|
| 142 | break; |
---|
[1240] | 143 | case 140: /* --irc */ |
---|
[1193] | 144 | cx->export = "irc"; |
---|
[1087] | 145 | break; |
---|
[1240] | 146 | case 141: /* --html */ |
---|
| 147 | cx->export = "html"; |
---|
| 148 | break; |
---|
[1087] | 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 | |
---|
[1114] | 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: |
---|
[1193] | 174 | printf("%s\n", cx->dir); |
---|
[1114] | 175 | return 0; |
---|
| 176 | case 3: |
---|
[1193] | 177 | printf("%s\n", cx->font); |
---|
[1114] | 178 | return 0; |
---|
| 179 | case 4: |
---|
[1193] | 180 | printf("%u\n", cx->term_width); |
---|
[1114] | 181 | return 0; |
---|
| 182 | default: |
---|
| 183 | return 0; |
---|
| 184 | } |
---|
| 185 | |
---|
[1241] | 186 | if(render_init(cx) < 0) |
---|
[1199] | 187 | return -1; |
---|
| 188 | |
---|
[1193] | 189 | if(optind >= argc) |
---|
[1241] | 190 | render_stdin(cx); |
---|
| 191 | else |
---|
| 192 | render_list(cx, argc - optind, argv + optind); |
---|
[1087] | 193 | |
---|
[1241] | 194 | render_end(cx); |
---|
| 195 | filter_end(cx); |
---|
[1087] | 196 | |
---|
[1084] | 197 | return 0; |
---|
| 198 | } |
---|
| 199 | |
---|
[1193] | 200 | #if defined(HAVE_GETOPT_H) |
---|
| 201 | # define USAGE \ |
---|
[1240] | 202 | "Usage: toilet [ -htv ] [ -d fontdirectory ]\n" \ |
---|
| 203 | " [ -f fontfile ] [ -F filter ] [ -w outputwidth ]\n" \ |
---|
[1319] | 204 | " [ -I infocode ] [ -E format ] [ message ]\n" |
---|
[1193] | 205 | #else |
---|
| 206 | # define USAGE "" |
---|
| 207 | #endif |
---|
| 208 | |
---|
[1114] | 209 | static void version(void) |
---|
| 210 | { |
---|
[1193] | 211 | printf( |
---|
| 212 | "TOIlet Copyright 2006 Sam Hocevar\n" |
---|
| 213 | "Internet: <sam@zoy.org> Version: %s, date: %s\n" |
---|
| 214 | "\n" |
---|
| 215 | "TOIlet, along with the various TOIlet fonts and documentation, may be\n" |
---|
| 216 | "freely copied and distributed.\n" |
---|
| 217 | "\n" |
---|
| 218 | "If you use TOIlet, please send an e-mail message to <sam@zoy.org>.\n" |
---|
| 219 | "\n" |
---|
| 220 | "The latest version of TOIlet is available from the web site,\n" |
---|
| 221 | " http://libcaca.zoy.org/toilet.html\n" |
---|
| 222 | "\n" |
---|
| 223 | USAGE, |
---|
| 224 | VERSION, DATE); |
---|
[1114] | 225 | } |
---|
| 226 | |
---|
| 227 | #if defined(HAVE_GETOPT_H) |
---|
| 228 | static void usage(void) |
---|
| 229 | { |
---|
[1193] | 230 | printf(USAGE); |
---|
[1114] | 231 | # ifdef HAVE_GETOPT_LONG |
---|
[1240] | 232 | printf(" -f, --font <name> select the font\n"); |
---|
[1115] | 233 | printf(" -d, --directory <dir> specify font directory\n"); |
---|
[1114] | 234 | printf(" -w, --width <width> set output width\n"); |
---|
[1116] | 235 | printf(" -t, --termwidth adapt to terminal's width\n"); |
---|
[1319] | 236 | printf(" -F, --filter <filters> apply one or several filters to the text\n"); |
---|
| 237 | printf(" -F, --filter list list available filters\n"); |
---|
[1240] | 238 | printf(" --gay rainbow filter (same as -F gay)\n"); |
---|
| 239 | printf(" --metal metal filter (same as -F metal)\n"); |
---|
[1319] | 240 | printf(" -E, --export <format> select export format\n"); |
---|
| 241 | printf(" -E, --export list list available export formats\n"); |
---|
| 242 | printf(" --irc output IRC colour codes (same as -E irc)\n"); |
---|
| 243 | printf(" --html output an HTML document (same as -E html)\n"); |
---|
[1114] | 244 | printf(" -h, --help display this help and exit\n"); |
---|
[1240] | 245 | printf(" -I, --infocode <code> print FIGlet-compatible infocode\n"); |
---|
[1114] | 246 | printf(" -v, --version output version information and exit\n"); |
---|
| 247 | # else |
---|
[1240] | 248 | printf(" -f <name> select the font\n"); |
---|
[1115] | 249 | printf(" -d <dir> specify font directory\n"); |
---|
[1114] | 250 | printf(" -w <width> set output width\n"); |
---|
[1116] | 251 | printf(" -t adapt to terminal's width\n"); |
---|
[1319] | 252 | printf(" -F <filters> apply one or several filters to the text\n"); |
---|
| 253 | printf(" -F list list available filters\n"); |
---|
| 254 | printf(" -E <format> select export format\n"); |
---|
| 255 | printf(" -E list list available export formats\n"); |
---|
[1114] | 256 | printf(" -h display this help and exit\n"); |
---|
[1240] | 257 | printf(" -I <code> print FIGlet-compatible infocode\n"); |
---|
[1114] | 258 | printf(" -v output version information and exit\n"); |
---|
| 259 | # endif |
---|
| 260 | } |
---|
| 261 | #endif |
---|
| 262 | |
---|
[1319] | 263 | static int export_list(void) |
---|
| 264 | { |
---|
| 265 | char const * const * exports, * const * p; |
---|
| 266 | |
---|
| 267 | exports = cucul_get_export_list(); |
---|
| 268 | |
---|
| 269 | printf("Available export formats:\n"); |
---|
| 270 | for(p = exports; *p; p += 2) |
---|
| 271 | printf("\"%s\": %s\n", *p, *(p + 1)); |
---|
| 272 | |
---|
| 273 | return 0; |
---|
| 274 | } |
---|
| 275 | |
---|