xref: /original-bsd/usr.bin/window/wwgets.c (revision b424313c)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)wwgets.c	3.11 (Berkeley) 02/21/88";
15 #endif /* not lint */
16 
17 #include "ww.h"
18 #include "char.h"
19 
20 wwgets(buf, n, w)
21 char *buf;
22 int n;
23 register struct ww *w;
24 {
25 	register char *p = buf;
26 	register char c;
27 	char uc = w->ww_unctrl;
28 
29 	w->ww_unctrl = 0;
30 	for (;;) {
31 		wwcurtowin(w);
32 		while ((c = wwgetc()) < 0)
33 			wwiomux();
34 		if (c == wwoldtty.ww_sgttyb.sg_erase) {
35 			if (p > buf)
36 				rub(*--p, w);
37 		} else if (c == wwoldtty.ww_sgttyb.sg_kill) {
38 			while (p > buf)
39 				rub(*--p, w);
40 		} else if (c == wwoldtty.ww_ltchars.t_werasc) {
41 			while (--p >= buf && (*p == ' ' || *p == '\t'))
42 				rub(*p, w);
43 			while (p >= buf && *p != ' ' && *p != '\t')
44 				rub(*p--, w);
45 			p++;
46 		} else if (c == '\r' || c == '\n') {
47 			break;
48 		} else {
49 			if (p >= buf + n - 1)
50 				wwputc(ctrl('g'), w);
51 			else
52 				wwputs(unctrl(*p++ = c), w);
53 		}
54 	}
55 	*p = 0;
56 	w->ww_unctrl = uc;
57 }
58 
59 static
60 rub(c, w)
61 struct ww *w;
62 {
63 	register i;
64 
65 	for (i = strlen(unctrl(c)); --i >= 0;)
66 		(void) wwwrite(w, "\b \b", 3);
67 }
68