From d0ed6fd30dad0591f735f576ecf473e5243ed766 Mon Sep 17 00:00:00 2001 From: Frank Kotler Date: Wed, 27 Aug 2003 11:33:56 +0000 Subject: [PATCH] Alexei's patch to allow "-I" paths to be searched for "incbin"ed files --- AUTHORS | 8 +++++++ CHANGES | 2 ++ assemble.c | 48 +++++++++++++++++++++++++++++++++++++++-- doc/nasmdoc.src | 7 +++--- preproc.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- preproc.h | 1 + 6 files changed, 124 insertions(+), 8 deletions(-) diff --git a/AUTHORS b/AUTHORS index 967fafa..a82f85e 100644 --- a/AUTHORS +++ b/AUTHORS @@ -109,3 +109,11 @@ N: Michael K. Ter Louw E: mterlo1 "at" uic "dot" edu D: Multisection support for "-f bin" +N: Martin Wawro +E: FIXME +D: stabs debug support for "-f elf" + +N: Alexei Frounze +E: alexfru@users.sourceforge.net +D: "-I" paths searched for "incbined" files +D: bugswatting diff --git a/CHANGES b/CHANGES index 39a79c6..3557617 100644 --- a/CHANGES +++ b/CHANGES @@ -1,5 +1,7 @@ 0.98.37 ------- +* Paths given in "-I" switch searched for "incbin"ed as + well as "%include"ed files. * Added stabs debugging for the ELF output format, patch from Martin Wawro. * Fix output/outbin.c to allow origin > 80000000h diff --git a/assemble.c b/assemble.c index 6da584a..160e671 100644 --- a/assemble.c +++ b/assemble.c @@ -74,6 +74,7 @@ #include "nasmlib.h" #include "assemble.h" #include "insns.h" +#include "preproc.h" extern struct itemplate *nasm_instructions[]; @@ -267,6 +268,8 @@ long assemble (long segment, long offset, int bits, unsigned long cp, static char fname[FILENAME_MAX]; FILE * fp; long len; + char *prefix = "", *combine; + char** pPrevPath = NULL; len = FILENAME_MAX-1; if (len > instruction->eops->stringlen) @@ -274,7 +277,26 @@ long assemble (long segment, long offset, int bits, unsigned long cp, strncpy (fname, instruction->eops->stringval, len); fname[len] = '\0'; - if ( (fp = fopen(fname, "rb")) == NULL) + while (1) /* added by alexfru: 'incbin' uses include paths */ + { + combine = nasm_malloc(strlen(prefix) + len + 1); + strcpy(combine, prefix); + strcat(combine, fname); + + if ( (fp = fopen(combine, "rb")) != NULL) + { + nasm_free(combine); + break; + } + + nasm_free(combine); + pPrevPath = pp_get_include_path_ptr (pPrevPath); + if (pPrevPath == NULL) + break; + prefix = *pPrevPath; + } + + if (fp == NULL) error (ERR_NONFATAL, "`incbin': unable to open file `%s'", fname); else if (fseek(fp, 0L, SEEK_END) < 0) error (ERR_NONFATAL, "`incbin': unable to seek on file `%s'", @@ -489,13 +511,35 @@ long insn_size (long segment, long offset, int bits, unsigned long cp, char fname[FILENAME_MAX]; FILE * fp; long len; + char *prefix = "", *combine; + char** pPrevPath = NULL; len = FILENAME_MAX-1; if (len > instruction->eops->stringlen) len = instruction->eops->stringlen; strncpy (fname, instruction->eops->stringval, len); fname[len] = '\0'; - if ( (fp = fopen(fname, "rb")) == NULL ) + + while (1) /* added by alexfru: 'incbin' uses include paths */ + { + combine = nasm_malloc(strlen(prefix) + len + 1); + strcpy(combine, prefix); + strcat(combine, fname); + + if ( (fp = fopen(combine, "rb")) != NULL) + { + nasm_free(combine); + break; + } + + nasm_free(combine); + pPrevPath = pp_get_include_path_ptr (pPrevPath); + if (pPrevPath == NULL) + break; + prefix = *pPrevPath; + } + + if (fp == NULL) error (ERR_NONFATAL, "`incbin': unable to open file `%s'", fname); else if (fseek(fp, 0L, SEEK_END) < 0) error (ERR_NONFATAL, "`incbin': unable to seek on file `%s'", diff --git a/doc/nasmdoc.src b/doc/nasmdoc.src index 795cf53..60bd694 100644 --- a/doc/nasmdoc.src +++ b/doc/nasmdoc.src @@ -599,8 +599,9 @@ See also the \c{-E} option, \k{opt-E}. \S{opt-i} The \i\c{-i}\I\c{-I} Option: Include File Search Directories -When NASM sees the \i\c{%include} directive in a source file (see -\k{include}), it will search for the given file not only in the +When NASM sees the \i\c{%include} or \i\c{incbin} directive in +a source file (see \k{include} or \k{incbin}), +it will search for the given file not only in the current directory, but also in any directories specified on the command line by the use of the \c{-i} option. Therefore you can include files from a \i{macro library}, for example, by typing @@ -621,8 +622,6 @@ Under Unix, a trailing forward slash is similarly necessary. by noting that the option \c{-ifoo} will cause \c{%include "bar.i"} to search for the file \c{foobar.i}...) -\#FIXME - the above is not true - see the "backslash()" function - If you want to define a \e{standard} \i{include search path}, similar to \c{/usr/include} on Unix systems, you should place one or more \c{-i} directives in the \c{NASMENV} environment variable (see diff --git a/preproc.c b/preproc.c index 2492df6..8974945 100644 --- a/preproc.c +++ b/preproc.c @@ -4358,12 +4358,74 @@ void pp_include_path(char *path) { IncPath *i; - +/* by alexfru: order of path inclusion fixed (was reverse order) */ i = nasm_malloc(sizeof(IncPath)); i->path = nasm_strdup(path); - i->next = ipath; + i->next = NULL; + + if (ipath != NULL) + { + IncPath *j = ipath; + while (j->next != NULL) + j = j->next; + j->next = i; + } + else + { ipath = i; } +} + +/* + * added by alexfru: + * + * This function is used to "export" the include paths, e.g. + * the paths specified in the '-I' command switch. + * The need for such exporting is due to the 'incbin' directive, + * which includes raw binary files (unlike '%include', which + * includes text source files). It would be real nice to be + * able to specify paths to search for incbin'ned files also. + * So, this is a simple workaround. + * + * The function use is simple: + * + * The 1st call (with NULL argument) returns a pointer to the 1st path + * (char** type) or NULL if none include paths available. + * + * All subsequent calls take as argument the value returned by this + * function last. The return value is either the next path + * (char** type) or NULL if the end of the paths list is reached. + * + * It is maybe not the best way to do things, but I didn't want + * to export too much, just one or two functions and no types or + * variables exported. + * + * Can't say I like the current situation with e.g. this path list either, + * it seems to be never deallocated after creation... + */ +char** +pp_get_include_path_ptr (char **pPrevPath) +{ +/* This macro returns offset of a member of a structure */ +#define GetMemberOffset(StructType,MemberName)\ + ((size_t)&((StructType*)0)->MemberName) + IncPath *i; + + if (pPrevPath == NULL) + { + if(ipath != NULL) + return &ipath->path; + else + return NULL; + } + i = (IncPath*) ((char*)pPrevPath - GetMemberOffset(IncPath,path)); + i = i->next; + if (i != NULL) + return &i->path; + else + return NULL; +#undef GetMemberOffset +} void pp_pre_include(char *fname) diff --git a/preproc.h b/preproc.h index 0b7df11..e196d38 100644 --- a/preproc.h +++ b/preproc.h @@ -10,6 +10,7 @@ #define NASM_PREPROC_H void pp_include_path (char *); +char** pp_get_include_path_ptr (char **pPrevPath); void pp_pre_include (char *); void pp_pre_define (char *); void pp_pre_undefine (char *); -- 2.7.4