1 /* 2 * Copyright (c) 1983 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 */ 17 18 #ifndef lint 19 static char sccsid[] = "@(#)io.c 5.4 (Berkeley) 05/28/90"; 20 #endif /* not lint */ 21 22 /* 23 * This file contains the I/O handling and the exchange of 24 * edit characters. This connection itself is established in 25 * ctl.c 26 */ 27 28 #include <sys/time.h> 29 #include "talk.h" 30 #include <stdio.h> 31 #include <errno.h> 32 #include <string.h> 33 34 #define A_LONG_TIME 10000000 35 #define STDIN_MASK (1<<fileno(stdin)) /* the bit mask for standard 36 input */ 37 38 /* 39 * The routine to do the actual talking 40 */ 41 talk() 42 { 43 register int read_template, sockt_mask; 44 int read_set, nb; 45 char buf[BUFSIZ]; 46 struct timeval wait; 47 48 message("Connection established\007\007\007"); 49 current_line = 0; 50 sockt_mask = (1<<sockt); 51 52 /* 53 * Wait on both the other process (sockt_mask) and 54 * standard input ( STDIN_MASK ) 55 */ 56 read_template = sockt_mask | STDIN_MASK; 57 forever { 58 read_set = read_template; 59 wait.tv_sec = A_LONG_TIME; 60 wait.tv_usec = 0; 61 nb = select(32, &read_set, 0, 0, &wait); 62 if (nb <= 0) { 63 if (errno == EINTR) { 64 read_set = read_template; 65 continue; 66 } 67 /* panic, we don't know what happened */ 68 p_error("Unexpected error from select"); 69 quit(); 70 } 71 if (read_set & sockt_mask) { 72 /* There is data on sockt */ 73 nb = read(sockt, buf, sizeof buf); 74 if (nb <= 0) { 75 message("Connection closed. Exiting"); 76 quit(); 77 } 78 display(&his_win, buf, nb); 79 } 80 if (read_set & STDIN_MASK) { 81 /* 82 * We can't make the tty non_blocking, because 83 * curses's output routines would screw up 84 */ 85 ioctl(0, FIONREAD, (struct sgttyb *) &nb); 86 nb = read(0, buf, nb); 87 display(&my_win, buf, nb); 88 /* might lose data here because sockt is non-blocking */ 89 write(sockt, buf, nb); 90 } 91 } 92 } 93 94 extern int errno; 95 extern int sys_nerr; 96 97 /* 98 * p_error prints the system error message on the standard location 99 * on the screen and then exits. (i.e. a curses version of perror) 100 */ 101 p_error(string) 102 char *string; 103 { 104 wmove(my_win.x_win, current_line%my_win.x_nlines, 0); 105 wprintw(my_win.x_win, "[%s : %s (%d)]\n", 106 string, strerror(errno), errno); 107 wrefresh(my_win.x_win); 108 move(LINES-1, 0); 109 refresh(); 110 quit(); 111 } 112 113 /* 114 * Display string in the standard location 115 */ 116 message(string) 117 char *string; 118 { 119 120 wmove(my_win.x_win, current_line%my_win.x_nlines, 0); 121 wprintw(my_win.x_win, "[%s]\n", string); 122 wrefresh(my_win.x_win); 123 } 124