void init_lean_path() { #if defined(LEAN_EMSCRIPTEN) *g_lean_path = "/library"; g_lean_path_vector->push_back(*g_lean_path); #else char * r = getenv("LEAN_PATH"); if (r == nullptr) { std::string exe_path = get_path(get_exe_location()); *g_lean_path = exe_path + g_sep + ".." + g_sep + "library"; *g_lean_path += g_path_sep; *g_lean_path += exe_path + g_sep + ".." + g_sep + "lib" + g_sep + "lean"; *g_lean_path += g_path_sep; *g_lean_path += "."; } else { *g_lean_path = r; } g_lean_path_vector->clear(); *g_lean_path = normalize_path(*g_lean_path); unsigned i = 0; unsigned j = 0; unsigned sz = g_lean_path->size(); for (; j < sz; j++) { if (is_path_sep((*g_lean_path)[j])) { if (j > i) g_lean_path_vector->push_back(g_lean_path->substr(i, j - i)); i = j + 1; } } if (j > i) g_lean_path_vector->push_back(g_lean_path->substr(i, j - i)); #endif }
/*================================================== * chop_path -- copy path into buff, & zero-separate all dirs * path: [IN] path list to copy * dirs: [OUT] output buffer * NB: dirs should be one byte larger than path * ignore zero length paths *================================================*/ INT chop_path (CNSTRING path, STRING dirs) { INT ndirs; STRING p; CNSTRING q; char c=0; ndirs=0; p = dirs;; q = path; while ((c = *q)) { if (is_path_sep(c)) { if (p == dirs || p[-1] == 0) { q++; } else { *p++ = 0; q++; ++ndirs; } } else { *p++ = *q++; } } if (!(p == dirs || p[-1] == 0)) { *p++ = 0; ++ndirs; } *p = 0; /* ends with extra trailing zero after last one */ return ndirs; }
int find_tup_dir(void) { struct stat st; if(getcwd(tup_wd, sizeof(tup_wd)) == NULL) { perror("getcwd"); fprintf(stderr, "tup error: Unable to get the current directory during tup initialization.\n"); exit(1); } tup_top_len = strlen(tup_wd); tup_sub_len = 0; for(;;) { if(stat(".tup", &st) == 0 && S_ISDIR(st.st_mode)) { tup_wd_offset = tup_top_len; while(is_path_sep(&tup_wd[tup_wd_offset])) { tup_wd_offset++; tup_sub_len--; } tup_wd[tup_top_len] = 0; break; } if(chdir("..") < 0) { perror("chdir"); exit(1); } while(tup_top_len > 0) { tup_top_len--; tup_sub_len++; if(is_path_sep(&tup_wd[tup_top_len])) { break; } } if(!tup_top_len) { return -1; } } return 0; }
int find_tup_dir(void) { struct stat st; if(getcwd(tup_wd, sizeof(tup_wd)) == NULL) { perror("getcwd"); exit(1); } tup_top_len = strlen(tup_wd); tup_sub_len = 0; for(;;) { if(stat(".tup", &st) == 0 && S_ISDIR(st.st_mode)) { tup_wd_offset = tup_top_len; while(is_path_sep(&tup_wd[tup_wd_offset])) { tup_wd_offset++; tup_sub_len--; } tup_wd[tup_top_len] = 0; break; } if(chdir("..") < 0) { perror("chdir"); exit(1); } while(tup_top_len > 0) { tup_top_len--; tup_sub_len++; if(is_path_sep(&tup_wd[tup_top_len])) { break; } } if(!tup_top_len) { return -1; } } return 0; }
/*=========================================== * zero_separate_path -- Zero separate dirs in path * (Also appends extra zero at end) * Assumes there are no empty directory entries. * Returns count of directories. *=========================================*/ static INT zero_separate_path (STRING path) { INT c=0, dirs=0; if (!path[0]) { path[1] = 0; return 0; } ++dirs; while ((c = (uchar)*path)) { if (is_path_sep((uchar)c)) { *path = 0; ++dirs; } ++path; } path[1] = 0; return dirs; }
optional<search_path> get_lean_path_from_env() { if (auto r = getenv("LEAN_PATH")) { auto lean_path = normalize_path(r); unsigned i = 0; unsigned j = 0; unsigned sz = static_cast<unsigned>(lean_path.size()); search_path path; for (; j < sz; j++) { if (is_path_sep(lean_path[j])) { if (j > i) path.push_back(lean_path.substr(i, j - i)); i = j + 1; } } if (j > i) path.push_back(lean_path.substr(i, j - i)); return optional<search_path>(path); } else { return optional<search_path>(); } }
static int mkdirtree(const char *dirname) { char *dirpart = strdup(dirname); char *p; if(!dirpart) { perror("strdup"); return -1; } p = dirpart; while(1) { char *slash = p; char slash_found = 0; while(*slash && !is_path_sep(slash)) { slash++; } if(*slash) { slash_found = *slash; *slash = 0; } if(mkdir(dirpart, 0777) < 0) { if(errno != EEXIST) { perror(dirpart); fprintf(stderr, "tup error: Unable to create directory '%s' for a tup repository.\n", dirname); return -1; } } if(slash_found) { *slash = slash_found; p = slash + 1; } else { break; } } free(dirpart); return 0; }