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