[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.22

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.22    ! ohara       2: /* $OpenXM: OpenXM/src/ox_math/math2ox.c,v 1.21 2003/01/13 12:04:53 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: {
                     88:     cmo *m;
1.6       ohara      89:     int len = strlen(s);
1.10      ohara      90:     init_parser(s);
1.3       ohara      91:
                     92:     if(s != NULL && len > 0 && (m = parse()) != NULL) {
1.6       ohara      93:         if (m->tag == OX_DATA) {
1.17      ohara      94:             send_ox_cmo(ss[id], ((ox_data *)m)->cmo);
1.6       ohara      95:         }else if (m->tag == OX_COMMAND) {
1.17      ohara      96:             send_ox_command(ss[id], ((ox_command *)m)->command);
1.6       ohara      97:         }else {
1.17      ohara      98:             send_ox_cmo(ss[id], m);
1.6       ohara      99:         }
                    100:         return 0;
                    101:     }
1.12      ohara     102:     return -1; /* if we failed. */
1.3       ohara     103: }
1.1       ohara     104:
                    105: int OX_start(char* s)
                    106: {
                    107:     if (s != NULL && s[0] != '\0') {
                    108:         oxprog = s;
                    109:     }
1.13      ohara     110:        if (++max_process < len_ss) {
                    111:                ss[max_process] = ox_start(host, ctlserv, oxprog);
1.21      ohara     112:                ox_printf("open (%s)\n", "localhost");
1.9       ohara     113:                return max_process;
                    114:        }
                    115:        return -1;
1.11      ohara     116: }
                    117:
                    118: int OX_start_remote_ssh(char *s, char *host)
                    119: {
                    120:     if (s != NULL && s[0] != '\0') {
                    121:         oxprog = s;
                    122:     }
                    123:     if (host != NULL || host[0] == '\0') {
                    124:         host = "localhost";
                    125:     }
1.13      ohara     126:        if (++max_process < len_ss) {
                    127:                ss[max_process] = ox_start_remote_with_ssh(oxprog, host);
1.21      ohara     128:                ox_printf("open (%s)\n", host);
1.11      ohara     129:                return max_process;
                    130:        }
                    131:     return -1;
1.4       ohara     132: }
                    133:
                    134: int OX_start_insecure(char *host, int portCtl, int portDat)
                    135: {
                    136:     if (host != NULL || host[0] == '\0') {
                    137:         host = "localhost";
                    138:     }
1.6       ohara     139:     if (portCtl == 0) {
                    140:         portCtl = 1200;
                    141:     }
                    142:     if (portDat == 0) {
                    143:         portDat = 1300;
                    144:     }
                    145:
1.13      ohara     146:        if (++max_process < len_ss) {
1.17      ohara     147:                ss[max_process] = ox_start_insecure(host, portCtl, portDat);
1.21      ohara     148:                ox_printf("math2ox :: connect to \"%s\" with (ctl, dat) = (%d, %d)\n", host, portCtl, portDat);
1.13      ohara     149:                return max_process;
                    150:        }
1.7       ohara     151:
1.13      ohara     152:     return -1;
1.1       ohara     153: }
                    154:
                    155: static char *cp_str(char *src)
                    156: {
                    157:     char *dest = malloc(strlen(src)+1);
                    158:     strcpy(dest, src);
                    159:     return dest;
                    160: }
                    161:
                    162: int OX_setClientParam(char *h, char* c, char* p)
                    163: {
                    164:     host    = cp_str(h);
                    165:     ctlserv = cp_str(c);
                    166:     oxprog  = cp_str(p);
                    167:     return 0;
                    168: }
                    169:
1.17      ohara     170: static OXFILE **new_sstack(int size)
1.13      ohara     171: {
                    172:        max_process = 0;
                    173:        len_ss = size;
1.17      ohara     174:        return (OXFILE **)malloc(sizeof(OXFILE *)*len_ss);
1.13      ohara     175: }
                    176:
1.17      ohara     177: static OXFILE *ss_id(int id)
1.13      ohara     178: {
                    179:        return ss[id];
                    180: }
                    181:
                    182: static int  ss_id_stream(int id)
                    183: {
1.17      ohara     184:        return ss[id];
1.13      ohara     185: }
                    186:
1.18      ohara     187: #define VERSION 0x11121400
                    188: #define ID_STRING  "2000/11/29"
                    189:
1.1       ohara     190: int main(int argc, char *argv[])
                    191: {
1.20      ohara     192:     ox_stderr_init(NULL);
                    193:
1.12      ohara     194:     /* setting the OX parser */
1.6       ohara     195:     setflag_parse(PFLAG_ADDREV);
1.13      ohara     196:        ss = new_sstack(20);
1.18      ohara     197:     mathcap_init(VERSION, ID_STRING, "math2ox", NULL, NULL);
1.8       ohara     198:
1.1       ohara     199:     MLMain(argc, argv);
                    200: }

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