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