display.c revision 200418
11590Srgrimes/*
21590Srgrimes * Copyright (c) 1983, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
3487710Smarkm#include <sys/cdefs.h>
3587710Smarkm
3687710Smarkm__FBSDID("$FreeBSD: head/usr.bin/talk/display.c 200418 2009-12-11 23:23:57Z delphij $");
3787710Smarkm
381590Srgrimes#ifndef lint
3987710Smarkmstatic const char sccsid[] = "@(#)display.c	8.1 (Berkeley) 6/6/93";
4032503Scharnier#endif
411590Srgrimes
421590Srgrimes/*
431590Srgrimes * The window 'manager', initializes curses and handles the actual
441590Srgrimes * displaying of text
451590Srgrimes */
462929Sache#include <ctype.h>
47200418Sdelphij#include <unistd.h>
481590Srgrimes
4987710Smarkm#include "talk.h"
5087710Smarkm
511590Srgrimesxwin_t	my_win;
521590Srgrimesxwin_t	his_win;
531590SrgrimesWINDOW	*line_win;
541590Srgrimes
551590Srgrimesint	curses_initialized = 0;
561590Srgrimes
571590Srgrimes/*
581590Srgrimes * max HAS to be a function, it is called with
59108533Sschweikh * an argument of the form --foo at least once.
601590Srgrimes */
6114443Sjoergint
62178642Sdelphijmax(int a, int b)
631590Srgrimes{
641590Srgrimes
651590Srgrimes	return (a > b ? a : b);
661590Srgrimes}
671590Srgrimes
681590Srgrimes/*
691590Srgrimes * Display some text on somebody's window, processing some control
701590Srgrimes * characters while we are at it.
711590Srgrimes */
7214443Sjoergvoid
73178642Sdelphijdisplay(xwin_t *win, char *text, int size)
741590Srgrimes{
7587710Smarkm	int i;
761590Srgrimes	char cch;
771590Srgrimes
781590Srgrimes	for (i = 0; i < size; i++) {
792929Sache		if (*text == '\n' || *text == '\r') {
802929Sache			waddch(win->x_win, '\n');
812929Sache			getyx(win->x_win, win->x_line, win->x_col);
821590Srgrimes			text++;
831590Srgrimes			continue;
841590Srgrimes		}
8599965Sluigi		if (*text == 004 && win == &my_win) {
8699965Sluigi			/* control-D clears the screen */
8799965Sluigi			werase(my_win.x_win);
8899965Sluigi			getyx(my_win.x_win, my_win.x_line, my_win.x_col);
8999965Sluigi			wrefresh(my_win.x_win);
9099965Sluigi			werase(his_win.x_win);
9199965Sluigi			getyx(his_win.x_win, his_win.x_line, his_win.x_col);
9299965Sluigi			wrefresh(his_win.x_win);
9399965Sluigi			text++;
9499965Sluigi			continue;
9599965Sluigi		}
96117238Sluigi
971590Srgrimes		/* erase character */
982929Sache		if (   *text == win->cerase
992929Sache		    || *text == 010     /* BS */
1002929Sache		    || *text == 0177    /* DEL */
1012929Sache		   ) {
1021590Srgrimes			wmove(win->x_win, win->x_line, max(--win->x_col, 0));
1031590Srgrimes			getyx(win->x_win, win->x_line, win->x_col);
1041590Srgrimes			waddch(win->x_win, ' ');
1051590Srgrimes			wmove(win->x_win, win->x_line, win->x_col);
1061590Srgrimes			getyx(win->x_win, win->x_line, win->x_col);
1071590Srgrimes			text++;
1081590Srgrimes			continue;
1091590Srgrimes		}
1101590Srgrimes		/*
1111590Srgrimes		 * On word erase search backwards until we find
1121590Srgrimes		 * the beginning of a word or the beginning of
1131590Srgrimes		 * the line.
1141590Srgrimes		 */
1152929Sache		if (   *text == win->werase
1162929Sache		    || *text == 027     /* ^W */
1172929Sache		   ) {
11887710Smarkm			int endcol, xcol, ii, c;
1191590Srgrimes
1201590Srgrimes			endcol = win->x_col;
1211590Srgrimes			xcol = endcol - 1;
1221590Srgrimes			while (xcol >= 0) {
1231590Srgrimes				c = readwin(win->x_win, win->x_line, xcol);
1241590Srgrimes				if (c != ' ')
1251590Srgrimes					break;
1261590Srgrimes				xcol--;
1271590Srgrimes			}
1281590Srgrimes			while (xcol >= 0) {
1291590Srgrimes				c = readwin(win->x_win, win->x_line, xcol);
1301590Srgrimes				if (c == ' ')
1311590Srgrimes					break;
1321590Srgrimes				xcol--;
1331590Srgrimes			}
1341590Srgrimes			wmove(win->x_win, win->x_line, xcol + 1);
13587710Smarkm			for (ii = xcol + 1; ii < endcol; ii++)
1361590Srgrimes				waddch(win->x_win, ' ');
1371590Srgrimes			wmove(win->x_win, win->x_line, xcol + 1);
1381590Srgrimes			getyx(win->x_win, win->x_line, win->x_col);
1391590Srgrimes			text++;
1401590Srgrimes			continue;
1411590Srgrimes		}
1421590Srgrimes		/* line kill */
1432929Sache		if (   *text == win->kill
1442929Sache		    || *text == 025     /* ^U */
1452929Sache		   ) {
1461590Srgrimes			wmove(win->x_win, win->x_line, 0);
1471590Srgrimes			wclrtoeol(win->x_win);
1481590Srgrimes			getyx(win->x_win, win->x_line, win->x_col);
1491590Srgrimes			text++;
1501590Srgrimes			continue;
1511590Srgrimes		}
1521590Srgrimes		if (*text == '\f') {
1531590Srgrimes			if (win == &my_win)
1541590Srgrimes				wrefresh(curscr);
1551590Srgrimes			text++;
1561590Srgrimes			continue;
1571590Srgrimes		}
1582929Sache		if (*text == '\7') {
15917676Speter			write(STDOUT_FILENO, text, 1);
1602929Sache			text++;
1612929Sache			continue;
1621590Srgrimes		}
16315022Sache		if (!isprint((unsigned char)*text) && *text != '\t') {
1641590Srgrimes			waddch(win->x_win, '^');
1651590Srgrimes			getyx(win->x_win, win->x_line, win->x_col);
1661590Srgrimes			cch = (*text & 63) + 64;
1671590Srgrimes			waddch(win->x_win, cch);
1681590Srgrimes		} else
16915022Sache			waddch(win->x_win, (unsigned char)*text);
1701590Srgrimes		getyx(win->x_win, win->x_line, win->x_col);
1711590Srgrimes		text++;
1721590Srgrimes	}
1731590Srgrimes	wrefresh(win->x_win);
1741590Srgrimes}
1751590Srgrimes
1761590Srgrimes/*
1771590Srgrimes * Read the character at the indicated position in win
1781590Srgrimes */
17914443Sjoergint
180178642Sdelphijreadwin(WINDOW *win, int line, int col)
1811590Srgrimes{
1821590Srgrimes	int oldline, oldcol;
18387710Smarkm	int c;
1841590Srgrimes
1851590Srgrimes	getyx(win, oldline, oldcol);
1861590Srgrimes	wmove(win, line, col);
1871590Srgrimes	c = winch(win);
1881590Srgrimes	wmove(win, oldline, oldcol);
1891590Srgrimes	return (c);
1901590Srgrimes}
191