main.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version 2
  5. * of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. * Author: g0tsu
  12. * Email: g0tsu at dnmx.0rg
  13. */
  14. #include <stdio.h>
  15. #include <config.h>
  16. #include <libhighlight.h>
  17. #include <errno.h>
  18. #include <stdlib.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <unistd.h>
  22. #include <fcntl.h>
  23. #include <errno.h>
  24. #include <ctype.h>
  25. #include <string.h>
  26. enum {
  27. PRINT_TYPE_TERM,
  28. PRINT_TYPE_HTML
  29. };
  30. #ifdef CONFIG_USE_HTML
  31. static char * print_html(hl_root *root) {
  32. return hl_compile_html(root);
  33. }
  34. #endif
  35. static char * print_term(hl_root *root) {
  36. return hl_compile_term(root);
  37. }
  38. static void usage(char *prog) {
  39. fprintf(stdout, "Usage: %s <input file>\n", prog);
  40. fprintf(stdout, "\t-v\t\tversion information\n");
  41. fprintf(stdout, "\t-h\t\tthis help\n");
  42. #ifdef CONFIG_USE_HTML
  43. fprintf(stdout, "\t-t\t\thtml output\n");
  44. #endif
  45. exit(1);
  46. }
  47. int main(int ac, char **av) {
  48. int fd;
  49. struct stat fdstat;
  50. int ptype = PRINT_TYPE_TERM;
  51. char *filename = NULL;
  52. char *out;
  53. int opt;
  54. hl_root *root;
  55. char useopt = 0;
  56. while ((opt = getopt(ac, av, "vth")) != -1) {
  57. switch (opt) {
  58. case 'v':
  59. fprintf(stdout, "%s %s\n", av[0], VERSION);
  60. exit(0);
  61. break;
  62. case 'h':
  63. usage(av[0]);
  64. break;
  65. #ifdef CONFIG_USE_HTML
  66. case 't':
  67. ptype = PRINT_TYPE_HTML;
  68. useopt++;
  69. break;
  70. #endif
  71. default:
  72. break;
  73. }
  74. }
  75. if (ac > (1 + useopt)) {
  76. filename = strdup(av[ac - 1]);
  77. if (stat(filename, &fdstat) !=0 || (fdstat.st_mode & S_IFMT) != S_IFREG) {
  78. fprintf(stderr, "The %s is not a \"regular\" file\n", filename);
  79. return ENOENT;
  80. }
  81. if ((fd = open(filename, O_RDONLY)) < 0) {
  82. perror("open()");
  83. return ENOENT;
  84. }
  85. } else {
  86. fd = fileno(stdin);
  87. }
  88. if (!(root = hl_parser_file(fd, NULL))) {
  89. fprintf(stderr, "Unable to parse file\n");
  90. return ENOENT;
  91. }
  92. if (filename) {
  93. close(fd);
  94. free(filename);
  95. }
  96. if (ptype == PRINT_TYPE_HTML)
  97. out = print_html(root);
  98. else
  99. out = print_term(root);
  100. if (!out) {
  101. fprintf(stderr, "Unable to compile output\n");
  102. return EILSEQ;
  103. }
  104. hl_root_free(root);
  105. puts(out);
  106. free(out);
  107. return 0;
  108. }