# HG changeset patch # User tuomov # Date 960051643 -7200 # Node ID 91f0c87b62a732cf78c5dde295a01b705ba7c28b # Parent 9eb09d246c9fe4e1eac78279711a9e5ac1b7a5f0 trunk: changeset 17 Default include path support diff -r 9eb09d246c9f -r 91f0c87b62a7 include/libtu/tokenizer.h --- a/include/libtu/tokenizer.h Fri May 26 18:34:04 2000 +0200 +++ b/include/libtu/tokenizer.h Sat Jun 03 19:00:43 2000 +0200 @@ -172,18 +172,22 @@ int filestack_n; Tokenizer_FInfo *filestack; + + char **includepaths; } Tokenizer; extern Tokenizer *tokz_open(const char *fname); -extern Tokenizer *tokz_open_file(FILE *file); +extern Tokenizer *tokz_open_file(FILE *file, const char *fname); extern void tokz_close(Tokenizer *tokz); extern bool tokz_get_token(Tokenizer *tokz, Token *tok); extern void tokz_unget_token(Tokenizer *tokz, Token *tok); extern void tokz_warn_error(const Tokenizer *tokz, int line, int e); extern bool tokz_pushf(Tokenizer *tokz, const char *fname); -extern bool tokz_pushf_file(Tokenizer *tokz, FILE *file); +extern bool tokz_pushf_file(Tokenizer *tokz, FILE *file, const char *fname); extern bool tokz_popf(Tokenizer *tokz); +extern void tokz_set_includepaths(Tokenizer *tokz, char **paths); + #endif /* LIBTU_TOKENIZER_H */ diff -r 9eb09d246c9f -r 91f0c87b62a7 parser.c --- a/parser.c Fri May 26 18:34:04 2000 +0200 +++ b/parser.c Sat Jun 03 19:00:43 2000 +0200 @@ -6,6 +6,7 @@ */ #include +#include #include #include @@ -423,7 +424,7 @@ Tokenizer *tokz; bool ret; - tokz=tokz_open_file(file); + tokz=tokz_open_file(file, NULL); if(tokz==NULL) return FALSE; @@ -581,35 +582,81 @@ /* */ -static bool opt_include(Tokenizer *tokz, int n, Token *toks) +static bool try_include(Tokenizer *tokz, const char *fname) { - const char *fname=TOK_STRING_VAL(toks+1); - const char *lastndx; + FILE *f; + + f=fopen(fname, "r"); + + if(f==NULL) + return FALSE; + + if(!tokz_pushf_file(tokz, f, fname)){ + fclose(f); + return FALSE; + } + + return TRUE; +} + + +static bool try_include_dir(Tokenizer *tokz, const char *dir, int dlen, + const char *file) +{ char *tmpname; bool retval; - if(fname[0]=='/' || tokz->name==NULL) - goto thisdir; - - lastndx=strrchr(tokz->name, '/'); - - if(lastndx==NULL) - goto thisdir; - - tmpname=scatn(tokz->name, lastndx-tokz->name+1, fname, -1); + tmpname=scatn(dir, dlen, file, -1); if(tmpname==NULL){ warn_err(); return FALSE; } - retval=tokz_pushf(tokz, tmpname); - + retval=try_include(tokz, tmpname); + free(tmpname); - + return retval; - -thisdir: - return tokz_pushf(tokz, fname); } + +static bool opt_include(Tokenizer *tokz, int n, Token *toks) +{ + const char *fname=TOK_STRING_VAL(toks+1); + const char *lastndx=NULL; + bool retval, e; + int i=0; + + if(fname[0]!='/' && tokz->name!=NULL) + lastndx=strrchr(tokz->name, '/'); + + if(lastndx==NULL) + retval=try_include(tokz, fname); + else + retval=try_include_dir(tokz, tokz->name, lastndx-tokz->name+1, fname); + + if(retval==TRUE) + return TRUE; + + e=errno; + + if(tokz->includepaths!=NULL){ + while(tokz->includepaths[i]!=NULL){ + if(try_include_dir(tokz, tokz->includepaths[i], -1, fname)) + return TRUE; + i++; + } + } + + warn_obj(fname, "%s", strerror(e)); + + return FALSE; +} + + +extern void tokz_set_includepaths(Tokenizer *tokz, char **paths) +{ + tokz->includepaths=paths; +} + diff -r 9eb09d246c9f -r 91f0c87b62a7 tokenizer.c --- a/tokenizer.c Fri May 26 18:34:04 2000 +0200 +++ b/tokenizer.c Sat Jun 03 19:00:43 2000 +0200 @@ -718,20 +718,32 @@ } -bool tokz_pushf_file(Tokenizer *tokz, FILE *file) +bool tokz_pushf_file(Tokenizer *tokz, FILE *file, const char *fname) { + char *fname_copy=NULL; + if(file==NULL) return FALSE; - - if(tokz->file!=NULL){ - if(!do_tokz_pushf(tokz)){ + + if(fname!=NULL){ + fname_copy=scopy(fname); + if(fname_copy==NULL){ warn_err(); return FALSE; } } + if(tokz->file!=NULL){ + if(!do_tokz_pushf(tokz)){ + warn_err(); + if(fname_copy!=NULL) + free(fname_copy); + return FALSE; + } + } + tokz->file=file; - tokz->name=NULL; + tokz->name=fname_copy; tokz->line=1; tokz->ungetc=-1; tokz->ungettok.type=TOK_INVALID; @@ -743,7 +755,6 @@ bool tokz_pushf(Tokenizer *tokz, const char *fname) { FILE *file; - char *fname_copy; file=fopen(fname, "r"); @@ -752,25 +763,12 @@ return FALSE; } - fname_copy=scopy(fname); - - if(fname_copy==NULL){ - warn_err(); - goto err1; + if(!tokz_pushf_file(tokz, file, fname)){ + fclose(file); + return FALSE; } - if(!tokz_pushf_file(tokz, file)) - goto err2; - - tokz->name=fname_copy; - return TRUE; - -err2: - free(fname_copy); -err1: - fclose(file); - return FALSE; } @@ -816,13 +814,13 @@ } -Tokenizer *tokz_open_file(FILE *file) +Tokenizer *tokz_open_file(FILE *file, const char *fname) { Tokenizer *tokz; tokz=tokz_create(); - if(!tokz_pushf_file(tokz, file)){ + if(!tokz_pushf_file(tokz, file, fname)){ free(tokz); return NULL; }