void call_pandoc(const char *input, const char *output, const char *extra_options) { dstr cmd; dstr input_native; char *p; strcpy(input_native, input); /* Use native Windows syntax to avoid "c:/foo.txt" being treated as a * remote URI by Pandoc 1.5 and 1.6. */ if (strlen(input_native) > 2 && isalpha(input_native[0]) && input_native[1] == ':') { for (p = input_native; *p != '\0'; p++) { if (*p == '/') *p = '\\'; } } sprintf(cmd, "\"%s\" %s %s %s --to %s --output %s", pandoc, input_native, pandoc_options, extra_options, to_format, output); if (system(cmd) != 0) { d_abort("system call failed: ", cmd); } }
/* Open a file for writing. */ void d_open_output(const char *filename) { FILE *f; d_close_output(); f = fopen(filename, "w"); if (!f) { d_abort("error opening file for writing: ", filename); } d_stdout = f; }
void generate_temp_file(char *filename) { /* gcc won't shut up if we use tmpnam() so we'll use mkstemp() if it is * likely to be available. */ #ifdef USE_MKSTEMP int fd; d_assign(filename, "make_doc_tmp.XXXXXX"); fd = mkstemp(filename); if (fd == -1) { d_abort("could not generate temporary file name", ""); } close(fd); #else char *name = TEMPNAM(NULL, "make_doc_tmp."); if (!name) { d_abort("could not generate temporary file name", ""); } d_assign(filename, name); free(name); #endif }
/* Open a single file for reading. */ void d_open_input(const char *filename) { if (d_file) { fclose(d_file); } d_file = fopen(filename, "r"); if (!d_file) { d_abort("could not open file for reading: ", filename); } d_assign(d_filename, filename); d_line_num = 0; d_file_num = -1; }
static re_cache_t *compile_regex(const char *regex) { re_cache_t *re; int i; for (i = 0; i < MAX_RE_CACHE; i++) { re = &d_regex_cache[i]; if (re->regex == NULL) { re->regex = regex; re->reg = trex_compile(regex, NULL); if (re->reg == NULL) { d_abort("error compiling regular expression: ", regex); } } if (re->regex == regex) { return re; } } d_abort("too many regular expressions", ""); return NULL; }
/* Read the next line from the current input file(s). */ bool d_getline(dstr var) { char *p = var; int c; /* Open the next file if necessary. */ if (!d_file) { if (d_file_num == -1 || d_file_num + 1 >= d_argc) { return false; } d_file_num++; d_file = fopen(d_argv[d_file_num], "r"); if (!d_file) { d_abort("could not open file for reading: ", d_argv[d_file_num]); } d_assign(d_filename, d_argv[d_file_num]); d_line_num = 0; } for (;;) { c = fgetc(d_file); if (c == EOF || c == '\n') { break; } *p++ = c; if (p - var >= MAX_STRING - 1) { fprintf(stderr, "dawk: string length limit reached\n"); break; } } *p = '\0'; if (c == EOF) { fclose(d_file); d_file = NULL; if (p == var) { return d_getline(var); } } else if (c == '\n') { d_line_num++; /* Remove trailing CR if present. */ if (p > var && p[-1] == '\r') { p[-1] = '\0'; } } return true; }