1 /* $OpenBSD: io.c,v 1.10 2001/09/05 00:29:20 deraadt Exp $ */ 2 /* $NetBSD: io.c,v 1.4 1994/12/09 02:14:20 jtc Exp $ */ 3 4 /* 5 * Copyright (c) 1983, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93"; 40 #endif 41 static char rcsid[] = "$OpenBSD: io.c,v 1.10 2001/09/05 00:29:20 deraadt Exp $"; 42 #endif /* not lint */ 43 44 /* 45 * This file contains the I/O handling and the exchange of 46 * edit characters. This connection itself is established in 47 * ctl.c 48 */ 49 50 #include "talk.h" 51 #include <sys/ioctl.h> 52 #include <sys/time.h> 53 #include <stdio.h> 54 #include <errno.h> 55 #include <unistd.h> 56 57 #define A_LONG_TIME 10000000 58 59 /* 60 * The routine to do the actual talking 61 */ 62 void 63 talk() 64 { 65 fd_set read_template, read_set; 66 int nb; 67 char buf[BUFSIZ]; 68 struct timeval wait; 69 int maxfd = 0; 70 71 #if defined(NCURSES_VERSION) || defined(beep) 72 message("Connection established"); 73 beep(); 74 beep(); 75 beep(); 76 #else 77 message("Connection established\007\007\007"); 78 #endif 79 current_line = 0; 80 81 /* 82 * Wait on both the other process (sockt_mask) and 83 * standard input ( STDIN_MASK ) 84 */ 85 FD_ZERO(&read_template); 86 FD_SET(fileno(stdin), &read_template); 87 if (fileno(stdin) > maxfd) 88 maxfd = fileno(stdin); 89 FD_SET(sockt, &read_template); 90 if (sockt > maxfd) 91 maxfd = sockt; 92 for (;;) { 93 read_set = read_template; 94 wait.tv_sec = A_LONG_TIME; 95 wait.tv_usec = 0; 96 nb = select(maxfd + 1, &read_set, 0, 0, &wait); 97 if (nb <= 0) { 98 if (errno == EINTR) { 99 read_set = read_template; 100 continue; 101 } 102 /* panic, we don't know what happened */ 103 quit("Unexpected error from select", 1); 104 } 105 if (FD_ISSET(sockt, &read_set)) { 106 /* There is data on sockt */ 107 nb = read(sockt, buf, sizeof buf); 108 if (nb <= 0) 109 quit("Connection closed. Exiting", 0); 110 display(&his_win, buf, nb); 111 } 112 if (FD_ISSET(fileno(stdin), &read_set)) { 113 /* 114 * We can't make the tty non_blocking, because 115 * curses's output routines would screw up 116 */ 117 ioctl(0, FIONREAD, &nb); 118 nb = read(0, buf, nb); 119 display(&my_win, buf, nb); 120 /* might lose data here because sockt is non-blocking */ 121 write(sockt, buf, nb); 122 } 123 } 124 } 125 126 /* 127 * Display string in the standard location 128 */ 129 void 130 message(string) 131 char *string; 132 { 133 wmove(my_win.x_win, current_line % my_win.x_nlines, 0); 134 wprintw(my_win.x_win, "[%s]", string); 135 wclrtoeol(my_win.x_win); 136 current_line++; 137 wmove(my_win.x_win, current_line % my_win.x_nlines, 0); 138 wrefresh(my_win.x_win); 139 } 140