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);
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") },
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)
{
{
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);
}
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)
{
/** 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 *).
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.
*/
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
((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) \
static Context *cstk;
static Include *istk;
-static size_t num_ipaths = 0;
-static char **ipaths = NULL;
static FILE *first_fp = NULL;
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);
}
}
-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)
{
#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 *);
}
}
-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)
{
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,