Sync usage with man page.
[netbsd-mini2440.git] / games / phantasia / io.c
blob0709d9a7514ac977059a4edbc93285a0be1f8283
1 /* $NetBSD: io.c,v 1.13 2009/08/12 08:21:41 dholland Exp $ */
3 /*
4 * io.c - input/output routines for Phantasia
5 */
7 #include <sys/cdefs.h>
9 #include <ctype.h>
10 #include <math.h>
11 #include <setjmp.h>
12 #include <signal.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <unistd.h>
17 #include "macros.h"
18 #include "phantdefs.h"
19 #include "phantstruct.h"
20 #include "phantglobs.h"
21 //#include "pathnames.h"
23 #undef bool
24 #include <curses.h>
26 static void catchalarm(int) __dead;
28 void
29 getstring(char *cp, int mx)
31 char *inptr; /* pointer into string for next string */
32 int x, y; /* original x, y coordinates on screen */
33 int ch; /* input */
35 getyx(stdscr, y, x); /* get coordinates on screen */
36 inptr = cp;
37 *inptr = '\0'; /* clear string to start */
38 --mx; /* reserve room in string for nul terminator */
41 /* get characters and process */
43 if (Echo)
44 mvaddstr(y, x, cp); /* print string on screen */
45 clrtoeol(); /* clear any data after string */
46 refresh(); /* update screen */
48 ch = getchar(); /* get character */
50 switch (ch) {
51 case CH_ERASE: /* back up one character */
52 if (inptr > cp)
53 --inptr;
54 break;
56 case CH_KILL: /* back up to original location */
57 inptr = cp;
58 break;
60 case CH_NEWLINE: /* terminate string */
61 break;
63 case CH_REDRAW:/* redraw screen */
64 clearok(stdscr, TRUE);
65 continue;
67 default: /* put data in string */
68 if (ch >= ' ' || Wizard)
69 /* printing char; put in string */
70 *inptr++ = ch;
73 *inptr = '\0'; /* terminate string */
75 while (ch != CH_NEWLINE && inptr < cp + mx);
78 void
79 more(int where)
81 mvaddstr(where, 0, "-- more --");
82 getanswer(" ", FALSE);
85 double
86 infloat(void)
88 double result; /* return value */
90 getstring(Databuf, SZ_DATABUF);
91 if (sscanf(Databuf, "%lf", &result) < 1)
92 /* no valid number entered */
93 result = 0.0;
95 return (result);
98 int
99 inputoption(void)
101 ++Player.p_age; /* increase age */
103 if (Player.p_ring.ring_type != R_SPOILED)
104 /* ring ok */
105 return (getanswer("T ", TRUE));
106 else
107 /* bad ring */
109 getanswer(" ", TRUE);
110 return ((int) ROLL(0.0, 5.0) + '0');
114 void
115 interrupt(void)
117 char line[81]; /* a place to store data already on screen */
118 int loop; /* counter */
119 int x, y; /* coordinates on screen */
120 int ch; /* input */
121 unsigned savealarm; /* to save alarm value */
123 #ifdef SYS3
124 signal(SIGINT, SIG_IGN);
125 #endif
126 #ifdef SYS5
127 signal(SIGINT, SIG_IGN);
128 #endif
130 savealarm = alarm(0); /* turn off any alarms */
132 getyx(stdscr, y, x); /* save cursor location */
134 for (loop = 0; loop < 80; ++loop) { /* save line on screen */
135 move(4, loop);
136 line[loop] = inch();
138 line[80] = '\0'; /* nul terminate */
140 if (Player.p_status == S_INBATTLE || Player.p_status == S_MONSTER)
141 /* in midst of fighting */
143 mvaddstr(4, 0, "Quitting now will automatically kill your character. Still want to ? ");
144 ch = getanswer("NY", FALSE);
145 if (ch == 'Y')
146 death("Bailing out");
147 /* NOTREACHED */
148 } else {
149 mvaddstr(4, 0, "Do you really want to quit ? ");
150 ch = getanswer("NY", FALSE);
151 if (ch == 'Y')
152 leavegame();
153 /* NOTREACHED */
156 mvaddstr(4, 0, line); /* restore data on screen */
157 move(y, x); /* restore cursor */
158 refresh();
160 #ifdef SYS3
161 signal(SIGINT, interrupt);
162 #endif
163 #ifdef SYS5
164 signal(SIGINT, interrupt);
165 #endif
167 alarm(savealarm); /* restore alarm */
171 getanswer(const char *choices, phbool def)
173 int ch; /* input */
174 volatile int loop; /* counter */
175 volatile int oldx, oldy; /* original coordinates on screen */
177 getyx(stdscr, oldy, oldx);
178 alarm(0); /* make sure alarm is off */
180 for (loop = 3; loop; --loop)
181 /* try for 3 times */
183 if (setjmp(Timeoenv) != 0)
184 /* timed out waiting for response */
186 if (def || loop <= 1)
187 /* return default answer */
188 break;
189 else
190 /* prompt, and try again */
191 goto YELL;
192 } else
193 /* wait for response */
195 clrtoeol();
196 refresh();
197 #ifdef BSD41
198 sigset(SIGALRM, catchalarm);
199 #else
200 signal(SIGALRM, catchalarm);
201 #endif
202 /* set timeout */
203 if (Timeout)
204 alarm(7); /* short */
205 else
206 alarm(600); /* long */
208 ch = getchar();
210 alarm(0); /* turn off timeout */
212 if (ch < 0)
213 /* caught some signal */
215 ++loop;
216 continue;
217 } else
218 if (ch == CH_REDRAW)
219 /* redraw screen */
221 clearok(stdscr, TRUE); /* force clear screen */
222 ++loop; /* don't count this input */
223 continue;
224 } else
225 if (Echo) {
226 addch(ch); /* echo character */
227 refresh();
229 if (islower(ch))
230 /* convert to upper case */
231 ch = toupper(ch);
233 if (def || strchr(choices, ch) != NULL)
234 /* valid choice */
235 return (ch);
236 else
237 if (!def && loop > 1)
238 /* bad choice; prompt, and try again */
240 YELL: mvprintw(oldy + 1, 0, "Please choose one of : [%s]\n", choices);
241 move(oldy, oldx);
242 clrtoeol();
243 continue;
244 } else
245 /* return default answer */
246 break;
250 return (*choices);
253 static void
254 catchalarm(int dummy __unused)
256 longjmp(Timeoenv, 1);