From 6dfbff9d4bcd3aa0dfa43bae90ab3dbc39c1d663 Mon Sep 17 00:00:00 2001 From: nhmall Date: Mon, 11 Nov 2019 12:49:54 -0500 Subject: [PATCH] --showpaths wasn't returning the paths if there was an error in a config file Also, Windows only: Introduces some environment variable substitution for paths specified in a config file --- include/flag.h | 2 ++ src/files.c | 17 +++++++++++- src/options.c | 2 +- sys/unix/unixmain.c | 2 ++ sys/winnt/windmain.c | 61 ++++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 80 insertions(+), 4 deletions(-) diff --git a/include/flag.h b/include/flag.h index 8fe94339b..5370faab7 100644 --- a/include/flag.h +++ b/include/flag.h @@ -268,6 +268,8 @@ struct instance_flags { boolean mon_polycontrol; /* debug: control monster polymorphs */ boolean in_dumplog; /* doing the dumplog right now? */ boolean in_parse; /* is a command being parsed? */ + /* suppress terminate during options parsing, for --showpaths */ + boolean initoptions_noterminate; /* stuff that is related to options and/or user or platform preferences */ diff --git a/src/files.c b/src/files.c index f8e77cfdf..38a692113 100644 --- a/src/files.c +++ b/src/files.c @@ -151,6 +151,10 @@ static int lockptr; #ifndef WIN_CE #define DeleteFile unlink #endif +#ifdef WIN32 +/*from windmain.c */ +extern char *FDECL(translate_path_variables, (const char *, char *)); +#endif #endif #ifdef MAC @@ -350,6 +354,10 @@ const char *basenam; int whichprefix UNUSED_if_not_PREFIXES_IN_USE; int buffnum UNUSED_if_not_PREFIXES_IN_USE; { +#ifdef WIN32 + char tmpbuf[BUFSZ]; + +#endif #ifndef PREFIXES_IN_USE return basenam; #else @@ -367,9 +375,16 @@ int buffnum UNUSED_if_not_PREFIXES_IN_USE; basenam); return basenam; /* XXX */ } +#ifdef WIN32 + if (strchr(fqn_prefix[whichprefix], '%') || + strchr(fqn_prefix[whichprefix], '~')) + Strcpy(fqn_filename_buffer[buffnum], + translate_path_variables(fqn_prefix[whichprefix], tmpbuf)); + else +#endif Strcpy(fqn_filename_buffer[buffnum], fqn_prefix[whichprefix]); return strcat(fqn_filename_buffer[buffnum], basenam); -#endif +#endif /* !PREFIXES_IN_USE */ } int diff --git a/src/options.c b/src/options.c index bfeb08604..203532cde 100644 --- a/src/options.c +++ b/src/options.c @@ -673,7 +673,7 @@ initoptions() /* ... and _must_ parse correctly. */ if (!read_config_file(SYSCF_FILE, SET_IN_SYS)) { - if (config_error_done()) + if (config_error_done() && !iflags.initoptions_noterminate) nh_terminate(EXIT_FAILURE); } config_error_done(); diff --git a/sys/unix/unixmain.c b/sys/unix/unixmain.c index 50c047ff8..c2690ce4a 100644 --- a/sys/unix/unixmain.c +++ b/sys/unix/unixmain.c @@ -118,7 +118,9 @@ char *argv[]; #ifdef CHDIR chdirx((char *) 0, 0); #endif + iflags.initoptions_noterminate = TRUE; initoptions(); + iflags.initoptions_noterminate = FALSE; reveal_paths(); exit(EXIT_SUCCESS); } diff --git a/sys/winnt/windmain.c b/sys/winnt/windmain.c index 22809751c..e1faeab50 100644 --- a/sys/winnt/windmain.c +++ b/sys/winnt/windmain.c @@ -24,6 +24,7 @@ static void FDECL(process_options, (int argc, char **argv)); static void NDECL(nhusage); static char *NDECL(get_executable_path); +char *FDECL(translate_path_variables, (char *, char *)); char *NDECL(exename); boolean NDECL(fakeconsole); void NDECL(freefakeconsole); @@ -62,8 +63,7 @@ char default_window_sys[] = "mswin"; static struct stat hbuf; #endif #include -#if defined(WIN32) || defined(MSDOS) -#endif + extern char orgdir[]; @@ -548,7 +548,9 @@ char *argv[]; nethack_exit(EXIT_SUCCESS); if (argcheck(argc, argv, ARG_SHOWPATHS) == 2) { + iflags.initoptions_noterminate = TRUE; initoptions(); + iflags.initoptions_noterminate = FALSE; reveal_paths(); nethack_exit(EXIT_SUCCESS); } @@ -853,6 +855,61 @@ get_executable_path() return path_buffer; } +char * +translate_path_variables(str, buf) +const char *str; +char *buf; +{ + const char *src; + char evar[BUFSZ], *dest, *envp, *eptr = (char *) 0; + boolean in_evar; + size_t ccount, ecount, destcount, slen = str ? strlen(str) : 0; + + if (!slen || !buf) { + if (buf) + *buf = '\0'; + return buf; + } + + dest = buf; + src = str; + in_evar = FALSE; + destcount = ecount = 0; + for (ccount = 0; ccount < slen && destcount < (BUFSZ - 1) && + ecount < (BUFSZ - 1); ++ccount, ++src) { + if (*src == '%') { + if (in_evar) { + *eptr = '\0'; + envp = nh_getenv(evar); + if (envp) { + size_t elen = strlen(envp); + + if ((elen + destcount) < (size_t) (BUFSZ - 1)) { + Strcpy(dest, envp); + dest += elen; + destcount += elen; + } + } + } else { + eptr = evar; + ecount = 0; + } + in_evar = !in_evar; + continue; + } + if (in_evar) { + *eptr++ = *src; + ecount++; + } else { + *dest++ = *src; + destcount++; + } + } + *dest = '\0'; + return buf; +} + + /*ARGSUSED*/ void windows_raw_print(str) -- 2.49.0