From ec8fab9d7a5851777bb61167624502274dc6d4b3 Mon Sep 17 00:00:00 2001 From: nhmall Date: Thu, 22 May 2025 14:04:10 -0400 Subject: [PATCH] more config-file code relocations --- src/cfgfiles.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/files.c | 43 --------------------------- src/pline.c | 37 ----------------------- 3 files changed, 80 insertions(+), 80 deletions(-) diff --git a/src/cfgfiles.c b/src/cfgfiles.c index bf48bf9f6..3e1197a4e 100644 --- a/src/cfgfiles.c +++ b/src/cfgfiles.c @@ -9,10 +9,20 @@ #include "dlb.h" #include +#if (!defined(MAC) && !defined(O_WRONLY) && !defined(AZTEC_C)) \ + || defined(USE_FCNTL) +#include +#endif + +#define BIGBUFSZ (5 * BUFSZ) /* big enough to format a 4*BUFSZ string (from + * config file parsing) with modest decoration; + * result will then be truncated to BUFSZ-1 */ + #ifdef USER_SOUNDS extern char *sounddir; /* defined in sounds.c */ #endif +staticfn void vconfig_error_add(const char *, va_list); staticfn FILE *fopen_config_file(const char *, int); staticfn int get_uchars(char *, uchar *, boolean, int, const char *); #ifdef NOCWD_ASSUMPTIONS @@ -1819,6 +1829,36 @@ parse_conf_file(FILE *fp, boolean (*proc)(char *arg)) return parser.rv; } +DISABLE_WARNING_FORMAT_NONLITERAL + +void +config_error_add(const char *str, ...) +{ + va_list the_args; + + va_start(the_args, str); + vconfig_error_add(str, the_args); + va_end(the_args); +} + +staticfn void +vconfig_error_add(const char *str, va_list the_args) +{ /* start of vconf...() or of nested block in USE_OLDARG's conf...() */ + int vlen = 0; + char buf[BIGBUFSZ]; /* will be chopped down to BUFSZ-1 if longer */ + + vlen = vsnprintf(buf, sizeof buf, str, the_args); +#if (NH_DEVEL_STATUS != NH_STATUS_RELEASED) && defined(DEBUG) + if (vlen >= (int) sizeof buf) + panic("%s: truncation of buffer at %zu of %d bytes", + "config_error_add", sizeof buf, vlen); +#else + nhUse(vlen); +#endif + buf[BUFSZ - 1] = '\0'; + config_erradd(buf); +} + #ifdef SYSCF staticfn void parseformat(int *arr, char *str) @@ -1852,6 +1892,46 @@ parseformat(int *arr, char *str) } } } +#ifdef SYSCF_FILE +void +assure_syscf_file(void) +{ + int fd; + +#ifdef WIN32 + /* We are checking that the sysconf exists ... lock the path */ + fqn_prefix_locked[SYSCONFPREFIX] = TRUE; +#endif + /* + * All we really care about is the end result - can we read the file? + * So just check that directly. + * + * Not tested on most of the old platforms (which don't attempt + * to implement SYSCF). + * Some ports don't like open()'s optional third argument; + * VMS overrides open() usage with a macro which requires it. + */ +#ifndef VMS +#if defined(NOCWD_ASSUMPTIONS) && defined(WIN32) + fd = open(fqname(SYSCF_FILE, SYSCONFPREFIX, 0), O_RDONLY); +#else + fd = open(SYSCF_FILE, O_RDONLY); +#endif +#else + fd = open(SYSCF_FILE, O_RDONLY, 0); +#endif + if (fd >= 0) { + /* readable */ + close(fd); + return; + } + if (gd.deferred_showpaths) + do_deferred_showpaths(1); /* does not return */ + raw_printf("Unable to open SYSCF_FILE.\n"); + exit(EXIT_FAILURE); +} + +#endif /* SYSCF_FILE */ #endif /* SYSCF */ /* ---------- END CONFIG FILE HANDLING ----------- */ diff --git a/src/files.c b/src/files.c index cd8ea9815..90ab61076 100644 --- a/src/files.c +++ b/src/files.c @@ -2603,49 +2603,6 @@ recover_savefile(void) /* ---------- OTHER ----------- */ -#ifdef SYSCF -#ifdef SYSCF_FILE -void -assure_syscf_file(void) -{ - int fd; - -#ifdef WIN32 - /* We are checking that the sysconf exists ... lock the path */ - fqn_prefix_locked[SYSCONFPREFIX] = TRUE; -#endif - /* - * All we really care about is the end result - can we read the file? - * So just check that directly. - * - * Not tested on most of the old platforms (which don't attempt - * to implement SYSCF). - * Some ports don't like open()'s optional third argument; - * VMS overrides open() usage with a macro which requires it. - */ -#ifndef VMS -# if defined(NOCWD_ASSUMPTIONS) && defined(WIN32) - fd = open(fqname(SYSCF_FILE, SYSCONFPREFIX, 0), O_RDONLY); -# else - fd = open(SYSCF_FILE, O_RDONLY); -# endif -#else - fd = open(SYSCF_FILE, O_RDONLY, 0); -#endif - if (fd >= 0) { - /* readable */ - close(fd); - return; - } - if (gd.deferred_showpaths) - do_deferred_showpaths(1); /* does not return */ - raw_printf("Unable to open SYSCF_FILE.\n"); - exit(EXIT_FAILURE); -} - -#endif /* SYSCF_FILE */ -#endif /* SYSCF */ - ATTRNORETURN void do_deferred_showpaths(int code) { diff --git a/src/pline.c b/src/pline.c index 7a1bc1511..48f8fc89f 100644 --- a/src/pline.c +++ b/src/pline.c @@ -682,43 +682,6 @@ execplinehandler(const char *line) #endif } -/* - * varargs handling for files.c - */ -staticfn void vconfig_error_add(const char *, va_list); - -DISABLE_WARNING_FORMAT_NONLITERAL - -void -config_error_add(const char *str, ...) -{ - va_list the_args; - - va_start(the_args, str); - vconfig_error_add(str, the_args); - va_end(the_args); -} - -staticfn void -vconfig_error_add(const char *str, va_list the_args) -{ /* start of vconf...() or of nested block in USE_OLDARG's conf...() */ - int vlen = 0; - char buf[BIGBUFSZ]; /* will be chopped down to BUFSZ-1 if longer */ - - vlen = vsnprintf(buf, sizeof buf, str, the_args); -#if (NH_DEVEL_STATUS != NH_STATUS_RELEASED) && defined(DEBUG) - if (vlen >= (int) sizeof buf) - panic("%s: truncation of buffer at %zu of %d bytes", - "config_error_add", sizeof buf, vlen); -#else - nhUse(vlen); -#endif - buf[BUFSZ - 1] = '\0'; - config_erradd(buf); -} - -RESTORE_WARNING_FORMAT_NONLITERAL - /* nhassert_failed is called when an nhassert's condition is false */ void nhassert_failed(const char *expression, const char *filepath, int line)