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

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.14    ! ohara       2: /* $OpenXM: OpenXM/src/ox_math/math2ox.c,v 1.13 2000/02/14 09:39:12 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.7       ohara      24: #include "serv2.h"
1.1       ohara      25:
                     26: static char *host    = "localhost";
                     27: static char *ctlserv = "ox";
                     28: static char *oxprog  = "ox_sm1";
                     29:
1.13      ohara      30: static ox_file_t *ss = NULL;
                     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.13      ohara      40:     receive_ox_tag(ss[id]->stream);
                     41:     c = receive_cmo(ss[id]->stream);
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.12      ohara      81: /* Parsing s and sending its cmo to an OX server. */
1.13      ohara      82: int OX_parse(int id, char *s)
1.3       ohara      83: {
                     84:     cmo *m;
1.6       ohara      85:     int len = strlen(s);
1.10      ohara      86:     init_parser(s);
1.3       ohara      87:
                     88:     if(s != NULL && len > 0 && (m = parse()) != NULL) {
1.6       ohara      89:         if (m->tag == OX_DATA) {
1.13      ohara      90:             send_ox_cmo(ss[id]->stream, ((ox_data *)m)->cmo);
1.6       ohara      91:         }else if (m->tag == OX_COMMAND) {
1.13      ohara      92:             send_ox_command(ss[id]->stream, ((ox_command *)m)->command);
1.6       ohara      93:         }else {
1.13      ohara      94:             send_ox_cmo(ss[id]->stream, m);
1.6       ohara      95:         }
                     96:         return 0;
                     97:     }
1.12      ohara      98:     return -1; /* if we failed. */
1.3       ohara      99: }
1.1       ohara     100:
                    101: int OX_start(char* s)
                    102: {
                    103:     if (s != NULL && s[0] != '\0') {
                    104:         oxprog = s;
                    105:     }
1.13      ohara     106:        if (++max_process < len_ss) {
                    107:                ss[max_process] = ox_start(host, ctlserv, oxprog);
1.9       ohara     108:                fprintf(stderr, "open (%s)\n", "localhost");
                    109:                return max_process;
                    110:        }
                    111:        return -1;
1.11      ohara     112: }
                    113:
                    114: int OX_start_remote_ssh(char *s, char *host)
                    115: {
                    116:     if (s != NULL && s[0] != '\0') {
                    117:         oxprog = s;
                    118:     }
                    119:     if (host != NULL || host[0] == '\0') {
                    120:         host = "localhost";
                    121:     }
1.13      ohara     122:        if (++max_process < len_ss) {
                    123:                ss[max_process] = ox_start_remote_with_ssh(oxprog, host);
1.11      ohara     124:                fprintf(stderr, "open (%s)\n", host);
                    125:                return max_process;
                    126:        }
                    127:     return -1;
1.4       ohara     128: }
                    129:
                    130: int OX_start_insecure(char *host, int portCtl, int portDat)
                    131: {
                    132:     if (host != NULL || host[0] == '\0') {
                    133:         host = "localhost";
                    134:     }
1.6       ohara     135:     if (portCtl == 0) {
                    136:         portCtl = 1200;
                    137:     }
                    138:     if (portDat == 0) {
                    139:         portDat = 1300;
                    140:     }
                    141:
1.13      ohara     142:        if (++max_process < len_ss) {
                    143:                ss[max_process] = ox_start_insecure_nonreverse(host, portCtl, portDat);
                    144:                fprintf(stderr, "math2ox :: connect to \"%s\" with (ctl, dat) = (%d, %d)\n", host, portCtl, portDat);
                    145:                return max_process;
                    146:        }
1.7       ohara     147:
1.13      ohara     148:     return -1;
1.1       ohara     149: }
                    150:
                    151: static char *cp_str(char *src)
                    152: {
                    153:     char *dest = malloc(strlen(src)+1);
                    154:     strcpy(dest, src);
                    155:     return dest;
                    156: }
                    157:
                    158: int OX_setClientParam(char *h, char* c, char* p)
                    159: {
                    160:     host    = cp_str(h);
                    161:     ctlserv = cp_str(c);
                    162:     oxprog  = cp_str(p);
                    163:     return 0;
                    164: }
                    165:
1.13      ohara     166: static ox_file_t *new_sstack(int size)
                    167: {
                    168:        max_process = 0;
                    169:        len_ss = size;
                    170:        return (ox_file_t *)malloc(sizeof(ox_file_t)*len_ss);
                    171: }
                    172:
                    173: static ox_file_t ss_id(int id)
                    174: {
                    175:        return ss[id];
                    176: }
                    177:
                    178: static int  ss_id_stream(int id)
                    179: {
                    180:        return ss[id]->stream;
                    181: }
                    182:
1.1       ohara     183: int main(int argc, char *argv[])
                    184: {
1.12      ohara     185:     /* setting the OX parser */
1.6       ohara     186:     setflag_parse(PFLAG_ADDREV);
1.13      ohara     187:        ss = new_sstack(20);
1.8       ohara     188:
1.1       ohara     189:     MLMain(argc, argv);
                    190: }

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