From c1f6c64c2d3d50bb7f41fc2cbe65ae588755cd51 Mon Sep 17 00:00:00 2001 From: David Griffith Date: Mon, 23 Sep 2019 19:32:25 -0700 Subject: [PATCH] Remove superfluous getopt() implementations and move to core. --- src/common/frotz.h | 9 +++++++++ src/common/getopt.c | 38 ++++++++++++++++++-------------------- src/curses/ux_init.c | 43 ++----------------------------------------- src/dumb/dumb_init.c | 43 +++---------------------------------------- src/sdl/sf_util.c | 31 ++++++++++++++++++------------- 5 files changed, 50 insertions(+), 114 deletions(-) diff --git a/src/common/frotz.h b/src/common/frotz.h index 5306c61..ea77e59 100644 --- a/src/common/frotz.h +++ b/src/common/frotz.h @@ -564,6 +564,9 @@ extern char *option_zcode_path; /* dg */ extern long reserve_mem; +extern int zoptind; +extern int zoptopt; +extern char *zoptarg; /*** Z-machine opcodes ***/ void z_add(void); @@ -776,6 +779,12 @@ bool read_yes_or_no(const char *); void screen_new_line(void); +#ifndef MSDOS_16BIT +#define cdecl +#endif + +int cdecl zgetopt(int, char **, const char *); + /*** returns the current window ***/ Zwindow * curwinrec(void); diff --git a/src/common/getopt.c b/src/common/getopt.c index efe99af..4e67f54 100644 --- a/src/common/getopt.c +++ b/src/common/getopt.c @@ -15,50 +15,48 @@ #define cdecl #endif -int optind = 1; -int optopt = 0; +int zoptind = 1; +int zoptopt = 0; +char *zoptarg = NULL; -const char *optarg = NULL; - -int cdecl getopt (int argc, char *argv[], const char *options) +int cdecl zgetopt (int argc, char *argv[], const char *options) { static int pos = 1; const char *p; - if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == 0) + if (zoptind >= argc || argv[zoptind][0] != '-' || argv[zoptind][1] == 0) return EOF; - optopt = argv[optind][pos++]; - optarg = NULL; + zoptopt = argv[zoptind][pos++]; + zoptarg = NULL; - if (argv[optind][pos] == 0) { + if (argv[zoptind][pos] == 0) { pos = 1; - optind++; + zoptind++; } - p = strchr(options, optopt); + p = strchr(options, zoptopt); - if (optopt == ':' || p == NULL) { + if (zoptopt == ':' || p == NULL) { fputs("illegal option -- ", stdout); goto error; } else if (p[1] == ':') { - if (optind >= argc) { + if (zoptind >= argc) { fputs("option requires an argument -- ", stdout); goto error; } else { - optarg = argv[optind]; + zoptarg = argv[zoptind]; if (pos != 1) - optarg += pos; + zoptarg += pos; pos = 1; - optind++; + zoptind++; } } - return optopt; + return zoptopt; error: - - fputc(optopt, stdout); + fputc(zoptopt, stdout); fputc('\n', stdout); return '?'; -} /* getopt */ +} /* zgetopt */ diff --git a/src/curses/ux_init.c b/src/curses/ux_init.c index 376b69f..d144d55 100644 --- a/src/curses/ux_init.c +++ b/src/curses/ux_init.c @@ -91,11 +91,6 @@ u_setup_t u_setup; static void sigint_handler(int); /* static void redraw(void); */ -static int zgetopt (int, char **, const char *); -static int zoptind = 1; -static int zoptopt = 0; -static char *zoptarg = NULL; - static void print_version(void); static int getconfig(char *); static int getbool(char *); @@ -205,6 +200,8 @@ void os_process_arguments (int argc, char *argv[]) char *home; char configfile[FILENAME_MAX + 1]; + zoptarg = NULL; + #ifndef WIN32 if ((getuid() == 0) || (geteuid() == 0)) { printf("I won't run as root!\n"); @@ -1155,42 +1152,6 @@ char *my_strrchr(const char *s, int c) #endif /* NO_STRRCHR */ -/* A unix-like getopt, but with the names changed to avoid any problems. */ -static int zgetopt (int argc, char *argv[], const char *options) -{ - static int pos = 1; - const char *p; - if (zoptind >= argc || argv[zoptind][0] != '-' || argv[zoptind][1] == 0) - return EOF; - zoptopt = argv[zoptind][pos++]; - zoptarg = NULL; - if (argv[zoptind][pos] == 0) { - pos = 1; - zoptind++; - } - p = strchr (options, zoptopt); - if (zoptopt == ':' || p == NULL) { - fputs ("illegal option -- ", stderr); - goto error; - } else if (p[1] == ':') { - if (zoptind >= argc) { - fputs ("option requires an argument -- ", stderr); - goto error; - } else { - zoptarg = argv[zoptind]; - if (pos != 1) - zoptarg += pos; - pos = 1; zoptind++; - } - } - return zoptopt; -error: - fputc (zoptopt, stderr); - fputc ('\n', stderr); - return '?'; -} /* zgetopt */ - - static void print_version(void) { printf("FROTZ V%s\tCurses interface. ", VERSION); diff --git a/src/dumb/dumb_init.c b/src/dumb/dumb_init.c index 48640e0..2a952e2 100644 --- a/src/dumb/dumb_init.c +++ b/src/dumb/dumb_init.c @@ -48,52 +48,13 @@ For more options and explanations, please read the manual page.\n\n\ While running, enter \"\\help\" to list the runtime escape sequences.\n" -/* A unix-like getopt, but with the names changed to avoid any problems. */ -static int zoptind = 1; -static int zoptopt = 0; -static char *zoptarg = NULL; -static int zgetopt (int argc, char *argv[], const char *options) -{ - static int pos = 1; - const char *p; - - if (zoptind >= argc || argv[zoptind][0] != '-' || - argv[zoptind][1] == 0) - return EOF; - zoptopt = argv[zoptind][pos++]; - zoptarg = NULL; - if (argv[zoptind][pos] == 0) { - pos = 1; - zoptind++; - } - p = strchr (options, zoptopt); - if (zoptopt == ':' || p == NULL) { - fputs ("illegal option -- ", stderr); - goto error; - } else if (p[1] == ':') { - if (zoptind >= argc) { - fputs ("option requires an argument -- ", stderr); - goto error; - } else { - zoptarg = argv[zoptind]; - if (pos != 1) - zoptarg += pos; - pos = 1; zoptind++; - } - } - return zoptopt; -error: - fputc (zoptopt, stderr); - fputc ('\n', stderr); - return '?'; -}/* zgetopt */ - static int user_screen_width = 75; static int user_screen_height = 24; static int user_random_seed = -1; static int user_tandy_bit = 0; static bool plain_ascii = FALSE; + /* * os_process_arguments * @@ -106,6 +67,8 @@ void os_process_arguments(int argc, char *argv[]) int c; char *p = NULL; + zoptarg = NULL; + do_more_prompts = TRUE; /* Parse the options */ do { diff --git a/src/sdl/sf_util.c b/src/sdl/sf_util.c index 7f4c695..8e73f33 100644 --- a/src/sdl/sf_util.c +++ b/src/sdl/sf_util.c @@ -218,8 +218,11 @@ static void usage(int type) static const char *progname = NULL; +/* extern char *optarg; extern int optind; +*/ + extern int m_timerinterval; static char *options = "@:%aAb:B:c:f:Fh:HiI:l:L:m:N:oOPqr:s:S:tTu:vVw:xZ:"; @@ -241,11 +244,11 @@ static void parse_options(int argc, char **argv) do { int num = 0, copt = 0;; - c = getopt(argc, argv, options); + c = zgetopt(argc, argv, options); - if (optarg != NULL) { - num = atoi(optarg); - copt = optarg[0]; + if (zoptarg != NULL) { + num = atoi(zoptarg); + copt = zoptarg[0]; } if (c == '%') @@ -265,9 +268,9 @@ static void parse_options(int argc, char **argv) if (c == 'N') user_names_format = copt; if (c == '@') - m_reslist_file = optarg; + m_reslist_file = zoptarg; if (c == 'I') - m_setupfile = optarg; + m_setupfile = zoptarg; if (c == 'f') user_foreground = num; if (c == 'F') @@ -286,7 +289,7 @@ static void parse_options(int argc, char **argv) f_setup.left_margin = num; if (c == 'L') { f_setup.restore_mode = TRUE; - f_setup.tmp_save_name = strdup(optarg); + f_setup.tmp_save_name = strdup(zoptarg); } if (c == 'q') m_no_sound = 1; @@ -320,7 +323,7 @@ static void parse_options(int argc, char **argv) if (num >= ERR_REPORT_NEVER && num <= ERR_REPORT_FATAL) f_setup.err_report_mode = num; if (c == '?') - optind = argc; + zoptind = argc; } while (c != EOF && c != '?'); } /* parse_options */ @@ -379,18 +382,20 @@ void os_process_arguments(int argc, char *argv[]) { char *p; + zoptarg = NULL; + sf_installhandlers(); sf_readsettings(); parse_options(argc, argv); - if (argv[optind] == NULL) { + if (argv[zoptind] == NULL) { usage(USAGE_NORMAL); exit(EXIT_FAILURE); } - f_setup.story_file = strdup(argv[optind]); + f_setup.story_file = strdup(argv[zoptind]); - if (argv[optind + 1] != NULL) - f_setup.blorb_file = argv[optind + 1]; + if (argv[zoptind + 1] != NULL) + f_setup.blorb_file = argv[zoptind + 1]; /* Strip path and extension off the story file name */ f_setup.story_name = new_basename(f_setup.story_file); @@ -413,7 +418,7 @@ void os_process_arguments(int argc, char *argv[]) *p = '\0'; /* extension removed */ } } - f_setup.story_path = new_dirname(argv[optind]); + f_setup.story_path = new_dirname(argv[zoptind]); /* Create nice default file names */ f_setup.script_name = -- 2.34.1