io.c revision 1.3 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1980 Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.1 cgd static char sccsid[] = "@(#)io.c 5.8 (Berkeley) 2/28/91";
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd # include <curses.h>
39 1.1 cgd # include <ctype.h>
40 1.1 cgd # include <signal.h>
41 1.1 cgd # include <stdarg.h>
42 1.1 cgd # include "deck.h"
43 1.1 cgd # include "cribbage.h"
44 1.1 cgd # include "cribcur.h"
45 1.1 cgd
46 1.1 cgd # define LINESIZE 128
47 1.1 cgd
48 1.1 cgd # ifdef CTRL
49 1.1 cgd # undef CTRL
50 1.1 cgd # endif
51 1.1 cgd # define CTRL(X) ('X' - 'A' + 1)
52 1.1 cgd
53 1.1 cgd # ifdef notdef /* defined in curses.h */
54 1.1 cgd # define erasechar() _tty.sg_erase
55 1.1 cgd # define killchar() _tty.sg_kill
56 1.1 cgd # endif
57 1.1 cgd
58 1.1 cgd char linebuf[ LINESIZE ];
59 1.1 cgd
60 1.1 cgd char *rankname[ RANKS ] = { "ACE", "TWO", "THREE", "FOUR",
61 1.1 cgd "FIVE", "SIX", "SEVEN", "EIGHT",
62 1.1 cgd "NINE", "TEN", "JACK", "QUEEN",
63 1.1 cgd "KING" };
64 1.1 cgd
65 1.1 cgd char *rankchar[ RANKS ] = { "A", "2", "3", "4", "5", "6", "7",
66 1.1 cgd "8", "9", "T", "J", "Q", "K" };
67 1.1 cgd
68 1.1 cgd char *suitname[ SUITS ] = { "SPADES", "HEARTS", "DIAMONDS",
69 1.1 cgd "CLUBS" };
70 1.1 cgd
71 1.1 cgd char *suitchar[ SUITS ] = { "S", "H", "D", "C" };
72 1.1 cgd
73 1.1 cgd
74 1.1 cgd
75 1.1 cgd /*
76 1.1 cgd * msgcard:
77 1.1 cgd * Call msgcrd in one of two forms
78 1.1 cgd */
79 1.1 cgd msgcard(c, brief)
80 1.1 cgd CARD c;
81 1.1 cgd BOOLEAN brief;
82 1.1 cgd {
83 1.1 cgd if (brief)
84 1.1 cgd return msgcrd(c, TRUE, (char *) NULL, TRUE);
85 1.1 cgd else
86 1.1 cgd return msgcrd(c, FALSE, " of ", FALSE);
87 1.1 cgd }
88 1.1 cgd
89 1.1 cgd
90 1.1 cgd
91 1.1 cgd /*
92 1.1 cgd * msgcrd:
93 1.1 cgd * Print the value of a card in ascii
94 1.1 cgd */
95 1.1 cgd msgcrd(c, brfrank, mid, brfsuit)
96 1.1 cgd CARD c;
97 1.1 cgd char *mid;
98 1.1 cgd BOOLEAN brfrank, brfsuit;
99 1.1 cgd {
100 1.1 cgd if (c.rank == EMPTY || c.suit == EMPTY)
101 1.1 cgd return FALSE;
102 1.1 cgd if (brfrank)
103 1.1 cgd addmsg("%1.1s", rankchar[c.rank]);
104 1.1 cgd else
105 1.1 cgd addmsg(rankname[c.rank]);
106 1.1 cgd if (mid != NULL)
107 1.1 cgd addmsg(mid);
108 1.1 cgd if (brfsuit)
109 1.1 cgd addmsg("%1.1s", suitchar[c.suit]);
110 1.1 cgd else
111 1.1 cgd addmsg(suitname[c.suit]);
112 1.1 cgd return TRUE;
113 1.1 cgd }
114 1.1 cgd
115 1.1 cgd /*
116 1.1 cgd * printcard:
117 1.1 cgd * Print out a card.
118 1.1 cgd */
119 1.1 cgd printcard(win, cardno, c, blank)
120 1.1 cgd WINDOW *win;
121 1.1 cgd int cardno;
122 1.1 cgd CARD c;
123 1.1 cgd BOOLEAN blank;
124 1.1 cgd {
125 1.1 cgd prcard(win, cardno * 2, cardno, c, blank);
126 1.1 cgd }
127 1.1 cgd
128 1.1 cgd /*
129 1.1 cgd * prcard:
130 1.1 cgd * Print out a card on the window at the specified location
131 1.1 cgd */
132 1.1 cgd prcard(win, y, x, c, blank)
133 1.1 cgd WINDOW *win;
134 1.1 cgd int y, x;
135 1.1 cgd CARD c;
136 1.1 cgd BOOLEAN blank;
137 1.1 cgd {
138 1.1 cgd if (c.rank == EMPTY)
139 1.1 cgd return;
140 1.1 cgd mvwaddstr(win, y + 0, x, "+-----+");
141 1.1 cgd mvwaddstr(win, y + 1, x, "| |");
142 1.1 cgd mvwaddstr(win, y + 2, x, "| |");
143 1.1 cgd mvwaddstr(win, y + 3, x, "| |");
144 1.1 cgd mvwaddstr(win, y + 4, x, "+-----+");
145 1.1 cgd if (!blank) {
146 1.1 cgd mvwaddch(win, y + 1, x + 1, rankchar[c.rank][0]);
147 1.1 cgd waddch(win, suitchar[c.suit][0]);
148 1.1 cgd mvwaddch(win, y + 3, x + 4, rankchar[c.rank][0]);
149 1.1 cgd waddch(win, suitchar[c.suit][0]);
150 1.1 cgd }
151 1.1 cgd }
152 1.1 cgd
153 1.1 cgd /*
154 1.1 cgd * prhand:
155 1.1 cgd * Print a hand of n cards
156 1.1 cgd */
157 1.1 cgd prhand(h, n, win, blank)
158 1.1 cgd CARD h[];
159 1.1 cgd int n;
160 1.1 cgd WINDOW *win;
161 1.1 cgd BOOLEAN blank;
162 1.1 cgd {
163 1.1 cgd register int i;
164 1.1 cgd
165 1.1 cgd werase(win);
166 1.1 cgd for (i = 0; i < n; i++)
167 1.1 cgd printcard(win, i, *h++, blank);
168 1.1 cgd wrefresh(win);
169 1.1 cgd }
170 1.1 cgd
171 1.1 cgd
172 1.1 cgd
173 1.1 cgd /*
174 1.1 cgd * infrom:
175 1.1 cgd * reads a card, supposedly in hand, accepting unambigous brief
176 1.1 cgd * input, returns the index of the card found...
177 1.1 cgd */
178 1.1 cgd infrom(hand, n, prompt)
179 1.1 cgd CARD hand[];
180 1.1 cgd int n;
181 1.1 cgd char *prompt;
182 1.1 cgd {
183 1.1 cgd register int i, j;
184 1.1 cgd CARD crd;
185 1.1 cgd
186 1.1 cgd if (n < 1) {
187 1.1 cgd printf("\nINFROM: %d = n < 1!!\n", n);
188 1.1 cgd exit(74);
189 1.1 cgd }
190 1.1 cgd for (;;) {
191 1.1 cgd msg(prompt);
192 1.1 cgd if (incard(&crd)) { /* if card is full card */
193 1.1 cgd if (!isone(crd, hand, n))
194 1.1 cgd msg("That's not in your hand");
195 1.1 cgd else {
196 1.1 cgd for (i = 0; i < n; i++)
197 1.1 cgd if (hand[i].rank == crd.rank &&
198 1.1 cgd hand[i].suit == crd.suit)
199 1.1 cgd break;
200 1.1 cgd if (i >= n) {
201 1.1 cgd printf("\nINFROM: isone or something messed up\n");
202 1.1 cgd exit(77);
203 1.1 cgd }
204 1.1 cgd return i;
205 1.1 cgd }
206 1.1 cgd }
207 1.1 cgd else /* if not full card... */
208 1.1 cgd if (crd.rank != EMPTY) {
209 1.1 cgd for (i = 0; i < n; i++)
210 1.1 cgd if (hand[i].rank == crd.rank)
211 1.1 cgd break;
212 1.1 cgd if (i >= n)
213 1.1 cgd msg("No such rank in your hand");
214 1.1 cgd else {
215 1.1 cgd for (j = i + 1; j < n; j++)
216 1.1 cgd if (hand[j].rank == crd.rank)
217 1.1 cgd break;
218 1.1 cgd if (j < n)
219 1.1 cgd msg("Ambiguous rank");
220 1.1 cgd else
221 1.1 cgd return i;
222 1.1 cgd }
223 1.1 cgd }
224 1.1 cgd else
225 1.1 cgd msg("Sorry, I missed that");
226 1.1 cgd }
227 1.1 cgd /* NOTREACHED */
228 1.1 cgd }
229 1.1 cgd
230 1.1 cgd
231 1.1 cgd
232 1.1 cgd /*
233 1.1 cgd * incard:
234 1.1 cgd * Inputs a card in any format. It reads a line ending with a CR
235 1.1 cgd * and then parses it.
236 1.1 cgd */
237 1.1 cgd incard(crd)
238 1.1 cgd CARD *crd;
239 1.1 cgd {
240 1.1 cgd char *getline();
241 1.1 cgd register int i;
242 1.1 cgd int rnk, sut;
243 1.1 cgd char *line, *p, *p1;
244 1.1 cgd BOOLEAN retval;
245 1.1 cgd
246 1.1 cgd retval = FALSE;
247 1.1 cgd rnk = sut = EMPTY;
248 1.1 cgd if (!(line = getline()))
249 1.1 cgd goto gotit;
250 1.1 cgd p = p1 = line;
251 1.1 cgd while( *p1 != ' ' && *p1 != NULL ) ++p1;
252 1.1 cgd *p1++ = NULL;
253 1.1 cgd if( *p == NULL ) goto gotit;
254 1.1 cgd /* IMPORTANT: no real card has 2 char first name */
255 1.1 cgd if( strlen(p) == 2 ) { /* check for short form */
256 1.1 cgd rnk = EMPTY;
257 1.1 cgd for( i = 0; i < RANKS; i++ ) {
258 1.1 cgd if( *p == *rankchar[i] ) {
259 1.1 cgd rnk = i;
260 1.1 cgd break;
261 1.1 cgd }
262 1.1 cgd }
263 1.1 cgd if( rnk == EMPTY ) goto gotit; /* it's nothing... */
264 1.1 cgd ++p; /* advance to next char */
265 1.1 cgd sut = EMPTY;
266 1.1 cgd for( i = 0; i < SUITS; i++ ) {
267 1.1 cgd if( *p == *suitchar[i] ) {
268 1.1 cgd sut = i;
269 1.1 cgd break;
270 1.1 cgd }
271 1.1 cgd }
272 1.1 cgd if( sut != EMPTY ) retval = TRUE;
273 1.1 cgd goto gotit;
274 1.1 cgd }
275 1.1 cgd rnk = EMPTY;
276 1.1 cgd for( i = 0; i < RANKS; i++ ) {
277 1.1 cgd if( !strcmp( p, rankname[i] ) || !strcmp( p, rankchar[i] ) ) {
278 1.1 cgd rnk = i;
279 1.1 cgd break;
280 1.1 cgd }
281 1.1 cgd }
282 1.1 cgd if( rnk == EMPTY ) goto gotit;
283 1.1 cgd p = p1;
284 1.1 cgd while( *p1 != ' ' && *p1 != NULL ) ++p1;
285 1.1 cgd *p1++ = NULL;
286 1.1 cgd if( *p == NULL ) goto gotit;
287 1.1 cgd if( !strcmp( "OF", p ) ) {
288 1.1 cgd p = p1;
289 1.1 cgd while( *p1 != ' ' && *p1 != NULL ) ++p1;
290 1.1 cgd *p1++ = NULL;
291 1.1 cgd if( *p == NULL ) goto gotit;
292 1.1 cgd }
293 1.1 cgd sut = EMPTY;
294 1.1 cgd for( i = 0; i < SUITS; i++ ) {
295 1.1 cgd if( !strcmp( p, suitname[i] ) || !strcmp( p, suitchar[i] ) ) {
296 1.1 cgd sut = i;
297 1.1 cgd break;
298 1.1 cgd }
299 1.1 cgd }
300 1.1 cgd if( sut != EMPTY ) retval = TRUE;
301 1.1 cgd gotit:
302 1.1 cgd (*crd).rank = rnk;
303 1.1 cgd (*crd).suit = sut;
304 1.1 cgd return( retval );
305 1.1 cgd }
306 1.1 cgd
307 1.1 cgd
308 1.1 cgd
309 1.1 cgd /*
310 1.1 cgd * getuchar:
311 1.1 cgd * Reads and converts to upper case
312 1.1 cgd */
313 1.1 cgd getuchar()
314 1.1 cgd {
315 1.1 cgd register int c;
316 1.1 cgd
317 1.1 cgd c = readchar();
318 1.1 cgd if (islower(c))
319 1.1 cgd c = toupper(c);
320 1.1 cgd waddch(Msgwin, c);
321 1.1 cgd return c;
322 1.1 cgd }
323 1.1 cgd
324 1.1 cgd /*
325 1.1 cgd * number:
326 1.1 cgd * Reads in a decimal number and makes sure it is between "lo" and
327 1.1 cgd * "hi" inclusive.
328 1.1 cgd */
329 1.1 cgd number(lo, hi, prompt)
330 1.1 cgd int lo, hi;
331 1.1 cgd char *prompt;
332 1.1 cgd {
333 1.1 cgd char *getline();
334 1.1 cgd register char *p;
335 1.1 cgd register int sum;
336 1.1 cgd
337 1.1 cgd sum = 0;
338 1.1 cgd for (;;) {
339 1.1 cgd msg(prompt);
340 1.1 cgd if(!(p = getline()) || *p == NULL) {
341 1.1 cgd msg(quiet ? "Not a number" : "That doesn't look like a number");
342 1.1 cgd continue;
343 1.1 cgd }
344 1.1 cgd sum = 0;
345 1.1 cgd
346 1.1 cgd if (!isdigit(*p))
347 1.1 cgd sum = lo - 1;
348 1.1 cgd else
349 1.1 cgd while (isdigit(*p)) {
350 1.1 cgd sum = 10 * sum + (*p - '0');
351 1.1 cgd ++p;
352 1.1 cgd }
353 1.1 cgd
354 1.1 cgd if (*p != ' ' && *p != '\t' && *p != NULL)
355 1.1 cgd sum = lo - 1;
356 1.1 cgd if (sum >= lo && sum <= hi)
357 1.1 cgd return sum;
358 1.1 cgd if (sum == lo - 1)
359 1.1 cgd msg("that doesn't look like a number, try again --> ");
360 1.1 cgd else
361 1.1 cgd msg("%d is not between %d and %d inclusive, try again --> ",
362 1.1 cgd sum, lo, hi);
363 1.1 cgd }
364 1.1 cgd }
365 1.1 cgd
366 1.1 cgd /*
367 1.1 cgd * msg:
368 1.1 cgd * Display a message at the top of the screen.
369 1.1 cgd */
370 1.1 cgd char Msgbuf[BUFSIZ] = { '\0' };
371 1.1 cgd
372 1.1 cgd int Mpos = 0;
373 1.1 cgd
374 1.1 cgd static int Newpos = 0;
375 1.1 cgd
376 1.1 cgd /* VARARGS1 */
377 1.2 mycroft msg(fmt,ap)
378 1.1 cgd char *fmt;
379 1.3 mycroft va_list ap;
380 1.1 cgd {
381 1.3 mycroft (void)vsprintf(&Msgbuf[Newpos], fmt, &ap);
382 1.1 cgd endmsg();
383 1.1 cgd }
384 1.1 cgd
385 1.1 cgd /*
386 1.1 cgd * addmsg:
387 1.1 cgd * Add things to the current message
388 1.1 cgd */
389 1.1 cgd /* VARARGS1 */
390 1.2 mycroft addmsg(fmt,ap)
391 1.1 cgd char *fmt;
392 1.3 mycroft va_list ap;
393 1.1 cgd {
394 1.3 mycroft (void)vsprintf(&Msgbuf[Newpos], fmt, &ap);
395 1.1 cgd }
396 1.1 cgd
397 1.1 cgd /*
398 1.1 cgd * endmsg:
399 1.1 cgd * Display a new msg.
400 1.1 cgd */
401 1.1 cgd
402 1.1 cgd int Lineno = 0;
403 1.1 cgd
404 1.1 cgd endmsg()
405 1.1 cgd {
406 1.1 cgd register int len;
407 1.1 cgd register char *mp, *omp;
408 1.1 cgd static int lastline = 0;
409 1.1 cgd
410 1.1 cgd /*
411 1.1 cgd * All messages should start with uppercase
412 1.1 cgd */
413 1.1 cgd mvaddch(lastline + Y_MSG_START, SCORE_X, ' ');
414 1.1 cgd if (islower(Msgbuf[0]) && Msgbuf[1] != ')')
415 1.1 cgd Msgbuf[0] = toupper(Msgbuf[0]);
416 1.1 cgd mp = Msgbuf;
417 1.1 cgd len = strlen(mp);
418 1.1 cgd if (len / MSG_X + Lineno >= MSG_Y) {
419 1.1 cgd while (Lineno < MSG_Y) {
420 1.1 cgd wmove(Msgwin, Lineno++, 0);
421 1.1 cgd wclrtoeol(Msgwin);
422 1.1 cgd }
423 1.1 cgd Lineno = 0;
424 1.1 cgd }
425 1.1 cgd mvaddch(Lineno + Y_MSG_START, SCORE_X, '*');
426 1.1 cgd lastline = Lineno;
427 1.1 cgd do {
428 1.1 cgd mvwaddstr(Msgwin, Lineno, 0, mp);
429 1.1 cgd if ((len = strlen(mp)) > MSG_X) {
430 1.1 cgd omp = mp;
431 1.1 cgd for (mp = &mp[MSG_X-1]; *mp != ' '; mp--)
432 1.1 cgd continue;
433 1.1 cgd while (*mp == ' ')
434 1.1 cgd mp--;
435 1.1 cgd mp++;
436 1.1 cgd wmove(Msgwin, Lineno, mp - omp);
437 1.1 cgd wclrtoeol(Msgwin);
438 1.1 cgd }
439 1.1 cgd if (++Lineno >= MSG_Y)
440 1.1 cgd Lineno = 0;
441 1.1 cgd } while (len > MSG_X);
442 1.1 cgd wclrtoeol(Msgwin);
443 1.1 cgd Mpos = len;
444 1.1 cgd Newpos = 0;
445 1.1 cgd wrefresh(Msgwin);
446 1.1 cgd refresh();
447 1.1 cgd wrefresh(Msgwin);
448 1.1 cgd }
449 1.1 cgd
450 1.1 cgd #ifdef notdef
451 1.1 cgd /*
452 1.1 cgd * doadd:
453 1.1 cgd * Perform an add onto the message buffer
454 1.1 cgd */
455 1.1 cgd doadd(fmt, args)
456 1.1 cgd char *fmt;
457 1.1 cgd int *args;
458 1.1 cgd {
459 1.1 cgd static FILE junk;
460 1.1 cgd
461 1.1 cgd /*
462 1.1 cgd * Do the printf into Msgbuf
463 1.1 cgd */
464 1.1 cgd junk._flag = _IOWRT + _IOSTRG;
465 1.1 cgd junk._ptr = &Msgbuf[Newpos];
466 1.1 cgd junk._cnt = 32767;
467 1.1 cgd _doprnt(fmt, args, &junk);
468 1.1 cgd putc('\0', &junk);
469 1.1 cgd Newpos = strlen(Msgbuf);
470 1.1 cgd }
471 1.1 cgd #endif
472 1.1 cgd
473 1.1 cgd /*
474 1.1 cgd * do_wait:
475 1.1 cgd * Wait for the user to type ' ' before doing anything else
476 1.1 cgd */
477 1.1 cgd do_wait()
478 1.1 cgd {
479 1.1 cgd register int line;
480 1.1 cgd static char prompt[] = { '-', '-', 'M', 'o', 'r', 'e', '-', '-', '\0' };
481 1.1 cgd
482 1.1 cgd if (Mpos + sizeof prompt < MSG_X)
483 1.1 cgd wmove(Msgwin, Lineno > 0 ? Lineno - 1 : MSG_Y - 1, Mpos);
484 1.1 cgd else {
485 1.1 cgd mvwaddch(Msgwin, Lineno, 0, ' ');
486 1.1 cgd wclrtoeol(Msgwin);
487 1.1 cgd if (++Lineno >= MSG_Y)
488 1.1 cgd Lineno = 0;
489 1.1 cgd }
490 1.1 cgd waddstr(Msgwin, prompt);
491 1.1 cgd wrefresh(Msgwin);
492 1.1 cgd wait_for(' ');
493 1.1 cgd }
494 1.1 cgd
495 1.1 cgd /*
496 1.1 cgd * wait_for
497 1.1 cgd * Sit around until the guy types the right key
498 1.1 cgd */
499 1.1 cgd wait_for(ch)
500 1.1 cgd register char ch;
501 1.1 cgd {
502 1.1 cgd register char c;
503 1.1 cgd
504 1.1 cgd if (ch == '\n')
505 1.1 cgd while ((c = readchar()) != '\n')
506 1.1 cgd continue;
507 1.1 cgd else
508 1.1 cgd while (readchar() != ch)
509 1.1 cgd continue;
510 1.1 cgd }
511 1.1 cgd
512 1.1 cgd /*
513 1.1 cgd * readchar:
514 1.1 cgd * Reads and returns a character, checking for gross input errors
515 1.1 cgd */
516 1.1 cgd readchar()
517 1.1 cgd {
518 1.1 cgd register int cnt, y, x;
519 1.1 cgd auto char c;
520 1.1 cgd
521 1.1 cgd over:
522 1.1 cgd cnt = 0;
523 1.1 cgd while (read(0, &c, 1) <= 0)
524 1.1 cgd if (cnt++ > 100) { /* if we are getting infinite EOFs */
525 1.1 cgd bye(); /* quit the game */
526 1.1 cgd exit(1);
527 1.1 cgd }
528 1.1 cgd if (c == CTRL(L)) {
529 1.1 cgd wrefresh(curscr);
530 1.1 cgd goto over;
531 1.1 cgd }
532 1.1 cgd if (c == '\r')
533 1.1 cgd return '\n';
534 1.1 cgd else
535 1.1 cgd return c;
536 1.1 cgd }
537 1.1 cgd
538 1.1 cgd /*
539 1.1 cgd * getline:
540 1.1 cgd * Reads the next line up to '\n' or EOF. Multiple spaces are
541 1.1 cgd * compressed to one space; a space is inserted before a ','
542 1.1 cgd */
543 1.1 cgd char *
544 1.1 cgd getline()
545 1.1 cgd {
546 1.1 cgd register char *sp;
547 1.1 cgd register int c, oy, ox;
548 1.1 cgd register WINDOW *oscr;
549 1.1 cgd
550 1.1 cgd oscr = stdscr;
551 1.1 cgd stdscr = Msgwin;
552 1.1 cgd getyx(stdscr, oy, ox);
553 1.1 cgd refresh();
554 1.1 cgd /*
555 1.1 cgd * loop reading in the string, and put it in a temporary buffer
556 1.1 cgd */
557 1.1 cgd for (sp = linebuf; (c = readchar()) != '\n'; clrtoeol(), refresh()) {
558 1.1 cgd if (c == -1)
559 1.1 cgd continue;
560 1.1 cgd else if (c == erasechar()) { /* process erase character */
561 1.1 cgd if (sp > linebuf) {
562 1.1 cgd register int i;
563 1.1 cgd
564 1.1 cgd sp--;
565 1.1 cgd for (i = strlen(unctrl(*sp)); i; i--)
566 1.1 cgd addch('\b');
567 1.1 cgd }
568 1.1 cgd continue;
569 1.1 cgd }
570 1.1 cgd else if (c == killchar()) { /* process kill character */
571 1.1 cgd sp = linebuf;
572 1.1 cgd move(oy, ox);
573 1.1 cgd continue;
574 1.1 cgd }
575 1.1 cgd else if (sp == linebuf && c == ' ')
576 1.1 cgd continue;
577 1.1 cgd if (sp >= &linebuf[LINESIZE-1] || !(isprint(c) || c == ' '))
578 1.1 cgd putchar(CTRL(G));
579 1.1 cgd else {
580 1.1 cgd if (islower(c))
581 1.1 cgd c = toupper(c);
582 1.1 cgd *sp++ = c;
583 1.1 cgd addstr(unctrl(c));
584 1.1 cgd Mpos++;
585 1.1 cgd }
586 1.1 cgd }
587 1.1 cgd *sp = '\0';
588 1.1 cgd stdscr = oscr;
589 1.1 cgd return linebuf;
590 1.1 cgd }
591 1.1 cgd
592 1.1 cgd rint()
593 1.1 cgd {
594 1.1 cgd bye();
595 1.1 cgd exit(1);
596 1.1 cgd }
597 1.1 cgd
598 1.1 cgd /*
599 1.1 cgd * bye:
600 1.1 cgd * Leave the program, cleaning things up as we go.
601 1.1 cgd */
602 1.1 cgd bye()
603 1.1 cgd {
604 1.1 cgd signal(SIGINT, SIG_IGN);
605 1.1 cgd mvcur(0, COLS - 1, LINES - 1, 0);
606 1.1 cgd fflush(stdout);
607 1.1 cgd endwin();
608 1.1 cgd putchar('\n');
609 1.1 cgd }
610