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

Annotation of OpenXM/src/ox_toolkit/sample5.c, Revision 1.1

1.1     ! takayama    1: /* $OpenXM$
        !             2:  */
        !             3:
        !             4: /* oxf_select, ox_reset examples. See oxf.c, readme*.txt
        !             5:   See also OpenXM/include/ox/*.h as for ox stackmachine commands.
        !             6:
        !             7:  Example:
        !             8:    OpenXM_HOME etc should be properly set by OpenXM/rc/dot.bashrc
        !             9:    OX_XTERM_GEOMETRY=80x20+0+0
        !            10:    python2
        !            11: import ctypes
        !            12: lib = ctypes.cdll.LoadLibrary('./sample5.so');
        !            13: lib.submit_string_to_ox_asir.argtypes = (ctypes.c_char_p,)
        !            14: lib.submit_string_to_ox_asir.restype = ctypes.c_char_p
        !            15: lib.pop_string_from_ox_asir.restype = ctypes.c_char_p
        !            16: lib.myselect.restype = ctypes.c_int
        !            17: lib.reset_ox_asir.restype = ctypes.c_int
        !            18: lib.open_ox_asir()
        !            19: s = lib.submit_string_to_ox_asir('fctr(x^10-1)')
        !            20: lib.myselect()
        !            21: lib.pop_string_from_ox_asir()
        !            22: s = lib.submit_string_to_ox_asir('fctr(x^1000-y^1000)')
        !            23: lib.myselect()
        !            24: lib.reset_ox_asir()
        !            25:
        !            26: s = lib.submit_string_to_ox_asir('fctr(x^10-1)')
        !            27: lib.myselect()
        !            28: lib.pop_string_from_ox_asir()
        !            29:
        !            30: lib.close_ox_asir()
        !            31:  */
        !            32:
        !            33: /*
        !            34:   gcc -shared -fPIC -o sample5.so sample5.c -I${OpenXM_HOME}/include -L${OpenXM_HOME}/lib -lox -lgmp -lmpfr -lgc
        !            35: */
        !            36:
        !            37: #include <stdio.h>
        !            38: #include <stdlib.h>
        !            39: #include <unistd.h>
        !            40: #include <string.h>
        !            41: #include <errno.h>
        !            42: #include <fcntl.h>
        !            43:
        !            44: #include "ox_toolkit.h"
        !            45:
        !            46: extern OXFILE *ox_start(char* host, char* prog1, char* prog2);
        !            47: OXFILE *sv;
        !            48:
        !            49: #define SIZE_CMDLINE  8192
        !            50:
        !            51: static int  size = SIZE_CMDLINE;
        !            52: static char cmdline[SIZE_CMDLINE];
        !            53:
        !            54: int open_ox_asir()
        !            55: {
        !            56:   char *server = "ox_asir";
        !            57:   sv = ox_start("localhost", "ox", server);
        !            58:   if (sv == NULL) {
        !            59:     ox_printf("testclient:: I cannot connect to servers.\n");
        !            60:     exit(1);
        !            61:   }
        !            62:   printf("Input size is limited to %d bytes.\n",SIZE_CMDLINE-1);
        !            63:   ox_stderr_init(stderr);
        !            64:   return 0;
        !            65: }
        !            66:
        !            67: char *submit_string_to_ox_asir(char *cmdline)
        !            68: {
        !            69:   ox* m = NULL;
        !            70:   char cmd1[SIZE_CMDLINE+1024];
        !            71:   int code;
        !            72:
        !            73:   if (strlen(cmdline)>SIZE_CMDLINE-1) return "Error: too long command. Increase SIZE_CMDLINE";
        !            74:   strcpy(cmd1,"(OX_DATA,(CMO_STRING,\"if(1){");
        !            75:   strcat(cmd1,cmdline);
        !            76:   strcat(cmd1,";}else{};\"))\n");
        !            77:   m = ox_parse_lisp(cmd1);
        !            78:   // if (m == NULL) ; //  error, not implemented
        !            79:   send_ox(sv, m);
        !            80:   m = ox_parse_lisp("(OX_COMMAND,(SM_executeStringByLocalParser))\n");
        !            81:   send_ox(sv, m);
        !            82:   m = ox_parse_lisp("(OX_COMMAND,(SM_popString))\n");
        !            83:   send_ox(sv, m);
        !            84:   return NULL;
        !            85: }
        !            86: char *pop_string_from_ox_asir() {
        !            87:   ox* m = NULL;
        !            88:   cmo* c = NULL;
        !            89:   int code;
        !            90:   m = ox_parse_lisp("(OX_COMMAND,(SM_popString))\n");
        !            91:   code = ((ox_command *)m)->command;
        !            92:   if (code >= 1024) {
        !            93:     ; // error, not implemented
        !            94:   }else if (code == SM_popCMO || code == SM_popString) {
        !            95:     receive_ox_tag(sv);
        !            96:     c = receive_cmo(sv);
        !            97:     ox_printf("testclient:: cmo received.\n");
        !            98:     //print_cmo(c);
        !            99:     if (c->tag == CMO_STRING)  return ((cmo_string *)c)->s;
        !           100:     else ; // error
        !           101:   }
        !           102:   return NULL;
        !           103: }
        !           104:
        !           105: /*
        !           106:  It returns non-zero number when the data is ready.
        !           107:  It return 0 when the data is not ready.
        !           108: */
        !           109: int myselect() {
        !           110:   fd_set readfds;
        !           111:   struct timeval timeout;
        !           112:   FD_ZERO(&readfds);
        !           113:   FD_SET(sv->fd,&readfds);
        !           114:   timeout.tv_sec = 0;
        !           115:   timeout.tv_usec = (long) 1;
        !           116:   return select((sv->fd)+1,&readfds, NULL,NULL, &timeout);
        !           117: }
        !           118:
        !           119: int reset_ox_asir() {
        !           120:   ox_reset(sv);
        !           121:   return 0;
        !           122: }
        !           123:
        !           124: int close_ox_asir() {
        !           125:   ox_reset(sv);
        !           126:   ox_printf("The testclient resets.\n");
        !           127:   ox_close(sv);
        !           128:   ox_printf("The testclient halts.\n");
        !           129:   return(0);
        !           130: }
        !           131:

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