Debian Codegraph @

talk/io.c

raw
1 /*
2 Copyright (C) 1995-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1983, 1993
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 /*
49 * This file contains the I/O handling and the exchange of
50 * edit characters. This connection itself is established in
51 * ctl.c
52 */
53
54 #include <config.h>
55
56 #include <sys/types.h>
57 #include <sys/ioctl.h>
58 #ifdef HAVE_SYS_FILIO_H
59 # include <sys/filio.h>
60 #endif
61 #include <sys/time.h>
62 #include <time.h>
63 #include <stdio.h>
64 #include <unistd.h>
65 #include <errno.h>
66 #include <string.h>
67 #include <sys/select.h>
68 #include "talk.h"
69
70 #define A_LONG_TIME 10000000
71
72 /*
73 * The routine to do the actual talking
74 */
75 int
76 talk (void)
77 {
78 fd_set read_template, read_set;
79 int stdin_fd = fileno (stdin);
80 int i, nb, num_fds;
81 char buf[BUFSIZ];
82 struct timeval wait;
83
84 message ("Connection established");
85 beep ();
86 current_line = 0;
87
88 /*
89 * Wait on both the other process (SOCKET) and stdin.
90 */
91 FD_ZERO (&read_template);
92 FD_SET (sockt, &read_template);
93 FD_SET (stdin_fd, &read_template);
94 num_fds = (stdin_fd > sockt ? stdin_fd : sockt) + 1;
95
96 for (;;)
97 {
98 read_set = read_template;
99 wait.tv_sec = A_LONG_TIME;
100 wait.tv_usec = 0;
101 nb = select (num_fds, &read_set, 0, 0, &wait);
102 if (nb <= 0)
103 {
104 if (errno == EINTR)
105 {
106 read_set = read_template;
107 continue;
108 }
109 /* panic, we don't know what happened */
110 p_error ("Unexpected error from select");
111 quit ();
112 }
113 if (FD_ISSET (sockt, &read_set))
114 {
115 /* There is data on sockt */
116 nb = read (sockt, buf, sizeof buf);
117 if (nb <= 0)
118 {
119 message ("Connection closed. Exiting");
120 quit ();
121 }
122 display (&his_win, buf, nb);
123 }
124 if (FD_ISSET (stdin_fd, &read_set))
125 {
126 /*
127 * We can't make the tty non_blocking, because
128 * curses's output routines would screw up
129 */
130 ioctl (0, FIONREAD, (struct sgttyb *) &nb);
131 for (i = 0; i < nb; i++)
132 buf[i] = getch ();
133 display (&my_win, buf, nb);
134 /* might lose data here because sockt is non-blocking */
135 write (sockt, buf, nb);
136 }
137 }
138 }
139
140 /*
141 * p_error prints the system error message on the standard location
142 * on the screen and then exits, i.e., a curses version of perror.
143 */
144 int
145 p_error (char *string)
146 {
147 if (curses_initialized)
148 {
149 wmove (my_win.x_win, current_line % my_win.x_nlines, 0);
150 wprintw (my_win.x_win, "[%s : %s (%d)]\n",
151 string, strerror (errno), errno);
152 wrefresh (my_win.x_win);
153 move (LINES - 1, 0);
154 refresh ();
155 }
156 else
157 perror (string);
158
159 quit ();
160
161 return 0;
162 }
163
164 /*
165 * Display string in the standard location.
166 */
167 int
168 message (char *string)
169 {
170 if (curses_initialized)
171 {
172 wmove (my_win.x_win, current_line % my_win.x_nlines, 0);
173 wprintw (my_win.x_win, "[%s]", string);
174 wclrtoeol (my_win.x_win);
175 current_line++;
176 wmove (my_win.x_win, current_line % my_win.x_nlines, 0);
177 wrefresh (my_win.x_win);
178 }
179 else if (string && string[0])
180 printf ("[%s]\n", string);
181
182 return 0;
183 }