[BACK]Return to display.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / talk

Annotation of src/usr.bin/talk/display.c, Revision 1.5

1.5     ! pjanzen     1: /*     $OpenBSD: display.c,v 1.4 1997/11/30 20:30:50 deraadt 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.5     ! pjanzen    41: static char rcsid[] = "$OpenBSD: display.c,v 1.4 1997/11/30 20:30:50 deraadt 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:
                     55: int    curses_initialized = 0;
                     56:
                     57: /*
                     58:  * max HAS to be a function, it is called with
                     59:  * a argument of the form --foo at least once.
                     60:  */
1.5     ! pjanzen    61: int
1.1       deraadt    62: max(a,b)
                     63:        int a, b;
                     64: {
                     65:
                     66:        return (a > b ? a : b);
                     67: }
                     68:
                     69: /*
                     70:  * Display some text on somebody's window, processing some control
                     71:  * characters while we are at it.
                     72:  */
1.5     ! pjanzen    73: void
1.1       deraadt    74: display(win, text, size)
                     75:        register xwin_t *win;
                     76:        register char *text;
                     77:        int size;
                     78: {
                     79:        register int i;
                     80:        char cch;
                     81:
                     82:        for (i = 0; i < size; i++) {
1.4       deraadt    83:                if (*text == '\n' || *text == '\r') {
1.1       deraadt    84:                        xscroll(win, 0);
                     85:                        text++;
                     86:                        continue;
                     87:                }
                     88:                /* erase character */
                     89:                if (*text == win->cerase) {
                     90:                        wmove(win->x_win, win->x_line, max(--win->x_col, 0));
                     91:                        getyx(win->x_win, win->x_line, win->x_col);
                     92:                        waddch(win->x_win, ' ');
                     93:                        wmove(win->x_win, win->x_line, win->x_col);
                     94:                        getyx(win->x_win, win->x_line, win->x_col);
                     95:                        text++;
                     96:                        continue;
                     97:                }
                     98:                /*
                     99:                 * On word erase search backwards until we find
                    100:                 * the beginning of a word or the beginning of
                    101:                 * the line.
                    102:                 */
                    103:                if (*text == win->werase) {
                    104:                        int endcol, xcol, i, c;
                    105:
                    106:                        endcol = win->x_col;
                    107:                        xcol = endcol - 1;
                    108:                        while (xcol >= 0) {
                    109:                                c = readwin(win->x_win, win->x_line, xcol);
                    110:                                if (c != ' ')
                    111:                                        break;
                    112:                                xcol--;
                    113:                        }
                    114:                        while (xcol >= 0) {
                    115:                                c = readwin(win->x_win, win->x_line, xcol);
                    116:                                if (c == ' ')
                    117:                                        break;
                    118:                                xcol--;
                    119:                        }
                    120:                        wmove(win->x_win, win->x_line, xcol + 1);
                    121:                        for (i = xcol + 1; i < endcol; i++)
                    122:                                waddch(win->x_win, ' ');
                    123:                        wmove(win->x_win, win->x_line, xcol + 1);
                    124:                        getyx(win->x_win, win->x_line, win->x_col);
                    125:                        text++;
                    126:                        continue;
                    127:                }
                    128:                /* line kill */
                    129:                if (*text == win->kill) {
                    130:                        wmove(win->x_win, win->x_line, 0);
                    131:                        wclrtoeol(win->x_win);
                    132:                        getyx(win->x_win, win->x_line, win->x_col);
                    133:                        text++;
                    134:                        continue;
                    135:                }
                    136:                if (*text == '\f') {
                    137:                        if (win == &my_win)
                    138:                                wrefresh(curscr);
                    139:                        text++;
                    140:                        continue;
                    141:                }
                    142:                if (win->x_col == COLS-1) {
                    143:                        /* check for wraparound */
                    144:                        xscroll(win, 0);
                    145:                }
1.3       deraadt   146:                if (!isprint(*text) && *text != '\t') {
1.1       deraadt   147:                        waddch(win->x_win, '^');
                    148:                        getyx(win->x_win, win->x_line, win->x_col);
                    149:                        if (win->x_col == COLS-1) /* check for wraparound */
                    150:                                xscroll(win, 0);
                    151:                        cch = (*text & 63) + 64;
                    152:                        waddch(win->x_win, cch);
                    153:                } else
                    154:                        waddch(win->x_win, *text);
                    155:                getyx(win->x_win, win->x_line, win->x_col);
                    156:                text++;
                    157:        }
                    158:        wrefresh(win->x_win);
                    159: }
                    160:
                    161: /*
                    162:  * Read the character at the indicated position in win
                    163:  */
1.5     ! pjanzen   164: int
1.1       deraadt   165: readwin(win, line, col)
                    166:        WINDOW *win;
1.5     ! pjanzen   167:        int line, col;
1.1       deraadt   168: {
                    169:        int oldline, oldcol;
                    170:        register int c;
                    171:
                    172:        getyx(win, oldline, oldcol);
                    173:        wmove(win, line, col);
                    174:        c = winch(win);
                    175:        wmove(win, oldline, oldcol);
                    176:        return (c);
                    177: }
                    178:
                    179: /*
                    180:  * Scroll a window, blanking out the line following the current line
                    181:  * so that the current position is obvious
                    182:  */
1.5     ! pjanzen   183: void
1.1       deraadt   184: xscroll(win, flag)
                    185:        register xwin_t *win;
                    186:        int flag;
                    187: {
                    188:
                    189:        if (flag == -1) {
                    190:                wmove(win->x_win, 0, 0);
                    191:                win->x_line = 0;
                    192:                win->x_col = 0;
                    193:                return;
                    194:        }
                    195:        win->x_line = (win->x_line + 1) % win->x_nlines;
                    196:        win->x_col = 0;
                    197:        wmove(win->x_win, win->x_line, win->x_col);
                    198:        wclrtoeol(win->x_win);
                    199:        wmove(win->x_win, (win->x_line + 1) % win->x_nlines, win->x_col);
                    200:        wclrtoeol(win->x_win);
                    201:        wmove(win->x_win, win->x_line, win->x_col);
                    202: }