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 349b50d902SRodney W. Grimes #ifndef lint 3520868061SPhilippe Charnier #if 0 369b50d902SRodney W. Grimes static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93"; 3720868061SPhilippe Charnier #endif 3820868061SPhilippe Charnier static const char rcsid[] = 39c3aac50fSPeter Wemm "$FreeBSD$"; 409b50d902SRodney W. Grimes #endif /* not lint */ 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 489b50d902SRodney W. Grimes #include <errno.h> 49e896c344SDima Dorfman #include <netdb.h> 50e896c344SDima Dorfman #include <stdlib.h> 519b50d902SRodney W. Grimes #include <string.h> 52b3445392SPeter Wemm #include <sys/filio.h> 53e1b4d8d0SSheldon Hearn #include <unistd.h> 549b50d902SRodney W. Grimes #include "talk.h" 55e896c344SDima Dorfman #include "talk_ctl.h" 569b50d902SRodney W. Grimes 579b50d902SRodney W. Grimes #define A_LONG_TIME 10000000 589b50d902SRodney W. Grimes 599b50d902SRodney W. Grimes /* 609b50d902SRodney W. Grimes * The routine to do the actual talking 619b50d902SRodney W. Grimes */ 6228592604SJoerg Wunsch void 639b50d902SRodney W. Grimes talk() 649b50d902SRodney W. Grimes { 65e896c344SDima Dorfman struct hostent *hp, *hp2; 6628592604SJoerg Wunsch int nb; 6728592604SJoerg Wunsch fd_set read_set, read_template; 68e896c344SDima Dorfman char buf[BUFSIZ], **addr, *his_machine_name; 699b50d902SRodney W. Grimes struct timeval wait; 709b50d902SRodney W. Grimes 71e896c344SDima Dorfman his_machine_name = NULL; 72e896c344SDima Dorfman hp = gethostbyaddr((const char *)&his_machine_addr.s_addr, 73e896c344SDima Dorfman sizeof(his_machine_addr.s_addr), AF_INET); 74e896c344SDima Dorfman if (hp != NULL) { 75e896c344SDima Dorfman hp2 = gethostbyname(hp->h_name); 76e896c344SDima Dorfman if (hp2 != NULL && hp2->h_addrtype == AF_INET && 77e896c344SDima Dorfman hp2->h_length == sizeof(his_machine_addr)) 78e896c344SDima Dorfman for (addr = hp2->h_addr_list; *addr != NULL; addr++) 79e896c344SDima Dorfman if (memcmp(*addr, &his_machine_addr, 80e896c344SDima Dorfman sizeof(his_machine_addr)) == 0) { 81e896c344SDima Dorfman his_machine_name = strdup(hp->h_name); 82e896c344SDima Dorfman break; 83e896c344SDima Dorfman } 84e896c344SDima Dorfman } 85e896c344SDima Dorfman if (his_machine_name == NULL) 86e896c344SDima Dorfman his_machine_name = strdup(inet_ntoa(his_machine_addr)); 87e896c344SDima Dorfman snprintf(buf, sizeof(buf), "Connection established with %s@%s.", 88e896c344SDima Dorfman msg.r_name, his_machine_name); 89e896c344SDima Dorfman free(his_machine_name); 90e896c344SDima Dorfman message(buf); 91e151ec23SPeter Wemm write(STDOUT_FILENO, "\007\007\007", 3); 92e151ec23SPeter Wemm 939b50d902SRodney W. Grimes current_line = 0; 949b50d902SRodney W. Grimes 959b50d902SRodney W. Grimes /* 969b50d902SRodney W. Grimes * Wait on both the other process (sockt_mask) and 979b50d902SRodney W. Grimes * standard input ( STDIN_MASK ) 989b50d902SRodney W. Grimes */ 9928592604SJoerg Wunsch FD_ZERO(&read_template); 10028592604SJoerg Wunsch FD_SET(sockt, &read_template); 10128592604SJoerg Wunsch FD_SET(fileno(stdin), &read_template); 1029b50d902SRodney W. Grimes for (;;) { 1039b50d902SRodney W. Grimes read_set = read_template; 1049b50d902SRodney W. Grimes wait.tv_sec = A_LONG_TIME; 1059b50d902SRodney W. Grimes wait.tv_usec = 0; 1069b50d902SRodney W. Grimes nb = select(32, &read_set, 0, 0, &wait); 1079b50d902SRodney W. Grimes if (nb <= 0) { 1089b50d902SRodney W. Grimes if (errno == EINTR) { 1099b50d902SRodney W. Grimes read_set = read_template; 1109b50d902SRodney W. Grimes continue; 1119b50d902SRodney W. Grimes } 1129b50d902SRodney W. Grimes /* panic, we don't know what happened */ 1139b50d902SRodney W. Grimes p_error("Unexpected error from select"); 1149b50d902SRodney W. Grimes quit(); 1159b50d902SRodney W. Grimes } 11628592604SJoerg Wunsch if (FD_ISSET(sockt, &read_set)) { 1179b50d902SRodney W. Grimes /* There is data on sockt */ 1189b50d902SRodney W. Grimes nb = read(sockt, buf, sizeof buf); 1199b50d902SRodney W. Grimes if (nb <= 0) { 1209b50d902SRodney W. Grimes message("Connection closed. Exiting"); 1219b50d902SRodney W. Grimes quit(); 1229b50d902SRodney W. Grimes } 1239b50d902SRodney W. Grimes display(&his_win, buf, nb); 1249b50d902SRodney W. Grimes } 12528592604SJoerg Wunsch if (FD_ISSET(fileno(stdin), &read_set)) { 1269b50d902SRodney W. Grimes /* 1279b50d902SRodney W. Grimes * We can't make the tty non_blocking, because 1289b50d902SRodney W. Grimes * curses's output routines would screw up 1299b50d902SRodney W. Grimes */ 13011128de9SPaul Saab int i; 1319b50d902SRodney W. Grimes ioctl(0, FIONREAD, (struct sgttyb *) &nb); 132e1b4d8d0SSheldon Hearn nb = read(STDIN_FILENO, buf, nb); 1339b50d902SRodney W. Grimes display(&my_win, buf, nb); 1349b50d902SRodney W. Grimes /* might lose data here because sockt is non-blocking */ 13511128de9SPaul Saab for (i = 0; i < nb; ++i) 13611128de9SPaul Saab if (buf[i] == '\r') 13711128de9SPaul Saab buf[i] = '\n'; 1389b50d902SRodney W. Grimes write(sockt, buf, nb); 1399b50d902SRodney W. Grimes } 1409b50d902SRodney W. Grimes } 1419b50d902SRodney W. Grimes } 1429b50d902SRodney W. Grimes 1439b50d902SRodney W. Grimes /* 1449b50d902SRodney W. Grimes * p_error prints the system error message on the standard location 1459b50d902SRodney W. Grimes * on the screen and then exits. (i.e. a curses version of perror) 1469b50d902SRodney W. Grimes */ 14728592604SJoerg Wunsch void 1489b50d902SRodney W. Grimes p_error(string) 1499b50d902SRodney W. Grimes char *string; 1509b50d902SRodney W. Grimes { 151b02b5aadSAndrey A. Chernov wmove(my_win.x_win, current_line, 0); 1529b50d902SRodney W. Grimes wprintw(my_win.x_win, "[%s : %s (%d)]\n", 1539b50d902SRodney W. Grimes string, strerror(errno), errno); 1549b50d902SRodney W. Grimes wrefresh(my_win.x_win); 1559b50d902SRodney W. Grimes move(LINES-1, 0); 1569b50d902SRodney W. Grimes refresh(); 1579b50d902SRodney W. Grimes quit(); 1589b50d902SRodney W. Grimes } 1599b50d902SRodney W. Grimes 1609b50d902SRodney W. Grimes /* 1619b50d902SRodney W. Grimes * Display string in the standard location 1629b50d902SRodney W. Grimes */ 16328592604SJoerg Wunsch void 1649b50d902SRodney W. Grimes message(string) 1659b50d902SRodney W. Grimes char *string; 1669b50d902SRodney W. Grimes { 167b02b5aadSAndrey A. Chernov wmove(my_win.x_win, current_line, 0); 168b02b5aadSAndrey A. Chernov wprintw(my_win.x_win, "[%s]\n", string); 169b02b5aadSAndrey A. Chernov if (current_line < my_win.x_nlines - 1) 1709b50d902SRodney W. Grimes current_line++; 1719b50d902SRodney W. Grimes wrefresh(my_win.x_win); 1729b50d902SRodney W. Grimes } 173