Annotation of OpenXM/src/kan96xx/plugin/file2.c, Revision 1.10
1.10 ! takayama 1: /*$OpenXM: OpenXM/src/kan96xx/plugin/file2.c,v 1.9 2004/02/23 09:03:43 takayama Exp $ */
1.1 maekawa 2: #include <stdio.h>
3: #include <sys/time.h>
4: #include <sys/types.h>
5: #include <unistd.h>
1.10 ! takayama 6: #include <errno.h>
1.1 maekawa 7: #include "file2.h"
8:
1.7 takayama 9: /* If you use file2 standalone to output string,
10: make the following dummy definition;
11: int KsocketSelect0(int a,int b) { return(0); }
12: int oxSocketSelect0(int a,int b) { return(0); }
13: or define FORSTRING
14: */
15: #ifdef FORSTRING
16: #define KsocketSelect0(a,b) 0
17: #define oxSocketSelect0(a,b) 0
18: #endif
19:
1.1 maekawa 20: #ifdef KXX
1.9 takayama 21: #define sGC_malloc(n) malloc(n)
1.1 maekawa 22: #else
1.9 takayama 23: void *sGC_malloc(int size);
1.1 maekawa 24: #endif
1.2 takayama 25: int WatchStream = 0;
1.1 maekawa 26: /* Note: 1997, 12/6 cf. SS475/kxx/openxxx.tex, SS475/memo1.txt
27: The functions in file2.c should not accept interruptions
28: during its critical operations. The synchronization mechanism
29: has not yet been implemented.
30: */
31:
32: static int debug1 = 0;
33: static int checkfp2(FILE2 *fp2,char *s);
1.7 takayama 34: static int fp2fputcString(int c,FILE2 *fp2);
1.1 maekawa 35: static int checkfp2(FILE2 *fp2,char *s)
36: {
37: if (fp2 == NULL) {
38: fprintf(stderr,"%s: NULL pointer.\n",s);
39: return(-1);
40: }
41: if (fp2->initialized != 1) {
42: fprintf(stderr,"%s: fp is not initialized.\n",s);
43: return(-1);
44: }
45: return(0);
46: }
47:
48: FILE2 *fp2open(int fd) {
49: FILE2 *fp2;
50: if (debug1) {
51: printf("fp2open is called. \n");
52: }
1.9 takayama 53: fp2 = (FILE2 *) sGC_malloc(sizeof(FILE2));
1.7 takayama 54: if (fd < -1) {
1.1 maekawa 55: fprintf(stderr,"fp2open Invalid file descriptor %d\n",fd);
56: return(NULL);
57: }
1.7 takayama 58: /* fd == -1 ==> store in string. */
1.1 maekawa 59: if (fp2 == NULL) {
60: fprintf(stderr,"fp2open. No memory.\n");
61: return(NULL);
62: }
63: fp2->fd = fd;
64: fp2->initialized = 1;
65: checkfp2(fp2,"fp2open ");
66: fp2->readpos = 0;
67: fp2->readsize = 0;
68: fp2->writepos = 0;
69: fp2->limit = FILE2BSIZE;
1.9 takayama 70: fp2->readBuf = (char *) sGC_malloc(FILE2BSIZE);
71: fp2->writeBuf = (char *) sGC_malloc(FILE2BSIZE);
1.7 takayama 72: if ((fp2->readBuf == NULL) || (fp2->writeBuf == NULL)) {
73: fprintf(stderr,"fp2open. No more memory.\n");
74: return(NULL);
75: }
1.1 maekawa 76: fp2->watch = 0;
77: fp2->watchFile = NULL;
78: fp2->mathcapList = NULL;
1.5 takayama 79: fp2->log_incomming = NULL;
80: fp2->log_outgoing = NULL;
1.1 maekawa 81: return(fp2);
82: }
83:
84:
85: int fp2fflush(FILE2 *fp2) {
86: int r;
87: if (debug1) {
88: printf("fp2fflush is called with FILE2 *%x.\n", (int )fp2);
89: fp2dumpBuffer(fp2);
90: printf("--------------------------\n");
91: }
92: if (checkfp2(fp2,"fp2fflush ") == -1) return(-1);
1.7 takayama 93: if (fp2->fd == -1) return(0);
1.1 maekawa 94: if (fp2->writepos > 0) {
95: r = write(fp2->fd,fp2->writeBuf,fp2->writepos);
96: fp2->writepos = 0;
97: if (r <= 0) {
98: fprintf(stderr,"fp2fflush(): write failed on %d.\n",fp2->fd);
99: }
100: return(r);
101: }else{
102: return(0);
103: }
104: }
105:
106: int fp2fclose(FILE2 *fp2) {
107: int r;
108: if (checkfp2(fp2," fp2fclose ") == -1) return(-1);
1.7 takayama 109: if (fp2->fd == -1) return(0);
1.1 maekawa 110: r = fp2fflush(fp2);
111: if (r < 0) {
112: fprintf(stderr,"fp2fclose: flush error.\n");
113: return(-1);
114: }
115: return(close(fp2->fd));
116: }
117:
118: int fp2fputc(int c,FILE2 *fp2) {
1.2 takayama 119: FILE *fp;
1.1 maekawa 120: if (debug1) {
121: printf("fp2fputc is called with %2x, fp2->writepos=%d, ",c,fp2->writepos);
122: printf("fp2 = %x.\n",(int) fp2);
123: }
1.2 takayama 124: if (fp2->watch || WatchStream) {
1.4 takayama 125: if (fp2->watch) fp = fp2->watchFile;
126: else fp = stderr;
1.6 takayama 127: fprintf(stderr,"put to <%x> ",fp2->fd); /* output the channel for debug */
1.1 maekawa 128: if (c >= ' ' && c <='z') {
1.3 takayama 129: fprintf(fp," %2x(%c)-> ",c& 0xff,c);
1.1 maekawa 130: }else{
1.3 takayama 131: fprintf(fp," %2x( )-> ",c& 0xff);
1.1 maekawa 132: }
133: fflush(NULL);
134: }
1.5 takayama 135: if (fp2->log_outgoing != NULL) fputc(c,fp2->log_outgoing);
1.1 maekawa 136: if (checkfp2(fp2," fp2fputc ") == -1) return(-1);
137: (fp2->writeBuf)[fp2->writepos] = c;
138: (fp2->writepos)++;
1.7 takayama 139: if (fp2->fd == -1) return(fp2fputcString(c,fp2));
1.1 maekawa 140: if (fp2->writepos < fp2->limit) {
141: return(c);
142: }else{
143: if (fp2fflush(fp2) <0) return(-1);
144: else return(c);
145: }
146: }
147:
148: int fp2fgetc(FILE2 *fp2) {
149: int c;
1.2 takayama 150: FILE *fp;
1.1 maekawa 151: /* printf("fp2fgetc is called. "); fflush(NULL); */
152: if (checkfp2(fp2," fp2fgetc ") == -1) return(-1);
153: if (fp2->readpos < fp2->readsize) {
154: fp2->readpos++;
155: c = fp2->readBuf[fp2->readpos -1];
1.2 takayama 156: if (fp2->watch || WatchStream) {
1.4 takayama 157: if (fp2->watch) fp = fp2->watchFile;
158: else fp = stderr;
1.6 takayama 159: fprintf(fp,"get from <%x> ",fp2->fd); /* output the channel for debug*/
1.1 maekawa 160: if (c >= ' ' && c <= 'z') {
1.4 takayama 161: fprintf(fp," %2x(%c) ",c,c);
1.1 maekawa 162: }else{
1.4 takayama 163: fprintf(fp," %2x( ) ",c);
1.1 maekawa 164: }
165: fflush(NULL);
166: }
1.5 takayama 167: if (fp2->log_incomming != NULL) fputc(c,fp2->log_incomming);
1.1 maekawa 168: return(c);
169: }else{
1.7 takayama 170: if (fp2->fd == -1) return(-1);
1.1 maekawa 171: fp2->readpos = 0;
172: fp2 ->readsize =
173: read(fp2->fd, fp2->readBuf, fp2->limit);
174: if (fp2->readsize == 0) {
1.2 takayama 175: if (fp2->watch || WatchStream) {
1.4 takayama 176: if (fp2->watch) fp = fp2->watchFile;
177: else fp = stderr;
178: fprintf(fp," <%2x ",c);
179: fflush(NULL);
1.1 maekawa 180: }
181: return(-1);
182: }
183: else {
184: return(fp2fgetc(fp2));
185: }
186: }
187: }
188:
189: int fp2select(FILE2 *fp2, int t) {
1.7 takayama 190: if (fp2->fd == -1) {
191: if (fp2->readpos < fp2->readsize) return(1);
192: else return(0);
193: }
1.1 maekawa 194: if (fp2->readpos < fp2->readsize) return(1);
195: else {
196: #ifdef KXX
197: return(oxSocketSelect0(fp2->fd,t));
198: #else
199: return(KsocketSelect0(fp2->fd,t));
200: #endif
201: }
202: }
203:
204: int fp2dumpBuffer(FILE2 *fp2) {
205: int i;
206: if (checkfp2(fp2," f2pdumpBuf ") == -1) {
207: return(-1);
208: }
209: printf("fd=%d\n",fp2->fd);
210: printf("initialied=%d\n",fp2->initialized);
211: printf("readpos=%d\n",fp2->readpos);
212: printf("readsize=%d\n",fp2->readsize);
213: printf("writepos=%d\n",fp2->writepos);
214: printf("limit=%d\n",fp2->limit);
215: for (i=0; i<fp2->readsize; i++) {
216: printf("readBuf[%d]=%2x ",i,fp2->readBuf[i]);
217: }
218: for (i=0; i<fp2->writepos; i++) {
219: printf("writeBuf[%d]=%2x ",i,fp2->writeBuf[i]);
220: }
221: printf("\n");
222: return(0);
223: }
224:
225: int fp2clearReadBuf(FILE2 *fp2) {
226: fd_set readfds;
227: struct timeval timeout;
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;
1.9 takayama 332: newwrite = (char *)sGC_malloc(newsize);
333: newread = (char *)sGC_malloc(newsize);
1.7 takayama 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>