MCPcopy Create free account
hub / github.com/NetHack/NetHack / lopt

Function lopt

src/earlyarg.c:70–144  ·  view source on GitHub ↗

approximate 'getopt_long()' for one option; all the comments refer to "-windowtype" but the code isn't specific to that */

Source from the content-addressed store, hash-verified

68/* approximate 'getopt_long()' for one option; all the comments refer to
69 "-windowtype" but the code isn't specific to that */
70staticfn char *
71lopt(char *arg, /* command line token; beginning matches 'optname' */
72 int lflags, /* cmdlinearg | errorhandling */
73 const char *optname, /* option's name; "-windowtype" in examples below */
74 const char *origarg, /* 'arg' might have had a dash prefix removed */
75 int *argc_p, /* argc that can have changes passed to caller */
76 char ***argv_p) /* argv[] ditto */
77{
78 int argc = *argc_p;
79 char **argv = *argv_p;
80 char *p, *nextarg = (argc > 1 && argv[1][0] != '-') ? argv[1] : 0;
81 int l, opttype = (lflags & ArgVal_mask);
82 boolean oneletterok = ((lflags & ArgNam_mask) == ArgNamOneLetter),
83 complain = ((lflags & ArgErr_mask) == ArgErrComplain);
84
85 /* first letter must match */
86 if (arg[1] != optname[1]) {
87 loptbail:
88 if (complain)
89 config_error_add("Unknown option: %.60s", origarg);
90 return (char *) 0;
91 loptnotallowed:
92 if (complain)
93 config_error_add("Value not allowed: %.60s", origarg);
94 return (char *) 0;
95 loptrequired:
96 if (complain)
97 config_error_add("Missing required value: %.60s", origarg);
98 return (char *) 0;
99 }
100
101 if ((p = strchr(arg, '=')) == 0)
102 p = strchr(arg, ':');
103 if (p && opttype == ArgValDisallowed)
104 goto loptnotallowed;
105
106 l = (int) (p ? (long) (p - arg) : (long) strlen(arg));
107 if ((l > 2 || oneletterok) && !strncmp(arg, optname, l)) {
108 /* "-windowtype[=foo]" */
109 if (p)
110 ++p; /* past '=' or ':' */
111 else if (opttype == ArgValRequired)
112 p = eos(arg); /* we have "-w[indowtype]" w/o "=foo"
113 * so we'll take foo from next element */
114 else
115 return ArgVal_novalue;
116 } else if (oneletterok) {
117 /* "-w..." but not "-w[indowtype[=foo]]" */
118 if (!p) {
119 p = &arg[2]; /* past 'w' of "-wfoo" */
120#if 0 /* -x:value could work but is not supported (callers don't expect it) \
121 */
122 } else if (p == arg + 2) {
123 ++p; /* past ':' of "-w:foo" */
124#endif
125 } else {
126 /* "-w...=foo" but not "-w[indowtype]=foo" */
127 goto loptbail;

Callers 1

early_optionsFunction · 0.85

Calls 2

config_error_addFunction · 0.85
eosFunction · 0.85

Tested by

no test coverage detected