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

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

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

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