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