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

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

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