int main(int argc, char *argv[]) { astring *dirname = NULL, *tempfilename = NULL, *tempheader = NULL, *tempfooter = NULL; UINT32 bufsize; void *buffer; int listnum; int result; /* first argument is the directory */ if (argc < 4) { fprintf(stderr, "Usage:\nregrep <template> <outputdir> <summary1> [<summary2> [<summary3> ...]]\n"); return 1; } tempfilename = astring_dupc(argv[1]); dirname = astring_dupc(argv[2]); list_count = argc - 3; /* read the template file into an astring */ if (core_fload(astring_c(tempfilename), &buffer, &bufsize) == FILERR_NONE) { tempheader = astring_dupch((const char *)buffer, bufsize); free(buffer); } /* verify the template */ if (tempheader == NULL) { fprintf(stderr, "Unable to read template file\n"); return 1; } result = astring_findc(tempheader, 0, "<!--CONTENT-->"); if (result == -1) { fprintf(stderr, "Template is missing a <!--CONTENT--> marker\n"); return 1; } tempfooter = astring_substr(astring_dup(tempheader), result + 14, -1); tempheader = astring_substr(tempheader, 0, result); /* loop over arguments and read the files */ for (listnum = 0; listnum < list_count; listnum++) { result = read_summary_log(argv[listnum + 3], listnum); if (result != 0) return result; } /* output the summary */ output_report(dirname, tempheader, tempfooter, sort_file_list()); astring_free(dirname); astring_free(tempfilename); astring_free(tempheader); astring_free(tempfooter); return 0; }
static astring *find_include_file(int srcrootlen, const astring *srcfile, const astring *filename) { include_path *curpath; /* iterate over include paths and find the file */ for (curpath = incpaths; curpath != NULL; curpath = curpath->next) { astring *srcincpath = astring_dup(curpath->path); core_file *testfile; int lastsepindex = 0; int sepindex; /* a '.' include path is specially treated */ if (astring_cmpc(curpath->path, ".") == 0) astring_cpysubstr(srcincpath, srcfile, 0, astring_rchr(srcfile, 0, PATH_SEPARATOR[0])); /* append the filename piecemeal to account for directories */ while ((sepindex = astring_chr(filename, lastsepindex, '/')) != -1) { astring *pathpart = astring_dupsubstr(filename, lastsepindex, sepindex - lastsepindex); /* handle .. by removing a chunk from the incpath */ if (astring_cmpc(pathpart, "..") == 0) { int sepindex_part = astring_rchr(srcincpath, 0, PATH_SEPARATOR[0]); if (sepindex_part != -1) astring_substr(srcincpath, 0, sepindex_part); } /* otherwise, append a path separator and the pathpart */ else astring_cat(astring_catc(srcincpath, PATH_SEPARATOR), pathpart); /* advance past the previous index */ lastsepindex = sepindex + 1; /* free the path part we extracted */ astring_free(pathpart); } /* now append the filename */ astring_catsubstr(astring_catc(srcincpath, PATH_SEPARATOR), filename, lastsepindex, -1); /* see if we can open it */ if (core_fopen(astring_c(srcincpath), OPEN_FLAG_READ, &testfile) == FILERR_NONE) { /* close the file */ core_fclose(testfile); return srcincpath; } /* free our include path */ astring_free(srcincpath); } return NULL; }
static void output_footer_and_close_file(core_file *file, const astring *templatefile, const astring *title) { astring *modified; modified = astring_dup(templatefile); astring_replacec(modified, 0, "<!--TITLE-->", astring_c(title)); core_fwrite(file, astring_c(modified), astring_len(modified)); astring_free(modified); core_fclose(file); }
static core_file *create_file_and_output_header(const astring *filename, const astring *templatefile, const astring *title) { astring *modified; core_file *file; /* create the indexfile */ if (core_fopen(astring_c(filename), OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS | OPEN_FLAG_NO_BOM, &file) != FILERR_NONE) return NULL; /* print a header */ modified = astring_dup(templatefile); astring_replacec(modified, 0, "<!--TITLE-->", astring_c(title)); core_fwrite(file, astring_c(modified), astring_len(modified)); /* return the file */ astring_free(modified); return file; }
static file_entry *compute_dependencies(int srcrootlen, const astring *srcfile) { astring *normalfile; UINT32 filelength; file_entry *file; char *filedata; int index; /* see if we already have an entry */ normalfile = astring_dup(srcfile); astring_replacechr(normalfile, PATH_SEPARATOR[0], '/'); file = (file_entry *)tagmap_find(file_map, astring_c(normalfile)); if (file != NULL) return file; /* create a new header entry */ file = (file_entry *)malloc(sizeof(*file)); file->deplist = NULL; file->name = normalfile; tagmap_add(file_map, astring_c(file->name), file, FALSE); /* read the source file */ if (core_fload(astring_c(srcfile), (void **)&filedata, &filelength) != FILERR_NONE) { fprintf(stderr, "Unable to read file '%s'\n", astring_c(srcfile)); return file; } /* find the #include directives in this file */ for (index = 0; index < filelength; index++) if (filedata[index] == '#' && strncmp(&filedata[index + 1], "include", 7) == 0) { astring *filename, *target; int scan = index; dependency *dep; int start; int just_continue = 0; /* first make sure we're not commented or quoted */ for (scan = index; scan > 2 && filedata[scan] != 13 && filedata[scan] != 10; scan--) if ((filedata[scan] == '/' && filedata[scan - 1] == '/') || filedata[scan] == '"') { just_continue = 1; break; } if (just_continue) continue; /* scan forward to find the quotes or bracket */ index += 7; for (scan = index; scan < filelength && filedata[scan] != '<' && filedata[scan] != '"' && filedata[scan] != 13 && filedata[scan] != 10; scan++) ; /* ignore if not found or if it's bracketed */ if (scan >= filelength || filedata[scan] != '"') continue; start = ++scan; /* find the closing quote */ while (scan < filelength && filedata[scan] != '"') scan++; if (scan >= filelength) continue; /* find the include file */ filename = astring_dupch(&filedata[start], scan - start); target = find_include_file(srcrootlen, srcfile, filename); /* create a new dependency */ if (target != NULL) { dep = (dependency *)malloc(sizeof(*dep)); dep->next = file->deplist; file->deplist = dep; dep->file = compute_dependencies(srcrootlen, target); astring_free(target); } astring_free(filename); } osd_free(filedata); return file; }
static int recurse_dir(int srcrootlen, const astring *srcdir) { static const osd_dir_entry_type typelist[] = { ENTTYPE_DIR, ENTTYPE_FILE }; int result = 0; int entindex; /* iterate first over directories, then over files */ for (entindex = 0; entindex < ARRAY_LENGTH(typelist) && result == 0; entindex++) { osd_dir_entry_type entry_type = typelist[entindex]; const osd_directory_entry *entry; list_entry **listarray = NULL; list_entry *list = NULL; list_entry *curlist; osd_directory *dir; int found = 0; /* open the directory and iterate through it */ dir = osd_opendir(astring_c(srcdir)); if (dir == NULL) { result = 1; goto error; } /* build up the list of files */ while ((entry = osd_readdir(dir)) != NULL) if (entry->type == entry_type && entry->name[0] != '.') { list_entry *lentry = (list_entry *)malloc(sizeof(*lentry)); lentry->name = astring_dupc(entry->name); lentry->next = list; list = lentry; found++; } /* close the directory */ osd_closedir(dir); /* skip if nothing found */ if (found == 0) continue; /* allocate memory for sorting */ listarray = (list_entry **)malloc(sizeof(list_entry *) * found); found = 0; for (curlist = list; curlist != NULL; curlist = curlist->next) listarray[found++] = curlist; /* sort the list */ qsort(listarray, found, sizeof(listarray[0]), compare_list_entries); /* rebuild the list */ list = NULL; while (--found >= 0) { listarray[found]->next = list; list = listarray[found]; } free(listarray); /* iterate through each file */ for (curlist = list; curlist != NULL && result == 0; curlist = curlist->next) { astring *srcfile; /* build the source filename */ srcfile = astring_alloc(); astring_printf(srcfile, "%s%c%s", astring_c(srcdir), PATH_SEPARATOR[0], astring_c(curlist->name)); /* if we have a file, output it */ if (entry_type == ENTTYPE_FILE) { /* make sure we care, first */ if (core_filename_ends_with(astring_c(curlist->name), ".c")) { tagmap *depend_map = tagmap_alloc(); tagmap_entry *map_entry; file_entry *file; astring *target; int taghash; /* find dependencies */ file = compute_dependencies(srcrootlen, srcfile); recurse_dependencies(file, depend_map); /* convert the target from source to object (makes assumptions about rules) */ target = astring_dup(file->name); astring_replacec(target, 0, "src/", "$(OBJ)/"); astring_replacec(target, 0, ".c", ".o"); printf("\n%s : \\\n", astring_c(target)); /* iterate over the hashed dependencies and output them as well */ for (taghash = 0; taghash < TAGMAP_HASH_SIZE; taghash++) for (map_entry = depend_map->table[taghash]; map_entry != NULL; map_entry = map_entry->next) printf("\t%s \\\n", astring_c((astring *)map_entry->object)); astring_free(target); tagmap_free(depend_map); } } /* if we have a directory, recurse */ else result = recurse_dir(srcrootlen, srcfile); /* free memory for the names */ astring_free(srcfile); } /* free all the allocated entries */ while (list != NULL) { list_entry *next = list->next; astring_free((astring *)list->name); free(list); list = next; } } error: return result; }