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