Annotation of src/usr.bin/talk/io.c, Revision 1.6
1.6 ! millert 1: /* $OpenBSD: io.c,v 1.5 1998/04/28 22:13:28 pjanzen 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.6 ! millert 41: static char rcsid[] = "$OpenBSD: io.c,v 1.5 1998/04/28 22:13:28 pjanzen 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:
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.2 tholo 70: #ifdef NCURSES_VERSION
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();
1.4 pjanzen 78: refresh();
1.2 tholo 79: #else
1.1 deraadt 80: message("Connection established\007\007\007");
1.2 tholo 81: #endif
1.1 deraadt 82: current_line = 0;
83:
84: /*
85: * Wait on both the other process (sockt_mask) and
86: * standard input ( STDIN_MASK )
87: */
1.5 pjanzen 88: FD_ZERO(&read_template);
89: FD_SET(sockt, &read_template);
90: FD_SET(fileno(stdin), &read_template);
1.1 deraadt 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: }
1.5 pjanzen 105: if (FD_ISSET(sockt, &read_set)) {
1.1 deraadt 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: }
1.5 pjanzen 114: if (FD_ISSET(fileno(stdin), &read_set)) {
1.1 deraadt 115: /*
116: * We can't make the tty non_blocking, because
117: * curses's output routines would screw up
118: */
1.6 ! millert 119: ioctl(0, FIONREAD, &nb);
1.1 deraadt 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: /*
129: * p_error prints the system error message on the standard location
130: * on the screen and then exits. (i.e. a curses version of perror)
131: */
1.5 pjanzen 132: void
1.1 deraadt 133: p_error(string)
134: char *string;
135: {
136: wmove(my_win.x_win, current_line%my_win.x_nlines, 0);
137: wprintw(my_win.x_win, "[%s : %s (%d)]\n",
138: string, strerror(errno), errno);
139: wrefresh(my_win.x_win);
140: move(LINES-1, 0);
141: refresh();
142: quit();
143: }
144:
145: /*
146: * Display string in the standard location
147: */
1.5 pjanzen 148: void
1.1 deraadt 149: message(string)
150: char *string;
151: {
152: wmove(my_win.x_win, current_line % my_win.x_nlines, 0);
153: wprintw(my_win.x_win, "[%s]", string);
154: wclrtoeol(my_win.x_win);
155: current_line++;
156: wmove(my_win.x_win, current_line % my_win.x_nlines, 0);
157: wrefresh(my_win.x_win);
158: }