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