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