aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 43155d5171f53a20ba97a3f17839d8f0724ce489 (plain)
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <stdbool.h>
  6. #include "cmark.h"
  7. #include "debug.h"
  8. #include "bench.h"
  9. void print_usage()
  10. {
  11. printf("Usage: cmark [FILE*]\n");
  12. printf("Options: --help, -h Print usage information\n");
  13. printf(" --ast Print AST instead of HTML\n");
  14. printf(" --version Print version\n");
  15. }
  16. static void print_document(node_block *document, bool ast)
  17. {
  18. if (ast) {
  19. cmark_debug_print(document);
  20. } else {
  21. printf("%s", cmark_render_html(document));
  22. }
  23. }
  24. int main(int argc, char *argv[])
  25. {
  26. int i, numfps = 0;
  27. bool ast = false;
  28. int files[argc];
  29. unsigned char buffer[4096];
  30. cmark_doc_parser *parser;
  31. size_t offset;
  32. node_block *document;
  33. parser = cmark_new_doc_parser();
  34. for (i = 1; i < argc; i++) {
  35. if (strcmp(argv[i], "--version") == 0) {
  36. printf("cmark %s", VERSION);
  37. printf(" - CommonMark converter (c) 2014 John MacFarlane\n");
  38. exit(0);
  39. } else if ((strcmp(argv[i], "--help") == 0) ||
  40. (strcmp(argv[i], "-h") == 0)) {
  41. print_usage();
  42. exit(0);
  43. } else if (strcmp(argv[i], "--ast") == 0) {
  44. ast = true;
  45. } else if (*argv[i] == '-') {
  46. print_usage();
  47. exit(1);
  48. } else { // treat as file argument
  49. files[numfps++] = i;
  50. }
  51. }
  52. for (i = 0; i < numfps; i++) {
  53. FILE *fp = fopen(argv[files[i]], "r");
  54. if (fp == NULL) {
  55. fprintf(stderr, "Error opening file %s: %s\n",
  56. argv[files[i]], strerror(errno));
  57. exit(1);
  58. }
  59. start_timer();
  60. while (fgets((char *)buffer, sizeof(buffer), fp)) {
  61. offset = strlen((char *)buffer);
  62. cmark_process_line(parser, buffer, offset);
  63. }
  64. end_timer("processing lines");
  65. fclose(fp);
  66. }
  67. if (numfps == 0) {
  68. /*
  69. document = cmark_parse_file(stdin);
  70. print_document(document, ast);
  71. exit(0);
  72. */
  73. while (fgets((char *)buffer, sizeof(buffer), stdin)) {
  74. offset = strlen((char *)buffer);
  75. cmark_process_line(parser, buffer, offset);
  76. }
  77. }
  78. start_timer();
  79. document = cmark_finish(parser);
  80. end_timer("finishing document");
  81. cmark_free_doc_parser(parser);
  82. start_timer();
  83. print_document(document, ast);
  84. end_timer("print_document");
  85. start_timer();
  86. cmark_free_blocks(document);
  87. end_timer("free_blocks");
  88. return 0;
  89. }