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