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