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