version 1.11, 2001/01/10 06:54:36 |
version 1.15.2.1, 2005/10/12 10:43:32 |
|
|
/* -*- mode: C -*- */ |
/* -*- mode: C -*- */ |
/* $OpenXM: OpenXM/src/oxc/oxc.c,v 1.10 2000/12/16 01:52:32 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> |
Line 17 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) |
{ |
{ |
pid_t pid; |
pid_t pid; |
if ((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); |
} |
} |
return pid; /* if error, pid == 0 */ |
return pid; /* if error, pid == 0 */ |
} |
} |
|
|
#define MAX_RETRY 2000 |
#define MAX_RETRY 2000 |
|
|
OXFILE *connection() |
OXFILE *connection() |
{ |
{ |
OXFILE *oxfp; |
OXFILE *oxfp; |
int counter = MAX_RETRY; |
int counter = MAX_RETRY; |
while((oxfp = oxf_connect_active(remote_host, port)) == NULL) { |
while((oxfp = oxf_connect_active(remote_host, port)) == NULL) { |
if (--counter > 0) { |
if (--counter > 0) { |
usleep(100); /* spends 100 micro seconds */ |
usleep(100); /* spends 100 micro seconds */ |
}else { |
}else { |
fprintf(stderr, "oxc: cannot connect.\n"); |
ox_printf("oxc: cannot connect.\n"); |
return NULL; |
return NULL; |
} |
|
} |
} |
oxf_confirm_server(oxfp, password); |
} |
oxf_determine_byteorder_server(oxfp); |
oxf_confirm_server(oxfp, password); |
return oxfp; |
oxf_determine_byteorder_server(oxfp); |
|
return oxfp; |
} |
} |
|
|
__inline__ |
__inline__ |
static char *sskip(char *s) |
static char *sskip(char *s) |
{ |
{ |
while (isspace(*s)) { |
while (isspace(*s)) { |
s++; |
s++; |
} |
} |
return s; |
return s; |
} |
} |
|
|
__inline__ |
__inline__ |
static char *wskip(char *s) |
static char *wskip(char *s) |
{ |
{ |
while (!isspace(*s) && *s != '\0') { |
while (!isspace(*s) && *s != '\0') { |
s++; |
s++; |
} |
} |
return s; |
return s; |
} |
} |
|
|
static int wc(char *s) |
static int wc(char *s) |
{ |
{ |
int n = 0; |
int n = 0; |
|
s = sskip(s); |
|
while(*s != '\0') { |
|
s = wskip(s); |
s = sskip(s); |
s = sskip(s); |
while(*s != '\0') { |
n++; |
s = wskip(s); |
} |
s = sskip(s); |
return n; |
n++; |
|
} |
|
return n; |
|
} |
} |
|
|
static void word(char *str, int argc, char *argv[]) |
static void word(char *str, int argc, char *argv[]) |
{ |
{ |
int i; |
int i; |
char *s = strcpy(malloc(strlen(str)+1), str); |
char *s = strcpy(malloc(strlen(str)+1), str); |
for(i=0; i<argc; i++) { |
for(i=0; i<argc; i++) { |
s = sskip(s); |
s = sskip(s); |
argv[i] = s; |
argv[i] = s; |
s = wskip(s); |
s = wskip(s); |
*s++ = '\0'; |
*s++ = '\0'; |
} |
} |
argv[i] = NULL; |
argv[i] = NULL; |
} |
} |
|
|
__inline__ |
__inline__ |
static int arglen(char *args[]) |
static int arglen(char *args[]) |
{ |
{ |
int len; |
int len; |
for(len = 0; args[len] != NULL; len++) { |
for(len = 0; args[len] != NULL; len++) { |
; |
; |
} |
} |
return len; |
return len; |
} |
} |
|
|
/* xterm, kterm, rxvt, gnome-terminal, ... */ |
/* xterm, kterm, rxvt, gnome-terminal, ... */ |
static char **makeargs(char **oldargs) |
static char **makeargs(char **oldargs) |
{ |
{ |
char *e = getenv("OpenXM_XTERM"); |
char *e = getenv("OpenXM_XTERM"); |
int argc; |
int argc; |
char **newargs; |
char **newargs; |
int len = arglen(oldargs); |
int len = arglen(oldargs); |
if (e == NULL) { |
if (e == NULL) { |
argc = 1; |
argc = 1; |
newargs = malloc((len+1+argc)*sizeof(char *)); |
newargs = malloc((len+1+argc)*sizeof(char *)); |
newargs[0] = "xterm"; |
newargs[0] = "xterm"; |
}else { |
}else { |
argc = wc(e); |
argc = wc(e); |
newargs = malloc((len+1+argc)*sizeof(char *)); |
newargs = malloc((len+1+argc)*sizeof(char *)); |
word(e, argc, newargs); |
word(e, argc, newargs); |
} |
} |
memcpy(newargs+argc, oldargs, (len+1)*sizeof(char *)); |
memcpy(newargs+argc, oldargs, (len+1)*sizeof(char *)); |
return newargs; |
return newargs; |
} |
} |
|
|
static int basic0[] = { |
|
CMO_ERROR2, |
|
CMO_NULL, |
|
CMO_INT32, |
|
CMO_DATUM, |
|
CMO_STRING, |
|
CMO_MATHCAP, |
|
CMO_LIST, |
|
0 |
|
}; |
|
|
|
/* We assume that data has the following format: |
/* We assume that data has the following format: |
LENGTH hostname '\0' port '\0' password '\0' |
LENGTH hostname '\0' port '\0' password '\0' |
where LENGTH is an integer with network byte order and its value |
where LENGTH is an integer with network byte order and its value |
Line 148 void pipe_read_info(char **hostname, int *port, char * |
|
Line 137 void pipe_read_info(char **hostname, int *port, char * |
|
|
|
int main(int argc, char *argv[]) |
int main(int argc, char *argv[]) |
{ |
{ |
OXFILE *oxfp; |
OXFILE *oxfp; |
char *port_s = ""; |
char *port_s = ""; |
char *myname = argv[0]; |
char *myname = argv[0]; |
int oxlog = 0; |
int oxlog = 0; |
int c; |
int c; |
int delay = 0; |
int delay = 0; |
char *delay_s = "0"; |
char *delay_s = "0"; |
|
int quiet = 0; |
|
|
while ((c = getopt(argc, argv, "d: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 'q': |
case 'x': |
quiet = 1; |
if (getenv("DISPLAY") != NULL) { |
break; |
oxlog = 1; |
case 'x': |
} |
if (getenv("DISPLAY") != NULL) { |
break; |
oxlog = 1; |
case 'd': |
} |
delay_s = optarg; |
break; |
delay = atoi(optarg); |
case 'd': |
break; |
delay_s = optarg; |
default: |
delay = atoi(optarg); |
} |
break; |
|
default: |
|
; |
} |
} |
argc -= optind; |
} |
argv += optind; |
argc -= optind; |
|
argv += optind; |
|
|
if (strlen(remote_host) == 0) { |
if (!quiet) { |
pipe_read_info(&remote_host, &port, &password); |
ox_stderr_init(stderr); |
port_s = malloc(32); |
} |
sprintf(port_s, "%d", port); |
|
} |
|
if (oxlog) { |
|
char *common_args[] = {"-e", myname, "-d", delay_s, |
|
"-h", remote_host, "-p", port_s, "-c", |
|
password, NULL}; |
|
char **args = makeargs(common_args); |
|
execvp(args[0], args); |
|
} |
|
|
|
fprintf(stderr, "start connection!\n"); |
if (strlen(remote_host) == 0) { |
usleep(delay); |
pipe_read_info(&remote_host, &port, &password); |
if ((oxfp = connection()) != NULL) { |
port_s = malloc(32); |
fprintf(stderr, "oxc: oxfp = %p, fd = %d\n", oxfp, oxfp->fd); |
sprintf(port_s, "%d", port); |
mathcap_init(20001006, "v2000.10.06", "oxc", basic0, NULL); |
} |
sm(oxfp); |
if (oxlog) { |
} |
char *common_args[] = {"-e", myname, "-d", delay_s, |
return 0; |
"-h", remote_host, "-p", port_s, "-c", |
|
password, NULL}; |
|
char **args = makeargs(common_args); |
|
execvp(args[0], args); |
|
} |
|
|
|
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; |
} |
} |