[BACK]Return to nullstackmachine.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kxx

Annotation of OpenXM/src/kxx/nullstackmachine.c, Revision 1.3

1.1       maekawa     1: #include <stdio.h>
                      2: #include "ox_kan.h"
                      3: #include "serversm.h"
                      4: #include <setjmp.h>
                      5: jmp_buf EnvOfStackMachine;  /* dummy data. */
                      6:
                      7: int SerialCurrent = -1;
                      8: int Quiet = 0;
                      9:
                     10: void *GC_malloc(n) {
                     11:   return((void *)malloc(n));
                     12: }
                     13:
                     14: /* internal use. */
                     15: int Sm1_popInt();
                     16:
                     17: Sm1_start() {
                     18:   fprintf(stderr,"nullstackmachine: sleep, pstack\n");
                     19: }
                     20:
                     21: int nullCmoGetInt32(ox_stream ostream)
                     22: {
                     23:   char d[4];
                     24:   int i;
                     25:   for (i=0; i<4; i++) {
                     26:     d[i] = fp2fgetc(ostream);
                     27:   }
                     28:   return(ntohl(* ( (int *)d)));
                     29: }
                     30:
                     31: /*  server stack machine */
                     32: static CMO_Object *LocalStack[200];
                     33: static int Stackp = 0;
                     34: void Sm1_pushToLocalStack(CMO_Object *op) {
                     35:   if (Stackp < 200) {
                     36:     LocalStack[Stackp++] = op;
                     37:   }else{
                     38:     fprintf(stderr,"Stack Overflow.\n");
                     39:   }
                     40: }
                     41: CMO_Object *Sm1_popFromLocalStack() {
                     42:   fprintf(stderr,"Stackp=%d\n",Stackp);
                     43:   if (Stackp <= 0) {
                     44:     fprintf(stderr,"Stack underflow.\n");
                     45:     return(NULL);
                     46:   }
                     47:   Stackp--;
                     48:   return(LocalStack[Stackp]);
                     49: }
                     50:
                     51: CMO_Object *CMO_new_string(char *s) {
                     52:   CMO_string_object *op;
                     53:   int i;
                     54:   op = (CMO_string_object *)mymalloc(sizeof(CMO_string_object)+strlen(s));
                     55:   op->tag = htonl(CMO_STRING);
                     56:   op->size = htonl(strlen(s)+1);
                     57:   for (i=0; i< strlen(s); i++) {
                     58:     (op->data)[i] = s[i];
                     59:     (op->data)[i+1] = '\0';
                     60:   }
                     61:   return( (CMO_Object *)op);
                     62: }
                     63: CMO_Object *CMO_new_int32(int k) {
                     64:   CMO_int32_object *op;
                     65:   int i;
                     66:   op = (CMO_int32_object *)mymalloc(sizeof(CMO_int32_object));
                     67:   op->tag = htonl(CMO_INT32);
                     68:   op->n = k;
                     69:   return( (CMO_Object *)op);
                     70: }
                     71: void printCMO_object(FILE *fp,CMO_Object *op)
                     72: {
                     73:   int n,i;
                     74:   if (op == NULL) {
                     75:     fprintf(fp,"null");
                     76:   }else{
                     77:     switch(ntohl(op->tag)) {
                     78:     case CMO_INT32:
                     79:       fprintf(fp,"%d",((CMO_int32_object *)op)->n);
                     80:       break;
                     81:     case CMO_STRING:
                     82:       n = ntohl(((CMO_string_object *)op)->size);
                     83:       fprintf(stderr,"n=%d :"); fflush(NULL);
                     84:       for (i=0; i<n; i++) {
1.2       takayama   85:         fprintf(fp,"%c",((CMO_string_object *)op)->data[i]);
1.1       maekawa    86:       }
                     87:       break;
                     88:     default:
                     89:       fprintf(fp,"Unknown object: tag=%d ",ntohl(op->tag));
                     90:       break;
                     91:     }
                     92:   }
                     93: }
                     94:
                     95: void *Sm1_mathcap(void) {
                     96:   int n,i;
                     97:   struct mathCap *mathcap;
                     98:   mathcap = (struct mathCap *) malloc(sizeof(struct mathCap));
                     99:   strcpy(mathcap->name,"nullserver00 Version=0.1");
                    100:   mathcap->version = 199901160;
                    101:   mathcap->cmo[0] = CMO_ERROR2;
                    102:   mathcap->cmo[1] = CMO_NULL;
                    103:   mathcap->cmo[2] = CMO_INT32;
                    104:   mathcap->cmo[3] = CMO_STRING;
                    105:   mathcap->cmo[4] = CMO_LIST;
                    106:   mathcap->n =  5;
                    107:   return((void *)mathcap);
                    108: }
                    109: int Sm1_setMathCap(ox_stream os) {
                    110:   fprintf(stderr,"setMathCap is not implemented.\n");
                    111:   return(-1);
                    112: }
                    113: void Sm1_pops(void) {
                    114:   int n;
                    115:   n = Sm1_popInt32();
                    116:   Stackp -= n;
                    117:   if (Stackp < 0) Stackp = 0;
                    118: }
                    119: int Sm1_executeStringByLocalParser(void) {
                    120:   char *s;
                    121:   CMO_Object *op;
                    122:   int i;
                    123:   s = Sm1_popString();
                    124:   if (s != NULL) {
                    125:     if (strcmp(s,"sleep") == 0) {
                    126:       while (1) {
1.2       takayama  127:         fprintf(stderr,"Sleeping...  "); fflush(NULL);
                    128:         sleep(10);
1.1       maekawa   129:       }
                    130:     }else if (strcmp(s,"pstack") == 0) {
                    131:       fprintf(stderr,"pstack -------------- Stackp = %d\n",Stackp);
                    132:       for (i=Stackp-1; i>=0; i--) {
1.2       takayama  133:         printCMO_object(stdout,LocalStack[i]); fprintf(stderr,"\n");
1.1       maekawa   134:       }
                    135:       fprintf(stderr,"\n--------------------\n");
                    136:     }else{
                    137:       fprintf(stderr,"Unknown operator: %s\n",s);
                    138:     }
                    139:   }else {
                    140:     fprintf(stderr,"nullstackmachine.c: pop the null string.");
                    141:   }
                    142:   /* Sm1_pushToLocalStack(CMO_new_string(s)); */
                    143:   return(0);
                    144: }
                    145: char *Sm1_popString() {
                    146:   CMO_Object *op;
                    147:   CMO_string_object *sop;
                    148:   char *c;
                    149:   op = Sm1_popFromLocalStack();
                    150:   if (op != NULL) {
                    151:     switch(ntohl(op->tag)) {
                    152:     case CMO_INT32:
                    153:       c = (char *)malloc(30);
                    154:       sprintf(c,"%d",((CMO_int32_object *)op)->n);
                    155:       return(c);
                    156:       break;
                    157:     case CMO_STRING:
                    158:       sop = (CMO_string_object *)op;
                    159:       return(sop->data);
                    160:       break;
                    161:     default:
                    162:       fprintf(stderr,"tag error \n");
                    163:       return(NULL);
                    164:     }
                    165:   }else{
                    166:     return(NULL);
                    167:   }
                    168: }
                    169:
                    170: int Sm1_popInt32() {
                    171:   CMO_Object *op;
                    172:   CMO_int32_object *sop;
                    173:   op = Sm1_popFromLocalStack();
                    174:   if (op != NULL) {
                    175:     if (op->tag != htonl(CMO_INT32)) {
                    176:       fprintf(stderr,"Object on the stack is not CMO_INT32. \n");
                    177:       return(0);
                    178:     }
                    179:     sop = (CMO_int32_object *)op;
                    180:     return(sop->n);
                    181:   }else{
                    182:     return(0);
                    183:   }
                    184: }
                    185:
                    186:
                    187: int Sm1_setName(void)
                    188: {
                    189:   char *s;
                    190:   s = Sm1_popString();
                    191:   if (s != NULL) fprintf(stderr,"setName %s\n",s);
                    192:   return(-1);
                    193: }
                    194:
                    195: int Sm1_evalName(void)
                    196: {
                    197:   char *s;
                    198:   s = Sm1_popString();
                    199:   if (s != NULL) fprintf(stderr,"evalName : %s");
                    200:   return(-1);
                    201: }
                    202:
                    203: static int isData(FILE2 *fp)
                    204: {
                    205:   if (fp->readpos < fp->readsize) return(1);
                    206:   else {
                    207:     return(oxSocketSelect0(fp->fd,0));
                    208:   }
                    209: }
                    210:
                    211: int Sm1_pushCMO(ox_stream ostream) /* old one went to junk.c */
                    212: {
                    213:   int size;
                    214:   char data[1000];
                    215:   int i;
                    216:   int c,n;
                    217:   if (ostream == NULL || ostream->initialized != 1) {
                    218:     fprintf(stderr,"pushCMO,  ostream is not initialized or null.\n");
                    219:     return(-1);
                    220:   }
                    221:   /* Read data from ostream */
                    222:   fprintf(stderr,"----------- CMO data from stream -----------------\n");fflush(NULL);
                    223:   if (isData(ostream) || oxSocketSelect0(ostream->fd,-1)) {
                    224:     c = nullCmoGetInt32(ostream);
                    225:     fprintf(stderr,"cmo tag=%d : ",c);
                    226:     switch(c) {
                    227:     case CMO_ERROR2: fprintf(stderr,"CMO_ERROR2 ;"); break;
                    228:     case CMO_ERROR: fprintf(stderr,"CMO_ERROR ;"); break;
                    229:     case CMO_INT32: fprintf(stderr,"CMO_INT32 ;"); break;
                    230:     case CMO_STRING: fprintf(stderr,"CMO_STRING ;"); break;
                    231:     default: fprintf(stderr,"Unknown"); break;
                    232:     }
                    233:     switch(c) {
                    234:     case CMO_ERROR:
                    235:       break;
                    236:     case CMO_INT32:
                    237:       n = nullCmoGetInt32(ostream);
                    238:       fprintf(stderr,"%d",n);
                    239:       Sm1_pushToLocalStack(CMO_new_int32(n));
                    240:       break;
                    241:     case CMO_STRING:
                    242:       n = nullCmoGetInt32(ostream);
                    243:       fprintf(stderr,"size=%d ",n);
                    244:       if (n > 1000-2) {
1.2       takayama  245:         fprintf(stderr," size is too large. \n");
1.1       maekawa   246:       }else{
1.2       takayama  247:         for (i=0; i<n; i++) {
                    248:           data[i] = fp2fgetc(ostream);
                    249:           data[i+1] = '\0';
                    250:         }
                    251:         fprintf(stderr," string=%s ",data);
                    252:         Sm1_pushToLocalStack(CMO_new_string(data));
1.1       maekawa   253:       }
                    254:       break;
                    255:     default:
                    256:       do {
1.2       takayama  257:         if ((c = fp2fgetc(ostream)) == EOF) {
                    258:           fprintf(stderr,"pushCMOFromStrem: Select returns 0, but there is no data or unexpected EOF.\n");
                    259:           return(-1);
                    260:         }
                    261:         fprintf(stderr,"%2x ",c);
1.1       maekawa   262:       }while(isData(ostream));
                    263:     }
                    264:   }
                    265:   fprintf(stderr,"\n-------------------------------------------------\n"); fflush(NULL);
                    266:   return(0);
                    267: }
                    268:
                    269: int Sm1_popCMO(ox_stream os,int serial)
                    270: {
                    271:   FILE *fp2;
                    272:   extern int errno;
                    273:   int c;
                    274:   int p;
                    275:   char data[1000];
                    276:
                    277:   fp2 = fopen("ex1.cmo","r");
                    278:   if (fp2 == NULL) {
                    279:     fprintf(stderr,"popCMO : file ex1.cmo is not found.\n");
                    280:     return(-1);
                    281:   }
                    282:   p = 0;
                    283:   while ((c=fgetc(fp2)) != EOF) {
                    284:     data[p] = c; p++;
                    285:     if (p >= 1000) {fp2write(os,data,1000); p=0;}
                    286:     fprintf(stderr," %2x ",c);
                    287:   }
                    288:   if (p>0) { fp2write(os,data,p); }
                    289:   fp2fflush(os);
                    290:
                    291:   return(0);
                    292: }
                    293:
                    294: int Sm1_pushError2(int serial,int no,char *s)
                    295: {
                    296:   fprintf(stderr,"Sm1_pushError2 : [%d,%d,%s] \n",serial,no,s);
                    297: }
                    298:
                    299:
                    300: /* These are dummy.  It is defined in stackmachine.c */
                    301: unlockCtrlCForOx() { ; }
                    302: restoreLockCtrlCForOx() { ; }
1.3     ! takayama  303: void cancelAlarm() { ; }
1.1       maekawa   304:

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