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

1.6     ! takayama    1: /*$OpenXM: OpenXM/src/kan96xx/plugin/file2.c,v 1.5 2003/11/17 05:45:47 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.6     ! takayama  105:        fprintf(stderr,"put to <%x> ",fp2->fd); /* output the channel for debug */
1.1       maekawa   106:     if (c >= ' ' && c <='z') {
1.3       takayama  107:       fprintf(fp," %2x(%c)-> ",c& 0xff,c);
1.1       maekawa   108:     }else{
1.3       takayama  109:       fprintf(fp," %2x( )-> ",c& 0xff);
1.1       maekawa   110:     }
                    111:     fflush(NULL);
                    112:   }
1.5       takayama  113:   if (fp2->log_outgoing != NULL) fputc(c,fp2->log_outgoing);
1.1       maekawa   114:   if (checkfp2(fp2," fp2fputc ") == -1) return(-1);
                    115:   (fp2->writeBuf)[fp2->writepos] = c;
                    116:   (fp2->writepos)++;
                    117:   if (fp2->writepos < fp2->limit) {
                    118:     return(c);
                    119:   }else{
                    120:     if (fp2fflush(fp2) <0) return(-1);
                    121:     else return(c);
                    122:   }
                    123: }
                    124:
                    125: int fp2fgetc(FILE2 *fp2) {
                    126:   int c;
1.2       takayama  127:   FILE *fp;
1.1       maekawa   128:   /* printf("fp2fgetc is called. "); fflush(NULL); */
                    129:   if (checkfp2(fp2," fp2fgetc ") == -1) return(-1);
                    130:   if (fp2->readpos < fp2->readsize) {
                    131:     fp2->readpos++;
                    132:     c = fp2->readBuf[fp2->readpos -1];
1.2       takayama  133:     if (fp2->watch || WatchStream) {
1.4       takayama  134:       if (fp2->watch) fp = fp2->watchFile;
                    135:       else fp = stderr;
1.6     ! takayama  136:       fprintf(fp,"get from <%x> ",fp2->fd); /* output the channel for debug*/
1.1       maekawa   137:       if (c >= ' ' && c <= 'z') {
1.4       takayama  138:         fprintf(fp," %2x(%c) ",c,c);
1.1       maekawa   139:       }else{
1.4       takayama  140:         fprintf(fp," %2x( ) ",c);
1.1       maekawa   141:       }
                    142:       fflush(NULL);
                    143:     }
1.5       takayama  144:     if (fp2->log_incomming != NULL) fputc(c,fp2->log_incomming);
1.1       maekawa   145:     return(c);
                    146:   }else{
                    147:     fp2->readpos = 0;
                    148:     fp2 ->readsize =
                    149:       read(fp2->fd, fp2->readBuf, fp2->limit);
                    150:     if (fp2->readsize == 0) {
1.2       takayama  151:       if (fp2->watch || WatchStream) {
1.4       takayama  152:         if (fp2->watch) fp = fp2->watchFile;
                    153:         else fp = stderr;
                    154:         fprintf(fp," <%2x ",c);
                    155:         fflush(NULL);
1.1       maekawa   156:       }
1.5       takayama  157:       if (fp2->log_incomming != NULL) fputc(c,fp2->log_incomming);
1.1       maekawa   158:       return(-1);
                    159:     }
                    160:     else {
                    161:       return(fp2fgetc(fp2));
                    162:     }
                    163:   }
                    164: }
                    165:
                    166: int fp2select(FILE2 *fp2, int t) {
                    167:   if (fp2->readpos < fp2->readsize) return(1);
                    168:   else {
                    169: #ifdef KXX
                    170:     return(oxSocketSelect0(fp2->fd,t));
                    171: #else
                    172:     return(KsocketSelect0(fp2->fd,t));
                    173: #endif
                    174:   }
                    175: }
                    176:
                    177: int fp2dumpBuffer(FILE2 *fp2) {
                    178:   int i;
                    179:   if (checkfp2(fp2," f2pdumpBuf ") == -1) {
                    180:     return(-1);
                    181:   }
                    182:   printf("fd=%d\n",fp2->fd);
                    183:   printf("initialied=%d\n",fp2->initialized);
                    184:   printf("readpos=%d\n",fp2->readpos);
                    185:   printf("readsize=%d\n",fp2->readsize);
                    186:   printf("writepos=%d\n",fp2->writepos);
                    187:   printf("limit=%d\n",fp2->limit);
                    188:   for (i=0; i<fp2->readsize; i++) {
                    189:     printf("readBuf[%d]=%2x ",i,fp2->readBuf[i]);
                    190:   }
                    191:   for (i=0; i<fp2->writepos; i++) {
                    192:     printf("writeBuf[%d]=%2x ",i,fp2->writeBuf[i]);
                    193:   }
                    194:   printf("\n");
                    195:   return(0);
                    196: }
                    197:
                    198: int fp2clearReadBuf(FILE2 *fp2) {
                    199:   fd_set readfds;
                    200:   struct timeval timeout;
                    201:   extern int errno;
                    202:   int fd;
                    203: #define TMP00SIZE  2000
                    204:   char tmp00[TMP00SIZE];
                    205:   int n;
                    206:
                    207:   if (checkfp2(fp2," fp2clearReadBuf ") == -1) {
                    208:     return(-1);
                    209:   }
                    210:
                    211:   fp2->readsize=0; fp2->readpos = 0;  /* Clear the buffer. */
                    212:
                    213:   fd = fp2->fd;
                    214:   while (1) {
                    215:     FD_ZERO(&readfds);
                    216:     FD_SET(fd,&readfds);
                    217:     timeout.tv_sec = 0;
                    218:     timeout.tv_usec = (long) 0;
                    219:     fp2->readpos = fp2->readsize = 0;
                    220:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
                    221:       fprintf(stderr,"fp2clearReadBuf: Select error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
                    222:       return(-1);
                    223:     }
                    224:     if (FD_ISSET(fd,&readfds)) {
                    225:       n = read(fd,tmp00, TMP00SIZE);
                    226:       if (n <=  0) {
1.4       takayama  227:         fprintf(stderr,"fp2clearReadBuf: File is closed or read error.\n");
                    228:         return(-1);
1.1       maekawa   229:       }
                    230:       if ( n < TMP00SIZE ) {
1.4       takayama  231:         return(0);
1.1       maekawa   232:       }
                    233:     }else {
                    234:       return(0);
                    235:     }
                    236:   }
                    237: }
                    238: int fp2write(FILE2 *os, char *data, int size) {
                    239:   int i,r;
                    240:   for (i=0; i<size; i++) {
                    241:     r = fp2fputc(data[i],os);
                    242:   }
                    243:   return(r);
                    244: }
                    245:
                    246: int fp2watch(FILE2 *fp2,FILE *f)
                    247: {
                    248:   if (f == NULL) {
                    249:     fprintf(stderr,"fp2watch FILE *f is NULL.\n");
                    250:     return(-1);
                    251:   }
                    252:   if (fp2 == NULL) {
                    253:     fprintf(stderr,"fp2watch FILE2 *fp2 is NULL.\n");
                    254:     return(-1);
                    255:   }
                    256:   if (fp2->watch) {
                    257:     fprintf(stderr,"fp2watch: fp2 is already taking a log.");
                    258:     return(-1);
                    259:   }
                    260:   fp2->watch = 1;
                    261:   fp2->watchFile = f;
                    262:   return(0);
                    263: }
                    264:
                    265: int fp2stopWatch(FILE2 *fp2)
                    266: {
                    267:   if (fp2 == NULL) {
                    268:     fprintf(stderr,"fp2stopWatch FILE2 *fp2 is NULL.\n");
                    269:     return(-1);
                    270:   }
                    271:   if (!fp2->watch) {
                    272:     fprintf(stderr,"fp2stopWatch: fp2 is not taking a log.");
                    273:     return(-1);
                    274:   }
                    275:   fp2->watch=0;
                    276:   if (fp2->watchFile != stdout) {
                    277:     return(fclose(fp2->watchFile));
                    278:   }
                    279: }
                    280:
1.5       takayama  281: int fp2log(FILE2 *fp,FILE *incomming,FILE *outgoing) {
                    282:   fp->log_incomming = incomming;
                    283:   fp->log_outgoing = outgoing;
                    284:   return 0;
                    285: }
                    286: int fp2stopLog(FILE2 *fp) {
                    287:   if (fp->log_incomming != NULL) fclose(fp->log_incomming);
                    288:   if (fp->log_outgoing != NULL) fclose(fp->log_outgoing);
                    289:   fp->log_incomming = fp->log_outgoing = NULL;
                    290:   return 0;
                    291: }

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