Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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