/* * addfile(), nextfile(): maintain a circular buffer of file names */ #include "m.h" #ifdef __unix__ # define LIBRARY "/usr/local/lib/m/" #endif #ifdef vms # define LIBRARY "esl$dir:[lib.m]" #endif #ifdef MSDOS # define LIBRARY "C:\\m\\lib\\" /* not used anymore */ #endif #ifndef LIBRARY # define LIBRARY "" #endif #define MAXFILES 128 static char *files[MAXFILES]; static char **start = files; /* location to return for nextfile */ static char **stop = files; /* next free location for addfile() */ static int nfiles = 0; /* number of file names in buffer */ #define inc(p) if( ++p >= files + MAXFILES) p = files void addfile( char *name) { if( nfiles == MAXFILES) { yyerror("file list overflow in addfile()"); return; } if( (*stop = strdup(name)) == NULL) yyerror("strdup() failed in addfile()"); else { inc( stop); ++nfiles; } } /* * I think this routine is no longer needed. * load is handled by #include in scan.l */ FILE *nextfile( void) { FILE *f = NULL; char name[BUFSIZ]; while( nfiles > 0) { #ifdef DEBUG if( prog_debug) dprint("nextfile: %s\n", *start); #endif --nfiles; if( (f = fopen( *start, "r")) == NULL) { #ifdef MSDOS (void) strcat( strcat( strcpy( name, exedir), "lib\\"), *start); #else (void) strcat( strcpy( name, LIBRARY), *start); #endif if( (f = fopen( name, "r")) == NULL) dprint("nextfile: could not open %s or %s\n", *start, name); } free( *start); inc(start); if( f) break; } return (f == NULL) ? stdin : f; } /* * include_name will contain no ' ', '\t', or '\n' when open_include is called * from yylex. It should be in one of the forms: name, , "name" */ FILE *open_include( const char *include_name) { char name[BUFSIZ]; /* copy of include_name for strtok() */ char *cwd_name; /* strtok'd name */ char lib_name[BUFSIZ]; /* Library path + cwd_name */ char *p1, *p2; /* paths to check first and second */ FILE *f; /* fopen() result */ #ifdef DEBUG if( prog_debug) dprint("open_include: %s\n", include_name); #endif strcpy( name, include_name); cwd_name = strtok( name, "\"<>"); if( cwd_name == NULL) { /* bad name, like "" or <> */ yyerror( "open_include: bad file name"); return NULL; } #ifdef MSDOS strcat( strcat( strcpy( lib_name, exedir), "lib\\"), cwd_name); #else strcat( strcpy( lib_name, LIBRARY), cwd_name); #endif if( *include_name == '<') { /* search M library first */ p1 = lib_name; p2 = cwd_name; } else { /* search cwd first */ p1 = cwd_name; p2 = lib_name; } #ifdef DEBUG if( prog_debug) dprint("open_include: trying %s then %s\n", p1, p2); #endif if( (f = fopen( p1, "r")) == NULL && (f = fopen( p2, "r")) == NULL) dprint("could not open %s or %s\n", p1, p2); return f; }