comparison srogue/options.c @ 36:2128c7dc8a40

Import Super-Rogue 9.0 from the Roguelike Restoration Project (r1490)
author elwin
date Thu, 25 Nov 2010 12:21:41 +0000
parents
children 34d7a614855e
comparison
equal deleted inserted replaced
35:05018c63a721 36:2128c7dc8a40
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 <termios.h>
18 #include <ctype.h>
19 #include "rogue.h"
20 #include "rogue.ext"
21
22 extern struct termios terminal;
23
24 /*
25 * description of an option and what to do with it
26 */
27 struct optstruct {
28 char *o_name; /* option name */
29 char *o_prompt; /* prompt for interactive entry */
30 char *o_opt; /* pointer to thing to set */
31 };
32
33 typedef struct optstruct OPTION;
34
35 int put_str(), get_str();
36
37 OPTION optlist[] = {
38 { "name", "Name: ", whoami },
39 { "fruit", "Fruit: ", fruit },
40 { "file", "Save file: ", file_name }
41 };
42 #define NUM_OPTS (sizeof optlist / sizeof (OPTION))
43
44 /*
45 * print and then set options from the terminal
46 */
47 option()
48 {
49 reg OPTION *op;
50 reg int wh;
51
52 wclear(hw);
53 touchwin(hw);
54 /*
55 * Display current values of options
56 */
57 for (op = optlist; op < &optlist[NUM_OPTS]; op++) {
58 wh = op - optlist;
59 mvwaddstr(hw, wh, 0, op->o_prompt);
60 mvwaddstr(hw, wh, 16, op->o_opt);
61 }
62 /*
63 * Set values
64 */
65 wmove(hw, 0, 0);
66 for (op = optlist; op < &optlist[NUM_OPTS]; op++) {
67 wmove(hw, op - optlist, 16);
68 if ((wh = get_str(op->o_opt, hw))) {
69 if (wh == QUIT)
70 break;
71 else if (op > optlist) {
72 wmove(hw, op - optlist, 0);
73 op -= 2;
74 }
75 else {
76 putchar(7);
77 wmove(hw, 0, 0);
78 op -= 1;
79 }
80 }
81 }
82 /*
83 * Switch back to original screen
84 */
85 dbotline(hw,spacemsg);
86 restscr(cw);
87 after = FALSE;
88 }
89
90
91 /*
92 * get_str:
93 * Set a string option
94 */
95 #define CTRLB 2
96 get_str(opt, awin)
97 char *opt;
98 WINDOW *awin;
99 {
100 reg char *sp;
101 reg int c, oy, ox;
102 char buf[LINLEN];
103
104 draw(awin);
105 getyx(awin, oy, ox);
106 /*
107 * loop reading in the string, and put it in a temporary buffer
108 */
109 for (sp = buf; (c=wgetch(awin)) != '\n' && c != '\r' && c != ESCAPE;
110 wclrtoeol(awin), draw(awin)) {
111 if (( (int)sp - (int)buf ) >= 50) {
112 *sp = '\0'; /* line was too long */
113 strucpy(opt,buf,strlen(buf));
114 mvwaddstr(awin, 0, 0, "Name was truncated --More--");
115 wclrtoeol(awin);
116 draw(awin);
117 wait_for(awin, ' ');
118 mvwprintw(awin, 0, 0, "Called: %s",opt);
119 draw(awin);
120 return NORM;
121 }
122 if (c == -1)
123 continue;
124 else if(c == terminal.c_cc[VERASE]) { /* process erase char */
125 if (sp > buf) {
126 reg int i;
127
128 sp--;
129 for (i = strlen(unctrl(*sp)); i; i--)
130 waddch(awin, '\b');
131 }
132 continue;
133 }
134 else if (c == terminal.c_cc[VKILL]) { /* process kill character */
135 sp = buf;
136 wmove(awin, oy, ox);
137 continue;
138 }
139 else if (sp == buf) {
140 if (c == CTRLB) /* CTRL - B */
141 break;
142 if (c == '~') {
143 strcpy(buf, home);
144 waddstr(awin, home);
145 sp += strlen(home);
146 continue;
147 }
148 }
149 *sp++ = c;
150 waddstr(awin, unctrl(c));
151 }
152 *sp = '\0';
153 if (sp > buf) /* only change option if something was typed */
154 strucpy(opt, buf, strlen(buf));
155 wmove(awin, oy, ox);
156 waddstr(awin, opt);
157 waddstr(awin, "\n\r");
158 draw(awin);
159 if (awin == cw)
160 mpos += sp - buf;
161 if (c == CTRLB)
162 return MINUS;
163 if (c == ESCAPE)
164 return QUIT;
165 return NORM;
166 }
167
168 /*
169 * parse_opts:
170 * Parse options from string, usually taken from the environment.
171 * the string is a series of comma seperated values, with strings
172 * being "name=....", with the string being defined up to a comma
173 * or the end of the entire option string.
174 */
175
176 parse_opts(str)
177 char *str;
178 {
179 reg char *sp;
180 reg OPTION *op;
181 reg int len;
182
183 while (*str) {
184 for (sp = str; isalpha(*sp); sp++) /* get option name */
185 continue;
186 len = sp - str;
187 for (op = optlist; op < &optlist[NUM_OPTS]; op++) {
188 if (EQSTR(str, op->o_name, len)) {
189 reg char *start;
190
191 for (str = sp + 1; *str == '='; str++)
192 continue;
193 if (*str == '~') {
194 strcpy(op->o_opt, home);
195 start = op->o_opt + strlen(home);
196 while (*++str == '/')
197 continue;
198 }
199 else
200 start = (char *) op->o_opt;
201 for (sp = str + 1; *sp && *sp != ','; sp++)
202 continue;
203 strucpy(start, str, sp - str);
204 }
205 }
206 /*
207 * skip to start of next option name
208 */
209 while (*sp && !isalpha(*sp))
210 sp++;
211 str = sp;
212 }
213 }
214
215 /*
216 * copy string using unctrl for things
217 */
218 strucpy(s1, s2, len)
219 char *s1, *s2;
220 int len;
221 {
222 reg char *sp;
223
224 while (len-- > 0) {
225 strcpy(s1, (sp = unctrl(*s2)));
226 s1 += strlen(sp);
227 s2++;
228 }
229 *s1 = '\0';
230 }