[BACK]Return to file2.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kan96xx / plugin

Annotation of OpenXM/src/kan96xx/plugin/file2.c, Revision 1.17

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>