Annotation of OpenXM/src/kan96xx/plugin/file2.c, Revision 1.11
1.11 ! takayama 1: /*$OpenXM: OpenXM/src/kan96xx/plugin/file2.c,v 1.10 2004/02/25 23:14:35 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.11 ! takayama 81: fp2->popened = 0;
! 82: fp2->pfp = NULL;
1.1 maekawa 83: return(fp2);
84: }
85:
1.11 ! takayama 86: void fp2setfp(FILE2 *fp2,FILE *fp,int popened) {
! 87: fp2->pfp = fp;
! 88: fp2->popened = popened;
! 89: }
1.1 maekawa 90:
91: int fp2fflush(FILE2 *fp2) {
92: int r;
93: if (debug1) {
94: printf("fp2fflush is called with FILE2 *%x.\n", (int )fp2);
95: fp2dumpBuffer(fp2);
96: printf("--------------------------\n");
97: }
98: if (checkfp2(fp2,"fp2fflush ") == -1) return(-1);
1.7 takayama 99: if (fp2->fd == -1) return(0);
1.1 maekawa 100: if (fp2->writepos > 0) {
101: r = write(fp2->fd,fp2->writeBuf,fp2->writepos);
102: fp2->writepos = 0;
103: if (r <= 0) {
104: fprintf(stderr,"fp2fflush(): write failed on %d.\n",fp2->fd);
105: }
106: return(r);
107: }else{
108: return(0);
109: }
110: }
111:
112: int fp2fclose(FILE2 *fp2) {
113: int r;
114: if (checkfp2(fp2," fp2fclose ") == -1) return(-1);
1.7 takayama 115: if (fp2->fd == -1) return(0);
1.1 maekawa 116: r = fp2fflush(fp2);
117: if (r < 0) {
118: fprintf(stderr,"fp2fclose: flush error.\n");
119: return(-1);
120: }
1.11 ! takayama 121: if (fp2->pfp != NULL) {
! 122: if (fp2->popened) {
! 123: return pclose(fp2->pfp);
! 124: } else return fclose(fp2->pfp);
! 125: }
! 126: else return(close(fp2->fd));
1.1 maekawa 127: }
128:
129: int fp2fputc(int c,FILE2 *fp2) {
1.2 takayama 130: FILE *fp;
1.1 maekawa 131: if (debug1) {
132: printf("fp2fputc is called with %2x, fp2->writepos=%d, ",c,fp2->writepos);
133: printf("fp2 = %x.\n",(int) fp2);
134: }
1.2 takayama 135: if (fp2->watch || WatchStream) {
1.4 takayama 136: if (fp2->watch) fp = fp2->watchFile;
137: else fp = stderr;
1.6 takayama 138: fprintf(stderr,"put to <%x> ",fp2->fd); /* output the channel for debug */
1.1 maekawa 139: if (c >= ' ' && c <='z') {
1.3 takayama 140: fprintf(fp," %2x(%c)-> ",c& 0xff,c);
1.1 maekawa 141: }else{
1.3 takayama 142: fprintf(fp," %2x( )-> ",c& 0xff);
1.1 maekawa 143: }
144: fflush(NULL);
145: }
1.5 takayama 146: if (fp2->log_outgoing != NULL) fputc(c,fp2->log_outgoing);
1.1 maekawa 147: if (checkfp2(fp2," fp2fputc ") == -1) return(-1);
148: (fp2->writeBuf)[fp2->writepos] = c;
149: (fp2->writepos)++;
1.7 takayama 150: if (fp2->fd == -1) return(fp2fputcString(c,fp2));
1.1 maekawa 151: if (fp2->writepos < fp2->limit) {
152: return(c);
153: }else{
154: if (fp2fflush(fp2) <0) return(-1);
155: else return(c);
156: }
157: }
158:
159: int fp2fgetc(FILE2 *fp2) {
160: int c;
1.2 takayama 161: FILE *fp;
1.1 maekawa 162: /* printf("fp2fgetc is called. "); fflush(NULL); */
163: if (checkfp2(fp2," fp2fgetc ") == -1) return(-1);
164: if (fp2->readpos < fp2->readsize) {
165: fp2->readpos++;
166: c = fp2->readBuf[fp2->readpos -1];
1.2 takayama 167: if (fp2->watch || WatchStream) {
1.4 takayama 168: if (fp2->watch) fp = fp2->watchFile;
169: else fp = stderr;
1.6 takayama 170: fprintf(fp,"get from <%x> ",fp2->fd); /* output the channel for debug*/
1.1 maekawa 171: if (c >= ' ' && c <= 'z') {
1.4 takayama 172: fprintf(fp," %2x(%c) ",c,c);
1.1 maekawa 173: }else{
1.4 takayama 174: fprintf(fp," %2x( ) ",c);
1.1 maekawa 175: }
176: fflush(NULL);
177: }
1.5 takayama 178: if (fp2->log_incomming != NULL) fputc(c,fp2->log_incomming);
1.1 maekawa 179: return(c);
180: }else{
1.7 takayama 181: if (fp2->fd == -1) return(-1);
1.1 maekawa 182: fp2->readpos = 0;
183: fp2 ->readsize =
184: read(fp2->fd, fp2->readBuf, fp2->limit);
185: if (fp2->readsize == 0) {
1.2 takayama 186: if (fp2->watch || WatchStream) {
1.4 takayama 187: if (fp2->watch) fp = fp2->watchFile;
188: else fp = stderr;
189: fprintf(fp," <%2x ",c);
190: fflush(NULL);
1.1 maekawa 191: }
192: return(-1);
193: }
194: else {
195: return(fp2fgetc(fp2));
196: }
197: }
198: }
199:
200: int fp2select(FILE2 *fp2, int t) {
1.7 takayama 201: if (fp2->fd == -1) {
202: if (fp2->readpos < fp2->readsize) return(1);
203: else return(0);
204: }
1.1 maekawa 205: if (fp2->readpos < fp2->readsize) return(1);
206: else {
207: #ifdef KXX
208: return(oxSocketSelect0(fp2->fd,t));
209: #else
210: return(KsocketSelect0(fp2->fd,t));
211: #endif
212: }
213: }
214:
215: int fp2dumpBuffer(FILE2 *fp2) {
216: int i;
217: if (checkfp2(fp2," f2pdumpBuf ") == -1) {
218: return(-1);
219: }
220: printf("fd=%d\n",fp2->fd);
221: printf("initialied=%d\n",fp2->initialized);
222: printf("readpos=%d\n",fp2->readpos);
223: printf("readsize=%d\n",fp2->readsize);
224: printf("writepos=%d\n",fp2->writepos);
225: printf("limit=%d\n",fp2->limit);
226: for (i=0; i<fp2->readsize; i++) {
227: printf("readBuf[%d]=%2x ",i,fp2->readBuf[i]);
228: }
229: for (i=0; i<fp2->writepos; i++) {
230: printf("writeBuf[%d]=%2x ",i,fp2->writeBuf[i]);
231: }
232: printf("\n");
233: return(0);
234: }
235:
236: int fp2clearReadBuf(FILE2 *fp2) {
237: fd_set readfds;
238: struct timeval timeout;
239: int fd;
240: #define TMP00SIZE 2000
241: char tmp00[TMP00SIZE];
242: int n;
243:
244: if (checkfp2(fp2," fp2clearReadBuf ") == -1) {
245: return(-1);
246: }
1.7 takayama 247: if (fp2->fd == -1) return(0);
1.1 maekawa 248:
249: fp2->readsize=0; fp2->readpos = 0; /* Clear the buffer. */
250:
251: fd = fp2->fd;
252: while (1) {
253: FD_ZERO(&readfds);
254: FD_SET(fd,&readfds);
255: timeout.tv_sec = 0;
256: timeout.tv_usec = (long) 0;
257: fp2->readpos = fp2->readsize = 0;
258: if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
259: fprintf(stderr,"fp2clearReadBuf: Select error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
260: return(-1);
261: }
262: if (FD_ISSET(fd,&readfds)) {
263: n = read(fd,tmp00, TMP00SIZE);
264: if (n <= 0) {
1.4 takayama 265: fprintf(stderr,"fp2clearReadBuf: File is closed or read error.\n");
266: return(-1);
1.1 maekawa 267: }
268: if ( n < TMP00SIZE ) {
1.4 takayama 269: return(0);
1.1 maekawa 270: }
271: }else {
272: return(0);
273: }
274: }
275: }
276: int fp2write(FILE2 *os, char *data, int size) {
277: int i,r;
278: for (i=0; i<size; i++) {
279: r = fp2fputc(data[i],os);
280: }
281: return(r);
282: }
283:
284: int fp2watch(FILE2 *fp2,FILE *f)
285: {
286: if (f == NULL) {
287: fprintf(stderr,"fp2watch FILE *f is NULL.\n");
288: return(-1);
289: }
290: if (fp2 == NULL) {
291: fprintf(stderr,"fp2watch FILE2 *fp2 is NULL.\n");
292: return(-1);
293: }
294: if (fp2->watch) {
295: fprintf(stderr,"fp2watch: fp2 is already taking a log.");
296: return(-1);
297: }
298: fp2->watch = 1;
299: fp2->watchFile = f;
300: return(0);
301: }
302:
303: int fp2stopWatch(FILE2 *fp2)
304: {
305: if (fp2 == NULL) {
306: fprintf(stderr,"fp2stopWatch FILE2 *fp2 is NULL.\n");
307: return(-1);
308: }
309: if (!fp2->watch) {
310: fprintf(stderr,"fp2stopWatch: fp2 is not taking a log.");
311: return(-1);
312: }
313: fp2->watch=0;
314: if (fp2->watchFile != stdout) {
315: return(fclose(fp2->watchFile));
316: }
317: }
318:
1.5 takayama 319: int fp2log(FILE2 *fp,FILE *incomming,FILE *outgoing) {
320: fp->log_incomming = incomming;
321: fp->log_outgoing = outgoing;
322: return 0;
323: }
324: int fp2stopLog(FILE2 *fp) {
325: if (fp->log_incomming != NULL) fclose(fp->log_incomming);
326: if (fp->log_outgoing != NULL) fclose(fp->log_outgoing);
327: fp->log_incomming = fp->log_outgoing = NULL;
328: return 0;
329: }
1.7 takayama 330:
331: static int fp2fputcString(int c,FILE2 *fp2) {
332: unsigned char *newwrite,*newread;
333: int newsize;
334: int i;
335: (fp2->readBuf)[fp2->readsize] = c;
336: (fp2->readsize)++;
337: if ((fp2->writepos < fp2->limit) && (fp2->readsize < fp2->limit)) return(c);
338: if ((fp2->limit)*2 >=0x3000000) {
339: fprintf(stderr,"Too big output string.\n");
340: return(-1);
341: }
342: newsize = (fp2->limit)*2;
1.9 takayama 343: newwrite = (char *)sGC_malloc(newsize);
344: newread = (char *)sGC_malloc(newsize);
1.7 takayama 345: if ((newwrite == NULL) || (newread == NULL)) {
346: fprintf(stderr,"fp2fputcString: No more memory.\n");
347: return(-1);
348: }
349: for (i=0; i<fp2->writepos; i++) {
350: newwrite[i] = fp2->writeBuf[i];
351: }
352: for (i=0; i<fp2->readsize; i++) {
353: newread[i] = fp2->readBuf[i];
354: }
355: fp2->writeBuf = newwrite;
356: fp2->readBuf = newread;
357: fp2->limit = newsize;
358: return(c);
359: }
360:
361: char *fp2fcloseInString(FILE2 *fp2, int *sizep)
362: {
363: if (fp2->fd == -1) {
364: fp2fputc(0,fp2);
365: *sizep = fp2->writepos-1;
366: return(fp2->writeBuf);
367: }else{
368: fprintf(stderr,"fp2fcloseInString is called for a file stream that is not associated to a string.\n");
369: }
1.8 takayama 370: }
371:
372: int fp2fputs(char *s,FILE2 *fp) {
373: int i,n;
374: n = strlen(s);
375: for (i=0; i<n; i++) {
376: if (fp2fputc(s[i],fp) < 0) return(-1);
377: }
378: return(0);
1.7 takayama 379: }
380:
381: /* Sample program FORSTRING
382: FILE2 *fp2;
383: int c;
384: char *s;
385: int size;
386: fp2 = fp2fopen(-1);
387: while ((c = getchar()) != EOF) {
388: fp2fputc(c,fp2);
389: }
390: s = fp2fcloseInString(fp2,&size);
391:
392: or
393:
394: while ((c = fp2fgetc(fp2)) != EOF) {
395: ....
396: }
397: */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>