/* Match a string against the given regular expression. * Returns true on a successful match. */ bool d_match(dstr line, const char *regex) { re_cache_t *re; TRexMatch match; int i; re = compile_regex(regex); if (!trex_search(re->reg, line, NULL, NULL)) { return false; } trex_getsubexp(re->reg, 0, &match); d_assignn(d_before_match, line, match.begin - line); d_assign(d_after_match, match.begin + match.len); for (i = 0; i < MAX_MATCH; i++) { if (trex_getsubexp(re->reg, i, &match)) { strncpy(d_submatches[i], match.begin, match.len); d_submatches[i][match.len] = '\0'; } else { d_submatches[i][0] = '\0'; } } return true; }
/* Close input file. */ void d_close_input(void) { if (d_file) { fclose(d_file); } d_file = NULL; d_assign(d_filename, ""); }
static int process_options(int argc, char *argv[]) { int i; for (i = 1; i < argc; ) { if (streq(argv[i], "--")) { i++; break; } if (argv[i][0] != '-') { break; } if (streq(argv[i], "--pandoc")) { d_assign(pandoc, argv[i + 1]); i += 2; } else if (streq(argv[i], "--protos")) { d_assign(protos_file, argv[i + 1]); i += 2; } else if (streq(argv[i], "--to")) { d_assign(to_format, argv[i + 1]); i += 2; } else if (streq(argv[i], "--raise-sections")) { raise_sections = true; i++; } else { /* Other options are assumed to be Pandoc options. */ strcat(pandoc_options, " "); strcat(pandoc_options, argv[i]); i++; if (i < argc && argv[i][0] != '-') { strcat(pandoc_options, " "); strcat(pandoc_options, argv[i]); i++; } } } /* Shift arguments, including sentinel, but not the command name. */ memmove(argv + 1, argv + i, (argc - i + 1) * sizeof(char *)); return argc - (i - 1); }
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; }
/* 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; }
static void load_prototypes(const char *filename) { dstr line; const char *name; const char *newtext; const char *file_name; const char *line_number; dstr text; d_open_input(filename); while (d_getline(line)) { if (d_match(line, "([^:]*): ([^:]*):([^:]*):([^:]*)")) { name = d_submatch(1); newtext = d_submatch(2); file_name = d_submatch(3); line_number = d_submatch(4); d_assign(text, lookup_prototype(name)); strcat(text, "\n"); strcat(text, newtext); protos = aa_insert(protos, name, text); d_assign(text, lookup_source(name)); if (strlen(text) == 0) { strcat(text, "https://github.com/liballeg/allegro5/blob/5.1/"); strcat(text, file_name); strcat(text, "#L"); strcat(text, line_number); sources = aa_insert(sources, name, text); } } } d_close_input(); }
int main(int argc, char *argv[]) { bool found_tag = false; bool do_print = false; dstr line = ""; dstr prefix = ""; d_init(argc, argv); while (d_getline(line)) { if (d_match(line, "/[*] (Function|Type|Enum): (.*)")) { found_tag = true; do_print = false; d_assign(prefix, d_submatch(2)); continue; } if (found_tag && d_match(line, "^ ?\\*/")) { found_tag = false; do_print = true; continue; } if (d_match(line, "^\\{|^$")) { do_print = false; continue; } /* Prototype ends on blank line. */ /* TODO: Should the above regexp match it? If so it doesn't here... */ if (line[0] == 0) { do_print = false; continue; } if (do_print) { printf("%s: %s\n", prefix, line); } if (d_match(line, "\\{ *$|; *$")) { do_print = false; } } return 0; }