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