xref: /netbsd/games/hack/hack.options.c (revision bf9ec67e)
1 /*	$NetBSD: hack.options.c,v 1.5 2001/02/05 00:37:43 christos Exp $	*/
2 
3 /*
4  * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
5  */
6 
7 #include <sys/cdefs.h>
8 #ifndef lint
9 __RCSID("$NetBSD: hack.options.c,v 1.5 2001/02/05 00:37:43 christos Exp $");
10 #endif				/* not lint */
11 
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include "hack.h"
15 #include "extern.h"
16 
17 void
18 initoptions()
19 {
20 	char           *opts;
21 
22 	flags.time = flags.nonews = flags.notombstone = flags.end_own =
23 		flags.standout = flags.nonull = FALSE;
24 	flags.no_rest_on_space = TRUE;
25 	flags.invlet_constant = TRUE;
26 	flags.end_top = 5;
27 	flags.end_around = 4;
28 	flags.female = FALSE;	/* players are usually male */
29 
30 	if ((opts = getenv("HACKOPTIONS")) != NULL)
31 		parseoptions(opts, TRUE);
32 }
33 
34 void
35 parseoptions(opts, from_env)
36 	char           *opts;
37 	boolean         from_env;
38 {
39 	char           *op, *op2;
40 	unsigned        num;
41 	boolean         negated;
42 
43 	if ((op = strchr(opts, ',')) != NULL) {
44 		*op++ = 0;
45 		parseoptions(op, from_env);
46 	}
47 	if ((op = strchr(opts, ' ')) != NULL) {
48 		op2 = op;
49 		while (*op++)
50 			if (*op != ' ')
51 				*op2++ = *op;
52 	}
53 	if (!*opts)
54 		return;
55 	negated = FALSE;
56 	while ((*opts == '!') || !strncmp(opts, "no", 2)) {
57 		if (*opts == '!')
58 			opts++;
59 		else
60 			opts += 2;
61 		negated = !negated;
62 	}
63 
64 	if (!strncmp(opts, "standout", 8)) {
65 		flags.standout = !negated;
66 		return;
67 	}
68 	if (!strncmp(opts, "null", 3)) {
69 		flags.nonull = negated;
70 		return;
71 	}
72 	if (!strncmp(opts, "tombstone", 4)) {
73 		flags.notombstone = negated;
74 		return;
75 	}
76 	if (!strncmp(opts, "news", 4)) {
77 		flags.nonews = negated;
78 		return;
79 	}
80 	if (!strncmp(opts, "time", 4)) {
81 		flags.time = !negated;
82 		flags.botl = 1;
83 		return;
84 	}
85 	if (!strncmp(opts, "restonspace", 4)) {
86 		flags.no_rest_on_space = negated;
87 		return;
88 	}
89 	if (!strncmp(opts, "fixinv", 4)) {
90 		if (from_env)
91 			flags.invlet_constant = !negated;
92 		else
93 			pline("The fixinvlet option must be in HACKOPTIONS.");
94 		return;
95 	}
96 	if (!strncmp(opts, "male", 4)) {
97 		flags.female = negated;
98 		return;
99 	}
100 	if (!strncmp(opts, "female", 6)) {
101 		flags.female = !negated;
102 		return;
103 	}
104 	/* name:string */
105 	if (!strncmp(opts, "name", 4)) {
106 		if (!from_env) {
107 			pline("The playername can be set only from HACKOPTIONS.");
108 			return;
109 		}
110 		op = strchr(opts, ':');
111 		if (!op)
112 			goto bad;
113 		(void) strncpy(plname, op + 1, sizeof(plname) - 1);
114 		return;
115 	}
116 	/* endgame:5t[op] 5a[round] o[wn] */
117 	if (!strncmp(opts, "endgame", 3)) {
118 		op = strchr(opts, ':');
119 		if (!op)
120 			goto bad;
121 		op++;
122 		while (*op) {
123 			num = 1;
124 			if (digit(*op)) {
125 				num = atoi(op);
126 				while (digit(*op))
127 					op++;
128 			} else if (*op == '!') {
129 				negated = !negated;
130 				op++;
131 			}
132 			switch (*op) {
133 			case 't':
134 				flags.end_top = num;
135 				break;
136 			case 'a':
137 				flags.end_around = num;
138 				break;
139 			case 'o':
140 				flags.end_own = !negated;
141 				break;
142 			default:
143 				goto bad;
144 			}
145 			while (letter(*++op));
146 			if (*op == '/')
147 				op++;
148 		}
149 		return;
150 	}
151 bad:
152 	if (!from_env) {
153 		if (!strncmp(opts, "help", 4)) {
154 			pline("%s%s%s",
155 			      "To set options use `HACKOPTIONS=\"<options>\"' in your environment, or ",
156 			      "give the command 'o' followed by the line `<options>' while playing. ",
157 			      "Here <options> is a list of <option>s separated by commas.");
158 			pline("%s%s%s",
159 			      "Simple (boolean) options are rest_on_space, news, time, ",
160 			      "null, tombstone, (fe)male. ",
161 			      "These can be negated by prefixing them with '!' or \"no\".");
162 			pline("%s",
163 			      "A string option is name, as in HACKOPTIONS=\"name:Merlin-W\".");
164 			pline("%s%s%s",
165 			      "A compound option is endgame; it is followed by a description of what ",
166 			      "parts of the scorelist you want to see. You might for example say: ",
167 			      "`endgame:own scores/5 top scores/4 around my score'.");
168 			return;
169 		}
170 		pline("Bad option: %s.", opts);
171 		pline("Type `o help<cr>' for help.");
172 		return;
173 	}
174 	puts("Bad syntax in HACKOPTIONS.");
175 	puts("Use for example:");
176 	puts(
177 	     "HACKOPTIONS=\"!restonspace,notombstone,endgame:own/5 topscorers/4 around me\""
178 		);
179 	getret();
180 }
181 
182 int
183 doset()
184 {
185 	char            buf[BUFSZ];
186 
187 	pline("What options do you want to set? ");
188 	getlin(buf);
189 	if (!buf[0] || buf[0] == '\033') {
190 		(void) strcpy(buf, "HACKOPTIONS=");
191 		(void) strcat(buf, flags.female ? "female," : "male,");
192 		if (flags.standout)
193 			(void) strcat(buf, "standout,");
194 		if (flags.nonull)
195 			(void) strcat(buf, "nonull,");
196 		if (flags.nonews)
197 			(void) strcat(buf, "nonews,");
198 		if (flags.time)
199 			(void) strcat(buf, "time,");
200 		if (flags.notombstone)
201 			(void) strcat(buf, "notombstone,");
202 		if (flags.no_rest_on_space)
203 			(void) strcat(buf, "!rest_on_space,");
204 		if (flags.end_top != 5 || flags.end_around != 4 || flags.end_own) {
205 			(void) sprintf(eos(buf), "endgame: %u topscores/%u around me",
206 				       flags.end_top, flags.end_around);
207 			if (flags.end_own)
208 				(void) strcat(buf, "/own scores");
209 		} else {
210 			char           *eop = eos(buf);
211 			if (*--eop == ',')
212 				*eop = 0;
213 		}
214 		pline(buf);
215 	} else
216 		parseoptions(buf, FALSE);
217 
218 	return (0);
219 }
220