xref: /freebsd/usr.bin/talk/display.c (revision a316b26e50bbed7cf655fbba726ab87d8ab7599d)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static char sccsid[] = "@(#)display.c	8.1 (Berkeley) 6/6/93";
36 #endif /* not lint */
37 
38 /*
39  * The window 'manager', initializes curses and handles the actual
40  * displaying of text
41  */
42 #include "talk.h"
43 #include <ctype.h>
44 
45 xwin_t	my_win;
46 xwin_t	his_win;
47 WINDOW	*line_win;
48 
49 int	curses_initialized = 0;
50 
51 /*
52  * max HAS to be a function, it is called with
53  * a argument of the form --foo at least once.
54  */
55 max(a,b)
56 	int a, b;
57 {
58 
59 	return (a > b ? a : b);
60 }
61 
62 /*
63  * Display some text on somebody's window, processing some control
64  * characters while we are at it.
65  */
66 display(win, text, size)
67 	register xwin_t *win;
68 	register unsigned char *text;
69 	int size;
70 {
71 	register int i;
72 	char cch;
73 
74 	for (i = 0; i < size; i++) {
75 		if (*text == '\n' || *text == '\r') {
76 			waddch(win->x_win, '\n');
77 			getyx(win->x_win, win->x_line, win->x_col);
78 			text++;
79 			continue;
80 		}
81 		/* erase character */
82 		if (   *text == win->cerase
83 		    || *text == 010     /* BS */
84 		    || *text == 0177    /* DEL */
85 		   ) {
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 		    || *text == 027     /* ^W */
101 		   ) {
102 			int endcol, xcol, i, c;
103 
104 			endcol = win->x_col;
105 			xcol = endcol - 1;
106 			while (xcol >= 0) {
107 				c = readwin(win->x_win, win->x_line, xcol);
108 				if (c != ' ')
109 					break;
110 				xcol--;
111 			}
112 			while (xcol >= 0) {
113 				c = readwin(win->x_win, win->x_line, xcol);
114 				if (c == ' ')
115 					break;
116 				xcol--;
117 			}
118 			wmove(win->x_win, win->x_line, xcol + 1);
119 			for (i = xcol + 1; i < endcol; i++)
120 				waddch(win->x_win, ' ');
121 			wmove(win->x_win, win->x_line, xcol + 1);
122 			getyx(win->x_win, win->x_line, win->x_col);
123 			text++;
124 			continue;
125 		}
126 		/* line kill */
127 		if (   *text == win->kill
128 		    || *text == 025     /* ^U */
129 		   ) {
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 == '\7') {
143 			_putchar(*text);
144 			text++;
145 			continue;
146 		}
147 		if (!isprint(*text) && *text != '\t') {
148 			waddch(win->x_win, '^');
149 			getyx(win->x_win, win->x_line, win->x_col);
150 			cch = (*text & 63) + 64;
151 			waddch(win->x_win, cch);
152 		} else
153 			waddch(win->x_win, *text);
154 		getyx(win->x_win, win->x_line, win->x_col);
155 		text++;
156 	}
157 	wrefresh(win->x_win);
158 }
159 
160 /*
161  * Read the character at the indicated position in win
162  */
163 readwin(win, line, col)
164 	WINDOW *win;
165 {
166 	int oldline, oldcol;
167 	register int c;
168 
169 	getyx(win, oldline, oldcol);
170 	wmove(win, line, col);
171 	c = winch(win);
172 	wmove(win, oldline, oldcol);
173 	return (c);
174 }
175