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