init_disp.c revision 5297
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 341590Srgrimes#ifndef lint 351590Srgrimesstatic char sccsid[] = "@(#)init_disp.c 8.2 (Berkeley) 2/16/94"; 361590Srgrimes#endif /* not lint */ 371590Srgrimes 381590Srgrimes/* 391590Srgrimes * Initialization code for the display package, 401590Srgrimes * as well as the signal handling routines. 411590Srgrimes */ 421590Srgrimes 431590Srgrimes#include <sys/ioctl.h> 441590Srgrimes#include <sys/ioctl_compat.h> 455297Sache#include <sys/types.h> 465297Sache#include <sys/stat.h> 471590Srgrimes 485297Sache#include <unistd.h> 491590Srgrimes#include <signal.h> 501590Srgrimes#include <err.h> 511590Srgrimes#include "talk.h" 521590Srgrimes 531590Srgrimes/* 545297Sache * Make sure the callee can write to the screen 555297Sache */ 565297Sachevoid check_writeable() 575297Sache{ 585297Sache char *tty; 595297Sache struct stat sb; 605297Sache 615297Sache if ((tty = ttyname(STDERR_FILENO)) == NULL) 625297Sache err(1, "ttyname"); 635297Sache if (stat(tty, &sb) < 0) 645297Sache err(1, "%s", tty); 655297Sache if (!(sb.st_mode & S_IWGRP)) 665297Sache errx(1, "The callee cannot write to this terminal, use \"mesg y\"."); 675297Sache} 685297Sache 695297Sache/* 701590Srgrimes * Set up curses, catch the appropriate signals, 711590Srgrimes * and build the various windows. 721590Srgrimes */ 731590Srgrimesinit_display() 741590Srgrimes{ 751590Srgrimes void sig_sent(); 761590Srgrimes struct sigvec sigv; 771590Srgrimes 781590Srgrimes if (initscr() == NULL) 791590Srgrimes errx(1, "Terminal type unset or lacking necessary features."); 801590Srgrimes (void) sigvec(SIGTSTP, (struct sigvec *)0, &sigv); 811590Srgrimes sigv.sv_mask |= sigmask(SIGALRM); 821590Srgrimes (void) sigvec(SIGTSTP, &sigv, (struct sigvec *)0); 831590Srgrimes curses_initialized = 1; 841590Srgrimes clear(); 851590Srgrimes refresh(); 861590Srgrimes noecho(); 871590Srgrimes crmode(); 881590Srgrimes signal(SIGINT, sig_sent); 891590Srgrimes signal(SIGPIPE, sig_sent); 901590Srgrimes /* curses takes care of ^Z */ 911590Srgrimes my_win.x_nlines = LINES / 2; 921590Srgrimes my_win.x_ncols = COLS; 931590Srgrimes my_win.x_win = newwin(my_win.x_nlines, my_win.x_ncols, 0, 0); 942929Sache scrollok(my_win.x_win, TRUE); 951590Srgrimes wclear(my_win.x_win); 961590Srgrimes 971590Srgrimes his_win.x_nlines = LINES / 2 - 1; 981590Srgrimes his_win.x_ncols = COLS; 991590Srgrimes his_win.x_win = newwin(his_win.x_nlines, his_win.x_ncols, 1001590Srgrimes my_win.x_nlines+1, 0); 1012929Sache scrollok(his_win.x_win, TRUE); 1021590Srgrimes wclear(his_win.x_win); 1031590Srgrimes 1041590Srgrimes line_win = newwin(1, COLS, my_win.x_nlines, 0); 1051590Srgrimes box(line_win, '-', '-'); 1061590Srgrimes wrefresh(line_win); 1071590Srgrimes /* let them know we are working on it */ 1081590Srgrimes current_state = "No connection yet"; 1091590Srgrimes} 1101590Srgrimes 1111590Srgrimes/* 1121590Srgrimes * Trade edit characters with the other talk. By agreement 1131590Srgrimes * the first three characters each talk transmits after 1141590Srgrimes * connection are the three edit characters. 1151590Srgrimes */ 1161590Srgrimesset_edit_chars() 1171590Srgrimes{ 1181590Srgrimes char buf[3]; 1191590Srgrimes int cc; 1201590Srgrimes struct sgttyb tty; 1211590Srgrimes struct ltchars ltc; 1221590Srgrimes 1231590Srgrimes ioctl(0, TIOCGETP, &tty); 1241590Srgrimes ioctl(0, TIOCGLTC, (struct sgttyb *)<c); 1251590Srgrimes my_win.cerase = tty.sg_erase; 1261590Srgrimes my_win.kill = tty.sg_kill; 1271590Srgrimes if (ltc.t_werasc == (char) -1) 1281590Srgrimes my_win.werase = '\027'; /* control W */ 1291590Srgrimes else 1301590Srgrimes my_win.werase = ltc.t_werasc; 1311590Srgrimes buf[0] = my_win.cerase; 1321590Srgrimes buf[1] = my_win.kill; 1331590Srgrimes buf[2] = my_win.werase; 1341590Srgrimes cc = write(sockt, buf, sizeof(buf)); 1351590Srgrimes if (cc != sizeof(buf) ) 1361590Srgrimes p_error("Lost the connection"); 1371590Srgrimes cc = read(sockt, buf, sizeof(buf)); 1381590Srgrimes if (cc != sizeof(buf) ) 1391590Srgrimes p_error("Lost the connection"); 1401590Srgrimes his_win.cerase = buf[0]; 1411590Srgrimes his_win.kill = buf[1]; 1421590Srgrimes his_win.werase = buf[2]; 1431590Srgrimes} 1441590Srgrimes 1451590Srgrimesvoid 1461590Srgrimessig_sent() 1471590Srgrimes{ 1481590Srgrimes 1491590Srgrimes message("Connection closing. Exiting"); 1501590Srgrimes quit(); 1511590Srgrimes} 1521590Srgrimes 1531590Srgrimes/* 1541590Srgrimes * All done talking...hang up the phone and reset terminal thingy's 1551590Srgrimes */ 1561590Srgrimesquit() 1571590Srgrimes{ 1581590Srgrimes 1591590Srgrimes if (curses_initialized) { 1601590Srgrimes wmove(his_win.x_win, his_win.x_nlines-1, 0); 1611590Srgrimes wclrtoeol(his_win.x_win); 1621590Srgrimes wrefresh(his_win.x_win); 1631590Srgrimes endwin(); 1641590Srgrimes } 1651590Srgrimes if (invitation_waiting) 1661590Srgrimes send_delete(); 1671590Srgrimes exit(0); 1681590Srgrimes} 169