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