Annotation of OpenXM/src/ox_math/sm_ext.c, Revision 1.8
1.1 ohara 1: /* -*- mode: C; coding: euc-japan -*- */
1.8 ! ohara 2: /* $OpenXM: OpenXM/src/ox_math/sm_ext.c,v 1.7 2003/02/04 14:27:43 ohara Exp $ */
1.1 ohara 3:
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: #include <stdio.h>
14: #include <stdlib.h>
15: #include <unistd.h>
1.8 ! ohara 16: #include <signal.h>
1.1 ohara 17: #include <ox_toolkit.h>
18: #include "sm.h"
1.8 ! ohara 19: #include "mlo.h"
1.1 ohara 20:
1.2 ohara 21: static struct { int (*func_ptr)(); int key; } tbl_smcmd[] = {
22: {sm_executeFunction, SM_executeFunction},
23: {sm_executeStringByLocalParser, SM_executeStringByLocalParser},
24: {sm_executeStringByLocalParser, SM_executeStringByLocalParserInBatchMode},
25: {sm_mathcap, SM_mathcap},
26: {sm_set_mathcap, SM_setMathCap},
27: {sm_popString, SM_popString},
28: {sm_popCMO, SM_popCMO},
29: {sm_pops, SM_pops},
30: {shutdown, SM_shutdown},
31: {NULL, NULL}
32: };
33:
34: extern OXFILE *stack_oxfp;
35:
1.8 ! ohara 36: static sigset_t mask;
! 37: static int flag_state_interrupting = 0;
! 38:
! 39: /* state management for the OpenXM robust interruption */
! 40: void sm_state_set_interrupting()
! 41: {
! 42: ml_state_set(RESERVE_INTERRUPTION);
! 43: }
! 44:
! 45: int sm_state_interrupting()
! 46: {
! 47: return ml_state(RESERVE_INTERRUPTION);
! 48: }
! 49:
! 50: void sm_state_clear_interrupting()
! 51: {
! 52: ml_state_clear_all();
! 53: }
! 54:
! 55: /* handling OpenXM singals */
! 56: static void sm_sighandler()
! 57: {
! 58: sm_state_set_interrupting();
! 59: }
! 60:
! 61: /* generating the mask pattern */
! 62: void sm_siginit()
! 63: {
! 64: signal(SIGUSR1, sm_sighandler);
! 65: sigemptyset(&mask);
! 66: sigaddset(&mask, SIGUSR1);
! 67: }
! 68:
! 69: void sm_sigmask()
! 70: {
! 71: sigprocmask(SIG_BLOCK, &mask, NULL);
! 72: }
! 73:
! 74: void sm_sigunmask()
! 75: {
! 76: sigprocmask(SIG_UNBLOCK, &mask, NULL);
! 77: }
! 78:
1.2 ohara 79: int (*sm_search_f(int code))()
80: {
81: int i;
82: for (i=0; tbl_smcmd[i].key != NULL; i++) {
83: if (code == tbl_smcmd[i].key) {
84: return tbl_smcmd[i].func_ptr;
85: }
1.1 ohara 86: }
1.2 ohara 87: return NULL;
1.1 ohara 88: }
89:
90: /* MathLink dependent */
1.2 ohara 91: void sm_popString()
1.1 ohara 92: {
93: char *s;
94: cmo *err;
1.7 ohara 95: cmo *m = pop();
1.1 ohara 96: if (m->tag == CMO_STRING) {
1.2 ohara 97: send_ox_cmo(stack_oxfp, m);
1.1 ohara 98: }else if ((s = new_string_set_cmo(m)) != NULL) {
1.2 ohara 99: send_ox_cmo(stack_oxfp, (cmo *)new_cmo_string(s));
1.1 ohara 100: }else {
101: err = make_error_object(SM_popString, m);
1.2 ohara 102: send_ox_cmo(stack_oxfp, err);
1.1 ohara 103: }
104: }
105:
106: int local_execute(char *s)
107: {
1.2 ohara 108: extern int flag_mlo_symbol;
109:
1.1 ohara 110: if(*s == 'i') {
111: switch(s[1]) {
112: case '+':
113: flag_mlo_symbol = FLAG_MLTKSYM_IS_STRING;
114: break;
115: case '-':
116: case '=':
117: default:
118: flag_mlo_symbol = FLAG_MLTKSYM_IS_INDETERMINATE;
119: }
120: }
121: return 0;
122: }
123:
124: /* The following function is depend on an implementation of a server. */
1.2 ohara 125: void sm_executeStringByLocalParser()
1.1 ohara 126: {
127: cmo* m = pop();
128: char *s = NULL;
129: if (m->tag == CMO_STRING
130: && strlen(s = ((cmo_string *)m)->s) != 0) {
131: if (s[0] == ':') {
132: local_execute(++s);
133: }else {
134: /* for mathematica */
135: /* Sending the string `s' to mathematica for its evaluation. */
136: ml_evaluateStringByLocalParser(s);
1.8 ! ohara 137: sm_sigunmask();
! 138: ml_select();
! 139: sm_sigmask();
1.7 ohara 140: m = ml_return();
141: push(m);
1.1 ohara 142: }
1.2 ohara 143: }else {
1.7 ohara 144: ox_printf(" <%s>", get_symbol_by_tag(m->tag));
1.2 ohara 145: push_error(SM_executeStringByLocalParser, m);
146: }
1.1 ohara 147: }
148:
1.2 ohara 149: void sm_executeFunction()
1.1 ohara 150: {
151: int i, argc;
152: cmo **argv;
153: char* func;
154: cmo* m;
155:
156: if ((m = pop()) == NULL || m->tag != CMO_STRING) {
1.2 ohara 157: push_error(SM_executeFunction, m);
1.1 ohara 158: }
159: func = ((cmo_string *)m)->s;
160:
161: if ((m = pop()) == NULL || m->tag != CMO_INT32) {
1.2 ohara 162: push_error(SM_executeFunction, m);
1.1 ohara 163: }
164:
165: argc = ((cmo_int32 *)m)->i;
166: argv = malloc(argc*sizeof(cmo *));
167: for (i=0; i<argc; i++) {
168: argv[i] = pop();
169: }
170: ml_executeFunction(func, argc, argv);
1.8 ! ohara 171: sm_sigunmask();
! 172: ml_select();
! 173: sm_sigmask();
1.7 ohara 174: m = ml_return();
175: push(m);
1.1 ohara 176: }
177:
1.2 ohara 178: void sm_mathcap()
1.1 ohara 179: {
1.2 ohara 180: push((cmo *)oxf_cmo_mathcap(stack_oxfp));
1.1 ohara 181: }
182:
1.2 ohara 183: void sm_set_mathcap()
1.1 ohara 184: {
185: cmo_mathcap *m = (cmo_mathcap *)pop();
186: if (m->tag == CMO_MATHCAP) {
1.2 ohara 187: oxf_mathcap_update(stack_oxfp, m);
1.1 ohara 188: }else {
189: push_error(-1, m);
190: /* an error object must be pushed */
191: }
192: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>