aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 90bb16dbd223debcc79af112c4b7286e75fe685e (plain)
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "stmd.h"
  5. #include "debug.h"
  6. void print_usage()
  7. {
  8. printf("Usage: stmd [FILE*]\n");
  9. printf("Options: --help, -h Print usage information\n");
  10. printf(" --ast Print AST instead of HTML\n");
  11. printf(" --version Print version\n");
  12. }
  13. static void print_document(node_block *document, bool ast)
  14. {
  15. strbuf html = GH_BUF_INIT;
  16. if (ast) {
  17. print_blocks(document, 0);
  18. } else {
  19. blocks_to_html(&html, document, false);
  20. printf("%s", html.ptr);
  21. strbuf_free(&html);
  22. }
  23. }
  24. int main(int argc, char *argv[])
  25. {
  26. int i, numfps = 0;
  27. bool ast = false;
  28. int files[argc];
  29. node_block *document = NULL;
  30. for (i = 1; i < argc; i++) {
  31. if (strcmp(argv[i], "--version") == 0) {
  32. printf("stmd %s", VERSION);
  33. printf(" - CommonMark converter (c) 2014 John MacFarlane\n");
  34. exit(0);
  35. } else if ((strcmp(argv[i], "--help") == 0) ||
  36. (strcmp(argv[i], "-h") == 0)) {
  37. print_usage();
  38. exit(0);
  39. } else if (strcmp(argv[i], "--ast") == 0) {
  40. ast = true;
  41. } else if (*argv[i] == '-') {
  42. print_usage();
  43. exit(1);
  44. } else { // treat as file argument
  45. files[numfps++] = i;
  46. }
  47. }
  48. if (numfps == 0) {
  49. document = stmd_parse_file(stdin);
  50. print_document(document, ast);
  51. free_blocks(document);
  52. } else {
  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. document = stmd_parse_file(fp);
  61. print_document(document, ast);
  62. free_blocks(document);
  63. fclose(fp);
  64. }
  65. }
  66. return 0;
  67. }