version 1.2, 2000/10/13 07:39:10 |
version 1.10, 2000/12/16 01:52:32 |
|
|
/* -*- 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.9 2000/12/15 03:34:43 ohara Exp $ */ |
|
|
#include <stdio.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <stdlib.h> |
Line 25 int oxf_connect_dup(char *remote, short port) |
|
Line 25 int oxf_connect_dup(char *remote, short port) |
|
|
|
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); |
fprintf(stderr, "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 { |
|
fprintf(stderr, "oxc: cannot connect.\n"); |
|
return NULL; |
|
} |
} |
} |
|
oxf_confirm_server(oxfp, password); |
|
oxf_determine_byteorder_server(oxfp); |
return oxfp; |
return oxfp; |
} |
} |
|
|
/* xterm, kterm, rxvt, gnome-terminal, ... */ |
/* xterm, kterm, rxvt, gnome-terminal, ... */ |
static char *xterminal() |
static char *xterminal() |
{ |
{ |
char *e = getenv("OpenXM_XTERM"); |
char *e = getenv("OpenXM_XTERM"); |
return (e != NULL)? e: "xterm"; |
return (e != NULL)? e: "xterm"; |
} |
} |
|
|
|
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: |
|
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 *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"; |
|
|
while ((c = getopt(argc, argv, "c:p:h:x")) != -1) { |
while ((c = getopt(argc, argv, "d:c:p:h:x")) != -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 'x': |
if (getenv("DISPLAY") != NULL) { |
if (getenv("DISPLAY") != NULL) { |
oxlog = 1; |
oxlog = 1; |
} |
} |
break; |
break; |
|
case 'd': |
|
delay_s = optarg; |
|
delay = atoi(optarg); |
|
break; |
default: |
default: |
} |
} |
} |
} |
argc -= optind; |
argc -= optind; |
argv += optind; |
argv += optind; |
|
|
if (oxlog) { |
if (strlen(remote_host) == 0) { |
execlp(xterm, xterm, "-e", myname, |
pipe_read_info(&remote_host, &port, &password); |
"-h", remote_host, "-p", port_s, "-c", password); |
port_s = malloc(32); |
} |
sprintf(port_s, "%d", port); |
|
} |
|
if (oxlog) { |
|
execlp(xterm, xterm, "-e", myname, "-d", delay_s, |
|
"-h", remote_host, "-p", port_s, "-c", password, NULL); |
|
} |
|
|
fprintf(stderr, "start connection!\n"); |
fprintf(stderr, "start connection!\n"); |
if (strlen(remote_host) == 0 || strlen(password) == 0 || port == 0) { |
usleep(delay); |
fprintf(stderr, "oxc: invalid arguments.\n"); |
if ((oxfp = connection()) != NULL) { |
fprintf(stderr, "usage: oxc -p port -h host -c password.\n"); |
fprintf(stderr, "oxc: oxfp = %p, fd = %d\n", oxfp, oxfp->fd); |
}else if ((oxfp = connection()) == NULL) { |
mathcap_init(20001006, "v2000.10.06", "oxc", basic0, NULL); |
fprintf(stderr, "oxc: cannot connect.\n"); |
sm(oxfp); |
}else { |
} |
fprintf(stderr, "oxc: oxfp = %p, fd = %d\n", oxfp, oxfp->fd); |
|
mathcap_sysinfo_set(20001006, "v2000.10.06", "oxc"); |
|
sm(oxfp); |
|
} |
|
return 0; |
return 0; |
} |
} |