Annotation of src/usr.bin/talk/io.c, Revision 1.1
1.1 ! deraadt 1: /* $NetBSD: io.c,v 1.4 1994/12/09 02:14:20 jtc 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: #ifndef lint
! 37: #if 0
! 38: static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93";
! 39: #endif
! 40: static char rcsid[] = "$NetBSD: io.c,v 1.4 1994/12/09 02:14:20 jtc Exp $";
! 41: #endif /* not lint */
! 42:
! 43: /*
! 44: * This file contains the I/O handling and the exchange of
! 45: * edit characters. This connection itself is established in
! 46: * ctl.c
! 47: */
! 48:
! 49: #include <sys/ioctl.h>
! 50: #include <sys/time.h>
! 51: #include <stdio.h>
! 52: #include <errno.h>
! 53: #include <string.h>
! 54: #include "talk.h"
! 55:
! 56: #define A_LONG_TIME 10000000
! 57: #define STDIN_MASK (1<<fileno(stdin)) /* the bit mask for standard
! 58: input */
! 59:
! 60: /*
! 61: * The routine to do the actual talking
! 62: */
! 63: talk()
! 64: {
! 65: register int read_template, sockt_mask;
! 66: int read_set, nb;
! 67: char buf[BUFSIZ];
! 68: struct timeval wait;
! 69:
! 70: message("Connection established\007\007\007");
! 71: current_line = 0;
! 72: sockt_mask = (1<<sockt);
! 73:
! 74: /*
! 75: * Wait on both the other process (sockt_mask) and
! 76: * standard input ( STDIN_MASK )
! 77: */
! 78: read_template = sockt_mask | STDIN_MASK;
! 79: for (;;) {
! 80: read_set = read_template;
! 81: wait.tv_sec = A_LONG_TIME;
! 82: wait.tv_usec = 0;
! 83: nb = select(32, &read_set, 0, 0, &wait);
! 84: if (nb <= 0) {
! 85: if (errno == EINTR) {
! 86: read_set = read_template;
! 87: continue;
! 88: }
! 89: /* panic, we don't know what happened */
! 90: p_error("Unexpected error from select");
! 91: quit();
! 92: }
! 93: if (read_set & sockt_mask) {
! 94: /* There is data on sockt */
! 95: nb = read(sockt, buf, sizeof buf);
! 96: if (nb <= 0) {
! 97: message("Connection closed. Exiting");
! 98: quit();
! 99: }
! 100: display(&his_win, buf, nb);
! 101: }
! 102: if (read_set & STDIN_MASK) {
! 103: /*
! 104: * We can't make the tty non_blocking, because
! 105: * curses's output routines would screw up
! 106: */
! 107: ioctl(0, FIONREAD, (struct sgttyb *) &nb);
! 108: nb = read(0, buf, nb);
! 109: display(&my_win, buf, nb);
! 110: /* might lose data here because sockt is non-blocking */
! 111: write(sockt, buf, nb);
! 112: }
! 113: }
! 114: }
! 115:
! 116: extern int errno;
! 117: extern int sys_nerr;
! 118:
! 119: /*
! 120: * p_error prints the system error message on the standard location
! 121: * on the screen and then exits. (i.e. a curses version of perror)
! 122: */
! 123: p_error(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 : %s (%d)]\n",
! 128: string, strerror(errno), errno);
! 129: wrefresh(my_win.x_win);
! 130: move(LINES-1, 0);
! 131: refresh();
! 132: quit();
! 133: }
! 134:
! 135: /*
! 136: * Display string in the standard location
! 137: */
! 138: message(string)
! 139: char *string;
! 140: {
! 141: wmove(my_win.x_win, current_line % my_win.x_nlines, 0);
! 142: wprintw(my_win.x_win, "[%s]", string);
! 143: wclrtoeol(my_win.x_win);
! 144: current_line++;
! 145: wmove(my_win.x_win, current_line % my_win.x_nlines, 0);
! 146: wrefresh(my_win.x_win);
! 147: }