19b50d902SRodney W. Grimes /* 29b50d902SRodney W. Grimes * Copyright (c) 1983, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 34caa64801SMark Murray #include <sys/cdefs.h> 35caa64801SMark Murray 36caa64801SMark Murray __FBSDID("$FreeBSD$"); 37caa64801SMark Murray 389b50d902SRodney W. Grimes #ifndef lint 39caa64801SMark Murray static const char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93"; 4020868061SPhilippe Charnier #endif 419b50d902SRodney W. Grimes 429b50d902SRodney W. Grimes /* 439b50d902SRodney W. Grimes * This file contains the I/O handling and the exchange of 449b50d902SRodney W. Grimes * edit characters. This connection itself is established in 459b50d902SRodney W. Grimes * ctl.c 469b50d902SRodney W. Grimes */ 479b50d902SRodney W. Grimes 48caa64801SMark Murray #include <sys/filio.h> 49caa64801SMark Murray 509b50d902SRodney W. Grimes #include <errno.h> 51ec8ffccbSOlivier Houchard #include <signal.h> 52e896c344SDima Dorfman #include <netdb.h> 53e896c344SDima Dorfman #include <stdlib.h> 549b50d902SRodney W. Grimes #include <string.h> 55e1b4d8d0SSheldon Hearn #include <unistd.h> 56caa64801SMark Murray 579b50d902SRodney W. Grimes #include "talk.h" 58e896c344SDima Dorfman #include "talk_ctl.h" 599b50d902SRodney W. Grimes 609b50d902SRodney W. Grimes #define A_LONG_TIME 10000000 619b50d902SRodney W. Grimes 62ec8ffccbSOlivier Houchard volatile sig_atomic_t gotwinch = 0; 63ec8ffccbSOlivier Houchard 649b50d902SRodney W. Grimes /* 659b50d902SRodney W. Grimes * The routine to do the actual talking 669b50d902SRodney W. Grimes */ 6728592604SJoerg Wunsch void 689b50d902SRodney W. Grimes talk() 699b50d902SRodney W. Grimes { 70e896c344SDima Dorfman struct hostent *hp, *hp2; 7128592604SJoerg Wunsch int nb; 7228592604SJoerg Wunsch fd_set read_set, read_template; 73e896c344SDima Dorfman char buf[BUFSIZ], **addr, *his_machine_name; 749b50d902SRodney W. Grimes struct timeval wait; 759b50d902SRodney W. Grimes 76e896c344SDima Dorfman his_machine_name = NULL; 77e896c344SDima Dorfman hp = gethostbyaddr((const char *)&his_machine_addr.s_addr, 78e896c344SDima Dorfman sizeof(his_machine_addr.s_addr), AF_INET); 79e896c344SDima Dorfman if (hp != NULL) { 80e896c344SDima Dorfman hp2 = gethostbyname(hp->h_name); 81e896c344SDima Dorfman if (hp2 != NULL && hp2->h_addrtype == AF_INET && 82e896c344SDima Dorfman hp2->h_length == sizeof(his_machine_addr)) 83e896c344SDima Dorfman for (addr = hp2->h_addr_list; *addr != NULL; addr++) 84e896c344SDima Dorfman if (memcmp(*addr, &his_machine_addr, 85e896c344SDima Dorfman sizeof(his_machine_addr)) == 0) { 86e896c344SDima Dorfman his_machine_name = strdup(hp->h_name); 87e896c344SDima Dorfman break; 88e896c344SDima Dorfman } 89e896c344SDima Dorfman } 90e896c344SDima Dorfman if (his_machine_name == NULL) 91e896c344SDima Dorfman his_machine_name = strdup(inet_ntoa(his_machine_addr)); 92e896c344SDima Dorfman snprintf(buf, sizeof(buf), "Connection established with %s@%s.", 93e896c344SDima Dorfman msg.r_name, his_machine_name); 94e896c344SDima Dorfman free(his_machine_name); 95e896c344SDima Dorfman message(buf); 96e151ec23SPeter Wemm write(STDOUT_FILENO, "\007\007\007", 3); 97e151ec23SPeter Wemm 989b50d902SRodney W. Grimes current_line = 0; 999b50d902SRodney W. Grimes 1009b50d902SRodney W. Grimes /* 1019b50d902SRodney W. Grimes * Wait on both the other process (sockt_mask) and 1029b50d902SRodney W. Grimes * standard input ( STDIN_MASK ) 1039b50d902SRodney W. Grimes */ 10428592604SJoerg Wunsch FD_ZERO(&read_template); 10528592604SJoerg Wunsch FD_SET(sockt, &read_template); 10628592604SJoerg Wunsch FD_SET(fileno(stdin), &read_template); 1079b50d902SRodney W. Grimes for (;;) { 1089b50d902SRodney W. Grimes read_set = read_template; 1099b50d902SRodney W. Grimes wait.tv_sec = A_LONG_TIME; 1109b50d902SRodney W. Grimes wait.tv_usec = 0; 1119b50d902SRodney W. Grimes nb = select(32, &read_set, 0, 0, &wait); 112ec8ffccbSOlivier Houchard if (gotwinch) { 113ec8ffccbSOlivier Houchard resize_display(); 114ec8ffccbSOlivier Houchard gotwinch = 0; 115ec8ffccbSOlivier Houchard } 1169b50d902SRodney W. Grimes if (nb <= 0) { 1179b50d902SRodney W. Grimes if (errno == EINTR) { 1189b50d902SRodney W. Grimes read_set = read_template; 1199b50d902SRodney W. Grimes continue; 1209b50d902SRodney W. Grimes } 1219b50d902SRodney W. Grimes /* panic, we don't know what happened */ 1229b50d902SRodney W. Grimes p_error("Unexpected error from select"); 1239b50d902SRodney W. Grimes quit(); 1249b50d902SRodney W. Grimes } 12528592604SJoerg Wunsch if (FD_ISSET(sockt, &read_set)) { 1269b50d902SRodney W. Grimes /* There is data on sockt */ 1279b50d902SRodney W. Grimes nb = read(sockt, buf, sizeof buf); 1289b50d902SRodney W. Grimes if (nb <= 0) { 1299b50d902SRodney W. Grimes message("Connection closed. Exiting"); 1309b50d902SRodney W. Grimes quit(); 1319b50d902SRodney W. Grimes } 1329b50d902SRodney W. Grimes display(&his_win, buf, nb); 1339b50d902SRodney W. Grimes } 13428592604SJoerg Wunsch if (FD_ISSET(fileno(stdin), &read_set)) { 1359b50d902SRodney W. Grimes /* 1369b50d902SRodney W. Grimes * We can't make the tty non_blocking, because 1379b50d902SRodney W. Grimes * curses's output routines would screw up 1389b50d902SRodney W. Grimes */ 13911128de9SPaul Saab int i; 1407045754fSTim J. Robbins ioctl(0, FIONREAD, (void *) &nb); 141e1b4d8d0SSheldon Hearn nb = read(STDIN_FILENO, buf, nb); 1429b50d902SRodney W. Grimes display(&my_win, buf, nb); 1439b50d902SRodney W. Grimes /* might lose data here because sockt is non-blocking */ 14411128de9SPaul Saab for (i = 0; i < nb; ++i) 14511128de9SPaul Saab if (buf[i] == '\r') 14611128de9SPaul Saab buf[i] = '\n'; 1479b50d902SRodney W. Grimes write(sockt, buf, nb); 1489b50d902SRodney W. Grimes } 1499b50d902SRodney W. Grimes } 1509b50d902SRodney W. Grimes } 1519b50d902SRodney W. Grimes 1529b50d902SRodney W. Grimes /* 1539b50d902SRodney W. Grimes * p_error prints the system error message on the standard location 1549b50d902SRodney W. Grimes * on the screen and then exits. (i.e. a curses version of perror) 1559b50d902SRodney W. Grimes */ 15628592604SJoerg Wunsch void 1579b50d902SRodney W. Grimes p_error(string) 158caa64801SMark Murray const char *string; 1599b50d902SRodney W. Grimes { 160b02b5aadSAndrey A. Chernov wmove(my_win.x_win, current_line, 0); 1619b50d902SRodney W. Grimes wprintw(my_win.x_win, "[%s : %s (%d)]\n", 1629b50d902SRodney W. Grimes string, strerror(errno), errno); 1639b50d902SRodney W. Grimes wrefresh(my_win.x_win); 1649b50d902SRodney W. Grimes move(LINES-1, 0); 1659b50d902SRodney W. Grimes refresh(); 1669b50d902SRodney W. Grimes quit(); 1679b50d902SRodney W. Grimes } 1689b50d902SRodney W. Grimes 1699b50d902SRodney W. Grimes /* 1709b50d902SRodney W. Grimes * Display string in the standard location 1719b50d902SRodney W. Grimes */ 17228592604SJoerg Wunsch void 1739b50d902SRodney W. Grimes message(string) 174caa64801SMark Murray const char *string; 1759b50d902SRodney W. Grimes { 176b02b5aadSAndrey A. Chernov wmove(my_win.x_win, current_line, 0); 177b02b5aadSAndrey A. Chernov wprintw(my_win.x_win, "[%s]\n", string); 178b02b5aadSAndrey A. Chernov if (current_line < my_win.x_nlines - 1) 1799b50d902SRodney W. Grimes current_line++; 1809b50d902SRodney W. Grimes wrefresh(my_win.x_win); 1819b50d902SRodney W. Grimes } 182