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

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

1.1       ohara       1: /* -*- mode: C -*- */
1.14    ! ohara       2: /* $OpenXM: OpenXM/src/oxc/oxc.c,v 1.13 2004/12/01 17:42:46 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.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: {
                    140:     OXFILE *oxfp;
1.6       ohara     141:     char *port_s = "";
                    142:     char *myname = argv[0];
                    143:     int oxlog = 0;
1.1       ohara     144:     int c;
1.10      ohara     145:     int delay = 0;
                    146:     char *delay_s = "0";
1.12      ohara     147:        int quiet = 0;
1.2       ohara     148:
1.12      ohara     149:     while ((c = getopt(argc, argv, "c:d:h:p:qx")) != -1) {
1.1       ohara     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);
1.6       ohara     159:             port_s = optarg;
1.2       ohara     160:             break;
1.12      ohara     161:         case 'q':
                    162:             quiet = 1;
                    163:             break;
1.6       ohara     164:         case 'x':
                    165:             if (getenv("DISPLAY") != NULL) {
                    166:                 oxlog = 1;
                    167:             }
1.1       ohara     168:             break;
1.10      ohara     169:         case 'd':
                    170:             delay_s = optarg;
                    171:             delay = atoi(optarg);
                    172:             break;
1.1       ohara     173:         default:
1.13      ohara     174:             ;
1.1       ohara     175:         }
                    176:     }
                    177:     argc -= optind;
                    178:     argv += optind;
                    179:
1.12      ohara     180:     if (!quiet) {
                    181:         ox_stderr_init(stderr);
                    182:        }
                    183:
1.5       ohara     184:     if (strlen(remote_host) == 0) {
1.6       ohara     185:         pipe_read_info(&remote_host, &port, &password);
                    186:         port_s = malloc(32);
                    187:         sprintf(port_s, "%d", port);
                    188:     }
                    189:     if (oxlog) {
1.11      ohara     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);
1.6       ohara     195:     }
1.5       ohara     196:
1.12      ohara     197:     ox_printf("start connection!\n");
1.10      ohara     198:     usleep(delay);
                    199:     if ((oxfp = connection()) != NULL) {
1.12      ohara     200:         ox_printf("oxc: oxfp = %p, fd = %d\n", oxfp, oxfp->fd);
1.14    ! ohara     201:         mathcap_init("v2000.10.06", "oxc");
1.6       ohara     202:         sm(oxfp);
                    203:     }
1.1       ohara     204:     return 0;
                    205: }

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