1 /* $NetBSD: io.c,v 1.10 2007/12/15 16:32:05 perry Exp $ */
4 * io.c - input/output routines for Phantasia
17 char *inptr
; /* pointer into string for next string */
18 int x
, y
; /* original x, y coordinates on screen */
21 getyx(stdscr
, y
, x
); /* get coordinates on screen */
23 *inptr
= '\0'; /* clear string to start */
24 --mx
; /* reserve room in string for nul terminator */
27 /* get characters and process */
30 mvaddstr(y
, x
, cp
); /* print string on screen */
31 clrtoeol(); /* clear any data after string */
32 refresh(); /* update screen */
34 ch
= getchar(); /* get character */
37 case CH_ERASE
: /* back up one character */
42 case CH_KILL
: /* back up to original location */
46 case CH_NEWLINE
: /* terminate string */
49 case CH_REDRAW
:/* redraw screen */
50 clearok(stdscr
, TRUE
);
53 default: /* put data in string */
54 if (ch
>= ' ' || Wizard
)
55 /* printing char; put in string */
59 *inptr
= '\0'; /* terminate string */
61 while (ch
!= CH_NEWLINE
&& inptr
< cp
+ mx
);
68 mvaddstr(where
, 0, "-- more --");
69 getanswer(" ", FALSE
);
75 double result
; /* return value */
77 getstring(Databuf
, SZ_DATABUF
);
78 if (sscanf(Databuf
, "%lf", &result
) < 1)
79 /* no valid number entered */
88 ++Player
.p_age
; /* increase age */
90 if (Player
.p_ring
.ring_type
!= R_SPOILED
)
92 return (getanswer("T ", TRUE
));
97 return ((int) ROLL(0.0, 5.0) + '0');
104 char line
[81]; /* a place to store data already on screen */
105 int loop
; /* counter */
106 int x
, y
; /* coordinates on screen */
108 unsigned savealarm
; /* to save alarm value */
111 signal(SIGINT
, SIG_IGN
);
114 signal(SIGINT
, SIG_IGN
);
117 savealarm
= alarm(0); /* turn off any alarms */
119 getyx(stdscr
, y
, x
); /* save cursor location */
121 for (loop
= 0; loop
< 80; ++loop
) { /* save line on screen */
125 line
[80] = '\0'; /* nul terminate */
127 if (Player
.p_status
== S_INBATTLE
|| Player
.p_status
== S_MONSTER
)
128 /* in midst of fighting */
130 mvaddstr(4, 0, "Quitting now will automatically kill your character. Still want to ? ");
131 ch
= getanswer("NY", FALSE
);
133 death("Bailing out");
136 mvaddstr(4, 0, "Do you really want to quit ? ");
137 ch
= getanswer("NY", FALSE
);
143 mvaddstr(4, 0, line
); /* restore data on screen */
144 move(y
, x
); /* restore cursor */
148 signal(SIGINT
, interrupt
);
151 signal(SIGINT
, interrupt
);
154 alarm(savealarm
); /* restore alarm */
158 getanswer(choices
, def
)
163 volatile int loop
; /* counter */
164 volatile int oldx
, oldy
; /* original coordinates on screen */
166 getyx(stdscr
, oldy
, oldx
);
167 alarm(0); /* make sure alarm is off */
169 for (loop
= 3; loop
; --loop
)
170 /* try for 3 times */
172 if (setjmp(Timeoenv
) != 0)
173 /* timed out waiting for response */
175 if (def
|| loop
<= 1)
176 /* return default answer */
179 /* prompt, and try again */
182 /* wait for response */
187 sigset(SIGALRM
, catchalarm
);
189 signal(SIGALRM
, catchalarm
);
193 alarm(7); /* short */
195 alarm(600); /* long */
199 alarm(0); /* turn off timeout */
202 /* caught some signal */
210 clearok(stdscr
, TRUE
); /* force clear screen */
211 ++loop
; /* don't count this input */
215 addch(ch
); /* echo character */
219 /* convert to upper case */
222 if (def
|| strchr(choices
, ch
) != NULL
)
226 if (!def
&& loop
> 1)
227 /* bad choice; prompt, and try again */
229 YELL
: mvprintw(oldy
+ 1, 0, "Please choose one of : [%s]\n", choices
);
234 /* return default answer */
246 longjmp(Timeoenv
, 1);