]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - phantasia/io.c
1 /* $NetBSD: io.c,v 1.13 2009/08/12 08:21:41 dholland Exp $ */
4 * io.c - input/output routines for Phantasia
12 static void catchalarm(int) __dead
;
15 getstring(char *cp
, int mx
)
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
);
67 mvaddstr(where
, 0, "-- more --");
68 getanswer(" ", FALSE
);
74 double result
; /* return value */
76 getstring(Databuf
, SZ_DATABUF
);
77 if (sscanf(Databuf
, "%lf", &result
) < 1)
78 /* no valid number entered */
87 ++Player
.p_age
; /* increase age */
89 if (Player
.p_ring
.ring_type
!= R_SPOILED
)
91 return (getanswer("T ", TRUE
));
96 return ((int) ROLL(0.0, 5.0) + '0');
103 char line
[81]; /* a place to store data already on screen */
104 int loop
; /* counter */
105 int x
, y
; /* coordinates on screen */
107 unsigned savealarm
; /* to save alarm value */
110 signal(SIGINT
, SIG_IGN
);
113 signal(SIGINT
, SIG_IGN
);
116 savealarm
= alarm(0); /* turn off any alarms */
118 getyx(stdscr
, y
, x
); /* save cursor location */
120 for (loop
= 0; loop
< 80; ++loop
) { /* save line on screen */
124 line
[80] = '\0'; /* nul terminate */
126 if (Player
.p_status
== S_INBATTLE
|| Player
.p_status
== S_MONSTER
)
127 /* in midst of fighting */
129 mvaddstr(4, 0, "Quitting now will automatically kill your character. Still want to ? ");
130 ch
= getanswer("NY", FALSE
);
132 death("Bailing out");
135 mvaddstr(4, 0, "Do you really want to quit ? ");
136 ch
= getanswer("NY", FALSE
);
142 mvaddstr(4, 0, line
); /* restore data on screen */
143 move(y
, x
); /* restore cursor */
147 signal(SIGINT
, interrupt
);
150 signal(SIGINT
, interrupt
);
153 alarm(savealarm
); /* restore alarm */
157 getanswer(const char *choices
, phbool def
)
160 volatile int loop
; /* counter */
161 volatile int oldx
, oldy
; /* original coordinates on screen */
163 getyx(stdscr
, oldy
, oldx
);
164 alarm(0); /* make sure alarm is off */
166 for (loop
= 3; loop
; --loop
)
167 /* try for 3 times */
169 if (setjmp(Timeoenv
) != 0)
170 /* timed out waiting for response */
172 if (def
|| loop
<= 1)
173 /* return default answer */
176 /* prompt, and try again */
179 /* wait for response */
184 sigset(SIGALRM
, catchalarm
);
186 signal(SIGALRM
, catchalarm
);
190 alarm(7); /* short */
192 alarm(600); /* long */
196 alarm(0); /* turn off timeout */
199 /* caught some signal */
207 clearok(stdscr
, TRUE
); /* force clear screen */
208 ++loop
; /* don't count this input */
212 addch(ch
); /* echo character */
216 /* convert to upper case */
219 if (def
|| strchr(choices
, ch
) != NULL
)
223 if (!def
&& loop
> 1)
224 /* bad choice; prompt, and try again */
226 YELL
: mvprintw(oldy
+ 1, 0, "Please choose one of : [%s]\n", choices
);
231 /* return default answer */
240 catchalarm(int dummy __unused
)
242 longjmp(Timeoenv
, 1);