move all envirnment reading into readenv; rename isuptodate; make dir function arguments constant

This commit is contained in:
anon 2024-11-12 21:22:27 +01:00
parent ea8166963b
commit e31e3d9536
6 changed files with 53 additions and 46 deletions

View File

@ -435,15 +435,13 @@ bool is_source_file(char *path) {
}
/* add a source directory to the list for each view path source directory */
void sourcedir(char *dirlist) {
char path[PATHLEN + 1];
char *dir;
void sourcedir(const char * dirlist) {
char path[PATHLEN + 1];
make_vp_source_directories(); /* make the view source directory list */
dirlist = strdup(dirlist); /* don't change environment variable text */
char * mdirlist = strdup(dirlist); /* don't change environment variable text */
/* parse the directory list */
dir = strtok(dirlist, DIRSEPS);
char * dir = strtok(mdirlist, DIRSEPS);
while(dir != NULL) {
int dir_len = strlen(dir);
@ -451,7 +449,7 @@ void sourcedir(char *dirlist) {
/* if it isn't a full path name and there is a
multi-directory view path */
if(*dirlist != '/' && vpndirs > 1) {
if(*mdirlist != '/' && vpndirs > 1) {
/* compute its path from higher view path source dirs */
for(unsigned i = 1; i < nvpsrcdirs; ++i) {
@ -466,19 +464,20 @@ void sourcedir(char *dirlist) {
}
dir = strtok(NULL, DIRSEPS);
}
free(dirlist);
free(mdirlist);
}
// XXX: useless strdup
/* add a #include directory to the list for each view path source directory */
void includedir(char *dirlist) {
void includedir(const char * dirlist) {
char path[PATHLEN + 1];
char *dir;
make_vp_source_directories(); /* make the view source directory list */
dirlist = strdup(dirlist); /* don't change environment variable text */
char * mdirlist = strdup(dirlist); /* don't change environment variable text */
/* parse the directory list */
dir = strtok(dirlist, DIRSEPS);
dir = strtok(mdirlist, DIRSEPS);
while(dir != NULL) {
size_t dir_len = strlen(dir);
@ -486,7 +485,7 @@ void includedir(char *dirlist) {
/* if it isn't a full path name and there is a
multi-directory view path */
if(*dirlist != '/' && vpndirs > 1) {
if(*mdirlist != '/' && vpndirs > 1) {
/* compute its path from higher view path source dirs */
for(unsigned i = 1; i < nvpsrcdirs; ++i) {
@ -501,7 +500,7 @@ void includedir(char *dirlist) {
}
dir = strtok(NULL, DIRSEPS);
}
free(dirlist); /* HBB 20000421: avoid leaks */
free(mdirlist); /* HBB 20000421: avoid leaks */
}
/* make the source file list */
@ -668,7 +667,7 @@ void freefilelist(void) {
int i;
/* if '-d' option is used a string space block is allocated */
if(isuptodate == false) {
if(preserve_database == false) {
while(nsrcfiles > 0) {
free(srcfiles[--nsrcfiles]);
}

View File

@ -706,7 +706,7 @@ int findinit(const char *pattern_) {
}
/* look for use of the -T option (truncate symbol to 8
characters) on a database not built with -T */
if(trun_syms == true && isuptodate == true && dbtruncated == false &&
if(trun_syms == true && preserve_database == true && dbtruncated == false &&
s - pattern >= 8) {
strcpy(pattern + 8, ".*");
isregexp = true;

View File

@ -101,6 +101,7 @@ extern int input_mode;
#ifndef DFLT_INCDIR
# define DFLT_INCDIR "/usr/include"
#endif
extern const char * incdir;
/* digraph data for text compression */
extern char dichar1[]; /* 16 most frequent first chars */
@ -132,7 +133,7 @@ extern char **fileargv; /* file argument values */
extern int fileversion; /* cross-reference file version */
extern bool incurses; /* in curses */
extern bool invertedindex; /* the database has an inverted index */
extern bool isuptodate; /* consider the crossref up-to-date */
extern bool preserve_database; /* consider the crossref up-to-date */
extern bool kernelmode; /* don't use DFLT_INCDIR - bad for kernels */
extern bool linemode; /* use line oriented user interface */
extern bool verbosemode; /* print extra information on line mode */
@ -229,7 +230,7 @@ char *read_block(void);
char *scanpast(char c);
char **parse_options(int *argc, char **argv);
void readenv(void);
void readenv(bool preserve_database);
void error_usage(void);
void longusage(void);
@ -284,7 +285,7 @@ void freecrossref(void);
void freefilelist(void);
const char *help(void);
void incfile(char *file, char *type);
void includedir(char *_dirname);
void includedir(const char *dirname);
void initsymtab(void);
void makefilelist(void);
void mousecleanup(void);
@ -302,7 +303,7 @@ void postfatal(const char *msg, ...);
void putposting(char *term, int type);
void fetch_string_from_dbase(char *, size_t);
void shellpath(char *out, int limit, char *in);
void sourcedir(char *dirlist);
void sourcedir(const char * dirlist);
void myungetch(int c);
void warning(char *text);
void writestring(char *s);

View File

@ -70,7 +70,7 @@ static void catchint(int sig) {
}
static inline bool rebuild_reference() {
if(isuptodate == true) {
if(preserve_database == true) {
postmsg("The -d option prevents rebuilding the symbol database");
return false;
}

View File

@ -67,13 +67,6 @@ unsigned int fileargc; /* file argument count */
char **fileargv; /* file argument values */
int fileversion; /* cross-reference file version */
bool incurses = false; /* in curses */
bool invertedindex; /* the database has an inverted index */
bool isuptodate; /* consider the crossref up-to-date */
bool kernelmode; /* don't use DFLT_INCDIR - bad for kernels */
bool linemode = false; /* use line oriented user interface */
bool verbosemode = false; /* print extra information on line mode */
bool recurse_dir = false; /* recurse dirs when searching for src files */
char *namefile; /* file of file names */
char *prependpath; /* prepend path to file names */
FILE *refsfound; /* references found file */
long totalterms; /* total inverted index terms */
@ -295,9 +288,6 @@ int main(int argc, char **argv) {
FILE *oldrefs; /* old cross-reference file */
char *s;
unsigned int i;
pid_t pid;
struct stat stat_buf;
mode_t orig_umask;
yyin = stdin;
yyout = stdout;
@ -308,7 +298,9 @@ int main(int argc, char **argv) {
argv = parse_options(&argc, argv);
/* read the environment */
readenv();
/* NOTE: the envirnment under no condition can overwrite cli set variables
*/
readenv(preserve_database);
/* XXX */
init_temp_files();
@ -343,7 +335,7 @@ int main(int argc, char **argv) {
/* if the cross-reference is to be considered up-to-date */
if(isuptodate == true) {
if(preserve_database == true) {
if((oldrefs = vpfopen(reffile, "rb")) == NULL) {
postfatal(PROGRAM_NAME ": cannot open file %s\n", reffile);
/* NOTREACHED */
@ -462,8 +454,6 @@ int main(int argc, char **argv) {
fileargc = argc;
fileargv = argv;
/* get source directories from the environment */
if((s = getenv("SOURCEDIRS")) != NULL) { sourcedir(s); }
/* make the source file list */
srcfiles = malloc(msrcfiles * sizeof(*srcfiles));
makefilelist();
@ -471,16 +461,11 @@ int main(int argc, char **argv) {
postfatal(PROGRAM_NAME ": no source files found\n");
/* NOTREACHED */
}
/* get include directories from the environment */
if((s = getenv("INCLUDEDIRS")) != NULL) { includedir(s); }
/* add /usr/include to the #include directory list,
but not in kernelmode... kernels tend not to use it. */
if(kernelmode == false) {
if(NULL != (s = getenv("INCDIR"))) {
includedir(s);
} else {
includedir(DFLT_INCDIR);
}
if(!kernelmode) {
includedir(incdir);
}
/* initialize the C keyword table */

View File

@ -22,10 +22,22 @@ char * shell;
char * lineflag;
char * tmpdir;
bool lineflagafterfile;
/* XXX: this might be a war crime;
* its currently required for respecting kernel mode
*/
const char * incdir = NULL;
/* option holders */
bool remove_symfile_onexit = false;
bool onesearch; /* one search only in line mode */
char *reflines; /* symbol reference lines file */
bool onesearch; /* one search only in line mode */
char *reflines; /* symbol reference lines file */
bool invertedindex; /* the database has an inverted index */
bool preserve_database = false; /* consider the crossref up-to-date */
bool kernelmode; /* don't use DFLT_INCDIR - bad for kernels */
bool linemode = false; /* use line oriented user interface */
bool verbosemode = false; /* print extra information on line mode */
bool recurse_dir = false; /* recurse dirs when searching for src files */
char *namefile; /* file of file names */
/* From a list of envirnment variable names,
* return the first valid variable value
@ -57,7 +69,7 @@ char * _coalesce_env(char * mydefault, size_t argc, ...) {
/* XXX: Add CSOPE_* equivalents while preserving the originals.
* DO NOT do it without writting documentation
*/
void readenv(void) {
void readenv(bool preserve_database) {
editor = coalesce_env(DEFAULT_EDITOR, "CSCOPE_EDITOR", "VIEWER", "EDITOR");
home = coalesce_env(DEFAULT_HOME, "HOME");
shell = coalesce_env(DEFAULT_SHELL, "SHELL");
@ -65,6 +77,16 @@ void readenv(void) {
tmpdir = coalesce_env(DEFAULT_TMPDIR, "TMPDIR");
lineflagafterfile = getenv("CSCOPE_LINEFLAG_AFTER_FILE") ? 1 : 0;
if (!preserve_database) {
incdir = coalesce_env(DFLT_INCDIR, "INCDIR");
/* get source directories from the environment */
const char * const my_source_dirs = getenv("SOURCEDIRS");
if(my_source_dirs) { sourcedir(my_source_dirs); }
/* get include directories from the environment */
const char * const my_include_dirs = getenv("INCLUDEDIRS");
if(my_include_dirs) { includedir(my_source_dirs); }
}
}
char **parse_options(int *argc, char **argv) {
@ -133,7 +155,7 @@ char **parse_options(int *argc, char **argv) {
egrepcaseless(caseless); /* simulate egrep -i flag */
break;
case 'd': /* consider crossref up-to-date */
isuptodate = true;
preserve_database = true;
break;
case 'e': /* suppress ^E prompt between files */
editallprompt = false;