| 1 |
/* |
| 2 |
Copyright (C) 1995-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
/* |
| 20 |
* Copyright (c) 1983, 1993 |
| 21 |
* The Regents of the University of California. All rights reserved. |
| 22 |
* |
| 23 |
* Redistribution and use in source and binary forms, with or without |
| 24 |
* modification, are permitted provided that the following conditions |
| 25 |
* are met: |
| 26 |
* 1. Redistributions of source code must retain the above copyright |
| 27 |
* notice, this list of conditions and the following disclaimer. |
| 28 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 29 |
* notice, this list of conditions and the following disclaimer in the |
| 30 |
* documentation and/or other materials provided with the distribution. |
| 31 |
* 3. Neither the name of the University nor the names of its contributors |
| 32 |
* may be used to endorse or promote products derived from this software |
| 33 |
* without specific prior written permission. |
| 34 |
* |
| 35 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 36 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 37 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 38 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 39 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 40 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 41 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 42 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 43 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 44 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 45 |
* SUCH DAMAGE. |
| 46 |
*/ |
| 47 |
|
| 48 |
#include <config.h> |
| 49 |
|
| 50 |
#include <stdlib.h> |
| 51 |
|
| 52 |
/* |
| 53 |
* Initialization code for the display package, |
| 54 |
* as well as the signal handling routines. |
| 55 |
*/ |
| 56 |
|
| 57 |
#include <termios.h> |
| 58 |
#include <unistd.h> |
| 59 |
#include <signal.h> |
| 60 |
#include <error.h> |
| 61 |
#include "talk.h" |
| 62 |
#include "attribute.h" |
| 63 |
|
| 64 |
static void |
| 65 |
sig_sent (int sig MAYBE_UNUSED) |
| 66 |
{ |
| 67 |
|
| 68 |
message ("Connection closing. Exiting"); |
| 69 |
quit (); |
| 70 |
} |
| 71 |
|
| 72 |
/* |
| 73 |
* Set up curses, catch the appropriate signals, |
| 74 |
* and build the various windows. |
| 75 |
*/ |
| 76 |
int |
| 77 |
init_display (void) |
| 78 |
{ |
| 79 |
#ifdef HAVE_SIGACTION |
| 80 |
struct sigaction siga; |
| 81 |
#else |
| 82 |
# ifdef HAVE_SIGVEC |
| 83 |
struct sigvec sigv; |
| 84 |
# endif |
| 85 |
#endif |
| 86 |
|
| 87 |
if (initscr () == NULL) |
| 88 |
error (EXIT_FAILURE, 0, |
| 89 |
"Terminal type unset or lacking necessary features."); |
| 90 |
|
| 91 |
#ifdef HAVE_SIGACTION |
| 92 |
sigaction (SIGTSTP, (struct sigaction *) 0, &siga); |
| 93 |
sigaddset (&siga.sa_mask, SIGALRM); |
| 94 |
sigaction (SIGTSTP, &siga, (struct sigaction *) 0); |
| 95 |
#else /* !HAVE_SIGACTION */ |
| 96 |
# ifdef HAVE_SIGVEC |
| 97 |
sigvec (SIGTSTP, (struct sigvec *) 0, &sigv); |
| 98 |
sigv.sv_mask |= sigmask (SIGALRM); |
| 99 |
sigvec (SIGTSTP, &sigv, (struct sigvec *) 0); |
| 100 |
# endif/* HAVE_SIGVEC */ |
| 101 |
#endif /* HAVE_SIGACTION */ |
| 102 |
|
| 103 |
curses_initialized = 1; |
| 104 |
clear (); |
| 105 |
refresh (); |
| 106 |
noecho (); |
| 107 |
crmode (); |
| 108 |
|
| 109 |
signal (SIGQUIT, sig_sent); |
| 110 |
signal (SIGINT, sig_sent); |
| 111 |
signal (SIGPIPE, sig_sent); |
| 112 |
|
| 113 |
/* curses takes care of ^Z */ |
| 114 |
my_win.x_nlines = LINES / 2; |
| 115 |
my_win.x_ncols = COLS; |
| 116 |
my_win.x_win = newwin (my_win.x_nlines, my_win.x_ncols, 0, 0); |
| 117 |
scrollok (my_win.x_win, FALSE); |
| 118 |
wclear (my_win.x_win); |
| 119 |
|
| 120 |
his_win.x_nlines = LINES / 2 - 1; |
| 121 |
his_win.x_ncols = COLS; |
| 122 |
his_win.x_win = newwin (his_win.x_nlines, his_win.x_ncols, |
| 123 |
my_win.x_nlines + 1, 0); |
| 124 |
scrollok (his_win.x_win, FALSE); |
| 125 |
wclear (his_win.x_win); |
| 126 |
|
| 127 |
line_win = newwin (1, COLS, my_win.x_nlines, 0); |
| 128 |
box (line_win, '-', '-'); |
| 129 |
wrefresh (line_win); |
| 130 |
|
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
/* |
| 135 |
* Trade edit characters with the other talk. By agreement |
| 136 |
* the first three characters each talk transmits after |
| 137 |
* connection are the three edit characters. |
| 138 |
*/ |
| 139 |
int |
| 140 |
set_edit_chars (void) |
| 141 |
{ |
| 142 |
int cc; |
| 143 |
char buf[3]; |
| 144 |
|
| 145 |
#ifdef HAVE_TCGETATTR |
| 146 |
struct termios tty; |
| 147 |
cc_t disable = (cc_t) - 1, erase, werase, kill; |
| 148 |
|
| 149 |
# if !defined _POSIX_VDISABLE && defined HAVE_FPATHCONF && defined _PC_VDISABLE |
| 150 |
disable = fpathconf (0, _PC_VDISABLE); |
| 151 |
# endif |
| 152 |
|
| 153 |
erase = werase = kill = disable; |
| 154 |
|
| 155 |
if (tcgetattr (0, &tty) >= 0) |
| 156 |
{ |
| 157 |
erase = tty.c_cc[VERASE]; |
| 158 |
# ifdef VWERASE |
| 159 |
werase = tty.c_cc[VWERASE]; |
| 160 |
# endif |
| 161 |
kill = tty.c_cc[VKILL]; |
| 162 |
} |
| 163 |
|
| 164 |
if (erase == disable) |
| 165 |
erase = '\177'; /* rubout */ |
| 166 |
if (werase == disable) |
| 167 |
werase = '\027'; /* ^W */ |
| 168 |
if (kill == disable) |
| 169 |
kill = '\025'; /* ^U */ |
| 170 |
|
| 171 |
my_win.cerase = erase; |
| 172 |
my_win.werase = werase; |
| 173 |
my_win.kill = kill; |
| 174 |
#else /* !HAVE_TCGETATTR */ |
| 175 |
struct sgttyb tty; |
| 176 |
struct ltchars ltc; |
| 177 |
|
| 178 |
ioctl (0, TIOCGETP, &tty); |
| 179 |
ioctl (0, TIOCGLTC, (struct sgttyb *) <c); |
| 180 |
my_win.cerase = tty.sg_erase; |
| 181 |
my_win.kill = tty.sg_kill; |
| 182 |
if (ltc.t_werasc == (char) -1) |
| 183 |
my_win.werase = '\027'; /* control W */ |
| 184 |
else |
| 185 |
my_win.werase = ltc.t_werasc; |
| 186 |
#endif /* HAVE_TCGETATTR */ |
| 187 |
|
| 188 |
buf[0] = my_win.cerase; |
| 189 |
buf[1] = my_win.kill; |
| 190 |
buf[2] = my_win.werase; |
| 191 |
cc = write (sockt, buf, sizeof (buf)); |
| 192 |
if (cc != sizeof (buf)) |
| 193 |
p_error ("Lost the connection"); |
| 194 |
cc = read (sockt, buf, sizeof (buf)); |
| 195 |
if (cc != sizeof (buf)) |
| 196 |
p_error ("Lost the connection"); |
| 197 |
his_win.cerase = buf[0]; |
| 198 |
his_win.kill = buf[1]; |
| 199 |
his_win.werase = buf[2]; |
| 200 |
|
| 201 |
return 0; |
| 202 |
} |
| 203 |
|
| 204 |
/* |
| 205 |
* All done talking...hang up the phone and reset terminal thingy's |
| 206 |
*/ |
| 207 |
int |
| 208 |
quit (void) |
| 209 |
{ |
| 210 |
|
| 211 |
if (curses_initialized) |
| 212 |
{ |
| 213 |
wmove (his_win.x_win, his_win.x_nlines - 1, 0); |
| 214 |
wclrtoeol (his_win.x_win); |
| 215 |
wrefresh (his_win.x_win); |
| 216 |
endwin (); |
| 217 |
} |
| 218 |
if (invitation_waiting) |
| 219 |
send_delete (); |
| 220 |
exit (EXIT_SUCCESS); |
| 221 |
} |