[BACK]Return to io.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / talk

Annotation of src/usr.bin/talk/io.c, Revision 1.4

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