Annotation of OpenXM/src/ox_math/serv2.c, Revision 1.15
1.1 ohara 1: /* -*- mode: C; coding: euc-japan -*- */
1.15 ! ohara 2: /* $OpenXM: OpenXM/src/ox_math/serv2.c,v 1.14 2000/03/10 12:38:47 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.1 ohara 94: int sm_popCMO(int fd_write)
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) {
103: send_ox_cmo(fd_write, m);
104: return 0;
105: }
106: return SM_popCMO;
107: }
108:
109: int sm_pops(int fd_write)
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.1 ohara 120: int sm_popString(int fd_write)
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.6 ohara 132: send_ox_cmo(fd_write, m);
1.11 ohara 133: }else if ((s = new_string_set_cmo(m)) != NULL) {
1.2 ohara 134: send_ox_cmo(fd_write, (cmo *)new_cmo_string(s));
1.6 ohara 135: }else {
1.7 ohara 136: err = make_error_object(SM_popString, m);
137: send_ox_cmo(fd_write, err);
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.1 ohara 159: int sm_executeStringByLocalParser(int fd_write)
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:
188: int sm_executeFunction(int fd_write)
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
216: #define ID_STRING "ox_math server 1999/12/14 15:25:00"
1.1 ohara 217:
218: int sm_mathcap(int fd_write)
219: {
1.7 ohara 220: push(make_mathcap_object(VERSION, ID_STRING));
1.1 ohara 221: return 0;
222: }
223:
224: int receive_sm_command(int fd_read)
225: {
226: return receive_int32(fd_read);
227: }
228:
229: int execute_sm_command(int fd_write, int code)
230: {
231: int err = 0;
1.10 ohara 232: #ifdef DEBUG
1.14 ohara 233: symbol_t sp = lookup_by_tag(code);
234: fprintf(stderr, "ox_math:: %s received.\n", symbol_get_key(sp));
1.8 ohara 235: #endif
1.1 ohara 236:
237: switch(code) {
238: case SM_popCMO:
239: err = sm_popCMO(fd_write);
240: break;
241: case SM_popString:
242: err = sm_popString(fd_write);
243: break;
244: case SM_mathcap:
245: err = sm_mathcap(fd_write);
246: break;
247: case SM_pops:
248: err = sm_pops(fd_write);
249: break;
250: case SM_executeStringByLocalParser:
1.10 ohara 251: case SM_executeStringByLocalParserInBatchMode:
1.1 ohara 252: err = sm_executeStringByLocalParser(fd_write);
253: break;
254: case SM_executeFunction:
255: err = sm_executeFunction(fd_write);
256: break;
1.10 ohara 257: case SM_shutdown:
258: shutdown();
259: break;
1.2 ohara 260: case SM_setMathCap:
1.13 ohara 261: pop(); /* ignore */
1.1 ohara 262: break;
263: default:
264: fprintf(stderr, "unknown command: %d.\n", code);
1.7 ohara 265: err = ERROR_ID_UNKNOWN_SM;
1.1 ohara 266: }
267:
268: if (err != 0) {
1.7 ohara 269: push((cmo *)make_error_object(err, new_cmo_null()));
1.1 ohara 270: }
271: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>