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

Annotation of OpenXM/src/oxc/oxc.c, Revision 1.15.2.1

1.1       ohara       1: /* -*- mode: C -*- */
1.15.2.1! takayama    2: /* $OpenXM: OpenXM/src/oxc/oxc.c,v 1.15 2005/10/12 10:37:24 takayama Exp $ */
1.1       ohara       3:
                      4: #include <stdio.h>
                      5: #include <stdlib.h>
                      6: #include <unistd.h>
1.11      ohara       7: #include <ctype.h>
1.1       ohara       8: #include <string.h>
                      9: #include "mysocket.h"
                     10: #include "ox_toolkit.h"
                     11: #include "sm.h"
                     12:
                     13: /* oxc -c password -p port -h hostname */
                     14: static char *remote_host = "";
                     15: static short port = 0;
                     16: static char *password = "";
                     17:
                     18: int oxf_connect_dup(char *remote, short port)
                     19: {
1.15.2.1! takayama   20:   int fd = mysocketOpen(remote, port);
        !            21:   /* Here do we need to confirm? */
        !            22:   dup2(fd, 3);
        !            23:   dup2(fd, 4); /* is it necessary? maybe fd == 4. */
        !            24:   return fd;
1.1       ohara      25: }
                     26:
                     27: int lf_oxc_open_main(char *cmd, short port)
                     28: {
1.15.2.1! takayama   29:   pid_t pid;
        !            30:   if ((pid = fork()) == 0) {
        !            31:     oxf_connect_dup(remote_host, port);
        !            32:     ox_printf("oxc: oxc_open(%s, %d)\n", cmd, port);
        !            33:     execlp(cmd, cmd, NULL);
        !            34:   }
        !            35:   return pid; /* if error, pid == 0 */
1.1       ohara      36: }
                     37:
1.10      ohara      38: #define MAX_RETRY  2000
                     39:
1.1       ohara      40: OXFILE *connection()
                     41: {
1.15.2.1! takayama   42:   OXFILE *oxfp;
        !            43:   int counter = MAX_RETRY;
        !            44:   while((oxfp = oxf_connect_active(remote_host, port)) == NULL) {
        !            45:     if (--counter > 0) {
        !            46:       usleep(100); /* spends 100 micro seconds */
        !            47:     }else {
        !            48:       ox_printf("oxc: cannot connect.\n");
        !            49:       return NULL;
1.1       ohara      50:     }
1.15.2.1! takayama   51:   }
        !            52:   oxf_confirm_server(oxfp, password);
        !            53:   oxf_determine_byteorder_server(oxfp);
        !            54:   return oxfp;
1.1       ohara      55: }
                     56:
1.11      ohara      57: __inline__
                     58: static char *sskip(char *s)
                     59: {
1.15.2.1! takayama   60:   while (isspace(*s)) {
        !            61:     s++;
        !            62:   }
        !            63:   return s;
1.11      ohara      64: }
                     65:
                     66: __inline__
                     67: static char *wskip(char *s)
                     68: {
1.15.2.1! takayama   69:   while (!isspace(*s) && *s != '\0') {
        !            70:     s++;
        !            71:   }
        !            72:   return s;
1.11      ohara      73: }
                     74:
                     75: static int wc(char *s)
                     76: {
1.15.2.1! takayama   77:   int n = 0;
        !            78:   s = sskip(s);
        !            79:   while(*s != '\0') {
        !            80:     s = wskip(s);
1.11      ohara      81:     s = sskip(s);
1.15.2.1! takayama   82:     n++;
        !            83:   }
        !            84:   return n;
1.11      ohara      85: }
                     86:
                     87: static void word(char *str, int argc, char *argv[])
                     88: {
1.15.2.1! takayama   89:   int i;
        !            90:   char *s = strcpy(malloc(strlen(str)+1), str);
        !            91:   for(i=0; i<argc; i++) {
        !            92:     s = sskip(s);
        !            93:     argv[i] = s;
        !            94:     s = wskip(s);
        !            95:     *s++ = '\0';
        !            96:   }
        !            97:   argv[i] = NULL;
1.11      ohara      98: }
                     99:
                    100: __inline__
                    101: static int arglen(char *args[])
                    102: {
1.15.2.1! takayama  103:   int len;
        !           104:   for(len = 0; args[len] != NULL; len++) {
        !           105:     ;
        !           106:   }
        !           107:   return len;
1.11      ohara     108: }
                    109:
1.2       ohara     110: /* xterm, kterm, rxvt, gnome-terminal, ... */
1.11      ohara     111: static char **makeargs(char **oldargs)
1.2       ohara     112: {
1.15.2.1! takayama  113:   char *e = getenv("OpenXM_XTERM");
        !           114:   int argc;
        !           115:   char **newargs;
        !           116:   int len = arglen(oldargs);
        !           117:   if (e == NULL) {
        !           118:     argc = 1;
        !           119:     newargs = malloc((len+1+argc)*sizeof(char *));
        !           120:     newargs[0] = "xterm";
        !           121:   }else {
        !           122:     argc = wc(e);
        !           123:     newargs = malloc((len+1+argc)*sizeof(char *));
        !           124:     word(e, argc, newargs);
        !           125:   }
        !           126:   memcpy(newargs+argc, oldargs, (len+1)*sizeof(char *));
        !           127:   return newargs;
1.2       ohara     128: }
                    129:
1.5       ohara     130: /* We assume that data has the following format:
                    131:    LENGTH hostname '\0' port '\0' password '\0'
                    132:    where LENGTH is an integer with network byte order and its value
                    133:    equals to the sum of the length of three data above.
                    134: */
                    135:
                    136: void pipe_read_info(char **hostname, int *port, char **password);
1.4       ohara     137:
1.1       ohara     138: int main(int argc, char *argv[])
                    139: {
1.15.2.1! takayama  140:   OXFILE *oxfp;
        !           141:   char *port_s = "";
        !           142:   char *myname = argv[0];
        !           143:   int oxlog = 0;
        !           144:   int c;
        !           145:   int delay = 0;
        !           146:   char *delay_s = "0";
        !           147:   int quiet = 0;
        !           148:
        !           149:   while ((c = getopt(argc, argv, "c:d:h:p:qx")) != -1) {
        !           150:     switch(c) {
        !           151:     case 'h':
        !           152:       remote_host = optarg;
        !           153:       break;
        !           154:     case 'c':
        !           155:       password = optarg;
        !           156:       break;
        !           157:     case 'p':
        !           158:       port = atoi(optarg);
        !           159:       port_s = optarg;
        !           160:       break;
        !           161:     case 'q':
        !           162:       quiet = 1;
        !           163:       break;
        !           164:     case 'x':
        !           165:       if (getenv("DISPLAY") != NULL) {
        !           166:         oxlog = 1;
        !           167:       }
        !           168:       break;
        !           169:     case 'd':
        !           170:       delay_s = optarg;
        !           171:       delay = atoi(optarg);
        !           172:       break;
        !           173:     default:
        !           174:       ;
        !           175:     }
        !           176:   }
        !           177:   argc -= optind;
        !           178:   argv += optind;
        !           179:
        !           180:   if (!quiet) {
        !           181:     ox_stderr_init(stderr);
        !           182:   }
        !           183:
        !           184:   if (strlen(remote_host) == 0) {
        !           185:     pipe_read_info(&remote_host, &port, &password);
        !           186:     port_s = malloc(32);
        !           187:     sprintf(port_s, "%d", port);
        !           188:   }
        !           189:   if (oxlog) {
        !           190:     char *common_args[] = {"-e", myname, "-d", delay_s,
        !           191:                            "-h", remote_host, "-p", port_s, "-c",
        !           192:                            password, NULL};
        !           193:     char **args = makeargs(common_args);
        !           194:     execvp(args[0], args);
        !           195:   }
        !           196:
        !           197:   ox_printf("start connection!\n");
        !           198:   usleep(delay);
        !           199:   if ((oxfp = connection()) != NULL) {
        !           200:     ox_printf("oxc: oxfp = %p, fd = %d\n", oxfp, oxfp->fd);
        !           201:     mathcap_init("v2000.10.06", "oxc");
        !           202:     sm(oxfp);
        !           203:   }
        !           204:   return 0;
1.1       ohara     205: }

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