-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhack.options.c
More file actions
315 lines (290 loc) · 8.15 KB
/
Copy pathhack.options.c
File metadata and controls
315 lines (290 loc) · 8.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* hack.options.c - version 1.0.3 */
/* $FreeBSD$ */
/*
* Game options system for 1984 Hack - player preferences and configuration
* Original 1984 source: docs/historical/original-source/hack.options.c
*
* Key modernizations: ANSI C function signatures
*/
#include "hack.h"
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
extern char *eos();
/* Forward declaration */
void parseoptions(char *opts, boolean from_env);
void initoptions(void) {
char *opts;
flags.time = flags.nonews = flags.notombstone = flags.end_own =
flags.standout = flags.nonull = FALSE;
flags.no_rest_on_space = TRUE;
flags.invlet_constant = TRUE;
flags.end_top = 5;
flags.end_around = 4;
flags.female = FALSE; /* players are usually male */
if ((opts = getenv("HACKOPTIONS"))) {
/* MODERN: Validate environment variable length to prevent attacks */
if (strlen(opts) > 1024) {
pline("HACKOPTIONS too long (max 1024 chars) - ignoring.");
} else {
parseoptions(opts, TRUE);
}
}
}
/* MODERN: Track recursion depth to prevent stack overflow */
static int parse_depth = 0;
#define MAX_PARSE_DEPTH 50
void parseoptions(char *opts, boolean from_env) {
char *op, *op2;
unsigned num;
boolean negated;
/* MODERN: Prevent stack overflow from recursive parsing */
if (++parse_depth > MAX_PARSE_DEPTH) {
pline("Options parsing too deep - possible malformed input.");
parse_depth--;
return;
}
if ((op = index(opts, ','))) {
*op++ = 0;
parseoptions(op, from_env);
}
parse_depth--;
if ((op = index(opts, ' '))) {
op2 = op;
/* MODERN: Safe space removal with bounds checking */
char *end = opts + strlen(opts);
while (op < end && *op) {
if (*op != ' ' && op2 < end) {
*op2++ = *op;
}
op++;
}
if (op2 < end) *op2 = '\0'; /* Ensure null termination */
}
if (!*opts)
return;
negated = FALSE;
while ((*opts == '!') || !strncmp(opts, "no", 2)) {
if (*opts == '!')
opts++;
else
opts += 2;
negated = !negated;
}
if (!strncmp(opts, "standout", 8)) {
flags.standout = !negated;
return;
}
if (!strncmp(opts, "null", 3)) {
flags.nonull = negated;
return;
}
if (!strncmp(opts, "tombstone", 4)) {
flags.notombstone = negated;
return;
}
if (!strncmp(opts, "news", 4)) {
flags.nonews = negated;
return;
}
if (!strncmp(opts, "time", 4)) {
flags.time = !negated;
flags.botl = 1;
return;
}
if (!strncmp(opts, "restonspace", 4)) {
flags.no_rest_on_space = negated;
return;
}
if (!strncmp(opts, "fixinv", 4)) {
if (from_env)
flags.invlet_constant = !negated;
else
pline("The fixinvlet option must be in HACKOPTIONS.");
return;
}
if (!strncmp(opts, "male", 4)) {
flags.female = negated;
return;
}
if (!strncmp(opts, "female", 6)) {
flags.female = !negated;
return;
}
/* name:string */
if (!strncmp(opts, "name", 4)) {
extern char plname[PL_NSIZ];
if (!from_env) {
pline("The playername can be set only from HACKOPTIONS.");
return;
}
op = index(opts, ':');
if (!op)
goto bad;
(void)strncpy(plname, op + 1, sizeof(plname) - 1);
plname[sizeof(plname) - 1] = '\0'; /* MODERN: Ensure null termination */
return;
}
/* endgame:5t[op] 5a[round] o[wn] */
if (!strncmp(opts, "endgame", 3)) {
op = index(opts, ':');
if (!op)
goto bad;
op++;
while (*op) {
num = 1;
if (digit(*op)) {
num = atoi(op);
/* MODERN: Validate numeric input to prevent integer overflow */
if (num > 9999) {
pline("Option value too large (max 9999): %u", num);
goto bad;
}
while (digit(*op))
op++;
} else if (*op == '!') {
negated = !negated;
op++;
}
switch (*op) {
case 't':
flags.end_top = num;
break;
case 'a':
flags.end_around = num;
break;
case 'o':
flags.end_own = !negated;
break;
default:
goto bad;
}
while (letter(*++op))
;
if (*op == '/')
op++;
}
return;
}
bad:
if (!from_env) {
if (!strncmp(opts, "help", 4)) {
pline("%s%s%s",
"To set options use `HACKOPTIONS=\"<options>\"' in your "
"environment, or ",
"give the command 'o' followed by the line `<options>' while "
"playing. ",
"Here <options> is a list of <option>s separated by commas.");
pline("%s%s%s",
"Simple (boolean) options are rest_on_space, news, time, ",
"null, tombstone, (fe)male. ",
"These can be negated by prefixing them with '!' or \"no\".");
pline("%s",
"A string option is name, as in HACKOPTIONS=\"name:Merlin-W\".");
pline(
"%s%s%s",
"A compound option is endgame; it is followed by a description of "
"what ",
"parts of the scorelist you want to see. You might for example say: ",
"`endgame:own scores/5 top scores/4 around my score'.");
return;
}
pline("Bad option: %.50s.", opts); /* MODERN: Limit format string length to prevent attacks */
pline("Type `o help<cr>' for help.");
return;
}
puts("Bad syntax in HACKOPTIONS.");
puts("Use for example:");
puts("HACKOPTIONS=\"!restonspace,notombstone,endgame:own/5 topscorers/4 "
"around me\"");
getret();
}
int doset(void) {
char buf[BUFSZ];
pline("What options do you want to set? ");
getlin(buf);
if (!buf[0] || buf[0] == '\033') {
/* MODERN: Safe options string building with bounds tracking */
int len = 0, remaining = BUFSZ;
len = snprintf(buf, remaining, "HACKOPTIONS=");
if (len >= remaining)
len = remaining - 1;
remaining -= len;
if (remaining > 0) {
int added = snprintf(buf + len, remaining, "%s",
flags.female ? "female," : "male,");
if (added > 0 && added < remaining) {
len += added;
remaining -= added;
}
}
if (flags.standout && remaining > 0) {
int added = snprintf(buf + len, remaining, "standout,");
if (added > 0 && added < remaining) {
len += added;
remaining -= added;
}
}
if (flags.nonull && remaining > 0) {
int added = snprintf(buf + len, remaining, "nonull,");
if (added > 0 && added < remaining) {
len += added;
remaining -= added;
}
}
if (flags.nonews && remaining > 0) {
int added = snprintf(buf + len, remaining, "nonews,");
if (added > 0 && added < remaining) {
len += added;
remaining -= added;
}
}
if (flags.time && remaining > 0) {
int added = snprintf(buf + len, remaining, "time,");
if (added > 0 && added < remaining) {
len += added;
remaining -= added;
}
}
if (flags.notombstone && remaining > 0) {
int added = snprintf(buf + len, remaining, "notombstone,");
if (added > 0 && added < remaining) {
len += added;
remaining -= added;
}
}
if (flags.no_rest_on_space && remaining > 0) {
int added = snprintf(buf + len, remaining, "!rest_on_space,");
if (added > 0 && added < remaining) {
len += added;
remaining -= added;
}
}
if ((flags.end_top != 5 || flags.end_around != 4 || flags.end_own) &&
remaining > 0) {
int added =
snprintf(buf + len, remaining, "endgame: %u topscores/%u around me",
flags.end_top, flags.end_around);
if (added > 0 && added < remaining) {
len += added;
remaining -= added;
}
if (flags.end_own && remaining > 0) {
added = snprintf(buf + len, remaining, "/own scores");
if (added > 0 && added < remaining) {
len += added;
remaining -= added;
}
}
} else {
/* Remove trailing comma if present */
if (len > 0 && buf[len - 1] == ',') {
buf[len - 1] = 0;
}
}
pline("%s", buf); /* Modern: Fix format string vulnerability */
} else
parseoptions(buf, FALSE);
return (0);
}