xref: /titanic_51/usr/src/cmd/cmd-inet/usr.bin/talk/display.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1994 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  * contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  * The window 'manager', initializes curses and handles the actual
44*7c478bd9Sstevel@tonic-gate  * displaying of text
45*7c478bd9Sstevel@tonic-gate  */
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate #include "talk.h"
48*7c478bd9Sstevel@tonic-gate #include <ctype.h>
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate static int readwin(WINDOW *, int, int);
51*7c478bd9Sstevel@tonic-gate static void xscroll(register xwin_t *, int);
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate xwin_t my_win;
54*7c478bd9Sstevel@tonic-gate xwin_t rem_win;
55*7c478bd9Sstevel@tonic-gate WINDOW *line_win;
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate int curses_initialized = 0;
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate /*
60*7c478bd9Sstevel@tonic-gate  * max HAS to be a function, it is called with
61*7c478bd9Sstevel@tonic-gate  * a argument of the form --foo at least once.
62*7c478bd9Sstevel@tonic-gate  */
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate static int
65*7c478bd9Sstevel@tonic-gate max(a, b)
66*7c478bd9Sstevel@tonic-gate int a, b;
67*7c478bd9Sstevel@tonic-gate {
68*7c478bd9Sstevel@tonic-gate 	if (a > b) {
69*7c478bd9Sstevel@tonic-gate 	return (a);
70*7c478bd9Sstevel@tonic-gate 	} else {
71*7c478bd9Sstevel@tonic-gate 	return (b);
72*7c478bd9Sstevel@tonic-gate 	}
73*7c478bd9Sstevel@tonic-gate }
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate /*
76*7c478bd9Sstevel@tonic-gate  * Display some text on somebody's window, processing some control
77*7c478bd9Sstevel@tonic-gate  * characters while we are at it.
78*7c478bd9Sstevel@tonic-gate  */
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate int
81*7c478bd9Sstevel@tonic-gate display(win, text, size)
82*7c478bd9Sstevel@tonic-gate register xwin_t *win;
83*7c478bd9Sstevel@tonic-gate register char *text;
84*7c478bd9Sstevel@tonic-gate int size;
85*7c478bd9Sstevel@tonic-gate {
86*7c478bd9Sstevel@tonic-gate 	register int i;
87*7c478bd9Sstevel@tonic-gate 	int mb_cur_max = MB_CUR_MAX;
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
90*7c478bd9Sstevel@tonic-gate 	int itext;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	if (*text == '\n'|| *text == '\r') {
93*7c478bd9Sstevel@tonic-gate 		xscroll(win, 0);
94*7c478bd9Sstevel@tonic-gate 		text++;
95*7c478bd9Sstevel@tonic-gate 		continue;
96*7c478bd9Sstevel@tonic-gate 	}
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 		/* erase character */
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	if (*text == win->cerase) {
101*7c478bd9Sstevel@tonic-gate 		wmove(win->x_win, win->x_line, max(--win->x_col, 0));
102*7c478bd9Sstevel@tonic-gate 		getyx(win->x_win, win->x_line, win->x_col);
103*7c478bd9Sstevel@tonic-gate 		waddch(win->x_win, ' ');
104*7c478bd9Sstevel@tonic-gate 		wmove(win->x_win, win->x_line, win->x_col);
105*7c478bd9Sstevel@tonic-gate 		getyx(win->x_win, win->x_line, win->x_col);
106*7c478bd9Sstevel@tonic-gate 		text++;
107*7c478bd9Sstevel@tonic-gate 		continue;
108*7c478bd9Sstevel@tonic-gate 	}
109*7c478bd9Sstevel@tonic-gate 	/*
110*7c478bd9Sstevel@tonic-gate 	 * On word erase search backwards until we find
111*7c478bd9Sstevel@tonic-gate 	 * the beginning of a word or the beginning of
112*7c478bd9Sstevel@tonic-gate 	 * the line.
113*7c478bd9Sstevel@tonic-gate 	 */
114*7c478bd9Sstevel@tonic-gate 	if (*text == win->werase) {
115*7c478bd9Sstevel@tonic-gate 		int endcol, xcol, i, c;
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 		endcol = win->x_col;
118*7c478bd9Sstevel@tonic-gate 		xcol = endcol - 1;
119*7c478bd9Sstevel@tonic-gate 		while (xcol >= 0) {
120*7c478bd9Sstevel@tonic-gate 		c = readwin(win->x_win, win->x_line, xcol);
121*7c478bd9Sstevel@tonic-gate 		if (c != ' ')
122*7c478bd9Sstevel@tonic-gate 			break;
123*7c478bd9Sstevel@tonic-gate 		xcol--;
124*7c478bd9Sstevel@tonic-gate 		}
125*7c478bd9Sstevel@tonic-gate 		while (xcol >= 0) {
126*7c478bd9Sstevel@tonic-gate 		c = readwin(win->x_win, win->x_line, xcol);
127*7c478bd9Sstevel@tonic-gate 		if (c == ' ')
128*7c478bd9Sstevel@tonic-gate 			break;
129*7c478bd9Sstevel@tonic-gate 		xcol--;
130*7c478bd9Sstevel@tonic-gate 		}
131*7c478bd9Sstevel@tonic-gate 		wmove(win->x_win, win->x_line, xcol + 1);
132*7c478bd9Sstevel@tonic-gate 		for (i = xcol + 1; i < endcol; i++)
133*7c478bd9Sstevel@tonic-gate 		waddch(win->x_win, ' ');
134*7c478bd9Sstevel@tonic-gate 		wmove(win->x_win, win->x_line, xcol + 1);
135*7c478bd9Sstevel@tonic-gate 		getyx(win->x_win, win->x_line, win->x_col);
136*7c478bd9Sstevel@tonic-gate 		continue;
137*7c478bd9Sstevel@tonic-gate 	}
138*7c478bd9Sstevel@tonic-gate 		/* line kill */
139*7c478bd9Sstevel@tonic-gate 	if (*text == win->kill) {
140*7c478bd9Sstevel@tonic-gate 		wmove(win->x_win, win->x_line, 0);
141*7c478bd9Sstevel@tonic-gate 		wclrtoeol(win->x_win);
142*7c478bd9Sstevel@tonic-gate 		getyx(win->x_win, win->x_line, win->x_col);
143*7c478bd9Sstevel@tonic-gate 		text++;
144*7c478bd9Sstevel@tonic-gate 		continue;
145*7c478bd9Sstevel@tonic-gate 	}
146*7c478bd9Sstevel@tonic-gate 	if (*text == '\f') {
147*7c478bd9Sstevel@tonic-gate 		if (win == &my_win)
148*7c478bd9Sstevel@tonic-gate 		wrefresh(curscr);
149*7c478bd9Sstevel@tonic-gate 		text++;
150*7c478bd9Sstevel@tonic-gate 		continue;
151*7c478bd9Sstevel@tonic-gate 	}
152*7c478bd9Sstevel@tonic-gate 	/* EOF character */
153*7c478bd9Sstevel@tonic-gate 	if (*text == '\004') {
154*7c478bd9Sstevel@tonic-gate 		quit();
155*7c478bd9Sstevel@tonic-gate 	}
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	/* typing alert character will alert recipient's terminal */
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	if (*text == '\007') {
160*7c478bd9Sstevel@tonic-gate 		beep();
161*7c478bd9Sstevel@tonic-gate 		continue;
162*7c478bd9Sstevel@tonic-gate 	}
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	/* check for wrap around */
165*7c478bd9Sstevel@tonic-gate 	if (win->x_col == COLS-1) {
166*7c478bd9Sstevel@tonic-gate 		xscroll(win, 0);
167*7c478bd9Sstevel@tonic-gate 	}
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 	/*
170*7c478bd9Sstevel@tonic-gate 	 * Handle the multibyte case
171*7c478bd9Sstevel@tonic-gate 	 * We print '?' for nonprintable widechars.
172*7c478bd9Sstevel@tonic-gate 	 */
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	if (mb_cur_max > 1 && mblen(text, mb_cur_max) > 1) {
175*7c478bd9Sstevel@tonic-gate 		wchar_t wc;
176*7c478bd9Sstevel@tonic-gate 		int len;
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 		len = mbtowc(&wc, text, mb_cur_max);
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 		if (iswprint(wc) || iswspace(wc)) {
181*7c478bd9Sstevel@tonic-gate 		/* its printable, put out the bytes */
182*7c478bd9Sstevel@tonic-gate 			do {
183*7c478bd9Sstevel@tonic-gate 				if (win->x_col == COLS-1) /* wraparound */
184*7c478bd9Sstevel@tonic-gate 					xscroll(win, 0);
185*7c478bd9Sstevel@tonic-gate 				waddch(win->x_win, *text++);
186*7c478bd9Sstevel@tonic-gate 				getyx(win->x_win, win->x_line, win->x_col);
187*7c478bd9Sstevel@tonic-gate 			} while (--len > 0);
188*7c478bd9Sstevel@tonic-gate 			continue;
189*7c478bd9Sstevel@tonic-gate 		}
190*7c478bd9Sstevel@tonic-gate 		/*
191*7c478bd9Sstevel@tonic-gate 		 * otherwise, punt and print a question mark.
192*7c478bd9Sstevel@tonic-gate 		 */
193*7c478bd9Sstevel@tonic-gate 		text += len;
194*7c478bd9Sstevel@tonic-gate 		waddch(win->x_win, '?');
195*7c478bd9Sstevel@tonic-gate 		getyx(win->x_win, win->x_line, win->x_col);
196*7c478bd9Sstevel@tonic-gate 		continue;
197*7c478bd9Sstevel@tonic-gate 	}
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 	itext = (unsigned int) *text;
200*7c478bd9Sstevel@tonic-gate 	if (isprint(itext) || *text == ' ' || *text == '\t' ||
201*7c478bd9Sstevel@tonic-gate 		*text == '\013' || *text == '\007' /* bell */) {
202*7c478bd9Sstevel@tonic-gate 		waddch(win->x_win, *text);
203*7c478bd9Sstevel@tonic-gate 	} else {
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 		if (!isascii(*text)) {
206*7c478bd9Sstevel@tonic-gate 			/* check for wrap around */
207*7c478bd9Sstevel@tonic-gate 			if (win->x_col == COLS-3) {
208*7c478bd9Sstevel@tonic-gate 				xscroll(win, 0);
209*7c478bd9Sstevel@tonic-gate 			}
210*7c478bd9Sstevel@tonic-gate 			waddch(win->x_win, 'M');
211*7c478bd9Sstevel@tonic-gate 			waddch(win->x_win, '-');
212*7c478bd9Sstevel@tonic-gate 			*text = toascii(*text);
213*7c478bd9Sstevel@tonic-gate 		}
214*7c478bd9Sstevel@tonic-gate 		if (iscntrl(*text)) {
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 			/* check for wrap around */
217*7c478bd9Sstevel@tonic-gate 			getyx(win->x_win, win->x_line, win->x_col);
218*7c478bd9Sstevel@tonic-gate 			if (win->x_col == COLS-2) {
219*7c478bd9Sstevel@tonic-gate 				xscroll(win, 0);
220*7c478bd9Sstevel@tonic-gate 			}
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 			waddch(win->x_win, '^');
223*7c478bd9Sstevel@tonic-gate 			waddch(win->x_win, *text + 0100);
224*7c478bd9Sstevel@tonic-gate 		}
225*7c478bd9Sstevel@tonic-gate 		else
226*7c478bd9Sstevel@tonic-gate 			waddch(win->x_win, *text);
227*7c478bd9Sstevel@tonic-gate 	}
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 	getyx(win->x_win, win->x_line, win->x_col);
230*7c478bd9Sstevel@tonic-gate 	text++;
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 	}  /* for loop */
233*7c478bd9Sstevel@tonic-gate 	wrefresh(win->x_win);
234*7c478bd9Sstevel@tonic-gate 	return (0);
235*7c478bd9Sstevel@tonic-gate }
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate /*
238*7c478bd9Sstevel@tonic-gate  * Read the character at the indicated position in win
239*7c478bd9Sstevel@tonic-gate  */
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate static int
242*7c478bd9Sstevel@tonic-gate readwin(win, line, col)
243*7c478bd9Sstevel@tonic-gate WINDOW *win;
244*7c478bd9Sstevel@tonic-gate int line, col;
245*7c478bd9Sstevel@tonic-gate {
246*7c478bd9Sstevel@tonic-gate int oldline, oldcol;
247*7c478bd9Sstevel@tonic-gate register int c;
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 	getyx(win, oldline, oldcol);
250*7c478bd9Sstevel@tonic-gate 	wmove(win, line, col);
251*7c478bd9Sstevel@tonic-gate 	c = winch(win);
252*7c478bd9Sstevel@tonic-gate 	wmove(win, oldline, oldcol);
253*7c478bd9Sstevel@tonic-gate 	return (c);
254*7c478bd9Sstevel@tonic-gate }
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate /*
257*7c478bd9Sstevel@tonic-gate  * Scroll a window, blanking out the line following the current line
258*7c478bd9Sstevel@tonic-gate  * so that the current position is obvious
259*7c478bd9Sstevel@tonic-gate  */
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate static void
262*7c478bd9Sstevel@tonic-gate xscroll(win, flag)
263*7c478bd9Sstevel@tonic-gate register xwin_t *win;
264*7c478bd9Sstevel@tonic-gate int flag;
265*7c478bd9Sstevel@tonic-gate {
266*7c478bd9Sstevel@tonic-gate 	if (flag == -1) {
267*7c478bd9Sstevel@tonic-gate 		wmove(win->x_win, 0, 0);
268*7c478bd9Sstevel@tonic-gate 		win->x_line = 0;
269*7c478bd9Sstevel@tonic-gate 		win->x_col = 0;
270*7c478bd9Sstevel@tonic-gate 		return;
271*7c478bd9Sstevel@tonic-gate 	}
272*7c478bd9Sstevel@tonic-gate 	win->x_line = (win->x_line + 1) % win->x_nlines;
273*7c478bd9Sstevel@tonic-gate 	win->x_col = 0;
274*7c478bd9Sstevel@tonic-gate 	wmove(win->x_win, win->x_line, win->x_col);
275*7c478bd9Sstevel@tonic-gate 	wclrtoeol(win->x_win);
276*7c478bd9Sstevel@tonic-gate 	wmove(win->x_win, (win->x_line + 1) % win->x_nlines, win->x_col);
277*7c478bd9Sstevel@tonic-gate 	wclrtoeol(win->x_win);
278*7c478bd9Sstevel@tonic-gate 	wmove(win->x_win, win->x_line, win->x_col);
279*7c478bd9Sstevel@tonic-gate }
280