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