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