From 0420621ce785c1a18080a1ef7b385ef0518c96e0 Mon Sep 17 00:00:00 2001 From: Marcelo Matus Date: Fri, 17 Sep 2004 05:55:43 +0000 Subject: [PATCH] added the -I- option and %include git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@6249 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Source/Modules/main.cxx | 13 ++++-- Source/Preprocessor/cpp.c | 24 ++++++----- Source/Swig/include.c | 91 ++++++++++++++++++++++++++++++++------- Source/Swig/swig.h | 3 ++ 4 files changed, 100 insertions(+), 31 deletions(-) diff --git a/Source/Modules/main.cxx b/Source/Modules/main.cxx index 115fb2f20..99c5db154 100644 --- a/Source/Modules/main.cxx +++ b/Source/Modules/main.cxx @@ -59,6 +59,7 @@ static const char *usage1 = (const char*)"\ -Fmicrosoft - Display error/warning messages in Microsoft format\n\ -help - This output\n\ -I - Look for SWIG files in \n\ + -I- - Don't search the current directory\n\ -ignoremissing - Ignore missing include files\n\ -importall - Follow all #include statements as imports\n\ -includeall - Follow all #include statements\n\ @@ -343,7 +344,11 @@ int SWIG_main(int argc, char *argv[], Language *l) { // Get options for (i = 1; i < argc; i++) { if (argv[i]) { - if (strncmp(argv[i],"-I",2) == 0) { + if (strncmp(argv[i],"-I-",3) == 0) { + // Don't push/pop directories + Swig_set_push_dir(0); + Swig_mark_arg(i); + } else if (strncmp(argv[i],"-I",2) == 0) { // Add a new directory search path includefiles[includecount++] = Swig_copy_string(argv[i]+2); Swig_mark_arg(i); @@ -633,12 +638,12 @@ int SWIG_main(int argc, char *argv[], Language *l) { SWIG_exit (EXIT_FAILURE); } fclose(df); - Printf(fs,"%%include \"swig.swg\"\n"); + Printf(fs,"%%include \n"); if (allkw) { - Printf(fs,"%%include \"allkw.swg\"\n"); + Printf(fs,"%%include \n"); } if (lang_config) { - Printf(fs,"\n%%include \"%s\"\n", lang_config); + Printf(fs,"\n%%include <%s>\n", lang_config); } Printf(fs,"%%include \"%s\"\n", Swig_last_file()); for (i = 0; i < Len(libfiles); i++) { diff --git a/Source/Preprocessor/cpp.c b/Source/Preprocessor/cpp.c index fd9912129..faf525102 100644 --- a/Source/Preprocessor/cpp.c +++ b/Source/Preprocessor/cpp.c @@ -77,9 +77,9 @@ copy_location(DOH *s1, DOH *s2) { Setline(s2,Getline(s1)); } -static String *cpp_include(String_or_char *fn) { +static String *cpp_include(String_or_char *fn, int sysfile) { String *s; - s = Swig_include(fn); + s = sysfile ? Swig_include_sys(fn) : Swig_include(fn); if (s && single_include) { String *file = Getfile(s); if (Getattr(included_files,file)) { @@ -484,7 +484,7 @@ find_args(String *s) * ----------------------------------------------------------------------------- */ static String * -get_filename(String *str) { +get_filename(String *str, int* sysfile) { String *fn; int c; @@ -492,9 +492,11 @@ get_filename(String *str) { fn = NewString(""); copy_location(str,fn); c = Getc(str); + *sysfile = 0; if (c == '\"') { while (((c = Getc(str)) != EOF) && (c != '\"')) Putc(c,fn); } else if (c == '<') { + *sysfile = 1; while (((c = Getc(str)) != EOF) && (c != '>')) Putc(c,fn); } else { Putc(c,fn); @@ -1363,10 +1365,10 @@ Preprocessor_parse(String *s) } else if (Cmp(id,"include") == 0) { if (((include_all) || (import_all)) && (allow)) { String *s1, *s2, *fn; - char *dirname; + char *dirname; int sysfile = 0; Seek(value,0,SEEK_SET); - fn = get_filename(value); - s1 = cpp_include(fn); + fn = get_filename(value, &sysfile); + s1 = cpp_include(fn, sysfile); if (s1) { if (include_all) Printf(ns,"%%includefile \"%s\" [\n", Swig_last_file()); @@ -1375,7 +1377,7 @@ Preprocessor_parse(String *s) /* See if the filename has a directory component */ dirname = Swig_file_dirname(Swig_last_file()); - if (!strlen(dirname)) dirname = 0; + if (sysfile || !strlen(dirname)) dirname = 0; if (dirname) { dirname[strlen(dirname)-1] = 0; /* Kill trailing directory delimeter */ Swig_push_directory(dirname); @@ -1478,7 +1480,7 @@ Preprocessor_parse(String *s) if ((Cmp(decl,"%include") == 0) || (Cmp(decl,"%import") == 0) || (Cmp(decl,"%extern") == 0)) { /* Got some kind of file inclusion directive */ if (allow) { - DOH *s1, *s2, *fn, *opt; + DOH *s1, *s2, *fn, *opt; int sysfile = 0; if (Cmp(decl,"%extern") == 0) { Swig_warning(WARN_DEPRECATED_EXTERN, Getfile(s),Getline(s),"%%extern is deprecated. Use %%import instead.\n"); @@ -1486,8 +1488,8 @@ Preprocessor_parse(String *s) Printf(decl,"%%import"); } opt = get_options(s); - fn = get_filename(s); - s1 = cpp_include(fn); + fn = get_filename(s, &sysfile); + s1 = cpp_include(fn, sysfile); if (s1) { char *dirname; add_chunk(ns,chunk,allow); @@ -1498,7 +1500,7 @@ Preprocessor_parse(String *s) Preprocessor_define("SWIGIMPORT 1", 0); } dirname = Swig_file_dirname(Swig_last_file()); - if (!strlen(dirname)) dirname = 0; + if (sysfile || !strlen(dirname)) dirname = 0; if (dirname) { dirname[strlen(dirname)-1] = 0; /* Kill trailing directory delimeter */ Swig_push_directory(dirname); diff --git a/Source/Swig/include.c b/Source/Swig/include.c index 5a1edf381..d79f5dcd7 100644 --- a/Source/Swig/include.c +++ b/Source/Swig/include.c @@ -21,6 +21,17 @@ static List *directories = 0; /* List of include directories */ static String *lastpath = 0; /* Last file that was included */ static String *swiglib = 0; /* Location of SWIG library */ static String *lang_config = 0; /* Language configuration file */ +static int dopush = 1; /* Whether to push directories */ + +/* This functions determine whether to push/pop dirs in the preprocessor */ +void Swig_set_push_dir(int push) { + dopush = push; +} + +int Swig_get_push_dir(void) { + return dopush; +} + /* This function sets the name of the configuration file */ @@ -59,15 +70,17 @@ Swig_swiglib_get() { void Swig_add_directory(const String_or_char *dirname) { + String *dir = 0; if (!directories) directories = NewList(); assert(directories); - if (!DohIsString(dirname)) { - dirname = NewString((char *) dirname); - assert(dirname); - } - Append(directories, dirname); + dir = NewString((char *) dirname); + assert(dir); + Setattr(dir,"sysdir","1"); + Append(directories, dir); } + + /* ----------------------------------------------------------------------------- * Swig_push_directory() * @@ -77,6 +90,7 @@ Swig_add_directory(const String_or_char *dirname) { void Swig_push_directory(const String_or_char *dirname) { + if (!Swig_get_push_dir()) return; if (!directories) directories = NewList(); assert(directories); if (!DohIsString(dirname)) { @@ -95,6 +109,7 @@ Swig_push_directory(const String_or_char *dirname) { void Swig_pop_directory() { + if (!Swig_get_push_dir()) return; if (!directories) return; Delitem(directories,0); } @@ -117,13 +132,14 @@ Swig_last_file() { * Returns a list of the current search paths. * ----------------------------------------------------------------------------- */ -List * -Swig_search_path() { +static List * +Swig_search_path_any(int syspath) { String *filename; String *dirname; - List *slist; + List *slist, *llist; int i; + llist = 0; slist = NewList(); assert(slist); filename = NewString(""); @@ -133,25 +149,50 @@ Swig_search_path() { #else Printf(filename,".%s", SWIG_FILE_DELIMETER); #endif - Append(slist,filename); + if (syspath) { + llist = NewList(); + assert(slist); + Append(llist,filename); + } else { + Append(slist,filename); + } + for (i = 0; i < Len(directories); i++) { dirname = Getitem(directories,i); filename = NewString(""); assert(filename); Printf(filename, "%s%s", dirname, SWIG_FILE_DELIMETER); - Append(slist,filename); + if (syspath && !Getattr(dirname,"sysdir")) { + Append(llist,filename); + } else { + Append(slist,filename); + } } + if (syspath) { + for (i = 0; i < Len(llist); i++) { + Append(slist,Getitem(llist,i)); + } + Delete(llist); + } + return slist; } +List * +Swig_search_path() { + return Swig_search_path_any(0); +} + + + /* ----------------------------------------------------------------------------- * Swig_open() * * Looks for a file and open it. Returns an open FILE * on success. * ----------------------------------------------------------------------------- */ -FILE * -Swig_open(const String_or_char *name) { +static FILE * +Swig_open_any(const String_or_char *name, int sysfile) { FILE *f; String *filename; List *spath = 0; @@ -166,7 +207,7 @@ Swig_open(const String_or_char *name) { assert(filename); f = fopen(Char(filename),"r"); if (!f) { - spath = Swig_search_path(); + spath = Swig_search_path_any(sysfile); for (i = 0; i < Len(spath); i++) { Clear(filename); Printf(filename,"%s%s", Getitem(spath,i), cname); @@ -187,6 +228,13 @@ Swig_open(const String_or_char *name) { return f; } +FILE * +Swig_open(const String_or_char *name) { + return Swig_open_any(name, 0); +} + + + /* ----------------------------------------------------------------------------- * Swig_read_file() * @@ -212,12 +260,12 @@ Swig_read_file(FILE *f) { * Opens a file and returns it as a string. * ----------------------------------------------------------------------------- */ -String * -Swig_include(const String_or_char *name) { +static String * +Swig_include_any(const String_or_char *name, int sysfile) { FILE *f; String *str; - f = Swig_open(name); + f = Swig_open_any(name, sysfile); if (!f) return 0; str = Swig_read_file(f); fclose(f); @@ -227,6 +275,17 @@ Swig_include(const String_or_char *name) { return str; } +String * +Swig_include(const String_or_char *name) { + return Swig_include_any(name, 0); +} + +String * +Swig_include_sys(const String_or_char *name) { + return Swig_include_any(name, 1); +} + + /* ----------------------------------------------------------------------------- * Swig_insert_file() * diff --git a/Source/Swig/swig.h b/Source/Swig/swig.h index 32c4a3487..ab5dca959 100644 --- a/Source/Swig/swig.h +++ b/Source/Swig/swig.h @@ -99,9 +99,12 @@ extern List *Swig_search_path(); extern FILE *Swig_open(const String_or_char *name); extern String *Swig_read_file(FILE *f); extern String *Swig_include(const String_or_char *name); +extern String *Swig_include_sys(const String_or_char *name); extern int Swig_insert_file(const String_or_char *name, File *outfile); extern void Swig_set_config_file(const String_or_char *filename); extern String *Swig_get_config_file(void); +extern void Swig_set_push_dir(int dopush); +extern int Swig_get_push_dir(void); extern void Swig_swiglib_set(const String_or_char *); extern String *Swig_swiglib_get(); extern void Swig_register_filebyname(const String_or_char *filename, File *outfile);