1 /* $OpenBSD: hack.options.c,v 1.4 2001/01/28 23:41:45 niklas Exp $ */ 2 3 /* 4 * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. 5 */ 6 7 #ifndef lint 8 static char rcsid[] = "$OpenBSD: hack.options.c,v 1.4 2001/01/28 23:41:45 niklas Exp $"; 9 #endif /* not lint */ 10 11 #include "config.h" 12 #include "hack.h" 13 extern char *eos(); 14 15 initoptions() 16 { 17 register char *opts; 18 extern char *getenv(); 19 20 flags.time = flags.nonews = flags.notombstone = flags.end_own = 21 flags.standout = flags.nonull = FALSE; 22 flags.no_rest_on_space = TRUE; 23 flags.invlet_constant = TRUE; 24 flags.end_top = 5; 25 flags.end_around = 4; 26 flags.female = FALSE; /* players are usually male */ 27 28 if(opts = getenv("HACKOPTIONS")) 29 parseoptions(opts,TRUE); 30 } 31 32 parseoptions(opts, from_env) 33 register char *opts; 34 boolean from_env; 35 { 36 register char *op,*op2; 37 unsigned num; 38 boolean negated; 39 40 if(op = strchr(opts, ',')) { 41 *op++ = 0; 42 parseoptions(op, from_env); 43 } 44 if(op = strchr(opts, ' ')) { 45 op2 = op; 46 while(*op++) 47 if(*op != ' ') *op2++ = *op; 48 } 49 if(!*opts) return; 50 negated = FALSE; 51 while((*opts == '!') || !strncmp(opts, "no", 2)) { 52 if(*opts == '!') opts++; else opts += 2; 53 negated = !negated; 54 } 55 56 if(!strncmp(opts,"standout",8)) { 57 flags.standout = !negated; 58 return; 59 } 60 61 if(!strncmp(opts,"null",3)) { 62 flags.nonull = negated; 63 return; 64 } 65 66 if(!strncmp(opts,"tombstone",4)) { 67 flags.notombstone = negated; 68 return; 69 } 70 71 if(!strncmp(opts,"news",4)) { 72 flags.nonews = negated; 73 return; 74 } 75 76 if(!strncmp(opts,"time",4)) { 77 flags.time = !negated; 78 flags.botl = 1; 79 return; 80 } 81 82 if(!strncmp(opts,"restonspace",4)) { 83 flags.no_rest_on_space = negated; 84 return; 85 } 86 87 if(!strncmp(opts,"fixinv",4)) { 88 if(from_env) 89 flags.invlet_constant = !negated; 90 else 91 pline("The fixinvlet option must be in HACKOPTIONS."); 92 return; 93 } 94 95 if(!strncmp(opts,"male",4)) { 96 flags.female = negated; 97 return; 98 } 99 if(!strncmp(opts,"female",6)) { 100 flags.female = !negated; 101 return; 102 } 103 104 /* name:string */ 105 if(!strncmp(opts,"name",4)) { 106 extern char plname[PL_NSIZ]; 107 if(!from_env) { 108 pline("The playername can be set only from HACKOPTIONS."); 109 return; 110 } 111 op = strchr(opts,':'); 112 if(!op) goto bad; 113 (void) strncpy(plname, op+1, sizeof(plname)-1); 114 plname[sizeof(plname)-1] = '\0'; 115 return; 116 } 117 118 /* endgame:5t[op] 5a[round] o[wn] */ 119 if(!strncmp(opts,"endgame",3)) { 120 op = strchr(opts,':'); 121 if(!op) goto bad; 122 op++; 123 while(*op) { 124 num = 1; 125 if(digit(*op)) { 126 num = atoi(op); 127 while(digit(*op)) op++; 128 } else 129 if(*op == '!') { 130 negated = !negated; 131 op++; 132 } 133 switch(*op) { 134 case 't': 135 flags.end_top = num; 136 break; 137 case 'a': 138 flags.end_around = num; 139 break; 140 case 'o': 141 flags.end_own = !negated; 142 break; 143 default: 144 goto bad; 145 } 146 while(letter(*++op)) ; 147 if(*op == '/') 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 doset() 183 { 184 char buf[BUFSZ]; 185 186 pline("What options do you want to set? "); 187 getlin(buf); 188 if(!buf[0] || buf[0] == '\033') { 189 (void) strcpy(buf,"HACKOPTIONS="); 190 (void) strcat(buf, flags.female ? "female," : "male,"); 191 if(flags.standout) (void) strcat(buf,"standout,"); 192 if(flags.nonull) (void) strcat(buf,"nonull,"); 193 if(flags.nonews) (void) strcat(buf,"nonews,"); 194 if(flags.time) (void) strcat(buf,"time,"); 195 if(flags.notombstone) (void) strcat(buf,"notombstone,"); 196 if(flags.no_rest_on_space) 197 (void) strcat(buf,"!rest_on_space,"); 198 if(flags.end_top != 5 || flags.end_around != 4 || flags.end_own){ 199 (void) sprintf(eos(buf), "endgame: %u topscores/%u around me", 200 flags.end_top, flags.end_around); 201 if(flags.end_own) (void) strcat(buf, "/own scores"); 202 } else { 203 register char *eop = eos(buf); 204 if(*--eop == ',') *eop = 0; 205 } 206 pline(buf); 207 } else 208 parseoptions(buf, FALSE); 209 210 return(0); 211 } 212