summaryrefslogtreecommitdiff
path: root/usr.bin/tmux/cmd.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin/tmux/cmd.c')
-rw-r--r--usr.bin/tmux/cmd.c142
1 files changed, 59 insertions, 83 deletions
diff --git a/usr.bin/tmux/cmd.c b/usr.bin/tmux/cmd.c
index 15c6f0d232f..c63d2e01a19 100644
--- a/usr.bin/tmux/cmd.c
+++ b/usr.bin/tmux/cmd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: cmd.c,v 1.9 2009/07/24 18:46:40 nicm Exp $ */
+/* $OpenBSD: cmd.c,v 1.10 2009/07/26 12:58:44 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -109,6 +109,64 @@ struct session *cmd_lookup_session(const char *, int *);
struct winlink *cmd_lookup_window(struct session *, const char *, int *);
int cmd_lookup_index(struct session *, const char *, int *);
+int
+cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
+{
+ size_t arglen;
+ int i;
+
+ *buf = '\0';
+ for (i = 0; i < argc; i++) {
+ if (strlcpy(buf, argv[i], len) >= len)
+ return (-1);
+ arglen = strlen(argv[i]) + 1;
+ buf += arglen;
+ len -= arglen;
+ }
+
+ return (0);
+}
+
+int
+cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
+{
+ int i;
+ size_t arglen;
+
+ if (argc == 0)
+ return (0);
+ *argv = xcalloc(argc, sizeof **argv);
+
+ buf[len - 1] = '\0';
+ for (i = 0; i < argc; i++) {
+ if (len == 0) {
+ cmd_free_argv(argc, *argv);
+ return (-1);
+ }
+
+ arglen = strlen(buf) + 1;
+ (*argv)[i] = xstrdup(buf);
+ buf += arglen;
+ len -= arglen;
+ }
+
+ return (0);
+}
+
+void
+cmd_free_argv(int argc, char **argv)
+{
+ int i;
+
+ if (argc == 0)
+ return;
+ for (i = 0; i < argc; i++) {
+ if (argv[i] != NULL)
+ xfree(argv[i]);
+ }
+ xfree(argv);
+}
+
struct cmd *
cmd_parse(int argc, char **argv, char **cause)
{
@@ -205,53 +263,6 @@ cmd_exec(struct cmd *cmd, struct cmd_ctx *ctx)
}
void
-cmd_send(struct cmd *cmd, struct buffer *b)
-{
- const struct cmd_entry **entryp;
- u_int n;
-
- n = 0;
- for (entryp = cmd_table; *entryp != NULL; entryp++) {
- if (*entryp == cmd->entry)
- break;
- n++;
- }
- if (*entryp == NULL)
- fatalx("command not found");
-
- buffer_write(b, &n, sizeof n);
-
- if (cmd->entry->send != NULL)
- cmd->entry->send(cmd, b);
-}
-
-struct cmd *
-cmd_recv(struct buffer *b)
-{
- const struct cmd_entry **entryp;
- struct cmd *cmd;
- u_int m, n;
-
- buffer_read(b, &m, sizeof m);
-
- n = 0;
- for (entryp = cmd_table; *entryp != NULL; entryp++) {
- if (n == m)
- break;
- n++;
- }
- if (*entryp == NULL)
- fatalx("command not found");
-
- cmd = xmalloc(sizeof *cmd);
- cmd->entry = *entryp;
-
- if (cmd->entry->recv != NULL)
- cmd->entry->recv(cmd, b);
- return (cmd);
-}
-
-void
cmd_free(struct cmd *cmd)
{
if (cmd->data != NULL && cmd->entry->free != NULL)
@@ -268,41 +279,6 @@ cmd_print(struct cmd *cmd, char *buf, size_t len)
return (cmd->entry->print(cmd, buf, len));
}
-void
-cmd_send_string(struct buffer *b, const char *s)
-{
- size_t n;
-
- if (s == NULL) {
- n = 0;
- buffer_write(b, &n, sizeof n);
- return;
- }
-
- n = strlen(s) + 1;
- buffer_write(b, &n, sizeof n);
-
- buffer_write(b, s, n);
-}
-
-char *
-cmd_recv_string(struct buffer *b)
-{
- char *s;
- size_t n;
-
- buffer_read(b, &n, sizeof n);
-
- if (n == 0)
- return (NULL);
-
- s = xmalloc(n);
- buffer_read(b, s, n);
- s[n - 1] = '\0';
-
- return (s);
-}
-
/*
* Figure out the current session. Use: 1) the current session, if the command
* context has one; 2) the session specified in the TMUX variable from the