Alexei's patch to allow "-I" paths to be searched for "incbin"ed files
authorFrank Kotler <fbkotler@users.sourceforge.net>
Wed, 27 Aug 2003 11:33:56 +0000 (11:33 +0000)
committerFrank Kotler <fbkotler@users.sourceforge.net>
Wed, 27 Aug 2003 11:33:56 +0000 (11:33 +0000)
AUTHORS
CHANGES
assemble.c
doc/nasmdoc.src
preproc.c
preproc.h

diff --git a/AUTHORS b/AUTHORS
index 967fafa570ed10ae4adc34cbe80b33501c0f2cb5..a82f85e0096332ab77741f23487c8b026dea6816 100644 (file)
--- 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 39a79c603f6c723d55391bc00377e0819b777530..355761793cd8531f342bd298e9dd2b6bc2f4bb55 100644 (file)
--- 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
index 6da584a556fc4fe8f20a3c417f15b2565e621227..160e6713d1ef3fd67c51df17f3113205f128f8ab 100644 (file)
@@ -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'",
index 795cf53fb1395479ef99f7332bdcb1e64175b178..60bd694b1a1d97efd7107a3371bf34bd9b253ff2 100644 (file)
@@ -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
index 2492df6840bbff6c73e647649e98e30aaae0d7a7..8974945323a082d0514d11a1eb27421f91f3057a 100644 (file)
--- 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)
index 0b7df114e62db8b67438e776cd186aad415e1a4d..e196d38e560fcd21cacf38434c973c9dbe7f5676 100644 (file)
--- 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 *);