#include #include #include #include #include #include #include #include #include "/usr/local/xledir/include/chart-typedefs.h" #include "/usr/local/xledir/include/regexp.h" #define SHELL "/bin/sh" typedef struct socket_state { int sockfd; int pid; char buffer[100001]; } socket_state; void error(char *msg) { perror(msg); } char *analyze(char *input, socket_state *state) { strcpy((*state).buffer,input); write((*state).sockfd,"tokenize ", 9); int n = write((*state).sockfd,(*state).buffer,strlen((*state).buffer)); if (strlen((*state).buffer) < 1) return ""; write((*state).sockfd,"\n",1); if (n < 0) error("ERROR writing to socket"); bzero((*state).buffer,100001); n = read((*state).sockfd,(*state).buffer,100000); if (n < 0) error("ERROR reading from socket"); // printf("%s\n",(*state).buffer); return (*state).buffer; }; char *character_encoding(void *state) { // return "iso8859-1"; return "utf-8"; }; void finalize(socket_state *state) { //printf("\nkilling %s\n", (*state).pid + 1); // ??? // to do: free socket memory! //kill((*state).pid + 1, 9); } socket_state *initialize() { int portno; struct socket_state *state = (socket_state *)malloc(sizeof(socket_state)); char *morphportstr; char *logonroot; char token_filename[64]; char server_filename[64]; FILE *tmpfile; // start the server // printf("morph-port: %s\n", getenv("MORPHPORT")); morphportstr = getenv("MORPHPORT"); if (morphportstr) { portno = atoi(morphportstr); make_connection(portno, state); sprintf(token_filename, "/tmp/cgp.socket.%d.%d", getuid(), getpid()); // printf("\nmorph: %s\n",token_filename); tmpfile = fopen(token_filename, "w"); fprintf(tmpfile, morphportstr); // fgets(portstr, 8, tmpfile); fclose(tmpfile); return state; } else { int pid; logonroot = getenv("LOGONROOT"); if (logonroot == NULL) { logonroot = "~/logon";}; // printf("logonroot: %s\n", logonroot); #if defined(__APPLE__) && defined(__ppc__) sprintf(server_filename, "%s/uib/bin/macos.ppc.32/morph-server/morph-server", logonroot); #else sprintf(server_filename, "%s/uib/bin/linux.x86.64/morph-server/morph-server", logonroot); #endif if ((pid = fork()) < 0) { perror("fork"); exit(1); } if (pid == 0) { printf("starting %s\n", server_filename); // system("/home/paul/logon/flat/cgp/linux.x86.32/morph-server/morph-server"); // (*state).pid = getpid(); system(server_filename); // start morph-server // execl(SHELL, SHELL, "-c", server_filename, NULL); _exit(EXIT_FAILURE); } else { //system("/home/paul/lisp/projects/xle/morph/clisp -K full -M /home/paul/lisp/projects/xle/morph/morph-server.mem &"); // system("clisp -K full -M server.mem &"); sleep(1); //printf("Now are we ready.\n"); // establish socket connection // read portno from /tmp/cgp.socket.. char portstr[8]; char morph_filename[64]; sprintf(token_filename, "/tmp/cgp.socket.%d.%d", getuid(), pid); sprintf(morph_filename, "/tmp/cgp.socket.%d.%d", getuid(), getpid()); (*state).pid = pid; // printf("\nmorph-pid: %d\n", (*state).pid); // printf("\nt: token: %s\n",token_filename); // printf("\nt: morph: %s\n",morph_filename); sleep(1); tmpfile = fopen(token_filename, "r"); fgets(portstr, 8, tmpfile); printf("port: %s\n", portstr); fclose(tmpfile); rename(token_filename,morph_filename); portno = atoi(portstr); // portno = 2001; printf("\nmake_connection..."); make_connection(portno, state); printf(" done."); }; return state; }}; int make_connection(int portno, socket_state *state) { struct sockaddr_in serv_addr; struct hostent *server; (*state).sockfd = socket(AF_INET, SOCK_STREAM, 0); if ((*state).sockfd < 0) error("ERROR opening socket"); server = gethostbyname("localhost"); if (server == NULL) { fprintf(stderr,"ERROR, no such host\n"); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(portno); if (connect((*state).sockfd,&serv_addr,sizeof(serv_addr)) < 0) error("ERROR connecting"); // send pid sprintf((*state).buffer, "%d\n", getpid()); write((*state).sockfd,(*state).buffer,strlen((*state).buffer)); bzero((*state).buffer,100001); init_xle(0,0); initialize_cfsm(); }