1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- #include <stdio.h>
- #include <stdbool.h>
- #include <string.h>
- #include "main.h"
- #include "args.h"
- #include "code.h"
- const char *argp_program_version = "warp";
- const char *argp_program_bug_address = "<t.rice@ms.unimelb.edu.au>";
- FILE *ist;
- #define IUNIT 1024 // Reading block size.
- #define IO_ERROR 5
- int main(int argc, char *argv[]) {
- ist = stdin;
- /* Set argument defaults */
- struct arguments args;
- args.infile = NULL;
- args.langs = NULL;
- args.complement = false;
- argp_parse(&argp, argc, argv, 0, 0, &args);
- if(args.infile) {
- ist = fopen(args.infile, "r");
- if (!ist) {
- fprintf(stderr, "Unable to open input file \"%s\": %s\n", args.infile, strerror(errno));
- return FILESYSTEM_ERROR;
- }
- }
-
- /* Process input */
- hoedown_buffer *ib;
- ib = hoedown_buffer_new(IUNIT);
- bool proc_input_success = (hoedown_buffer_putf(ib, ist) == 0);
- if(ist != NULL) {
- fclose(ist);
- }
- ist = NULL;
- if(!proc_input_success) {
- fprintf(stderr, "Error reading input.\n");
- if(ib) {
- hoedown_buffer_free(ib);
- }
- return IO_ERROR;
- }
- /* Print in defined output format to destination. */
- unsigned int extensions = HOEDOWN_EXT_FENCED_CODE;
- hoedown_renderer* renderer = hoedown_code_renderer_new(args.langs, args.complement);
- hoedown_document* document = hoedown_document_new(renderer, extensions, 16);
- hoedown_buffer* ob = hoedown_buffer_new(16);
- hoedown_document_render(document, ob, ib->data, ib->size);
- /* Tidy up */
- (void)fwrite(ob->data, 1, ob->size, stdout);
- hoedown_buffer_free(ib);
- hoedown_document_free(document);
- hoedown_code_renderer_free(renderer);
- return 0;
- }
|