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

Annotation of OpenXM/src/ox_maple/simple2.c, Revision 1.2

1.1       takayama    1: /* -*- mode: C -*- */
1.2     ! iwane       2: /* $OpenXM: OpenXM/src/ox_maple/simple2.c,v 1.1 2004/07/02 13:10:43 takayama Exp $ */
1.1       takayama    3:
                      4: /* Example:
                      5:    maple
                      6:    read `simple2.ml`;
                      7:    ox_start_asir();
                      8:    ox_execute_string("fctr(x^1000-y^1000");");
                      9:    ox_pop_string();
                     10:    ctrl-C      ===> ox_asir is restarted.
                     11:    ox_execute_string("1+2;");
                     12:    ox_pop_string();
                     13:  */
                     14:
                     15: /* A sample implementation for oxserver invoked from maple
                     16:
                     17:    NOTE:
                     18:       Maple has the structure
                     19:          try --- catch  --- finally ---
                     20:       or
                     21:           traperror(---);
                     22:       However, they cannot catch the interruption.
                     23:       Under this restriction, any implementation of ox_reset by ctrl-C
                     24:       will become difficult.
                     25:      [ I've not yet tested signal() in simple2.c. If it works, then there
                     26:        is a hope. ]
                     27:
                     28:       This sample implementation gives a partial solution for the problem;
                     29:       we restart ox servers after ctrl-C.
                     30:  */
                     31:
                     32: #include <stdio.h>
                     33: #include <sys/socket.h>
                     34: #include <sys/time.h>
                     35:
                     36: #include "ox_toolkit.h"
                     37:
                     38: extern OXFILE *ox_start(char* host, char* prog1, char* prog2);
                     39: OXFILE *sv = NULL;
                     40: int In_ox = 0;
                     41:
                     42: #define ox_restart() if (sv != NULL && In_ox) { ox_shutdown(sv); In_ox=0; sv=NULL; ml_start_asir();}
                     43:
                     44:
                     45: int oxselect(OXFILE *serv) {
                     46:   fd_set readfds;
                     47:   struct timeval timeout;
                     48:   int fd;
                     49:   fd = serv->fd;
                     50:   FD_ZERO(&readfds);
                     51:   FD_SET(fd,&readfds);
                     52:   timeout.tv_sec = 0;
                     53:   timeout.tv_usec = (long) 1;
                     54:   if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
                     55:        fprintf(stderr,"Select error.\n");
                     56:        return (-1);
                     57:   }
                     58:   /* Maple seems to accept ctrl-C in select (and in sleep),
                     59:      but in the other parts,
                     60:      it does not accept ctrl-C out of select.
                     61:   */
                     62:   if (FD_ISSET(fd,&readfds)) return 1;
                     63:   else return 0;
                     64: }
                     65:
                     66: int ml_init() {
                     67:   static int p = 1;
                     68:   In_ox = 1;
                     69:   if (p) {
                     70:        GC_init();
                     71:     ox_stderr_init(stderr);
                     72:     p = 0;
                     73:   }
                     74:   In_ox = 0;
                     75: }
                     76:
1.2     ! iwane      77: int ml_start_maple()
        !            78: {
        !            79:     char *server = "ox_maple";
        !            80:        ml_init();
        !            81:
        !            82:     sv = ox_start("localhost", "ox", server);
        !            83:     if (sv == NULL) {
        !            84:         ox_printf("simple:: I cannot connect to servers.\n");
        !            85:         return -1;
        !            86:     }
        !            87:        return 0;
        !            88: }
1.1       takayama   89: int ml_start_asir()
                     90: {
                     91:     char *server = "ox_asir";
                     92:        ml_init();
                     93:
                     94:     sv = ox_start("localhost", "ox", server);
                     95:     if (sv == NULL) {
                     96:         ox_printf("simple:: I cannot connect to servers.\n");
                     97:         return -1;
                     98:     }
                     99:        return 0;
                    100: }
                    101:
                    102: int ml_push_int(int i) {
                    103:   if (sv == NULL) ml_start_asir();
                    104:   ox_restart();
                    105:   In_ox=1;
                    106:   ox_push_cmo(sv,(cmo *)new_cmo_int32(i));
                    107:   In_ox=0;
                    108:   return i;
                    109: }
                    110:
                    111: int ml_execute_string(char *s) {
                    112:   if (sv == NULL) ml_start_asir();
                    113:   ox_restart();
                    114:   In_ox=1;
                    115:   ox_execute_string(sv,s);
                    116:   In_ox=0;
                    117:   return 0;
                    118: }
                    119:
                    120: int ml_pop_string0() {
                    121:   if (sv == NULL) ml_start_asir();
                    122:   ox_restart();
                    123:   In_ox = 1;
                    124:   send_ox_command(sv,SM_popString);
                    125:   return 1;
                    126: }
                    127: char *ml_get_string() {
                    128:   char *s;
                    129:   receive_ox_tag(sv); /* OX_DATA */
                    130:   s = ((cmo_string *) receive_cmo(sv))->s;
                    131:   In_ox = 0;
                    132:   return s;
                    133: }
                    134:
                    135: int ml_select() {
                    136:   return oxselect(sv);
                    137: }

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