]> granicus.if.org Git - yasm/commitdiff
Refactor include file handling into libyasm from preprocessors.
authorPeter Johnson <peter@tortall.net>
Sat, 28 Oct 2006 21:31:38 +0000 (21:31 -0000)
committerPeter Johnson <peter@tortall.net>
Sat, 28 Oct 2006 21:31:38 +0000 (21:31 -0000)
svn path=/trunk/yasm/; revision=1669

frontends/yasm/yasm.c
libyasm/file.c
libyasm/file.h
libyasm/preproc.h
modules/preprocs/nasm/nasm-pp.c
modules/preprocs/nasm/nasm-pp.h
modules/preprocs/nasm/nasm-preproc.c

index 3281ad89c4f082aaac7c0b15dbef889af6a43073..85b38f955df7e5d06178497935d517e1603c93f4 100644 (file)
@@ -94,6 +94,7 @@ static int opt_objfile_handler(char *cmd, /*@null@*/ char *param, int extra);
 static int opt_machine_handler(char *cmd, /*@null@*/ char *param, int extra);
 static int opt_warning_handler(char *cmd, /*@null@*/ char *param, int extra);
 static int preproc_only_handler(char *cmd, /*@null@*/ char *param, int extra);
+static int opt_include_option(char *cmd, /*@null@*/ char *param, int extra);
 static int opt_preproc_option(char *cmd, /*@null@*/ char *param, int extra);
 static int opt_ewmsg_handler(char *cmd, /*@null@*/ char *param, int extra);
 static int opt_makedep_handler(char *cmd, /*@null@*/ char *param, int extra);
@@ -159,15 +160,15 @@ static opt_option options[] =
       N_("generate Makefile dependencies on stdout"), NULL },
     { 'e', "preproc-only", 0, preproc_only_handler, 0,
       N_("preprocess only (writes output to stdout by default)"), NULL },
-    { 'i', NULL, 1, opt_preproc_option, 0,
+    { 'i', NULL, 1, opt_include_option, 0,
       N_("add include path"), N_("path") },
-    { 'I', NULL, 1, opt_preproc_option, 0,
+    { 'I', NULL, 1, opt_include_option, 0,
       N_("add include path"), N_("path") },
-    { 'P', NULL, 1, opt_preproc_option, 1,
+    { 'P', NULL, 1, opt_preproc_option, 0,
       N_("pre-include file"), N_("filename") },
-    { 'D', NULL, 1, opt_preproc_option, 2,
+    { 'D', NULL, 1, opt_preproc_option, 1,
       N_("pre-define a macro, optionally to value"), N_("macro[=value]") },
-    { 'U', NULL, 1, opt_preproc_option, 3,
+    { 'U', NULL, 1, opt_preproc_option, 2,
       N_("undefine a macro"), N_("macro") },
     { 'X', NULL, 1, opt_ewmsg_handler, 0,
       N_("select error/warning message style (`gnu' or `vc')"), N_("style") },
@@ -1008,6 +1009,13 @@ preproc_only_handler(/*@unused@*/ char *cmd, /*@unused@*/ char *param,
     return 0;
 }
 
+static int
+opt_include_option(/*@unused@*/ char *cmd, char *param, /*@unused@*/ int extra)
+{
+    yasm_add_include_path(param);
+    return 0;
+}
+
 static int
 opt_preproc_option(/*@unused@*/ char *cmd, char *param, int extra)
 {
@@ -1066,14 +1074,13 @@ apply_preproc_saved_options()
 {
     constcharparam *cp, *cpnext;
 
-    void (*funcs[4])(yasm_preproc *, const char *);
-    funcs[0] = cur_preproc_module->add_include_path;
-    funcs[1] = cur_preproc_module->add_include_file;
-    funcs[2] = cur_preproc_module->predefine_macro;
-    funcs[3] = cur_preproc_module->undefine_macro;
+    void (*funcs[3])(yasm_preproc *, const char *);
+    funcs[0] = cur_preproc_module->add_include_file;
+    funcs[1] = cur_preproc_module->predefine_macro;
+    funcs[2] = cur_preproc_module->undefine_macro;
 
     STAILQ_FOREACH(cp, &preproc_options, link) {
-       if (0 <= cp->id && cp->id < 4 && funcs[cp->id])
+       if (0 <= cp->id && cp->id < 3 && funcs[cp->id])
            funcs[cp->id](cur_preproc, cp->param);
     }
 
index 94b61c6ab1049f5e83c41ff11645ea9d0a7b164a..10f73fdfbaea19eb6542e6858ee8008153ef25e6 100644 (file)
@@ -402,32 +402,68 @@ yasm__combpath_win(const char *from, const char *to)
     return out;
 }
 
+typedef struct incpath {
+    STAILQ_ENTRY(incpath) link;
+    /*@owned@*/ char *path;
+} incpath;
+
+STAILQ_HEAD(, incpath) incpaths = STAILQ_HEAD_INITIALIZER(incpaths);
+
 FILE *
-yasm__fopen_include(const char *iname, const char *from, const char **paths,
-                   const char *mode, char **oname)
+yasm_fopen_include(const char *iname, const char *from, const char *mode,
+                  char **oname)
 {
     FILE *f;
     char *combine;
-    const char *path;
+    incpath *np;
 
     /* Try directly relative to from first, then each of the include paths */
-    path = from;
-    while (path) {
-       combine = yasm__combpath(path, iname);
+    if (from) {
+       combine = yasm__combpath(from, iname);
+       f = fopen(combine, mode);
+       if (f) {
+           *oname = combine;
+           return f;
+       }
+       yasm_xfree(combine);
+    }
+
+    STAILQ_FOREACH(np, &incpaths, link) {
+       combine = yasm__combpath(np->path, iname);
        f = fopen(combine, mode);
        if (f) {
            *oname = combine;
            return f;
        }
        yasm_xfree(combine);
-       if (!paths)
-           break;
-       path = *paths++;
     }
 
     return NULL;
 }
 
+void
+yasm_delete_include_paths(void)
+{
+    incpath *n1, *n2;
+
+    n1 = STAILQ_FIRST(&incpaths);
+    while (n1) {
+       n2 = STAILQ_NEXT(n1, link);
+       yasm_xfree(n1->path);
+       yasm_xfree(n1);
+       n1 = n2;
+    }
+    STAILQ_INIT(&incpaths);
+}
+
+void
+yasm_add_include_path(const char *path)
+{
+    incpath *np = yasm_xmalloc(sizeof(incpath));
+    np->path = yasm__xstrdup(path);
+    STAILQ_INSERT_TAIL(&incpaths, np, link);
+}
+
 size_t
 yasm_fwrite_16_l(unsigned short val, FILE *f)
 {
index 594f5d0ce9ffa563cd67a8d11b365f483ab6a8a2..f37062d0ac60deae7882f2fba2f89bf4e868e09c 100644 (file)
@@ -170,28 +170,36 @@ char *yasm__combpath_win(const char *from, const char *to);
 
 /** Try to find and open an include file, searching through include paths.
  * First iname is looked for relative to the directory containing "from", then
- * it's looked for relative to each of the paths.
+ * it's looked for relative to each of the include paths.
  *
- * All pathnames may be either absolute or relative; from, oname, and paths,
- * if relative, are relative from the current working directory.
+ * All pathnames may be either absolute or relative; from, oname, and
+ * include paths, if relative, are relative from the current working directory.
  *
  * First match wins; the full pathname (newly allocated) to the opened file
  * is saved into oname, and the fopen'ed FILE * is returned.  If not found,
  * NULL is returned.
  *
- * \internal
  * \param iname            file to include
  * \param from     file doing the including
- * \param paths            NULL-terminated array of paths to search (relative to from)
- *                  may be NULL if no extra paths
  * \param mode     fopen mode string
  * \param oname            full pathname of included file (may be relative)
  * \return fopen'ed include file, or NULL if not found.
  */
-/*@null@*/ FILE *yasm__fopen_include(const char *iname, const char *from,
-                                    /*@null@*/ const char **paths,
-                                    const char *mode,
-                                    /*@out@*/ /*@only@*/ char **oname);
+/*@null@*/ FILE *yasm_fopen_include(const char *iname, const char *from,
+                                   const char *mode,
+                                   /*@out@*/ /*@only@*/ char **oname);
+
+/** Delete any stored include paths added by yasm_add_include_path().
+ */
+void yasm_delete_include_paths(void);
+
+/** Add an include path for use by yasm_fopen_include().
+ * If path is relative, it is treated by yasm_fopen_include() as relative to
+ * the current working directory.
+ *
+ * \param path     path to add
+ */
+void yasm_add_include_path(const char *path);
 
 /** Write an 8-bit value to a buffer, incrementing buffer pointer.
  * \note Only works properly if ptr is an (unsigned char *).
index 21ed7ec73a4b41f62966a9a4436efe5df7fb2700..96b1317337e1b019904b67e63b5ac7440e712349 100644 (file)
@@ -85,11 +85,6 @@ typedef struct yasm_preproc_module {
     size_t (*get_included_file) (yasm_preproc *preproc, /*@out@*/ char *buf,
                                  size_t max_size);
 
-    /** Module-level implementation of yasm_preproc_add_include_path().
-     * Call yasm_preproc_add_include_path() instead of calling this function.
-     */
-    void (*add_include_path) (yasm_preproc *preproc, const char *path);
-
     /** Module-level implementation of yasm_preproc_add_include_file().
      * Call yasm_preproc_add_include_file() instead of calling this function.
      */
@@ -150,12 +145,6 @@ size_t yasm_preproc_input(yasm_preproc *preproc, /*@out@*/ char *buf,
 size_t yasm_preproc_get_included_file(yasm_preproc *preproc,
                                      /*@out@*/ char *buf, size_t max_size);
 
-/** Add a directory to the include search path.
- * \param preproc      preprocessor
- * \param path         pathname
- */
-void yasm_preproc_add_include_path(yasm_preproc *preproc, const char *path);
-
 /** Pre-include a file.
  * \param preproc      preprocessor
  * \param filename     filename
@@ -196,8 +185,6 @@ void yasm_preproc_define_builtin(yasm_preproc *preproc,
     ((yasm_preproc_base *)preproc)->module->input(preproc, buf, max_size)
 #define yasm_preproc_get_included_file(preproc, buf, max_size) \
     ((yasm_preproc_base *)preproc)->module->get_included_file(preproc, buf, max_size)
-#define yasm_preproc_add_include_path(preproc, path) \
-    ((yasm_preproc_base *)preproc)->module->add_include_path(preproc, path)
 #define yasm_preproc_add_include_file(preproc, filename) \
     ((yasm_preproc_base *)preproc)->module->add_include_file(preproc, filename)
 #define yasm_preproc_predefine_macro(preproc, macronameval) \
index 7408a7e66b76ff12d64292a52a3b75bd5e8b4fc3..41387e00f1837ad24c9918ef256aa3068e476800 100644 (file)
@@ -339,8 +339,6 @@ static int LocalOffset = 4;
 
 static Context *cstk;
 static Include *istk;
-static size_t num_ipaths = 0;
-static char **ipaths = NULL;
 
 static FILE *first_fp = NULL;
 
@@ -1293,8 +1291,7 @@ inc_fopen(char *file, char **newname)
     FILE *fp;
     char *combine = NULL;
 
-    fp = yasm__fopen_include(file, nasm_src_get_fname(), (const char **)ipaths,
-                            "r", &combine);
+    fp = yasm_fopen_include(file, nasm_src_get_fname(), "r", &combine);
     if (!fp)
        error(ERR_FATAL, "unable to open include file `%s'", file);
     nasm_preproc_add_dep(combine);
@@ -4454,28 +4451,6 @@ pp_cleanup(int pass_)
        }
 }
 
-static void
-backslash(char *s)
-{
-    int pos = strlen(s);
-    if (s[pos - 1] != '\\' && s[pos - 1] != '/')
-    {
-       s[pos] = '/';
-       s[pos + 1] = '\0';
-    }
-}
-
-void
-pp_include_path(const char *path)
-{
-    num_ipaths++;
-    ipaths = yasm_xrealloc(ipaths, (num_ipaths+1)*sizeof(char *));
-    ipaths[num_ipaths-1] = yasm_xmalloc(strlen(path)+2);
-    strcpy(ipaths[num_ipaths-1], path);
-    backslash(ipaths[num_ipaths-1]);
-    ipaths[num_ipaths] = NULL;
-} 
-
 void
 pp_pre_include(const char *fname)
 {
index 921419c72582c0e9e499ec51eb86c349723dc957..a1990206f2014f4cddb0627e8cc7a9edf52a7422 100644 (file)
@@ -9,7 +9,6 @@
 #ifndef YASM_NASM_PREPROC_H
 #define YASM_NASM_PREPROC_H
 
-void pp_include_path (const char *);
 void pp_pre_include (const char *);
 void pp_pre_define (char *);
 void pp_pre_undefine (char *);
index 681354ccb36d81bdf3e3dec6c96f23b8c445aee2..5702a1d1b01a0cf8fe56e5d1dbe5dfddffb43235 100644 (file)
@@ -266,12 +266,6 @@ nasm_preproc_get_included_file(yasm_preproc *preproc, /*@out@*/ char *buf,
     }
 }
 
-static void
-nasm_preproc_add_include_path(yasm_preproc *preproc, const char *path)
-{
-    pp_include_path(path);
-}
-
 static void
 nasm_preproc_add_include_file(yasm_preproc *preproc, const char *filename)
 {
@@ -310,7 +304,6 @@ yasm_preproc_module yasm_nasm_LTX_preproc = {
     nasm_preproc_destroy,
     nasm_preproc_input,
     nasm_preproc_get_included_file,
-    nasm_preproc_add_include_path,
     nasm_preproc_add_include_file,
     nasm_preproc_predefine_macro,
     nasm_preproc_undefine_macro,