#include <sys/cdefs.h>
#include <ctype.h>
#include <math.h>
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "macros.h"
#include "phantdefs.h"
#include "phantstruct.h"
#include "phantglobs.h"
#undef bool
#include <curses.h>
static void catchalarm(int) __dead;
void
getstring(char *cp, int mx)
{
char *inptr;
int x, y;
int ch;
getyx(stdscr, y, x);
inptr = cp;
*inptr = '\0';
--mx;
do
{
if (Echo)
mvaddstr(y, x, cp);
clrtoeol();
refresh();
ch = getchar();
switch (ch) {
case CH_ERASE:
if (inptr > cp)
--inptr;
break;
case CH_KILL:
inptr = cp;
break;
case CH_NEWLINE:
break;
case CH_REDRAW:
clearok(stdscr, TRUE);
continue;
default:
if (ch >= ' ' || Wizard)
*inptr++ = ch;
}
*inptr = '\0';
}
while (ch != CH_NEWLINE && inptr < cp + mx);
}
void
more(int where)
{
mvaddstr(where, 0, "-- more --");
getanswer(" ", FALSE);
}
double
infloat(void)
{
double result;
getstring(Databuf, SZ_DATABUF);
if (sscanf(Databuf, "%lf", &result) < 1)
result = 0.0;
return (result);
}
int
inputoption(void)
{
++Player.p_age;
if (Player.p_ring.ring_type != R_SPOILED)
return (getanswer("T ", TRUE));
else
{
getanswer(" ", TRUE);
return ((int) ROLL(0.0, 5.0) + '0');
}
}
void
interrupt(void)
{
char line[81];
int loop;
int x, y;
int ch;
unsigned savealarm;
#ifdef SYS3
signal(SIGINT, SIG_IGN);
#endif
#ifdef SYS5
signal(SIGINT, SIG_IGN);
#endif
savealarm = alarm(0);
getyx(stdscr, y, x);
for (loop = 0; loop < 80; ++loop) {
move(4, loop);
line[loop] = inch();
}
line[80] = '\0';
if (Player.p_status == S_INBATTLE || Player.p_status == S_MONSTER)
{
mvaddstr(4, 0, "Quitting now will automatically kill your character. Still want to ? ");
ch = getanswer("NY", FALSE);
if (ch == 'Y')
death("Bailing out");
} else {
mvaddstr(4, 0, "Do you really want to quit ? ");
ch = getanswer("NY", FALSE);
if (ch == 'Y')
leavegame();
}
mvaddstr(4, 0, line);
move(y, x);
refresh();
#ifdef SYS3
signal(SIGINT, interrupt);
#endif
#ifdef SYS5
signal(SIGINT, interrupt);
#endif
alarm(savealarm);
}
int
getanswer(const char *choices, phbool def)
{
int ch;
volatile int loop;
volatile int oldx, oldy;
getyx(stdscr, oldy, oldx);
alarm(0);
for (loop = 3; loop; --loop)
{
if (setjmp(Timeoenv) != 0)
{
if (def || loop <= 1)
break;
else
goto YELL;
} else
{
clrtoeol();
refresh();
#ifdef BSD41
sigset(SIGALRM, catchalarm);
#else
signal(SIGALRM, catchalarm);
#endif
if (Timeout)
alarm(7);
else
alarm(600);
ch = getchar();
alarm(0);
if (ch < 0)
{
++loop;
continue;
} else
if (ch == CH_REDRAW)
{
clearok(stdscr, TRUE);
++loop;
continue;
} else
if (Echo) {
addch(ch);
refresh();
}
if (islower(ch))
ch = toupper(ch);
if (def || strchr(choices, ch) != NULL)
return (ch);
else
if (!def && loop > 1)
{
YELL: mvprintw(oldy + 1, 0, "Please choose one of : [%s]\n", choices);
move(oldy, oldx);
clrtoeol();
continue;
} else
break;
}
}
return (*choices);
}
static void
catchalarm(int dummy __unused)
{
longjmp(Timeoenv, 1);
}