summaryrefslogtreecommitdiffstats
path: root/usr.bin/tmux/cmd-source-file.c
diff options
context:
space:
mode:
authornicm <nicm@openbsd.org>2011-01-04 00:42:46 +0000
committernicm <nicm@openbsd.org>2011-01-04 00:42:46 +0000
commitca7befcc5189fa88f5a692b06394473e8d9ada88 (patch)
treed067816fbc088f99c228eb4c8bc0954e84c0eab1 /usr.bin/tmux/cmd-source-file.c
parentPartial cleanup of argument count validation in mdoc(7): (diff)
downloadwireguard-openbsd-ca7befcc5189fa88f5a692b06394473e8d9ada88.tar.xz
wireguard-openbsd-ca7befcc5189fa88f5a692b06394473e8d9ada88.zip
Clean up and simplify tmux command argument parsing.
Originally, tmux commands were parsed in the client process into a struct with the command data which was then serialised and sent to the server to be executed. The parsing was later moved into the server (an argv was sent from the client), but the parse step and intermediate struct was kept. This change removes that struct and the separate parse step. Argument parsing and printing is now common to all commands (in arguments.c) with each command left with just an optional check function (to validate the arguments at parse time), the exec function and a function to set up any key bindings (renamed from the old init function). This is overall more simple and consistent. There should be no changes to any commands behaviour or syntax although as this touches every command please watch for any unexpected changes.
Diffstat (limited to 'usr.bin/tmux/cmd-source-file.c')
-rw-r--r--usr.bin/tmux/cmd-source-file.c99
1 files changed, 13 insertions, 86 deletions
diff --git a/usr.bin/tmux/cmd-source-file.c b/usr.bin/tmux/cmd-source-file.c
index 28f960d0b23..6d04a508e6c 100644
--- a/usr.bin/tmux/cmd-source-file.c
+++ b/usr.bin/tmux/cmd-source-file.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: cmd-source-file.c,v 1.10 2010/12/29 21:28:32 nicm Exp $ */
+/* $OpenBSD: cmd-source-file.c,v 1.11 2011/01/04 00:42:47 nicm Exp $ */
/*
* Copyright (c) 2008 Tiago Cunha <me@tiagocunha.org>
@@ -24,80 +24,31 @@
* Sources a configuration file.
*/
-int cmd_source_file_parse(struct cmd *, int, char **, char **);
int cmd_source_file_exec(struct cmd *, struct cmd_ctx *);
-void cmd_source_file_free(struct cmd *);
-void cmd_source_file_init(struct cmd *, int);
-size_t cmd_source_file_print(struct cmd *, char *, size_t);
-
-struct cmd_source_file_data {
- char *path;
-};
const struct cmd_entry cmd_source_file_entry = {
"source-file", "source",
+ "", 1, 1,
"path",
- 0, "",
- cmd_source_file_init,
- cmd_source_file_parse,
- cmd_source_file_exec,
- cmd_source_file_free,
- cmd_source_file_print
+ 0,
+ NULL,
+ NULL,
+ cmd_source_file_exec
};
-/* ARGSUSED */
-void
-cmd_source_file_init(struct cmd *self, unused int arg)
-{
- struct cmd_source_file_data *data;
-
- self->data = data = xmalloc(sizeof *data);
- data->path = NULL;
-}
-
-int
-cmd_source_file_parse(struct cmd *self, int argc, char **argv, char **cause)
-{
- struct cmd_source_file_data *data;
- int opt;
-
- self->entry->init(self, KEYC_NONE);
- data = self->data;
-
- while ((opt = getopt(argc, argv, "")) != -1) {
- switch (opt) {
- default:
- goto usage;
- }
- }
- argc -= optind;
- argv += optind;
- if (argc != 1)
- goto usage;
-
- data->path = xstrdup(argv[0]);
- return (0);
-
-usage:
- xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
-
- self->entry->free(self);
- return (-1);
-}
-
int
cmd_source_file_exec(struct cmd *self, struct cmd_ctx *ctx)
{
- struct cmd_source_file_data *data = self->data;
- struct causelist causes;
- char *cause;
- struct window_pane *wp;
- int retval;
- u_int i;
+ struct args *args = self->args;
+ struct causelist causes;
+ char *cause;
+ struct window_pane *wp;
+ int retval;
+ u_int i;
ARRAY_INIT(&causes);
- retval = load_cfg(data->path, ctx, &causes);
+ retval = load_cfg(args->argv[0], ctx, &causes);
if (ARRAY_EMPTY(&causes))
return (retval);
@@ -121,27 +72,3 @@ cmd_source_file_exec(struct cmd *self, struct cmd_ctx *ctx)
return (retval);
}
-
-void
-cmd_source_file_free(struct cmd *self)
-{
- struct cmd_source_file_data *data = self->data;
-
- if (data->path != NULL)
- xfree(data->path);
- xfree(data);
-}
-
-size_t
-cmd_source_file_print(struct cmd *self, char *buf, size_t len)
-{
- struct cmd_source_file_data *data = self->data;
- size_t off = 0;
-
- off += xsnprintf(buf, len, "%s", self->entry->name);
- if (data == NULL)
- return (off);
- if (off < len && data->path != NULL)
- off += cmd_prarg(buf + off, len - off, " ", data->path);
- return (off);
-}