io.c revision 1.1 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.1 cgd msg(fmt)
378 1.1 cgd char *fmt;
379 1.1 cgd {
380 1.1 cgd va_list ap;
381 1.1 cgd
382 1.1 cgd va_start(ap, fmt);
383 1.1 cgd (void)vsprintf(&Msgbuf[Newpos], fmt, ap);
384 1.1 cgd va_end(ap);
385 1.1 cgd endmsg();
386 1.1 cgd }
387 1.1 cgd
388 1.1 cgd /*
389 1.1 cgd * addmsg:
390 1.1 cgd * Add things to the current message
391 1.1 cgd */
392 1.1 cgd /* VARARGS1 */
393 1.1 cgd addmsg(fmt)
394 1.1 cgd char *fmt;
395 1.1 cgd {
396 1.1 cgd va_list ap;
397 1.1 cgd
398 1.1 cgd va_start(ap, fmt);
399 1.1 cgd (void)vsprintf(&Msgbuf[Newpos], fmt, ap);
400 1.1 cgd va_end(ap);
401 1.1 cgd }
402 1.1 cgd
403 1.1 cgd /*
404 1.1 cgd * endmsg:
405 1.1 cgd * Display a new msg.
406 1.1 cgd */
407 1.1 cgd
408 1.1 cgd int Lineno = 0;
409 1.1 cgd
410 1.1 cgd endmsg()
411 1.1 cgd {
412 1.1 cgd register int len;
413 1.1 cgd register char *mp, *omp;
414 1.1 cgd static int lastline = 0;
415 1.1 cgd
416 1.1 cgd /*
417 1.1 cgd * All messages should start with uppercase
418 1.1 cgd */
419 1.1 cgd mvaddch(lastline + Y_MSG_START, SCORE_X, ' ');
420 1.1 cgd if (islower(Msgbuf[0]) && Msgbuf[1] != ')')
421 1.1 cgd Msgbuf[0] = toupper(Msgbuf[0]);
422 1.1 cgd mp = Msgbuf;
423 1.1 cgd len = strlen(mp);
424 1.1 cgd if (len / MSG_X + Lineno >= MSG_Y) {
425 1.1 cgd while (Lineno < MSG_Y) {
426 1.1 cgd wmove(Msgwin, Lineno++, 0);
427 1.1 cgd wclrtoeol(Msgwin);
428 1.1 cgd }
429 1.1 cgd Lineno = 0;
430 1.1 cgd }
431 1.1 cgd mvaddch(Lineno + Y_MSG_START, SCORE_X, '*');
432 1.1 cgd lastline = Lineno;
433 1.1 cgd do {
434 1.1 cgd mvwaddstr(Msgwin, Lineno, 0, mp);
435 1.1 cgd if ((len = strlen(mp)) > MSG_X) {
436 1.1 cgd omp = mp;
437 1.1 cgd for (mp = &mp[MSG_X-1]; *mp != ' '; mp--)
438 1.1 cgd continue;
439 1.1 cgd while (*mp == ' ')
440 1.1 cgd mp--;
441 1.1 cgd mp++;
442 1.1 cgd wmove(Msgwin, Lineno, mp - omp);
443 1.1 cgd wclrtoeol(Msgwin);
444 1.1 cgd }
445 1.1 cgd if (++Lineno >= MSG_Y)
446 1.1 cgd Lineno = 0;
447 1.1 cgd } while (len > MSG_X);
448 1.1 cgd wclrtoeol(Msgwin);
449 1.1 cgd Mpos = len;
450 1.1 cgd Newpos = 0;
451 1.1 cgd wrefresh(Msgwin);
452 1.1 cgd refresh();
453 1.1 cgd wrefresh(Msgwin);
454 1.1 cgd }
455 1.1 cgd
456 1.1 cgd #ifdef notdef
457 1.1 cgd /*
458 1.1 cgd * doadd:
459 1.1 cgd * Perform an add onto the message buffer
460 1.1 cgd */
461 1.1 cgd doadd(fmt, args)
462 1.1 cgd char *fmt;
463 1.1 cgd int *args;
464 1.1 cgd {
465 1.1 cgd static FILE junk;
466 1.1 cgd
467 1.1 cgd /*
468 1.1 cgd * Do the printf into Msgbuf
469 1.1 cgd */
470 1.1 cgd junk._flag = _IOWRT + _IOSTRG;
471 1.1 cgd junk._ptr = &Msgbuf[Newpos];
472 1.1 cgd junk._cnt = 32767;
473 1.1 cgd _doprnt(fmt, args, &junk);
474 1.1 cgd putc('\0', &junk);
475 1.1 cgd Newpos = strlen(Msgbuf);
476 1.1 cgd }
477 1.1 cgd #endif
478 1.1 cgd
479 1.1 cgd /*
480 1.1 cgd * do_wait:
481 1.1 cgd * Wait for the user to type ' ' before doing anything else
482 1.1 cgd */
483 1.1 cgd do_wait()
484 1.1 cgd {
485 1.1 cgd register int line;
486 1.1 cgd static char prompt[] = { '-', '-', 'M', 'o', 'r', 'e', '-', '-', '\0' };
487 1.1 cgd
488 1.1 cgd if (Mpos + sizeof prompt < MSG_X)
489 1.1 cgd wmove(Msgwin, Lineno > 0 ? Lineno - 1 : MSG_Y - 1, Mpos);
490 1.1 cgd else {
491 1.1 cgd mvwaddch(Msgwin, Lineno, 0, ' ');
492 1.1 cgd wclrtoeol(Msgwin);
493 1.1 cgd if (++Lineno >= MSG_Y)
494 1.1 cgd Lineno = 0;
495 1.1 cgd }
496 1.1 cgd waddstr(Msgwin, prompt);
497 1.1 cgd wrefresh(Msgwin);
498 1.1 cgd wait_for(' ');
499 1.1 cgd }
500 1.1 cgd
501 1.1 cgd /*
502 1.1 cgd * wait_for
503 1.1 cgd * Sit around until the guy types the right key
504 1.1 cgd */
505 1.1 cgd wait_for(ch)
506 1.1 cgd register char ch;
507 1.1 cgd {
508 1.1 cgd register char c;
509 1.1 cgd
510 1.1 cgd if (ch == '\n')
511 1.1 cgd while ((c = readchar()) != '\n')
512 1.1 cgd continue;
513 1.1 cgd else
514 1.1 cgd while (readchar() != ch)
515 1.1 cgd continue;
516 1.1 cgd }
517 1.1 cgd
518 1.1 cgd /*
519 1.1 cgd * readchar:
520 1.1 cgd * Reads and returns a character, checking for gross input errors
521 1.1 cgd */
522 1.1 cgd readchar()
523 1.1 cgd {
524 1.1 cgd register int cnt, y, x;
525 1.1 cgd auto char c;
526 1.1 cgd
527 1.1 cgd over:
528 1.1 cgd cnt = 0;
529 1.1 cgd while (read(0, &c, 1) <= 0)
530 1.1 cgd if (cnt++ > 100) { /* if we are getting infinite EOFs */
531 1.1 cgd bye(); /* quit the game */
532 1.1 cgd exit(1);
533 1.1 cgd }
534 1.1 cgd if (c == CTRL(L)) {
535 1.1 cgd wrefresh(curscr);
536 1.1 cgd goto over;
537 1.1 cgd }
538 1.1 cgd if (c == '\r')
539 1.1 cgd return '\n';
540 1.1 cgd else
541 1.1 cgd return c;
542 1.1 cgd }
543 1.1 cgd
544 1.1 cgd /*
545 1.1 cgd * getline:
546 1.1 cgd * Reads the next line up to '\n' or EOF. Multiple spaces are
547 1.1 cgd * compressed to one space; a space is inserted before a ','
548 1.1 cgd */
549 1.1 cgd char *
550 1.1 cgd getline()
551 1.1 cgd {
552 1.1 cgd register char *sp;
553 1.1 cgd register int c, oy, ox;
554 1.1 cgd register WINDOW *oscr;
555 1.1 cgd
556 1.1 cgd oscr = stdscr;
557 1.1 cgd stdscr = Msgwin;
558 1.1 cgd getyx(stdscr, oy, ox);
559 1.1 cgd refresh();
560 1.1 cgd /*
561 1.1 cgd * loop reading in the string, and put it in a temporary buffer
562 1.1 cgd */
563 1.1 cgd for (sp = linebuf; (c = readchar()) != '\n'; clrtoeol(), refresh()) {
564 1.1 cgd if (c == -1)
565 1.1 cgd continue;
566 1.1 cgd else if (c == erasechar()) { /* process erase character */
567 1.1 cgd if (sp > linebuf) {
568 1.1 cgd register int i;
569 1.1 cgd
570 1.1 cgd sp--;
571 1.1 cgd for (i = strlen(unctrl(*sp)); i; i--)
572 1.1 cgd addch('\b');
573 1.1 cgd }
574 1.1 cgd continue;
575 1.1 cgd }
576 1.1 cgd else if (c == killchar()) { /* process kill character */
577 1.1 cgd sp = linebuf;
578 1.1 cgd move(oy, ox);
579 1.1 cgd continue;
580 1.1 cgd }
581 1.1 cgd else if (sp == linebuf && c == ' ')
582 1.1 cgd continue;
583 1.1 cgd if (sp >= &linebuf[LINESIZE-1] || !(isprint(c) || c == ' '))
584 1.1 cgd putchar(CTRL(G));
585 1.1 cgd else {
586 1.1 cgd if (islower(c))
587 1.1 cgd c = toupper(c);
588 1.1 cgd *sp++ = c;
589 1.1 cgd addstr(unctrl(c));
590 1.1 cgd Mpos++;
591 1.1 cgd }
592 1.1 cgd }
593 1.1 cgd *sp = '\0';
594 1.1 cgd stdscr = oscr;
595 1.1 cgd return linebuf;
596 1.1 cgd }
597 1.1 cgd
598 1.1 cgd rint()
599 1.1 cgd {
600 1.1 cgd bye();
601 1.1 cgd exit(1);
602 1.1 cgd }
603 1.1 cgd
604 1.1 cgd /*
605 1.1 cgd * bye:
606 1.1 cgd * Leave the program, cleaning things up as we go.
607 1.1 cgd */
608 1.1 cgd bye()
609 1.1 cgd {
610 1.1 cgd signal(SIGINT, SIG_IGN);
611 1.1 cgd mvcur(0, COLS - 1, LINES - 1, 0);
612 1.1 cgd fflush(stdout);
613 1.1 cgd endwin();
614 1.1 cgd putchar('\n');
615 1.1 cgd }
616