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

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

1.1       ohara       1: /* -*- mode: C -*- */
1.11    ! ohara       2: /* $OpenXM: OpenXM/src/oxc/oxc.c,v 1.10 2000/12/16 01:52:32 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);
                     32:         fprintf(stderr, "oxc: oxc_open(%s, %d)\n", cmd, port);
                     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 {
                     48:             fprintf(stderr, "oxc: cannot connect.\n");
                     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.2       ohara     158:
1.9       ohara     159:     while ((c = getopt(argc, argv, "d:c:p:h:x")) != -1) {
1.1       ohara     160:         switch(c) {
                    161:         case 'h':
                    162:             remote_host = optarg;
                    163:             break;
                    164:         case 'c':
                    165:             password = optarg;
1.6       ohara     166:
1.1       ohara     167:             break;
                    168:         case 'p':
                    169:             port = atoi(optarg);
1.6       ohara     170:             port_s = optarg;
1.2       ohara     171:             break;
1.6       ohara     172:         case 'x':
                    173:             if (getenv("DISPLAY") != NULL) {
                    174:                 oxlog = 1;
                    175:             }
1.1       ohara     176:             break;
1.10      ohara     177:         case 'd':
                    178:             delay_s = optarg;
                    179:             delay = atoi(optarg);
                    180:             break;
1.1       ohara     181:         default:
                    182:         }
                    183:     }
                    184:     argc -= optind;
                    185:     argv += optind;
                    186:
1.5       ohara     187:     if (strlen(remote_host) == 0) {
1.6       ohara     188:         pipe_read_info(&remote_host, &port, &password);
                    189:         port_s = malloc(32);
                    190:         sprintf(port_s, "%d", port);
                    191:     }
                    192:     if (oxlog) {
1.11    ! ohara     193:         char *common_args[] = {"-e", myname, "-d", delay_s,
        !           194:                           "-h", remote_host, "-p", port_s, "-c",
        !           195:                           password, NULL};
        !           196:         char **args = makeargs(common_args);
        !           197:         execvp(args[0], args);
1.6       ohara     198:     }
1.5       ohara     199:
1.1       ohara     200:     fprintf(stderr, "start connection!\n");
1.10      ohara     201:     usleep(delay);
                    202:     if ((oxfp = connection()) != NULL) {
1.6       ohara     203:         fprintf(stderr, "oxc: oxfp = %p, fd = %d\n", oxfp, oxfp->fd);
                    204:         mathcap_init(20001006, "v2000.10.06", "oxc", basic0, NULL);
                    205:         sm(oxfp);
                    206:     }
1.1       ohara     207:     return 0;
                    208: }

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