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