Debian Codegraph @

talk/display.c

raw
1 /*
2 Copyright (C) 1995-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1983, 1993
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 /*
49 * The window 'manager', initializes curses and handles the actual
50 * displaying of text
51 */
52
53 #include <config.h>
54
55 #include <ctype.h>
56 #include "talk.h"
57
58 xwin_t my_win;
59 xwin_t his_win;
60 WINDOW *line_win;
61
62 int curses_initialized = 0;
63
64 #undef max
65 /*
66 * max HAS to be a function, it is called with
67 * a argument of the form --foo at least once.
68 */
69 int
70 max (int a, int b)
71 {
72 return a > b ? a : b;
73 }
74
75 /*
76 * Read the character at the indicated position in win
77 */
78 static int
79 readwin (WINDOW *win, int line, int col)
80 {
81 int oldline, oldcol;
82 int c;
83
84 getyx (win, oldline, oldcol);
85 wmove (win, line, col);
86 c = winch (win);
87 wmove (win, oldline, oldcol);
88 return (c);
89 }
90
91 /*
92 * Scroll a window, blanking out the line following the current line
93 * so that the current position is obvious
94 */
95 static void
96 xscroll (xwin_t *win, int flag)
97 {
98 if (flag == -1)
99 {
100 wmove (win->x_win, 0, 0);
101 win->x_line = 0;
102 win->x_col = 0;
103 return;
104 }
105 win->x_line = (win->x_line + 1) % win->x_nlines;
106 win->x_col = 0;
107 wmove (win->x_win, win->x_line, win->x_col);
108 wclrtoeol (win->x_win);
109 wmove (win->x_win, (win->x_line + 1) % win->x_nlines, win->x_col);
110 wclrtoeol (win->x_win);
111 wmove (win->x_win, win->x_line, win->x_col);
112 }
113
114 /*
115 * Display some text on somebody's window, processing some control
116 * characters while we are at it.
117 */
118 int
119 display (xwin_t *win, char *text, int size)
120 {
121 int i;
122 unsigned char cch;
123
124 for (i = 0; i < size; i++)
125 {
126 if (*text == '\n')
127 {
128 xscroll (win, 0);
129 text++;
130 continue;
131 }
132 if (*text == '\a')
133 {
134 beep ();
135 wrefresh (curscr);
136 text++;
137 continue;
138 }
139 /* erase character */
140 if (*text == win->cerase)
141 {
142 wmove (win->x_win, win->x_line, max (--win->x_col, 0));
143 getyx (win->x_win, win->x_line, win->x_col);
144 waddch (win->x_win, ' ');
145 wmove (win->x_win, win->x_line, win->x_col);
146 getyx (win->x_win, win->x_line, win->x_col);
147 text++;
148 continue;
149 }
150 /*
151 * On word erase search backwards until we find
152 * the beginning of a word or the beginning of
153 * the line.
154 */
155 if (*text == win->werase)
156 {
157 int endcol, xcol, i, c;
158
159 endcol = win->x_col;
160 xcol = endcol - 1;
161 while (xcol >= 0)
162 {
163 c = readwin (win->x_win, win->x_line, xcol);
164 if (c != ' ')
165 break;
166 xcol--;
167 }
168 while (xcol >= 0)
169 {
170 c = readwin (win->x_win, win->x_line, xcol);
171 if (c == ' ')
172 break;
173 xcol--;
174 }
175 wmove (win->x_win, win->x_line, xcol + 1);
176 for (i = xcol + 1; i < endcol; i++)
177 waddch (win->x_win, ' ');
178 wmove (win->x_win, win->x_line, xcol + 1);
179 getyx (win->x_win, win->x_line, win->x_col);
180 text++;
181 continue;
182 }
183 /* line kill */
184 if (*text == win->kill)
185 {
186 wmove (win->x_win, win->x_line, 0);
187 wclrtoeol (win->x_win);
188 getyx (win->x_win, win->x_line, win->x_col);
189 text++;
190 continue;
191 }
192 /* Refresh screen with input ^L, Ctrl-L.
193 * Local trigger only.
194 */
195 if (*text == '\f')
196 {
197 if (win == &my_win)
198 wrefresh (curscr);
199 text++;
200 continue;
201 }
202 /* Clear both windows with input ^D, Ctrl-D.
203 * Local trigger only.
204 */
205 if (*text == '\04' && win == &my_win)
206 {
207 wclear (my_win.x_win);
208 wclear (his_win.x_win);
209 wrefresh (my_win.x_win);
210 wrefresh (his_win.x_win);
211 text++;
212 continue;
213 }
214 if (win->x_col == COLS - 1)
215 {
216 /* check for wraparound */
217 xscroll (win, 0);
218 }
219
220 /*
221 * Printable characters, SP, and TAB are printed
222 * verbatim. Characters beyond the ASCII table
223 * must be handled. Beware of sign extension!
224 *
225 * The locale setting is in effect when testing
226 * printability of any input character.
227 */
228 if (isprint (*text & 0xff) || *text == '\t')
229 waddch (win->x_win, *text & 0xff);
230 else
231 {
232 waddch (win->x_win, '^');
233 getyx (win->x_win, win->x_line, win->x_col);
234 if (win->x_col == COLS - 1) /* check for wraparound */
235 xscroll (win, 0);
236 cch = (*text & 63) + 64;
237 waddch (win->x_win, cch);
238 }
239
240 getyx (win->x_win, win->x_line, win->x_col);
241 text++;
242 }
243 wrefresh (win->x_win);
244
245 return 0;
246 }