version 1.1.1.1, 1999/10/29 08:06:41 |
version 1.4, 1999/11/04 03:05:51 |
|
|
/* -*- mode: C; coding: euc-japan -*- */ |
/* -*- mode: C; coding: euc-japan -*- */ |
/* $OpenXM$ */ |
/* $OpenXM: OpenXM/src/ox_math/serv2.c,v 1.3 1999/11/03 10:56:40 ohara Exp $ */ |
/* $Id$ */ |
|
|
|
/* Open Mathematica サーバ */ |
/* Open Mathematica サーバ */ |
/* ファイルディスクリプタ 3, 4 は open されていると仮定して動作する. */ |
/* ファイルディスクリプタ 3, 4 は open されていると仮定して動作する. */ |
|
|
#define UNKNOWN_SM_COMMAND 50000 |
#define UNKNOWN_SM_COMMAND 50000 |
#define MATH_ERROR 50001 |
#define MATH_ERROR 50001 |
|
|
|
|
/* MLINK はポインタ型. */ |
/* MLINK はポインタ型. */ |
MLINK lp = NULL; |
MLINK lp = NULL; |
|
|
|
|
|
typedef cmo mlo; |
|
typedef cmo_string mlo_string; |
|
typedef cmo_zz mlo_zz; |
|
|
|
/* cmo_list の派生クラス*/ |
|
typedef struct { |
|
int tag; |
|
int length; |
|
cell *head; |
|
char *function; |
|
} mlo_function; |
|
|
|
|
|
mlo *receive_mlo_zz() |
|
{ |
|
char *s; |
|
mlo *m; |
|
|
|
MLGetString(lp, &s); |
|
fprintf(stderr, "--debug: zz = %s.\n", s); |
|
m = (mlo *)new_cmo_zz_set_string(s); |
|
MLDisownString(lp, s); |
|
return m; |
|
} |
|
|
|
mlo *receive_mlo_string() |
|
{ |
|
char *s; |
|
mlo *m; |
|
MLGetString(lp, &s); |
|
m = (cmo *)new_cmo_string(s); |
|
MLDisownString(lp, s); |
|
return m; |
|
} |
|
|
/* Mathematica を起動する. */ |
/* Mathematica を起動する. */ |
int MATH_init() |
int MATH_init() |
{ |
{ |
Line 69 char *MATH_getObject() |
|
Line 103 char *MATH_getObject() |
|
return s; |
return s; |
} |
} |
|
|
|
|
cmo *MATH_getObject2() |
cmo *MATH_getObject2() |
{ |
{ |
char *s; |
|
cmo *m; |
|
char **sp; |
|
int i,n; |
|
|
|
/* skip any packets before the first ReturnPacket */ |
/* skip any packets before the first ReturnPacket */ |
while (MLNextPacket(lp) != RETURNPKT) { |
while (MLNextPacket(lp) != RETURNPKT) { |
usleep(10); |
usleep(10); |
MLNewPacket(lp); |
MLNewPacket(lp); |
} |
} |
/* いまはタイプにかかわらず文字列を取得する. */ |
/* いまはタイプにかかわらず文字列を取得する. */ |
switch(MLGetNext(lp)) { |
return MATH_getObject3(); |
|
} |
|
|
|
cmo *MATH_getObject3() |
|
{ |
|
char *s; |
|
cmo *m; |
|
cmo *ob; |
|
int i,n; |
|
int type; |
|
|
|
/* いまはタイプにかかわらず文字列を取得する. */ |
|
type = MLGetNext(lp); |
|
switch(type) { |
case MLTKINT: |
case MLTKINT: |
fprintf(stderr, "type is INTEGER.\n"); |
fprintf(stderr, "--debug: MLO == MLTKINT.\n"); |
MLGetString(lp, &s); |
m = receive_mlo_zz(); |
m = (cmo *)new_cmo_zz_set_string(s); |
|
break; |
break; |
case MLTKSTR: |
case MLTKSTR: |
fprintf(stderr, "type is STRING.\n"); |
fprintf(stderr, "--debug: MLO == MLTKSTR.\n"); |
|
m = receive_mlo_string(); |
|
break; |
|
#if 0 |
|
case MLTKREAL: |
|
fprintf(stderr, "MLTKREAL is not supported: we use MLTKSTR.\n"); |
MLGetString(lp, &s); |
MLGetString(lp, &s); |
m = (cmo *)new_cmo_string(s); |
m = (cmo *)new_cmo_string(s); |
break; |
break; |
case MLTKERR: |
case MLTKERR: |
fprintf(stderr, "type is ERROR.\n"); |
fprintf(stderr, "--debug: MLO == MLTKERR.\n"); |
m = gen_error_object(MATH_ERROR); |
m = (cmo *)gen_error_object(MATH_ERROR); |
break; |
break; |
|
#endif |
case MLTKSYM: |
case MLTKSYM: |
fprintf(stderr, "MLTKSYM.\n"); |
fprintf(stderr, "--debug: MLO == MLTKSYM.\n"); |
MLGetString(lp, s); |
/* この部分に問題がある. */ |
|
MLGetSymbol(lp, &s); |
|
fprintf(stderr, "--debug: Symbol \"%s\".\n", s); |
m = (cmo *)new_cmo_string(s); |
m = (cmo *)new_cmo_string(s); |
break; |
break; |
case MLTKFUNC: |
case MLTKFUNC: |
fprintf(stderr, "MLTKFUNC.\n"); |
fprintf(stderr, "--debug: MLO == MLTKFUNC.\n"); |
#if DEBUG |
MLGetFunction(lp, &s, &n); |
MLGetString(lp, s); |
fprintf(stderr, "--debug: Function = \"%s\", # of args = %d\n", s, n); |
m = (cmo *)new_cmo_string(s); |
m = new_cmo_list(); |
break; |
append_cmo_list(m, new_cmo_string(s)); |
#endif |
fflush(stderr); |
MLGetFunction(lp, sp, &n); |
for (i=0; i<n; i++) { |
fprintf(stderr, "n = %d\n", n); |
fprintf(stderr, "--debug: arg[%d]\n", i); |
for (i=0; i<=n; i++) { |
fflush(stderr); |
fprintf(stderr, "%s "); |
ob = MATH_getObject3(); |
|
append_cmo_list(m, ob); |
} |
} |
fprintf(stderr, "\n"); |
|
m = (cmo *)new_cmo_string(s[0]); |
|
break; |
break; |
case MLTKREAL: |
|
fprintf(stderr, "MLTKREAL is not supported: we use MLTKSTR.\n"); |
|
MLGetString(lp, &s); |
|
m = (cmo *)new_cmo_string(s); |
|
break; |
|
default: |
default: |
fprintf(stderr, "unknown type: we use STRING.\n"); |
fprintf(stderr, "--debug: MLO(%d) != MLTKINT, MLTKSTR.\n", type); |
|
fprintf(stderr, "--debug: MLTKFUNC(%d), MLTKSYM(%d).\n", MLTKFUNC, MLTKSYM); |
|
|
MLGetString(lp, &s); |
MLGetString(lp, &s); |
|
fprintf(stderr, "--debug: \"%s\"\n", s); |
m = (cmo *)new_cmo_string(s); |
m = (cmo *)new_cmo_string(s); |
} |
} |
return m; |
return m; |
Line 144 int MATH_sendObject(cmo *m) |
|
Line 191 int MATH_sendObject(cmo *m) |
|
break; |
break; |
default: |
default: |
MLPutFunction(lp, "ToExpression", 1); |
MLPutFunction(lp, "ToExpression", 1); |
s = CONVERT_CMO_TO_CSTRING(m); |
s = convert_cmo_to_string(m); |
MLPutString(lp, s); |
MLPutString(lp, s); |
fprintf(stderr, "put %s.", s); |
fprintf(stderr, "put %s.", s); |
break; |
break; |
Line 196 int push(cmo* m) |
|
Line 243 int push(cmo* m) |
|
} |
} |
} |
} |
|
|
/* エラーのときは NULL を返す */ |
/* スタックが空のときは, (CMO_NULL) をかえす. */ |
/* gen_error_object(SM_popCMO); */ |
|
/* CMO_ERROR2 */ |
|
|
|
cmo* pop() |
cmo* pop() |
{ |
{ |
if (Stack_Pointer > 0) { |
if (Stack_Pointer > 0) { |
Stack_Pointer--; |
Stack_Pointer--; |
return Operand_Stack[Stack_Pointer]; |
return Operand_Stack[Stack_Pointer]; |
} |
} |
return NULL; |
return new_cmo_null(); |
} |
} |
|
|
void pops(int n) |
void pops(int n) |
Line 217 void pops(int n) |
|
Line 261 void pops(int n) |
|
} |
} |
} |
} |
|
|
|
|
/* sm_XXX 関数群は、エラーのときは 0 以外の値を返し、呼び出し元で |
/* sm_XXX 関数群は、エラーのときは 0 以外の値を返し、呼び出し元で |
エラーオブジェクトをセットする */ |
エラーオブジェクトをセットする */ |
int sm_popCMO(int fd_write) |
int sm_popCMO(int fd_write) |
Line 252 int sm_popString(int fd_write) |
|
Line 295 int sm_popString(int fd_write) |
|
fprintf(stderr, "code: SM_popString.\n"); |
fprintf(stderr, "code: SM_popString.\n"); |
#endif |
#endif |
|
|
if ((m = pop()) != NULL && (s = CONVERT_CMO_TO_CSTRING(m)) != NULL) { |
if ((m = pop()) != NULL && (s = convert_cmo_to_string(m)) != NULL) { |
send_ox_cmo(fd_write, new_cmo_string(s)); |
send_ox_cmo(fd_write, (cmo *)new_cmo_string(s)); |
return 0; |
return 0; |
} |
} |
return SM_popString; |
return SM_popString; |
Line 343 int execute_sm_command(int fd_write, int code) |
|
Line 386 int execute_sm_command(int fd_write, int code) |
|
case SM_executeFunction: |
case SM_executeFunction: |
err = sm_executeFunction(fd_write); |
err = sm_executeFunction(fd_write); |
break; |
break; |
case SM_setMathcap: |
case SM_setMathCap: |
pop(); /* 無視する */ |
pop(); /* 無視する */ |
break; |
break; |
default: |
default: |
Line 352 int execute_sm_command(int fd_write, int code) |
|
Line 395 int execute_sm_command(int fd_write, int code) |
|
} |
} |
|
|
if (err != 0) { |
if (err != 0) { |
push(gen_error_object(err)); |
push((cmo *)gen_error_object(err)); |
} |
} |
} |
} |