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 33 #ifndef lint 34 static const char sccsid[] = "@(#)display.c 8.1 (Berkeley) 6/6/93"; 35 #endif 36 37 /* 38 * The window 'manager', initializes curses and handles the actual 39 * displaying of text 40 */ 41 #include <ctype.h> 42 #include <unistd.h> 43 #include <wctype.h> 44 #define _XOPEN_SOURCE_EXTENDED 45 #include <curses.h> 46 47 #include "talk.h" 48 49 void display(xwin_t *, wchar_t *); 50 51 xwin_t my_win; 52 xwin_t his_win; 53 WINDOW *line_win; 54 55 int curses_initialized = 0; 56 57 /* 58 * max HAS to be a function, it is called with 59 * an argument of the form --foo at least once. 60 */ 61 int 62 max(int a, int b) 63 { 64 65 return (a > b ? a : b); 66 } 67 68 static cchar_t * 69 makecchar(wchar_t in) 70 { 71 static cchar_t cc; 72 wchar_t wc[2]; 73 74 wc[0] = in; 75 wc[1] = L'\0'; 76 77 if (setcchar(&cc, wc, A_NORMAL, 0, NULL) != OK) 78 p_error("settchar(3) failure"); 79 80 return (&cc); 81 } 82 83 /* 84 * Display a symbol on somebody's window, processing some control 85 * characters while we are at it. 86 */ 87 void 88 display(xwin_t *win, wchar_t *wc) 89 { 90 91 /* 92 * Alas, can't use variables in C switch statement. 93 * Workaround these 3 cases with goto. 94 */ 95 if (*wc == win->kill) 96 goto kill; 97 else if (*wc == win->cerase) 98 goto cerase; 99 else if (*wc == win->werase) 100 goto werase; 101 102 switch (*wc) { 103 case L'\n': 104 case L'\r': 105 wadd_wch(win->x_win, makecchar(L'\n')); 106 getyx(win->x_win, win->x_line, win->x_col); 107 wrefresh(win->x_win); 108 return; 109 110 case 004: 111 if (win == &my_win) { 112 /* Ctrl-D clears the screen. */ 113 werase(my_win.x_win); 114 getyx(my_win.x_win, my_win.x_line, my_win.x_col); 115 wrefresh(my_win.x_win); 116 werase(his_win.x_win); 117 getyx(his_win.x_win, his_win.x_line, his_win.x_col); 118 wrefresh(his_win.x_win); 119 } 120 return; 121 122 /* Erase character. */ 123 case 010: /* BS */ 124 case 0177: /* DEL */ 125 cerase: 126 wmove(win->x_win, win->x_line, max(--win->x_col, 0)); 127 getyx(win->x_win, win->x_line, win->x_col); 128 waddch(win->x_win, ' '); 129 wmove(win->x_win, win->x_line, win->x_col); 130 getyx(win->x_win, win->x_line, win->x_col); 131 wrefresh(win->x_win); 132 return; 133 134 case 027: /* ^W */ 135 werase: 136 { 137 /* 138 * On word erase search backwards until we find 139 * the beginning of a word or the beginning of 140 * the line. 141 */ 142 int endcol, xcol, c; 143 144 endcol = win->x_col; 145 xcol = endcol - 1; 146 while (xcol >= 0) { 147 c = readwin(win->x_win, win->x_line, xcol); 148 if (c != ' ') 149 break; 150 xcol--; 151 } 152 while (xcol >= 0) { 153 c = readwin(win->x_win, win->x_line, xcol); 154 if (c == ' ') 155 break; 156 xcol--; 157 } 158 wmove(win->x_win, win->x_line, xcol + 1); 159 for (int i = xcol + 1; i < endcol; i++) 160 waddch(win->x_win, ' '); 161 wmove(win->x_win, win->x_line, xcol + 1); 162 getyx(win->x_win, win->x_line, win->x_col); 163 wrefresh(win->x_win); 164 return; 165 } 166 167 case 025: /* ^U */ 168 kill: 169 wmove(win->x_win, win->x_line, 0); 170 wclrtoeol(win->x_win); 171 getyx(win->x_win, win->x_line, win->x_col); 172 wrefresh(win->x_win); 173 return; 174 175 case L'\f': 176 if (win == &my_win) 177 wrefresh(curscr); 178 return; 179 180 case L'\7': 181 write(STDOUT_FILENO, wc, sizeof(*wc)); 182 return; 183 } 184 185 186 if (iswprint(*wc) || *wc == L'\t') 187 wadd_wch(win->x_win, makecchar(*wc)); 188 else 189 beep(); 190 191 getyx(win->x_win, win->x_line, win->x_col); 192 wrefresh(win->x_win); 193 } 194 195 /* 196 * Read the character at the indicated position in win 197 */ 198 int 199 readwin(WINDOW *win, int line, int col) 200 { 201 int oldline, oldcol; 202 int c; 203 204 getyx(win, oldline, oldcol); 205 wmove(win, line, col); 206 c = winch(win); 207 wmove(win, oldline, oldcol); 208 return (c); 209 } 210