From: Michael Urman Date: Tue, 1 Apr 2003 04:06:47 +0000 (-0000) Subject: Implemented -I and -P command line options frontend, nasm and raw preprocs. X-Git-Tag: v0.2.2~3^2~31 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9399c654590809a83934374f321c8cff7ce4ac00;p=yasm Implemented -I and -P command line options frontend, nasm and raw preprocs. svn path=/trunk/yasm/; revision=906 --- diff --git a/frontends/yasm/yasm.c b/frontends/yasm/yasm.c index b68d1086..164d74a5 100644 --- a/frontends/yasm/yasm.c +++ b/frontends/yasm/yasm.c @@ -70,6 +70,10 @@ static int opt_dbgfmt_handler(char *cmd, /*@null@*/ char *param, int extra); static int opt_objfile_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 /*@only@*/ char *replace_extension(const char *orig, /*@null@*/ const char *ext, const char *def); @@ -85,6 +89,8 @@ static void print_yasm_error(const char *filename, unsigned long line, static void print_yasm_warning(const char *filename, unsigned long line, const char *msg); +static void apply_preproc_saved_options(void); + /* values for special_options */ #define SPECIAL_SHOW_HELP 0x01 #define SPECIAL_SHOW_VERSION 0x02 @@ -112,6 +118,10 @@ 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, + N_("add include path"), N_("path") }, + { 'P', NULL, 1, opt_preproc_include_file, 0, + N_("pre-include file"), N_("filename") }, }; /* version message */ @@ -141,6 +151,17 @@ static opt_option options[] = "\n" "Report bugs to bug-yasm@tortall.net\n"); +/* parsed command line storage until appropriate modules have been loaded */ +typedef STAILQ_HEAD(constcharparam_head, constcharparam) constcharparam_head; + +typedef struct constcharparam { + STAILQ_ENTRY(constcharparam) link; + const char *param; +} constcharparam; + +static constcharparam_head includepaths; +static constcharparam_head includefiles; + /* main function */ /*@-globstate -unrecog@*/ int @@ -195,6 +216,10 @@ main(int argc, char *argv[]) } #endif + /* Initialize parameter storage */ + STAILQ_INIT(&includepaths); + STAILQ_INIT(&includefiles); + if (parse_cmdline(argc, argv, options, NELEMS(options), print_error)) return EXIT_FAILURE; @@ -270,6 +295,8 @@ main(int argc, char *argv[]) return EXIT_FAILURE; } + apply_preproc_saved_options(); + /* Pre-process until done */ cur_preproc->initialize(in, in_filename, &yasm_std_linemgr); while ((got = cur_preproc->input(preproc_buf, PREPROC_BUF_SIZE)) != 0) @@ -400,6 +427,7 @@ main(int argc, char *argv[]) return EXIT_FAILURE; } } + apply_preproc_saved_options(); /* Get initial x86 BITS setting from object format */ if (strcmp(cur_arch->keyword, "x86") == 0) { @@ -664,6 +692,63 @@ preproc_only_handler(/*@unused@*/ char *cmd, /*@unused@*/ char *param, return 0; } +static int +opt_preproc_include_path(/*@unused@*/ char *cmd, char *param, + /*@unused@*/ int extra) +{ + constcharparam *cp; + cp = yasm_xmalloc(sizeof(constcharparam)); + cp->param = param; + STAILQ_INSERT_TAIL(&includepaths, cp, link); + return 0; +} + +static void +apply_preproc_saved_options() +{ + constcharparam *cp; + constcharparam *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); + + if (cur_preproc->add_include_file != NULL) { + STAILQ_FOREACH(cp, &includefiles, link) { + cur_preproc->add_include_file(cp->param); + } + } + + cp = STAILQ_FIRST(&includepaths); + 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; +} + /* Replace extension on a filename (or append one if none is present). * If output filename would be identical to input (same extension out as in), * returns (copy of) def. diff --git a/libyasm/preproc.h b/libyasm/preproc.h index 67d970c9..8226f9aa 100644 --- a/libyasm/preproc.h +++ b/libyasm/preproc.h @@ -51,6 +51,12 @@ struct yasm_preproc { /* Gets more preprocessed source code (up to max_size bytes) into buf. * Note that more than a single line may be returned in buf. */ size_t (*input) (/*@out@*/ char *buf, size_t max_size); + + /* Add a directory to the %include search path */ + void (*add_include_path) (const char *path); + + /* Pre-include a file */ + void (*add_include_file) (const char *filename); }; #endif diff --git a/modules/preprocs/nasm/nasm-preproc.c b/modules/preprocs/nasm/nasm-preproc.c index 81f7fe88..eda59b21 100644 --- a/modules/preprocs/nasm/nasm-preproc.c +++ b/modules/preprocs/nasm/nasm-preproc.c @@ -174,6 +174,22 @@ nasm_preproc_input(char *buf, size_t max_size) return tot; } +static void +nasm_preproc_add_include_path (const char *path) +{ + char *p = yasm__xstrdup(path); + pp_include_path(p); + yasm_xfree(p); +} + +static void +nasm_preproc_add_include_file (const char *filename) +{ + char *f = yasm__xstrdup(filename); + pp_pre_include(f); + yasm_xfree(f); +} + /* Define preproc structure -- see preproc.h for details */ yasm_preproc yasm_nasm_LTX_preproc = { @@ -181,5 +197,7 @@ yasm_preproc yasm_nasm_LTX_preproc = { "nasm", nasm_preproc_initialize, nasm_preproc_cleanup, - nasm_preproc_input + nasm_preproc_input, + nasm_preproc_add_include_path, + nasm_preproc_add_include_file }; diff --git a/modules/preprocs/raw/raw-preproc.c b/modules/preprocs/raw/raw-preproc.c index ecf36319..e971b5f2 100644 --- a/modules/preprocs/raw/raw-preproc.c +++ b/modules/preprocs/raw/raw-preproc.c @@ -76,5 +76,7 @@ yasm_preproc yasm_raw_LTX_preproc = { "raw", raw_preproc_initialize, raw_preproc_cleanup, - raw_preproc_input + raw_preproc_input, + /* no include paths */ NULL, + /* no pre-include files */ NULL };