aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: be3d305b3480c531a9053c88397580609981c7e8 (plain)
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include "config.h"
  6. #include "cmark.h"
  7. #include "debug.h"
  8. #include "bench.h"
  9. typedef enum {
  10. FORMAT_NONE,
  11. FORMAT_HTML,
  12. FORMAT_MAN,
  13. FORMAT_AST
  14. } writer_format;
  15. void print_usage()
  16. {
  17. printf("Usage: cmark [FILE*]\n");
  18. printf("Options:\n");
  19. printf(" --to, -t FORMAT Specify output format (html, man, ast)\n");
  20. printf(" --help, -h Print usage information\n");
  21. printf(" --version Print version\n");
  22. }
  23. static void print_document(cmark_node *document, writer_format writer)
  24. {
  25. char *result;
  26. switch (writer) {
  27. case FORMAT_AST:
  28. result = cmark_render_ast(document);
  29. break;
  30. case FORMAT_HTML:
  31. result = cmark_render_html(document);
  32. break;
  33. case FORMAT_MAN:
  34. result = cmark_render_man(document);
  35. break;
  36. default:
  37. fprintf(stderr, "Unknown format %d\n", writer);
  38. exit(1);
  39. }
  40. printf("%s", result);
  41. free(result);
  42. }
  43. int main(int argc, char *argv[])
  44. {
  45. int i, numfps = 0;
  46. int *files;
  47. char buffer[4096];
  48. cmark_parser *parser;
  49. size_t bytes;
  50. cmark_node *document;
  51. writer_format writer = FORMAT_HTML;
  52. parser = cmark_parser_new();
  53. files = (int *)malloc(argc * sizeof(*files));
  54. for (i = 1; i < argc; i++) {
  55. if (strcmp(argv[i], "--version") == 0) {
  56. printf("cmark %s", CMARK_VERSION);
  57. printf(" - CommonMark converter (c) 2014 John MacFarlane\n");
  58. exit(0);
  59. } else if ((strcmp(argv[i], "--help") == 0) ||
  60. (strcmp(argv[i], "-h") == 0)) {
  61. print_usage();
  62. exit(0);
  63. } else if ((strcmp(argv[i], "-t") == 0) ||
  64. (strcmp(argv[i], "--to") == 0)) {
  65. i += 1;
  66. if (i < argc) {
  67. if (strcmp(argv[i], "man") == 0) {
  68. writer = FORMAT_MAN;
  69. } else if (strcmp(argv[i], "html") == 0) {
  70. writer = FORMAT_HTML;
  71. } else if (strcmp(argv[i], "ast") == 0) {
  72. writer = FORMAT_AST;
  73. } else {
  74. fprintf(stderr,
  75. "Unknown format %s\n", argv[i]);
  76. exit(1);
  77. }
  78. } else {
  79. fprintf(stderr, "No argument provided for %s\n",
  80. argv[i - 1]);
  81. exit(1);
  82. }
  83. } else if (*argv[i] == '-') {
  84. print_usage();
  85. exit(1);
  86. } else { // treat as file argument
  87. files[numfps++] = i;
  88. }
  89. }
  90. for (i = 0; i < numfps; i++) {
  91. FILE *fp = fopen(argv[files[i]], "r");
  92. if (fp == NULL) {
  93. fprintf(stderr, "Error opening file %s: %s\n",
  94. argv[files[i]], strerror(errno));
  95. exit(1);
  96. }
  97. start_timer();
  98. while ((bytes = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
  99. cmark_parser_feed(parser, buffer, bytes);
  100. if (bytes < sizeof(buffer)) {
  101. break;
  102. }
  103. }
  104. end_timer("processing lines");
  105. fclose(fp);
  106. }
  107. if (numfps == 0) {
  108. while ((bytes = fread(buffer, 1, sizeof(buffer), stdin)) > 0) {
  109. cmark_parser_feed(parser, buffer, bytes);
  110. if (bytes < sizeof(buffer)) {
  111. break;
  112. }
  113. }
  114. }
  115. start_timer();
  116. document = cmark_parser_finish(parser);
  117. end_timer("finishing document");
  118. cmark_parser_free(parser);
  119. start_timer();
  120. print_document(document, writer);
  121. end_timer("print_document");
  122. start_timer();
  123. cmark_node_free(document);
  124. end_timer("free_blocks");
  125. free(files);
  126. return 0;
  127. }