[BACK]Return to math2ox.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_math

Annotation of OpenXM/src/ox_math/math2ox.c, Revision 1.23

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>