Unix command line parsing

Move a bunch of stuff out of main() into new early_options(): '-dpath'
playground directory handling, '-s ...' show scores instead of playing,
and the 'argcheck()' options:  --version, --showpaths, --dumpenums,
and --debug (not to be confused with -D).  Also introduce
| --nethackrc=filename
| --no-nethackrc
to control RC file without using NETHACKOPTIONS so that that is still
available for setting other options.  They can start with either one
or two dashes.  --no-nethackrc is just --nethackrc=/dev/null under the
hood.  '-dpath' can now be '--directory=path' or '--directory path'
but the old syntax should still work.  '-s ...' can be '--scores ...'.

Basic call sequence in unixmain relating to options is now
|main() {
|  early_options(argc, argv[]);
|  initoptions(); /* process sysconf, .nethackrc, NETHACKOPTIONS */
|  process_options(possibly_modified_argc, possibly_modified_argv[]);
|}
Options processed by early_options() that don't terminate the program
are moved to the end of argv[], with argc reduced accordingly.  Then
process_options() only sees the ones that early_options() declines to
handle.

Most early options were using plain exit() instead of nh_terminate()
so not performing any nethack-specific cleanup.  However, since they
run before the game starts, there wasn't much cleanup being overlooked.

chdirx() takes a boolean as second argument but all its callers were
passing int (with value of 1 or 0, so it still worked after being
implicitly fixed by prototype).  Change them to pass TRUE or FALSE.

argcheck() was refusing (argc,argv[]) with count of 1 but then it was
checking 0..N-1 rather than 1..N-1, so it tested whether argv[0] was
an argument instead of skipping that as the program name.  Change to
allow count of 1 with modified argv that has an option name in argv[0].
That happens to fit well with how early_options() wanted to use it.
This commit is contained in:
PatR
2022-02-18 14:38:24 -08:00
parent cd14456b02
commit 45bc2dafa9
3 changed files with 351 additions and 126 deletions

View File

@@ -1,4 +1,4 @@
/* NetHack 3.7 allmain.c $NHDT-Date: 1644517022 2022/02/10 18:17:02 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.174 $ */
/* NetHack 3.7 allmain.c $NHDT-Date: 1645223894 2022/02/18 22:38:14 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.177 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) Robert Patrick Rankin, 2012. */
/* NetHack may be freely redistributed. See license for details. */
@@ -845,7 +845,6 @@ extern int windows_early_options(const char *);
* 1 = found and skip past this argument
* 2 = found and trigger immediate exit
*/
int
argcheck(int argc, char *argv[], enum earlyarg e_arg)
{
@@ -858,7 +857,7 @@ argcheck(int argc, char *argv[], enum earlyarg e_arg)
if (earlyopts[idx].e == e_arg)
break;
}
if ((idx >= SIZE(earlyopts)) || (argc <= 1))
if (idx >= SIZE(earlyopts) || argc < 1)
return FALSE;
for (i = 0; i < argc; ++i) {