Annotation of OpenXM/src/gnuplot/plot.c.diff, Revision 1.2
1.1 maekawa 1: $OpenXM$
2:
1.2 ! maekawa 3: --- plot.c.orig Tue Feb 1 19:09:06 2000
! 4: +++ plot.c Tue Feb 1 19:09:12 2000
1.1 maekawa 5: @@ -42,6 +42,23 @@
6: #include "fnproto.h"
7: #include <setjmp.h>
8:
9: +/* TAKAYAMA Nobuki */
10: +#include <sys/types.h>
11: +#include <sys/socket.h>
12: +#include <sys/time.h>
13: +#include <netinet/in.h>
14: +#include <netdb.h>
15: +
16: +FILE *openTCP __P((char *));
17: +
18: +int socketConnect __P((char *, int));
19: +int socketOpen __P((char *, int));
20: +int socketAccept __P((int));
21: +
22: +int oxSocketSelect0 __P((int, int));
23: +int oxSocketMultiSelect __P((int *, int, int, int *));
24: +/* END */
25: +
26: #if defined(MSDOS) || defined(DOS386) || defined(__EMX__)
27: # include <io.h>
28: #endif
1.2 ! maekawa 29: @@ -321,6 +338,7 @@
1.1 maekawa 30: int argc;
31: char **argv;
32: {
33: + FILE *fp;
34: #ifdef LINUXVGA
35: LINUX_setup();
36: #endif
1.2 ! maekawa 37: @@ -527,8 +545,12 @@
1.1 maekawa 38:
1.2 ! maekawa 39: /* interactive = FALSE; */ /* should this be here? */
1.1 maekawa 40:
41: - } else
42: - load_file(fopen(*argv, "r"), *argv, FALSE);
43: + } else {
44: + fp = openTCP(*argv);
45: + load_file(fp, *argv, FALSE);
46: + fprintf(stderr, "gnuplot : EOF or there was an error"
47: + "in the input stream.\n");
48: + }
49: }
50: #ifdef _Windows
51: if (noend) {
1.2 ! maekawa 52: @@ -725,3 +747,308 @@
1.1 maekawa 53: return 0;
54: }
55: #endif
56: +
57: +/*
58: + * TAKAYAMA Nobuki
59: + */
60: +FILE *
61: +openTCP(name)
62: + char *name;
63: +{
64: + FILE *fp;
65: + int fd, port, reverse = 0;
66: +
67: + fprintf(stderr, "openTCP port number : %s\n", name);
68: +
69: + if (name[0] == 'r') {
70: + fprintf(stderr, "openTCP : trying to reverse connetion.\n");
71: + reverse = 1;
72: + sscanf(&name[1], "%d", &port);
73: + } else {
74: + sscanf(name, "%d", &port);
75: + }
76: +
77: + if (reverse) {
78: + fd = socketConnect("localhost", port);
79: + fprintf(stderr, "socketConnect is succeded: fd = %d.", fd);
80: + } else {
81: + fprintf(stderr, "Port number is %d.\n", port);
82: + fd = socketOpen("localhost", port);
83: + fprintf(stderr, "socketOpen is succeded: fd = %d.", fd);
84: + fd = socketAccept(fd);
85: + }
86: +
87: + fprintf(stderr, "\n Port %d : Connected.\n", port);
88: + fp = fdopen(fd, "r");
89: +
90: + return(fp);
91: +}
92: +
93: +#define READBUFSIZE 10000
94: +
95: +FILE *TcpioError = stderr;
96: +int OpenedSocket = 0, Quiet = 0;
97: +
98: +int
99: +socketConnect(serverName, portNumber)
100: + char *serverName;
101: + int portNumber;
102: +{
103: + struct hostent *servhost;
104: + struct sockaddr_in serv;
105: + int socketid, on;
106: +
107: + if ((servhost = gethostbyname(serverName)) == NULL) {
108: + fprintf(stderr, "Bad server name.\n\n");
109: + return (-1);
110: + }
111: +
112: + bzero((void *)&serv, sizeof(serv));
113: + serv.sin_family = AF_INET;
114: + serv.sin_port = htons(portNumber);
115: + bcopy(servhost->h_addr, (void *)&serv.sin_addr, servhost->h_length);
116: +
117: + if ((socketid = socket(AF_INET, SOCK_STREAM, 0)) <0) {
118: + fprintf(stderr, "Socket allocation is failed.\n\n");
119: + return (-1);
120: + }
121: +
122: +#if 0 /* XXX */
123: + on = 1;
124: + setsockopt(socketid, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
125: +#endif
126: +
127: + if (!Quiet) {
128: + fprintf(TcpioError, "Trying to connect port %d, ip=%x\n",
129: + ntohs(serv.sin_port), serv.sin_addr);
130: + }
131: +
132: + if (connect(socketid, (struct sockaddr *)&serv, sizeof(serv)) == -1) {
133: + fprintf(stderr, "cannot connect\n");
134: + return (-1);
135: + }
136: +
137: + if (!Quiet)
138: + fprintf(TcpioError, "connected.\n");
139: +
140: + return(socketid);
141: +}
142: +
143: +int
144: +socketOpen(serverName, portNumber)
145: + char *serverName;
146: + int portNumber;
147: +{
148: + static struct hostent *servhost;
149: + static struct sockaddr_in serv;
150: + static int s_wait;
151: + static int on;
152: + extern int errno;
153: + int tt;
154: +
155: + fprintf(TcpioError, "Hello from open. serverName is %s "
156: + "and portNumber is %d\n", serverName, portNumber);
157: +
158: + if ((servhost = gethostbyname(serverName)) == NULL) {
159: + fprintf(stderr, "Bad server name.\n");
160: + return (-1);
161: + }
162: +
163: + bzero((void *)&serv, sizeof(serv));
164: + serv.sin_family = AF_INET;
165: + serv.sin_port = htons(portNumber);
166: + bcopy(servhost->h_addr, &serv.sin_addr, servhost->h_length);
167: +
168: + if ((s_wait = socket(AF_INET,SOCK_STREAM, 0)) < 0) {
169: + fprintf(stderr, "Socket allocation is failed.\n");
170: + return (-1);
171: + }
172: +
173: + on = 1;
174: + setsockopt(s_wait, SOL_SOCKET,SO_REUSEADDR, &on, sizeof(on));
175: +
176: + /* important */
177: + if ((tt = bind(s_wait, (struct sockaddr *)&serv, sizeof(serv))) == -1) {
178: + fprintf(TcpioError, "bind error. Error no is %d. "
179: + "See /usr/include/sys/errno.h. "
180: + "(asm/errno.h)\n", errno);
181: + fprintf(stderr, "cannot bind\n");
182: + return (-1);
183: + }
184: +
185: +#if 0 /* XXX */
186: + printf("bind returns %d\n", tt);
187: +#endif
188: +
189: + tt = sizeof(serv);
190: + if (getsockname(s_wait, (struct sockaddr *)&serv, &tt) < 0) {
191: + fprintf(TcpioError, "getsockname error. Error no is %d. "
192: + "See /usr/include/sys/errno.h "
193: + "(asm/errno.h).\n", errno);
194: + fprintf(stderr, "cannot getsockname\n");
195: + return (-1);
196: + }
197: +
198: + if (listen(s_wait, 1) < 0) {
199: + fprintf(stderr, "Listen failed\n");
200: + return (-1);
201: + }
202: +
203: + fprintf(TcpioError, "Done the initialization. "
204: + "port =%d\n", ntohs(serv.sin_port));
205: + OpenedSocket = ntohs(serv.sin_port);
206: +
207: + return (s_wait);
208: +}
209: +
210: +int
211: +socketAccept(snum)
212: + int snum;
213: +{
214: + int s, news;
215: +
216: + s = snum;
217: +
218: + fprintf(TcpioError, "Trying to accept... ");
219: + fflush(TcpioError);
220: +
221: + if ((news = accept(s, NULL, NULL)) < 0) {
222: + fprintf(stderr, "Error in accept.\n");
223: + return (-1);
224: + }
225: +
226: + fprintf(TcpioError, "Accepted.\n");
227: + fflush(TcpioError);
228: +
229: + if (close(s) < 0) {
230: + fprintf(stderr, "Error in closing the old socket.\n");
231: + return (-1);
232: + }
233: +
234: + return(news);
235: +}
236: +
237: +int
238: +oxSocketSelect0(fd, t)
239: + int fd, t;
240: +{
241: + fd_set readfds;
242: + struct timeval timeout;
243: + int debug = 0;
244: + extern int errno;
245: +
246: + FD_ZERO(&readfds);
247: + FD_SET(fd, &readfds);
248: + timeout.tv_sec = 0;
249: + timeout.tv_usec = (long)t;
250: +
251: + if (t >= 0) {
252: + if (debug) {
253: + printf("select t >= 0 for fd = %d\n", fd);
254: + fflush(NULL);
255: + }
256: +
257: + /* It must be fd + 1!, Not fd. */
258: + if (select(fd + 1, &readfds, NULL, NULL, &timeout) < 0) {
259: + fprintf(TcpioError, "select (non-block) error. "
260: + "Error no is %d. "
261: + "See /usr/include/sys/errno.h "
262: + "(asm/errno.h).\n", errno);
263: + fprintf(stderr, "oxSocketSelect0() : select failed.\n");
264: + return (0);
265: + }
266: +
267: + if (debug) {
268: + printf("Return from select t >= 0 for fd = %d\n", fd);
269: + fflush(NULL);
270: + }
271: + } else {
272: + /* block */
273: + if (select(fd + 1, &readfds, NULL, NULL, NULL) < 0) {
274: + fprintf(TcpioError, "select (block) error. "
275: + "Error no is %d. "
276: + "See /usr/include/sys/errno.h "
277: + "(asm/errno.h).\n", errno);
278: + fprintf(stderr, "socketSelect0() : select failed.\n");
279: + return (0);
280: + }
281: + }
282: +
283: + if (FD_ISSET(fd, &readfds)) {
284: + return(1);
285: + } else {
286: + return(0);
287: + }
288: +}
289: +
290: +int
291: +oxSocketMultiSelect(sid, size, t, result)
292: + int sid[], size, t, result[];
293: +{
294: + fd_set readfds;
295: + struct timeval timeout;
296: + int i, fd, p, isdata = 0;
297: + extern int errno;
298: +
299: + FD_ZERO(&readfds);
300: + timeout.tv_sec = 0;
301: + timeout.tv_usec = (long)t;
302: +
303: + fd = 0;
304: +
305: + for (i = 0 ; i < size ; i++) {
306: + if (sid[i] >= 0) {
307: + p = sid[i];
308: + if (p > fd)
309: + fd = p;
310: + FD_SET(p,&readfds);
311: +#if 0 /* XXX */
312: + printf("p = %d, fd=%d", p, fd);
313: +#endif
314: + }
315: + }
316: +
317: +#if 0 /* XXX */
318: + printf("MultiSelect..\n");
319: + fflush(NULL);
320: +#endif
321: +
322: + if (t >= 0) {
323: + /* It must be fd + 1!, Not fd. */
324: + if (select(fd + 1, &readfds, NULL, NULL, &timeout) < 0) {
325: + fprintf(TcpioError, "Select error. Error no is %d. "
326: + "See /usr/include/sys/errno.h "
327: + "(asm/errno.h).\n", errno);
328: + fprintf(stderr, "oxSocketMultiSelect() : "
329: + "select failed.\n");
330: + return (0);
331: + }
332: + } else {
333: + /* block */
334: + if (select(fd + 1, &readfds, NULL, NULL, NULL) < 0) {
335: + fprintf(TcpioError, "Select error. Error no is %d. "
336: + "See /usr/include/sys/errno.h "
337: + "(asm/errno.h).\n", errno);
338: + fprintf(stderr, "oxSocketMultiSelect() : "
339: + "(block) select failed.\n");
340: + return (0);
341: + }
342: + }
343: +
344: +#if 0 /* XXX */
345: + printf("Done. MultiSelect.\n");
346: + fflush(NULL);
347: +#endif
348: +
349: + for (i = 0 ; i < size ; i++) {
350: + p = sid[i];
351: + if ((sid[i] >= 0) && FD_ISSET(p, &readfds)) {
352: + result[i] = 1;
353: + isdata = 1;
354: + } else {
355: + result[i] = 0;
356: + }
357: + }
358: +
359: + return (isdata);
360: +}
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>