Annotation of src/usr.bin/talk/display.c, Revision 1.7
1.7 ! angelos 1: /* $OpenBSD: display.c,v 1.6 1999/03/23 17:00:38 millert Exp $ */
1.1 deraadt 2: /* $NetBSD: display.c,v 1.3 1994/12/09 02:14:13 jtc Exp $ */
3:
4: /*
5: * Copyright (c) 1983, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: #if 0
39: static char sccsid[] = "@(#)display.c 8.1 (Berkeley) 6/6/93";
40: #endif
1.7 ! angelos 41: static char rcsid[] = "$OpenBSD: display.c,v 1.6 1999/03/23 17:00:38 millert Exp $";
1.1 deraadt 42: #endif /* not lint */
43:
44: /*
45: * The window 'manager', initializes curses and handles the actual
46: * displaying of text
47: */
48: #include "talk.h"
1.3 deraadt 49: #include <ctype.h>
1.1 deraadt 50:
51: xwin_t my_win;
52: xwin_t his_win;
53: WINDOW *line_win;
54:
1.7 ! angelos 55: #undef isprint
! 56: #define isprint(n) ((unsigned char)(n)>31)
! 57:
1.1 deraadt 58: int curses_initialized = 0;
59:
60: /*
61: * max HAS to be a function, it is called with
62: * a argument of the form --foo at least once.
63: */
1.5 pjanzen 64: int
1.1 deraadt 65: max(a,b)
66: int a, b;
67: {
68:
69: return (a > b ? a : b);
70: }
71:
72: /*
73: * Display some text on somebody's window, processing some control
74: * characters while we are at it.
75: */
1.5 pjanzen 76: void
1.1 deraadt 77: display(win, text, size)
78: register xwin_t *win;
79: register char *text;
80: int size;
81: {
82: register int i;
83: char cch;
84:
85: for (i = 0; i < size; i++) {
1.6 millert 86: /*
87: * Since we do not use curses's input routines we must
88: * convert '\r' -> '\n' ourselves.
89: */
90: if (*text == '\r')
91: *text = '\n';
92: if (*text == '\n') {
1.1 deraadt 93: xscroll(win, 0);
94: text++;
95: continue;
96: }
97: /* erase character */
98: if (*text == win->cerase) {
99: wmove(win->x_win, win->x_line, max(--win->x_col, 0));
100: getyx(win->x_win, win->x_line, win->x_col);
101: waddch(win->x_win, ' ');
102: wmove(win->x_win, win->x_line, win->x_col);
103: getyx(win->x_win, win->x_line, win->x_col);
104: text++;
105: continue;
106: }
107: /*
108: * On word erase search backwards until we find
109: * the beginning of a word or the beginning of
110: * the line.
111: */
112: if (*text == win->werase) {
113: int endcol, xcol, i, c;
114:
115: endcol = win->x_col;
116: xcol = endcol - 1;
117: while (xcol >= 0) {
118: c = readwin(win->x_win, win->x_line, xcol);
119: if (c != ' ')
120: break;
121: xcol--;
122: }
123: while (xcol >= 0) {
124: c = readwin(win->x_win, win->x_line, xcol);
125: if (c == ' ')
126: break;
127: xcol--;
128: }
129: wmove(win->x_win, win->x_line, xcol + 1);
130: for (i = xcol + 1; i < endcol; i++)
131: waddch(win->x_win, ' ');
132: wmove(win->x_win, win->x_line, xcol + 1);
133: getyx(win->x_win, win->x_line, win->x_col);
134: text++;
135: continue;
136: }
137: /* line kill */
138: if (*text == win->kill) {
139: wmove(win->x_win, win->x_line, 0);
140: wclrtoeol(win->x_win);
141: getyx(win->x_win, win->x_line, win->x_col);
142: text++;
143: continue;
144: }
145: if (*text == '\f') {
146: if (win == &my_win)
147: wrefresh(curscr);
148: text++;
149: continue;
150: }
151: if (win->x_col == COLS-1) {
152: /* check for wraparound */
153: xscroll(win, 0);
154: }
1.3 deraadt 155: if (!isprint(*text) && *text != '\t') {
1.1 deraadt 156: waddch(win->x_win, '^');
157: getyx(win->x_win, win->x_line, win->x_col);
158: if (win->x_col == COLS-1) /* check for wraparound */
159: xscroll(win, 0);
160: cch = (*text & 63) + 64;
161: waddch(win->x_win, cch);
162: } else
1.7 ! angelos 163: waddch(win->x_win, (unsigned char)(*text));
1.1 deraadt 164: getyx(win->x_win, win->x_line, win->x_col);
165: text++;
166: }
167: wrefresh(win->x_win);
168: }
169:
170: /*
171: * Read the character at the indicated position in win
172: */
1.5 pjanzen 173: int
1.1 deraadt 174: readwin(win, line, col)
175: WINDOW *win;
1.5 pjanzen 176: int line, col;
1.1 deraadt 177: {
178: int oldline, oldcol;
179: register int c;
180:
181: getyx(win, oldline, oldcol);
182: wmove(win, line, col);
183: c = winch(win);
184: wmove(win, oldline, oldcol);
185: return (c);
186: }
187:
188: /*
189: * Scroll a window, blanking out the line following the current line
190: * so that the current position is obvious
191: */
1.5 pjanzen 192: void
1.1 deraadt 193: xscroll(win, flag)
194: register xwin_t *win;
195: int flag;
196: {
197:
198: if (flag == -1) {
199: wmove(win->x_win, 0, 0);
200: win->x_line = 0;
201: win->x_col = 0;
202: return;
203: }
204: win->x_line = (win->x_line + 1) % win->x_nlines;
205: win->x_col = 0;
206: wmove(win->x_win, win->x_line, win->x_col);
207: wclrtoeol(win->x_win);
208: wmove(win->x_win, (win->x_line + 1) % win->x_nlines, win->x_col);
209: wclrtoeol(win->x_win);
210: wmove(win->x_win, win->x_line, win->x_col);
211: }