Annotation of OpenXM/src/ox_math/serv2.c, Revision 1.10
1.1 ohara 1: /* -*- mode: C; coding: euc-japan -*- */
1.10 ! ohara 2: /* $OpenXM: OpenXM/src/ox_math/serv2.c,v 1.9 1999/11/19 20:51:36 ohara Exp $ */
1.1 ohara 3:
4: /* Open Mathematica サーバ */
5: /* ファイルディスクリプタ 3, 4 は open されていると仮定して動作する. */
6:
7: #include <stdio.h>
8: #include <stdlib.h>
9: #include <unistd.h>
10: #include <gmp.h>
11: #include <mathlink.h>
12: #include "ox.h"
1.6 ohara 13: #include "parse.h"
1.1 ohara 14: #include "serv2.h"
15:
1.10 ! ohara 16: extern int flag_mlo_symbol;
1.1 ohara 17:
18: /* MathLink 非依存部分 */
19:
20: #define SIZE_OPERAND_STACK 2048
21:
22: static cmo* Operand_Stack[SIZE_OPERAND_STACK];
23: static int Stack_Pointer = 0;
24:
25: int initialize_stack()
26: {
27: Stack_Pointer = 0;
1.10 ! ohara 28: }
1.1 ohara 29:
30: int push(cmo* m)
31: {
32: #if DEBUG
1.7 ohara 33: symbol *symp;
1.6 ohara 34:
1.1 ohara 35: if (m->tag == CMO_STRING) {
1.7 ohara 36: fprintf(stderr, "ox_math:: a CMO_STRING(%s) was pushed.\n", ((cmo_string *)m)->s);
1.5 ohara 37: }else {
1.7 ohara 38: symp = lookup_by_tag(m->tag);
39: fprintf(stderr, "ox_math:: a %s was pushed.\n", symp->key);
40: }
1.1 ohara 41: #endif
42: Operand_Stack[Stack_Pointer] = m;
43: Stack_Pointer++;
44: if (Stack_Pointer >= SIZE_OPERAND_STACK) {
45: fprintf(stderr, "stack over flow.\n");
1.7 ohara 46: Stack_Pointer--;
1.1 ohara 47: }
48: }
49:
1.3 ohara 50: /* スタックが空のときは, (CMO_NULL) をかえす. */
1.1 ohara 51: cmo* pop()
52: {
53: if (Stack_Pointer > 0) {
54: Stack_Pointer--;
55: return Operand_Stack[Stack_Pointer];
56: }
1.3 ohara 57: return new_cmo_null();
1.1 ohara 58: }
59:
60: void pops(int n)
61: {
62: Stack_Pointer -= n;
63: if (Stack_Pointer < 0) {
64: Stack_Pointer = 0;
65: }
66: }
67:
68: /* sm_XXX 関数群は、エラーのときは 0 以外の値を返し、呼び出し元で
69: エラーオブジェクトをセットする */
70: int sm_popCMO(int fd_write)
71: {
72: cmo* m = pop();
1.6 ohara 73: #ifdef DEBUG
1.7 ohara 74: symbol *symp = lookup_by_tag(m->tag);
1.6 ohara 75: fprintf(stderr, "ox_math:: opecode = SM_popCMO. (%s)\n", symp->key);
76: #endif
1.10 ! ohara 77:
1.1 ohara 78: if (m != NULL) {
79: send_ox_cmo(fd_write, m);
80: return 0;
81: }
82: return SM_popCMO;
83: }
84:
85: int sm_pops(int fd_write)
86: {
87: cmo* m = pop();
88: if (m != NULL && m->tag == CMO_INT32) {
89: pops(((cmo_int32 *)m)->i);
90: return 0;
91: }
1.7 ohara 92: return ERROR_ID_UNKNOWN_SM;
1.1 ohara 93: }
94:
95: /* MathLink 依存部分 */
96: int sm_popString(int fd_write)
97: {
1.6 ohara 98: char *s;
99: cmo *err;
100: cmo *m;
1.1 ohara 101:
102: #ifdef DEBUG
1.5 ohara 103: fprintf(stderr, "ox_math:: opecode = SM_popString.\n");
1.1 ohara 104: #endif
105:
1.7 ohara 106: m = pop();
107: if (m->tag == CMO_STRING) {
1.6 ohara 108: send_ox_cmo(fd_write, m);
1.7 ohara 109: }else if ((s = convert_cmo_to_string(m)) != NULL) {
1.2 ohara 110: send_ox_cmo(fd_write, (cmo *)new_cmo_string(s));
1.6 ohara 111: }else {
1.7 ohara 112: err = make_error_object(SM_popString, m);
113: send_ox_cmo(fd_write, err);
114: }
115: return 0;
1.6 ohara 116: }
117:
118: int local_execute(char *s)
119: {
1.10 ! ohara 120: if(*s == 'i') {
! 121: switch(s[1]) {
! 122: case '+':
! 123: flag_mlo_symbol = FLAG_MLTKSYM_IS_STRING;
! 124: break;
! 125: case '-':
! 126: case '=':
! 127: default:
! 128: flag_mlo_symbol = FLAG_MLTKSYM_IS_INDETERMINATE;
! 129: }
! 130: }
1.7 ohara 131: return 0;
1.1 ohara 132: }
133:
134: /* この関数はサーバに依存する. */
135: int sm_executeStringByLocalParser(int fd_write)
136: {
1.7 ohara 137: symbol *symp;
1.6 ohara 138: cmo* m = pop();
1.7 ohara 139: char *s = NULL;
1.1 ohara 140: #ifdef DEBUG
1.5 ohara 141: fprintf(stderr, "ox_math:: opecode = SM_executeStringByLocalParser.\n");
1.1 ohara 142: #endif
1.6 ohara 143:
144: if (m->tag == CMO_STRING
1.7 ohara 145: && strlen(s = ((cmo_string *)m)->s) != 0) {
146: if (s[0] == ':') {
1.8 ohara 147: local_execute(++s);
1.7 ohara 148: }else {
149: /* for mathematica */
150: /* mathematica に文字列を送って評価させる */
1.10 ! ohara 151: ml_evaluateStringByLocalParser(s);
! 152: push(ml_get_object());
1.7 ohara 153: }
154: return 0;
1.1 ohara 155: }
1.6 ohara 156: #ifdef DEBUG
1.10 ! ohara 157: symp = lookup_by_tag(m->tag);
! 158: fprintf(stderr, "ox_math:: error. the top of stack is %s.\n", symp->key);
1.6 ohara 159: #endif
1.1 ohara 160: return SM_executeStringByLocalParser;
161: }
162:
163: int sm_executeFunction(int fd_write)
164: {
165: int i, argc;
166: cmo **argv;
167: char* func;
168: cmo* m;
169:
170: if ((m = pop()) == NULL || m->tag != CMO_STRING) {
171: return SM_executeFunction;
172: }
173: func = ((cmo_string *)m)->s;
174:
175: if ((m = pop()) == NULL || m->tag != CMO_INT32) {
176: return SM_executeFunction;
177: }
1.6 ohara 178:
1.1 ohara 179: argc = ((cmo_int32 *)m)->i;
1.7 ohara 180: argv = malloc(argc*sizeof(cmo *));
1.1 ohara 181: for (i=0; i<argc; i++) {
1.6 ohara 182: argv[i] = pop();
1.1 ohara 183: }
1.10 ! ohara 184: ml_executeFunction(func, argc, argv);
! 185: push(ml_get_object());
1.1 ohara 186: return 0;
187: }
188:
189: /* 平成11年10月13日 */
190: #define VERSION 0x11102700
191: #define ID_STRING "ox_math server 1999/10/28 17:29:25"
192:
193: int sm_mathcap(int fd_write)
194: {
1.7 ohara 195: push(make_mathcap_object(VERSION, ID_STRING));
1.1 ohara 196: return 0;
197: }
198:
199: int receive_sm_command(int fd_read)
200: {
201: return receive_int32(fd_read);
202: }
203:
204: int execute_sm_command(int fd_write, int code)
205: {
206: int err = 0;
1.10 ! ohara 207: #ifdef DEBUG
! 208: symbol *sp = lookup_by_tag(code);
! 209: fprintf(stderr, "ox_math:: %s received.\n", sp->key);
1.8 ohara 210: #endif
1.1 ohara 211:
212: switch(code) {
213: case SM_popCMO:
214: err = sm_popCMO(fd_write);
215: break;
216: case SM_popString:
217: err = sm_popString(fd_write);
218: break;
219: case SM_mathcap:
220: err = sm_mathcap(fd_write);
221: break;
222: case SM_pops:
223: err = sm_pops(fd_write);
224: break;
225: case SM_executeStringByLocalParser:
1.10 ! ohara 226: case SM_executeStringByLocalParserInBatchMode:
1.1 ohara 227: err = sm_executeStringByLocalParser(fd_write);
228: break;
229: case SM_executeFunction:
230: err = sm_executeFunction(fd_write);
231: break;
1.10 ! ohara 232: case SM_shutdown:
! 233: shutdown();
! 234: break;
1.2 ohara 235: case SM_setMathCap:
1.1 ohara 236: pop(); /* 無視する */
237: break;
238: default:
239: fprintf(stderr, "unknown command: %d.\n", code);
1.7 ohara 240: err = ERROR_ID_UNKNOWN_SM;
1.1 ohara 241: }
242:
243: if (err != 0) {
1.7 ohara 244: push((cmo *)make_error_object(err, new_cmo_null()));
1.1 ohara 245: }
246: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>