From 7a18cdaf3ca1486c98be1e89cf79c08cd6904ef4 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Fri, 19 Jan 2007 08:05:17 +0000 Subject: [PATCH] Support environment variables in %include along the lines of FASM: %include "%SOMEVAR%/subdir/file.inc" Multiple %VAR% are supported in a single %include. Undefined vars are not expanded. Suggested by: Martin Mocko svn path=/trunk/yasm/; revision=1737 --- modules/preprocs/nasm/nasm-pp.c | 54 +++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/modules/preprocs/nasm/nasm-pp.c b/modules/preprocs/nasm/nasm-pp.c index 41387e00..b643a9f9 100644 --- a/modules/preprocs/nasm/nasm-pp.c +++ b/modules/preprocs/nasm/nasm-pp.c @@ -1290,12 +1290,62 @@ inc_fopen(char *file, char **newname) { FILE *fp; char *combine = NULL; + char *pb, *p1, *p2, *file2 = NULL; - fp = yasm_fopen_include(file, nasm_src_get_fname(), "r", &combine); + /* Try to expand all %ENVVAR% in filename. Warn, and leave %string% + * intact, if ENVVAR is not set in the environment. + */ + pb = file; + for (;;) { + char *env; + p1 = pb; + while (*p1 != '\0' && *p1 != '%') + p1++; + if (*p1 == '\0') + break; + p2 = p1+1; + while (*p2 != '\0' && *p2 != '%') + p2++; + if (*p2 == '\0') + break; + /* Okay, we have a %...%, with p1 pointing to the first %, and p2 + * pointing to the second %. + */ + *p2 = '\0'; + env = getenv(p1+1); + if (!env) { + /* warn, restore %, and continue looking */ + error(ERR_WARNING, "environment variable `%s' does not exist", + p1+1); + *p2 = '%'; + pb = p2+1; + continue; + } + /* need to expand */ + if (!file2) { + file2 = nasm_malloc(strlen(file)+strlen(env)+1); + file2[0] = '\0'; + } else + file2 = nasm_realloc(file2, strlen(file2)+strlen(env)+1); + *p1 = '\0'; + strcat(file2, pb); + strcat(file2, env); + pb = p2+1; + } + /* add tail end; string is long enough that we don't need to realloc */ + if (file2) + strcat(file2, pb); + + fp = yasm_fopen_include(file2 ? file2 : file, nasm_src_get_fname(), "r", + &combine); if (!fp) - error(ERR_FATAL, "unable to open include file `%s'", file); + error(ERR_FATAL, "unable to open include file `%s'", + file2 ? file2 : file); nasm_preproc_add_dep(combine); + if (file2) + nasm_free(file2); + *newname = combine; return fp; } -- 2.40.0