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