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

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

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