-/* $OpenBSD: arguments.c,v 1.37 2021/08/20 18:59:53 nicm Exp $ */
+/* $OpenBSD: arguments.c,v 1.38 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2010 Nicholas Marriott <nicholas.marriott@gmail.com>
RB_ENTRY(args_entry) entry;
};
+struct args {
+ struct args_tree tree;
+ int argc;
+ char **argv;
+};
+
static struct args_entry *args_find(struct args *, u_char);
static int args_cmp(struct args_entry *, struct args_entry *);
/* Parse an argv and argc into a new argument set. */
struct args *
-args_parse(const char *template, int argc, char **argv)
+args_parse(const char *template, int argc, char **argv, int lower, int upper)
{
struct args *args;
int opt;
args->argc = argc;
args->argv = cmd_copy_argv(argc, argv);
+ if ((lower != -1 && argc < lower) || (upper != -1 && argc > upper)) {
+ args_free(args);
+ return (NULL);
+ }
return (args);
}
free(args);
}
+/* Convert arguments to vector. */
+void
+args_vector(struct args *args, int *argc, char ***argv)
+{
+ *argc = args->argc;
+ *argv = cmd_copy_argv(args->argc, args->argv);
+}
+
/* Add to string. */
static void printflike(3, 4)
args_print_add(char **buf, size_t *len, const char *fmt, ...)
free(s);
}
-/* Add value to string. */
-static void
-args_print_add_value(char **buf, size_t *len, struct args_entry *entry,
- struct args_value *value)
-{
- char *escaped;
-
- if (**buf != '\0')
- args_print_add(buf, len, " -%c ", entry->flag);
- else
- args_print_add(buf, len, "-%c ", entry->flag);
-
- escaped = args_escape(value->value);
- args_print_add(buf, len, "%s", escaped);
- free(escaped);
-}
-
/* Add argument to string. */
static void
args_print_add_argument(char **buf, size_t *len, const char *argument)
/* Then the flags with arguments. */
RB_FOREACH(entry, args_tree, &args->tree) {
- TAILQ_FOREACH(value, &entry->values, entry)
- args_print_add_value(&buf, &len, entry, value);
+ TAILQ_FOREACH(value, &entry->values, entry) {
+ if (*buf != '\0')
+ args_print_add(&buf, &len, " -%c", entry->flag);
+ else
+ args_print_add(&buf, &len, "-%c", entry->flag);
+ args_print_add_argument(&buf, &len, value->value);
+ }
}
/* And finally the argument vector. */
return ((*entry)->flag);
}
+/* Get argument count. */
+u_int
+args_count(struct args *args)
+{
+ return (args->argc);
+}
+
+/* Return argument as string. */
+const char *
+args_string(struct args *args, u_int idx)
+{
+ if (idx >= (u_int)args->argc)
+ return (NULL);
+ return (args->argv[idx]);
+}
+
/* Get first value in argument. */
struct args_value *
args_first_value(struct args *args, u_char flag)
-/* $OpenBSD: cmd-bind-key.c,v 1.38 2020/09/08 10:19:19 nicm Exp $ */
+/* $OpenBSD: cmd-bind-key.c,v 1.39 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
key_code key;
const char *tablename, *note = args_get(args, 'N');
struct cmd_parse_result *pr;
- char **argv = args->argv;
- int argc = args->argc, repeat;
+ char **argv;
+ int argc, repeat;
+ u_int count = args_count(args);
- key = key_string_lookup_string(argv[0]);
+ key = key_string_lookup_string(args_string(args, 0));
if (key == KEYC_NONE || key == KEYC_UNKNOWN) {
- cmdq_error(item, "unknown key: %s", argv[0]);
+ cmdq_error(item, "unknown key: %s", args_string(args, 0));
return (CMD_RETURN_ERROR);
}
tablename = "prefix";
repeat = args_has(args, 'r');
- if (argc != 1) {
- if (argc == 2)
- pr = cmd_parse_from_string(argv[1], NULL);
- else
+ if (count != 1) {
+ if (count == 2)
+ pr = cmd_parse_from_string(args_string(args, 1), NULL);
+ else {
+ args_vector(args, &argc, &argv);
pr = cmd_parse_from_arguments(argc - 1, argv + 1, NULL);
+ cmd_free_argv(argc, argv);
+ }
switch (pr->status) {
case CMD_PARSE_EMPTY:
cmdq_error(item, "empty command");
-/* $OpenBSD: cmd-command-prompt.c,v 1.57 2021/08/17 19:26:42 nicm Exp $ */
+/* $OpenBSD: cmd-command-prompt.c,v 1.58 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
struct args *args = cmd_get_args(self);
struct client *tc = cmdq_get_target_client(item);
struct cmd_find_state *target = cmdq_get_target(item);
- const char *inputs, *prompts, *type;
+ const char *inputs, *prompts, *type, *s;
struct cmd_command_prompt_cdata *cdata;
- char *prompt, *ptr, *input = NULL;
+ char *prompt, *comma, *input = NULL;
+ u_int count = args_count(args);
size_t n;
int wait = !args_has(args, 'b');
if (wait)
cdata->item = item;
- if (args->argc != 0 && args_has(args, 'F'))
- cdata->template = format_single_from_target(item, args->argv[0]);
- else if (args->argc != 0)
- cdata->template = xstrdup(args->argv[0]);
- else
+ if (count != 0) {
+ s = args_string(args, 0);
+ if (args_has(args, 'F'))
+ cdata->template = format_single_from_target(item, s);
+ else
+ cdata->template = xstrdup(s);
+ } else
cdata->template = xstrdup("%1");
if ((prompts = args_get(args, 'p')) != NULL)
cdata->prompts = xstrdup(prompts);
- else if (args->argc != 0) {
+ else if (count != 0) {
n = strcspn(cdata->template, " ,");
- xasprintf(&cdata->prompts, "(%.*s) ", (int) n, cdata->template);
+ xasprintf(&cdata->prompts, "(%.*s) ", (int)n, cdata->template);
} else
cdata->prompts = xstrdup(":");
/* Get first prompt. */
cdata->next_prompt = cdata->prompts;
- ptr = strsep(&cdata->next_prompt, ",");
+ comma = strsep(&cdata->next_prompt, ",");
if (prompts == NULL)
- prompt = xstrdup(ptr);
+ prompt = xstrdup(comma);
else
- xasprintf(&prompt, "%s ", ptr);
+ xasprintf(&prompt, "%s ", comma);
/* Get initial prompt input. */
if ((inputs = args_get(args, 'I')) != NULL) {
int done)
{
struct cmd_command_prompt_cdata *cdata = data;
- char *new_template, *prompt, *ptr, *error;
+ char *new_template, *prompt, *comma, *error;
char *input = NULL;
struct cmdq_item *item = cdata->item;
enum cmd_parse_status status;
* Check if there are more prompts; if so, get its respective input
* and update the prompt data.
*/
- if (done && (ptr = strsep(&cdata->next_prompt, ",")) != NULL) {
- xasprintf(&prompt, "%s ", ptr);
+ if (done && (comma = strsep(&cdata->next_prompt, ",")) != NULL) {
+ xasprintf(&prompt, "%s ", comma);
input = strsep(&cdata->next_input, ",");
status_prompt_update(c, prompt, input);
-/* $OpenBSD: cmd-confirm-before.c,v 1.46 2021/08/17 11:20:13 nicm Exp $ */
+/* $OpenBSD: cmd-confirm-before.c,v 1.47 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
struct cmd_confirm_before_data *cdata;
struct client *tc = cmdq_get_target_client(item);
struct cmd_find_state *target = cmdq_get_target(item);
- char *cmd, *copy, *new_prompt, *ptr;
+ char *cmd, *copy, *new_prompt, *tmp;
const char *prompt;
int wait = !args_has(args, 'b');
+ cdata = xcalloc(1, sizeof *cdata);
+ cdata->cmd = xstrdup(args_string(args, 0));
+
if ((prompt = args_get(args, 'p')) != NULL)
xasprintf(&new_prompt, "%s ", prompt);
else {
- ptr = copy = xstrdup(args->argv[0]);
- cmd = strsep(&ptr, " \t");
+ tmp = copy = xstrdup(cdata->cmd);
+ cmd = strsep(&tmp, " \t");
xasprintf(&new_prompt, "Confirm '%s'? (y/n) ", cmd);
free(copy);
}
- cdata = xcalloc(1, sizeof *cdata);
- cdata->cmd = xstrdup(args->argv[0]);
-
cmd_get_source(self, &cdata->pi.file, &cdata->pi.line);
if (wait)
cdata->pi.item = item;
-/* $OpenBSD: cmd-display-menu.c,v 1.27 2021/08/13 17:03:29 nicm Exp $ */
+/* $OpenBSD: cmd-display-menu.c,v 1.28 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com>
struct client *tc = cmdq_get_target_client(item);
struct menu *menu = NULL;
struct menu_item menu_item;
- const char *key;
- char *title, *name;
- int flags = 0, i;
- u_int px, py;
+ const char *key, *name;
+ char *title;
+ int flags = 0;
+ u_int px, py, i, count = args_count(args);
if (tc->overlay_draw != NULL)
return (CMD_RETURN_NORMAL);
title = xstrdup("");
menu = menu_create(title);
- for (i = 0; i != args->argc; /* nothing */) {
- name = args->argv[i++];
+ for (i = 0; i != count; /* nothing */) {
+ name = args_string(args, i++);
if (*name == '\0') {
menu_add_item(menu, NULL, item, tc, target);
continue;
}
- if (args->argc - i < 2) {
+ if (count - i < 2) {
cmdq_error(item, "not enough arguments");
free(title);
menu_free(menu);
return (CMD_RETURN_ERROR);
}
- key = args->argv[i++];
+ key = args_string(args, i++);
menu_item.name = name;
menu_item.key = key_string_lookup_string(key);
- menu_item.command = args->argv[i++];
+ menu_item.command = args_string(args, i++);
menu_add_item(menu, &menu_item, item, tc, target);
}
struct session *s = target->s;
struct client *tc = cmdq_get_target_client(item);
struct tty *tty = &tc->tty;
- const char *value, *shell[] = { NULL, NULL };
- const char *shellcmd = NULL;
- char *cwd, *cause, **argv = args->argv;
- int flags = 0, argc = args->argc;
- u_int px, py, w, h;
+ const char *value, *shell, *shellcmd = NULL;
+ char *cwd, *cause, **argv = NULL;
+ int flags = 0, argc = 0;
+ u_int px, py, w, h, count = args_count(args);
if (args_has(args, 'C')) {
server_client_clear_overlay(tc);
cwd = format_single_from_target(item, value);
else
cwd = xstrdup(server_client_get_cwd(tc, s));
- if (argc == 0)
+ if (count == 0)
shellcmd = options_get_string(s->options, "default-command");
- else if (argc == 1)
- shellcmd = argv[0];
- if (argc <= 1 && (shellcmd == NULL || *shellcmd == '\0')) {
+ else if (count == 1)
+ shellcmd = args_string(args, 0);
+ if (count <= 1 && (shellcmd == NULL || *shellcmd == '\0')) {
shellcmd = NULL;
- shell[0] = options_get_string(s->options, "default-shell");
- if (!checkshell(shell[0]))
- shell[0] = _PATH_BSHELL;
- argc = 1;
- argv = (char**)shell;
- }
+ shell = options_get_string(s->options, "default-shell");
+ if (!checkshell(shell))
+ shell = _PATH_BSHELL;
+ cmd_append_argv(&argc, &argv, shell);
+ } else
+ args_vector(args, &argc, &argv);
if (args_has(args, 'E') > 1)
flags |= POPUP_CLOSEEXITZERO;
if (args_has(args, 'B'))
flags |= POPUP_NOBORDER;
if (popup_display(flags, item, px, py, w, h, shellcmd, argc, argv, cwd,
- tc, s, NULL, NULL) != 0)
+ tc, s, NULL, NULL) != 0) {
+ cmd_free_argv(argc, argv);
return (CMD_RETURN_NORMAL);
+ }
+ cmd_free_argv(argc, argv);
return (CMD_RETURN_WAIT);
}
-/* $OpenBSD: cmd-display-message.c,v 1.58 2021/08/14 08:06:37 nicm Exp $ */
+/* $OpenBSD: cmd-display-message.c,v 1.59 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
struct window_pane *wp = target->wp;
const char *template;
char *msg, *cause;
- int delay = -1;
+ int delay = -1, flags;
struct format_tree *ft;
- int flags;
+ u_int count = args_count(args);
if (args_has(args, 'I')) {
if (wp == NULL)
return (CMD_RETURN_WAIT);
}
- if (args_has(args, 'F') && args->argc != 0) {
+ if (args_has(args, 'F') && count != 0) {
cmdq_error(item, "only one of -F or argument must be given");
return (CMD_RETURN_ERROR);
}
}
}
- template = args_get(args, 'F');
- if (args->argc != 0)
- template = args->argv[0];
+ if (count != 0)
+ template = args_string(args, 0);
+ else
+ template = args_get(args, 'F');
if (template == NULL)
template = DISPLAY_MESSAGE_TEMPLATE;
-/* $OpenBSD: cmd-display-panes.c,v 1.40 2021/08/13 18:54:54 nicm Exp $ */
+/* $OpenBSD: cmd-display-panes.c,v 1.41 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
delay = options_get_number(s->options, "display-panes-time");
cdata = xmalloc(sizeof *cdata);
- if (args->argc != 0)
- cdata->command = xstrdup(args->argv[0]);
+ if (args_count(args))
+ cdata->command = xstrdup(args_string(args, 0));
else
cdata->command = xstrdup("select-pane -t '%%'");
if (args_has(args, 'b'))
-/* $OpenBSD: cmd-find-window.c,v 1.51 2021/08/20 17:53:54 nicm Exp $ */
+/* $OpenBSD: cmd-find-window.c,v 1.52 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
struct args *args = cmd_get_args(self), *new_args;
struct cmd_find_state *target = cmdq_get_target(item);
struct window_pane *wp = target->wp;
- const char *s = args->argv[0], *suffix = "";
+ const char *s = args_string(args, 0), *suffix = "";
char *filter;
int C, N, T;
-/* $OpenBSD: cmd-if-shell.c,v 1.76 2021/08/13 06:50:42 nicm Exp $ */
+/* $OpenBSD: cmd-if-shell.c,v 1.77 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
struct cmd_find_state *target = cmdq_get_target(item);
struct cmdq_state *state = cmdq_get_state(item);
struct cmd_if_shell_data *cdata;
- char *shellcmd, *cmd, *error;
- const char *file;
+ char *shellcmd, *error;
+ const char *cmd = NULL, *file;
struct client *tc = cmdq_get_target_client(item);
struct session *s = target->s;
struct cmd_parse_input pi;
enum cmd_parse_status status;
+ u_int count = args_count(args);
- shellcmd = format_single_from_target(item, args->argv[0]);
+ shellcmd = format_single_from_target(item, args_string(args, 0));
if (args_has(args, 'F')) {
if (*shellcmd != '0' && *shellcmd != '\0')
- cmd = args->argv[1];
- else if (args->argc == 3)
- cmd = args->argv[2];
- else
- cmd = NULL;
+ cmd = args_string(args, 1);
+ else if (count == 3)
+ cmd = args_string(args, 2);
free(shellcmd);
if (cmd == NULL)
return (CMD_RETURN_NORMAL);
cdata = xcalloc(1, sizeof *cdata);
- cdata->cmd_if = xstrdup(args->argv[1]);
- if (args->argc == 3)
- cdata->cmd_else = xstrdup(args->argv[2]);
+ cdata->cmd_if = xstrdup(args_string(args, 1));
+ if (count == 3)
+ cdata->cmd_else = xstrdup(args_string(args, 2));
if (!args_has(args, 'b'))
cdata->client = cmdq_get_client(item);
-/* $OpenBSD: cmd-list-keys.c,v 1.64 2021/08/20 17:50:42 nicm Exp $ */
+/* $OpenBSD: cmd-list-keys.c,v 1.65 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
struct args *args = cmd_get_args(self);
struct key_table *table;
struct key_binding *bd;
- const char *tablename, *r;
+ const char *tablename, *r, *keystr;
char *key, *cp, *tmp, *start, *empty;
key_code prefix, only = KEYC_UNKNOWN;
int repeat, width, tablewidth, keywidth, found = 0;
if (cmd_get_entry(self) == &cmd_list_commands_entry)
return (cmd_list_keys_commands(self, item));
- if (args->argc != 0) {
- only = key_string_lookup_string(args->argv[0]);
+ if ((keystr = args_string(args, 0)) != NULL) {
+ only = key_string_lookup_string(keystr);
if (only == KEYC_UNKNOWN) {
- cmdq_error(item, "invalid key: %s", args->argv[0]);
+ cmdq_error(item, "invalid key: %s", keystr);
return (CMD_RETURN_ERROR);
}
only &= (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS);
tmpsize = 256;
tmp = xmalloc(tmpsize);
+
table = key_bindings_first_table();
while (table != NULL) {
if (tablename != NULL && strcmp(table->name, tablename) != 0) {
out:
if (only != KEYC_UNKNOWN && !found) {
- cmdq_error(item, "unknown key: %s", args->argv[0]);
+ cmdq_error(item, "unknown key: %s", args_string(args, 0));
return (CMD_RETURN_ERROR);
}
return (CMD_RETURN_NORMAL);
const struct cmd_entry **entryp;
const struct cmd_entry *entry;
struct format_tree *ft;
- const char *template, *s, *command = NULL;
+ const char *template, *s, *command;
char *line;
- if (args->argc != 0)
- command = args->argv[0];
-
if ((template = args_get(args, 'F')) == NULL) {
template = "#{command_list_name}"
"#{?command_list_alias, (#{command_list_alias}),} "
ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
format_defaults(ft, NULL, NULL, NULL, NULL);
+ command = args_string(args, 0);
for (entryp = cmd_table; *entryp != NULL; entryp++) {
entry = *entryp;
if (command != NULL &&
-/* $OpenBSD: cmd-load-buffer.c,v 1.62 2020/09/04 12:24:25 nicm Exp $ */
+/* $OpenBSD: cmd-load-buffer.c,v 1.63 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
cdata->client->references++;
}
- path = format_single_from_target(item, args->argv[0]);
+ path = format_single_from_target(item, args_string(args, 0));
file_read(cmdq_get_client(item), path, cmd_load_buffer_done, cdata);
free(path);
-/* $OpenBSD: cmd-new-session.c,v 1.140 2021/08/20 18:59:53 nicm Exp $ */
+/* $OpenBSD: cmd-new-session.c,v 1.141 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
char *cause, *cwd = NULL, *cp, *newname = NULL;
char *name, *prefix = NULL;
int detached, already_attached, is_control = 0;
- u_int sx, sy, dsx, dsy;
- struct spawn_context sc;
+ u_int sx, sy, dsx, dsy, count = args_count(args);
+ struct spawn_context sc = { 0 };
enum cmd_retval retval;
struct cmd_find_state fs;
struct args_value *av;
return (CMD_RETURN_NORMAL);
}
- if (args_has(args, 't') && (args->argc != 0 || args_has(args, 'n'))) {
+ if (args_has(args, 't') && (count != 0 || args_has(args, 'n'))) {
cmdq_error(item, "command or window name given with target");
return (CMD_RETURN_ERROR);
}
s = session_create(prefix, newname, cwd, env, oo, tiop);
/* Spawn the initial window. */
- memset(&sc, 0, sizeof sc);
sc.item = item;
sc.s = s;
if (!detached)
sc.tc = c;
sc.name = args_get(args, 'n');
- sc.argc = args->argc;
- sc.argv = args->argv;
+ args_vector(args, &sc.argc, &sc.argv);
sc.idx = -1;
sc.cwd = args_get(args, 'c');
cmd_find_from_session(&fs, s, 0);
cmdq_insert_hook(s, item, &fs, "after-new-session");
+ if (sc.argv != NULL)
+ cmd_free_argv(sc.argc, sc.argv);
free(cwd);
free(newname);
free(prefix);
return (CMD_RETURN_NORMAL);
fail:
+ if (sc.argv != NULL)
+ cmd_free_argv(sc.argc, sc.argv);
free(cwd);
free(newname);
free(prefix);
-/* $OpenBSD: cmd-new-window.c,v 1.92 2021/08/20 19:08:36 nicm Exp $ */
+/* $OpenBSD: cmd-new-window.c,v 1.93 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
struct client *c = cmdq_get_client(item);
struct cmd_find_state *current = cmdq_get_current(item);
struct cmd_find_state *target = cmdq_get_target(item);
- struct spawn_context sc;
+ struct spawn_context sc = { 0 };
struct client *tc = cmdq_get_target_client(item);
struct session *s = target->s;
- struct winlink *wl = target->wl;
+ struct winlink *wl = target->wl, *new_wl = NULL;
int idx = target->idx, before;
- struct winlink *new_wl = NULL;
char *cause = NULL, *cp;
const char *template, *name;
struct cmd_find_state fs;
idx = target->idx;
}
- memset(&sc, 0, sizeof sc);
sc.item = item;
sc.s = s;
sc.tc = tc;
sc.name = args_get(args, 'n');
- sc.argc = args->argc;
- sc.argv = args->argv;
+ args_vector(args, &sc.argc, &sc.argv);
sc.environ = environ_create();
av = args_first_value(args, 'e');
if ((new_wl = spawn_window(&sc, &cause)) == NULL) {
cmdq_error(item, "create window failed: %s", cause);
free(cause);
+ if (sc.argv != NULL)
+ cmd_free_argv(sc.argc, sc.argv);
environ_free(sc.environ);
return (CMD_RETURN_ERROR);
}
cmd_find_from_winlink(&fs, new_wl, 0);
cmdq_insert_hook(s, item, &fs, "after-new-window");
+ if (sc.argv != NULL)
+ cmd_free_argv(sc.argc, sc.argv);
environ_free(sc.environ);
return (CMD_RETURN_NORMAL);
}
-/* $OpenBSD: cmd-pipe-pane.c,v 1.56 2021/08/17 19:37:55 nicm Exp $ */
+/* $OpenBSD: cmd-pipe-pane.c,v 1.57 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
}
/* If no pipe command, that is enough. */
- if (args->argc == 0 || *args->argv[0] == '\0')
+ if (args_count(args) == 0 || *args_string(args, 0) == '\0')
return (CMD_RETURN_NORMAL);
/*
/* Expand the command. */
ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
format_defaults(ft, tc, s, wl, wp);
- cmd = format_expand_time(ft, args->argv[0]);
+ cmd = format_expand_time(ft, args_string(args, 0));
format_free(ft);
/* Fork the child. */
-/* $OpenBSD: cmd-queue.c,v 1.104 2021/08/20 18:59:53 nicm Exp $ */
+/* $OpenBSD: cmd-queue.c,v 1.105 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2013 Nicholas Marriott <nicholas.marriott@gmail.com>
struct options *oo;
va_list ap;
char *name, tmp[32], flag, *arguments;
- int i;
+ u_int i;
const char *value;
struct cmdq_item *new_item;
struct cmdq_state *new_state;
cmdq_add_format(new_state, "hook_arguments", "%s", arguments);
free(arguments);
- for (i = 0; i < args->argc; i++) {
+ for (i = 0; i < args_count(args); i++) {
xsnprintf(tmp, sizeof tmp, "hook_argument_%d", i);
- cmdq_add_format(new_state, tmp, "%s", args->argv[i]);
+ cmdq_add_format(new_state, tmp, "%s", args_string(args, i));
}
flag = args_first(args, &ae);
while (flag != 0) {
-/* $OpenBSD: cmd-refresh-client.c,v 1.41 2021/08/20 19:34:51 nicm Exp $ */
+/* $OpenBSD: cmd-refresh-client.c,v 1.42 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
args_has(args, 'U') ||
args_has(args, 'D'))
{
- if (args->argc == 0)
+ if (args_count(args) == 0)
adjust = 1;
else {
- adjust = strtonum(args->argv[0], 1, INT_MAX, &errstr);
+ adjust = strtonum(args_string(args, 0), 1, INT_MAX,
+ &errstr);
if (errstr != NULL) {
cmdq_error(item, "adjustment %s", errstr);
return (CMD_RETURN_ERROR);
-/* $OpenBSD: cmd-rename-session.c,v 1.33 2021/07/06 08:18:38 nicm Exp $ */
+/* $OpenBSD: cmd-rename-session.c,v 1.34 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
struct session *s = target->s;
char *newname, *tmp;
- tmp = format_single_from_target(item, args->argv[0]);
+ tmp = format_single_from_target(item, args_string(args, 0));
newname = session_check_name(tmp);
if (newname == NULL) {
cmdq_error(item, "invalid session: %s", tmp);
-/* $OpenBSD: cmd-rename-window.c,v 1.26 2020/05/16 15:01:30 nicm Exp $ */
+/* $OpenBSD: cmd-rename-window.c,v 1.27 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
struct winlink *wl = target->wl;
char *newname;
- newname = format_single_from_target(item, args->argv[0]);
+ newname = format_single_from_target(item, args_string(args, 0));
window_set_name(wl->window, newname);
options_set_number(wl->window->options, "automatic-rename", 0);
-/* $OpenBSD: cmd-resize-pane.c,v 1.48 2020/05/25 18:17:14 nicm Exp $ */
+/* $OpenBSD: cmd-resize-pane.c,v 1.49 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
}
server_unzoom_window(w);
- if (args->argc == 0)
+ if (args_count(args) == 0)
adjust = 1;
else {
- adjust = strtonum(args->argv[0], 1, INT_MAX, &errstr);
+ adjust = strtonum(args_string(args, 0), 1, INT_MAX,
+ &errstr);
if (errstr != NULL) {
cmdq_error(item, "adjustment %s", errstr);
return (CMD_RETURN_ERROR);
-/* $OpenBSD: cmd-resize-window.c,v 1.5 2020/04/13 10:59:58 nicm Exp $ */
+/* $OpenBSD: cmd-resize-window.c,v 1.6 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2018 Nicholas Marriott <nicholas.marriott@gmail.com>
u_int adjust, sx, sy;
int xpixel = -1, ypixel = -1;
- if (args->argc == 0)
+ if (args_count(args) == 0)
adjust = 1;
else {
- adjust = strtonum(args->argv[0], 1, INT_MAX, &errstr);
+ adjust = strtonum(args_string(args, 0), 1, INT_MAX,
+ &errstr);
if (errstr != NULL) {
cmdq_error(item, "adjustment %s", errstr);
return (CMD_RETURN_ERROR);
-/* $OpenBSD: cmd-respawn-pane.c,v 1.34 2021/08/20 18:59:53 nicm Exp $ */
+/* $OpenBSD: cmd-respawn-pane.c,v 1.35 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
{
struct args *args = cmd_get_args(self);
struct cmd_find_state *target = cmdq_get_target(item);
- struct spawn_context sc;
+ struct spawn_context sc = { 0 };
struct session *s = target->s;
struct winlink *wl = target->wl;
struct window_pane *wp = target->wp;
char *cause = NULL;
struct args_value *av;
- memset(&sc, 0, sizeof sc);
sc.item = item;
sc.s = s;
sc.wl = wl;
sc.lc = NULL;
sc.name = NULL;
- sc.argc = args->argc;
- sc.argv = args->argv;
+ args_vector(args, &sc.argc, &sc.argv);
sc.environ = environ_create();
av = args_first_value(args, 'e');
if (spawn_pane(&sc, &cause) == NULL) {
cmdq_error(item, "respawn pane failed: %s", cause);
free(cause);
+ if (sc.argv != NULL)
+ cmd_free_argv(sc.argc, sc.argv);
environ_free(sc.environ);
return (CMD_RETURN_ERROR);
}
server_redraw_window_borders(wp->window);
server_status_window(wp->window);
+ if (sc.argv != NULL)
+ cmd_free_argv(sc.argc, sc.argv);
environ_free(sc.environ);
return (CMD_RETURN_NORMAL);
}
-/* $OpenBSD: cmd-respawn-window.c,v 1.45 2021/08/20 18:59:53 nicm Exp $ */
+/* $OpenBSD: cmd-respawn-window.c,v 1.46 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
{
struct args *args = cmd_get_args(self);
struct cmd_find_state *target = cmdq_get_target(item);
- struct spawn_context sc;
+ struct spawn_context sc = { 0 };
struct client *tc = cmdq_get_target_client(item);
struct session *s = target->s;
struct winlink *wl = target->wl;
char *cause = NULL;
struct args_value *av;
- memset(&sc, 0, sizeof sc);
sc.item = item;
sc.s = s;
sc.wl = wl;
sc.tc = tc;
sc.name = NULL;
- sc.argc = args->argc;
- sc.argv = args->argv;
+ args_vector(args, &sc.argc, &sc.argv);
sc.environ = environ_create();
av = args_first_value(args, 'e');
if (spawn_window(&sc, &cause) == NULL) {
cmdq_error(item, "respawn window failed: %s", cause);
free(cause);
+ if (sc.argv != NULL)
+ cmd_free_argv(sc.argc, sc.argv);
environ_free(sc.environ);
return (CMD_RETURN_ERROR);
}
server_redraw_window(wl->window);
+ if (sc.argv != NULL)
+ cmd_free_argv(sc.argc, sc.argv);
environ_free(sc.environ);
return (CMD_RETURN_NORMAL);
}
-/* $OpenBSD: cmd-run-shell.c,v 1.74 2021/08/13 06:50:42 nicm Exp $ */
+/* $OpenBSD: cmd-run-shell.c,v 1.75 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
double d;
struct timeval tv;
char *end;
+ const char *cmd = args_string(args, 0);
int wait = !args_has(args, 'b');
if ((delay = args_get(args, 'd')) != NULL) {
cmdq_error(item, "invalid delay time: %s", delay);
return (CMD_RETURN_ERROR);
}
- } else if (args->argc == 0)
+ } else if (args_count(args) == 0)
return (CMD_RETURN_NORMAL);
cdata = xcalloc(1, sizeof *cdata);
- if (args->argc != 0)
- cdata->cmd = format_single_from_target(item, args->argv[0]);
+ if (cmd != NULL)
+ cdata->cmd = format_single_from_target(item, cmd);
cdata->shell = !args_has(args, 'C');
if (!cdata->shell) {
-/* $OpenBSD: cmd-save-buffer.c,v 1.52 2021/02/11 09:03:38 nicm Exp $ */
+/* $OpenBSD: cmd-save-buffer.c,v 1.53 2021/08/20 19:50:16 nicm Exp $ */
/*
* Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
}
path = xstrdup("-");
} else
- path = format_single_from_target(item, args->argv[0]);
+ path = format_single_from_target(item, args_string(args, 0));
if (args_has(args, 'a'))
flags = O_APPEND;
else
-/* $OpenBSD: cmd-select-layout.c,v 1.38 2020/04/13 10:59:58 nicm Exp $ */
+/* $OpenBSD: cmd-select-layout.c,v 1.39 2021/08/20 19:50:17 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
goto changed;
}
+ if (args_count(args) != 0)
+ layoutname = args_string(args, 0);
+ else if (args_has(args, 'o'))
+ layoutname = oldlayout;
+ else
+ layoutname = NULL;
+
if (!args_has(args, 'o')) {
- if (args->argc == 0)
+ if (layoutname == NULL)
layout = w->lastlayout;
else
- layout = layout_set_lookup(args->argv[0]);
+ layout = layout_set_lookup(layoutname);
if (layout != -1) {
layout_set_select(w, layout);
goto changed;
}
}
- if (args->argc != 0)
- layoutname = args->argv[0];
- else if (args_has(args, 'o'))
- layoutname = oldlayout;
- else
- layoutname = NULL;
-
if (layoutname != NULL) {
if (layout_parse(w, layoutname) == -1) {
cmdq_error(item, "can't set layout: %s", layoutname);
-/* $OpenBSD: cmd-send-keys.c,v 1.66 2021/08/11 20:49:55 nicm Exp $ */
+/* $OpenBSD: cmd-send-keys.c,v 1.67 2021/08/20 19:50:17 nicm Exp $ */
/*
* Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
cmd_send_keys_inject_string(struct cmdq_item *item, struct cmdq_item *after,
struct args *args, int i)
{
- const char *s = args->argv[i];
+ const char *s = args_string(args, i);
struct utf8_data *ud, *loop;
utf8_char uc;
key_code key;
struct mouse_event *m = &event->m;
struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
struct cmdq_item *after = item;
- int i;
key_code key;
- u_int np = 1;
+ u_int i, np = 1;
+ u_int count = args_count(args);
char *cause = NULL;
if (args_has(args, 'N')) {
free(cause);
return (CMD_RETURN_ERROR);
}
- if (wme != NULL && (args_has(args, 'X') || args->argc == 0)) {
+ if (wme != NULL && (args_has(args, 'X') || count == 0)) {
if (wme->mode->command == NULL) {
cmdq_error(item, "not in a mode");
return (CMD_RETURN_ERROR);
}
for (; np != 0; np--) {
- for (i = 0; i < args->argc; i++) {
+ for (i = 0; i < count; i++) {
after = cmd_send_keys_inject_string(item, after, args,
i);
}
-/* $OpenBSD: cmd-set-buffer.c,v 1.30 2020/09/02 13:46:35 nicm Exp $ */
+/* $OpenBSD: cmd-set-buffer.c,v 1.31 2021/08/20 19:50:17 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
return (CMD_RETURN_NORMAL);
}
- if (args->argc != 1) {
+ if (args_count(args) != 1) {
cmdq_error(item, "no data specified");
return (CMD_RETURN_ERROR);
}
- if ((newsize = strlen(args->argv[0])) == 0)
+ if ((newsize = strlen(args_string(args, 0))) == 0)
return (CMD_RETURN_NORMAL);
bufsize = 0;
}
bufdata = xrealloc(bufdata, bufsize + newsize);
- memcpy(bufdata + bufsize, args->argv[0], newsize);
+ memcpy(bufdata + bufsize, args_string(args, 0), newsize);
bufsize += newsize;
if (paste_set(bufdata, bufsize, bufname, &cause) != 0) {
-/* $OpenBSD: cmd-set-environment.c,v 1.26 2020/09/01 09:19:01 nicm Exp $ */
+/* $OpenBSD: cmd-set-environment.c,v 1.27 2021/08/20 19:50:17 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
struct args *args = cmd_get_args(self);
struct cmd_find_state *target = cmdq_get_target(item);
struct environ *env;
- const char *name, *value, *tflag;
- char *expand = NULL;
+ const char *name = args_string(args, 0), *value;
+ const char *tflag;
+ char *expanded = NULL;
enum cmd_retval retval = CMD_RETURN_NORMAL;
- name = args->argv[0];
if (*name == '\0') {
cmdq_error(item, "empty variable name");
return (CMD_RETURN_ERROR);
return (CMD_RETURN_ERROR);
}
- if (args->argc < 2)
+ if (args_count(args) < 2)
value = NULL;
- else if (args_has(args, 'F'))
- value = expand = format_single_from_target(item, args->argv[1]);
else
- value = args->argv[1];
-
+ value = args_string(args, 1);
+ if (value != NULL && args_has(args, 'F')) {
+ expanded = format_single_from_target(item, value);
+ value = expanded;
+ }
if (args_has(args, 'g'))
env = global_environ;
else {
}
out:
- free(expand);
+ free(expanded);
return (retval);
}
-/* $OpenBSD: cmd-set-option.c,v 1.138 2020/12/15 08:31:50 nicm Exp $ */
+/* $OpenBSD: cmd-set-option.c,v 1.139 2021/08/20 19:50:17 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
struct window_pane *loop;
struct options *oo;
struct options_entry *parent, *o, *po;
- char *name, *argument, *value = NULL, *cause;
+ char *name, *argument, *expanded = NULL;
+ char *cause;
+ const char *value;
int window, idx, already, error, ambiguous;
int scope;
window = (cmd_get_entry(self) == &cmd_set_window_option_entry);
/* Expand argument. */
- argument = format_single_from_target(item, args->argv[0]);
+ argument = format_single_from_target(item, args_string(args, 0));
/* If set-hook -R, fire the hook straight away. */
if (cmd_get_entry(self) == &cmd_set_hook_entry && args_has(args, 'R')) {
cmdq_error(item, "invalid option: %s", argument);
goto fail;
}
- if (args->argc < 2)
+ if (args_count(args) < 2)
value = NULL;
- else if (args_has(args, 'F'))
- value = format_single_from_target(item, args->argv[1]);
else
- value = xstrdup(args->argv[1]);
+ value = args_string(args, 1);
+ if (value != NULL && args_has(args, 'F')) {
+ expanded = format_single_from_target(item, value);
+ value = expanded;
+ }
/* Get the scope and table for the option .*/
scope = options_scope_from_name(args, window, name, target, &oo,
out:
free(argument);
- free(value);
+ free(expanded);
free(name);
return (CMD_RETURN_NORMAL);
fail:
free(argument);
- free(value);
+ free(expanded);
free(name);
return (CMD_RETURN_ERROR);
}
-/* $OpenBSD: cmd-show-environment.c,v 1.26 2020/04/13 10:59:58 nicm Exp $ */
+/* $OpenBSD: cmd-show-environment.c,v 1.27 2021/08/20 19:50:17 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
struct cmd_find_state *target = cmdq_get_target(item);
struct environ *env;
struct environ_entry *envent;
- const char *tflag;
+ const char *tflag, *name = args_string(args, 0);
if ((tflag = args_get(args, 't')) != NULL) {
if (target->s == NULL) {
env = target->s->environ;
}
- if (args->argc != 0) {
- envent = environ_find(env, args->argv[0]);
+ if (name != NULL) {
+ envent = environ_find(env, name);
if (envent == NULL) {
- cmdq_error(item, "unknown variable: %s", args->argv[0]);
+ cmdq_error(item, "unknown variable: %s", name);
return (CMD_RETURN_ERROR);
}
cmd_show_environment_print(self, item, envent);
-/* $OpenBSD: cmd-show-options.c,v 1.65 2020/12/28 09:40:27 nicm Exp $ */
+/* $OpenBSD: cmd-show-options.c,v 1.66 2021/08/20 19:50:17 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
window = (cmd_get_entry(self) == &cmd_show_window_options_entry);
- if (args->argc == 0) {
+ if (args_count(args) == 0) {
scope = options_scope_from_flags(args, window, target, &oo,
&cause);
if (scope == OPTIONS_TABLE_NONE) {
}
return (cmd_show_options_all(self, item, scope, oo));
}
- argument = format_single_from_target(item, args->argv[0]);
+ argument = format_single_from_target(item, args_string(args, 0));
name = options_match(argument, &idx, &ambiguous);
if (name == NULL) {
-/* $OpenBSD: cmd-source-file.c,v 1.49 2021/06/10 07:53:19 nicm Exp $ */
+/* $OpenBSD: cmd-source-file.c,v 1.50 2021/08/20 19:50:17 nicm Exp $ */
/*
* Copyright (c) 2008 Tiago Cunha <me@tiagocunha.org>
struct cmd_source_file_data *cdata;
struct client *c = cmdq_get_client(item);
enum cmd_retval retval = CMD_RETURN_NORMAL;
- char *pattern, *cwd, *expand = NULL;
+ char *pattern, *cwd, *expanded = NULL;
const char *path, *error;
glob_t g;
- int i, result;
- u_int j;
+ int result;
+ u_int i, j;
cdata = xcalloc(1, sizeof *cdata);
cdata->item = item;
utf8_stravis(&cwd, server_client_get_cwd(c, NULL), VIS_GLOB);
- for (i = 0; i < args->argc; i++) {
+ for (i = 0; i < args_count(args); i++) {
+ path = args_string(args, i);
if (args_has(args, 'F')) {
- free(expand);
- expand = format_single_from_target(item, args->argv[i]);
- path = expand;
- } else
- path = args->argv[i];
+ free(expanded);
+ expanded = format_single_from_target(item, path);
+ path = expanded;
+ }
if (strcmp(path, "-") == 0) {
cmd_source_file_add(cdata, "-");
continue;
free(pattern);
continue;
}
- free(expand);
+ free(expanded);
free(pattern);
for (j = 0; j < g.gl_pathc; j++)
-/* $OpenBSD: cmd-split-window.c,v 1.106 2021/08/20 18:59:53 nicm Exp $ */
+/* $OpenBSD: cmd-split-window.c,v 1.107 2021/08/20 19:50:17 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
struct args *args = cmd_get_args(self);
struct cmd_find_state *current = cmdq_get_current(item);
struct cmd_find_state *target = cmdq_get_target(item);
- struct spawn_context sc;
+ struct spawn_context sc = { 0 };
struct client *tc = cmdq_get_target_client(item);
struct session *s = target->s;
struct winlink *wl = target->wl;
char *cause, *cp, *copy;
size_t plen;
struct args_value *av;
+ u_int count = args_count(args);
if (args_has(args, 'h'))
type = LAYOUT_LEFTRIGHT;
size = -1;
window_push_zoom(wp->window, 1, args_has(args, 'Z'));
- input = (args_has(args, 'I') && args->argc == 0);
+ input = (args_has(args, 'I') && count == 0);
flags = 0;
if (args_has(args, 'b'))
flags |= SPAWN_BEFORE;
if (args_has(args, 'f'))
flags |= SPAWN_FULLSIZE;
- if (input || (args->argc == 1 && *args->argv[0] == '\0'))
+ if (input || (count == 1 && *args_string(args, 0) == '\0'))
flags |= SPAWN_EMPTY;
lc = layout_split_pane(wp, type, size, flags);
return (CMD_RETURN_ERROR);
}
- memset(&sc, 0, sizeof sc);
sc.item = item;
sc.s = s;
sc.wl = wl;
sc.lc = lc;
sc.name = NULL;
- sc.argc = args->argc;
- sc.argv = args->argv;
+ args_vector(args, &sc.argc, &sc.argv);
sc.environ = environ_create();
av = args_first_value(args, 'e');
if ((new_wp = spawn_pane(&sc, &cause)) == NULL) {
cmdq_error(item, "create pane failed: %s", cause);
free(cause);
+ if (sc.argv != NULL)
+ cmd_free_argv(sc.argc, sc.argv);
environ_free(sc.environ);
return (CMD_RETURN_ERROR);
}
window_remove_pane(wp->window, new_wp);
cmdq_error(item, "%s", cause);
free(cause);
+ if (sc.argv != NULL)
+ cmd_free_argv(sc.argc, sc.argv);
environ_free(sc.environ);
return (CMD_RETURN_ERROR);
}
cmd_find_from_winlink_pane(&fs, wl, new_wp, 0);
cmdq_insert_hook(s, item, &fs, "after-split-window");
+ if (sc.argv != NULL)
+ cmd_free_argv(sc.argc, sc.argv);
environ_free(sc.environ);
if (input)
return (CMD_RETURN_WAIT);
-/* $OpenBSD: cmd-unbind-key.c,v 1.32 2020/09/16 19:12:59 nicm Exp $ */
+/* $OpenBSD: cmd-unbind-key.c,v 1.33 2021/08/20 19:50:17 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
{
struct args *args = cmd_get_args(self);
key_code key;
- const char *tablename;
+ const char *tablename, *keystr = args_string(args, 0);
int quiet = args_has(args, 'q');
if (args_has(args, 'a')) {
- if (args->argc != 0) {
+ if (keystr != NULL) {
if (!quiet)
cmdq_error(item, "key given with -a");
return (CMD_RETURN_ERROR);
return (CMD_RETURN_NORMAL);
}
- if (args->argc != 1) {
+ if (keystr == NULL) {
if (!quiet)
cmdq_error(item, "missing key");
return (CMD_RETURN_ERROR);
}
- key = key_string_lookup_string(args->argv[0]);
+ key = key_string_lookup_string(keystr);
if (key == KEYC_NONE || key == KEYC_UNKNOWN) {
if (!quiet)
- cmdq_error(item, "unknown key: %s", args->argv[0]);
+ cmdq_error(item, "unknown key: %s", keystr);
return (CMD_RETURN_ERROR);
}
-/* $OpenBSD: cmd-wait-for.c,v 1.20 2020/04/13 10:59:58 nicm Exp $ */
+/* $OpenBSD: cmd-wait-for.c,v 1.21 2021/08/20 19:50:17 nicm Exp $ */
/*
* Copyright (c) 2013 Nicholas Marriott <nicholas.marriott@gmail.com>
cmd_wait_for_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = cmd_get_args(self);
- const char *name = args->argv[0];
- struct wait_channel *wc, wc0;
+ const char *name = args_string(args, 0);
+ struct wait_channel *wc, find;
- wc0.name = name;
- wc = RB_FIND(wait_channels, &wait_channels, &wc0);
+ find.name = name;
+ wc = RB_FIND(wait_channels, &wait_channels, &find);
if (args_has(args, 'S'))
return (cmd_wait_for_signal(item, name, wc));
-/* $OpenBSD: cmd.c,v 1.165 2021/08/20 19:34:51 nicm Exp $ */
+/* $OpenBSD: cmd.c,v 1.166 2021/08/20 19:50:17 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
return (NULL);
cmd_log_argv(argc, argv, "%s: %s", __func__, entry->name);
- args = args_parse(entry->args.template, argc, argv);
+ args = args_parse(entry->args.template, argc, argv, entry->args.lower,
+ entry->args.upper);
if (args == NULL)
goto usage;
- if (entry->args.lower != -1 && args->argc < entry->args.lower)
- goto usage;
- if (entry->args.upper != -1 && args->argc > entry->args.upper)
- goto usage;
cmd = xcalloc(1, sizeof *cmd);
cmd->entry = entry;
-/* $OpenBSD: tmux.h,v 1.1126 2021/08/20 19:34:51 nicm Exp $ */
+/* $OpenBSD: tmux.h,v 1.1127 2021/08/20 19:50:17 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
/* Arguments set. */
struct args_entry;
RB_HEAD(args_tree, args_entry);
-struct args {
- struct args_tree tree;
- int argc;
- char **argv;
-};
/* Command find structures. */
enum cmd_find_type {
/* arguments.c */
void args_set(struct args *, u_char, const char *);
struct args *args_create(void);
-struct args *args_parse(const char *, int, char **);
+struct args *args_parse(const char *, int, char **, int, int);
+void args_vector(struct args *, int *, char ***);
void args_free(struct args *);
char *args_print(struct args *);
char *args_escape(const char *);
const char *args_get(struct args *, u_char);
u_char args_first(struct args *, struct args_entry **);
u_char args_next(struct args_entry **);
+u_int args_count(struct args *);
+const char *args_string(struct args *, u_int);
struct args_value *args_first_value(struct args *, u_char);
struct args_value *args_next_value(struct args_value *);
long long args_strtonum(struct args *, u_char, long long, long long,
-/* $OpenBSD: window-buffer.c,v 1.34 2021/06/10 07:45:43 nicm Exp $ */
+/* $OpenBSD: window-buffer.c,v 1.35 2021/08/20 19:50:17 nicm Exp $ */
/*
* Copyright (c) 2017 Nicholas Marriott <nicholas.marriott@gmail.com>
data->key_format = xstrdup(WINDOW_BUFFER_DEFAULT_KEY_FORMAT);
else
data->key_format = xstrdup(args_get(args, 'K'));
- if (args == NULL || args->argc == 0)
+ if (args == NULL || args_count(args) == 0)
data->command = xstrdup(WINDOW_BUFFER_DEFAULT_COMMAND);
else
- data->command = xstrdup(args->argv[0]);
+ data->command = xstrdup(args_string(args, 0));
data->data = mode_tree_start(wp, args, window_buffer_build,
window_buffer_draw, window_buffer_search, window_buffer_menu, NULL,
-/* $OpenBSD: window-client.c,v 1.31 2021/04/12 06:50:25 nicm Exp $ */
+/* $OpenBSD: window-client.c,v 1.32 2021/08/20 19:50:17 nicm Exp $ */
/*
* Copyright (c) 2017 Nicholas Marriott <nicholas.marriott@gmail.com>
data->key_format = xstrdup(WINDOW_CLIENT_DEFAULT_KEY_FORMAT);
else
data->key_format = xstrdup(args_get(args, 'K'));
- if (args == NULL || args->argc == 0)
+ if (args == NULL || args_count(args) == 0)
data->command = xstrdup(WINDOW_CLIENT_DEFAULT_COMMAND);
else
- data->command = xstrdup(args->argv[0]);
+ data->command = xstrdup(args_string(args, 0));
data->data = mode_tree_start(wp, args, window_client_build,
window_client_draw, NULL, window_client_menu, NULL,
-/* $OpenBSD: window-copy.c,v 1.328 2021/08/20 19:08:36 nicm Exp $ */
+/* $OpenBSD: window-copy.c,v 1.329 2021/08/20 19:50:17 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
{
struct window_mode_entry *wme = cs->wme;
struct window_copy_mode_data *data = wme->data;
- const char *argument;
+ const char *ss = args_string(cs->args, 1);
char *expanded;
- if (cs->args->argc == 2) {
- argument = cs->args->argv[1];
- if (*argument != '\0') {
- if (args_has(cs->args, 'F')) {
- expanded = format_single(NULL, argument, NULL,
- NULL, NULL, wme->wp);
- if (*expanded == '\0') {
- free(expanded);
- return (0);
- }
- free(data->searchstr);
- data->searchstr = expanded;
- } else {
- free(data->searchstr);
- data->searchstr = xstrdup(argument);
- }
+ if (ss == NULL || *ss == '\0')
+ return (0);
+
+ if (args_has(cs->args, 'F')) {
+ expanded = format_single(NULL, ss, NULL, NULL, NULL, wme->wp);
+ if (*expanded == '\0') {
+ free(expanded);
+ return (0);
}
+ free(data->searchstr);
+ data->searchstr = expanded;
+ } else {
+ free(data->searchstr);
+ data->searchstr = xstrdup(ss);
}
return (1);
}
int cancel)
{
struct window_mode_entry *wme = cs->wme;
- int argc = cs->args->argc;
- char **argv = cs->args->argv;
struct client *c = cs->c;
struct session *s = cs->s;
struct winlink *wl = cs->wl;
struct window_pane *wp = wme->wp;
+ u_int count = args_count(cs->args);
u_int np = wme->prefix, ocx, ocy, ooy;
struct window_copy_mode_data *data = wme->data;
char *prefix = NULL, *command = NULL;
+ const char *arg1 = args_string(cs->args, 1);
+ const char *arg2 = args_string(cs->args, 2);
if (pipe) {
- if (argc == 3)
- prefix = format_single(NULL, argv[2], c, s, wl, wp);
- if (s != NULL && argc > 1 && *argv[1] != '\0')
- command = format_single(NULL, argv[1], c, s, wl, wp);
+ if (count == 3)
+ prefix = format_single(NULL, arg2, c, s, wl, wp);
+ if (s != NULL && count > 1 && *arg1 != '\0')
+ command = format_single(NULL, arg1, c, s, wl, wp);
} else {
- if (argc == 2)
- prefix = format_single(NULL, argv[1], c, s, wl, wp);
+ if (count == 2)
+ prefix = format_single(NULL, arg1, c, s, wl, wp);
}
ocx = data->cx;
window_copy_do_copy_line(struct window_copy_cmd_state *cs, int pipe, int cancel)
{
struct window_mode_entry *wme = cs->wme;
- int argc = cs->args->argc;
- char **argv = cs->args->argv;
struct client *c = cs->c;
struct session *s = cs->s;
struct winlink *wl = cs->wl;
struct window_pane *wp = wme->wp;
struct window_copy_mode_data *data = wme->data;
+ u_int count = args_count(cs->args);
u_int np = wme->prefix, ocx, ocy, ooy;
char *prefix = NULL, *command = NULL;
+ const char *arg1 = args_string(cs->args, 1);
+ const char *arg2 = args_string(cs->args, 2);
if (pipe) {
- if (argc == 3)
- prefix = format_single(NULL, argv[2], c, s, wl, wp);
- if (s != NULL && argc > 1 && *argv[1] != '\0')
- command = format_single(NULL, argv[1], c, s, wl, wp);
+ if (count == 3)
+ prefix = format_single(NULL, arg2, c, s, wl, wp);
+ if (s != NULL && count > 1 && *arg1 != '\0')
+ command = format_single(NULL, arg1, c, s, wl, wp);
} else {
- if (argc == 2)
- prefix = format_single(NULL, argv[1], c, s, wl, wp);
+ if (count == 2)
+ prefix = format_single(NULL, arg1, c, s, wl, wp);
}
ocx = data->cx;
struct winlink *wl = cs->wl;
struct window_pane *wp = wme->wp;
char *prefix = NULL;
+ const char *arg1 = args_string(cs->args, 1);
- if (cs->args->argc == 2)
- prefix = format_single(NULL, cs->args->argv[1], c, s, wl, wp);
+ if (arg1 != NULL)
+ prefix = format_single(NULL, arg1, c, s, wl, wp);
if (s != NULL)
window_copy_copy_selection(wme, prefix);
struct session *s = cs->s;
struct winlink *wl = cs->wl;
struct window_pane *wp = wme->wp;
- char *command = NULL;
- char *prefix = NULL;
+ char *command = NULL, *prefix = NULL;
+ const char *arg1 = args_string(cs->args, 1);
+ const char *arg2 = args_string(cs->args, 2);
- if (cs->args->argc == 3)
- prefix = format_single(NULL, cs->args->argv[2], c, s, wl, wp);
+ if (arg2 != NULL)
+ prefix = format_single(NULL, arg2, c, s, wl, wp);
- if (s != NULL && cs->args->argc > 1 && *cs->args->argv[1] != '\0')
- command = format_single(NULL, cs->args->argv[1], c, s, wl, wp);
+ if (s != NULL && arg1 != NULL && *arg1 != '\0')
+ command = format_single(NULL, arg1, c, s, wl, wp);
window_copy_copy_pipe(wme, s, prefix, command);
free(command);
struct winlink *wl = cs->wl;
struct window_pane *wp = wme->wp;
char *command = NULL;
+ const char *arg1 = args_string(cs->args, 1);
- if (s != NULL && cs->args->argc > 1 && *cs->args->argv[1] != '\0')
- command = format_single(NULL, cs->args->argv[1], c, s, wl, wp);
+ if (s != NULL && arg1 != NULL && *arg1 != '\0')
+ command = format_single(NULL, arg1, c, s, wl, wp);
window_copy_pipe(wme, s, command);
free(command);
window_copy_cmd_goto_line(struct window_copy_cmd_state *cs)
{
struct window_mode_entry *wme = cs->wme;
- const char *argument = cs->args->argv[1];
+ const char *arg1 = args_string(cs->args, 1);
- if (*argument != '\0')
- window_copy_goto_line(wme, argument);
+ if (*arg1 != '\0')
+ window_copy_goto_line(wme, arg1);
return (WINDOW_COPY_CMD_NOTHING);
}
struct window_mode_entry *wme = cs->wme;
struct window_copy_mode_data *data = wme->data;
u_int np = wme->prefix;
- const char *argument = cs->args->argv[1];
+ const char *arg1 = args_string(cs->args, 1);
- if (*argument != '\0') {
+ if (*arg1 != '\0') {
data->jumptype = WINDOW_COPY_JUMPBACKWARD;
free(data->jumpchar);
- data->jumpchar = utf8_fromcstr(argument);
+ data->jumpchar = utf8_fromcstr(arg1);
for (; np != 0; np--)
window_copy_cursor_jump_back(wme);
}
struct window_mode_entry *wme = cs->wme;
struct window_copy_mode_data *data = wme->data;
u_int np = wme->prefix;
- const char *argument = cs->args->argv[1];
+ const char *arg1 = args_string(cs->args, 1);
- if (*argument != '\0') {
+ if (*arg1 != '\0') {
data->jumptype = WINDOW_COPY_JUMPFORWARD;
free(data->jumpchar);
- data->jumpchar = utf8_fromcstr(argument);
+ data->jumpchar = utf8_fromcstr(arg1);
for (; np != 0; np--)
window_copy_cursor_jump(wme);
}
struct window_mode_entry *wme = cs->wme;
struct window_copy_mode_data *data = wme->data;
u_int np = wme->prefix;
- const char *argument = cs->args->argv[1];
+ const char *arg1 = args_string(cs->args, 1);
- if (*argument != '\0') {
+ if (*arg1 != '\0') {
data->jumptype = WINDOW_COPY_JUMPTOBACKWARD;
free(data->jumpchar);
- data->jumpchar = utf8_fromcstr(argument);
+ data->jumpchar = utf8_fromcstr(arg1);
for (; np != 0; np--)
window_copy_cursor_jump_to_back(wme);
}
struct window_mode_entry *wme = cs->wme;
struct window_copy_mode_data *data = wme->data;
u_int np = wme->prefix;
- const char *argument = cs->args->argv[1];
+ const char *arg1 = args_string(cs->args, 1);
- if (*argument != '\0') {
+ if (*arg1 != '\0') {
data->jumptype = WINDOW_COPY_JUMPTOFORWARD;
free(data->jumpchar);
- data->jumpchar = utf8_fromcstr(argument);
+ data->jumpchar = utf8_fromcstr(arg1);
for (; np != 0; np--)
window_copy_cursor_jump_to(wme);
}
{
struct window_mode_entry *wme = cs->wme;
struct window_copy_mode_data *data = wme->data;
- const char *argument = cs->args->argv[1];
+ const char *arg1 = args_string(cs->args, 1);
const char *ss = data->searchstr;
char prefix;
enum window_copy_cmd_action action = WINDOW_COPY_CMD_NOTHING;
data->timeout = 0;
- log_debug("%s: %s", __func__, argument);
+ log_debug("%s: %s", __func__, arg1);
- prefix = *argument++;
+ prefix = *arg1++;
if (data->searchx == -1 || data->searchy == -1) {
data->searchx = data->cx;
data->searchy = data->cy;
data->searcho = data->oy;
- } else if (ss != NULL && strcmp(argument, ss) != 0) {
+ } else if (ss != NULL && strcmp(arg1, ss) != 0) {
data->cx = data->searchx;
data->cy = data->searchy;
data->oy = data->searcho;
action = WINDOW_COPY_CMD_REDRAW;
}
- if (*argument == '\0') {
+ if (*arg1 == '\0') {
window_copy_clear_marks(wme);
return (WINDOW_COPY_CMD_REDRAW);
}
data->searchtype = WINDOW_COPY_SEARCHUP;
data->searchregex = 0;
free(data->searchstr);
- data->searchstr = xstrdup(argument);
+ data->searchstr = xstrdup(arg1);
if (!window_copy_search_up(wme, 0)) {
window_copy_clear_marks(wme);
return (WINDOW_COPY_CMD_REDRAW);
data->searchtype = WINDOW_COPY_SEARCHDOWN;
data->searchregex = 0;
free(data->searchstr);
- data->searchstr = xstrdup(argument);
+ data->searchstr = xstrdup(arg1);
if (!window_copy_search_down(wme, 0)) {
window_copy_clear_marks(wme);
return (WINDOW_COPY_CMD_REDRAW);
{
struct window_mode_entry *wme = cs->wme;
struct window_copy_mode_data *data = wme->data;
- const char *argument = cs->args->argv[1];
+ const char *arg1 = args_string(cs->args, 1);
const char *ss = data->searchstr;
char prefix;
enum window_copy_cmd_action action = WINDOW_COPY_CMD_NOTHING;
data->timeout = 0;
- log_debug("%s: %s", __func__, argument);
+ log_debug("%s: %s", __func__, arg1);
- prefix = *argument++;
+ prefix = *arg1++;
if (data->searchx == -1 || data->searchy == -1) {
data->searchx = data->cx;
data->searchy = data->cy;
data->searcho = data->oy;
- } else if (ss != NULL && strcmp(argument, ss) != 0) {
+ } else if (ss != NULL && strcmp(arg1, ss) != 0) {
data->cx = data->searchx;
data->cy = data->searchy;
data->oy = data->searcho;
action = WINDOW_COPY_CMD_REDRAW;
}
- if (*argument == '\0') {
+ if (*arg1 == '\0') {
window_copy_clear_marks(wme);
return (WINDOW_COPY_CMD_REDRAW);
}
data->searchtype = WINDOW_COPY_SEARCHDOWN;
data->searchregex = 0;
free(data->searchstr);
- data->searchstr = xstrdup(argument);
+ data->searchstr = xstrdup(arg1);
if (!window_copy_search_down(wme, 0)) {
window_copy_clear_marks(wme);
return (WINDOW_COPY_CMD_REDRAW);
data->searchtype = WINDOW_COPY_SEARCHUP;
data->searchregex = 0;
free(data->searchstr);
- data->searchstr = xstrdup(argument);
+ data->searchstr = xstrdup(arg1);
if (!window_copy_search_up(wme, 0)) {
window_copy_clear_marks(wme);
return (WINDOW_COPY_CMD_REDRAW);
static const struct {
const char *command;
- int minargs;
- int maxargs;
+ u_int minargs;
+ u_int maxargs;
enum window_copy_cmd_clear clear;
enum window_copy_cmd_action (*f)(struct window_copy_cmd_state *);
} window_copy_cmd_table[] = {
enum window_copy_cmd_action action;
enum window_copy_cmd_clear clear = WINDOW_COPY_CMD_CLEAR_NEVER;
const char *command;
- u_int i;
+ u_int i, count = args_count(args);
int keys;
- if (args->argc == 0)
+ if (count == 0)
return;
- command = args->argv[0];
-
+ command = args_string(args, 0);
if (m != NULL && m->valid && !MOUSE_WHEEL(m->b))
window_copy_move_mouse(m);
action = WINDOW_COPY_CMD_NOTHING;
for (i = 0; i < nitems(window_copy_cmd_table); i++) {
if (strcmp(window_copy_cmd_table[i].command, command) == 0) {
- if (args->argc - 1 < window_copy_cmd_table[i].minargs ||
- args->argc - 1 > window_copy_cmd_table[i].maxargs)
+ if (count - 1 < window_copy_cmd_table[i].minargs ||
+ count - 1 > window_copy_cmd_table[i].maxargs)
break;
clear = window_copy_cmd_table[i].clear;
action = window_copy_cmd_table[i].f(&cs);
const char *separators, int already)
{
struct window_copy_mode_data *data = wme->data;
+ struct window *w = wme->wp->window;
struct screen *back_s = data->backing;
struct grid_reader gr;
u_int px, py, oldy, hsize;
int stop_at_eol;
- stop_at_eol =
- options_get_number(wme->wp->window->options, "mode-keys")
- == MODEKEY_EMACS;
+ if (options_get_number(w->options, "mode-keys") == MODEKEY_EMACS)
+ stop_at_eol = 1;
+ else
+ stop_at_eol = 0;
px = data->cx;
hsize = screen_hsize(back_s);
-/* $OpenBSD: window-tree.c,v 1.55 2021/06/10 07:50:04 nicm Exp $ */
+/* $OpenBSD: window-tree.c,v 1.56 2021/08/20 19:50:17 nicm Exp $ */
/*
* Copyright (c) 2017 Nicholas Marriott <nicholas.marriott@gmail.com>
data->key_format = xstrdup(WINDOW_TREE_DEFAULT_KEY_FORMAT);
else
data->key_format = xstrdup(args_get(args, 'K'));
- if (args == NULL || args->argc == 0)
+ if (args == NULL || args_count(args) == 0)
data->command = xstrdup(WINDOW_TREE_DEFAULT_COMMAND);
else
- data->command = xstrdup(args->argv[0]);
+ data->command = xstrdup(args_string(args, 0));
data->squash_groups = !args_has(args, 'G');
data->data = mode_tree_start(wp, args, window_tree_build,