[BACK]Return to command.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cu

Annotation of src/usr.bin/cu/command.c, Revision 1.3

1.3     ! nicm        1: /* $OpenBSD: command.c,v 1.2 2012/07/10 08:16:27 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2012 Nicholas Marriott <nicm@openbsd.org>
                      5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
                     15:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                     16:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
                     19: #include <sys/types.h>
                     20: #include <sys/ioctl.h>
                     21: #include <sys/wait.h>
                     22:
                     23: #include <event.h>
                     24: #include <err.h>
                     25: #include <errno.h>
                     26: #include <fcntl.h>
                     27: #include <paths.h>
                     28: #include <signal.h>
                     29: #include <stdlib.h>
                     30: #include <stdio.h>
                     31: #include <unistd.h>
                     32:
                     33: #include "cu.h"
                     34:
                     35: void   pipe_command(void);
                     36: void   send_file(void);
                     37:
                     38: void
                     39: pipe_command(void)
                     40: {
                     41:        const char      *cmd;
                     42:        pid_t            pid;
                     43:        int              fd;
                     44:
                     45:        cmd = get_input("Local command?");
                     46:        if (cmd == NULL || *cmd == '\0')
                     47:                return;
                     48:
1.3     ! nicm       49:        restore_termios();
        !            50:
1.1       nicm       51:        switch (pid = fork()) {
                     52:        case -1:
                     53:                err(1, "fork");
                     54:        case 0:
                     55:                fd = open(_PATH_DEVNULL, O_RDWR);
                     56:                if (fd < 0 || dup2(fd, STDIN_FILENO) == -1)
                     57:                        _exit(1);
                     58:                close(fd);
                     59:
1.3     ! nicm       60:                if (signal(SIGINT, SIG_DFL) == SIG_ERR)
        !            61:                        _exit(1);
        !            62:                if (signal(SIGQUIT, SIG_DFL) == SIG_ERR)
        !            63:                        _exit(1);
        !            64:
1.1       nicm       65:                /* attach stdout to line */
                     66:                if (dup2(line_fd, STDOUT_FILENO) == -1)
                     67:                        _exit(1);
                     68:
                     69:                if (closefrom(STDOUT_FILENO + 1) != 0)
                     70:                        _exit(1);
                     71:
                     72:                execl(_PATH_BSHELL, "sh", "-c", cmd, (void*)NULL);
                     73:                _exit(1);
                     74:        default:
                     75:                while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
                     76:                        /* nothing */;
                     77:                break;
                     78:        }
1.3     ! nicm       79:
        !            80:        set_termios();
1.1       nicm       81: }
                     82:
                     83: void
                     84: send_file(void)
                     85: {
                     86:        const char      *file;
                     87:        FILE            *f;
                     88:        char             buf[BUFSIZ], *expanded;
                     89:        size_t           len;
                     90:
                     91:        file = get_input("Local file?");
                     92:        if (file == NULL || *file == '\0')
                     93:                return;
                     94:
                     95:        expanded = tilde_expand(file);
                     96:        f = fopen(expanded, "r");
                     97:        if (f == NULL) {
                     98:                warn("%s", file);
                     99:                return;
                    100:        }
                    101:
                    102:        while (!feof(f) && !ferror(f)) {
                    103:                len = fread(buf, 1, sizeof(buf), f);
                    104:                if (len != 0)
                    105:                        bufferevent_write(line_ev, buf, len);
                    106:        }
                    107:
                    108:        fclose(f);
                    109:        free(expanded);
                    110: }
                    111:
                    112: void
1.2       nicm      113: set_speed(void)
                    114: {
                    115:        const char      *s, *errstr;
                    116:        int              speed;
                    117:
                    118:        s = get_input("New speed?");
                    119:        if (s == NULL || *s == '\0')
                    120:                return;
                    121:
                    122:        speed = strtonum(s, 0, UINT_MAX, &errstr);
                    123:        if (errstr != NULL) {
                    124:                warnx("speed is %s: %s", errstr, s);
                    125:                return;
                    126:        }
                    127:
                    128:        set_line(speed);
                    129: }
                    130:
                    131: void
1.1       nicm      132: do_command(char c)
                    133: {
                    134:        switch (c) {
                    135:        case '.':
                    136:        case '\004': /* ^D */
                    137:                event_loopexit(NULL);
                    138:                break;
                    139:        case '\032': /* ^Z */
                    140:                restore_termios();
                    141:                kill(getpid(), SIGTSTP);
                    142:                set_termios();
                    143:                break;
1.2       nicm      144:        case 'S':
                    145:                set_speed();
                    146:                break;
1.1       nicm      147:        case '$':
                    148:                pipe_command();
                    149:                break;
                    150:        case '>':
                    151:                send_file();
                    152:                break;
                    153:        case '#':
                    154:                ioctl(line_fd, TIOCSBRK, NULL);
                    155:                sleep(1);
                    156:                ioctl(line_fd, TIOCCBRK, NULL);
                    157:                break;
                    158:        case '?':
                    159:                printf("\r\n"
1.2       nicm      160:                    "~#      send break\r\n"
                    161:                    "~$      pipe local command to remote host\r\n"
1.1       nicm      162:                    "~>      send file to remote host\r\n"
1.2       nicm      163:                    "~S      set speed\r\n"
1.1       nicm      164:                    "~?      get this summary\r\n"
1.2       nicm      165:                );
1.1       nicm      166:                break;
                    167:        }
                    168: }