aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: e849b823c8fc46f4c12fffcc63ad3072e09996cd (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. #if defined(_WIN32) && !defined(__CYGWIN__)
  10. #include <io.h>
  11. #include <fcntl.h>
  12. #endif
  13. typedef enum {
  14. FORMAT_NONE,
  15. FORMAT_HTML,
  16. FORMAT_XML,
  17. FORMAT_MAN,
  18. } writer_format;
  19. void print_usage()
  20. {
  21. printf("Usage: cmark [FILE*]\n");
  22. printf("Options:\n");
  23. printf(" --to, -t FORMAT Specify output format (html, xml, man)\n");
  24. printf(" --sourcepos Include source position attribute\n");
  25. printf(" --hardbreaks Treat newlines as hard line breaks\n");
  26. printf(" --help, -h Print usage information\n");
  27. printf(" --version Print version\n");
  28. }
  29. static void print_document(cmark_node *document, writer_format writer,
  30. long options)
  31. {
  32. char *result;
  33. switch (writer) {
  34. case FORMAT_HTML:
  35. result = cmark_render_html(document, options);
  36. break;
  37. case FORMAT_XML:
  38. result = cmark_render_xml(document, options);
  39. break;
  40. case FORMAT_MAN:
  41. result = cmark_render_man(document, options);
  42. break;
  43. default:
  44. fprintf(stderr, "Unknown format %d\n", writer);
  45. exit(1);
  46. }
  47. printf("%s", result);
  48. free(result);
  49. }
  50. int main(int argc, char *argv[])
  51. {
  52. int i, numfps = 0;
  53. int *files;
  54. char buffer[4096];
  55. cmark_parser *parser;
  56. size_t bytes;
  57. cmark_node *document;
  58. writer_format writer = FORMAT_HTML;
  59. long options = CMARK_OPT_DEFAULT;
  60. #if defined(_WIN32) && !defined(__CYGWIN__)
  61. _setmode(_fileno(stdout), _O_BINARY);
  62. #endif
  63. parser = cmark_parser_new();
  64. files = (int *)malloc(argc * sizeof(*files));
  65. for (i = 1; i < argc; i++) {
  66. if (strcmp(argv[i], "--version") == 0) {
  67. printf("cmark %s", CMARK_VERSION);
  68. printf(" - CommonMark converter (c) 2014 John MacFarlane\n");
  69. exit(0);
  70. } else if (strcmp(argv[i], "--sourcepos") == 0) {
  71. options |= CMARK_OPT_SOURCEPOS;
  72. } else if (strcmp(argv[i], "--hardbreaks") == 0) {
  73. options |= CMARK_OPT_HARDBREAKS;
  74. } else if ((strcmp(argv[i], "--help") == 0) ||
  75. (strcmp(argv[i], "-h") == 0)) {
  76. print_usage();
  77. exit(0);
  78. } else if ((strcmp(argv[i], "-t") == 0) ||
  79. (strcmp(argv[i], "--to") == 0)) {
  80. i += 1;
  81. if (i < argc) {
  82. if (strcmp(argv[i], "man") == 0) {
  83. writer = FORMAT_MAN;
  84. } else if (strcmp(argv[i], "html") == 0) {
  85. writer = FORMAT_HTML;
  86. } else if (strcmp(argv[i], "xml") == 0) {
  87. writer = FORMAT_XML;
  88. } else {
  89. fprintf(stderr,
  90. "Unknown format %s\n", argv[i]);
  91. exit(1);
  92. }
  93. } else {
  94. fprintf(stderr, "No argument provided for %s\n",
  95. argv[i - 1]);
  96. exit(1);
  97. }
  98. } else if (*argv[i] == '-') {
  99. print_usage();
  100. exit(1);
  101. } else { // treat as file argument
  102. files[numfps++] = i;
  103. }
  104. }
  105. for (i = 0; i < numfps; i++) {
  106. FILE *fp = fopen(argv[files[i]], "r");
  107. if (fp == NULL) {
  108. fprintf(stderr, "Error opening file %s: %s\n",
  109. argv[files[i]], strerror(errno));
  110. exit(1);
  111. }
  112. start_timer();
  113. while ((bytes = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
  114. cmark_parser_feed(parser, buffer, bytes);
  115. if (bytes < sizeof(buffer)) {
  116. break;
  117. }
  118. }
  119. end_timer("processing lines");
  120. fclose(fp);
  121. }
  122. if (numfps == 0) {
  123. while ((bytes = fread(buffer, 1, sizeof(buffer), stdin)) > 0) {
  124. cmark_parser_feed(parser, buffer, bytes);
  125. if (bytes < sizeof(buffer)) {
  126. break;
  127. }
  128. }
  129. }
  130. start_timer();
  131. document = cmark_parser_finish(parser);
  132. end_timer("finishing document");
  133. cmark_parser_free(parser);
  134. start_timer();
  135. print_document(document, writer, options);
  136. end_timer("print_document");
  137. start_timer();
  138. cmark_node_free(document);
  139. end_timer("free_blocks");
  140. free(files);
  141. return 0;
  142. }