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

Annotation of OpenXM/src/ox_math/serv2.c, Revision 1.18

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.18    ! ohara       2: /* $OpenXM: OpenXM/src/ox_math/serv2.c,v 1.17 2000/11/28 20:16:03 ohara Exp $ */
1.1       ohara       3:
1.13      ohara       4: /*
                      5:    Copyright (C) Katsuyoshi OHARA, 2000.
                      6:    Portions copyright 1999 Wolfram Research, Inc.
                      7:
                      8:    You must see OpenXM/Copyright/Copyright.generic.
                      9:    The MathLink Library is licensed from Wolfram Research Inc..
                     10:    See OpenXM/Copyright/Copyright.mathlink for detail.
                     11: */
                     12:
                     13: /*
                     14:    Remarks:
                     15:    file descripter 3 and 4 are already opened by the parent process.
                     16: */
1.1       ohara      17:
                     18: #include <stdio.h>
                     19: #include <stdlib.h>
                     20: #include <unistd.h>
                     21: #include <gmp.h>
                     22: #include <mathlink.h>
1.14      ohara      23: #include <ox_toolkit.h>
1.1       ohara      24: #include "serv2.h"
                     25:
1.10      ohara      26: extern int flag_mlo_symbol;
1.1       ohara      27:
1.13      ohara      28: /* MathLink independent */
1.18    ! ohara      29: static cmo **stack = NULL;
1.11      ohara      30: static int stack_size = 0;
1.18    ! ohara      31: static int stack_ptr = 0;
        !            32: OXFILE *stack_oxfp = NULL;
1.1       ohara      33:
1.18    ! ohara      34: #define DIFFERENCE_OF_STACK  1024
1.11      ohara      35:
1.18    ! ohara      36: static void stack_extend()
1.11      ohara      37: {
1.18    ! ohara      38:     int newsize = stack_size + DIFFERENCE_OF_STACK;
        !            39:     cmo **newstack = (cmo **)malloc(sizeof(cmo *)*newsize);
        !            40:     if (stack != NULL) {
        !            41:         memcpy(newstack, stack, sizeof(cmo *)*stack_size);
        !            42:         free(stack);
        !            43:     }
        !            44:     stack_size = newsize;
        !            45:     stack = newstack;
1.10      ohara      46: }
1.1       ohara      47:
                     48: int push(cmo* m)
                     49: {
                     50: #if DEBUG
1.14      ohara      51:     symbol_t symp;
1.6       ohara      52:
1.1       ohara      53:     if (m->tag == CMO_STRING) {
1.7       ohara      54:         fprintf(stderr, "ox_math:: a CMO_STRING(%s) was pushed.\n", ((cmo_string *)m)->s);
1.5       ohara      55:     }else {
1.7       ohara      56:         symp = lookup_by_tag(m->tag);
1.14      ohara      57:         fprintf(stderr, "ox_math:: a %s was pushed.\n", symbol_get_key(symp));
1.7       ohara      58:     }
1.1       ohara      59: #endif
1.18    ! ohara      60:     if (stack_ptr >= stack_size) {
        !            61:         stack_extend();
1.1       ohara      62:     }
1.18    ! ohara      63:     stack[stack_ptr] = m;
        !            64:     stack_ptr++;
1.1       ohara      65: }
                     66:
1.13      ohara      67: /* if the stack is empty, then pop() returns (CMO_NULL). */
1.1       ohara      68: cmo* pop()
                     69: {
1.18    ! ohara      70:     if (stack_ptr > 0) {
        !            71:         return stack[--stack_ptr];
1.1       ohara      72:     }
1.3       ohara      73:     return new_cmo_null();
1.1       ohara      74: }
                     75:
                     76: void pops(int n)
                     77: {
1.18    ! ohara      78:     stack_ptr -= n;
        !            79:     if (stack_ptr < 0) {
        !            80:         stack_ptr = 0;
1.1       ohara      81:     }
                     82: }
                     83:
1.18    ! ohara      84: void push_error(int errcode, cmo* pushback)
        !            85: {
        !            86:     return push((cmo *)make_error_object(errcode, pushback));
        !            87: }
        !            88:
1.13      ohara      89: /*
                     90:    if error occurs, then a sm_*() function returns non-zero and
                     91:    an error obect is set by a function which calls sm_*().
                     92: */
1.16      ohara      93: int sm_popCMO(OXFILE* oxfp)
1.1       ohara      94: {
                     95:     cmo* m = pop();
1.6       ohara      96: #ifdef DEBUG
1.15      ohara      97:     symbol_t symp = lookup_by_tag(m->tag);
1.14      ohara      98:     fprintf(stderr, "ox_math:: opecode = SM_popCMO. (%s)\n", symbol_get_key(symp));
1.6       ohara      99: #endif
1.10      ohara     100:
1.1       ohara     101:     if (m != NULL) {
1.16      ohara     102:         send_ox_cmo(oxfp, m);
1.1       ohara     103:         return 0;
                    104:     }
                    105:     return SM_popCMO;
                    106: }
                    107:
1.16      ohara     108: int sm_pops(OXFILE* oxfp)
1.1       ohara     109: {
                    110:     cmo* m = pop();
                    111:     if (m != NULL && m->tag == CMO_INT32) {
                    112:         pops(((cmo_int32 *)m)->i);
                    113:         return 0;
                    114:     }
1.7       ohara     115:     return ERROR_ID_UNKNOWN_SM;
1.1       ohara     116: }
                    117:
1.13      ohara     118: /* MathLink dependent */
1.16      ohara     119: int sm_popString(OXFILE* oxfp)
1.1       ohara     120: {
1.6       ohara     121:     char *s;
                    122:     cmo *err;
                    123:     cmo *m;
1.1       ohara     124:
                    125: #ifdef DEBUG
1.5       ohara     126:     fprintf(stderr, "ox_math:: opecode = SM_popString.\n");
1.1       ohara     127: #endif
                    128:
1.7       ohara     129:     m = pop();
                    130:     if (m->tag == CMO_STRING) {
1.16      ohara     131:         send_ox_cmo(oxfp, m);
1.11      ohara     132:     }else if ((s = new_string_set_cmo(m)) != NULL) {
1.16      ohara     133:         send_ox_cmo(oxfp, (cmo *)new_cmo_string(s));
1.6       ohara     134:     }else {
1.7       ohara     135:         err = make_error_object(SM_popString, m);
1.16      ohara     136:         send_ox_cmo(oxfp, err);
1.7       ohara     137:     }
                    138:     return 0;
1.6       ohara     139: }
                    140:
                    141: int local_execute(char *s)
                    142: {
1.10      ohara     143:     if(*s == 'i') {
                    144:         switch(s[1]) {
                    145:         case '+':
                    146:             flag_mlo_symbol = FLAG_MLTKSYM_IS_STRING;
                    147:             break;
                    148:         case '-':
                    149:         case '=':
                    150:         default:
                    151:             flag_mlo_symbol = FLAG_MLTKSYM_IS_INDETERMINATE;
                    152:         }
                    153:     }
1.7       ohara     154:     return 0;
1.1       ohara     155: }
                    156:
1.13      ohara     157: /* The following function is depend on an implementation of a server. */
1.16      ohara     158: int sm_executeStringByLocalParser(OXFILE* oxfp)
1.1       ohara     159: {
1.15      ohara     160:     symbol_t symp;
1.6       ohara     161:     cmo* m = pop();
1.7       ohara     162:     char *s = NULL;
1.1       ohara     163: #ifdef DEBUG
1.5       ohara     164:     fprintf(stderr, "ox_math:: opecode = SM_executeStringByLocalParser.\n");
1.1       ohara     165: #endif
1.6       ohara     166:
                    167:     if (m->tag == CMO_STRING
1.7       ohara     168:         && strlen(s = ((cmo_string *)m)->s) != 0) {
                    169:         if (s[0] == ':') {
1.8       ohara     170:             local_execute(++s);
1.7       ohara     171:         }else {
                    172:             /* for mathematica */
1.13      ohara     173:             /* Sending the string `s' to mathematica for its evaluation. */
1.10      ohara     174:             ml_evaluateStringByLocalParser(s);
1.18    ! ohara     175:             ml_select();
1.11      ohara     176:             push(receive_mlo());
1.7       ohara     177:         }
                    178:         return 0;
1.1       ohara     179:     }
1.6       ohara     180: #ifdef DEBUG
1.10      ohara     181:     symp = lookup_by_tag(m->tag);
1.14      ohara     182:     fprintf(stderr, "ox_math:: error. the top of stack is %s.\n", symbol_get_key(symp));
1.6       ohara     183: #endif
1.1       ohara     184:     return SM_executeStringByLocalParser;
                    185: }
                    186:
1.16      ohara     187: int sm_executeFunction(OXFILE* oxfp)
1.1       ohara     188: {
                    189:     int i, argc;
                    190:     cmo **argv;
                    191:     char* func;
                    192:     cmo* m;
                    193:
                    194:     if ((m = pop()) == NULL || m->tag != CMO_STRING) {
                    195:         return SM_executeFunction;
                    196:     }
                    197:     func = ((cmo_string *)m)->s;
                    198:
                    199:     if ((m = pop()) == NULL || m->tag != CMO_INT32) {
                    200:         return SM_executeFunction;
                    201:     }
1.6       ohara     202:
1.1       ohara     203:     argc = ((cmo_int32 *)m)->i;
1.7       ohara     204:     argv = malloc(argc*sizeof(cmo *));
1.1       ohara     205:     for (i=0; i<argc; i++) {
1.6       ohara     206:         argv[i] = pop();
1.1       ohara     207:     }
1.10      ohara     208:     ml_executeFunction(func, argc, argv);
1.18    ! ohara     209:     ml_select();
1.11      ohara     210:     push(receive_mlo());
1.1       ohara     211:     return 0;
                    212: }
                    213:
1.16      ohara     214: int sm_mathcap(OXFILE* oxfp)
1.1       ohara     215: {
1.17      ohara     216:     push((cmo *)oxf_cmo_mathcap(oxfp));
1.1       ohara     217:     return 0;
1.17      ohara     218: }
                    219:
                    220: void sm_set_mathcap(OXFILE *oxfp)
                    221: {
                    222:     cmo_mathcap *m = (cmo_mathcap *)pop();
                    223:     if (m->tag == CMO_MATHCAP) {
                    224:         oxf_mathcap_update(oxfp, m);
                    225:     }else {
                    226:         push_error(-1, m);
                    227:         /* an error object must be pushed */
                    228:     }
1.1       ohara     229: }
                    230:
1.16      ohara     231: int receive_sm_command(OXFILE* oxfp)
1.1       ohara     232: {
1.16      ohara     233:     return receive_int32(oxfp);
1.1       ohara     234: }
                    235:
1.16      ohara     236: int execute_sm_command(OXFILE* oxfp, int code)
1.1       ohara     237: {
                    238:     int err = 0;
1.10      ohara     239: #ifdef DEBUG
1.14      ohara     240:     symbol_t sp = lookup_by_tag(code);
                    241:     fprintf(stderr, "ox_math:: %s received.\n", symbol_get_key(sp));
1.8       ohara     242: #endif
1.1       ohara     243:
                    244:     switch(code) {
                    245:     case SM_popCMO:
1.16      ohara     246:         err = sm_popCMO(oxfp);
1.1       ohara     247:         break;
                    248:     case SM_popString:
1.16      ohara     249:         err = sm_popString(oxfp);
1.1       ohara     250:         break;
                    251:     case SM_mathcap:
1.16      ohara     252:         err = sm_mathcap(oxfp);
1.1       ohara     253:         break;
                    254:     case SM_pops:
1.16      ohara     255:         err = sm_pops(oxfp);
1.1       ohara     256:         break;
                    257:     case SM_executeStringByLocalParser:
1.10      ohara     258:     case SM_executeStringByLocalParserInBatchMode:
1.16      ohara     259:         err = sm_executeStringByLocalParser(oxfp);
1.1       ohara     260:         break;
                    261:     case SM_executeFunction:
1.16      ohara     262:         err = sm_executeFunction(oxfp);
1.1       ohara     263:         break;
1.10      ohara     264:     case SM_shutdown:
                    265:         shutdown();
                    266:         break;
1.2       ohara     267:     case SM_setMathCap:
1.13      ohara     268:         pop();  /* ignore */
1.1       ohara     269:         break;
                    270:     default:
                    271:         fprintf(stderr, "unknown command: %d.\n", code);
1.7       ohara     272:         err = ERROR_ID_UNKNOWN_SM;
1.1       ohara     273:     }
                    274:
                    275:     if (err != 0) {
1.7       ohara     276:         push((cmo *)make_error_object(err, new_cmo_null()));
1.1       ohara     277:     }
                    278: }

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