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

1.2     ! takayama    1: /*$OpenXM$ */
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;
                     59:   return(fp2);
                     60: }
                     61:
                     62:
                     63: int fp2fflush(FILE2 *fp2) {
                     64:   int r;
                     65:   if (debug1) {
                     66:     printf("fp2fflush is called with FILE2 *%x.\n", (int )fp2);
                     67:     fp2dumpBuffer(fp2);
                     68:     printf("--------------------------\n");
                     69:   }
                     70:   if (checkfp2(fp2,"fp2fflush ") == -1) return(-1);
                     71:   if (fp2->writepos > 0) {
                     72:     r = write(fp2->fd,fp2->writeBuf,fp2->writepos);
                     73:     fp2->writepos = 0;
                     74:     if (r <= 0) {
                     75:       fprintf(stderr,"fp2fflush(): write failed on %d.\n",fp2->fd);
                     76:     }
                     77:     return(r);
                     78:   }else{
                     79:     return(0);
                     80:   }
                     81: }
                     82:
                     83: int fp2fclose(FILE2 *fp2) {
                     84:   int r;
                     85:   if (checkfp2(fp2," fp2fclose ") == -1) return(-1);
                     86:   r = fp2fflush(fp2);
                     87:   if (r < 0) {
                     88:     fprintf(stderr,"fp2fclose: flush error.\n");
                     89:     return(-1);
                     90:   }
                     91:   return(close(fp2->fd));
                     92: }
                     93:
                     94: int fp2fputc(int c,FILE2 *fp2) {
1.2     ! takayama   95:   FILE *fp;
1.1       maekawa    96:   if (debug1) {
                     97:     printf("fp2fputc is called with %2x, fp2->writepos=%d, ",c,fp2->writepos);
                     98:     printf("fp2 = %x.\n",(int) fp2);
                     99:   }
1.2     ! takayama  100:   if (fp2->watch || WatchStream) {
        !           101:        if (fp2->watch) fp = fp2->watchFile;
        !           102:        else fp = stderr;
1.1       maekawa   103:     if (c >= ' ' && c <='z') {
1.2     ! takayama  104:       fprintf(fp," %2x(%c)-> ",c,c);
1.1       maekawa   105:     }else{
1.2     ! takayama  106:       fprintf(fp," %2x( )-> ",c);
1.1       maekawa   107:     }
                    108:     fflush(NULL);
                    109:   }
                    110:   if (checkfp2(fp2," fp2fputc ") == -1) return(-1);
                    111:   (fp2->writeBuf)[fp2->writepos] = c;
                    112:   (fp2->writepos)++;
                    113:   if (fp2->writepos < fp2->limit) {
                    114:     return(c);
                    115:   }else{
                    116:     if (fp2fflush(fp2) <0) return(-1);
                    117:     else return(c);
                    118:   }
                    119: }
                    120:
                    121: int fp2fgetc(FILE2 *fp2) {
                    122:   int c;
1.2     ! takayama  123:   FILE *fp;
1.1       maekawa   124:   /* printf("fp2fgetc is called. "); fflush(NULL); */
                    125:   if (checkfp2(fp2," fp2fgetc ") == -1) return(-1);
                    126:   if (fp2->readpos < fp2->readsize) {
                    127:     fp2->readpos++;
                    128:     c = fp2->readBuf[fp2->readpos -1];
1.2     ! takayama  129:     if (fp2->watch || WatchStream) {
        !           130:          if (fp2->watch) fp = fp2->watchFile;
        !           131:          else fp = stderr;
1.1       maekawa   132:       if (c >= ' ' && c <= 'z') {
1.2     ! takayama  133:                fprintf(fp," %2x(%c) ",c,c);
1.1       maekawa   134:       }else{
1.2     ! takayama  135:                fprintf(fp," %2x( ) ",c);
1.1       maekawa   136:       }
                    137:       fflush(NULL);
                    138:     }
                    139:     return(c);
                    140:   }else{
                    141:     fp2->readpos = 0;
                    142:     fp2 ->readsize =
                    143:       read(fp2->fd, fp2->readBuf, fp2->limit);
                    144:     if (fp2->readsize == 0) {
1.2     ! takayama  145:       if (fp2->watch || WatchStream) {
        !           146:                if (fp2->watch) fp = fp2->watchFile;
        !           147:                else fp = stderr;
        !           148:                fprintf(fp," <%2x ",c);
        !           149:                fflush(NULL);
1.1       maekawa   150:       }
                    151:       return(-1);
                    152:     }
                    153:     else {
                    154:       return(fp2fgetc(fp2));
                    155:     }
                    156:   }
                    157: }
                    158:
                    159: int fp2select(FILE2 *fp2, int t) {
                    160:   if (fp2->readpos < fp2->readsize) return(1);
                    161:   else {
                    162: #ifdef KXX
                    163:     return(oxSocketSelect0(fp2->fd,t));
                    164: #else
                    165:     return(KsocketSelect0(fp2->fd,t));
                    166: #endif
                    167:   }
                    168: }
                    169:
                    170: int fp2dumpBuffer(FILE2 *fp2) {
                    171:   int i;
                    172:   if (checkfp2(fp2," f2pdumpBuf ") == -1) {
                    173:     return(-1);
                    174:   }
                    175:   printf("fd=%d\n",fp2->fd);
                    176:   printf("initialied=%d\n",fp2->initialized);
                    177:   printf("readpos=%d\n",fp2->readpos);
                    178:   printf("readsize=%d\n",fp2->readsize);
                    179:   printf("writepos=%d\n",fp2->writepos);
                    180:   printf("limit=%d\n",fp2->limit);
                    181:   for (i=0; i<fp2->readsize; i++) {
                    182:     printf("readBuf[%d]=%2x ",i,fp2->readBuf[i]);
                    183:   }
                    184:   for (i=0; i<fp2->writepos; i++) {
                    185:     printf("writeBuf[%d]=%2x ",i,fp2->writeBuf[i]);
                    186:   }
                    187:   printf("\n");
                    188:   return(0);
                    189: }
                    190:
                    191: int fp2clearReadBuf(FILE2 *fp2) {
                    192:   fd_set readfds;
                    193:   struct timeval timeout;
                    194:   extern int errno;
                    195:   int fd;
                    196: #define TMP00SIZE  2000
                    197:   char tmp00[TMP00SIZE];
                    198:   int n;
                    199:
                    200:   if (checkfp2(fp2," fp2clearReadBuf ") == -1) {
                    201:     return(-1);
                    202:   }
                    203:
                    204:   fp2->readsize=0; fp2->readpos = 0;  /* Clear the buffer. */
                    205:
                    206:   fd = fp2->fd;
                    207:   while (1) {
                    208:     FD_ZERO(&readfds);
                    209:     FD_SET(fd,&readfds);
                    210:     timeout.tv_sec = 0;
                    211:     timeout.tv_usec = (long) 0;
                    212:     fp2->readpos = fp2->readsize = 0;
                    213:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
                    214:       fprintf(stderr,"fp2clearReadBuf: Select error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
                    215:       return(-1);
                    216:     }
                    217:     if (FD_ISSET(fd,&readfds)) {
                    218:       n = read(fd,tmp00, TMP00SIZE);
                    219:       if (n <=  0) {
                    220:        fprintf(stderr,"fp2clearReadBuf: File is closed or read error.\n");
                    221:        return(-1);
                    222:       }
                    223:       if ( n < TMP00SIZE ) {
                    224:        return(0);
                    225:       }
                    226:     }else {
                    227:       return(0);
                    228:     }
                    229:   }
                    230: }
                    231: int fp2write(FILE2 *os, char *data, int size) {
                    232:   int i,r;
                    233:   for (i=0; i<size; i++) {
                    234:     r = fp2fputc(data[i],os);
                    235:   }
                    236:   return(r);
                    237: }
                    238:
                    239: int fp2watch(FILE2 *fp2,FILE *f)
                    240: {
                    241:   if (f == NULL) {
                    242:     fprintf(stderr,"fp2watch FILE *f is NULL.\n");
                    243:     return(-1);
                    244:   }
                    245:   if (fp2 == NULL) {
                    246:     fprintf(stderr,"fp2watch FILE2 *fp2 is NULL.\n");
                    247:     return(-1);
                    248:   }
                    249:   if (fp2->watch) {
                    250:     fprintf(stderr,"fp2watch: fp2 is already taking a log.");
                    251:     return(-1);
                    252:   }
                    253:   fp2->watch = 1;
                    254:   fp2->watchFile = f;
                    255:   return(0);
                    256: }
                    257:
                    258: int fp2stopWatch(FILE2 *fp2)
                    259: {
                    260:   if (fp2 == NULL) {
                    261:     fprintf(stderr,"fp2stopWatch FILE2 *fp2 is NULL.\n");
                    262:     return(-1);
                    263:   }
                    264:   if (!fp2->watch) {
                    265:     fprintf(stderr,"fp2stopWatch: fp2 is not taking a log.");
                    266:     return(-1);
                    267:   }
                    268:   fp2->watch=0;
                    269:   if (fp2->watchFile != stdout) {
                    270:     return(fclose(fp2->watchFile));
                    271:   }
                    272: }
                    273:

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