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