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