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 359b50d902SRodney W. Grimes static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93"; 369b50d902SRodney W. Grimes #endif /* not lint */ 379b50d902SRodney W. Grimes 389b50d902SRodney W. Grimes /* 399b50d902SRodney W. Grimes * This file contains the I/O handling and the exchange of 409b50d902SRodney W. Grimes * edit characters. This connection itself is established in 419b50d902SRodney W. Grimes * ctl.c 429b50d902SRodney W. Grimes */ 439b50d902SRodney W. Grimes 449b50d902SRodney W. Grimes #include <sys/ioctl.h> 459b50d902SRodney W. Grimes #include <sys/time.h> 469b50d902SRodney W. Grimes #include <stdio.h> 479b50d902SRodney W. Grimes #include <errno.h> 489b50d902SRodney W. Grimes #include <string.h> 499b50d902SRodney W. Grimes #include "talk.h" 509b50d902SRodney W. Grimes 519b50d902SRodney W. Grimes #define A_LONG_TIME 10000000 529b50d902SRodney W. Grimes 539b50d902SRodney W. Grimes /* 549b50d902SRodney W. Grimes * The routine to do the actual talking 559b50d902SRodney W. Grimes */ 5628592604SJoerg Wunsch void 579b50d902SRodney W. Grimes talk() 589b50d902SRodney W. Grimes { 5928592604SJoerg Wunsch int nb; 6028592604SJoerg Wunsch fd_set read_set, read_template; 619b50d902SRodney W. Grimes char buf[BUFSIZ]; 629b50d902SRodney W. Grimes struct timeval wait; 639b50d902SRodney W. Grimes 64e151ec23SPeter Wemm message("Connection established"); 65e151ec23SPeter Wemm write(STDOUT_FILENO, "\007\007\007", 3); 66e151ec23SPeter Wemm 679b50d902SRodney W. Grimes current_line = 0; 689b50d902SRodney W. Grimes 699b50d902SRodney W. Grimes /* 709b50d902SRodney W. Grimes * Wait on both the other process (sockt_mask) and 719b50d902SRodney W. Grimes * standard input ( STDIN_MASK ) 729b50d902SRodney W. Grimes */ 7328592604SJoerg Wunsch FD_ZERO(&read_template); 7428592604SJoerg Wunsch FD_SET(sockt, &read_template); 7528592604SJoerg Wunsch FD_SET(fileno(stdin), &read_template); 769b50d902SRodney W. Grimes for (;;) { 779b50d902SRodney W. Grimes read_set = read_template; 789b50d902SRodney W. Grimes wait.tv_sec = A_LONG_TIME; 799b50d902SRodney W. Grimes wait.tv_usec = 0; 809b50d902SRodney W. Grimes nb = select(32, &read_set, 0, 0, &wait); 819b50d902SRodney W. Grimes if (nb <= 0) { 829b50d902SRodney W. Grimes if (errno == EINTR) { 839b50d902SRodney W. Grimes read_set = read_template; 849b50d902SRodney W. Grimes continue; 859b50d902SRodney W. Grimes } 869b50d902SRodney W. Grimes /* panic, we don't know what happened */ 879b50d902SRodney W. Grimes p_error("Unexpected error from select"); 889b50d902SRodney W. Grimes quit(); 899b50d902SRodney W. Grimes } 9028592604SJoerg Wunsch if (FD_ISSET(sockt, &read_set)) { 919b50d902SRodney W. Grimes /* There is data on sockt */ 929b50d902SRodney W. Grimes nb = read(sockt, buf, sizeof buf); 939b50d902SRodney W. Grimes if (nb <= 0) { 949b50d902SRodney W. Grimes message("Connection closed. Exiting"); 959b50d902SRodney W. Grimes quit(); 969b50d902SRodney W. Grimes } 979b50d902SRodney W. Grimes display(&his_win, buf, nb); 989b50d902SRodney W. Grimes } 9928592604SJoerg Wunsch if (FD_ISSET(fileno(stdin), &read_set)) { 1009b50d902SRodney W. Grimes /* 1019b50d902SRodney W. Grimes * We can't make the tty non_blocking, because 1029b50d902SRodney W. Grimes * curses's output routines would screw up 1039b50d902SRodney W. Grimes */ 1049b50d902SRodney W. Grimes ioctl(0, FIONREAD, (struct sgttyb *) &nb); 1059b50d902SRodney W. Grimes nb = read(0, buf, nb); 1069b50d902SRodney W. Grimes display(&my_win, buf, nb); 1079b50d902SRodney W. Grimes /* might lose data here because sockt is non-blocking */ 1089b50d902SRodney W. Grimes write(sockt, buf, nb); 1099b50d902SRodney W. Grimes } 1109b50d902SRodney W. Grimes } 1119b50d902SRodney W. Grimes } 1129b50d902SRodney W. Grimes 1139b50d902SRodney W. Grimes extern int errno; 1149b50d902SRodney W. Grimes extern int sys_nerr; 1159b50d902SRodney W. Grimes 1169b50d902SRodney W. Grimes /* 1179b50d902SRodney W. Grimes * p_error prints the system error message on the standard location 1189b50d902SRodney W. Grimes * on the screen and then exits. (i.e. a curses version of perror) 1199b50d902SRodney W. Grimes */ 12028592604SJoerg Wunsch void 1219b50d902SRodney W. Grimes p_error(string) 1229b50d902SRodney W. Grimes char *string; 1239b50d902SRodney W. Grimes { 124b02b5aadSAndrey A. Chernov wmove(my_win.x_win, current_line, 0); 1259b50d902SRodney W. Grimes wprintw(my_win.x_win, "[%s : %s (%d)]\n", 1269b50d902SRodney W. Grimes string, strerror(errno), errno); 1279b50d902SRodney W. Grimes wrefresh(my_win.x_win); 1289b50d902SRodney W. Grimes move(LINES-1, 0); 1299b50d902SRodney W. Grimes refresh(); 1309b50d902SRodney W. Grimes quit(); 1319b50d902SRodney W. Grimes } 1329b50d902SRodney W. Grimes 1339b50d902SRodney W. Grimes /* 1349b50d902SRodney W. Grimes * Display string in the standard location 1359b50d902SRodney W. Grimes */ 13628592604SJoerg Wunsch void 1379b50d902SRodney W. Grimes message(string) 1389b50d902SRodney W. Grimes char *string; 1399b50d902SRodney W. Grimes { 140b02b5aadSAndrey A. Chernov wmove(my_win.x_win, current_line, 0); 141b02b5aadSAndrey A. Chernov wprintw(my_win.x_win, "[%s]\n", string); 142b02b5aadSAndrey A. Chernov if (current_line < my_win.x_nlines - 1) 1439b50d902SRodney W. Grimes current_line++; 1449b50d902SRodney W. Grimes wrefresh(my_win.x_win); 1459b50d902SRodney W. Grimes } 146