xref: /netbsd-src/usr.bin/talk/display.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: display.c,v 1.5 1997/10/20 00:23:17 lukem 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.5 1997/10/20 00:23:17 lukem 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 (win->x_col == COLS-1) {
143 			/* check for wraparound */
144 			xscroll(win, 0);
145 		}
146 		if ( !isprint((u_char)*text) && *text != '\t') {
147 			waddch(win->x_win, '^');
148 			getyx(win->x_win, win->x_line, win->x_col);
149 			if (win->x_col == COLS-1) /* check for wraparound */
150 				xscroll(win, 0);
151 			cch = (*text & 63) + 64;
152 			waddch(win->x_win, cch);
153 		} else
154 			waddch(win->x_win, *text);
155 		getyx(win->x_win, win->x_line, win->x_col);
156 		text++;
157 	}
158 	wrefresh(win->x_win);
159 }
160 
161 /*
162  * Read the character at the indicated position in win
163  */
164 int
165 readwin(win, line, col)
166 	WINDOW *win;
167 	int line, col;
168 {
169 	int oldline, oldcol;
170 	int c;
171 
172 	getyx(win, oldline, oldcol);
173 	wmove(win, line, col);
174 	c = winch(win);
175 	wmove(win, oldline, oldcol);
176 	return (c);
177 }
178 
179 /*
180  * Scroll a window, blanking out the line following the current line
181  * so that the current position is obvious
182  */
183 void
184 xscroll(win, flag)
185 	xwin_t *win;
186 	int flag;
187 {
188 
189 	if (flag == -1) {
190 		wmove(win->x_win, 0, 0);
191 		win->x_line = 0;
192 		win->x_col = 0;
193 		return;
194 	}
195 	win->x_line = (win->x_line + 1) % win->x_nlines;
196 	win->x_col = 0;
197 	wmove(win->x_win, win->x_line, win->x_col);
198 	wclrtoeol(win->x_win);
199 	wmove(win->x_win, (win->x_line + 1) % win->x_nlines, win->x_col);
200 	wclrtoeol(win->x_win);
201 	wmove(win->x_win, win->x_line, win->x_col);
202 }
203