annotate lib-src/tcp.c @ 9108:c0287cefc0f8

(record_insert, truncate_undo_list, Fprimitive_undo): Use type test macros.
author Karl Heuer <kwzh@gnu.org>
date Tue, 27 Sep 1994 01:13:35 +0000
parents fa94f629c732
children ee40177f6c68
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6110
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
1 /*
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
2 * TCP/IP stream emulation for GNU Emacs.
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
3 * Copyright (C) 1988, 1989, 1992, 1993 Free Software Foundation, Inc.
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
4
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
5 * Author: Masanobu Umeda
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
6 * Maintainer: umerin@mse.kyutech.ac.jp
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
7
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
8 This file is part of GNU Emacs.
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
9
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
10 GNU Emacs is free software; you can redistribute it and/or modify
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
11 it under the terms of the GNU General Public License as published by
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
12 the Free Software Foundation; either version 2, or (at your option)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
13 any later version.
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
14
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
15 GNU Emacs is distributed in the hope that it will be useful,
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
18 GNU General Public License for more details.
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
19
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
20 You should have received a copy of the GNU General Public License
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
21 along with GNU Emacs; see the file COPYING. If not, write to
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
22 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
23
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
24 *
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
25 * Yasunari, Itoh at PFU limited contributed for Fujitsu UTS and SX/A.
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
26 *
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
27 * Thu Apr 6 13:47:37 JST 1989
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
28 * USG fixes by Sakaeda <saka@mickey.trad.pf.fujitsu.junet>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
29 *
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
30 * For Fujitsu UTS compile with:
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
31 * cc -O -o tcp tcp.c -DFUJITSU_UTS -lu -lsocket
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
32 */
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
33
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
34 #include <stdio.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
35 #include <fcntl.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
36 #include <ctype.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
37 #include <sys/types.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
38
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
39 #ifdef FUJITSU_UTS
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
40 #define USG
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
41 #include <sys/ucbtypes.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
42 #include <sys/tisp/socket.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
43 #include <netdb.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
44 #include <sys/tisp/in.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
45 #else
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
46 #include <sys/socket.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
47 #include <netdb.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
48 #include <netinet/in.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
49 #endif
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
50
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
51 #ifdef USG
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
52 #include <sys/stat.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
53 #include <signal.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
54 #endif
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
55
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
56 #ifdef FUJITSU_UTS
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
57 #define bcopy(f, t, n) memcpy (t, f, n)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
58 #define bcmp(b1, b2, n) (memcmp (b1, b2, n)!=0)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
59 #define bzero(b, n) memset (b, 0, n)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
60 #endif
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
61
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
62 #ifdef USG
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
63 int selectable = 1;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
64
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
65 sigout ()
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
66 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
67 fcntl (fileno (stdin), F_SETFL, 0);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
68 exit (-1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
69 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
70 #endif
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
71
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
72 main (argc, argv)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
73 int argc;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
74 char *argv[];
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
75 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
76 struct hostent *host;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
77 struct sockaddr_in sockin, sockme;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
78 struct servent *serv;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
79 char *hostname = NULL;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
80 char *service = "nntp";
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
81 int port;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
82 int readfds;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
83 int writefds;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
84 int server; /* NNTP Server */
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
85 int emacsIn = fileno (stdin); /* Emacs intput */
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
86 int emacsOut = fileno (stdout); /* Emacs output */
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
87 char buffer[1024];
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
88 int nbuffer; /* Number of bytes in buffer */
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
89 int wret;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
90 char *retry; /* retry bufferp */
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
91 int false = 0; /* FALSE flag for setsockopt () */
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
92
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
93 if (argc < 2)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
94 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
95 fprintf (stderr, "Usage: %s HOST [SERVICE]\n", argv[0]);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
96 exit (1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
97 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
98 if (argc >= 2)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
99 hostname = argv[1];
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
100 if (argc >= 3)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
101 service = argv[2];
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
102
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
103 if ((host = gethostbyname (hostname)) == NULL)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
104 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
105 perror ("gethostbyname");
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
106 exit (1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
107 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
108 if (isdigit (service[0]))
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
109 port = atoi (service);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
110 else
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
111 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
112 serv = getservbyname (service, "tcp");
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
113 if (serv == NULL)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
114 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
115 perror ("getservbyname");
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
116 exit (1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
117 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
118 port = serv->s_port;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
119 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
120
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
121 bzero (&sockin, sizeof (sockin));
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
122 sockin.sin_family = host->h_addrtype;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
123 bcopy (host->h_addr, &sockin.sin_addr, host->h_length);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
124 sockin.sin_port = port;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
125 if ((server = socket (AF_INET, SOCK_STREAM, 0)) < 0)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
126 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
127 perror ("socket");
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
128 exit (1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
129 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
130 if (setsockopt (server, SOL_SOCKET, SO_REUSEADDR, &false, sizeof (false)))
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
131 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
132 perror ("setsockopt");
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
133 exit (1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
134 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
135 bzero (&sockme, sizeof (sockme));
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
136 sockme.sin_family = sockin.sin_family;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
137 sockme.sin_addr.s_addr = INADDR_ANY;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
138 if (bind (server, &sockme, sizeof (sockme)) < 0)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
139 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
140 perror ("bind");
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
141 exit (1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
142 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
143 if (connect (server, &sockin, sizeof (sockin)) < 0)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
144 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
145 perror ("connect");
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
146 close (server);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
147 exit (1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
148 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
149
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
150 #ifdef O_NDELAY
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
151 fcntl (server, F_SETFL, O_NDELAY);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
152
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
153 #ifdef USG
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
154 /* USG pipe cannot not select emacsIn */
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
155 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
156 struct stat statbuf;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
157 fstat (emacsIn, &statbuf);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
158 if (statbuf.st_mode & 010000)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
159 selectable = 0;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
160 if (!selectable)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
161 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
162 signal (SIGINT, sigout);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
163 fcntl (emacsIn, F_SETFL, O_NDELAY);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
164 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
165 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
166 #endif
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
167 #endif
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
168
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
169 /* Connection established. */
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
170 while (1)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
171 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
172 readfds = (1 << server) | (1 << emacsIn);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
173 if (select (32, &readfds, NULL, NULL, (struct timeval *)NULL) == -1)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
174 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
175 perror ("select");
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
176 exit (1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
177 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
178 if (readfds & (1 << emacsIn))
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
179 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
180 /* From Emacs */
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
181 nbuffer = read (emacsIn, buffer, sizeof buffer -1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
182
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
183 #ifdef USG
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
184 if (selectable && nbuffer == 0)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
185 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
186 goto finish;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
187 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
188 else if (!(readfds & (1 << server)) && nbuffer == 0)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
189 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
190 sleep (1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
191 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
192 else
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
193 #else
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
194 if (nbuffer == 0)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
195 goto finish;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
196 #endif
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
197 for (retry = buffer; nbuffer > 0; nbuffer -= wret, retry += wret)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
198 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
199 writefds = 1 << server;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
200 if (select (server+1, NULL, &writefds, NULL, (struct timeval*)NULL) == -1)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
201 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
202 perror ("select");
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
203 exit (1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
204 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
205 wret = write (server, retry, nbuffer);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
206 if (wret < 0) goto finish;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
207 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
208 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
209 if (readfds & (1 << server))
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
210 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
211 /* From NNTP server */
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
212 nbuffer = read (server, buffer, sizeof buffer -1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
213 if (nbuffer == 0)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
214 goto finish;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
215 for (retry = buffer; nbuffer > 0; nbuffer -= wret, retry += wret)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
216 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
217 writefds = 1 << emacsOut;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
218 #ifdef USG
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
219 if (selectable)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
220 #endif
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
221 if (select (emacsOut+1, NULL, &writefds, NULL, (struct timeval*)NULL) == -1)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
222 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
223 perror ("select");
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
224 exit (1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
225 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
226 wret = write (emacsOut, retry, nbuffer);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
227 if (wret < 0) goto finish;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
228 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
229 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
230 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
231
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
232 /* End of communication. */
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
233 finish:
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
234 close (server);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
235 #ifdef USG
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
236 if (!selectable) fcntl (emacsIn, F_SETFL, 0);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
237 #endif
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
238 close (emacsIn);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
239 close (emacsOut);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
240 exit (0);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
241 }