Annotation of OpenXM/src/ox_maple/simple2.c, Revision 1.1
1.1 ! takayama 1: /* -*- mode: C -*- */
! 2: /* $OpenXM$ */
! 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:
! 77: int ml_start_asir()
! 78: {
! 79: char *server = "ox_asir";
! 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: }
! 89:
! 90: int ml_push_int(int i) {
! 91: if (sv == NULL) ml_start_asir();
! 92: ox_restart();
! 93: In_ox=1;
! 94: ox_push_cmo(sv,(cmo *)new_cmo_int32(i));
! 95: In_ox=0;
! 96: return i;
! 97: }
! 98:
! 99: int ml_execute_string(char *s) {
! 100: if (sv == NULL) ml_start_asir();
! 101: ox_restart();
! 102: In_ox=1;
! 103: ox_execute_string(sv,s);
! 104: In_ox=0;
! 105: return 0;
! 106: }
! 107:
! 108: int ml_pop_string0() {
! 109: if (sv == NULL) ml_start_asir();
! 110: ox_restart();
! 111: In_ox = 1;
! 112: send_ox_command(sv,SM_popString);
! 113: return 1;
! 114: }
! 115: char *ml_get_string() {
! 116: char *s;
! 117: receive_ox_tag(sv); /* OX_DATA */
! 118: s = ((cmo_string *) receive_cmo(sv))->s;
! 119: In_ox = 0;
! 120: return s;
! 121: }
! 122:
! 123: int ml_select() {
! 124: return oxselect(sv);
! 125: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>