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