1 /* $NetBSD: io.c,v 1.13 2009/08/12 08:21:41 dholland Exp $ */
4 * io.c - input/output routines for Phantasia
18 #include "phantdefs.h"
19 #include "phantstruct.h"
20 #include "phantglobs.h"
21 //#include "pathnames.h"
26 static void catchalarm(int) __dead
;
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 */
35 getyx(stdscr
, y
, x
); /* get coordinates on screen */
37 *inptr
= '\0'; /* clear string to start */
38 --mx
; /* reserve room in string for nul terminator */
41 /* get characters and process */
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 */
51 case CH_ERASE
: /* back up one character */
56 case CH_KILL
: /* back up to original location */
60 case CH_NEWLINE
: /* terminate string */
63 case CH_REDRAW
:/* redraw screen */
64 clearok(stdscr
, TRUE
);
67 default: /* put data in string */
68 if (ch
>= ' ' || Wizard
)
69 /* printing char; put in string */
73 *inptr
= '\0'; /* terminate string */
75 while (ch
!= CH_NEWLINE
&& inptr
< cp
+ mx
);
81 mvaddstr(where
, 0, "-- more --");
82 getanswer(" ", FALSE
);
88 double result
; /* return value */
90 getstring(Databuf
, SZ_DATABUF
);
91 if (sscanf(Databuf
, "%lf", &result
) < 1)
92 /* no valid number entered */
101 ++Player
.p_age
; /* increase age */
103 if (Player
.p_ring
.ring_type
!= R_SPOILED
)
105 return (getanswer("T ", TRUE
));
109 getanswer(" ", TRUE
);
110 return ((int) ROLL(0.0, 5.0) + '0');
117 char line
[81]; /* a place to store data already on screen */
118 int loop
; /* counter */
119 int x
, y
; /* coordinates on screen */
121 unsigned savealarm
; /* to save alarm value */
124 signal(SIGINT
, SIG_IGN
);
127 signal(SIGINT
, SIG_IGN
);
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 */
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
);
146 death("Bailing out");
149 mvaddstr(4, 0, "Do you really want to quit ? ");
150 ch
= getanswer("NY", FALSE
);
156 mvaddstr(4, 0, line
); /* restore data on screen */
157 move(y
, x
); /* restore cursor */
161 signal(SIGINT
, interrupt
);
164 signal(SIGINT
, interrupt
);
167 alarm(savealarm
); /* restore alarm */
171 getanswer(const char *choices
, phbool def
)
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 */
190 /* prompt, and try again */
193 /* wait for response */
198 sigset(SIGALRM
, catchalarm
);
200 signal(SIGALRM
, catchalarm
);
204 alarm(7); /* short */
206 alarm(600); /* long */
210 alarm(0); /* turn off timeout */
213 /* caught some signal */
221 clearok(stdscr
, TRUE
); /* force clear screen */
222 ++loop
; /* don't count this input */
226 addch(ch
); /* echo character */
230 /* convert to upper case */
233 if (def
|| strchr(choices
, ch
) != NULL
)
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
);
245 /* return default answer */
254 catchalarm(int dummy __unused
)
256 longjmp(Timeoenv
, 1);