io.c revision 1.5 1 /* $NetBSD: io.c,v 1.5 1997/10/20 00:23:24 lukem 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 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93";
40 #endif
41 __RCSID("$NetBSD: io.c,v 1.5 1997/10/20 00:23:24 lukem Exp $");
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 "talk.h"
51 #include <sys/ioctl.h>
52 #include <sys/time.h>
53 #include <stdio.h>
54 #include <errno.h>
55 #include <string.h>
56 #include <unistd.h>
57
58 #define A_LONG_TIME 10000000
59
60 /*
61 * The routine to do the actual talking
62 */
63 void
64 talk()
65 {
66 fd_set read_template, read_set;
67 int nb;
68 char buf[BUFSIZ];
69 struct timeval wait;
70
71 message("Connection established\007\007\007");
72 current_line = 0;
73
74 /*
75 * Wait on both the other process (sockt_mask) and
76 * standard input ( STDIN_MASK )
77 */
78 FD_ZERO(&read_template);
79 FD_SET(sockt, &read_template);
80 FD_SET(fileno(stdin), &read_template);
81 for (;;) {
82 read_set = read_template;
83 wait.tv_sec = A_LONG_TIME;
84 wait.tv_usec = 0;
85 nb = select(32, &read_set, 0, 0, &wait);
86 if (nb <= 0) {
87 if (errno == EINTR) {
88 read_set = read_template;
89 continue;
90 }
91 /* panic, we don't know what happened */
92 p_error("Unexpected error from select");
93 quit();
94 }
95 if (FD_ISSET(sockt, &read_set)) {
96 /* There is data on sockt */
97 nb = read(sockt, buf, sizeof buf);
98 if (nb <= 0) {
99 message("Connection closed. Exiting");
100 quit();
101 }
102 display(&his_win, buf, nb);
103 }
104 if (FD_ISSET(fileno(stdin), &read_set)) {
105 /*
106 * We can't make the tty non_blocking, because
107 * curses's output routines would screw up
108 */
109 ioctl(0, FIONREAD, (struct sgttyb *) &nb);
110 nb = read(0, buf, nb);
111 display(&my_win, buf, nb);
112 /* might lose data here because sockt is non-blocking */
113 write(sockt, buf, nb);
114 }
115 }
116 }
117
118 /*
119 * p_error prints the system error message on the standard location
120 * on the screen and then exits. (i.e. a curses version of perror)
121 */
122 void
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 void
139 message(string)
140 char *string;
141 {
142 wmove(my_win.x_win, current_line % my_win.x_nlines, 0);
143 wprintw(my_win.x_win, "[%s]", string);
144 wclrtoeol(my_win.x_win);
145 current_line++;
146 wmove(my_win.x_win, current_line % my_win.x_nlines, 0);
147 wrefresh(my_win.x_win);
148 }
149