Annotation of OpenXM/src/kan96xx/plugin/file2.c, Revision 1.18
1.18 ! takayama 1: /*$OpenXM: OpenXM/src/kan96xx/plugin/file2.c,v 1.17 2020/10/06 11:33:47 takayama Exp $ */
1.1 maekawa 2: #include <stdio.h>
1.13 ohara 3: #include <string.h>
1.1 maekawa 4: #include <sys/time.h>
5: #include <sys/types.h>
6: #include <unistd.h>
1.12 takayama 7: #include <signal.h>
1.10 takayama 8: #include <errno.h>
1.15 takayama 9: #include <stdlib.h>
1.1 maekawa 10: #include "file2.h"
1.16 takayama 11: #include "mysig.h"
1.17 takayama 12: #include "sm1Socket.h"
1.1 maekawa 13:
1.7 takayama 14: /* If you use file2 standalone to output string,
15: make the following dummy definition;
16: int KsocketSelect0(int a,int b) { return(0); }
17: int oxSocketSelect0(int a,int b) { return(0); }
18: or define FORSTRING
1.18 ! takayama 19: Real oxSocketSelect0 is defined in kan96xx/plugin/mytcpio.c
1.7 takayama 20: */
21: #ifdef FORSTRING
22: #define KsocketSelect0(a,b) 0
23: #define oxSocketSelect0(a,b) 0
24: #endif
25:
1.1 maekawa 26: #ifdef KXX
1.9 takayama 27: #define sGC_malloc(n) malloc(n)
1.18 ! takayama 28: int oxSocketSelect0(int fd,int t);
1.1 maekawa 29: #else
1.9 takayama 30: void *sGC_malloc(int size);
1.1 maekawa 31: #endif
1.2 takayama 32: int WatchStream = 0;
1.1 maekawa 33: /* Note: 1997, 12/6 cf. SS475/kxx/openxxx.tex, SS475/memo1.txt
34: The functions in file2.c should not accept interruptions
35: during its critical operations. The synchronization mechanism
36: has not yet been implemented.
37: */
38:
39: static int debug1 = 0;
40: static int checkfp2(FILE2 *fp2,char *s);
1.7 takayama 41: static int fp2fputcString(int c,FILE2 *fp2);
1.1 maekawa 42: static int checkfp2(FILE2 *fp2,char *s)
43: {
44: if (fp2 == NULL) {
45: fprintf(stderr,"%s: NULL pointer.\n",s);
46: return(-1);
47: }
48: if (fp2->initialized != 1) {
49: fprintf(stderr,"%s: fp is not initialized.\n",s);
50: return(-1);
51: }
52: return(0);
53: }
54:
55: FILE2 *fp2open(int fd) {
56: FILE2 *fp2;
57: if (debug1) {
58: printf("fp2open is called. \n");
59: }
1.9 takayama 60: fp2 = (FILE2 *) sGC_malloc(sizeof(FILE2));
1.7 takayama 61: if (fd < -1) {
1.1 maekawa 62: fprintf(stderr,"fp2open Invalid file descriptor %d\n",fd);
63: return(NULL);
64: }
1.7 takayama 65: /* fd == -1 ==> store in string. */
1.1 maekawa 66: if (fp2 == NULL) {
67: fprintf(stderr,"fp2open. No memory.\n");
68: return(NULL);
69: }
70: fp2->fd = fd;
71: fp2->initialized = 1;
72: checkfp2(fp2,"fp2open ");
73: fp2->readpos = 0;
74: fp2->readsize = 0;
75: fp2->writepos = 0;
76: fp2->limit = FILE2BSIZE;
1.9 takayama 77: fp2->readBuf = (char *) sGC_malloc(FILE2BSIZE);
78: fp2->writeBuf = (char *) sGC_malloc(FILE2BSIZE);
1.7 takayama 79: if ((fp2->readBuf == NULL) || (fp2->writeBuf == NULL)) {
80: fprintf(stderr,"fp2open. No more memory.\n");
81: return(NULL);
82: }
1.1 maekawa 83: fp2->watch = 0;
84: fp2->watchFile = NULL;
85: fp2->mathcapList = NULL;
1.5 takayama 86: fp2->log_incomming = NULL;
87: fp2->log_outgoing = NULL;
1.11 takayama 88: fp2->popened = 0;
89: fp2->pfp = NULL;
1.1 maekawa 90: return(fp2);
91: }
92:
1.11 takayama 93: void fp2setfp(FILE2 *fp2,FILE *fp,int popened) {
94: fp2->pfp = fp;
95: fp2->popened = popened;
96: }
1.1 maekawa 97:
98: int fp2fflush(FILE2 *fp2) {
99: int r;
100: if (debug1) {
1.17 takayama 101: printf("fp2fflush is called with FILE2 *%lx.\n", (long)fp2);
1.1 maekawa 102: fp2dumpBuffer(fp2);
103: printf("--------------------------\n");
104: }
105: if (checkfp2(fp2,"fp2fflush ") == -1) return(-1);
1.7 takayama 106: if (fp2->fd == -1) return(0);
1.1 maekawa 107: if (fp2->writepos > 0) {
1.16 takayama 108: mysignal(SIGPIPE,SIG_IGN);
1.1 maekawa 109: r = write(fp2->fd,fp2->writeBuf,fp2->writepos);
1.16 takayama 110: mysignal(SIGPIPE,SIG_DFL);
1.1 maekawa 111: fp2->writepos = 0;
112: if (r <= 0) {
113: fprintf(stderr,"fp2fflush(): write failed on %d.\n",fp2->fd);
1.12 takayama 114: if (errno == EPIPE) {
115: fprintf(stderr,"Your peer is closed --- SIGPIPE. Closing this fp2.\n");
116: fp2fclose(fp2);
117: return r;
118: }
1.1 maekawa 119: }
120: return(r);
121: }else{
122: return(0);
123: }
124: }
125:
126: int fp2fclose(FILE2 *fp2) {
127: int r;
128: if (checkfp2(fp2," fp2fclose ") == -1) return(-1);
1.7 takayama 129: if (fp2->fd == -1) return(0);
1.1 maekawa 130: r = fp2fflush(fp2);
131: if (r < 0) {
132: fprintf(stderr,"fp2fclose: flush error.\n");
133: return(-1);
134: }
1.11 takayama 135: if (fp2->pfp != NULL) {
136: if (fp2->popened) {
137: return pclose(fp2->pfp);
138: } else return fclose(fp2->pfp);
139: }
140: else return(close(fp2->fd));
1.1 maekawa 141: }
142:
143: int fp2fputc(int c,FILE2 *fp2) {
1.2 takayama 144: FILE *fp;
1.1 maekawa 145: if (debug1) {
146: printf("fp2fputc is called with %2x, fp2->writepos=%d, ",c,fp2->writepos);
1.17 takayama 147: printf("fp2 = %lx.\n",(long) fp2);
1.1 maekawa 148: }
1.2 takayama 149: if (fp2->watch || WatchStream) {
1.4 takayama 150: if (fp2->watch) fp = fp2->watchFile;
151: else fp = stderr;
1.6 takayama 152: fprintf(stderr,"put to <%x> ",fp2->fd); /* output the channel for debug */
1.1 maekawa 153: if (c >= ' ' && c <='z') {
1.3 takayama 154: fprintf(fp," %2x(%c)-> ",c& 0xff,c);
1.1 maekawa 155: }else{
1.3 takayama 156: fprintf(fp," %2x( )-> ",c& 0xff);
1.1 maekawa 157: }
158: fflush(NULL);
159: }
1.5 takayama 160: if (fp2->log_outgoing != NULL) fputc(c,fp2->log_outgoing);
1.1 maekawa 161: if (checkfp2(fp2," fp2fputc ") == -1) return(-1);
162: (fp2->writeBuf)[fp2->writepos] = c;
163: (fp2->writepos)++;
1.7 takayama 164: if (fp2->fd == -1) return(fp2fputcString(c,fp2));
1.1 maekawa 165: if (fp2->writepos < fp2->limit) {
166: return(c);
167: }else{
168: if (fp2fflush(fp2) <0) return(-1);
169: else return(c);
170: }
171: }
172:
173: int fp2fgetc(FILE2 *fp2) {
174: int c;
1.2 takayama 175: FILE *fp;
1.1 maekawa 176: /* printf("fp2fgetc is called. "); fflush(NULL); */
177: if (checkfp2(fp2," fp2fgetc ") == -1) return(-1);
178: if (fp2->readpos < fp2->readsize) {
179: fp2->readpos++;
180: c = fp2->readBuf[fp2->readpos -1];
1.2 takayama 181: if (fp2->watch || WatchStream) {
1.4 takayama 182: if (fp2->watch) fp = fp2->watchFile;
183: else fp = stderr;
1.6 takayama 184: fprintf(fp,"get from <%x> ",fp2->fd); /* output the channel for debug*/
1.1 maekawa 185: if (c >= ' ' && c <= 'z') {
1.4 takayama 186: fprintf(fp," %2x(%c) ",c,c);
1.1 maekawa 187: }else{
1.4 takayama 188: fprintf(fp," %2x( ) ",c);
1.1 maekawa 189: }
190: fflush(NULL);
191: }
1.5 takayama 192: if (fp2->log_incomming != NULL) fputc(c,fp2->log_incomming);
1.1 maekawa 193: return(c);
194: }else{
1.7 takayama 195: if (fp2->fd == -1) return(-1);
1.1 maekawa 196: fp2->readpos = 0;
197: fp2 ->readsize =
198: read(fp2->fd, fp2->readBuf, fp2->limit);
199: if (fp2->readsize == 0) {
1.2 takayama 200: if (fp2->watch || WatchStream) {
1.4 takayama 201: if (fp2->watch) fp = fp2->watchFile;
202: else fp = stderr;
203: fprintf(fp," <%2x ",c);
204: fflush(NULL);
1.1 maekawa 205: }
206: return(-1);
207: }
208: else {
209: return(fp2fgetc(fp2));
210: }
211: }
212: }
213:
214: int fp2select(FILE2 *fp2, int t) {
1.7 takayama 215: if (fp2->fd == -1) {
216: if (fp2->readpos < fp2->readsize) return(1);
217: else return(0);
218: }
1.1 maekawa 219: if (fp2->readpos < fp2->readsize) return(1);
220: else {
221: #ifdef KXX
222: return(oxSocketSelect0(fp2->fd,t));
223: #else
224: return(KsocketSelect0(fp2->fd,t));
225: #endif
226: }
227: }
228:
229: int fp2dumpBuffer(FILE2 *fp2) {
230: int i;
231: if (checkfp2(fp2," f2pdumpBuf ") == -1) {
232: return(-1);
233: }
234: printf("fd=%d\n",fp2->fd);
235: printf("initialied=%d\n",fp2->initialized);
236: printf("readpos=%d\n",fp2->readpos);
237: printf("readsize=%d\n",fp2->readsize);
238: printf("writepos=%d\n",fp2->writepos);
239: printf("limit=%d\n",fp2->limit);
240: for (i=0; i<fp2->readsize; i++) {
241: printf("readBuf[%d]=%2x ",i,fp2->readBuf[i]);
242: }
243: for (i=0; i<fp2->writepos; i++) {
244: printf("writeBuf[%d]=%2x ",i,fp2->writeBuf[i]);
245: }
246: printf("\n");
247: return(0);
248: }
249:
250: int fp2clearReadBuf(FILE2 *fp2) {
251: fd_set readfds;
252: struct timeval timeout;
253: int fd;
254: #define TMP00SIZE 2000
255: char tmp00[TMP00SIZE];
256: int n;
257:
258: if (checkfp2(fp2," fp2clearReadBuf ") == -1) {
259: return(-1);
260: }
1.7 takayama 261: if (fp2->fd == -1) return(0);
1.1 maekawa 262:
263: fp2->readsize=0; fp2->readpos = 0; /* Clear the buffer. */
264:
265: fd = fp2->fd;
266: while (1) {
267: FD_ZERO(&readfds);
268: FD_SET(fd,&readfds);
269: timeout.tv_sec = 0;
270: timeout.tv_usec = (long) 0;
271: fp2->readpos = fp2->readsize = 0;
272: if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
273: fprintf(stderr,"fp2clearReadBuf: Select error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
274: return(-1);
275: }
276: if (FD_ISSET(fd,&readfds)) {
277: n = read(fd,tmp00, TMP00SIZE);
278: if (n <= 0) {
1.4 takayama 279: fprintf(stderr,"fp2clearReadBuf: File is closed or read error.\n");
280: return(-1);
1.1 maekawa 281: }
282: if ( n < TMP00SIZE ) {
1.4 takayama 283: return(0);
1.1 maekawa 284: }
285: }else {
286: return(0);
287: }
288: }
289: }
290: int fp2write(FILE2 *os, char *data, int size) {
291: int i,r;
292: for (i=0; i<size; i++) {
1.14 takayama 293: r = fp2fputc((unsigned char)(data[i]),os);
1.1 maekawa 294: }
295: return(r);
296: }
297:
298: int fp2watch(FILE2 *fp2,FILE *f)
299: {
300: if (f == NULL) {
301: fprintf(stderr,"fp2watch FILE *f is NULL.\n");
302: return(-1);
303: }
304: if (fp2 == NULL) {
305: fprintf(stderr,"fp2watch FILE2 *fp2 is NULL.\n");
306: return(-1);
307: }
308: if (fp2->watch) {
309: fprintf(stderr,"fp2watch: fp2 is already taking a log.");
310: return(-1);
311: }
312: fp2->watch = 1;
313: fp2->watchFile = f;
314: return(0);
315: }
316:
317: int fp2stopWatch(FILE2 *fp2)
318: {
319: if (fp2 == NULL) {
320: fprintf(stderr,"fp2stopWatch FILE2 *fp2 is NULL.\n");
321: return(-1);
322: }
323: if (!fp2->watch) {
324: fprintf(stderr,"fp2stopWatch: fp2 is not taking a log.");
325: return(-1);
326: }
327: fp2->watch=0;
328: if (fp2->watchFile != stdout) {
329: return(fclose(fp2->watchFile));
330: }
331: }
332:
1.5 takayama 333: int fp2log(FILE2 *fp,FILE *incomming,FILE *outgoing) {
334: fp->log_incomming = incomming;
335: fp->log_outgoing = outgoing;
336: return 0;
337: }
338: int fp2stopLog(FILE2 *fp) {
339: if (fp->log_incomming != NULL) fclose(fp->log_incomming);
340: if (fp->log_outgoing != NULL) fclose(fp->log_outgoing);
341: fp->log_incomming = fp->log_outgoing = NULL;
342: return 0;
343: }
1.7 takayama 344:
345: static int fp2fputcString(int c,FILE2 *fp2) {
346: unsigned char *newwrite,*newread;
347: int newsize;
348: int i;
349: (fp2->readBuf)[fp2->readsize] = c;
350: (fp2->readsize)++;
351: if ((fp2->writepos < fp2->limit) && (fp2->readsize < fp2->limit)) return(c);
352: if ((fp2->limit)*2 >=0x3000000) {
353: fprintf(stderr,"Too big output string.\n");
354: return(-1);
355: }
356: newsize = (fp2->limit)*2;
1.9 takayama 357: newwrite = (char *)sGC_malloc(newsize);
358: newread = (char *)sGC_malloc(newsize);
1.7 takayama 359: if ((newwrite == NULL) || (newread == NULL)) {
360: fprintf(stderr,"fp2fputcString: No more memory.\n");
361: return(-1);
362: }
363: for (i=0; i<fp2->writepos; i++) {
364: newwrite[i] = fp2->writeBuf[i];
365: }
366: for (i=0; i<fp2->readsize; i++) {
367: newread[i] = fp2->readBuf[i];
368: }
369: fp2->writeBuf = newwrite;
370: fp2->readBuf = newread;
371: fp2->limit = newsize;
372: return(c);
373: }
374:
375: char *fp2fcloseInString(FILE2 *fp2, int *sizep)
376: {
377: if (fp2->fd == -1) {
378: fp2fputc(0,fp2);
379: *sizep = fp2->writepos-1;
380: return(fp2->writeBuf);
381: }else{
382: fprintf(stderr,"fp2fcloseInString is called for a file stream that is not associated to a string.\n");
383: }
1.8 takayama 384: }
385:
386: int fp2fputs(char *s,FILE2 *fp) {
387: int i,n;
388: n = strlen(s);
389: for (i=0; i<n; i++) {
1.14 takayama 390: if (fp2fputc((unsigned char)(s[i]),fp) < 0) return(-1);
1.8 takayama 391: }
392: return(0);
1.7 takayama 393: }
394:
395: /* Sample program FORSTRING
396: FILE2 *fp2;
397: int c;
398: char *s;
399: int size;
400: fp2 = fp2fopen(-1);
401: while ((c = getchar()) != EOF) {
402: fp2fputc(c,fp2);
403: }
404: s = fp2fcloseInString(fp2,&size);
405:
406: or
407:
408: while ((c = fp2fgetc(fp2)) != EOF) {
409: ....
410: }
411: */
1.12 takayama 412:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>