You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

264 lines
7.5 KiB

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