Add formats for after hook command arguments.

This commit is contained in:
nicm 2020-05-16 15:40:04 +00:00
parent d056144aa1
commit beb214bcb3
3 changed files with 78 additions and 17 deletions

View File

@ -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);
}

View File

@ -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;

2
tmux.h
View File

@ -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,