From 66007e67830e17cae954c63687a28a463e94fa37 Mon Sep 17 00:00:00 2001 From: nhmall Date: Sun, 1 Jun 2025 15:32:55 -0400 Subject: [PATCH] remove duplicated code from sfctool.c, sftags.c Link with hacklib to provide them instead --- sys/unix/Makefile.utl | 71 +++--- sys/windows/Makefile.nmake | 27 ++- sys/windows/vs/sfctool/sfctool.vcxproj | 1 + sys/windows/vs/sftags/sftags.vcxproj | 8 + util/sfctool.c | 156 ++------------ util/sftags.c | 285 ++++++++++++++----------- 6 files changed, 233 insertions(+), 315 deletions(-) diff --git a/sys/unix/Makefile.utl b/sys/unix/Makefile.utl index f402bf0c3..bcd2c4da4 100644 --- a/sys/unix/Makefile.utl +++ b/sys/unix/Makefile.utl @@ -298,23 +298,24 @@ recover-version.o: ../src/version.c $(HACK_H) # dependencies for optional sfctool # # object files for sfctool utility -SFCTOOLOBJS = $(TARGETPFX)sfctool.o $(TARGETPFX)sf-alloc.o \ - $(TARGETPFX)sf-monst.o $(TARGETPFX)sf-objects.o \ - $(TARGETPFX)sfbase.o $(TARGETPFX)sfstruct.o \ - $(TARGETPFX)sfexpasc.o \ - $(TARGETPFX)sfdata.o $(TARGETPFX)sf-nhlua.o \ - $(TARGETPFX)panic.o $(TARGETPFX)sf-date.o \ - $(TARGETPFX)sf-decl.o $(TARGETPFX)sf-artifact.o \ +SFCTOOLOBJS = $(TARGETPFX)sfctool.o \ + $(TARGETPFX)sf-alloc.o $(TARGETPFX)sfdata.o \ + $(TARGETPFX)sfexpasc.o $(TARGETPFX)sf-cfgfiles.o \ + $(TARGETPFX)sf-date.o $(TARGETPFX)sf-artifact.o \ + $(TARGETPFX)sf-calendar.o $(TARGETPFX)sf-decl.o \ $(TARGETPFX)sf-dungeon.o $(TARGETPFX)sf-end.o \ - $(TARGETPFX)sf-engrave.o $(TARGETPFX)sf-cfgfiles.o \ - $(TARGETPFX)sf-files.o $(TARGETPFX)sf-light.o \ - $(TARGETPFX)sf-mdlib.o $(TARGETPFX)sf-mkmaze.o \ - $(TARGETPFX)sf-mkroom.o $(TARGETPFX)sf-o_init.o \ - $(TARGETPFX)sf-region.o $(TARGETPFX)sf-restore.o \ - $(TARGETPFX)sf-rumors.o $(TARGETPFX)sf-sys.o \ + $(TARGETPFX)sf-engrave.o $(TARGETPFX)sf-files.o \ + $(TARGETPFX)sf-light.o $(TARGETPFX)sf-mdlib.o \ + $(TARGETPFX)sf-mkmaze.o $(TARGETPFX)sf-mkroom.o \ + $(TARGETPFX)sf-monst.o $(TARGETPFX)sf-nhlua.o \ + $(TARGETPFX)sf-objects.o $(TARGETPFX)sf-o_init.o \ + $(TARGETPFX)panic.o $(TARGETPFX)sf-region.o \ + $(TARGETPFX)sf-restore.o $(TARGETPFX)sf-rumors.o \ + $(TARGETPFX)sfbase.o $(TARGETPFX)sf-struct.o \ + $(TARGETPFX)strutil.o $(TARGETPFX)sf-sys.o \ $(TARGETPFX)sf-timeout.o $(TARGETPFX)sf-track.o \ - $(TARGETPFX)sf-version.o $(TARGETPFX)sf-worm.o \ - $(TARGETPFX)strutil.o + $(TARGETPFX)sf-version.o $(TARGETPFX)sf-worm.o + SFCTOOLBIN = sfctool SFFLAGS=-DSFCTOOL -DNOPANICTRACE -DNOCRASHREPORT -DNO_CHRONICLE @@ -324,26 +325,22 @@ $(SFCTOOLBIN): $(SFCTOOLOBJS) $(HACKLIB) $(TARGET_CLINK) $(TARGET_LFLAGS) -o $@ $(SFCTOOLOBJS) $(HACKLIB) $(TARGET_LIBS) $(TARGETPFX)sfctool.o: sfctool.c $(HACK_H) ../include/sfprocs.h $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c sfctool.c -$(TARGETPFX)sfdata.o: sfdata.c $(HACK_H) ../include/sfprocs.h ../include/sfproto.h - $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c sfdata.c -$(TARGETPFX)sfexpasc.o: sfexpasc.c $(HACK_H) ../include/sfprocs.h ../include/sfproto.h - $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c sfexpasc.c $(TARGETPFX)sf-alloc.o: ../src/alloc.c $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/alloc.c +$(TARGETPFX)sf-calendar.o: ../src/calendar.c + $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/calendar.c +$(TARGETPFX)sf-cfgfiles.o: ../src/cfgfiles.c + $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/cfgfiles.c $(TARGETPFX)sf-date.o: ../src/date.c $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/date.c $(TARGETPFX)sf-decl.o: ../src/decl.c $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/decl.c -$(TARGETPFX)sf-panic.o: panic.c $(CONFIG_H) - $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c panic.c $(TARGETPFX)sf-monst.o: ../src/monst.c $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/monst.c $(TARGETPFX)sf-objects.o: ../src/objects.c $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/objects.c -$(TARGETPFX)sf-sfbase.o: ../src/sfbase.c - $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/sfbase.c -$(TARGETPFX)sfstruct.o: ../src/sfstruct.c - $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/sfstruct.c +$(TARGETPFX)sf-panic.o: panic.c $(CONFIG_H) + $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c panic.c $(TARGETPFX)sf-artifact.o: ../src/artifact.c $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/artifact.c $(TARGETPFX)sf-dungeon.o: ../src/dungeon.c @@ -352,8 +349,6 @@ $(TARGETPFX)sf-end.o: ../src/end.c $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/end.c $(TARGETPFX)sf-engrave.o: ../src/engrave.c $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/engrave.c -$(TARGETPFX)sf-cfgfiles.o: ../src/cfgfiles.c - $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/cfgfiles.c $(TARGETPFX)sf-files.o: ../src/files.c $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/files.c $(TARGETPFX)sf-light.o: ../src/light.c @@ -364,6 +359,8 @@ $(TARGETPFX)sf-mkmaze.o: ../src/mkmaze.c $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/mkmaze.c $(TARGETPFX)sf-mkroom.o: ../src/mkroom.c $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/mkroom.c +$(TARGETPFX)sf-nhlua.o: ../src/nhlua.c + $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/nhlua.c $(TARGETPFX)sf-o_init.o: ../src/o_init.c $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/o_init.c $(TARGETPFX)sf-region.o: ../src/region.c @@ -372,6 +369,16 @@ $(TARGETPFX)sf-restore.o: ../src/restore.c $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/restore.c $(TARGETPFX)sf-rumors.o: ../src/rumors.c $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/rumors.c +$(TARGETPFX)sfbase.o: ../src/sfbase.c + $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/sfbase.c +$(TARGETPFX)sfdata.o: sfdata.c $(HACK_H) ../include/sfprocs.h ../include/sfproto.h + $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c sfdata.c +$(TARGETPFX)sfexpasc.o: sfexpasc.c $(HACK_H) ../include/sfprocs.h ../include/sfproto.h + $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c sfexpasc.c +$(TARGETPFX)sf-struct.o: ../src/sfstruct.c + $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/sfstruct.c +$(TARGETPFX)strutil.o: ../src/strutil.c + $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/strutil.c $(TARGETPFX)sf-sys.o: ../src/sys.c $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/sys.c $(TARGETPFX)sf-timeout.o: ../src/timeout.c @@ -382,15 +389,9 @@ $(TARGETPFX)sf-version.o: ../src/version.c $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/version.c $(TARGETPFX)sf-worm.o: ../src/worm.c $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/worm.c -$(TARGETPFX)sf-nhlua.o: ../src/nhlua.c - $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/nhlua.c -$(TARGETPFX)sfbase.o: ../src/sfbase.c - $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/sfbase.c -$(TARGETPFX)strutil.o: ../src/strutil.c - $(TARGET_CC) $(TARGET_CFLAGS) $(SFFLAGS) -o $@ -c ../src/strutil.c -sftags: sftags.o - $(CLINK) $(LFLAGS) -o $@ sftags.o $(LIBS) +sftags: sftags.o $(HACKLIB) + $(CLINK) $(LFLAGS) -o $@ sftags.o $(HACKLIB) $(LIBS) sftags.o: sftags.c $(HACK_H) $(CC) $(CFLAGS) -c sftags.c ../include/sfproto.h: sf.tags sftags diff --git a/sys/windows/Makefile.nmake b/sys/windows/Makefile.nmake index f63336683..5f3223117 100644 --- a/sys/windows/Makefile.nmake +++ b/sys/windows/Makefile.nmake @@ -2479,21 +2479,18 @@ $(DAT)bogusmon: $(U)makedefs.exe $(DAT)bogusmon.txt #=============================================================================== # sfutil #=============================================================================== -SFCTOOLOBJS = $(OUTL)sfctool.o $(OUTL)sf-alloc.o \ - $(OUTL)sf-monst.o $(OUTL)sf-objects.o \ - $(OUTL)sfbase.o $(OUTL)sf-struct.o \ - $(OUTL)sfexpasc.o \ - $(OUTL)sfdata.o $(OUTL)sf-nhlua.o \ - $(OUTL)panic.o $(OUTL)sf-date.o $(OUTL)sf-decl.o \ - $(OUTL)sf-artifact.o $(OUTL)sf-cfgfiles.o \ - $(OUTL)sf-dungeon.o $(OUTL)sf-end.o \ +SFCTOOLOBJS = $(OUTL)sfctool.o \ + $(OUTL)sf-alloc.o $(OUTL)sf-artifact.o $(OUTL)sfdata.o \ + $(OUTL)sf-date.o $(OUTL)sf-decl.o $(OUTL)sf-calendar.o \ + $(OUTL)sf-cfgfiles.o $(OUTL)sf-dungeon.o $(OUTL)sf-end.o \ $(OUTL)sf-engrave.o $(OUTL)sf-files.o $(OUTL)sf-light.o \ $(OUTL)sf-mdlib.o $(OUTL)sf-mkmaze.o $(OUTL)sf-mkroom.o \ - $(OUTL)sf-o_init.o $(OUTL)sf-region.o \ - $(OUTL)sf-restore.o $(OUTL)sf-rumors.o \ + $(OUTL)sf-monst.o $(OUTL)sf-nhlua.o $(OUTL)sf-objects.o \ + $(OUTL)sf-o_init.o $(OUTL)panic.o $(OUTL)sf-region.o \ + $(OUTL)sf-restore.o $(OUTL)sf-rumors.o $(OUTL)sfbase.o \ + $(OUTL)sfexpasc.o $(OUTL)sf-struct.o $(OUTL)strutil.o \ $(OUTL)sf-sys.o $(OUTL)sf-timeout.o $(OUTL)sf-track.o \ - $(OUTL)sf-version.o $(OUTL)sf-worm.o \ - $(OUTL)strutil.o $(OUTL)sf-windsys.o + $(OUTL)sf-version.o $(OUTL)sf-worm.o $(OUTL)sf-windsys.o SFCTOOLBIN = $(BinDir)sfctool.exe SFFLAGS = -DSFCTOOL -DNOPANICTRACE -DNOCRASHREPORT -DNO_CHRONICLE @@ -2534,6 +2531,8 @@ $(OUTL)sf-struct.o: $(SRC)sfstruct.c $(Q)$(CC) $(CFLAGS) $(SFFLAGS) -Fo$@ -c $(SRC)sfstruct.c $(OUTL)sf-artifact.o: $(SRC)artifact.c $(Q)$(CC) $(CFLAGS) $(SFFLAGS) -Fo$@ -c $(SRC)artifact.c +$(OUTL)sf-calendar.o: $(SRC)calendar.c + $(Q)$(CC) $(CFLAGS) $(SFFLAGS) -Fo$@ -c $(SRC)calendar.c $(OUTL)sf-cfgfiles.o: $(SRC)cfgfiles.c $(Q)$(CC) $(CFLAGS) $(SFFLAGS) -Fo$@ -c $(SRC)cfgfiles.c $(OUTL)sf-dungeon.o: $(SRC)dungeon.c @@ -2575,8 +2574,8 @@ $(OUTL)sf-sys.o: $(SRC)sys.c $(OUTL)sf-windsys.o: $(MSWSYS)windsys.c $(Q)$(CC) $(CFLAGS) $(SFFLAGS) -Fo$@ -c $(MSWSYS)windsys.c -$(UTIL)sftags.exe: $(OUTL)sftags.o - $(link) $(LFLAGS) /OUT:$@ $(OUTL)sftags.o +$(UTIL)sftags.exe: $(OUTL)sftags.o $(OUTLHACKLIB) + $(link) $(LFLAGS) /OUT:$@ $(OUTL)sftags.o $(OUTLHACKLIB) $(OUTL)sftags.o: $(UTIL)sftags.c $(HACK_H) $(Q)$(CC) $(CFLAGS) -Fo$@ -c $(UTIL)sftags.c $(INCL)sfproto.h: $(UTIL)sftags.exe $(UTIL)sf.tags diff --git a/sys/windows/vs/sfctool/sfctool.vcxproj b/sys/windows/vs/sfctool/sfctool.vcxproj index 7a64f39c9..73fee3c05 100644 --- a/sys/windows/vs/sfctool/sfctool.vcxproj +++ b/sys/windows/vs/sfctool/sfctool.vcxproj @@ -62,6 +62,7 @@ + diff --git a/sys/windows/vs/sftags/sftags.vcxproj b/sys/windows/vs/sftags/sftags.vcxproj index 4131dcd5c..f2979e613 100644 --- a/sys/windows/vs/sftags/sftags.vcxproj +++ b/sys/windows/vs/sftags/sftags.vcxproj @@ -91,6 +91,8 @@ Console true + $(ToolsDir);%(AdditionalLibraryDirectories) + hacklib.lib;%(AdditionalDependencies) @@ -108,6 +110,8 @@ true true true + $(ToolsDir);%(AdditionalLibraryDirectories) + hacklib.lib;%(AdditionalDependencies) @@ -121,6 +125,8 @@ Console true + $(ToolsDir);%(AdditionalLibraryDirectories) + hacklib.lib;%(AdditionalDependencies) @@ -139,6 +145,8 @@ true true true + $(ToolsDir);%(AdditionalLibraryDirectories) + hacklib.lib;%(AdditionalDependencies) diff --git a/util/sfctool.c b/util/sfctool.c index 83b13884e..d1ecaaceb 100644 --- a/util/sfctool.c +++ b/util/sfctool.c @@ -153,7 +153,6 @@ const char *const rensuffixes[] = { extern boolean get_user_home_folder(char *homebuf, size_t sz); /* files.c */ extern void set_default_prefix_locations(const char *programPath); #endif - enum saveformats convertstyle = exportascii; boolean chosen_unconvert = FALSE, explicit_option = FALSE; @@ -693,17 +692,27 @@ read_sysconf(void) #endif /* SYSCF */ } +/* provided for linkage only */ DISABLE_WARNING_FORMAT_NONLITERAL -/* provided for linkage only */ +void +raw_printf(const char *line, ...) +{ + va_list the_args; + + va_start(the_args, line); + vfprintf(stdout, line, the_args); + va_end(the_args); +} + void error(const char *s, ...) { va_list the_args; va_start(the_args, s); - printf(s, the_args); + vprintf(s, the_args); va_end(the_args); exit(EXIT_FAILURE); } @@ -714,7 +723,7 @@ pline(const char *s, ...) va_list the_args; va_start(the_args, s); - printf(s, the_args); + vprintf(s, the_args); va_end(the_args); } @@ -724,148 +733,13 @@ impossible(const char *s, ...) va_list the_args; va_start(the_args, s); - printf(s, the_args); + vprintf(s, the_args); va_end(the_args); exit(EXIT_FAILURE); } RESTORE_WARNING_FORMAT_NONLITERAL -/* TIME_type: type of the argument to time(); we actually use &(time_t) */ -#if defined(BSD) && !defined(POSIX_TYPES) -#define TIME_type long * -#else -#define TIME_type time_t * -#endif -/* LOCALTIME_type: type of the argument to localtime() */ -#if (defined(ULTRIX) && !(defined(ULTRIX_PROTO) || defined(NHSTDC))) \ - || (defined(BSD) && !defined(POSIX_TYPES)) -#define LOCALTIME_type long * -#else -#define LOCALTIME_type time_t * -#endif - -#if defined(AMIGA) && !defined(AZTEC_C) && !defined(__SASC_60) \ - && !defined(_DCC) && !defined(__GNUC__) -extern struct tm *localtime(time_t *); -#endif -static struct tm *getlt(void); - -time_t -getnow(void) -{ - time_t datetime = 0; - - (void) time((TIME_type) &datetime); - return datetime; -} - -static struct tm * -getlt(void) -{ - time_t date = getnow(); - - return localtime((LOCALTIME_type) &date); -} - -int -getyear(void) -{ - return (1900 + getlt()->tm_year); -} - -time_t -time_from_yyyymmddhhmmss(char *buf) -{ - int k; - time_t timeresult = (time_t) 0; - struct tm t, *lt; - char *d, *p, y[5], mo[3], md[3], h[3], mi[3], s[3]; - - if (buf && strlen(buf) == 14) { - d = buf; - p = y; /* year */ - for (k = 0; k < 4; ++k) - *p++ = *d++; - *p = '\0'; - p = mo; /* month */ - for (k = 0; k < 2; ++k) - *p++ = *d++; - *p = '\0'; - p = md; /* day */ - for (k = 0; k < 2; ++k) - *p++ = *d++; - *p = '\0'; - p = h; /* hour */ - for (k = 0; k < 2; ++k) - *p++ = *d++; - *p = '\0'; - p = mi; /* minutes */ - for (k = 0; k < 2; ++k) - *p++ = *d++; - *p = '\0'; - p = s; /* seconds */ - for (k = 0; k < 2; ++k) - *p++ = *d++; - *p = '\0'; - lt = getlt(); - if (lt) { - t = *lt; - t.tm_year = atoi(y) - 1900; - t.tm_mon = atoi(mo) - 1; - t.tm_mday = atoi(md); - t.tm_hour = atoi(h); - t.tm_min = atoi(mi); - t.tm_sec = atoi(s); - timeresult = mktime(&t); - } - return timeresult; - } - return (time_t) 0; -} - -char * -yyyymmddhhmmss(time_t date) -{ - long datenum; - static char datestr[15]; - struct tm *lt; - - if (date == 0) - lt = getlt(); - else -#if (defined(ULTRIX) && !(defined(ULTRIX_PROTO) || defined(NHSTDC))) \ - || defined(BSD) - lt = localtime((long *) (&date)); -#else - lt = localtime(&date); -#endif - /* just in case somebody's localtime supplies (year % 100) - rather than the expected (year - 1900) */ - if (lt->tm_year < 70) - datenum = (long) lt->tm_year + 2000L; - else - datenum = (long) lt->tm_year + 1900L; - Snprintf(datestr, sizeof datestr, "%04ld%02d%02d%02d%02d%02d", - datenum, lt->tm_mon + 1, - lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec); - return datestr; -} - -DISABLE_WARNING_FORMAT_NONLITERAL - -void -raw_printf(const char *line, ...) -{ - va_list the_args; - - va_start(the_args, line); - fprintf(stdout, line, the_args); - va_end(the_args); -} - -RESTORE_WARNING_FORMAT_NONLITERAL - #ifdef UNIX /* normalize file name - we don't like .'s, /'s, spaces */ void @@ -898,7 +772,7 @@ regularize(char *s) #endif #endif } -#endif +#endif /* UNIX */ int util_strncmpi(const char *s1, const char *s2, size_t sz) diff --git a/util/sftags.c b/util/sftags.c index ba821aa6f..5845b2ff5 100644 --- a/util/sftags.c +++ b/util/sftags.c @@ -30,7 +30,7 @@ #elif defined(_MSC_VER) #define strcmpi _stricmp #ifndef strncmpi -#define strncmpi _strnicmp +#define strncmpi _strnicmp #endif #endif @@ -83,7 +83,7 @@ struct needs_array_handling { #define SFDATA_NAME "../util/sfdata.c" static char *fgetline(FILE*); -static void quit(void); +ATTRNORETURN static void quit(void) NORETURN; static void out_of_memory(void); static void doline(char *); static void chain(struct tagstruct *); @@ -154,6 +154,16 @@ static long lineno; static char ssdef[BUFSZ]; static char fieldfixbuf[BUFSZ]; static boolean suppress_count; +static char folderprefix[BUFSZ]; +static char filenmbuf[ +#if defined(UNIX) && defined(PATHLEN) + PATHLEN +#elif defined(WIN32) && defined(_MAX_PATH) + _MAX_PATH +#else + BUFSZ * 4 +#endif + ]; #define NHTYPE_SIMPLE 1 #define NHTYPE_COMPLEX 2 @@ -268,8 +278,7 @@ struct nhdatatypes_t readtagstypes[] = { { NHTYPE_COMPLEX, (char *) "version_info", sizeof(struct version_info) }, { NHTYPE_COMPLEX, (char *) "vlaunchinfo", sizeof(union vlaunchinfo) }, { NHTYPE_COMPLEX, (char *) "vptrs", sizeof(union vptrs) }, - { NHTYPE_COMPLEX, (char *) "you", sizeof(struct you) } - + { NHTYPE_COMPLEX, (char *) "you", sizeof(struct you) }, }; @@ -311,13 +320,30 @@ DISABLE_WARNING_UNREACHABLE_CODE int main(int argc, char *argv[]) { tagcount = 0; - +#if defined(WIN32) + char fbuf[_MAX_PATH]; +#endif + + folderprefix[0] = '\0'; if (argc > 1) infilenm = argv[1]; if (!infilenm || !*infilenm) infilenm = DEFAULTTAGNAME; +#if defined(WIN32) + if (!file_exists(infilenm)) { + /* (void) GetCurrentDirectoryA(sizeof cdir, (LPSTR) cdir); */ + Strcpy(folderprefix, "../../../"); + (void) snprintf(fbuf, sizeof fbuf, "%s%s", folderprefix, DEFAULTTAGNAME); + if (!file_exists(fbuf)) { + fprintf(stdout, "%s doesn't exist\n", fbuf); + exit(EXIT_SUCCESS); + } + infilenm = (const char *) fbuf; + } +#endif + infile = fopen(infilenm,"r"); if (!infile) { - printf("%s not found or unavailable\n",infilenm); + printf("%s not found or unavailable\n", infilenm); quit(); } else { while (fgets(line, sizeof line, infile)) { @@ -341,6 +367,20 @@ int main(int argc, char *argv[]) } } +#ifdef WIN32 +boolean +file_exists(const char *path) +{ + struct stat sb; + + /* Just see if it's there */ + if (stat(path, &sb)) { + return FALSE; + } + return TRUE; +} +#endif + RESTORE_WARNINGS static void doline(char *aline) @@ -373,23 +413,19 @@ static void doline(char *aline) return; } -static struct tagstruct * ALIGN32 prevtag = NULL; +struct tagstruct * ALIGN32 prevtag = 0; -static void chain(struct tagstruct *tag) +static void +chain(struct tagstruct *tag) { - if (!first) { - tag->next = (struct tagstruct *)0; + tag->next = (struct tagstruct *) 0; first = tag; } else { - tag->next = (struct tagstruct *)0; - if (prevtag) { - if (prevtag->marker == 0xDEADBEEF) { - prevtag->next = tag; - } else { - printf("Possible corruption."); - quit(); - } + tag->next = (struct tagstruct *) 0; + + if (prevtag->marker == 0xDEADBEEF) { + prevtag->next = tag; } else { printf("Error - No previous tag at %s\n", tag->tag); } @@ -448,7 +484,7 @@ void set_member_array_size(struct tagstruct *tmptag) if (!tmptag) return; strcpy(buf, tmptag->searchtext); - + tmptag->arraysize1[0] = '\0'; tmptag->arraysize2[0] = '\0'; @@ -491,13 +527,17 @@ void set_member_array_size(struct tagstruct *tmptag) static void parseExtensionFields (struct tagstruct *tmptag, char *buf) { char *p = buf; +#ifdef WIN32 + int debug_catch = 0; +#endif + while (p != (char *)0 && *p != '\0') { while (*p == TAB) *p++ = '\0'; if (*p != '\0') { char *colon; char *field = p; - + p = strchr (p, TAB); if (p != (char *)0) *p++ = '\0'; @@ -510,6 +550,10 @@ static void parseExtensionFields (struct tagstruct *tmptag, char *buf) *colon = '\0'; if ((strcmp (key, "struct") == 0) || (strcmp (key, "union") == 0)) { +#ifdef WIN32 + if (!strcmp(key, "union")) + debug_catch = 1; +#endif colon = strstr(value,"::"); if (colon) value = colon +2; @@ -609,15 +653,6 @@ taglineparse(char *p, struct tagstruct *tmptag) } } -/* eos() is copied from hacklib.c */ -/* return the end of a string (pointing at '\0') */ -char * -eos(char *s) -{ - while (*s) s++; /* s += strlen(s); */ - return s; -} - static char stripbuf[255]; #if 0 @@ -643,7 +678,7 @@ static char *deblank(char *st) *out = '\0'; if (!st) return st; while(*st) { - if (*st == SPACE) { + if (*st == SPACE) { *out++ = '_'; st++; } else @@ -659,7 +694,7 @@ static char *deeol(char *st) *out = '\0'; if (!st) return st; while(*st) { - if ((*st == '\r') || (*st == '\n')) { + if ((*st == '\r') || (*st == '\n')) { st++; } else *out++ = *st++; @@ -680,7 +715,7 @@ static void showthem(void) t->parent[0] ? t->searchtext : ""); #endif buf[0] = '\0'; - tmp = member_array_dims(t,buf); + tmp = member_array_dims(t,buf); if (tmp) printf("%s", tmp); printf("%s","\n"); t = t->next; @@ -819,7 +854,7 @@ findtype(char *st, char *tag) while (isspace(*tmp3)) --tmp3; } - while (!isspace(*tmp3) && (*tmp3 != '*')) + while (!isspace(*tmp3) && (*tmp3 != '*')) --tmp3; while (isspace(*tmp3)) --tmp3; @@ -990,28 +1025,45 @@ static void generate_c_files(void) boolean did_i; boolean normalize_param_used; - SFDATA = fopen(SFDATA_NAME, "w"); +#ifdef WIN32 + int debug_catch = 0; +#endif + + Snprintf(filenmbuf, sizeof filenmbuf, "%s%s", folderprefix,SFDATA_NAME); + SFDATA = fopen(filenmbuf, "w"); if (!SFDATA) return; - SFPROTO = fopen(SFPROTO_NAME, "w"); + Snprintf(filenmbuf, sizeof filenmbuf, "%s%s", folderprefix, SFPROTO_NAME); + SFPROTO = fopen(filenmbuf, "w"); if (!SFPROTO) return; - SFO_DATA = fopen("../util/sfo_data.tmp", "w"); + Snprintf(filenmbuf, sizeof filenmbuf, "%s%s", folderprefix, + "../util/sfo_data.tmp"); + SFO_DATA = fopen(filenmbuf, "w"); if (!SFO_DATA) return; - SFO_PROTO = fopen("../include/sfo_proto.tmp", "w"); + Snprintf(filenmbuf, sizeof filenmbuf, "%s%s", folderprefix, "../include/sfo_proto.tmp"); + SFO_PROTO = fopen(filenmbuf, "w"); if (!SFO_PROTO) return; - SFI_PROTO = fopen("../include/sfi_proto.tmp", "w"); + Snprintf(filenmbuf, sizeof filenmbuf, "%s%s", folderprefix, + "../include/sfi_proto.tmp"); + SFI_PROTO = fopen(filenmbuf, "w"); if (!SFI_PROTO) return; - SFI_DATA = fopen("../util/sfi_data.tmp", "w"); + Snprintf(filenmbuf, sizeof filenmbuf, "%s%s", folderprefix, + "../util/sfi_data.tmp"); + SFI_DATA = fopen(filenmbuf, "w"); if (!SFI_DATA) return; - SFDATATMP = fopen("../util/sfdata.tmp", "w"); + Snprintf(filenmbuf, sizeof filenmbuf, "%s%s", folderprefix, + "../util/sfdata.tmp"); + SFDATATMP = fopen(filenmbuf, "w"); if (!SFDATATMP) return; - SF_NORMALIZE_POINTERS = fopen("../util/sfnormptrs.tmp", "w"); + Snprintf(filenmbuf, sizeof filenmbuf, "%s%s", folderprefix, + "../util/sfnormptrs.tmp"); + SF_NORMALIZE_POINTERS = fopen(filenmbuf, "w"); if (!SF_NORMALIZE_POINTERS) return; (void) time(&clocktim); @@ -1023,7 +1075,7 @@ static void generate_c_files(void) *c = '\0'; /* strip off the '\n' */ /* begin sfproto.h */ - Fprintf(SFPROTO,"/* NetHack %d.%d sfproto.h */\n", + Fprintf(SFPROTO,"/* NetHack %d.%d sfproto.h */\n", VERSION_MAJOR, VERSION_MINOR); for (j = 0; j < 3; ++j) Fprintf(SFPROTO, "%s", get_preamble(j)); @@ -1035,7 +1087,7 @@ static void generate_c_files(void) Fprintf(SFPROTO,"%s\n", "extern void sfi_bitfield(NHFILE *, uint8_t *, const char *, int);"); Fprintf(SFO_PROTO, "/* generated output functions */\n"); Fprintf(SFI_PROTO, "/* generated input functions */\n"); - + /* begin sfdata.c */ Fprintf(SFDATA,"/* NetHack %d.%d sfdata.c */\n", VERSION_MAJOR, VERSION_MINOR); @@ -1062,7 +1114,7 @@ static void generate_c_files(void) output_types(SFDATATMP); Fprintf(SFDATATMP, "const char *critical_members[] = {\n"); - + k = 0; did_i = FALSE; while(k < SIZE(readtagstypes)) { @@ -1070,7 +1122,8 @@ static void generate_c_files(void) suppress_count = TRUE; if (readtagstypes[k].dtclass == NHTYPE_COMPLEX) { - + + if (!strncmpi(readtagstypes[k].dtype,"Bitfield",8)) { Fprintf(SFO_DATA, "\nvoid\nsfo_bitfield(nhfp,\n" @@ -1094,17 +1147,22 @@ static void generate_c_files(void) if (!strncmpi(readtagstypes[k].dtype,"version_info",8)) insert_const = TRUE; #endif +#ifdef WIN32 + if (!strncmpi(readtagstypes[k].dtype, "vlaunchinfo", 11) + || !strncmpi(readtagstypes[k].dtype, "vptrs", 5) + || k == SIZE(readtagstypes) - 2) + debug_catch = TRUE; +#endif pt = (const char *) 0; t = first; while(t) { - if (t->tagtype == 'u' && !strcmp(t->tag, readtagstypes[k].dtype)) { - pt = "union"; + if (!strcmp(t->tag, readtagstypes[k].dtype)) { + pt = (t->tagtype == 'u') ? "union" : "struct"; break; } t = t->next; } - if (!pt) { pt = "struct"; } @@ -1127,7 +1185,7 @@ static void generate_c_files(void) insert_const ? "const " : "", pt, readtagstypes[k].dtype, deblank(readtagstypes[k].dtype)); - + #if 0 Fprintf(SFO_DATA, " const char *parent = \"%s\";\n", @@ -1152,7 +1210,7 @@ static void generate_c_files(void) /* deblank(readtagstypes[k].dtype), */ insert_const ? "const " : "", pt, readtagstypes[k].dtype, - deblank(readtagstypes[k].dtype)); + deblank(readtagstypes[k].dtype)); #if 0 Fprintf(SFI_DATA, @@ -1162,7 +1220,7 @@ static void generate_c_files(void) Sprintf(sfparent, "%s %s", pt, readtagstypes[k].dtype); - + Fprintf(SF_NORMALIZE_POINTERS, "\nvoid normalize_pointers_%s(%s " "*d_%s);\n", @@ -1200,14 +1258,15 @@ static void generate_c_files(void) t = first; normalize_param_used = FALSE; - while(t) { + while(t) { x = 0; okeydokey = 0; -/* - if (!strcmp(t->tag, "nextc") +/* + if (!strcmp(t->tag, "nextc") && !strcmp(readtagstypes[k].dtype, "engrave_info")) __debugbreak(); */ + if (t->tagtype == 's') { char *ss = strstr(t->searchtext,"{$/"); @@ -1262,7 +1321,7 @@ static void generate_c_files(void) char lbuf[BUFSZ]; int j2, z; - Sprintf(lbuf, + Sprintf(lbuf, " " "bitfield = d_%s->%s;", readtagstypes[k].dtype, t->tag); @@ -1274,7 +1333,7 @@ static void generate_c_files(void) Fprintf(SFO_DATA, " " "sfo_bitfield(nhfp, &bitfield, \"%s\", %s);\n", - t->tag, bfsize(ft)); + t->tag, bfsize(ft)); #if 0 Fprintf(SFI_DATA, " " @@ -1450,7 +1509,7 @@ static void generate_c_files(void) (insert_loop && array_of_ptrs) ? "[" : "", (insert_loop && array_of_ptrs) ? "i" : "", (insert_loop && array_of_ptrs) ? "]" : "", - t->parent, t->tag, + t->parent, t->tag, (insert_loop && array_of_ptrs) ? "[" : "", (insert_loop && array_of_ptrs) ? "i" : "", (insert_loop && array_of_ptrs) ? "]" : "", @@ -1553,52 +1612,64 @@ static void generate_c_files(void) /* Consolidate SFO_* and SFI_* etc into single files */ - SFO_DATA = fopen("../util/sfo_data.tmp", "r"); + Snprintf(filenmbuf, sizeof filenmbuf, "%s%s", folderprefix, + "../util/sfo_data.tmp"); + SFO_DATA = fopen(filenmbuf, "r"); if (!SFO_DATA) return; while ((gline = fgetline(SFO_DATA)) != 0) { (void) fputs(gline, SFDATA); free(gline); } (void) fclose(SFO_DATA); - (void) remove("../util/sfo_data.tmp"); + (void) remove(filenmbuf); - SFI_DATA = fopen("../util/sfi_data.tmp", "r"); + Snprintf(filenmbuf, sizeof filenmbuf, "%s%s", folderprefix, + "../util/sfi_data.tmp"); + SFI_DATA = fopen(filenmbuf, "r"); if (!SFI_DATA) return; while ((gline = fgetline(SFI_DATA)) != 0) { (void) fputs(gline, SFDATA); free(gline); } (void) fclose(SFI_DATA); - (void) remove("../util/sfi_data.tmp"); + (void) remove(filenmbuf); - SFO_PROTO = fopen("../include/sfo_proto.tmp", "r"); + Snprintf(filenmbuf, sizeof filenmbuf, "%s%s", folderprefix, + "../include/sfo_proto.tmp"); + SFO_PROTO = fopen(filenmbuf, "r"); if (!SFO_PROTO) return; while ((gline = fgetline(SFO_PROTO)) != 0) { (void) fputs(gline, SFPROTO); free(gline); } (void) fclose(SFO_PROTO); - (void) remove("../include/sfo_proto.tmp"); + (void) remove(filenmbuf); - SFI_PROTO = fopen("../include/sfi_proto.tmp", "r"); + Snprintf(filenmbuf, sizeof filenmbuf, "%s%s", folderprefix, + "../include/sfi_proto.tmp"); + SFI_PROTO = fopen(filenmbuf, "r"); if (!SFI_PROTO) return; while ((gline = fgetline(SFI_PROTO)) != 0) { (void) fputs(gline, SFPROTO); free(gline); } (void) fclose(SFI_PROTO); - (void) remove("../include/sfi_proto.tmp"); + (void) remove(filenmbuf); - SFDATATMP = fopen("../util/sfdata.tmp", "r"); + Snprintf(filenmbuf, sizeof filenmbuf, "%s%s", folderprefix, + "../util/sfdata.tmp"); + SFDATATMP = fopen(filenmbuf, "r"); if (!SFDATATMP) return; while ((gline = fgetline(SFDATATMP)) != 0) { (void) fputs(gline, SFDATA); free(gline); } (void) fclose(SFDATATMP); - (void) remove("../util/sfdata.tmp"); - - SF_NORMALIZE_POINTERS = fopen("../util/sfnormptrs.tmp", "r"); + (void) remove(filenmbuf); + + Snprintf(filenmbuf, sizeof filenmbuf, "%s%s", folderprefix, + "../util/sfnormptrs.tmp"); + SF_NORMALIZE_POINTERS = fopen(filenmbuf, "r"); if (!SF_NORMALIZE_POINTERS) return; while ((gline = fgetline(SF_NORMALIZE_POINTERS)) != 0) { @@ -1606,7 +1677,7 @@ static void generate_c_files(void) free(gline); } (void) fclose(SF_NORMALIZE_POINTERS); - (void) remove("../util/sfnormptrs.tmp"); + (void) remove(filenmbuf); Fprintf(SFDATA, "/*sfdata.c*/\n"); Fprintf(SFPROTO,"#endif /* SFPROTO_H */\n"); @@ -1687,6 +1758,7 @@ dtfn(const char *str, if (!str) return (char *)0; (void)strncpy(buf, str, 127); + buf[sizeof buf - 1] = '\0'; c = buf; while (*c) c++; /* eos */ @@ -1782,7 +1854,7 @@ bfsize(const char *str) *subst++ = ' '; *subst++ = '1'; } - + c2 = buf; c1 = str; while (*c1) { @@ -1812,10 +1884,18 @@ fgetline(FILE *fd) { static const int inc = 256; int len = inc; - char *c = malloc(len), *ret; + char *c = malloc(len), *ret, *loc, *avoidleak; + if (!c) { + fprintf(stderr, "Memory issue\n"); + quit(); + } for (;;) { - ret = fgets(c + len - inc, inc, fd); + loc = c + len - inc; + if (!loc) { + quit(); + } + ret = fgets(loc, inc, fd); if (!ret) { free(c); c = NULL; @@ -1825,13 +1905,19 @@ fgetline(FILE *fd) break; } len += inc; + avoidleak = c; c = realloc(c, len); + if (!c) { + free(avoidleak); + quit(); + } } return c; } +#ifndef _MSC_VER int -strncmpi(register const char *s1, register const char *s2, size_t n) +strncmpi(const char *s1, const char *s2, size_t n) { register char t1, t2; @@ -1847,59 +1933,8 @@ strncmpi(register const char *s1, register const char *s2, size_t n) } return 0; /* s1 == s2 */ } - -/* force 'c' into uppercase */ -char -highc(char c) -{ - return (char) (('a' <= c && c <= 'z') ? (c & ~040) : c); -} - -/* force 'c' into lowercase */ -char -lowc(char c) -{ - return (char) (('A' <= c && c <= 'Z') ? (c | 040) : c); -} - -DISABLE_WARNING_FORMAT_NONLITERAL - -/* - * Wrap snprintf for use in the main code. - * - * Wrap reasons: - * 1. If there are any platform issues, we have one spot to fix them - - * snprintf is a routine with a troubling history of bad implementations. - * 2. Add combersome error checking in one spot. Problems with text wrangling - * do not have to be fatal. - * 3. Gcc 9+ will issue a warning unless the return value is used. - * Annoyingly, explicitly casting to void does not remove the error. - * So, use the result - see reason #2. - */ -void -nh_snprintf(const char *func, int myline, char *str, size_t size, - const char *fmt, ...) -{ - va_list ap; - int n; - - va_start(ap, fmt); -#ifdef NO_VSNPRINTF - n = vsprintf(str, fmt, ap); -#else - n = vsnprintf(str, size, fmt, ap); #endif - va_end(ap); - if (n < 0 || (size_t)n >= size) { /* is there a problem? */ - fprintf(stderr, "snprintf %s: func %s, file line %d", - n < 0 ? "format error" - : "overflow", - func, myline); - str[size-1] = 0; /* make sure it is nul terminated */ - } -} -RESTORE_WARNING_FORMAT_NONLITERAL struct already_in_sfbase { int typ;