xref: /netbsd-src/usr.bin/talk/display.c (revision 1f2744e6e4915c9da2a3f980279398c4cf7d5e6d)
1 /*	$NetBSD: display.c,v 1.3 1994/12/09 02:14:13 jtc 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 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)display.c	8.1 (Berkeley) 6/6/93";
39 #endif
40 static char rcsid[] = "$NetBSD: display.c,v 1.3 1994/12/09 02:14:13 jtc Exp $";
41 #endif /* not lint */
42 
43 /*
44  * The window 'manager', initializes curses and handles the actual
45  * displaying of text
46  */
47 #include "talk.h"
48 
49 xwin_t	my_win;
50 xwin_t	his_win;
51 WINDOW	*line_win;
52 
53 int	curses_initialized = 0;
54 
55 /*
56  * max HAS to be a function, it is called with
57  * a argument of the form --foo at least once.
58  */
59 max(a,b)
60 	int a, b;
61 {
62 
63 	return (a > b ? a : b);
64 }
65 
66 /*
67  * Display some text on somebody's window, processing some control
68  * characters while we are at it.
69  */
70 display(win, text, size)
71 	register xwin_t *win;
72 	register char *text;
73 	int size;
74 {
75 	register int i;
76 	char cch;
77 
78 	for (i = 0; i < size; i++) {
79 		if (*text == '\n') {
80 			xscroll(win, 0);
81 			text++;
82 			continue;
83 		}
84 		/* erase character */
85 		if (*text == win->cerase) {
86 			wmove(win->x_win, win->x_line, max(--win->x_col, 0));
87 			getyx(win->x_win, win->x_line, win->x_col);
88 			waddch(win->x_win, ' ');
89 			wmove(win->x_win, win->x_line, win->x_col);
90 			getyx(win->x_win, win->x_line, win->x_col);
91 			text++;
92 			continue;
93 		}
94 		/*
95 		 * On word erase search backwards until we find
96 		 * the beginning of a word or the beginning of
97 		 * the line.
98 		 */
99 		if (*text == win->werase) {
100 			int endcol, xcol, i, c;
101 
102 			endcol = win->x_col;
103 			xcol = endcol - 1;
104 			while (xcol >= 0) {
105 				c = readwin(win->x_win, win->x_line, xcol);
106 				if (c != ' ')
107 					break;
108 				xcol--;
109 			}
110 			while (xcol >= 0) {
111 				c = readwin(win->x_win, win->x_line, xcol);
112 				if (c == ' ')
113 					break;
114 				xcol--;
115 			}
116 			wmove(win->x_win, win->x_line, xcol + 1);
117 			for (i = xcol + 1; i < endcol; i++)
118 				waddch(win->x_win, ' ');
119 			wmove(win->x_win, win->x_line, xcol + 1);
120 			getyx(win->x_win, win->x_line, win->x_col);
121 			text++;
122 			continue;
123 		}
124 		/* line kill */
125 		if (*text == win->kill) {
126 			wmove(win->x_win, win->x_line, 0);
127 			wclrtoeol(win->x_win);
128 			getyx(win->x_win, win->x_line, win->x_col);
129 			text++;
130 			continue;
131 		}
132 		if (*text == '\f') {
133 			if (win == &my_win)
134 				wrefresh(curscr);
135 			text++;
136 			continue;
137 		}
138 		if (win->x_col == COLS-1) {
139 			/* check for wraparound */
140 			xscroll(win, 0);
141 		}
142 		if (*text < ' ' && *text != '\t') {
143 			waddch(win->x_win, '^');
144 			getyx(win->x_win, win->x_line, win->x_col);
145 			if (win->x_col == COLS-1) /* check for wraparound */
146 				xscroll(win, 0);
147 			cch = (*text & 63) + 64;
148 			waddch(win->x_win, cch);
149 		} else
150 			waddch(win->x_win, *text);
151 		getyx(win->x_win, win->x_line, win->x_col);
152 		text++;
153 	}
154 	wrefresh(win->x_win);
155 }
156 
157 /*
158  * Read the character at the indicated position in win
159  */
160 readwin(win, line, col)
161 	WINDOW *win;
162 {
163 	int oldline, oldcol;
164 	register int c;
165 
166 	getyx(win, oldline, oldcol);
167 	wmove(win, line, col);
168 	c = winch(win);
169 	wmove(win, oldline, oldcol);
170 	return (c);
171 }
172 
173 /*
174  * Scroll a window, blanking out the line following the current line
175  * so that the current position is obvious
176  */
177 xscroll(win, flag)
178 	register xwin_t *win;
179 	int flag;
180 {
181 
182 	if (flag == -1) {
183 		wmove(win->x_win, 0, 0);
184 		win->x_line = 0;
185 		win->x_col = 0;
186 		return;
187 	}
188 	win->x_line = (win->x_line + 1) % win->x_nlines;
189 	win->x_col = 0;
190 	wmove(win->x_win, win->x_line, win->x_col);
191 	wclrtoeol(win->x_win);
192 	wmove(win->x_win, (win->x_line + 1) % win->x_nlines, win->x_col);
193 	wclrtoeol(win->x_win);
194 	wmove(win->x_win, win->x_line, win->x_col);
195 }
196