Annotation of OpenXM/src/oxc/oxc.c, Revision 1.13
1.1 ohara 1: /* -*- mode: C -*- */
1.13 ! ohara 2: /* $OpenXM: OpenXM/src/oxc/oxc.c,v 1.12 2003/05/07 04:00:30 ohara 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: {
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;
25: }
26:
27: int lf_oxc_open_main(char *cmd, short port)
28: {
1.7 ohara 29: pid_t pid;
30: if ((pid = fork()) == 0) {
1.1 ohara 31: oxf_connect_dup(remote_host, port);
1.12 ohara 32: ox_printf("oxc: oxc_open(%s, %d)\n", cmd, port);
1.1 ohara 33: execlp(cmd, cmd, NULL);
34: }
35: return pid; /* if error, pid == 0 */
36: }
37:
1.10 ohara 38: #define MAX_RETRY 2000
39:
1.1 ohara 40: OXFILE *connection()
41: {
1.10 ohara 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 {
1.12 ohara 48: ox_printf("oxc: cannot connect.\n");
1.10 ohara 49: return NULL;
50: }
1.1 ohara 51: }
1.10 ohara 52: oxf_confirm_server(oxfp, password);
53: oxf_determine_byteorder_server(oxfp);
1.1 ohara 54: return oxfp;
55: }
56:
1.11 ohara 57: __inline__
58: static char *sskip(char *s)
59: {
60: while (isspace(*s)) {
61: s++;
62: }
63: return s;
64: }
65:
66: __inline__
67: static char *wskip(char *s)
68: {
69: while (!isspace(*s) && *s != '\0') {
70: s++;
71: }
72: return s;
73: }
74:
75: static int wc(char *s)
76: {
77: int n = 0;
78: s = sskip(s);
79: while(*s != '\0') {
80: s = wskip(s);
81: s = sskip(s);
82: n++;
83: }
84: return n;
85: }
86:
87: static void word(char *str, int argc, char *argv[])
88: {
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;
98: }
99:
100: __inline__
101: static int arglen(char *args[])
102: {
103: int len;
104: for(len = 0; args[len] != NULL; len++) {
105: ;
106: }
107: return len;
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.6 ohara 113: char *e = getenv("OpenXM_XTERM");
1.11 ohara 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.4 ohara 130: static int basic0[] = {
1.6 ohara 131: CMO_ERROR2,
132: CMO_NULL,
133: CMO_INT32,
134: CMO_DATUM,
135: CMO_STRING,
136: CMO_MATHCAP,
137: CMO_LIST,
138: 0
1.5 ohara 139: };
140:
141: /* We assume that data has the following format:
142: LENGTH hostname '\0' port '\0' password '\0'
143: where LENGTH is an integer with network byte order and its value
144: equals to the sum of the length of three data above.
145: */
146:
147: void pipe_read_info(char **hostname, int *port, char **password);
1.4 ohara 148:
1.1 ohara 149: int main(int argc, char *argv[])
150: {
151: OXFILE *oxfp;
1.6 ohara 152: char *port_s = "";
153: char *myname = argv[0];
154: int oxlog = 0;
1.1 ohara 155: int c;
1.10 ohara 156: int delay = 0;
157: char *delay_s = "0";
1.12 ohara 158: int quiet = 0;
1.2 ohara 159:
1.12 ohara 160: while ((c = getopt(argc, argv, "c:d:h:p:qx")) != -1) {
1.1 ohara 161: switch(c) {
162: case 'h':
163: remote_host = optarg;
164: break;
165: case 'c':
166: password = optarg;
167: break;
168: case 'p':
169: port = atoi(optarg);
1.6 ohara 170: port_s = optarg;
1.2 ohara 171: break;
1.12 ohara 172: case 'q':
173: quiet = 1;
174: break;
1.6 ohara 175: case 'x':
176: if (getenv("DISPLAY") != NULL) {
177: oxlog = 1;
178: }
1.1 ohara 179: break;
1.10 ohara 180: case 'd':
181: delay_s = optarg;
182: delay = atoi(optarg);
183: break;
1.1 ohara 184: default:
1.13 ! ohara 185: ;
1.1 ohara 186: }
187: }
188: argc -= optind;
189: argv += optind;
190:
1.12 ohara 191: if (!quiet) {
192: ox_stderr_init(stderr);
193: }
194:
1.5 ohara 195: if (strlen(remote_host) == 0) {
1.6 ohara 196: pipe_read_info(&remote_host, &port, &password);
197: port_s = malloc(32);
198: sprintf(port_s, "%d", port);
199: }
200: if (oxlog) {
1.11 ohara 201: char *common_args[] = {"-e", myname, "-d", delay_s,
202: "-h", remote_host, "-p", port_s, "-c",
203: password, NULL};
204: char **args = makeargs(common_args);
205: execvp(args[0], args);
1.6 ohara 206: }
1.5 ohara 207:
1.12 ohara 208: ox_printf("start connection!\n");
1.10 ohara 209: usleep(delay);
210: if ((oxfp = connection()) != NULL) {
1.12 ohara 211: ox_printf("oxc: oxfp = %p, fd = %d\n", oxfp, oxfp->fd);
1.6 ohara 212: mathcap_init(20001006, "v2000.10.06", "oxc", basic0, NULL);
213: sm(oxfp);
214: }
1.1 ohara 215: return 0;
216: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>