annotate lib-src/tcp.c @ 26370:5f52cc1417ab

Use block statements in cases and declare v1 and v2 locally there. Rearrange case statements so that thos most frequently executed come first. Avoid goto's in frequently executed cases.
author Gerd Moellmann <gerd@gnu.org>
date Sun, 07 Nov 1999 13:06:59 +0000
parents 38829b121db7
children 60712692fdbc
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
14186
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 6110
diff changeset
22 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 6110
diff changeset
23 Boston, MA 02111-1307, USA.
6110
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 *
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
26 * Yasunari, Itoh at PFU limited contributed for Fujitsu UTS and SX/A.
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
27 *
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
28 * Thu Apr 6 13:47:37 JST 1989
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
29 * USG fixes by Sakaeda <saka@mickey.trad.pf.fujitsu.junet>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
30 *
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
31 * For Fujitsu UTS compile with:
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
32 * cc -O -o tcp tcp.c -DFUJITSU_UTS -lu -lsocket
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
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
35 #include <stdio.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
36 #include <fcntl.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
37 #include <ctype.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
38 #include <sys/types.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
39
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
40 #ifdef FUJITSU_UTS
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
41 #define USG
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
42 #include <sys/ucbtypes.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
43 #include <sys/tisp/socket.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
44 #include <netdb.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
45 #include <sys/tisp/in.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
46 #else
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
47 #include <sys/socket.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
48 #include <netdb.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
49 #include <netinet/in.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
50 #endif
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
51
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
52 #ifdef USG
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
53 #include <sys/stat.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
54 #include <signal.h>
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
55 #endif
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
56
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
57 #ifdef FUJITSU_UTS
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
58 #define bcopy(f, t, n) memcpy (t, f, n)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
59 #define bcmp(b1, b2, n) (memcmp (b1, b2, n)!=0)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
60 #define bzero(b, n) memset (b, 0, n)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
61 #endif
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
62
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
63 #ifdef USG
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
64 int selectable = 1;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
65
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
66 sigout ()
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
67 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
68 fcntl (fileno (stdin), F_SETFL, 0);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
69 exit (-1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
70 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
71 #endif
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
72
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
73 main (argc, argv)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
74 int argc;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
75 char *argv[];
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
76 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
77 struct hostent *host;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
78 struct sockaddr_in sockin, sockme;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
79 struct servent *serv;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
80 char *hostname = NULL;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
81 char *service = "nntp";
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
82 int port;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
83 int readfds;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
84 int writefds;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
85 int server; /* NNTP Server */
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
86 int emacsIn = fileno (stdin); /* Emacs intput */
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
87 int emacsOut = fileno (stdout); /* Emacs output */
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
88 char buffer[1024];
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
89 int nbuffer; /* Number of bytes in buffer */
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
90 int wret;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
91 char *retry; /* retry bufferp */
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
92 int false = 0; /* FALSE flag for setsockopt () */
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
93
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
94 if (argc < 2)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
95 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
96 fprintf (stderr, "Usage: %s HOST [SERVICE]\n", argv[0]);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
97 exit (1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
98 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
99 if (argc >= 2)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
100 hostname = argv[1];
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
101 if (argc >= 3)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
102 service = argv[2];
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
103
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
104 if ((host = gethostbyname (hostname)) == NULL)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
105 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
106 perror ("gethostbyname");
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
107 exit (1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
108 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
109 if (isdigit (service[0]))
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
110 port = atoi (service);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
111 else
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
112 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
113 serv = getservbyname (service, "tcp");
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
114 if (serv == NULL)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
115 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
116 perror ("getservbyname");
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
117 exit (1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
118 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
119 port = serv->s_port;
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
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
122 bzero (&sockin, sizeof (sockin));
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
123 sockin.sin_family = host->h_addrtype;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
124 bcopy (host->h_addr, &sockin.sin_addr, host->h_length);
14608
38829b121db7 (main): Convert port to network byte order.
Karl Heuer <kwzh@gnu.org>
parents: 14186
diff changeset
125 sockin.sin_port = htons (port);
6110
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
126 if ((server = socket (AF_INET, SOCK_STREAM, 0)) < 0)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
127 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
128 perror ("socket");
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
129 exit (1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
130 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
131 if (setsockopt (server, SOL_SOCKET, SO_REUSEADDR, &false, sizeof (false)))
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
132 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
133 perror ("setsockopt");
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
134 exit (1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
135 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
136 bzero (&sockme, sizeof (sockme));
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
137 sockme.sin_family = sockin.sin_family;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
138 sockme.sin_addr.s_addr = INADDR_ANY;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
139 if (bind (server, &sockme, sizeof (sockme)) < 0)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
140 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
141 perror ("bind");
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
142 exit (1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
143 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
144 if (connect (server, &sockin, sizeof (sockin)) < 0)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
145 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
146 perror ("connect");
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
147 close (server);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
148 exit (1);
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
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
151 #ifdef O_NDELAY
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
152 fcntl (server, F_SETFL, O_NDELAY);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
153
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
154 #ifdef USG
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
155 /* USG pipe cannot not select emacsIn */
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
156 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
157 struct stat statbuf;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
158 fstat (emacsIn, &statbuf);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
159 if (statbuf.st_mode & 010000)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
160 selectable = 0;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
161 if (!selectable)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
162 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
163 signal (SIGINT, sigout);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
164 fcntl (emacsIn, F_SETFL, O_NDELAY);
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 }
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 #endif
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
169
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
170 /* Connection established. */
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
171 while (1)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
172 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
173 readfds = (1 << server) | (1 << emacsIn);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
174 if (select (32, &readfds, NULL, NULL, (struct timeval *)NULL) == -1)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
175 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
176 perror ("select");
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
177 exit (1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
178 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
179 if (readfds & (1 << emacsIn))
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
180 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
181 /* From Emacs */
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
182 nbuffer = read (emacsIn, buffer, sizeof buffer -1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
183
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
184 #ifdef USG
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
185 if (selectable && nbuffer == 0)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
186 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
187 goto finish;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
188 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
189 else if (!(readfds & (1 << server)) && nbuffer == 0)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
190 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
191 sleep (1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
192 }
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 #else
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
195 if (nbuffer == 0)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
196 goto finish;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
197 #endif
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
198 for (retry = buffer; nbuffer > 0; nbuffer -= wret, retry += wret)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
199 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
200 writefds = 1 << server;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
201 if (select (server+1, NULL, &writefds, NULL, (struct timeval*)NULL) == -1)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
202 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
203 perror ("select");
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
204 exit (1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
205 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
206 wret = write (server, retry, nbuffer);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
207 if (wret < 0) goto finish;
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 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
210 if (readfds & (1 << server))
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
211 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
212 /* From NNTP server */
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
213 nbuffer = read (server, buffer, sizeof buffer -1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
214 if (nbuffer == 0)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
215 goto finish;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
216 for (retry = buffer; nbuffer > 0; nbuffer -= wret, retry += wret)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
217 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
218 writefds = 1 << emacsOut;
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
219 #ifdef USG
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
220 if (selectable)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
221 #endif
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
222 if (select (emacsOut+1, NULL, &writefds, NULL, (struct timeval*)NULL) == -1)
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
223 {
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
224 perror ("select");
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
225 exit (1);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
226 }
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
227 wret = write (emacsOut, retry, nbuffer);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
228 if (wret < 0) goto finish;
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
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
233 /* End of communication. */
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
234 finish:
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
235 close (server);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
236 #ifdef USG
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
237 if (!selectable) fcntl (emacsIn, F_SETFL, 0);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
238 #endif
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
239 close (emacsIn);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
240 close (emacsOut);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
241 exit (0);
fa94f629c732 Initial revision
David J. MacKenzie <djm@gnu.org>
parents:
diff changeset
242 }