1 /* $NetBSD: hack.options.c,v 1.7 2004/01/01 16:02:51 jsm 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.7 2004/01/01 16:02:51 jsm Exp $"); 67 #endif /* not lint */ 68 69 #include <stdlib.h> 70 #include <unistd.h> 71 #include "hack.h" 72 #include "extern.h" 73 74 void 75 initoptions() 76 { 77 char *opts; 78 79 flags.time = flags.nonews = flags.notombstone = flags.end_own = 80 flags.standout = flags.nonull = FALSE; 81 flags.no_rest_on_space = TRUE; 82 flags.invlet_constant = TRUE; 83 flags.end_top = 5; 84 flags.end_around = 4; 85 flags.female = FALSE; /* players are usually male */ 86 87 if ((opts = getenv("HACKOPTIONS")) != NULL) 88 parseoptions(opts, TRUE); 89 } 90 91 void 92 parseoptions(opts, from_env) 93 char *opts; 94 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() 241 { 242 char buf[BUFSZ]; 243 244 pline("What options do you want to set? "); 245 getlin(buf); 246 if (!buf[0] || buf[0] == '\033') { 247 (void) strcpy(buf, "HACKOPTIONS="); 248 (void) strcat(buf, flags.female ? "female," : "male,"); 249 if (flags.standout) 250 (void) strcat(buf, "standout,"); 251 if (flags.nonull) 252 (void) strcat(buf, "nonull,"); 253 if (flags.nonews) 254 (void) strcat(buf, "nonews,"); 255 if (flags.time) 256 (void) strcat(buf, "time,"); 257 if (flags.notombstone) 258 (void) strcat(buf, "notombstone,"); 259 if (flags.no_rest_on_space) 260 (void) strcat(buf, "!rest_on_space,"); 261 if (flags.end_top != 5 || flags.end_around != 4 || flags.end_own) { 262 (void) sprintf(eos(buf), "endgame: %u topscores/%u around me", 263 flags.end_top, flags.end_around); 264 if (flags.end_own) 265 (void) strcat(buf, "/own scores"); 266 } else { 267 char *eop = eos(buf); 268 if (*--eop == ',') 269 *eop = 0; 270 } 271 pline(buf); 272 } else 273 parseoptions(buf, FALSE); 274 275 return (0); 276 } 277