]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - phantasia/io.c
1 /* $NetBSD: io.c,v 1.12 2009/05/25 23:08:45 dholland Exp $ */
4 * io.c - input/output routines for Phantasia
13 getstring(char *cp
, int mx
)
15 char *inptr
; /* pointer into string for next string */
16 int x
, y
; /* original x, y coordinates on screen */
19 getyx(stdscr
, y
, x
); /* get coordinates on screen */
21 *inptr
= '\0'; /* clear string to start */
22 --mx
; /* reserve room in string for nul terminator */
25 /* get characters and process */
28 mvaddstr(y
, x
, cp
); /* print string on screen */
29 clrtoeol(); /* clear any data after string */
30 refresh(); /* update screen */
32 ch
= getchar(); /* get character */
35 case CH_ERASE
: /* back up one character */
40 case CH_KILL
: /* back up to original location */
44 case CH_NEWLINE
: /* terminate string */
47 case CH_REDRAW
:/* redraw screen */
48 clearok(stdscr
, TRUE
);
51 default: /* put data in string */
52 if (ch
>= ' ' || Wizard
)
53 /* printing char; put in string */
57 *inptr
= '\0'; /* terminate string */
59 while (ch
!= CH_NEWLINE
&& inptr
< cp
+ mx
);
65 mvaddstr(where
, 0, "-- more --");
66 getanswer(" ", FALSE
);
72 double result
; /* return value */
74 getstring(Databuf
, SZ_DATABUF
);
75 if (sscanf(Databuf
, "%lf", &result
) < 1)
76 /* no valid number entered */
85 ++Player
.p_age
; /* increase age */
87 if (Player
.p_ring
.ring_type
!= R_SPOILED
)
89 return (getanswer("T ", TRUE
));
94 return ((int) ROLL(0.0, 5.0) + '0');
101 char line
[81]; /* a place to store data already on screen */
102 int loop
; /* counter */
103 int x
, y
; /* coordinates on screen */
105 unsigned savealarm
; /* to save alarm value */
108 signal(SIGINT
, SIG_IGN
);
111 signal(SIGINT
, SIG_IGN
);
114 savealarm
= alarm(0); /* turn off any alarms */
116 getyx(stdscr
, y
, x
); /* save cursor location */
118 for (loop
= 0; loop
< 80; ++loop
) { /* save line on screen */
122 line
[80] = '\0'; /* nul terminate */
124 if (Player
.p_status
== S_INBATTLE
|| Player
.p_status
== S_MONSTER
)
125 /* in midst of fighting */
127 mvaddstr(4, 0, "Quitting now will automatically kill your character. Still want to ? ");
128 ch
= getanswer("NY", FALSE
);
130 death("Bailing out");
133 mvaddstr(4, 0, "Do you really want to quit ? ");
134 ch
= getanswer("NY", FALSE
);
140 mvaddstr(4, 0, line
); /* restore data on screen */
141 move(y
, x
); /* restore cursor */
145 signal(SIGINT
, interrupt
);
148 signal(SIGINT
, interrupt
);
151 alarm(savealarm
); /* restore alarm */
155 getanswer(const char *choices
, phbool def
)
158 volatile int loop
; /* counter */
159 volatile int oldx
, oldy
; /* original coordinates on screen */
161 getyx(stdscr
, oldy
, oldx
);
162 alarm(0); /* make sure alarm is off */
164 for (loop
= 3; loop
; --loop
)
165 /* try for 3 times */
167 if (setjmp(Timeoenv
) != 0)
168 /* timed out waiting for response */
170 if (def
|| loop
<= 1)
171 /* return default answer */
174 /* prompt, and try again */
177 /* wait for response */
182 sigset(SIGALRM
, catchalarm
);
184 signal(SIGALRM
, catchalarm
);
188 alarm(7); /* short */
190 alarm(600); /* long */
194 alarm(0); /* turn off timeout */
197 /* caught some signal */
205 clearok(stdscr
, TRUE
); /* force clear screen */
206 ++loop
; /* don't count this input */
210 addch(ch
); /* echo character */
214 /* convert to upper case */
217 if (def
|| strchr(choices
, ch
) != NULL
)
221 if (!def
&& loop
> 1)
222 /* bad choice; prompt, and try again */
224 YELL
: mvprintw(oldy
+ 1, 0, "Please choose one of : [%s]\n", choices
);
229 /* return default answer */
238 catchalarm(int dummy __unused
)
240 longjmp(Timeoenv
, 1);