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