summaryrefslogtreecommitdiffstats
path: root/usr.bin/cu/command.c
diff options
context:
space:
mode:
authornicm <nicm@openbsd.org>2012-07-10 08:02:27 +0000
committernicm <nicm@openbsd.org>2012-07-10 08:02:27 +0000
commitf391cec58bc34055add0062126a8915927e0ddca (patch)
tree347326a2d516b4f2a3f2e61463e3ba4b134c6bb8 /usr.bin/cu/command.c
parentfix the last outstanding functionality difference i could find between (diff)
downloadwireguard-openbsd-f391cec58bc34055add0062126a8915927e0ddca.tar.xz
wireguard-openbsd-f391cec58bc34055add0062126a8915927e0ddca.zip
Add first cut of replacement for tip/cu. Not linked to the build.
Currently supports only -l and -s (no parity), no variables and ~., ~>, ~$, ~#, ~^Z, ~?. More to come. Tested by naddy, otto. ok miod deraadt
Diffstat (limited to 'usr.bin/cu/command.c')
-rw-r--r--usr.bin/cu/command.c135
1 files changed, 135 insertions, 0 deletions
diff --git a/usr.bin/cu/command.c b/usr.bin/cu/command.c
new file mode 100644
index 00000000000..b63ccd08a46
--- /dev/null
+++ b/usr.bin/cu/command.c
@@ -0,0 +1,135 @@
+/* $OpenBSD: command.c,v 1.1 2012/07/10 08:02:27 nicm Exp $ */
+
+/*
+ * Copyright (c) 2012 Nicholas Marriott <nicm@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
+ * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
+ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/wait.h>
+
+#include <event.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <paths.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include "cu.h"
+
+void pipe_command(void);
+void send_file(void);
+
+void
+pipe_command(void)
+{
+ const char *cmd;
+ pid_t pid;
+ int fd;
+
+ cmd = get_input("Local command?");
+ if (cmd == NULL || *cmd == '\0')
+ return;
+
+ switch (pid = fork()) {
+ case -1:
+ err(1, "fork");
+ case 0:
+ fd = open(_PATH_DEVNULL, O_RDWR);
+ if (fd < 0 || dup2(fd, STDIN_FILENO) == -1)
+ _exit(1);
+ close(fd);
+
+ /* attach stdout to line */
+ if (dup2(line_fd, STDOUT_FILENO) == -1)
+ _exit(1);
+
+ if (closefrom(STDOUT_FILENO + 1) != 0)
+ _exit(1);
+
+ execl(_PATH_BSHELL, "sh", "-c", cmd, (void*)NULL);
+ _exit(1);
+ default:
+ while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
+ /* nothing */;
+ break;
+ }
+}
+
+void
+send_file(void)
+{
+ const char *file;
+ FILE *f;
+ char buf[BUFSIZ], *expanded;
+ size_t len;
+
+ file = get_input("Local file?");
+ if (file == NULL || *file == '\0')
+ return;
+
+ expanded = tilde_expand(file);
+ f = fopen(expanded, "r");
+ if (f == NULL) {
+ warn("%s", file);
+ return;
+ }
+
+ while (!feof(f) && !ferror(f)) {
+ len = fread(buf, 1, sizeof(buf), f);
+ if (len != 0)
+ bufferevent_write(line_ev, buf, len);
+ }
+
+ fclose(f);
+ free(expanded);
+}
+
+void
+do_command(char c)
+{
+ switch (c) {
+ case '.':
+ case '\004': /* ^D */
+ event_loopexit(NULL);
+ break;
+ case '\032': /* ^Z */
+ restore_termios();
+ kill(getpid(), SIGTSTP);
+ set_termios();
+ break;
+ case '$':
+ pipe_command();
+ break;
+ case '>':
+ send_file();
+ break;
+ case '#':
+ ioctl(line_fd, TIOCSBRK, NULL);
+ sleep(1);
+ ioctl(line_fd, TIOCCBRK, NULL);
+ break;
+ case '?':
+ printf("\r\n"
+ "~> send file to remote host\r\n"
+ "~$ pipe local command to remote host\r\n"
+ "~? get this summary\r\n"
+ "~# send break\r\n");
+ break;
+ }
+}