/parsleyc_main.c

http://github.com/fizx/parsley · C · 108 lines · 91 code · 16 blank · 1 comment · 7 complexity · 21663bddae8059c65fbfd7bd76f595f3 MD5 · raw file

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <argp.h>
  4. #include <string.h>
  5. #include <json/printbuf.h>
  6. #include "parsley.h"
  7. #include "util.h"
  8. struct list_elem {
  9. int has_next;
  10. struct list_elem *next;
  11. char *string;
  12. };
  13. struct arguments
  14. {
  15. struct list_elem *include_files;
  16. char *parsley;
  17. char *output_file;
  18. };
  19. const char *argp_program_version = "parsleyc 0.1";
  20. const char *argp_program_bug_address = "<kyle@kylemaxwell.com>";
  21. static char args_doc[] = "DEX_FILE";
  22. static char doc[] = "Parsleyc is a parselet to XSLT compiler";
  23. static struct argp_option options[] = {
  24. {"debug", 'd', 0, 0, "Turn on Bison parser debugging" },
  25. {"output", 'o', "FILE", 0, "Output to FILE instead of standard output" },
  26. {"include", 'i', "FILE", 0, "Include the contents of FILE in the produced XSLT" },
  27. { 0 }
  28. };
  29. static error_t parse_opt (int key, char *arg, struct argp_state *state)
  30. {
  31. struct arguments *arguments = state->input;
  32. struct list_elem *base = arguments->include_files;
  33. struct list_elem *e;
  34. switch (key)
  35. {
  36. case 'i':
  37. e = (struct list_elem *) calloc(1, sizeof(e));
  38. e->string = arg;
  39. while(base->has_next) base = base->next;
  40. base->next = e;
  41. base->has_next = 1;
  42. break;
  43. case 'd':
  44. // parsley_set_debug_mode(1);
  45. break;
  46. case 'o':
  47. arguments->output_file = arg;
  48. break;
  49. case ARGP_KEY_ARG:
  50. if (state->arg_num >= 1) argp_usage (state);
  51. arguments->parsley = arg;
  52. break;
  53. case ARGP_KEY_END:
  54. if (state->arg_num < 1) argp_usage (state);
  55. break;
  56. default:
  57. return ARGP_ERR_UNKNOWN;
  58. }
  59. return 0;
  60. }
  61. static struct argp argp = { options, parse_opt, args_doc, doc };
  62. int main (int argc, char **argv) {
  63. struct arguments arguments;
  64. struct list_elem elem;
  65. struct list_elem *elemptr = &elem;
  66. elem.has_next = 0;
  67. arguments.include_files = elemptr;
  68. arguments.output_file = "-";
  69. arguments.parsley = "-";
  70. argp_parse (&argp, argc, argv, 0, 0, &arguments);
  71. struct printbuf* parsley = printbuf_new();
  72. struct printbuf* incl = printbuf_new();
  73. sprintbuf(parsley, "");
  74. sprintbuf(incl, "");
  75. FILE* in = parsley_fopen(arguments.parsley, "r");
  76. printbuf_file_read(in, parsley);
  77. while(elemptr->has_next) {
  78. elemptr = elemptr->next;
  79. FILE* f = parsley_fopen(elemptr->string, "r");
  80. printbuf_file_read(f, incl);
  81. fclose(f);
  82. }
  83. parsleyPtr compiled = parsley_compile(parsley->buf, incl->buf);
  84. if(compiled->error != NULL) {
  85. fprintf(stderr, "%s\n", compiled->error);
  86. exit(1);
  87. }
  88. FILE* fo = parsley_fopen(arguments.output_file, "w");
  89. xmlDocFormatDump(fo, compiled->stylesheet->doc, 1);
  90. fclose(fo);
  91. return 0;
  92. }