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