From 9991a14e8105b0b8e6f2baaec3ed50d1fb54f009 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Tue, 5 May 2020 06:19:29 +0100 Subject: [PATCH] Add formats for after hook command arguments. --- arguments.c | 54 +++++++++++++++++++++++++++++++++++++---------------- cmd-queue.c | 39 +++++++++++++++++++++++++++++++++++++- tmux.h | 2 ++ 3 files changed, 78 insertions(+), 17 deletions(-) diff --git a/arguments.c b/arguments.c index e1956ace..962f3c6a 100644 --- a/arguments.c +++ b/arguments.c @@ -55,11 +55,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)); } @@ -248,11 +248,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); @@ -260,15 +260,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); @@ -284,22 +284,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); @@ -322,15 +344,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); } @@ -348,13 +370,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/cmd-queue.c b/cmd-queue.c index 59f86c64..620a3e93 100644 --- a/cmd-queue.c +++ b/cmd-queue.c @@ -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/tmux.h b/tmux.h index a1f6d924..57c8da17 100644 --- a/tmux.h +++ b/tmux.h @@ -2090,6 +2090,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,