1 /* $OpenBSD: hack.options.c,v 1.12 2016/01/09 18:33:15 mestre 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 <ctype.h> 65 #include <stdio.h> 66 #include <stdlib.h> 67 68 #include "hack.h" 69 70 static void parseoptions(char *, boolean); 71 72 void 73 initoptions(void) 74 { 75 char *opts; 76 77 flags.time = flags.nonews = flags.notombstone = flags.end_own = 78 flags.standout = flags.nonull = FALSE; 79 flags.no_rest_on_space = TRUE; 80 flags.invlet_constant = TRUE; 81 flags.end_top = 5; 82 flags.end_around = 4; 83 flags.female = FALSE; /* players are usually male */ 84 85 if ((opts = getenv("HACKOPTIONS"))) 86 parseoptions(opts,TRUE); 87 } 88 89 static void 90 parseoptions(char *opts, boolean from_env) 91 { 92 char *op,*op2; 93 unsigned num; 94 boolean negated; 95 96 if ((op = strchr(opts, ','))) { 97 *op++ = 0; 98 parseoptions(op, from_env); 99 } 100 if ((op = strchr(opts, ' '))) { 101 op2 = op; 102 while(*op++) 103 if(*op != ' ') *op2++ = *op; 104 } 105 if (!*opts) 106 return; 107 negated = FALSE; 108 while((*opts == '!') || !strncmp(opts, "no", 2)) { 109 if(*opts == '!') opts++; else opts += 2; 110 negated = !negated; 111 } 112 113 if(!strncmp(opts,"standout",8)) { 114 flags.standout = !negated; 115 return; 116 } 117 118 if(!strncmp(opts,"null",3)) { 119 flags.nonull = negated; 120 return; 121 } 122 123 if(!strncmp(opts,"tombstone",4)) { 124 flags.notombstone = negated; 125 return; 126 } 127 128 if(!strncmp(opts,"news",4)) { 129 flags.nonews = negated; 130 return; 131 } 132 133 if(!strncmp(opts,"time",4)) { 134 flags.time = !negated; 135 flags.botl = 1; 136 return; 137 } 138 139 if(!strncmp(opts,"restonspace",4)) { 140 flags.no_rest_on_space = negated; 141 return; 142 } 143 144 if(!strncmp(opts,"fixinv",4)) { 145 if(from_env) 146 flags.invlet_constant = !negated; 147 else 148 pline("The fixinvlet option must be in HACKOPTIONS."); 149 return; 150 } 151 152 if(!strncmp(opts,"male",4)) { 153 flags.female = negated; 154 return; 155 } 156 if(!strncmp(opts,"female",6)) { 157 flags.female = !negated; 158 return; 159 } 160 161 /* name:string */ 162 if(!strncmp(opts,"name",4)) { 163 extern char plname[PL_NSIZ]; 164 if(!from_env) { 165 pline("The playername can be set only from HACKOPTIONS."); 166 return; 167 } 168 op = strchr(opts,':'); 169 if(!op) goto bad; 170 (void) strlcpy(plname, op+1, sizeof(plname)); 171 return; 172 } 173 174 /* endgame:5t[op] 5a[round] o[wn] */ 175 if(!strncmp(opts,"endgame",3)) { 176 op = strchr(opts,':'); 177 if(!op) goto bad; 178 op++; 179 while(*op) { 180 num = 1; 181 if(isdigit((unsigned char)*op)) { 182 num = atoi(op); 183 while(isdigit((unsigned char)*op)) op++; 184 } else 185 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 == '/') op++; 204 } 205 return; 206 } 207 bad: 208 if(!from_env) { 209 if(!strncmp(opts, "help", 4)) { 210 pline("%s%s%s", 211 "To set options use `HACKOPTIONS=\"<options>\"' in your environment, or ", 212 "give the command 'O' followed by the line `<options>' while playing. ", 213 "Here <options> is a list of <option>s separated by commas." ); 214 pline("%s%s%s", 215 "Simple (boolean) options are rest_on_space, news, time, ", 216 "null, tombstone, (fe)male. ", 217 "These can be negated by prefixing them with '!' or \"no\"." ); 218 pline("%s", 219 "A string option is name, as in HACKOPTIONS=\"name:Merlin-W\"." ); 220 pline("%s%s%s", 221 "A compound option is endgame; it is followed by a description of what ", 222 "parts of the scorelist you want to see. You might for example say: ", 223 "`endgame:own scores/5 top scores/4 around my score'." ); 224 return; 225 } 226 pline("Bad option: %s.", opts); 227 pline("Type `O help<cr>' for help."); 228 return; 229 } 230 puts("Bad syntax in HACKOPTIONS."); 231 puts("Use for example:"); 232 puts( 233 "HACKOPTIONS=\"!restonspace,notombstone,endgame:own/5 topscorers/4 around me\"" 234 ); 235 getret(); 236 } 237 238 int 239 doset(void) 240 { 241 char buf[BUFSZ]; 242 243 pline("What options do you want to set? "); 244 getlin(buf); 245 if(!buf[0] || buf[0] == '\033') { 246 (void) strlcpy(buf,"HACKOPTIONS=", sizeof buf); 247 (void) strlcat(buf, flags.female ? "female," : "male,", sizeof buf); 248 if(flags.standout) (void) strlcat(buf,"standout,", sizeof buf); 249 if(flags.nonull) (void) strlcat(buf,"nonull,", sizeof buf); 250 if(flags.nonews) (void) strlcat(buf,"nonews,", sizeof buf); 251 if(flags.time) (void) strlcat(buf,"time,", sizeof buf); 252 if(flags.notombstone) (void) strlcat(buf,"notombstone,", sizeof buf); 253 if(flags.no_rest_on_space) 254 (void) strlcat(buf,"!rest_on_space,", sizeof buf); 255 if(flags.end_top != 5 || flags.end_around != 4 || flags.end_own){ 256 (void) snprintf(eos(buf), buf + sizeof buf - eos(buf), 257 "endgame: %u topscores/%u around me", 258 flags.end_top, flags.end_around); 259 if(flags.end_own) (void) strlcat(buf, "/own scores", sizeof buf); 260 } else { 261 char *eop = eos(buf); 262 if(*--eop == ',') *eop = 0; 263 } 264 pline("%s", buf); 265 } else 266 parseoptions(buf, FALSE); 267 268 return(0); 269 } 270