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

Diff for /OpenXM/src/oxc/oxc.c between version 1.2 and 1.15.2.1

version 1.2, 2000/10/13 07:39:10 version 1.15.2.1, 2005/10/12 10:43:32
Line 1 
Line 1 
 /* -*- mode: C -*- */  /* -*- mode: C -*- */
 /* $OpenXM: OpenXM/src/oxc/oxc.c,v 1.1 2000/10/13 06:05:12 ohara Exp $ */  /* $OpenXM: OpenXM/src/oxc/oxc.c,v 1.15 2005/10/12 10:37:24 takayama Exp $ */
   
 #include <stdio.h>  #include <stdio.h>
 #include <stdlib.h>  #include <stdlib.h>
 #include <unistd.h>  #include <unistd.h>
   #include <ctype.h>
 #include <string.h>  #include <string.h>
 #include "mysocket.h"  #include "mysocket.h"
 #include "ox_toolkit.h"  #include "ox_toolkit.h"
Line 16  static char *password = "";
Line 17  static char *password = "";
   
 int oxf_connect_dup(char *remote, short port)  int oxf_connect_dup(char *remote, short port)
 {  {
     int fd = mysocketOpen(remote, port);    int fd = mysocketOpen(remote, port);
     /* Here do we need to confirm? */    /* Here do we need to confirm? */
     dup2(fd, 3);    dup2(fd, 3);
     dup2(fd, 4); /* is it necessary? maybe fd == 4. */    dup2(fd, 4); /* is it necessary? maybe fd == 4. */
     return fd;    return fd;
 }  }
   
 int lf_oxc_open_main(char *cmd, short port)  int lf_oxc_open_main(char *cmd, short port)
 {  {
     int pid = 0;    pid_t pid;
     if (cmd != NULL && (pid = fork()) == 0) {    if ((pid = fork()) == 0) {
         oxf_connect_dup(remote_host, port);      oxf_connect_dup(remote_host, port);
         fprintf(stderr, "oxc: oxc_open(%s, %d)\n", cmd, port);      ox_printf("oxc: oxc_open(%s, %d)\n", cmd, port);
         execlp(cmd, cmd, NULL);      execlp(cmd, cmd, NULL);
     }    }
     fprintf(stderr, "oxc: cannnot oxc_open(%s, %d).\n", cmd, port);    return pid; /* if error, pid == 0 */
     return pid; /* if error, pid == 0 */  
 }  }
   
   #define MAX_RETRY  2000
   
 OXFILE *connection()  OXFILE *connection()
 {  {
     OXFILE *oxfp = oxf_connect_active(remote_host, port);    OXFILE *oxfp;
     if (oxfp != NULL) {    int counter = MAX_RETRY;
         oxf_confirm_server(oxfp, password);    while((oxfp = oxf_connect_active(remote_host, port)) == NULL) {
         oxf_determine_byteorder_server(oxfp);      if (--counter > 0) {
         usleep(100); /* spends 100 micro seconds */
       }else {
         ox_printf("oxc: cannot connect.\n");
         return NULL;
     }      }
     return oxfp;    }
     oxf_confirm_server(oxfp, password);
     oxf_determine_byteorder_server(oxfp);
     return oxfp;
 }  }
   
   __inline__
   static char *sskip(char *s)
   {
     while (isspace(*s)) {
       s++;
     }
     return s;
   }
   
   __inline__
   static char *wskip(char *s)
   {
     while (!isspace(*s) && *s != '\0') {
       s++;
     }
     return s;
   }
   
   static int wc(char *s)
   {
     int n = 0;
     s = sskip(s);
     while(*s != '\0') {
       s = wskip(s);
       s = sskip(s);
       n++;
     }
     return n;
   }
   
   static void word(char *str, int argc, char *argv[])
   {
     int i;
     char *s = strcpy(malloc(strlen(str)+1), str);
     for(i=0; i<argc; i++) {
       s = sskip(s);
       argv[i] = s;
       s = wskip(s);
       *s++ = '\0';
     }
     argv[i] = NULL;
   }
   
   __inline__
   static int arglen(char *args[])
   {
     int len;
     for(len = 0; args[len] != NULL; len++) {
       ;
     }
     return len;
   }
   
 /* xterm, kterm, rxvt, gnome-terminal, ... */  /* xterm, kterm, rxvt, gnome-terminal, ... */
 static char *xterminal()  static char **makeargs(char **oldargs)
 {  {
         char *e = getenv("OpenXM_XTERM");    char *e = getenv("OpenXM_XTERM");
         return (e != NULL)? e: "xterm";    int argc;
     char **newargs;
     int len = arglen(oldargs);
     if (e == NULL) {
       argc = 1;
       newargs = malloc((len+1+argc)*sizeof(char *));
       newargs[0] = "xterm";
     }else {
       argc = wc(e);
       newargs = malloc((len+1+argc)*sizeof(char *));
       word(e, argc, newargs);
     }
     memcpy(newargs+argc, oldargs, (len+1)*sizeof(char *));
     return newargs;
 }  }
   
   /* We assume that data has the following format:
      LENGTH hostname '\0' port '\0' password '\0'
      where LENGTH is an integer with network byte order and its value
      equals to the sum of the length of three data above.
   */
   
   void pipe_read_info(char **hostname, int *port, char **password);
   
 int main(int argc, char *argv[])  int main(int argc, char *argv[])
 {  {
     OXFILE *oxfp;    OXFILE *oxfp;
         char *port_s = "";    char *port_s = "";
         char *xterm =  xterminal();    char *myname = argv[0];
         char *myname = argv[0];    int oxlog = 0;
         int oxlog = 0;    int c;
     int c;    int delay = 0;
     char *delay_s = "0";
     int quiet = 0;
   
     while ((c = getopt(argc, argv, "c:p:h:x")) != -1) {    while ((c = getopt(argc, argv, "c:d:h:p:qx")) != -1) {
         switch(c) {      switch(c) {
         case 'h':      case 'h':
             remote_host = optarg;        remote_host = optarg;
             break;        break;
         case 'c':      case 'c':
             password = optarg;        password = optarg;
             break;        break;
         case 'p':      case 'p':
             port = atoi(optarg);        port = atoi(optarg);
                         port_s = optarg;        port_s = optarg;
             break;        break;
                 case 'x':      case 'q':
                         if (getenv("DISPLAY") != NULL) {        quiet = 1;
                                 oxlog = 1;        break;
                         }      case 'x':
             break;        if (getenv("DISPLAY") != NULL) {
         default:          oxlog = 1;
         }        }
         break;
       case 'd':
         delay_s = optarg;
         delay = atoi(optarg);
         break;
       default:
         ;
     }      }
     argc -= optind;    }
     argv += optind;    argc -= optind;
     argv += optind;
   
         if (oxlog) {    if (!quiet) {
                 execlp(xterm, xterm, "-e", myname,      ox_stderr_init(stderr);
                            "-h", remote_host, "-p", port_s, "-c", password);    }
         }  
     fprintf(stderr, "start connection!\n");    if (strlen(remote_host) == 0) {
     if (strlen(remote_host) == 0 || strlen(password) == 0 || port == 0) {      pipe_read_info(&remote_host, &port, &password);
         fprintf(stderr, "oxc: invalid arguments.\n");      port_s = malloc(32);
                 fprintf(stderr, "usage: oxc -p port -h host -c password.\n");      sprintf(port_s, "%d", port);
     }else if ((oxfp = connection()) == NULL) {    }
         fprintf(stderr, "oxc: cannot connect.\n");    if (oxlog) {
     }else {      char *common_args[] = {"-e", myname, "-d", delay_s,
                 fprintf(stderr, "oxc: oxfp = %p, fd = %d\n", oxfp, oxfp->fd);                             "-h", remote_host, "-p", port_s, "-c",
                 mathcap_sysinfo_set(20001006, "v2000.10.06", "oxc");                             password, NULL};
                 sm(oxfp);      char **args = makeargs(common_args);
         }      execvp(args[0], args);
     return 0;    }
   
     ox_printf("start connection!\n");
     usleep(delay);
     if ((oxfp = connection()) != NULL) {
       ox_printf("oxc: oxfp = %p, fd = %d\n", oxfp, oxfp->fd);
       mathcap_init("v2000.10.06", "oxc");
       sm(oxfp);
     }
     return 0;
 }  }

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.15.2.1

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