1 /* $OpenBSD: io.c,v 1.9 1999/03/03 20:43:30 millert 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.9 1999/03/03 20:43:30 millert 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 70 #if defined(NCURSES_VERSION) || defined(beep) 71 message("Connection established"); 72 beep(); 73 beep(); 74 beep(); 75 #else 76 message("Connection established\007\007\007"); 77 #endif 78 current_line = 0; 79 80 /* 81 * Wait on both the other process (sockt_mask) and 82 * standard input ( STDIN_MASK ) 83 */ 84 FD_ZERO(&read_template); 85 FD_SET(sockt, &read_template); 86 FD_SET(fileno(stdin), &read_template); 87 for (;;) { 88 read_set = read_template; 89 wait.tv_sec = A_LONG_TIME; 90 wait.tv_usec = 0; 91 nb = select(32, &read_set, 0, 0, &wait); 92 if (nb <= 0) { 93 if (errno == EINTR) { 94 read_set = read_template; 95 continue; 96 } 97 /* panic, we don't know what happened */ 98 quit("Unexpected error from select", 1); 99 } 100 if (FD_ISSET(sockt, &read_set)) { 101 /* There is data on sockt */ 102 nb = read(sockt, buf, sizeof buf); 103 if (nb <= 0) 104 quit("Connection closed. Exiting", 0); 105 display(&his_win, buf, nb); 106 } 107 if (FD_ISSET(fileno(stdin), &read_set)) { 108 /* 109 * We can't make the tty non_blocking, because 110 * curses's output routines would screw up 111 */ 112 ioctl(0, FIONREAD, &nb); 113 nb = read(0, buf, nb); 114 display(&my_win, buf, nb); 115 /* might lose data here because sockt is non-blocking */ 116 write(sockt, buf, nb); 117 } 118 } 119 } 120 121 /* 122 * Display string in the standard location 123 */ 124 void 125 message(string) 126 char *string; 127 { 128 wmove(my_win.x_win, current_line % my_win.x_nlines, 0); 129 wprintw(my_win.x_win, "[%s]", string); 130 wclrtoeol(my_win.x_win); 131 current_line++; 132 wmove(my_win.x_win, current_line % my_win.x_nlines, 0); 133 wrefresh(my_win.x_win); 134 } 135