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