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