Annotation of OpenXM/src/ox_math/math2ox.c, Revision 1.21
1.1 ohara 1: /* -*- mode: C; coding: euc-japan -*- */
1.21 ! ohara 2: /* $OpenXM: OpenXM/src/ox_math/math2ox.c,v 1.20 2003/01/11 12:38:57 ohara Exp $ */
1.11 ohara 3:
4: /*
1.12 ohara 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.
1.11 ohara 11: */
1.1 ohara 12:
13: #include <sys/types.h>
14: #include <sys/stat.h>
15: #include <fcntl.h>
16:
17: #include <stdio.h>
18: #include <stdlib.h>
19: #include <mathlink.h>
20: #include <unistd.h>
21: #include <signal.h>
1.7 ohara 22: #include <mathlink.h>
1.14 ohara 23: #include <ox_toolkit.h>
1.19 ohara 24: #include "sm.h"
1.1 ohara 25:
26: static char *host = "localhost";
27: static char *ctlserv = "ox";
28: static char *oxprog = "ox_sm1";
29:
1.17 ohara 30: static OXFILE **ss = NULL;
1.13 ohara 31: static int len_ss = 0;
1.9 ohara 32: static int max_process = 0;
1.1 ohara 33:
1.12 ohara 34: /* The following functions are called from Mathematica.
35: See math2.tm for detail. */
1.13 ohara 36: void OX_get(int id)
1.7 ohara 37: {
1.8 ohara 38: cmo *c = NULL;
1.7 ohara 39:
1.17 ohara 40: receive_ox_tag(ss[id]);
41: c = receive_cmo(ss[id]);
1.21 ! ohara 42: ox_printf("ox message is received in OxGet[].\n");
1.8 ohara 43: print_cmo(c);
1.9 ohara 44: send_mlo(c);
45: ml_flush();
1.7 ohara 46: }
47:
1.13 ohara 48: int OX_execute_string(int id, const char *str)
1.1 ohara 49: {
1.13 ohara 50: ox_execute_string(ss[id], str);
1.1 ohara 51: return 0;
52: }
53:
1.13 ohara 54: char *OX_popString(int id)
1.1 ohara 55: {
1.13 ohara 56: return ox_popString(ss[id]);
1.1 ohara 57: }
58:
1.13 ohara 59: void OX_popCMO(int id)
1.7 ohara 60: {
1.13 ohara 61: cmo *c = ox_pop_cmo(ss[id]);
1.9 ohara 62: send_mlo(c);
63: ml_flush();
1.7 ohara 64: }
65:
1.13 ohara 66: int OX_close(int id)
1.1 ohara 67: {
1.13 ohara 68: ox_close(ss[id]);
1.1 ohara 69: return 0;
70: }
71:
1.13 ohara 72: int OX_reset(int id)
1.1 ohara 73: {
1.13 ohara 74: ox_reset(ss[id]);
1.1 ohara 75: return 0;
76: }
77:
1.15 ohara 78: /* This function is compatible with previous version of math2ox. */
1.12 ohara 79: /* Parsing s and sending its cmo to an OX server. */
1.13 ohara 80: int OX_parse(int id, char *s)
1.15 ohara 81: {
1.16 ohara 82: return OX_sendMessage(id, s);
1.15 ohara 83: }
84:
85: /* After creating an OX message by parsing a string s,
86: the function send the message to the OX server id. */
87: int OX_sendMessage(int id, char *s)
1.3 ohara 88: {
89: cmo *m;
1.6 ohara 90: int len = strlen(s);
1.10 ohara 91: init_parser(s);
1.3 ohara 92:
93: if(s != NULL && len > 0 && (m = parse()) != NULL) {
1.6 ohara 94: if (m->tag == OX_DATA) {
1.17 ohara 95: send_ox_cmo(ss[id], ((ox_data *)m)->cmo);
1.6 ohara 96: }else if (m->tag == OX_COMMAND) {
1.17 ohara 97: send_ox_command(ss[id], ((ox_command *)m)->command);
1.6 ohara 98: }else {
1.17 ohara 99: send_ox_cmo(ss[id], m);
1.6 ohara 100: }
101: return 0;
102: }
1.12 ohara 103: return -1; /* if we failed. */
1.3 ohara 104: }
1.1 ohara 105:
106: int OX_start(char* s)
107: {
108: if (s != NULL && s[0] != '\0') {
109: oxprog = s;
110: }
1.13 ohara 111: if (++max_process < len_ss) {
112: ss[max_process] = ox_start(host, ctlserv, oxprog);
1.21 ! ohara 113: ox_printf("open (%s)\n", "localhost");
1.9 ohara 114: return max_process;
115: }
116: return -1;
1.11 ohara 117: }
118:
119: int OX_start_remote_ssh(char *s, char *host)
120: {
121: if (s != NULL && s[0] != '\0') {
122: oxprog = s;
123: }
124: if (host != NULL || host[0] == '\0') {
125: host = "localhost";
126: }
1.13 ohara 127: if (++max_process < len_ss) {
128: ss[max_process] = ox_start_remote_with_ssh(oxprog, host);
1.21 ! ohara 129: ox_printf("open (%s)\n", host);
1.11 ohara 130: return max_process;
131: }
132: return -1;
1.4 ohara 133: }
134:
135: int OX_start_insecure(char *host, int portCtl, int portDat)
136: {
137: if (host != NULL || host[0] == '\0') {
138: host = "localhost";
139: }
1.6 ohara 140: if (portCtl == 0) {
141: portCtl = 1200;
142: }
143: if (portDat == 0) {
144: portDat = 1300;
145: }
146:
1.13 ohara 147: if (++max_process < len_ss) {
1.17 ohara 148: ss[max_process] = ox_start_insecure(host, portCtl, portDat);
1.21 ! ohara 149: ox_printf("math2ox :: connect to \"%s\" with (ctl, dat) = (%d, %d)\n", host, portCtl, portDat);
1.13 ohara 150: return max_process;
151: }
1.7 ohara 152:
1.13 ohara 153: return -1;
1.1 ohara 154: }
155:
156: static char *cp_str(char *src)
157: {
158: char *dest = malloc(strlen(src)+1);
159: strcpy(dest, src);
160: return dest;
161: }
162:
163: int OX_setClientParam(char *h, char* c, char* p)
164: {
165: host = cp_str(h);
166: ctlserv = cp_str(c);
167: oxprog = cp_str(p);
168: return 0;
169: }
170:
1.17 ohara 171: static OXFILE **new_sstack(int size)
1.13 ohara 172: {
173: max_process = 0;
174: len_ss = size;
1.17 ohara 175: return (OXFILE **)malloc(sizeof(OXFILE *)*len_ss);
1.13 ohara 176: }
177:
1.17 ohara 178: static OXFILE *ss_id(int id)
1.13 ohara 179: {
180: return ss[id];
181: }
182:
183: static int ss_id_stream(int id)
184: {
1.17 ohara 185: return ss[id];
1.13 ohara 186: }
187:
1.18 ohara 188: #define VERSION 0x11121400
189: #define ID_STRING "2000/11/29"
190:
1.1 ohara 191: int main(int argc, char *argv[])
192: {
1.20 ohara 193: ox_stderr_init(NULL);
194:
1.12 ohara 195: /* setting the OX parser */
1.6 ohara 196: setflag_parse(PFLAG_ADDREV);
1.13 ohara 197: ss = new_sstack(20);
1.18 ohara 198: mathcap_init(VERSION, ID_STRING, "math2ox", NULL, NULL);
1.8 ohara 199:
1.1 ohara 200: MLMain(argc, argv);
201: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>