init_disp.c revision 78733
1/*
2 * Copyright (c) 1983, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35#if 0
36static char sccsid[] = "@(#)init_disp.c	8.2 (Berkeley) 2/16/94";
37#endif
38static const char rcsid[] =
39  "$FreeBSD: head/usr.bin/talk/init_disp.c 78733 2001-06-24 23:22:14Z dd $";
40#endif /* not lint */
41
42/*
43 * Initialization code for the display package,
44 * as well as the signal handling routines.
45 */
46
47#include <err.h>
48#include <signal.h>
49#include <stdlib.h>
50#include <sys/stat.h>
51#include <unistd.h>
52#include <termios.h>
53#include "talk.h"
54
55/*
56 * Make sure the callee can write to the screen
57 */
58void
59check_writeable()
60{
61	char *tty;
62	struct stat sb;
63
64	if ((tty = ttyname(STDERR_FILENO)) == NULL)
65		err(1, "ttyname");
66	if (stat(tty, &sb) < 0)
67		err(1, "%s", tty);
68	if (!(sb.st_mode & S_IWGRP))
69		errx(1, "The callee cannot write to this terminal, use \"mesg y\".");
70}
71
72/*
73 * Set up curses, catch the appropriate signals,
74 * and build the various windows.
75 */
76void
77init_display()
78{
79	struct sigaction sa;
80
81	if (initscr() == NULL)
82		errx(1, "Terminal type unset or lacking necessary features.");
83	(void) sigaction(SIGTSTP, (struct sigaction *)0, &sa);
84	sigaddset(&sa.sa_mask, SIGALRM);
85	(void) sigaction(SIGTSTP, &sa, (struct sigaction *)0);
86	curses_initialized = 1;
87	clear();
88	refresh();
89	noecho();
90	crmode();
91	signal(SIGINT, sig_sent);
92	signal(SIGPIPE, sig_sent);
93	/* curses takes care of ^Z */
94	my_win.x_nlines = LINES / 2;
95	my_win.x_ncols = COLS;
96	my_win.x_win = newwin(my_win.x_nlines, my_win.x_ncols, 0, 0);
97	idlok(my_win.x_win, TRUE);
98	scrollok(my_win.x_win, TRUE);
99	wclear(my_win.x_win);
100
101	his_win.x_nlines = LINES / 2 - 1;
102	his_win.x_ncols = COLS;
103	his_win.x_win = newwin(his_win.x_nlines, his_win.x_ncols,
104	    my_win.x_nlines+1, 0);
105	idlok(my_win.x_win, TRUE);
106	scrollok(his_win.x_win, TRUE);
107	wclear(his_win.x_win);
108
109	line_win = newwin(1, COLS, my_win.x_nlines, 0);
110#if defined(hline) || defined(whline) || defined(NCURSES_VERSION)
111	whline(line_win, 0, COLS);
112#else
113	box(line_win, '-', '-');
114#endif
115	wrefresh(line_win);
116	/* let them know we are working on it */
117	current_state = "No connection yet";
118}
119
120/*
121 * Trade edit characters with the other talk. By agreement
122 * the first three characters each talk transmits after
123 * connection are the three edit characters.
124 */
125void
126set_edit_chars()
127{
128	char buf[3];
129	int cc;
130	struct termios tio;
131
132	tcgetattr(0, &tio);
133	my_win.cerase = tio.c_cc[VERASE];
134	my_win.kill = tio.c_cc[VKILL];
135	my_win.werase = tio.c_cc[VWERASE];
136	if (my_win.cerase == (char)_POSIX_VDISABLE)
137		my_win.kill = CERASE;
138	if (my_win.kill == (char)_POSIX_VDISABLE)
139		my_win.kill = CKILL;
140	if (my_win.werase == (char)_POSIX_VDISABLE)
141		my_win.werase = CWERASE;
142	buf[0] = my_win.cerase;
143	buf[1] = my_win.kill;
144	buf[2] = my_win.werase;
145	cc = write(sockt, buf, sizeof(buf));
146	if (cc != sizeof(buf) )
147		p_error("Lost the connection");
148	cc = read(sockt, buf, sizeof(buf));
149	if (cc != sizeof(buf) )
150		p_error("Lost the connection");
151	his_win.cerase = buf[0];
152	his_win.kill = buf[1];
153	his_win.werase = buf[2];
154}
155
156/* ARGSUSED */
157void
158sig_sent(signo)
159	int signo;
160{
161
162	message("Connection closing. Exiting");
163	quit();
164}
165
166/*
167 * All done talking...hang up the phone and reset terminal thingy's
168 */
169void
170quit()
171{
172
173	if (curses_initialized) {
174		wmove(his_win.x_win, his_win.x_nlines-1, 0);
175		wclrtoeol(his_win.x_win);
176		wrefresh(his_win.x_win);
177		endwin();
178	}
179	if (invitation_waiting)
180		send_delete();
181	exit(0);
182}
183