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