From 7bfb3852e8102196aa5430889c9e49b9eb3700d8 Mon Sep 17 00:00:00 2001 From: Michael Urman Date: Sun, 19 Oct 2003 18:11:29 +0000 Subject: [PATCH] Add support for nasm-inherited command line options -D and -U. Unify option storage for cleanliness and to maintain command line PIDU order. svn path=/trunk/yasm/; revision=1070 --- frontends/yasm/yasm.c | 69 ++++++++++------------------ libyasm/preproc.h | 8 +++- modules/preprocs/nasm/nasm-preproc.c | 20 +++++++- modules/preprocs/raw/raw-preproc.c | 4 +- 4 files changed, 53 insertions(+), 48 deletions(-) diff --git a/frontends/yasm/yasm.c b/frontends/yasm/yasm.c index 16091479..22d791c5 100644 --- a/frontends/yasm/yasm.c +++ b/frontends/yasm/yasm.c @@ -91,10 +91,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_preproc_include_path(char *cmd, /*@null@*/ char *param, - int extra); -static int opt_preproc_include_file(char *cmd, /*@null@*/ char *param, - int extra); +static int opt_preproc_option(char *cmd, /*@null@*/ char *param, int extra); static /*@only@*/ char *replace_extension(const char *orig, /*@null@*/ const char *ext, const char *def); @@ -145,10 +142,14 @@ static opt_option options[] = N_("enables/disables warning"), NULL }, { 'e', "preproc-only", 0, preproc_only_handler, 0, N_("preprocess only (writes output to stdout by default)"), NULL }, - { 'I', NULL, 1, opt_preproc_include_path, 0, + { 'I', NULL, 1, opt_preproc_option, 0, N_("add include path"), N_("path") }, - { 'P', NULL, 1, opt_preproc_include_file, 0, + { 'P', NULL, 1, opt_preproc_option, 1, N_("pre-include file"), N_("filename") }, + { 'D', NULL, 1, opt_preproc_option, 2, + N_("pre-define a macro, optionally to value"), N_("macro[=value]") }, + { 'U', NULL, 1, opt_preproc_option, 3, + N_("undefine a macro"), N_("macro") }, }; /* version message */ @@ -203,10 +204,10 @@ typedef STAILQ_HEAD(constcharparam_head, constcharparam) constcharparam_head; typedef struct constcharparam { STAILQ_ENTRY(constcharparam) link; const char *param; + int id; } constcharparam; -static constcharparam_head includepaths; -static constcharparam_head includefiles; +static constcharparam_head preproc_options; /* main function */ /*@-globstate -unrecog@*/ @@ -268,8 +269,7 @@ main(int argc, char *argv[]) #endif /* Initialize parameter storage */ - STAILQ_INIT(&includepaths); - STAILQ_INIT(&includefiles); + STAILQ_INIT(&preproc_options); if (parse_cmdline(argc, argv, options, NELEMS(options), print_error)) return EXIT_FAILURE; @@ -882,60 +882,39 @@ preproc_only_handler(/*@unused@*/ char *cmd, /*@unused@*/ char *param, } static int -opt_preproc_include_path(/*@unused@*/ char *cmd, char *param, - /*@unused@*/ int extra) +opt_preproc_option(/*@unused@*/ char *cmd, char *param, int extra) { constcharparam *cp; cp = yasm_xmalloc(sizeof(constcharparam)); cp->param = param; - STAILQ_INSERT_TAIL(&includepaths, cp, link); + cp->id = extra; + STAILQ_INSERT_TAIL(&preproc_options, cp, link); return 0; } static void apply_preproc_saved_options() { - constcharparam *cp; - constcharparam *cpnext; + constcharparam *cp, *cpnext; - if (cur_preproc->add_include_path != NULL) { - STAILQ_FOREACH(cp, &includepaths, link) { - cur_preproc->add_include_path(cp->param); - } - } - - cp = STAILQ_FIRST(&includepaths); - while (cp != NULL) { - cpnext = STAILQ_NEXT(cp, link); - yasm_xfree(cp); - cp = cpnext; - } - STAILQ_INIT(&includepaths); + void (*funcs[4])(const char *); + funcs[0] = cur_preproc->add_include_path; + funcs[1] = cur_preproc->add_include_file; + funcs[2] = cur_preproc->predefine_macro; + funcs[3] = cur_preproc->undefine_macro; - if (cur_preproc->add_include_file != NULL) { - STAILQ_FOREACH(cp, &includefiles, link) { - cur_preproc->add_include_file(cp->param); - } + STAILQ_FOREACH(cp, &preproc_options, link) { + if (0 <= cp->id && cp->id < 4 && funcs[cp->id]) + funcs[cp->id](cp->param); } - cp = STAILQ_FIRST(&includepaths); + cp = STAILQ_FIRST(&preproc_options); while (cp != NULL) { cpnext = STAILQ_NEXT(cp, link); yasm_xfree(cp); cp = cpnext; } - STAILQ_INIT(&includepaths); -} - -static int -opt_preproc_include_file(/*@unused@*/ char *cmd, char *param, - /*@unused@*/ int extra) -{ - constcharparam *cp; - cp = yasm_xmalloc(sizeof(constcharparam)); - cp->param = param; - STAILQ_INSERT_TAIL(&includefiles, cp, link); - return 0; + STAILQ_INIT(&preproc_options); } /* Replace extension on a filename (or append one if none is present). diff --git a/libyasm/preproc.h b/libyasm/preproc.h index 88f84dff..fd43d364 100644 --- a/libyasm/preproc.h +++ b/libyasm/preproc.h @@ -36,7 +36,7 @@ * definitions match the module loader's function definitions. The version * number must never be decreased. */ -#define YASM_PREPROC_VERSION 1 +#define YASM_PREPROC_VERSION 2 /* Interface to the preprocesor module(s) */ struct yasm_preproc { @@ -74,6 +74,12 @@ struct yasm_preproc { /* Pre-include a file */ void (*add_include_file) (const char *filename); + + /* Pre-define a macro */ + void (*predefine_macro) (const char *macronameval); + + /* Un-define a macro */ + void (*undefine_macro) (const char *macroname); }; #endif diff --git a/modules/preprocs/nasm/nasm-preproc.c b/modules/preprocs/nasm/nasm-preproc.c index 4946da91..88a42750 100644 --- a/modules/preprocs/nasm/nasm-preproc.c +++ b/modules/preprocs/nasm/nasm-preproc.c @@ -189,6 +189,22 @@ nasm_preproc_add_include_file (const char *filename) pp_pre_include(filename); } +static void +nasm_preproc_predefine_macro (const char *macronameval) +{ + char *mnv = yasm__xstrdup(macronameval); + pp_pre_define(mnv); + yasm_xfree(mnv); +} + +static void +nasm_preproc_undefine_macro (const char *macroname) +{ + char *mn = yasm__xstrdup(macroname); + pp_pre_undefine(mn); + yasm_xfree(mn); +} + /* Define preproc structure -- see preproc.h for details */ yasm_preproc yasm_nasm_LTX_preproc = { @@ -199,5 +215,7 @@ yasm_preproc yasm_nasm_LTX_preproc = { nasm_preproc_cleanup, nasm_preproc_input, nasm_preproc_add_include_path, - nasm_preproc_add_include_file + nasm_preproc_add_include_file, + nasm_preproc_predefine_macro, + nasm_preproc_undefine_macro }; diff --git a/modules/preprocs/raw/raw-preproc.c b/modules/preprocs/raw/raw-preproc.c index d83b74e6..a455cf7c 100644 --- a/modules/preprocs/raw/raw-preproc.c +++ b/modules/preprocs/raw/raw-preproc.c @@ -82,5 +82,7 @@ yasm_preproc yasm_raw_LTX_preproc = { raw_preproc_cleanup, raw_preproc_input, /* no include paths */ NULL, - /* no pre-include files */ NULL + /* no pre-include files */ NULL, + /* no predefining macros */ NULL, + /* no undefining macros */ NULL }; -- 2.40.0