]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/talk/display.c
ident(1): Normalizing date format
[FreeBSD/FreeBSD.git] / usr.bin / talk / display.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #include <sys/cdefs.h>
33
34 __FBSDID("$FreeBSD$");
35
36 #ifndef lint
37 static const char sccsid[] = "@(#)display.c     8.1 (Berkeley) 6/6/93";
38 #endif
39
40 /*
41  * The window 'manager', initializes curses and handles the actual
42  * displaying of text
43  */
44 #include <ctype.h>
45 #include <unistd.h>
46 #include <wctype.h>
47 #define _XOPEN_SOURCE_EXTENDED
48 #include <curses.h>
49
50 #include "talk.h"
51
52 void    display(xwin_t *, wchar_t *);
53
54 xwin_t  my_win;
55 xwin_t  his_win;
56 WINDOW  *line_win;
57
58 int     curses_initialized = 0;
59
60 /*
61  * max HAS to be a function, it is called with
62  * an argument of the form --foo at least once.
63  */
64 int
65 max(int a, int b)
66 {
67
68         return (a > b ? a : b);
69 }
70
71 static cchar_t *
72 makecchar(wchar_t in)
73 {
74         static cchar_t cc;
75         wchar_t wc[2];
76
77         wc[0] = in;
78         wc[1] = L'\0';
79
80         if (setcchar(&cc, wc, A_NORMAL, 0, NULL) != OK)
81                 p_error("settchar(3) failure");
82
83         return (&cc);
84 }
85
86 /*
87  * Display a symbol on somebody's window, processing some control
88  * characters while we are at it.
89  */
90 void
91 display(xwin_t *win, wchar_t *wc)
92 {
93
94         /*
95          * Alas, can't use variables in C switch statement.
96          * Workaround these 3 cases with goto.
97          */
98         if (*wc == win->kill)
99                 goto kill;
100         else if (*wc == win->cerase)
101                 goto cerase;
102         else if (*wc == win->werase)
103                 goto werase;
104
105         switch (*wc) {
106         case L'\n':
107         case L'\r':
108                 wadd_wch(win->x_win, makecchar(L'\n'));
109                 getyx(win->x_win, win->x_line, win->x_col);
110                 wrefresh(win->x_win);
111                 return;
112
113         case 004:
114                 if (win == &my_win) {
115                         /* Ctrl-D clears the screen. */
116                         werase(my_win.x_win);
117                         getyx(my_win.x_win, my_win.x_line, my_win.x_col);
118                         wrefresh(my_win.x_win);
119                         werase(his_win.x_win);
120                         getyx(his_win.x_win, his_win.x_line, his_win.x_col);
121                         wrefresh(his_win.x_win);
122                 }
123                 return;
124
125         /* Erase character. */
126         case 010:       /* BS */
127         case 0177:      /* DEL */
128 cerase:
129                 wmove(win->x_win, win->x_line, max(--win->x_col, 0));
130                 getyx(win->x_win, win->x_line, win->x_col);
131                 waddch(win->x_win, ' ');
132                 wmove(win->x_win, win->x_line, win->x_col);
133                 getyx(win->x_win, win->x_line, win->x_col);
134                 wrefresh(win->x_win);
135                 return;
136
137         case 027:       /* ^W */
138 werase:
139             {
140                 /*
141                  * On word erase search backwards until we find
142                  * the beginning of a word or the beginning of
143                  * the line.
144                  */
145                 int endcol, xcol, c;
146
147                 endcol = win->x_col;
148                 xcol = endcol - 1;
149                 while (xcol >= 0) {
150                         c = readwin(win->x_win, win->x_line, xcol);
151                         if (c != ' ')
152                                 break;
153                         xcol--;
154                 }
155                 while (xcol >= 0) {
156                         c = readwin(win->x_win, win->x_line, xcol);
157                         if (c == ' ')
158                                 break;
159                         xcol--;
160                 }
161                 wmove(win->x_win, win->x_line, xcol + 1);
162                 for (int i = xcol + 1; i < endcol; i++)
163                         waddch(win->x_win, ' ');
164                 wmove(win->x_win, win->x_line, xcol + 1);
165                 getyx(win->x_win, win->x_line, win->x_col);
166                 wrefresh(win->x_win);
167                 return;
168             }
169
170         case 025:       /* ^U */
171 kill:
172                 wmove(win->x_win, win->x_line, 0);
173                 wclrtoeol(win->x_win);
174                 getyx(win->x_win, win->x_line, win->x_col);
175                 wrefresh(win->x_win);
176                 return;
177
178         case L'\f':
179                 if (win == &my_win)
180                         wrefresh(curscr);
181                 return;
182
183         case L'\7':
184                 write(STDOUT_FILENO, wc, sizeof(*wc));
185                 return;
186         }
187
188
189         if (iswprint(*wc) || *wc == L'\t')
190                 wadd_wch(win->x_win, makecchar(*wc));
191         else
192                 beep();
193
194         getyx(win->x_win, win->x_line, win->x_col);
195         wrefresh(win->x_win);
196 }
197
198 /*
199  * Read the character at the indicated position in win
200  */
201 int
202 readwin(WINDOW *win, int line, int col)
203 {
204         int oldline, oldcol;
205         int c;
206
207         getyx(win, oldline, oldcol);
208         wmove(win, line, col);
209         c = winch(win);
210         wmove(win, oldline, oldcol);
211         return (c);
212 }