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