xref: /netbsd-src/usr.bin/talk/display.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: display.c,v 1.6 1999/08/13 13:52:59 proff Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)display.c	8.1 (Berkeley) 6/6/93";
40 #endif
41 __RCSID("$NetBSD: display.c,v 1.6 1999/08/13 13:52:59 proff Exp $");
42 #endif /* not lint */
43 
44 /*
45  * The window 'manager', initializes curses and handles the actual
46  * displaying of text
47  */
48 #include "talk.h"
49 #include <ctype.h>
50 
51 xwin_t	my_win;
52 xwin_t	his_win;
53 WINDOW	*line_win;
54 
55 int	curses_initialized = 0;
56 
57 /*
58  * max HAS to be a function, it is called with
59  * a argument of the form --foo at least once.
60  */
61 int
62 max(a,b)
63 	int a, b;
64 {
65 
66 	return (a > b ? a : b);
67 }
68 
69 /*
70  * Display some text on somebody's window, processing some control
71  * characters while we are at it.
72  */
73 void
74 display(win, text, size)
75 	xwin_t *win;
76 	char *text;
77 	int size;
78 {
79 	int i;
80 	char cch;
81 
82 	for (i = 0; i < size; i++) {
83 		if (*text == '\n') {
84 			xscroll(win, 0);
85 			text++;
86 			continue;
87 		}
88 		/* erase character */
89 		if (*text == win->cerase) {
90 			wmove(win->x_win, win->x_line, max(--win->x_col, 0));
91 			getyx(win->x_win, win->x_line, win->x_col);
92 			waddch(win->x_win, ' ');
93 			wmove(win->x_win, win->x_line, win->x_col);
94 			getyx(win->x_win, win->x_line, win->x_col);
95 			text++;
96 			continue;
97 		}
98 		/*
99 		 * On word erase search backwards until we find
100 		 * the beginning of a word or the beginning of
101 		 * the line.
102 		 */
103 		if (*text == win->werase) {
104 			int endcol, xcol, i, c;
105 
106 			endcol = win->x_col;
107 			xcol = endcol - 1;
108 			while (xcol >= 0) {
109 				c = readwin(win->x_win, win->x_line, xcol);
110 				if (c != ' ')
111 					break;
112 				xcol--;
113 			}
114 			while (xcol >= 0) {
115 				c = readwin(win->x_win, win->x_line, xcol);
116 				if (c == ' ')
117 					break;
118 				xcol--;
119 			}
120 			wmove(win->x_win, win->x_line, xcol + 1);
121 			for (i = xcol + 1; i < endcol; i++)
122 				waddch(win->x_win, ' ');
123 			wmove(win->x_win, win->x_line, xcol + 1);
124 			getyx(win->x_win, win->x_line, win->x_col);
125 			text++;
126 			continue;
127 		}
128 		/* line kill */
129 		if (*text == win->kill) {
130 			wmove(win->x_win, win->x_line, 0);
131 			wclrtoeol(win->x_win);
132 			getyx(win->x_win, win->x_line, win->x_col);
133 			text++;
134 			continue;
135 		}
136 		if (*text == '\f') {
137 			if (win == &my_win)
138 				wrefresh(curscr);
139 			text++;
140 			continue;
141 		}
142 		if (*text == '\07') {
143 			beep();
144 			text++;
145 			continue;
146 		}
147 		if (win->x_col == COLS-1) {
148 			/* check for wraparound */
149 			xscroll(win, 0);
150 		}
151 		if ( !isprint((u_char)*text) && *text != '\t') {
152 			waddch(win->x_win, '^');
153 			getyx(win->x_win, win->x_line, win->x_col);
154 			if (win->x_col == COLS-1) /* check for wraparound */
155 				xscroll(win, 0);
156 			cch = (*text & 63) + 64;
157 			waddch(win->x_win, cch);
158 		} else
159 			waddch(win->x_win, *text);
160 		getyx(win->x_win, win->x_line, win->x_col);
161 		text++;
162 	}
163 	wrefresh(win->x_win);
164 }
165 
166 /*
167  * Read the character at the indicated position in win
168  */
169 int
170 readwin(win, line, col)
171 	WINDOW *win;
172 	int line, col;
173 {
174 	int oldline, oldcol;
175 	int c;
176 
177 	getyx(win, oldline, oldcol);
178 	wmove(win, line, col);
179 	c = winch(win);
180 	wmove(win, oldline, oldcol);
181 	return (c);
182 }
183 
184 /*
185  * Scroll a window, blanking out the line following the current line
186  * so that the current position is obvious
187  */
188 void
189 xscroll(win, flag)
190 	xwin_t *win;
191 	int flag;
192 {
193 
194 	if (flag == -1) {
195 		wmove(win->x_win, 0, 0);
196 		win->x_line = 0;
197 		win->x_col = 0;
198 		return;
199 	}
200 	win->x_line = (win->x_line + 1) % win->x_nlines;
201 	win->x_col = 0;
202 	wmove(win->x_win, win->x_line, win->x_col);
203 	wclrtoeol(win->x_win);
204 	wmove(win->x_win, (win->x_line + 1) % win->x_nlines, win->x_col);
205 	wclrtoeol(win->x_win);
206 	wmove(win->x_win, win->x_line, win->x_col);
207 }
208