From: Peter Johnson Date: Sat, 28 Oct 2006 21:31:38 +0000 (-0000) Subject: Refactor include file handling into libyasm from preprocessors. X-Git-Tag: v0.6.0~112 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c81b221152c9c31e30ca226d96f2026ffd6ef2f4;p=yasm Refactor include file handling into libyasm from preprocessors. svn path=/trunk/yasm/; revision=1669 --- diff --git a/frontends/yasm/yasm.c b/frontends/yasm/yasm.c index 3281ad89..85b38f95 100644 --- a/frontends/yasm/yasm.c +++ b/frontends/yasm/yasm.c @@ -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); } diff --git a/libyasm/file.c b/libyasm/file.c index 94b61c6a..10f73fdf 100644 --- a/libyasm/file.c +++ b/libyasm/file.c @@ -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) { diff --git a/libyasm/file.h b/libyasm/file.h index 594f5d0c..f37062d0 100644 --- a/libyasm/file.h +++ b/libyasm/file.h @@ -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 *). diff --git a/libyasm/preproc.h b/libyasm/preproc.h index 21ed7ec7..96b13173 100644 --- a/libyasm/preproc.h +++ b/libyasm/preproc.h @@ -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) \ diff --git a/modules/preprocs/nasm/nasm-pp.c b/modules/preprocs/nasm/nasm-pp.c index 7408a7e6..41387e00 100644 --- a/modules/preprocs/nasm/nasm-pp.c +++ b/modules/preprocs/nasm/nasm-pp.c @@ -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) { diff --git a/modules/preprocs/nasm/nasm-pp.h b/modules/preprocs/nasm/nasm-pp.h index 921419c7..a1990206 100644 --- a/modules/preprocs/nasm/nasm-pp.h +++ b/modules/preprocs/nasm/nasm-pp.h @@ -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 *); diff --git a/modules/preprocs/nasm/nasm-preproc.c b/modules/preprocs/nasm/nasm-preproc.c index 681354cc..5702a1d1 100644 --- a/modules/preprocs/nasm/nasm-preproc.c +++ b/modules/preprocs/nasm/nasm-preproc.c @@ -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,