Expand PMF_FN_* macros.
[netbsd-mini2440.git] / games / hack / hack.options.c
blob71d2ddf6fdf9813535301a6fc0effd455660f111
1 /* $NetBSD: hack.options.c,v 1.9 2009/06/07 20:13:18 dholland Exp $ */
3 /*
4 * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
5 * Amsterdam
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
12 * - Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
38 * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
39 * All rights reserved.
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.
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.
64 #include <sys/cdefs.h>
65 #ifndef lint
66 __RCSID("$NetBSD: hack.options.c,v 1.9 2009/06/07 20:13:18 dholland Exp $");
67 #endif /* not lint */
69 #include <stdlib.h>
70 #include <unistd.h>
71 #include "hack.h"
72 #include "extern.h"
74 static void parseoptions(char *, boolean);
76 void
77 initoptions(void)
79 char *opts;
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 */
89 if ((opts = getenv("HACKOPTIONS")) != NULL)
90 parseoptions(opts, TRUE);
93 static void
94 parseoptions(char *opts, boolean from_env)
96 char *op, *op2;
97 unsigned num;
98 boolean negated;
100 if ((op = strchr(opts, ',')) != NULL) {
101 *op++ = 0;
102 parseoptions(op, from_env);
104 if ((op = strchr(opts, ' ')) != NULL) {
105 op2 = op;
106 while (*op++)
107 if (*op != ' ')
108 *op2++ = *op;
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;
121 if (!strncmp(opts, "standout", 8)) {
122 flags.standout = !negated;
123 return;
125 if (!strncmp(opts, "null", 3)) {
126 flags.nonull = negated;
127 return;
129 if (!strncmp(opts, "tombstone", 4)) {
130 flags.notombstone = negated;
131 return;
133 if (!strncmp(opts, "news", 4)) {
134 flags.nonews = negated;
135 return;
137 if (!strncmp(opts, "time", 4)) {
138 flags.time = !negated;
139 flags.botl = 1;
140 return;
142 if (!strncmp(opts, "restonspace", 4)) {
143 flags.no_rest_on_space = negated;
144 return;
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;
153 if (!strncmp(opts, "male", 4)) {
154 flags.female = negated;
155 return;
157 if (!strncmp(opts, "female", 6)) {
158 flags.female = !negated;
159 return;
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;
167 op = strchr(opts, ':');
168 if (!op)
169 goto bad;
170 (void) strncpy(plname, op + 1, sizeof(plname) - 1);
171 return;
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++;
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;
202 while (letter(*++op));
203 if (*op == '/')
204 op++;
206 return;
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;
227 pline("Bad option: %s.", opts);
228 pline("Type `O help<cr>' for help.");
229 return;
231 puts("Bad syntax in HACKOPTIONS.");
232 puts("Use for example:");
233 puts(
234 "HACKOPTIONS=\"!restonspace,notombstone,endgame:own/5 topscorers/4 around me\""
236 getret();
240 doset(void)
242 char buf[BUFSZ];
243 size_t pos;
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;
274 pline(buf);
275 } else
276 parseoptions(buf, FALSE);
278 return (0);