diff options
author | Nicholas Marriott <nicm@cvs.openbsd.org> | 2020-05-16 15:40:05 +0000 |
---|---|---|
committer | Nicholas Marriott <nicm@cvs.openbsd.org> | 2020-05-16 15:40:05 +0000 |
commit | 04b60dccecdc5b6aeb47eb6021b44be3a851d734 (patch) | |
tree | aa3343570857896ed1e42ddd5ceb89ad9d29a642 /usr.bin | |
parent | 5c889ad6cabb9b2dd48cdb05847817557bd29170 (diff) |
Add formats for after hook command arguments.
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/tmux/arguments.c | 56 | ||||
-rw-r--r-- | usr.bin/tmux/cmd-queue.c | 41 | ||||
-rw-r--r-- | usr.bin/tmux/tmux.h | 4 |
3 files changed, 81 insertions, 20 deletions
diff --git a/usr.bin/tmux/arguments.c b/usr.bin/tmux/arguments.c index 39ebd0d4add..24a70ee1b5e 100644 --- a/usr.bin/tmux/arguments.c +++ b/usr.bin/tmux/arguments.c @@ -1,4 +1,4 @@ -/* $OpenBSD: arguments.c,v 1.31 2020/04/22 06:57:13 nicm Exp $ */ +/* $OpenBSD: arguments.c,v 1.32 2020/05/16 15:40:04 nicm Exp $ */ /* * Copyright (c) 2010 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -56,11 +56,11 @@ args_cmp(struct args_entry *a1, struct args_entry *a2) /* Find a flag in the arguments tree. */ static struct args_entry * -args_find(struct args *args, u_char ch) +args_find(struct args *args, u_char flag) { struct args_entry entry; - entry.flag = ch; + entry.flag = flag; return (RB_FIND(args_tree, &args->tree, &entry)); } @@ -249,11 +249,11 @@ args_escape(const char *s) /* Return if an argument is present. */ int -args_has(struct args *args, u_char ch) +args_has(struct args *args, u_char flag) { struct args_entry *entry; - entry = args_find(args, ch); + entry = args_find(args, flag); if (entry == NULL) return (0); return (entry->count); @@ -261,15 +261,15 @@ args_has(struct args *args, u_char ch) /* Set argument value in the arguments tree. */ void -args_set(struct args *args, u_char ch, const char *s) +args_set(struct args *args, u_char flag, const char *s) { struct args_entry *entry; struct args_value *value; - entry = args_find(args, ch); + entry = args_find(args, flag); if (entry == NULL) { entry = xcalloc(1, sizeof *entry); - entry->flag = ch; + entry->flag = flag; entry->count = 1; TAILQ_INIT(&entry->values); RB_INSERT(args_tree, &args->tree, entry); @@ -285,22 +285,44 @@ args_set(struct args *args, u_char ch, const char *s) /* Get argument value. Will be NULL if it isn't present. */ const char * -args_get(struct args *args, u_char ch) +args_get(struct args *args, u_char flag) { struct args_entry *entry; - if ((entry = args_find(args, ch)) == NULL) + if ((entry = args_find(args, flag)) == NULL) + return (NULL); + if (TAILQ_EMPTY(&entry->values)) return (NULL); return (TAILQ_LAST(&entry->values, args_values)->value); } +/* Get first argument. */ +u_char +args_first(struct args *args, struct args_entry **entry) +{ + *entry = RB_MIN(args_tree, &args->tree); + if (*entry == NULL) + return (0); + return ((*entry)->flag); +} + +/* Get next argument. */ +u_char +args_next(struct args_entry **entry) +{ + *entry = RB_NEXT(args_tree, &args->tree, *entry); + if (*entry == NULL) + return (0); + return ((*entry)->flag); +} + /* Get first value in argument. */ const char * -args_first_value(struct args *args, u_char ch, struct args_value **value) +args_first_value(struct args *args, u_char flag, struct args_value **value) { struct args_entry *entry; - if ((entry = args_find(args, ch)) == NULL) + if ((entry = args_find(args, flag)) == NULL) return (NULL); *value = TAILQ_FIRST(&entry->values); @@ -323,15 +345,15 @@ args_next_value(struct args_value **value) /* Convert an argument value to a number. */ long long -args_strtonum(struct args *args, u_char ch, long long minval, long long maxval, - char **cause) +args_strtonum(struct args *args, u_char flag, long long minval, + long long maxval, char **cause) { const char *errstr; long long ll; struct args_entry *entry; struct args_value *value; - if ((entry = args_find(args, ch)) == NULL) { + if ((entry = args_find(args, flag)) == NULL) { *cause = xstrdup("missing"); return (0); } @@ -349,13 +371,13 @@ args_strtonum(struct args *args, u_char ch, long long minval, long long maxval, /* Convert an argument to a number which may be a percentage. */ long long -args_percentage(struct args *args, u_char ch, long long minval, +args_percentage(struct args *args, u_char flag, long long minval, long long maxval, long long curval, char **cause) { const char *value; struct args_entry *entry; - if ((entry = args_find(args, ch)) == NULL) { + if ((entry = args_find(args, flag)) == NULL) { *cause = xstrdup("missing"); return (0); } diff --git a/usr.bin/tmux/cmd-queue.c b/usr.bin/tmux/cmd-queue.c index 0c6944474c3..a1f8564534f 100644 --- a/usr.bin/tmux/cmd-queue.c +++ b/usr.bin/tmux/cmd-queue.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-queue.c,v 1.91 2020/04/23 05:48:42 nicm Exp $ */ +/* $OpenBSD: cmd-queue.c,v 1.92 2020/05/16 15:40:04 nicm Exp $ */ /* * Copyright (c) 2013 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -341,9 +341,15 @@ cmdq_insert_hook(struct session *s, struct cmdq_item *item, struct cmd_find_state *current, const char *fmt, ...) { struct cmdq_state *state = item->state; + struct cmd *cmd = item->cmd; + struct args *args = cmd_get_args(cmd); + struct args_entry *entryp; + struct args_value *valuep; struct options *oo; va_list ap; - char *name; + char *name, tmp[32], flag, *arguments; + int i; + const char *value; struct cmdq_item *new_item; struct cmdq_state *new_state; struct options_entry *o; @@ -375,6 +381,37 @@ cmdq_insert_hook(struct session *s, struct cmdq_item *item, new_state = cmdq_new_state(current, &state->event, CMDQ_STATE_NOHOOKS); cmdq_add_format(new_state, "hook", "%s", name); + arguments = args_print(args); + cmdq_add_format(new_state, "hook_arguments", "%s", arguments); + free(arguments); + + for (i = 0; i < args->argc; i++) { + xsnprintf(tmp, sizeof tmp, "hook_argument_%d", i); + cmdq_add_format(new_state, tmp, "%s", args->argv[i]); + } + flag = args_first(args, &entryp); + while (flag != 0) { + value = args_get(args, flag); + if (value == NULL) { + xsnprintf(tmp, sizeof tmp, "hook_flag_%c", flag); + cmdq_add_format(new_state, tmp, "1"); + } else { + xsnprintf(tmp, sizeof tmp, "hook_flag_%c", flag); + cmdq_add_format(new_state, tmp, "%s", value); + } + + i = 0; + value = args_first_value(args, flag, &valuep); + while (value != NULL) { + xsnprintf(tmp, sizeof tmp, "hook_flag_%c_%d", flag, i); + cmdq_add_format(new_state, tmp, "%s", value); + i++; + value = args_next_value(&valuep); + } + + flag = args_next(&entryp); + } + a = options_array_first(o); while (a != NULL) { cmdlist = options_array_item_value(a)->cmdlist; diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index 578c7cd0398..d0a1b5a251f 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.1031 2020/05/16 15:34:08 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.1032 2020/05/16 15:40:04 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> @@ -2088,6 +2088,8 @@ char *args_print(struct args *); char *args_escape(const char *); int args_has(struct args *, u_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 **); const char *args_first_value(struct args *, u_char, struct args_value **); const char *args_next_value(struct args_value **); long long args_strtonum(struct args *, u_char, long long, long long, |