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