Annotation of early-roguelike/srogue/options.c, Revision 1.1
1.1 ! rubenllo 1: /*
! 2: * This file has all the code for the option command.
! 3: *
! 4: * @(#)options.c 9.0 (rdk) 7/17/84
! 5: *
! 6: * Super-Rogue
! 7: * Copyright (C) 1984 Robert D. Kindelberger
! 8: * All rights reserved.
! 9: *
! 10: * Based on "Rogue: Exploring the Dungeons of Doom"
! 11: * Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
! 12: * All rights reserved.
! 13: *
! 14: * See the file LICENSE.TXT for full copyright and licensing information.
! 15: */
! 16:
! 17: #include <ctype.h>
! 18: #include <string.h>
! 19: #include "rogue.h"
! 20: #include "rogue.ext"
! 21:
! 22: /*
! 23: * description of an option and what to do with it
! 24: */
! 25: struct optstruct {
! 26: char *o_name; /* option name */
! 27: char *o_prompt; /* prompt for interactive entry */
! 28: char *o_opt; /* pointer to thing to set */
! 29: };
! 30:
! 31: typedef struct optstruct OPTION;
! 32:
! 33: int allowchange(OPTION *opt);
! 34:
! 35: OPTION optlist[] = {
! 36: { "name", "Name: ", whoami },
! 37: { "fruit", "Fruit: ", fruit },
! 38: { "file", "Save file: ", file_name }
! 39: };
! 40: #define NUM_OPTS (sizeof optlist / sizeof (OPTION))
! 41: OPTION safeoptlist[] = {
! 42: { "fruit", "Fruit: ", fruit },
! 43: };
! 44: #define NUM_SOPTS (sizeof safeoptlist / sizeof (OPTION))
! 45:
! 46: /*
! 47: * print and then set options from the terminal
! 48: */
! 49: void
! 50: option(void)
! 51: {
! 52: reg OPTION *op;
! 53: reg int wh;
! 54: OPTION *olist;
! 55: int olen;
! 56:
! 57: if (use_savedir) {
! 58: olist = safeoptlist;
! 59: olen = NUM_SOPTS;
! 60: }
! 61: else {
! 62: olist = optlist;
! 63: olen = NUM_OPTS;
! 64: }
! 65:
! 66: wclear(hw);
! 67: touchwin(hw);
! 68: /*
! 69: * Display current values of options
! 70: */
! 71: for (op = olist; op < &olist[olen]; op++) {
! 72: wh = op - olist;
! 73: mvwaddstr(hw, wh, 0, op->o_prompt);
! 74: mvwaddstr(hw, wh, 16, op->o_opt);
! 75: }
! 76: /*
! 77: * Set values
! 78: */
! 79: wmove(hw, 0, 0);
! 80: for (op = olist; op < &olist[olen]; op++) {
! 81: wmove(hw, op - olist, 16);
! 82: if ((wh = get_str(op->o_opt, hw))) {
! 83: if (wh == QUIT)
! 84: break;
! 85: else if (op > olist) {
! 86: wmove(hw, op - olist, 0);
! 87: op -= 2;
! 88: }
! 89: else {
! 90: putchar(7);
! 91: wmove(hw, 0, 0);
! 92: op -= 1;
! 93: }
! 94: }
! 95: }
! 96: /*
! 97: * Switch back to original screen
! 98: */
! 99: dbotline(hw,spacemsg);
! 100: restscr(cw);
! 101: after = FALSE;
! 102: }
! 103:
! 104:
! 105: /*
! 106: * get_str:
! 107: * Set a string option
! 108: */
! 109: #define CTRLB 2
! 110: int
! 111: get_str(char *opt, WINDOW *awin)
! 112: {
! 113: reg char *sp;
! 114: reg int c, oy, ox;
! 115: char buf[LINLEN];
! 116:
! 117: draw(awin);
! 118: getyx(awin, oy, ox);
! 119: /*
! 120: * loop reading in the string, and put it in a temporary buffer
! 121: */
! 122: for (sp = buf; (c=readchar()) != '\n' && c != '\r' && c != ESCAPE;
! 123: wclrtoeol(awin), draw(awin)) {
! 124: if (sp - buf >= 50) {
! 125: *sp = '\0'; /* line was too long */
! 126: strucpy(opt,buf,strlen(buf));
! 127: mvwaddstr(awin, 0, 0, "Name was truncated --More--");
! 128: wclrtoeol(awin);
! 129: draw(awin);
! 130: wait_for(awin, ' ');
! 131: mvwprintw(awin, 0, 0, "Called: %s",opt);
! 132: draw(awin);
! 133: return NORM;
! 134: }
! 135: if (c == -1)
! 136: continue;
! 137: else if(c == md_erasechar()) { /* process erase char */
! 138: if (sp > buf) {
! 139: reg int i;
! 140:
! 141: sp--;
! 142: for (i = strlen(unctrl(*sp)); i; i--)
! 143: waddch(awin, '\b');
! 144: }
! 145: continue;
! 146: }
! 147: else if (c == md_killchar()) { /* process kill character */
! 148: sp = buf;
! 149: wmove(awin, oy, ox);
! 150: continue;
! 151: }
! 152: else if (sp == buf) {
! 153: if (c == CTRLB) /* CTRL - B */
! 154: break;
! 155: if (c == '~') {
! 156: strcpy(buf, home);
! 157: waddstr(awin, home);
! 158: sp += strlen(home);
! 159: continue;
! 160: }
! 161: }
! 162: *sp++ = c;
! 163: waddstr(awin, unctrl(c));
! 164: }
! 165: *sp = '\0';
! 166: if (sp > buf) /* only change option if something was typed */
! 167: strucpy(opt, buf, strlen(buf));
! 168: wmove(awin, oy, ox);
! 169: waddstr(awin, opt);
! 170: waddstr(awin, "\n\r");
! 171: draw(awin);
! 172: if (awin == cw)
! 173: mpos += sp - buf;
! 174: if (c == CTRLB)
! 175: return MINUS;
! 176: if (c == ESCAPE)
! 177: return QUIT;
! 178: return NORM;
! 179: }
! 180:
! 181: /*
! 182: * parse_opts:
! 183: * Parse options from string, usually taken from the environment.
! 184: * the string is a series of comma seperated values, with strings
! 185: * being "name=....", with the string being defined up to a comma
! 186: * or the end of the entire option string.
! 187: */
! 188:
! 189: void
! 190: parse_opts(char *str)
! 191: {
! 192: reg char *sp;
! 193: reg OPTION *op;
! 194: reg int len;
! 195:
! 196: while (*str) {
! 197: for (sp = str; isalpha(*sp); sp++) /* get option name */
! 198: continue;
! 199: len = sp - str;
! 200: for (op = optlist; op < &optlist[NUM_OPTS]; op++) {
! 201: /* For security, some options can't be changed. */
! 202: if (!allowchange(op))
! 203: continue;
! 204: if (EQSTR(str, op->o_name, len)) {
! 205: reg char *start;
! 206:
! 207: for (str = sp + 1; *str == '='; str++)
! 208: continue;
! 209: if (*str == '~') {
! 210: strcpy(op->o_opt, home);
! 211: start = op->o_opt + strlen(home);
! 212: while (*++str == '/')
! 213: continue;
! 214: }
! 215: else
! 216: start = (char *) op->o_opt;
! 217: for (sp = str + 1; *sp && *sp != ','; sp++)
! 218: continue;
! 219: strucpy(start, str, sp - str);
! 220: }
! 221: }
! 222: /*
! 223: * skip to start of next option name
! 224: */
! 225: while (*sp && !isalpha(*sp))
! 226: sp++;
! 227: str = sp;
! 228: }
! 229: }
! 230:
! 231: /*
! 232: * copy string using unctrl for things
! 233: */
! 234: void
! 235: strucpy(char *s1, char *s2, int len)
! 236: {
! 237: const char *sp;
! 238:
! 239: while (len-- > 0) {
! 240: strcpy(s1, (sp = unctrl(*s2)));
! 241: s1 += strlen(sp);
! 242: s2++;
! 243: }
! 244: *s1 = '\0';
! 245: }
! 246:
! 247: int allowchange(OPTION *opt)
! 248: {
! 249: if (!use_savedir)
! 250: return 1;
! 251: if (!strcmp(opt->o_name, "name"))
! 252: return 0;
! 253: if (!strcmp(opt->o_name, "file"))
! 254: return 0;
! 255: return 1;
! 256: }
CVSweb