1 /* $NetBSD: io.c,v 1.14 2009/08/31 08:27:16 dholland Exp $ */
4 * io.c - input/output routines for Phantasia
10 static void catchalarm(int) __dead2
;
13 * FUNCTION: read a string from operator
16 * char *cp - pointer to buffer area to fill
17 * int mx - maximum number of characters to put in buffer
19 * GLOBAL INPUTS: Echo, _iob[], Wizard, *stdscr
21 * GLOBAL OUTPUTS: _iob[]
24 * Read a string from the keyboard.
25 * This routine is specially designed to:
27 * - strip non-printing characters (unless Wizard)
29 * - redraw the screen if CH_REDRAW is entered
30 * - read in only 'mx - 1' characters or less characters
31 * - nul-terminate string, and throw away newline
33 * 'mx' is assumed to be at least 2.
37 getstring(char *cp
, int mx
)
39 char *inptr
; /* pointer into string for next string */
40 int x
, y
; /* original x, y coordinates on screen */
43 getyx(stdscr
, y
, x
); /* get coordinates on screen */
45 *inptr
= '\0'; /* clear string to start */
46 --mx
; /* reserve room in string for nul terminator */
49 /* get characters and process */
51 mvaddstr(y
, x
, cp
); /* print string on screen */
52 clrtoeol(); /* clear any data after string */
53 refresh(); /* update screen */
55 ch
= getchar(); /* get character */
58 case CH_ERASE
: /* back up one character */
63 case CH_KILL
: /* back up to original location */
67 case CH_NEWLINE
: /* terminate string */
70 case CH_REDRAW
: /* redraw screen */
71 clearok(stdscr
, TRUE
);
74 default: /* put data in string */
75 if (ch
>= ' ' || Wizard
)
76 /* printing char; put in string */
80 *inptr
= '\0'; /* terminate string */
81 } while (ch
!= CH_NEWLINE
&& inptr
< cp
+ mx
);
85 * FUNCTION: pause and prompt player
88 * int where - line on screen on which to pause
90 * GLOBAL INPUTS: *stdscr
93 * Print a message, and wait for a space character.
99 mvaddstr(where
, 0, "-- more --");
100 getanswer(" ", FALSE
);
104 * FUNCTION: input a floating point number from operator
106 * RETURN VALUE: floating point number from operator
108 * GLOBAL INPUTS: Databuf[]
111 * Read a string from player, and scan for a floating point
113 * If no valid number is found, return 0.0.
119 double result
; /* return value */
121 getstring(Databuf
, SZ_DATABUF
);
122 if (sscanf(Databuf
, "%lf", &result
) < 1)
123 /* no valid number entered */
130 * FUNCTION: input an option value from player
132 * GLOBAL INPUTS: Player
134 * GLOBAL OUTPUTS: Player
137 * Age increases with every move.
138 * Refresh screen, and get a single character option from player.
139 * Return a random value if player's ring has gone bad.
145 ++Player
.p_age
; /* increase age */
147 if (Player
.p_ring
.ring_type
!= R_SPOILED
)
149 return (getanswer("T ", TRUE
));
152 getanswer(" ", TRUE
);
153 return ((int)ROLL(0.0, 5.0) + '0');
158 * FUNCTION: handle interrupt from operator
160 * GLOBAL INPUTS: Player, *stdscr
163 * Allow player to quit upon hitting the interrupt key.
164 * If the player wants to quit while in battle, he/she automatically
171 char line
[81]; /* a place to store data already on screen */
172 int loop
; /* counter */
173 int x
, y
; /* coordinates on screen */
175 unsigned savealarm
; /* to save alarm value */
178 signal(SIGINT
, SIG_IGN
);
181 signal(SIGINT
, SIG_IGN
);
184 savealarm
= alarm(0); /* turn off any alarms */
186 getyx(stdscr
, y
, x
); /* save cursor location */
188 for (loop
= 0; loop
< 80; ++loop
) { /* save line on screen */
192 line
[80] = '\0'; /* nul terminate */
194 if (Player
.p_status
== S_INBATTLE
|| Player
.p_status
== S_MONSTER
) {
195 /* in midst of fighting */
196 mvaddstr(4, 0, "Quitting now will automatically kill your character. Still want to ? ");
197 ch
= getanswer("NY", FALSE
);
199 death("Bailing out");
202 mvaddstr(4, 0, "Do you really want to quit ? ");
203 ch
= getanswer("NY", FALSE
);
209 mvaddstr(4, 0, line
); /* restore data on screen */
210 move(y
, x
); /* restore cursor */
214 signal(SIGINT
, interrupt
);
217 signal(SIGINT
, interrupt
);
220 alarm(savealarm
); /* restore alarm */
224 * FUNCTION: get an answer from operator
227 * char *choices - string of (upper case) valid choices
228 * bool def - set if default answer
230 * GLOBAL INPUTS: catchalarm(), Echo, _iob[], _ctype[], *stdscr, Timeout,
233 * GLOBAL OUTPUTS: _iob[]
236 * Get a single character answer from operator.
237 * Timeout waiting for response. If we timeout, or the
238 * answer in not in the list of valid choices, print choices,
239 * and wait again, otherwise return the first character in ths
241 * Give up after 3 tries.
245 getanswer(const char *choices
, bool def
)
248 volatile int loop
; /* counter */
249 volatile int oldx
, oldy
; /* original coordinates on screen */
251 getyx(stdscr
, oldy
, oldx
);
252 alarm(0); /* make sure alarm is off */
254 for (loop
= 3; loop
; --loop
) {
255 /* try for 3 times */
256 if (setjmp(Timeoenv
) != 0) {
257 /* timed out waiting for response */
258 if (def
|| loop
<= 1)
259 /* return default answer */
262 /* prompt, and try again */
265 /* wait for response */
269 sigset(SIGALRM
, catchalarm
);
271 signal(SIGALRM
, catchalarm
);
275 alarm(7); /* short */
277 alarm(600); /* long */
281 alarm(0); /* turn off timeout */
284 /* caught some signal */
287 } else if (ch
== CH_REDRAW
) {
289 clearok(stdscr
, TRUE
); /* force clear screen */
290 ++loop
; /* don't count this input */
293 addch(ch
); /* echo character */
298 /* convert to upper case */
301 if (def
|| strchr(choices
, ch
) != NULL
)
304 else if (!def
&& loop
> 1) {
305 /* bad choice; prompt, and try again */
306 YELL
: mvprintw(oldy
+ 1, 0,
307 "Please choose one of : [%s]\n", choices
);
312 /* return default answer */
321 * FUNCTION: catch timer when waiting for input
323 * GLOBAL INPUTS: Timeoenv[]
326 * Come here when the alarm expires while waiting for input.
327 * Simply longjmp() into getanswer().
331 catchalarm(__unused
int sig
)
333 longjmp(Timeoenv
, 1);