| 1 | /* $Header: /a/guest/moore/talk/RCS/io.c,v 1.3 83/07/06 00:17:15 moore Exp $ */ |
| 2 | |
| 3 | /* this file contains the I/O handling and the exchange of |
| 4 | edit characters. This connection itself is established in |
| 5 | ctl.c |
| 6 | */ |
| 7 | |
| 8 | #include "talk.h" |
| 9 | #include <stdio.h> |
| 10 | #include <errno.h> |
| 11 | #include <sys/time.h> |
| 12 | |
| 13 | #define A_LONG_TIME 10000000 |
| 14 | #define STDIN_MASK (1<<fileno(stdin)) /* the bit mask for standard |
| 15 | input */ |
| 16 | extern int errno; |
| 17 | |
| 18 | /* |
| 19 | * The routine to do the actual talking |
| 20 | */ |
| 21 | |
| 22 | talk() |
| 23 | { |
| 24 | register int read_template, sockt_mask; |
| 25 | int read_set, nb; |
| 26 | char buf[BUFSIZ]; |
| 27 | struct timeval wait; |
| 28 | |
| 29 | message("Connection established\007\007\007"); |
| 30 | current_line = 0; |
| 31 | |
| 32 | sockt_mask = (1<<sockt); |
| 33 | |
| 34 | /* |
| 35 | * wait on both the other process (sockt_mask) and |
| 36 | * standard input ( STDIN_MASK ) |
| 37 | */ |
| 38 | |
| 39 | read_template = sockt_mask | STDIN_MASK; |
| 40 | |
| 41 | forever { |
| 42 | |
| 43 | read_set = read_template; |
| 44 | |
| 45 | wait.tv_sec = A_LONG_TIME; |
| 46 | wait.tv_usec = 0; |
| 47 | |
| 48 | nb = select(32, &read_set, 0, 0, &wait); |
| 49 | |
| 50 | if (nb <= 0) { |
| 51 | |
| 52 | /* We may be returning from an interupt handler */ |
| 53 | |
| 54 | if (errno == EINTR) { |
| 55 | read_set = read_template; |
| 56 | continue; |
| 57 | } else { |
| 58 | /* panic, we don't know what happened */ |
| 59 | p_error("Unexpected error from select"); |
| 60 | quit(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if ( read_set & sockt_mask ) { |
| 65 | |
| 66 | /* There is data on sockt */ |
| 67 | nb = read(sockt, buf, sizeof buf); |
| 68 | |
| 69 | if (nb <= 0) { |
| 70 | message("Connection closed. Exiting"); |
| 71 | quit(); |
| 72 | } else { |
| 73 | display(&his_win, buf, nb); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if ( read_set & STDIN_MASK ) { |
| 78 | |
| 79 | /* we can't make the tty non_blocking, because |
| 80 | curses's output routines would screw up */ |
| 81 | |
| 82 | ioctl(0, FIONREAD, (struct sgttyb *) &nb); |
| 83 | nb = read(0, buf, nb); |
| 84 | |
| 85 | display(&my_win, buf, nb); |
| 86 | write(sockt, buf, nb); /* We might lose data here |
| 87 | because sockt is non-blocking |
| 88 | */ |
| 89 | |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | extern int errno; |
| 95 | extern int sys_nerr; |
| 96 | extern char *sys_errlist[]; |
| 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 | |
| 102 | p_error(string) |
| 103 | char *string; |
| 104 | { |
| 105 | char *sys; |
| 106 | |
| 107 | sys = "Unknown error"; |
| 108 | if(errno < sys_nerr) { |
| 109 | sys = sys_errlist[errno]; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | wmove(my_win.x_win, current_line%my_win.x_nlines, 0); |
| 114 | wprintw(my_win.x_win, "[%s : %s (%d)]\n", string, sys, errno); |
| 115 | wrefresh(my_win.x_win); |
| 116 | move(LINES-1, 0); |
| 117 | refresh(); |
| 118 | quit(); |
| 119 | } |
| 120 | |
| 121 | /* display string in the standard location */ |
| 122 | |
| 123 | message(string) |
| 124 | char *string; |
| 125 | { |
| 126 | wmove(my_win.x_win, current_line%my_win.x_nlines, 0); |
| 127 | wprintw(my_win.x_win, "[%s]\n", string); |
| 128 | wrefresh(my_win.x_win); |
| 129 | } |