io.c revision 1.2       1  1.2  cgd /*	$NetBSD: io.c,v 1.2 1995/03/24 03:58:50 cgd Exp $	*/
      2  1.2  cgd 
      3  1.1  jtc /*
      4  1.1  jtc  * io.c - input/output routines for Phantasia
      5  1.1  jtc  */
      6  1.1  jtc 
      7  1.1  jtc #include "include.h"
      8  1.1  jtc 
      9  1.1  jtc /************************************************************************
     10  1.1  jtc /
     11  1.1  jtc / FUNCTION NAME: getstring()
     12  1.1  jtc /
     13  1.1  jtc / FUNCTION: read a string from operator
     14  1.1  jtc /
     15  1.1  jtc / AUTHOR: E. A. Estes, 12/4/85
     16  1.1  jtc /
     17  1.1  jtc / ARGUMENTS:
     18  1.1  jtc /	char *cp - pointer to buffer area to fill
     19  1.1  jtc /	int mx - maximum number of characters to put in buffer
     20  1.1  jtc /
     21  1.1  jtc / RETURN VALUE: none
     22  1.1  jtc /
     23  1.1  jtc / MODULES CALLED: wmove(), _filbuf(), clearok(), waddstr(), wrefresh(),
     24  1.1  jtc /	wclrtoeol()
     25  1.1  jtc /
     26  1.1  jtc / GLOBAL INPUTS: Echo, _iob[], Wizard, *stdscr
     27  1.1  jtc /
     28  1.1  jtc / GLOBAL OUTPUTS: _iob[]
     29  1.1  jtc /
     30  1.1  jtc / DESCRIPTION:
     31  1.1  jtc /	Read a string from the keyboard.
     32  1.1  jtc /	This routine is specially designed to:
     33  1.1  jtc /
     34  1.1  jtc /	    - strip non-printing characters (unless Wizard)
     35  1.1  jtc /	    - echo, if desired
     36  1.1  jtc /	    - redraw the screen if CH_REDRAW is entered
     37  1.1  jtc /	    - read in only 'mx - 1' characters or less characters
     38  1.1  jtc /	    - nul-terminate string, and throw away newline
     39  1.1  jtc /
     40  1.1  jtc /	'mx' is assumed to be at least 2.
     41  1.1  jtc /
     42  1.1  jtc /************************************************************************/
     43  1.1  jtc 
     44  1.1  jtc getstring(cp, mx)
     45  1.1  jtc register char	*cp;
     46  1.1  jtc register int	mx;
     47  1.1  jtc {
     48  1.1  jtc register char	*inptr;		/* pointer into string for next string */
     49  1.1  jtc int	x, y;			/* original x, y coordinates on screen */
     50  1.1  jtc int	ch;			/* input */
     51  1.1  jtc 
     52  1.1  jtc     getyx(stdscr, y, x);	/* get coordinates on screen */
     53  1.1  jtc     inptr = cp;
     54  1.1  jtc     *inptr = '\0';		/* clear string to start */
     55  1.1  jtc     --mx;			/* reserve room in string for nul terminator */
     56  1.1  jtc 
     57  1.1  jtc     do
     58  1.1  jtc 	/* get characters and process */
     59  1.1  jtc 	{
     60  1.1  jtc 	if (Echo)
     61  1.1  jtc 	    mvaddstr(y, x, cp);	/* print string on screen */
     62  1.1  jtc 	clrtoeol();		/* clear any data after string */
     63  1.1  jtc 	refresh();		/* update screen */
     64  1.1  jtc 
     65  1.1  jtc 	ch = getchar();		/* get character */
     66  1.1  jtc 
     67  1.1  jtc 	switch (ch)
     68  1.1  jtc 	    {
     69  1.1  jtc 	    case CH_ERASE:	/* back up one character */
     70  1.1  jtc 		if (inptr > cp)
     71  1.1  jtc 		    --inptr;
     72  1.1  jtc 		break;
     73  1.1  jtc 
     74  1.1  jtc 	    case CH_KILL:	/* back up to original location */
     75  1.1  jtc 		inptr = cp;
     76  1.1  jtc 		break;
     77  1.1  jtc 
     78  1.1  jtc 	    case CH_NEWLINE:	/* terminate string */
     79  1.1  jtc 		break;
     80  1.1  jtc 
     81  1.1  jtc 	    case CH_REDRAW:	/* redraw screen */
     82  1.1  jtc 		clearok(stdscr, TRUE);
     83  1.1  jtc 		continue;
     84  1.1  jtc 
     85  1.1  jtc 	    default:		/* put data in string */
     86  1.1  jtc 		if (ch >= ' ' || Wizard)
     87  1.1  jtc 		    /* printing char; put in string */
     88  1.1  jtc 		    *inptr++ = ch;
     89  1.1  jtc 	    }
     90  1.1  jtc 
     91  1.1  jtc 	*inptr = '\0';		/* terminate string */
     92  1.1  jtc 	}
     93  1.1  jtc     while (ch != CH_NEWLINE && inptr < cp + mx);
     94  1.1  jtc }
     95  1.1  jtc /**/
     97  1.1  jtc /************************************************************************
     98  1.1  jtc /
     99  1.1  jtc / FUNCTION NAME: more()
    100  1.1  jtc /
    101  1.1  jtc / FUNCTION: pause and prompt player
    102  1.1  jtc /
    103  1.1  jtc / AUTHOR: E. A. Estes, 12/4/85
    104  1.1  jtc /
    105  1.1  jtc / ARGUMENTS:
    106  1.1  jtc /	int where - line on screen on which to pause
    107  1.1  jtc /
    108  1.1  jtc / RETURN VALUE: none
    109  1.1  jtc /
    110  1.1  jtc / MODULES CALLED: wmove(), waddstr(), getanswer()
    111  1.1  jtc /
    112  1.1  jtc / GLOBAL INPUTS: *stdscr
    113  1.1  jtc /
    114  1.1  jtc / GLOBAL OUTPUTS: none
    115  1.1  jtc /
    116  1.1  jtc / DESCRIPTION:
    117  1.1  jtc /	Print a message, and wait for a space character.
    118  1.1  jtc /
    119  1.1  jtc /************************************************************************/
    120  1.1  jtc 
    121  1.1  jtc more(where)
    122  1.1  jtc int	where;
    123  1.1  jtc {
    124  1.1  jtc     mvaddstr(where, 0, "-- more --");
    125  1.1  jtc     getanswer(" ", FALSE);
    126  1.1  jtc }
    127  1.1  jtc /**/
    129  1.1  jtc /************************************************************************
    130  1.1  jtc /
    131  1.1  jtc / FUNCTION NAME: infloat()
    132  1.1  jtc /
    133  1.1  jtc / FUNCTION: input a floating point number from operator
    134  1.1  jtc /
    135  1.1  jtc / AUTHOR: E. A. Estes, 12/4/85
    136  1.1  jtc /
    137  1.1  jtc / ARGUMENTS: none
    138  1.1  jtc /
    139  1.1  jtc / RETURN VALUE: floating point number from operator
    140  1.1  jtc /
    141  1.1  jtc / MODULES CALLED: sscanf(), getstring()
    142  1.1  jtc /
    143  1.1  jtc / GLOBAL INPUTS: Databuf[]
    144  1.1  jtc /
    145  1.1  jtc / GLOBAL OUTPUTS: none
    146  1.1  jtc /
    147  1.1  jtc / DESCRIPTION:
    148  1.1  jtc /	Read a string from player, and scan for a floating point
    149  1.1  jtc /	number.
    150  1.1  jtc /	If no valid number is found, return 0.0.
    151  1.1  jtc /
    152  1.1  jtc /************************************************************************/
    153  1.1  jtc 
    154  1.1  jtc double
    155  1.1  jtc infloat()
    156  1.1  jtc {
    157  1.1  jtc double	result;		/* return value */
    158  1.1  jtc 
    159  1.1  jtc     getstring(Databuf, SZ_DATABUF);
    160  1.1  jtc     if (sscanf(Databuf, "%lf", &result) < 1)
    161  1.1  jtc 	/* no valid number entered */
    162  1.1  jtc 	result = 0.0;
    163  1.1  jtc 
    164  1.1  jtc     return(result);
    165  1.1  jtc }
    166  1.1  jtc /**/
    168  1.1  jtc /************************************************************************
    169  1.1  jtc /
    170  1.1  jtc / FUNCTION NAME: inputoption()
    171  1.1  jtc /
    172  1.1  jtc / FUNCTION: input an option value from player
    173  1.1  jtc /
    174  1.1  jtc / AUTHOR: E. A. Estes, 12/4/85
    175  1.1  jtc /
    176  1.1  jtc / ARGUMENTS: none
    177  1.1  jtc /
    178  1.1  jtc / RETURN VALUE: none
    179  1.1  jtc /
    180  1.1  jtc / MODULES CALLED: floor(), drandom(), getanswer()
    181  1.1  jtc /
    182  1.1  jtc / GLOBAL INPUTS: Player
    183  1.1  jtc /
    184  1.1  jtc / GLOBAL OUTPUTS: Player
    185  1.1  jtc /
    186  1.1  jtc / DESCRIPTION:
    187  1.1  jtc /	Age increases with every move.
    188  1.1  jtc /	Refresh screen, and get a single character option from player.
    189  1.1  jtc /	Return a random value if player's ring has gone bad.
    190  1.1  jtc /
    191  1.1  jtc /************************************************************************/
    192  1.1  jtc 
    193  1.1  jtc inputoption()
    194  1.1  jtc {
    195  1.1  jtc     ++Player.p_age;		/* increase age */
    196  1.1  jtc 
    197  1.1  jtc     if (Player.p_ring.ring_type != R_SPOILED)
    198  1.1  jtc 	/* ring ok */
    199  1.1  jtc 	return(getanswer("T ", TRUE));
    200  1.1  jtc     else
    201  1.1  jtc 	/* bad ring */
    202  1.1  jtc 	{
    203  1.1  jtc 	getanswer(" ", TRUE);
    204  1.1  jtc 	return((int) ROLL(0.0, 5.0) + '0');
    205  1.1  jtc 	}
    206  1.1  jtc }
    207  1.1  jtc /**/
    209  1.1  jtc /************************************************************************
    210  1.1  jtc /
    211  1.1  jtc / FUNCTION NAME: interrupt()
    212  1.1  jtc /
    213  1.1  jtc / FUNCTION: handle interrupt from operator
    214  1.1  jtc /
    215  1.1  jtc / AUTHOR: E. A. Estes, 12/4/85
    216  1.1  jtc /
    217  1.1  jtc / ARGUMENTS: none
    218  1.1  jtc /
    219  1.1  jtc / RETURN VALUE: none
    220  1.1  jtc /
    221  1.1  jtc / MODULES CALLED: fork(), exit(), wait(), death(), alarm(), execl(), wmove(),
    222  1.1  jtc /	getgid(), signal(), getenv(), wclear(), setuid(), getuid(), setgid(),
    223  1.1  jtc /	crmode(), clearok(), waddstr(), cleanup(), wrefresh(), leavegame(),
    224  1.1  jtc /	getanswer()
    225  1.1  jtc /
    226  1.1  jtc / GLOBAL INPUTS: Player, *stdscr
    227  1.1  jtc /
    228  1.1  jtc / GLOBAL OUTPUTS: none
    229  1.1  jtc /
    230  1.1  jtc / DESCRIPTION:
    231  1.1  jtc /	Allow player to quit upon hitting the interrupt key.
    232  1.1  jtc /	If the player wants to quit while in battle, he/she automatically
    233  1.1  jtc /	dies.
    234  1.1  jtc /
    235  1.1  jtc /************************************************************************/
    236  1.1  jtc 
    237  1.1  jtc interrupt()
    238  1.1  jtc {
    239  1.1  jtc char	line[81];		/* a place to store data already on screen */
    240  1.1  jtc register int	loop;		/* counter */
    241  1.1  jtc int	x, y;			/* coordinates on screen */
    242  1.1  jtc int	ch;			/* input */
    243  1.1  jtc unsigned	savealarm;	/* to save alarm value */
    244  1.1  jtc 
    245  1.1  jtc #ifdef SYS3
    246  1.1  jtc     signal(SIGINT, SIG_IGN);
    247  1.1  jtc #endif
    248  1.1  jtc #ifdef SYS5
    249  1.1  jtc     signal(SIGINT, SIG_IGN);
    250  1.1  jtc #endif
    251  1.1  jtc 
    252  1.1  jtc     savealarm = alarm(0);		/* turn off any alarms */
    253  1.1  jtc 
    254  1.1  jtc     getyx(stdscr, y, x);		/* save cursor location */
    255  1.1  jtc 
    256  1.1  jtc     for (loop = 0; loop < 80; ++loop)	/* save line on screen */
    257  1.1  jtc 	{
    258  1.1  jtc 	move(4, loop);
    259  1.1  jtc 	line[loop] = inch();
    260  1.1  jtc 	}
    261  1.1  jtc     line[80] = '\0';			/* nul terminate */
    262  1.1  jtc 
    263  1.1  jtc     if (Player.p_status == S_INBATTLE || Player.p_status == S_MONSTER)
    264  1.1  jtc 	/* in midst of fighting */
    265  1.1  jtc 	{
    266  1.1  jtc 	mvaddstr(4, 0, "Quitting now will automatically kill your character.  Still want to ? ");
    267  1.1  jtc 	ch = getanswer("NY", FALSE);
    268  1.1  jtc 	if (ch == 'Y')
    269  1.1  jtc 	    death("Bailing out");
    270  1.1  jtc 	    /*NOTREACHED*/
    271  1.1  jtc 	}
    272  1.1  jtc     else
    273  1.1  jtc 	{
    274  1.1  jtc 	mvaddstr(4, 0, "Do you really want to quit ? ");
    275  1.1  jtc 	ch = getanswer("NY", FALSE);
    276  1.1  jtc 	if (ch == 'Y')
    277  1.1  jtc 	    leavegame();
    278  1.1  jtc 	    /*NOTREACHED*/
    279  1.1  jtc 	}
    280  1.1  jtc 
    281  1.1  jtc     mvaddstr(4, 0, line); 		/* restore data on screen */
    282  1.1  jtc     move(y, x);				/* restore cursor */
    283  1.1  jtc     refresh();
    284  1.1  jtc 
    285  1.1  jtc #ifdef SYS3
    286  1.1  jtc     signal(SIGINT, interrupt);
    287  1.1  jtc #endif
    288  1.1  jtc #ifdef SYS5
    289  1.1  jtc     signal(SIGINT, interrupt);
    290  1.1  jtc #endif
    291  1.1  jtc 
    292  1.1  jtc     alarm(savealarm);			/* restore alarm */
    293  1.1  jtc }
    294  1.1  jtc /**/
    296  1.1  jtc /************************************************************************
    297  1.1  jtc /
    298  1.1  jtc / FUNCTION NAME: getanswer()
    299  1.1  jtc /
    300  1.1  jtc / FUNCTION: get an answer from operator
    301  1.1  jtc /
    302  1.1  jtc / AUTHOR: E. A. Estes, 12/4/85
    303  1.1  jtc /
    304  1.1  jtc / ARGUMENTS:
    305  1.1  jtc /	char *choices - string of (upper case) valid choices
    306  1.1  jtc /	bool def - set if default answer
    307  1.1  jtc /
    308  1.1  jtc / RETURN VALUE: none
    309  1.1  jtc /
    310  1.1  jtc / MODULES CALLED: alarm(), wmove(), waddch(), signal(), setjmp(), strchr(),
    311  1.1  jtc /	_filbuf(), clearok(), toupper(), wrefresh(), mvprintw(), wclrtoeol()
    312  1.1  jtc /
    313  1.1  jtc / GLOBAL INPUTS: catchalarm(), Echo, _iob[], _ctype[], *stdscr, Timeout,
    314  1.1  jtc /	Timeoenv[]
    315  1.1  jtc /
    316  1.1  jtc / GLOBAL OUTPUTS: _iob[]
    317  1.1  jtc /
    318  1.1  jtc / DESCRIPTION:
    319  1.1  jtc /	Get a single character answer from operator.
    320  1.1  jtc /	Timeout waiting for response.  If we timeout, or the
    321  1.1  jtc /	answer in not in the list of valid choices, print choices,
    322  1.1  jtc /	and wait again, otherwise return the first character in ths
    323  1.1  jtc /	list of choices.
    324  1.1  jtc /	Give up after 3 tries.
    325  1.1  jtc /
    326  1.1  jtc /************************************************************************/
    327  1.1  jtc 
    328  1.1  jtc getanswer(choices, def)
    329  1.1  jtc char	*choices;
    330  1.1  jtc bool	def;
    331  1.1  jtc {
    332  1.1  jtc int	ch;			/* input */
    333  1.1  jtc int	loop;			/* counter */
    334  1.1  jtc int	oldx, oldy;		/* original coordinates on screen */
    335  1.1  jtc 
    336  1.1  jtc     getyx(stdscr, oldy, oldx);
    337  1.1  jtc     alarm(0);				/* make sure alarm is off */
    338  1.1  jtc 
    339  1.1  jtc     for (loop = 3; loop; --loop)
    340  1.1  jtc 	/* try for 3 times */
    341  1.1  jtc 	{
    342  1.1  jtc 	if (setjmp(Timeoenv) != 0)
    343  1.1  jtc 	    /* timed out waiting for response */
    344  1.1  jtc 	    {
    345  1.1  jtc 	    if (def || loop <= 1)
    346  1.1  jtc 		/* return default answer */
    347  1.1  jtc 		break;
    348  1.1  jtc 	    else
    349  1.1  jtc 		/* prompt, and try again */
    350  1.1  jtc 		goto YELL;
    351  1.1  jtc 	    }
    352  1.1  jtc 	else
    353  1.1  jtc 	    /* wait for response */
    354  1.1  jtc 	    {
    355  1.1  jtc 	    clrtoeol();
    356  1.1  jtc 	    refresh();
    357  1.1  jtc #ifdef BSD41
    358  1.1  jtc 	    sigset(SIGALRM, catchalarm);
    359  1.1  jtc #else
    360  1.1  jtc 	    signal(SIGALRM, catchalarm);
    361  1.1  jtc #endif
    362  1.1  jtc 	    /* set timeout */
    363  1.1  jtc 	    if (Timeout)
    364  1.1  jtc 		alarm(7);		/* short */
    365  1.1  jtc 	    else
    366  1.1  jtc 		alarm(600);		/* long */
    367  1.1  jtc 
    368  1.1  jtc 	    ch = getchar();
    369  1.1  jtc 
    370  1.1  jtc 	    alarm(0);			/* turn off timeout */
    371  1.1  jtc 
    372  1.1  jtc 	    if (ch < 0)
    373  1.1  jtc 		/* caught some signal */
    374  1.1  jtc 		{
    375  1.1  jtc 		++loop;
    376  1.1  jtc 		continue;
    377  1.1  jtc 		}
    378  1.1  jtc 	    else if (ch == CH_REDRAW)
    379  1.1  jtc 		/* redraw screen */
    380  1.1  jtc 		{
    381  1.1  jtc 		clearok(stdscr, TRUE);	/* force clear screen */
    382  1.1  jtc 		++loop;			/* don't count this input */
    383  1.1  jtc 		continue;
    384  1.1  jtc 		}
    385  1.1  jtc 	    else if (Echo)
    386  1.1  jtc 		{
    387  1.1  jtc 		addch(ch);		/* echo character */
    388  1.1  jtc 		refresh();
    389  1.1  jtc 		}
    390  1.1  jtc 
    391  1.1  jtc 	    if (islower(ch))
    392  1.1  jtc 		/* convert to upper case */
    393  1.1  jtc 		ch = toupper(ch);
    394  1.1  jtc 
    395  1.1  jtc 	    if (def || strchr(choices, ch) != NULL)
    396  1.1  jtc 		/* valid choice */
    397  1.1  jtc 		return(ch);
    398  1.1  jtc 	    else if (!def && loop > 1)
    399  1.1  jtc 		/* bad choice; prompt, and try again */
    400  1.1  jtc 		{
    401  1.1  jtc YELL:		mvprintw(oldy + 1, 0, "Please choose one of : [%s]\n", choices);
    402  1.1  jtc 		move(oldy, oldx);
    403  1.1  jtc 		clrtoeol();
    404  1.1  jtc 		continue;
    405  1.1  jtc 		}
    406  1.1  jtc 	    else
    407  1.1  jtc 		/* return default answer */
    408  1.1  jtc 		break;
    409  1.1  jtc 	    }
    410  1.1  jtc 	}
    411  1.1  jtc 
    412  1.1  jtc     return(*choices);
    413  1.1  jtc }
    414  1.1  jtc /**/
    416  1.1  jtc /************************************************************************
    417  1.1  jtc /
    418  1.1  jtc / FUNCTION NAME: catchalarm()
    419  1.1  jtc /
    420  1.1  jtc / FUNCTION: catch timer when waiting for input
    421  1.1  jtc /
    422  1.1  jtc / AUTHOR: E. A. Estes, 12/4/85
    423  1.1  jtc /
    424  1.1  jtc / ARGUMENTS: none
    425  1.1  jtc /
    426  1.1  jtc / RETURN VALUE: none
    427  1.1  jtc /
    428  1.1  jtc / MODULES CALLED: longjmp()
    429  1.1  jtc /
    430  1.1  jtc / GLOBAL INPUTS: Timeoenv[]
    431  1.1  jtc /
    432  1.1  jtc / GLOBAL OUTPUTS: none
    433  1.1  jtc /
    434  1.1  jtc / DESCRIPTION:
    435  1.1  jtc /	Come here when the alarm expires while waiting for input.
    436  1.1  jtc /	Simply longjmp() into getanswer().
    437  1.1  jtc /
    438  1.1  jtc /************************************************************************/
    439           
    440           void
    441           catchalarm()
    442           {
    443               longjmp(Timeoenv, 1);
    444           }
    445