aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 9b987184b2a8a29503e097a595743f0aeb4e9a6a (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. 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;
  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. files = (int *)malloc(argc * sizeof(*files));
  35. for (i = 1; i < argc; i++) {
  36. if (strcmp(argv[i], "--version") == 0) {
  37. printf("cmark %s", VERSION);
  38. printf(" - CommonMark converter (c) 2014 John MacFarlane\n");
  39. exit(0);
  40. } else if ((strcmp(argv[i], "--help") == 0) ||
  41. (strcmp(argv[i], "-h") == 0)) {
  42. print_usage();
  43. exit(0);
  44. } else if (strcmp(argv[i], "--ast") == 0) {
  45. ast = true;
  46. } else if (*argv[i] == '-') {
  47. print_usage();
  48. exit(1);
  49. } else { // treat as file argument
  50. files[numfps++] = i;
  51. }
  52. }
  53. for (i = 0; i < numfps; i++) {
  54. FILE *fp = fopen(argv[files[i]], "r");
  55. if (fp == NULL) {
  56. fprintf(stderr, "Error opening file %s: %s\n",
  57. argv[files[i]], strerror(errno));
  58. exit(1);
  59. }
  60. start_timer();
  61. while (fgets((char *)buffer, sizeof(buffer), fp)) {
  62. offset = strlen((char *)buffer);
  63. cmark_process_line(parser, buffer, offset);
  64. }
  65. end_timer("processing lines");
  66. fclose(fp);
  67. }
  68. if (numfps == 0) {
  69. /*
  70. document = cmark_parse_file(stdin);
  71. print_document(document, ast);
  72. exit(0);
  73. */
  74. while (fgets((char *)buffer, sizeof(buffer), stdin)) {
  75. offset = strlen((char *)buffer);
  76. cmark_process_line(parser, buffer, offset);
  77. }
  78. }
  79. start_timer();
  80. document = cmark_finish(parser);
  81. end_timer("finishing document");
  82. cmark_free_doc_parser(parser);
  83. start_timer();
  84. print_document(document, ast);
  85. end_timer("print_document");
  86. start_timer();
  87. cmark_free_blocks(document);
  88. end_timer("free_blocks");
  89. free(files);
  90. return 0;
  91. }