FILE* run_preprocessor(FILE* input, char* filename) { assert(input); preprocessor_state preproc; preproc.input = input; preproc.output = tmpfile(); if (!preproc.output) { perror("Failed to open file for preprocessor output"); exit(0); } cvec_str(&preproc.macros, 0, 15); cvec_str(&preproc.values, 0, 15); cvec_void(&preproc.params, 0, 15, sizeof(macro_params), free_macro_params, NULL); cvec_token_lex(&preproc.if_stack, 0, 10, free_token_lex, NULL); preproc.lexer = (lexer_state){ filename, 1, 0, 0 }; //TODO preprocess_file(&preproc); cvec_free_str(&preproc.macros); cvec_free_str(&preproc.values); cvec_free_void(&preproc.params); cvec_free_token_lex(&preproc.if_stack); fclose(input); rewind(preproc.output); return preproc.output; }
// offset meaning: // == 0 match version exactly // > 0 find latest version >= ver // < 0 find latest version <= ver static char* locate_versioned_dv_module_in_path(const char* mod_name, const char* ver, int offset, char* path) { char* q; DIR* d; struct dirent* de; char* latest = NULL; int latest_i = -1; char *vcurr = NULL, *vlatest = NULL; char* dir_name; int strsz; int replace_reqd; char name[1024]; char* fname; int i; cvector_str modules; cvec_str(&modules, 0, 16); int nmodules = get_module_versions(mod_name, path, &modules); for (i = 0; i < nmodules; i++) { fname = modules.a[i]; vcurr = extract_dv_mod_ver_from_fname(mod_name, fname); replace_reqd = 0; if ((offset == 0 && cmp_mod_ver(vcurr, ver) == 0) || (offset > 0 && cmp_mod_ver(vcurr, ver) >= 0 && cmp_mod_ver(vcurr, vlatest) > 0) || (offset < 0 && cmp_mod_ver(vcurr, ver) <= 0 && cmp_mod_ver(vcurr, vlatest) > 0)) { latest_i = i; vlatest = vcurr; } } if (latest_i >= 0) latest = strdup(modules.a[i]); cvec_free_str(&modules); return latest; }
static char* locate_latest_dv_module_in_path(const char* mod_name, char* path) { char* latest = NULL; char *vcurr = NULL, *vlatest = NULL; char name[1024]; char* fname; cvector_str modules; cvec_str(&modules, 0, 16); int nmodules = get_module_versions(mod_name, path, &modules); for (int i = 0; i < nmodules; i++) { fname = modules.a[i]; vcurr = extract_dv_mod_ver_from_fname(mod_name, fname); if (cmp_mod_ver(vcurr, vlatest) > 0) { if (latest) free(latest); latest = strdup(fname); vlatest = vcurr; } } cvec_free_str(&modules); return latest; }