[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.5

1.5     ! takayama    1: /*$OpenXM: OpenXM/src/kan96xx/plugin/file2.c,v 1.4 2001/05/04 01:06: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:
                      8: #ifdef KXX
                      9: #define GC_malloc(n) malloc(n)
                     10: #else
                     11: void *GC_malloc(int size);
                     12: #endif
1.2       takayama   13: int WatchStream = 0;
1.1       maekawa    14: /*  Note:  1997, 12/6   cf. SS475/kxx/openxxx.tex, SS475/memo1.txt
                     15:     The functions in file2.c should not accept interruptions
                     16:     during its critical operations.  The synchronization mechanism
                     17:     has not yet been implemented.
                     18:     */
                     19:
                     20: static int debug1 = 0;
                     21: static int checkfp2(FILE2 *fp2,char *s);
                     22: static int checkfp2(FILE2 *fp2,char *s)
                     23: {
                     24:   if (fp2 == NULL) {
                     25:     fprintf(stderr,"%s:  NULL pointer.\n",s);
                     26:     return(-1);
                     27:   }
                     28:   if (fp2->initialized != 1) {
                     29:     fprintf(stderr,"%s:  fp is not initialized.\n",s);
                     30:     return(-1);
                     31:   }
                     32:   return(0);
                     33: }
                     34:
                     35: FILE2 *fp2open(int fd) {
                     36:   FILE2 *fp2;
                     37:   if (debug1) {
                     38:     printf("fp2open is called. \n");
                     39:   }
                     40:   fp2 = (FILE2 *) GC_malloc(sizeof(FILE2));
                     41:   if (fd < 0) {
                     42:     fprintf(stderr,"fp2open  Invalid file descriptor %d\n",fd);
                     43:     return(NULL);
                     44:   }
                     45:   if (fp2 == NULL) {
                     46:     fprintf(stderr,"fp2open.  No memory.\n");
                     47:     return(NULL);
                     48:   }
                     49:   fp2->fd = fd;
                     50:   fp2->initialized = 1;
                     51:   checkfp2(fp2,"fp2open ");
                     52:   fp2->readpos = 0;
                     53:   fp2->readsize = 0;
                     54:   fp2->writepos = 0;
                     55:   fp2->limit = FILE2BSIZE;
                     56:   fp2->watch = 0;
                     57:   fp2->watchFile = NULL;
                     58:   fp2->mathcapList = NULL;
1.5     ! takayama   59:   fp2->log_incomming = NULL;
        !            60:   fp2->log_outgoing = NULL;
1.1       maekawa    61:   return(fp2);
                     62: }
                     63:
                     64:
                     65: int fp2fflush(FILE2 *fp2) {
                     66:   int r;
                     67:   if (debug1) {
                     68:     printf("fp2fflush is called with FILE2 *%x.\n", (int )fp2);
                     69:     fp2dumpBuffer(fp2);
                     70:     printf("--------------------------\n");
                     71:   }
                     72:   if (checkfp2(fp2,"fp2fflush ") == -1) return(-1);
                     73:   if (fp2->writepos > 0) {
                     74:     r = write(fp2->fd,fp2->writeBuf,fp2->writepos);
                     75:     fp2->writepos = 0;
                     76:     if (r <= 0) {
                     77:       fprintf(stderr,"fp2fflush(): write failed on %d.\n",fp2->fd);
                     78:     }
                     79:     return(r);
                     80:   }else{
                     81:     return(0);
                     82:   }
                     83: }
                     84:
                     85: int fp2fclose(FILE2 *fp2) {
                     86:   int r;
                     87:   if (checkfp2(fp2," fp2fclose ") == -1) return(-1);
                     88:   r = fp2fflush(fp2);
                     89:   if (r < 0) {
                     90:     fprintf(stderr,"fp2fclose: flush error.\n");
                     91:     return(-1);
                     92:   }
                     93:   return(close(fp2->fd));
                     94: }
                     95:
                     96: int fp2fputc(int c,FILE2 *fp2) {
1.2       takayama   97:   FILE *fp;
1.1       maekawa    98:   if (debug1) {
                     99:     printf("fp2fputc is called with %2x, fp2->writepos=%d, ",c,fp2->writepos);
                    100:     printf("fp2 = %x.\n",(int) fp2);
                    101:   }
1.2       takayama  102:   if (fp2->watch || WatchStream) {
1.4       takayama  103:     if (fp2->watch) fp = fp2->watchFile;
                    104:     else fp = stderr;
1.1       maekawa   105:     if (c >= ' ' && c <='z') {
1.3       takayama  106:       fprintf(fp," %2x(%c)-> ",c& 0xff,c);
1.1       maekawa   107:     }else{
1.3       takayama  108:       fprintf(fp," %2x( )-> ",c& 0xff);
1.1       maekawa   109:     }
                    110:     fflush(NULL);
                    111:   }
1.5     ! takayama  112:   if (fp2->log_outgoing != NULL) fputc(c,fp2->log_outgoing);
1.1       maekawa   113:   if (checkfp2(fp2," fp2fputc ") == -1) return(-1);
                    114:   (fp2->writeBuf)[fp2->writepos] = c;
                    115:   (fp2->writepos)++;
                    116:   if (fp2->writepos < fp2->limit) {
                    117:     return(c);
                    118:   }else{
                    119:     if (fp2fflush(fp2) <0) return(-1);
                    120:     else return(c);
                    121:   }
                    122: }
                    123:
                    124: int fp2fgetc(FILE2 *fp2) {
                    125:   int c;
1.2       takayama  126:   FILE *fp;
1.1       maekawa   127:   /* printf("fp2fgetc is called. "); fflush(NULL); */
                    128:   if (checkfp2(fp2," fp2fgetc ") == -1) return(-1);
                    129:   if (fp2->readpos < fp2->readsize) {
                    130:     fp2->readpos++;
                    131:     c = fp2->readBuf[fp2->readpos -1];
1.2       takayama  132:     if (fp2->watch || WatchStream) {
1.4       takayama  133:       if (fp2->watch) fp = fp2->watchFile;
                    134:       else fp = stderr;
1.1       maekawa   135:       if (c >= ' ' && c <= 'z') {
1.4       takayama  136:         fprintf(fp," %2x(%c) ",c,c);
1.1       maekawa   137:       }else{
1.4       takayama  138:         fprintf(fp," %2x( ) ",c);
1.1       maekawa   139:       }
                    140:       fflush(NULL);
                    141:     }
1.5     ! takayama  142:     if (fp2->log_incomming != NULL) fputc(c,fp2->log_incomming);
1.1       maekawa   143:     return(c);
                    144:   }else{
                    145:     fp2->readpos = 0;
                    146:     fp2 ->readsize =
                    147:       read(fp2->fd, fp2->readBuf, fp2->limit);
                    148:     if (fp2->readsize == 0) {
1.2       takayama  149:       if (fp2->watch || WatchStream) {
1.4       takayama  150:         if (fp2->watch) fp = fp2->watchFile;
                    151:         else fp = stderr;
                    152:         fprintf(fp," <%2x ",c);
                    153:         fflush(NULL);
1.1       maekawa   154:       }
1.5     ! takayama  155:       if (fp2->log_incomming != NULL) fputc(c,fp2->log_incomming);
1.1       maekawa   156:       return(-1);
                    157:     }
                    158:     else {
                    159:       return(fp2fgetc(fp2));
                    160:     }
                    161:   }
                    162: }
                    163:
                    164: int fp2select(FILE2 *fp2, int t) {
                    165:   if (fp2->readpos < fp2->readsize) return(1);
                    166:   else {
                    167: #ifdef KXX
                    168:     return(oxSocketSelect0(fp2->fd,t));
                    169: #else
                    170:     return(KsocketSelect0(fp2->fd,t));
                    171: #endif
                    172:   }
                    173: }
                    174:
                    175: int fp2dumpBuffer(FILE2 *fp2) {
                    176:   int i;
                    177:   if (checkfp2(fp2," f2pdumpBuf ") == -1) {
                    178:     return(-1);
                    179:   }
                    180:   printf("fd=%d\n",fp2->fd);
                    181:   printf("initialied=%d\n",fp2->initialized);
                    182:   printf("readpos=%d\n",fp2->readpos);
                    183:   printf("readsize=%d\n",fp2->readsize);
                    184:   printf("writepos=%d\n",fp2->writepos);
                    185:   printf("limit=%d\n",fp2->limit);
                    186:   for (i=0; i<fp2->readsize; i++) {
                    187:     printf("readBuf[%d]=%2x ",i,fp2->readBuf[i]);
                    188:   }
                    189:   for (i=0; i<fp2->writepos; i++) {
                    190:     printf("writeBuf[%d]=%2x ",i,fp2->writeBuf[i]);
                    191:   }
                    192:   printf("\n");
                    193:   return(0);
                    194: }
                    195:
                    196: int fp2clearReadBuf(FILE2 *fp2) {
                    197:   fd_set readfds;
                    198:   struct timeval timeout;
                    199:   extern int errno;
                    200:   int fd;
                    201: #define TMP00SIZE  2000
                    202:   char tmp00[TMP00SIZE];
                    203:   int n;
                    204:
                    205:   if (checkfp2(fp2," fp2clearReadBuf ") == -1) {
                    206:     return(-1);
                    207:   }
                    208:
                    209:   fp2->readsize=0; fp2->readpos = 0;  /* Clear the buffer. */
                    210:
                    211:   fd = fp2->fd;
                    212:   while (1) {
                    213:     FD_ZERO(&readfds);
                    214:     FD_SET(fd,&readfds);
                    215:     timeout.tv_sec = 0;
                    216:     timeout.tv_usec = (long) 0;
                    217:     fp2->readpos = fp2->readsize = 0;
                    218:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
                    219:       fprintf(stderr,"fp2clearReadBuf: Select error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
                    220:       return(-1);
                    221:     }
                    222:     if (FD_ISSET(fd,&readfds)) {
                    223:       n = read(fd,tmp00, TMP00SIZE);
                    224:       if (n <=  0) {
1.4       takayama  225:         fprintf(stderr,"fp2clearReadBuf: File is closed or read error.\n");
                    226:         return(-1);
1.1       maekawa   227:       }
                    228:       if ( n < TMP00SIZE ) {
1.4       takayama  229:         return(0);
1.1       maekawa   230:       }
                    231:     }else {
                    232:       return(0);
                    233:     }
                    234:   }
                    235: }
                    236: int fp2write(FILE2 *os, char *data, int size) {
                    237:   int i,r;
                    238:   for (i=0; i<size; i++) {
                    239:     r = fp2fputc(data[i],os);
                    240:   }
                    241:   return(r);
                    242: }
                    243:
                    244: int fp2watch(FILE2 *fp2,FILE *f)
                    245: {
                    246:   if (f == NULL) {
                    247:     fprintf(stderr,"fp2watch FILE *f is NULL.\n");
                    248:     return(-1);
                    249:   }
                    250:   if (fp2 == NULL) {
                    251:     fprintf(stderr,"fp2watch FILE2 *fp2 is NULL.\n");
                    252:     return(-1);
                    253:   }
                    254:   if (fp2->watch) {
                    255:     fprintf(stderr,"fp2watch: fp2 is already taking a log.");
                    256:     return(-1);
                    257:   }
                    258:   fp2->watch = 1;
                    259:   fp2->watchFile = f;
                    260:   return(0);
                    261: }
                    262:
                    263: int fp2stopWatch(FILE2 *fp2)
                    264: {
                    265:   if (fp2 == NULL) {
                    266:     fprintf(stderr,"fp2stopWatch FILE2 *fp2 is NULL.\n");
                    267:     return(-1);
                    268:   }
                    269:   if (!fp2->watch) {
                    270:     fprintf(stderr,"fp2stopWatch: fp2 is not taking a log.");
                    271:     return(-1);
                    272:   }
                    273:   fp2->watch=0;
                    274:   if (fp2->watchFile != stdout) {
                    275:     return(fclose(fp2->watchFile));
                    276:   }
                    277: }
                    278:
1.5     ! takayama  279: int fp2log(FILE2 *fp,FILE *incomming,FILE *outgoing) {
        !           280:   fp->log_incomming = incomming;
        !           281:   fp->log_outgoing = outgoing;
        !           282:   return 0;
        !           283: }
        !           284: int fp2stopLog(FILE2 *fp) {
        !           285:   if (fp->log_incomming != NULL) fclose(fp->log_incomming);
        !           286:   if (fp->log_outgoing != NULL) fclose(fp->log_outgoing);
        !           287:   fp->log_incomming = fp->log_outgoing = NULL;
        !           288:   return 0;
        !           289: }

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