main.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <string.h>
  4. #include "main.h"
  5. #include "args.h"
  6. #include "code.h"
  7. const char *argp_program_version = "warp";
  8. const char *argp_program_bug_address = "<t.rice@ms.unimelb.edu.au>";
  9. FILE *ist;
  10. #define IUNIT 1024 // Reading block size.
  11. #define IO_ERROR 5
  12. int main(int argc, char *argv[]) {
  13. ist = stdin;
  14. /* Set argument defaults */
  15. struct arguments args;
  16. args.infile = NULL;
  17. args.langs = NULL;
  18. args.complement = false;
  19. argp_parse(&argp, argc, argv, 0, 0, &args);
  20. if(args.infile) {
  21. ist = fopen(args.infile, "r");
  22. if (!ist) {
  23. fprintf(stderr, "Unable to open input file \"%s\": %s\n", args.infile, strerror(errno));
  24. return FILESYSTEM_ERROR;
  25. }
  26. }
  27. /* Process input */
  28. hoedown_buffer *ib;
  29. ib = hoedown_buffer_new(IUNIT);
  30. bool proc_input_success = (hoedown_buffer_putf(ib, ist) == 0);
  31. if(ist != NULL) {
  32. fclose(ist);
  33. }
  34. ist = NULL;
  35. if(!proc_input_success) {
  36. fprintf(stderr, "Error reading input.\n");
  37. if(ib) {
  38. hoedown_buffer_free(ib);
  39. }
  40. return IO_ERROR;
  41. }
  42. /* Print in defined output format to destination. */
  43. unsigned int extensions = HOEDOWN_EXT_FENCED_CODE;
  44. hoedown_renderer* renderer = hoedown_code_renderer_new(args.langs, args.complement);
  45. hoedown_document* document = hoedown_document_new(renderer, extensions, 16);
  46. hoedown_buffer* ob = hoedown_buffer_new(16);
  47. hoedown_document_render(document, ob, ib->data, ib->size);
  48. /* Tidy up */
  49. (void)fwrite(ob->data, 1, ob->size, stdout);
  50. hoedown_buffer_free(ib);
  51. hoedown_document_free(document);
  52. hoedown_code_renderer_free(renderer);
  53. return 0;
  54. }