xref: /netbsd/games/hack/hack.options.c (revision 6550d01e)
1 /*	$NetBSD: hack.options.c,v 1.10 2009/08/12 07:28:41 dholland Exp $	*/
2 
3 /*
4  * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
5  * Amsterdam
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are
10  * met:
11  *
12  * - Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * - Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * - Neither the name of the Stichting Centrum voor Wiskunde en
20  * Informatica, nor the names of its contributors may be used to endorse or
21  * promote products derived from this software without specific prior
22  * written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 /*
38  * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
39  * All rights reserved.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. The name of the author may not be used to endorse or promote products
50  *    derived from this software without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
55  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  */
63 
64 #include <sys/cdefs.h>
65 #ifndef lint
66 __RCSID("$NetBSD: hack.options.c,v 1.10 2009/08/12 07:28:41 dholland Exp $");
67 #endif				/* not lint */
68 
69 #include <stdlib.h>
70 #include <unistd.h>
71 #include "hack.h"
72 #include "extern.h"
73 
74 static void parseoptions(char *, boolean);
75 
76 void
77 initoptions(void)
78 {
79 	char           *opts;
80 
81 	flags.time = flags.nonews = flags.notombstone = flags.end_own =
82 		flags.standout = flags.nonull = FALSE;
83 	flags.no_rest_on_space = TRUE;
84 	flags.invlet_constant = TRUE;
85 	flags.end_top = 5;
86 	flags.end_around = 4;
87 	flags.female = FALSE;	/* players are usually male */
88 
89 	if ((opts = getenv("HACKOPTIONS")) != NULL)
90 		parseoptions(opts, TRUE);
91 }
92 
93 static void
94 parseoptions(char *opts, boolean from_env)
95 {
96 	char           *op, *op2;
97 	unsigned        num;
98 	boolean         negated;
99 
100 	if ((op = strchr(opts, ',')) != NULL) {
101 		*op++ = 0;
102 		parseoptions(op, from_env);
103 	}
104 	if ((op = strchr(opts, ' ')) != NULL) {
105 		op2 = op;
106 		while (*op++)
107 			if (*op != ' ')
108 				*op2++ = *op;
109 	}
110 	if (!*opts)
111 		return;
112 	negated = FALSE;
113 	while ((*opts == '!') || !strncmp(opts, "no", 2)) {
114 		if (*opts == '!')
115 			opts++;
116 		else
117 			opts += 2;
118 		negated = !negated;
119 	}
120 
121 	if (!strncmp(opts, "standout", 8)) {
122 		flags.standout = !negated;
123 		return;
124 	}
125 	if (!strncmp(opts, "null", 3)) {
126 		flags.nonull = negated;
127 		return;
128 	}
129 	if (!strncmp(opts, "tombstone", 4)) {
130 		flags.notombstone = negated;
131 		return;
132 	}
133 	if (!strncmp(opts, "news", 4)) {
134 		flags.nonews = negated;
135 		return;
136 	}
137 	if (!strncmp(opts, "time", 4)) {
138 		flags.time = !negated;
139 		flags.botl = 1;
140 		return;
141 	}
142 	if (!strncmp(opts, "restonspace", 4)) {
143 		flags.no_rest_on_space = negated;
144 		return;
145 	}
146 	if (!strncmp(opts, "fixinv", 4)) {
147 		if (from_env)
148 			flags.invlet_constant = !negated;
149 		else
150 			pline("The fixinvlet option must be in HACKOPTIONS.");
151 		return;
152 	}
153 	if (!strncmp(opts, "male", 4)) {
154 		flags.female = negated;
155 		return;
156 	}
157 	if (!strncmp(opts, "female", 6)) {
158 		flags.female = !negated;
159 		return;
160 	}
161 	/* name:string */
162 	if (!strncmp(opts, "name", 4)) {
163 		if (!from_env) {
164 			pline("The playername can be set only from HACKOPTIONS.");
165 			return;
166 		}
167 		op = strchr(opts, ':');
168 		if (!op)
169 			goto bad;
170 		(void) strncpy(plname, op + 1, sizeof(plname) - 1);
171 		return;
172 	}
173 	/* endgame:5t[op] 5a[round] o[wn] */
174 	if (!strncmp(opts, "endgame", 3)) {
175 		op = strchr(opts, ':');
176 		if (!op)
177 			goto bad;
178 		op++;
179 		while (*op) {
180 			num = 1;
181 			if (digit(*op)) {
182 				num = atoi(op);
183 				while (digit(*op))
184 					op++;
185 			} else if (*op == '!') {
186 				negated = !negated;
187 				op++;
188 			}
189 			switch (*op) {
190 			case 't':
191 				flags.end_top = num;
192 				break;
193 			case 'a':
194 				flags.end_around = num;
195 				break;
196 			case 'o':
197 				flags.end_own = !negated;
198 				break;
199 			default:
200 				goto bad;
201 			}
202 			while (letter(*++op));
203 			if (*op == '/')
204 				op++;
205 		}
206 		return;
207 	}
208 bad:
209 	if (!from_env) {
210 		if (!strncmp(opts, "help", 4)) {
211 			pline("%s%s%s",
212 			      "To set options use `HACKOPTIONS=\"<options>\"' in your environment, or ",
213 			      "give the command 'O' followed by the line `<options>' while playing. ",
214 			      "Here <options> is a list of <option>s separated by commas.");
215 			pline("%s%s%s",
216 			      "Simple (boolean) options are rest_on_space, news, time, ",
217 			      "null, tombstone, (fe)male. ",
218 			      "These can be negated by prefixing them with '!' or \"no\".");
219 			pline("%s",
220 			      "A string option is name, as in HACKOPTIONS=\"name:Merlin-W\".");
221 			pline("%s%s%s",
222 			      "A compound option is endgame; it is followed by a description of what ",
223 			      "parts of the scorelist you want to see. You might for example say: ",
224 			      "`endgame:own scores/5 top scores/4 around my score'.");
225 			return;
226 		}
227 		pline("Bad option: %s.", opts);
228 		pline("Type `O help<cr>' for help.");
229 		return;
230 	}
231 	puts("Bad syntax in HACKOPTIONS.");
232 	puts("Use for example:");
233 	puts(
234 	     "HACKOPTIONS=\"!restonspace,notombstone,endgame:own/5 topscorers/4 around me\""
235 		);
236 	getret();
237 }
238 
239 int
240 doset(void)
241 {
242 	char buf[BUFSZ];
243 	size_t pos;
244 
245 	pline("What options do you want to set? ");
246 	getlin(buf);
247 	if (!buf[0] || buf[0] == '\033') {
248 		(void) strcpy(buf, "HACKOPTIONS=");
249 		(void) strcat(buf, flags.female ? "female," : "male,");
250 		if (flags.standout)
251 			(void) strlcat(buf, "standout,", sizeof(buf));
252 		if (flags.nonull)
253 			(void) strlcat(buf, "nonull,", sizeof(buf));
254 		if (flags.nonews)
255 			(void) strlcat(buf, "nonews,", sizeof(buf));
256 		if (flags.time)
257 			(void) strlcat(buf, "time,", sizeof(buf));
258 		if (flags.notombstone)
259 			(void) strlcat(buf, "notombstone,", sizeof(buf));
260 		if (flags.no_rest_on_space)
261 			(void) strlcat(buf, "!rest_on_space,", sizeof(buf));
262 		if (flags.end_top != 5 || flags.end_around != 4 || flags.end_own) {
263 			pos = strlen(buf);
264 			(void) snprintf(buf+pos, sizeof(buf)-pos,
265 				       "endgame: %u topscores/%u around me",
266 				       flags.end_top, flags.end_around);
267 			if (flags.end_own)
268 				(void) strlcat(buf, "/own scores", sizeof(buf));
269 		} else {
270 			char           *eop = eos(buf);
271 			if (*--eop == ',')
272 				*eop = 0;
273 		}
274 		pline(buf);
275 	} else
276 		parseoptions(buf, FALSE);
277 
278 	return (0);
279 }
280