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 <curses.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], *p; |
---|
69 | struct timeval wait; |
---|
70 | |
---|
71 | message("Connection established"); |
---|
72 | beep(); |
---|
73 | beep(); |
---|
74 | beep(); |
---|
75 | current_line = 0; |
---|
76 | sockt_mask = (1<<sockt); |
---|
77 | |
---|
78 | /* |
---|
79 | * Wait on both the other process (sockt_mask) and |
---|
80 | * standard input ( STDIN_MASK ) |
---|
81 | */ |
---|
82 | read_template = sockt_mask | STDIN_MASK; |
---|
83 | for (;;) { |
---|
84 | read_set = read_template; |
---|
85 | wait.tv_sec = A_LONG_TIME; |
---|
86 | wait.tv_usec = 0; |
---|
87 | nb = select(32, &read_set, 0, 0, &wait); |
---|
88 | if (nb <= 0) { |
---|
89 | if (errno == EINTR) { |
---|
90 | read_set = read_template; |
---|
91 | continue; |
---|
92 | } |
---|
93 | /* panic, we don't know what happened */ |
---|
94 | p_error("Unexpected error from select"); |
---|
95 | quit(); |
---|
96 | } |
---|
97 | if (read_set & sockt_mask) { |
---|
98 | /* There is data on sockt */ |
---|
99 | nb = read(sockt, buf, sizeof buf); |
---|
100 | if (nb <= 0) { |
---|
101 | message("Connection closed. Exiting"); |
---|
102 | quit(); |
---|
103 | } |
---|
104 | display(&his_win, buf, nb); |
---|
105 | } |
---|
106 | if (read_set & STDIN_MASK) { |
---|
107 | nb = read(0, buf, sizeof(buf)); |
---|
108 | for (p = buf; p < buf + nb; p++) { |
---|
109 | if (*p == '\r') |
---|
110 | *p = '\n'; |
---|
111 | } |
---|
112 | display(&my_win, buf, nb); |
---|
113 | /* might lose data here because sockt is non-blocking */ |
---|
114 | write(sockt, buf, nb); |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | extern int errno; |
---|
120 | extern int sys_nerr; |
---|
121 | |
---|
122 | /* |
---|
123 | * p_error prints the system error message on the standard location |
---|
124 | * on the screen and then exits. (i.e. a curses version of perror) |
---|
125 | */ |
---|
126 | p_error(string) |
---|
127 | char *string; |
---|
128 | { |
---|
129 | wmove(my_win.x_win, current_line%my_win.x_nlines, 0); |
---|
130 | wprintw(my_win.x_win, "[%s : %s (%d)]\n", |
---|
131 | string, strerror(errno), errno); |
---|
132 | wrefresh(my_win.x_win); |
---|
133 | move(LINES-1, 0); |
---|
134 | refresh(); |
---|
135 | quit(); |
---|
136 | } |
---|
137 | |
---|
138 | /* |
---|
139 | * Display string in the standard location |
---|
140 | */ |
---|
141 | message(string) |
---|
142 | char *string; |
---|
143 | { |
---|
144 | wmove(my_win.x_win, current_line % my_win.x_nlines, 0); |
---|
145 | wprintw(my_win.x_win, "[%s]", string); |
---|
146 | wclrtoeol(my_win.x_win); |
---|
147 | current_line++; |
---|
148 | wmove(my_win.x_win, current_line % my_win.x_nlines, 0); |
---|
149 | wrefresh(my_win.x_win); |
---|
150 | } |
---|