Merge branch 'obsd-master'
[tmux.git] / cmd-queue.c
blobe188afcdbb49f1ebb1959d3b938811ce899a05f2
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2013 Nicholas Marriott <nicholas.marriott@gmail.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
21 #include <ctype.h>
22 #include <pwd.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26 #include <unistd.h>
28 #include "tmux.h"
30 /* Command queue flags. */
31 #define CMDQ_FIRED 0x1
32 #define CMDQ_WAITING 0x2
34 /* Command queue item type. */
35 enum cmdq_type {
36 CMDQ_COMMAND,
37 CMDQ_CALLBACK,
40 /* Command queue item. */
41 struct cmdq_item {
42 char *name;
43 struct cmdq_list *queue;
44 struct cmdq_item *next;
46 struct client *client;
47 struct client *target_client;
49 enum cmdq_type type;
50 u_int group;
52 u_int number;
53 time_t time;
55 int flags;
57 struct cmdq_state *state;
58 struct cmd_find_state source;
59 struct cmd_find_state target;
61 struct cmd_list *cmdlist;
62 struct cmd *cmd;
64 cmdq_cb cb;
65 void *data;
67 TAILQ_ENTRY(cmdq_item) entry;
69 TAILQ_HEAD(cmdq_item_list, cmdq_item);
72 * Command queue state. This is the context for commands on the command queue.
73 * It holds information about how the commands were fired (the key and flags),
74 * any additional formats for the commands, and the current default target.
75 * Multiple commands can share the same state and a command may update the
76 * default target.
78 struct cmdq_state {
79 int references;
80 int flags;
82 struct format_tree *formats;
84 struct key_event event;
85 struct cmd_find_state current;
88 /* Command queue. */
89 struct cmdq_list {
90 struct cmdq_item *item;
91 struct cmdq_item_list list;
94 /* Get command queue name. */
95 static const char *
96 cmdq_name(struct client *c)
98 static char s[256];
100 if (c == NULL)
101 return ("<global>");
102 if (c->name != NULL)
103 xsnprintf(s, sizeof s, "<%s>", c->name);
104 else
105 xsnprintf(s, sizeof s, "<%p>", c);
106 return (s);
109 /* Get command queue from client. */
110 static struct cmdq_list *
111 cmdq_get(struct client *c)
113 static struct cmdq_list *global_queue;
115 if (c == NULL) {
116 if (global_queue == NULL)
117 global_queue = cmdq_new();
118 return (global_queue);
120 return (c->queue);
123 /* Create a queue. */
124 struct cmdq_list *
125 cmdq_new(void)
127 struct cmdq_list *queue;
129 queue = xcalloc(1, sizeof *queue);
130 TAILQ_INIT (&queue->list);
131 return (queue);
134 /* Free a queue. */
135 void
136 cmdq_free(struct cmdq_list *queue)
138 if (!TAILQ_EMPTY(&queue->list))
139 fatalx("queue not empty");
140 free(queue);
143 /* Get item name. */
144 const char *
145 cmdq_get_name(struct cmdq_item *item)
147 return (item->name);
150 /* Get item client. */
151 struct client *
152 cmdq_get_client(struct cmdq_item *item)
154 return (item->client);
157 /* Get item target client. */
158 struct client *
159 cmdq_get_target_client(struct cmdq_item *item)
161 return (item->target_client);
164 /* Get item state. */
165 struct cmdq_state *
166 cmdq_get_state(struct cmdq_item *item)
168 return (item->state);
171 /* Get item target. */
172 struct cmd_find_state *
173 cmdq_get_target(struct cmdq_item *item)
175 return (&item->target);
178 /* Get item source. */
179 struct cmd_find_state *
180 cmdq_get_source(struct cmdq_item *item)
182 return (&item->source);
185 /* Get state event. */
186 struct key_event *
187 cmdq_get_event(struct cmdq_item *item)
189 return (&item->state->event);
192 /* Get state current target. */
193 struct cmd_find_state *
194 cmdq_get_current(struct cmdq_item *item)
196 return (&item->state->current);
199 /* Get state flags. */
201 cmdq_get_flags(struct cmdq_item *item)
203 return (item->state->flags);
206 /* Create a new state. */
207 struct cmdq_state *
208 cmdq_new_state(struct cmd_find_state *current, struct key_event *event,
209 int flags)
211 struct cmdq_state *state;
213 state = xcalloc(1, sizeof *state);
214 state->references = 1;
215 state->flags = flags;
217 if (event != NULL)
218 memcpy(&state->event, event, sizeof state->event);
219 else
220 state->event.key = KEYC_NONE;
221 if (current != NULL && cmd_find_valid_state(current))
222 cmd_find_copy_state(&state->current, current);
223 else
224 cmd_find_clear_state(&state->current, 0);
226 return (state);
229 /* Add a reference to a state. */
230 struct cmdq_state *
231 cmdq_link_state(struct cmdq_state *state)
233 state->references++;
234 return (state);
237 /* Make a copy of a state. */
238 struct cmdq_state *
239 cmdq_copy_state(struct cmdq_state *state, struct cmd_find_state *current)
241 if (current != NULL)
242 return (cmdq_new_state(current, &state->event, state->flags));
243 return (cmdq_new_state(&state->current, &state->event, state->flags));
246 /* Free a state. */
247 void
248 cmdq_free_state(struct cmdq_state *state)
250 if (--state->references != 0)
251 return;
253 if (state->formats != NULL)
254 format_free(state->formats);
255 free(state);
258 /* Add a format to command queue. */
259 void
260 cmdq_add_format(struct cmdq_state *state, const char *key, const char *fmt, ...)
262 va_list ap;
263 char *value;
265 va_start(ap, fmt);
266 xvasprintf(&value, fmt, ap);
267 va_end(ap);
269 if (state->formats == NULL)
270 state->formats = format_create(NULL, NULL, FORMAT_NONE, 0);
271 format_add(state->formats, key, "%s", value);
273 free(value);
276 /* Add formats to command queue. */
277 void
278 cmdq_add_formats(struct cmdq_state *state, struct format_tree *ft)
280 if (state->formats == NULL)
281 state->formats = format_create(NULL, NULL, FORMAT_NONE, 0);
282 format_merge(state->formats, ft);
285 /* Merge formats from item. */
286 void
287 cmdq_merge_formats(struct cmdq_item *item, struct format_tree *ft)
289 const struct cmd_entry *entry;
291 if (item->cmd != NULL) {
292 entry = cmd_get_entry(item->cmd);
293 format_add(ft, "command", "%s", entry->name);
295 if (item->state->formats != NULL)
296 format_merge(ft, item->state->formats);
299 /* Append an item. */
300 struct cmdq_item *
301 cmdq_append(struct client *c, struct cmdq_item *item)
303 struct cmdq_list *queue = cmdq_get(c);
304 struct cmdq_item *next;
306 do {
307 next = item->next;
308 item->next = NULL;
310 if (c != NULL)
311 c->references++;
312 item->client = c;
314 item->queue = queue;
315 TAILQ_INSERT_TAIL(&queue->list, item, entry);
316 log_debug("%s %s: %s", __func__, cmdq_name(c), item->name);
318 item = next;
319 } while (item != NULL);
320 return (TAILQ_LAST(&queue->list, cmdq_item_list));
323 /* Insert an item. */
324 struct cmdq_item *
325 cmdq_insert_after(struct cmdq_item *after, struct cmdq_item *item)
327 struct client *c = after->client;
328 struct cmdq_list *queue = after->queue;
329 struct cmdq_item *next;
331 do {
332 next = item->next;
333 item->next = after->next;
334 after->next = item;
336 if (c != NULL)
337 c->references++;
338 item->client = c;
340 item->queue = queue;
341 TAILQ_INSERT_AFTER(&queue->list, after, item, entry);
342 log_debug("%s %s: %s after %s", __func__, cmdq_name(c),
343 item->name, after->name);
345 after = item;
346 item = next;
347 } while (item != NULL);
348 return (after);
351 /* Insert a hook. */
352 void
353 cmdq_insert_hook(struct session *s, struct cmdq_item *item,
354 struct cmd_find_state *current, const char *fmt, ...)
356 struct cmdq_state *state = item->state;
357 struct cmd *cmd = item->cmd;
358 struct args *args = cmd_get_args(cmd);
359 struct args_entry *ae;
360 struct args_value *av;
361 struct options *oo;
362 va_list ap;
363 char *name, tmp[32], flag, *arguments;
364 u_int i;
365 const char *value;
366 struct cmdq_item *new_item;
367 struct cmdq_state *new_state;
368 struct options_entry *o;
369 struct options_array_item *a;
370 struct cmd_list *cmdlist;
372 if (item->state->flags & CMDQ_STATE_NOHOOKS)
373 return;
374 if (s == NULL)
375 oo = global_s_options;
376 else
377 oo = s->options;
379 va_start(ap, fmt);
380 xvasprintf(&name, fmt, ap);
381 va_end(ap);
383 o = options_get(oo, name);
384 if (o == NULL) {
385 free(name);
386 return;
388 log_debug("running hook %s (parent %p)", name, item);
391 * The hooks get a new state because they should not update the current
392 * target or formats for any subsequent commands.
394 new_state = cmdq_new_state(current, &state->event, CMDQ_STATE_NOHOOKS);
395 cmdq_add_format(new_state, "hook", "%s", name);
397 arguments = args_print(args);
398 cmdq_add_format(new_state, "hook_arguments", "%s", arguments);
399 free(arguments);
401 for (i = 0; i < args_count(args); i++) {
402 xsnprintf(tmp, sizeof tmp, "hook_argument_%d", i);
403 cmdq_add_format(new_state, tmp, "%s", args_string(args, i));
405 flag = args_first(args, &ae);
406 while (flag != 0) {
407 value = args_get(args, flag);
408 if (value == NULL) {
409 xsnprintf(tmp, sizeof tmp, "hook_flag_%c", flag);
410 cmdq_add_format(new_state, tmp, "1");
411 } else {
412 xsnprintf(tmp, sizeof tmp, "hook_flag_%c", flag);
413 cmdq_add_format(new_state, tmp, "%s", value);
416 i = 0;
417 av = args_first_value(args, flag);
418 while (av != NULL) {
419 xsnprintf(tmp, sizeof tmp, "hook_flag_%c_%d", flag, i);
420 cmdq_add_format(new_state, tmp, "%s", av->string);
421 i++;
422 av = args_next_value(av);
425 flag = args_next(&ae);
428 a = options_array_first(o);
429 while (a != NULL) {
430 cmdlist = options_array_item_value(a)->cmdlist;
431 if (cmdlist != NULL) {
432 new_item = cmdq_get_command(cmdlist, new_state);
433 if (item != NULL)
434 item = cmdq_insert_after(item, new_item);
435 else
436 item = cmdq_append(NULL, new_item);
438 a = options_array_next(a);
441 cmdq_free_state(new_state);
442 free(name);
445 /* Continue processing command queue. */
446 void
447 cmdq_continue(struct cmdq_item *item)
449 item->flags &= ~CMDQ_WAITING;
452 /* Remove an item. */
453 static void
454 cmdq_remove(struct cmdq_item *item)
456 if (item->client != NULL)
457 server_client_unref(item->client);
458 if (item->cmdlist != NULL)
459 cmd_list_free(item->cmdlist);
460 cmdq_free_state(item->state);
462 TAILQ_REMOVE(&item->queue->list, item, entry);
464 free(item->name);
465 free(item);
468 /* Remove all subsequent items that match this item's group. */
469 static void
470 cmdq_remove_group(struct cmdq_item *item)
472 struct cmdq_item *this, *next;
474 if (item->group == 0)
475 return;
476 this = TAILQ_NEXT(item, entry);
477 while (this != NULL) {
478 next = TAILQ_NEXT(this, entry);
479 if (this->group == item->group)
480 cmdq_remove(this);
481 this = next;
485 /* Empty command callback. */
486 static enum cmd_retval
487 cmdq_empty_command(__unused struct cmdq_item *item, __unused void *data)
489 return (CMD_RETURN_NORMAL);
492 /* Get a command for the command queue. */
493 struct cmdq_item *
494 cmdq_get_command(struct cmd_list *cmdlist, struct cmdq_state *state)
496 struct cmdq_item *item, *first = NULL, *last = NULL;
497 struct cmd *cmd;
498 const struct cmd_entry *entry;
499 int created = 0;
501 if ((cmd = cmd_list_first(cmdlist)) == NULL)
502 return (cmdq_get_callback(cmdq_empty_command, NULL));
504 if (state == NULL) {
505 state = cmdq_new_state(NULL, NULL, 0);
506 created = 1;
509 while (cmd != NULL) {
510 entry = cmd_get_entry(cmd);
512 item = xcalloc(1, sizeof *item);
513 xasprintf(&item->name, "[%s/%p]", entry->name, item);
514 item->type = CMDQ_COMMAND;
516 item->group = cmd_get_group(cmd);
517 item->state = cmdq_link_state(state);
519 item->cmdlist = cmdlist;
520 item->cmd = cmd;
522 cmdlist->references++;
523 log_debug("%s: %s group %u", __func__, item->name, item->group);
525 if (first == NULL)
526 first = item;
527 if (last != NULL)
528 last->next = item;
529 last = item;
531 cmd = cmd_list_next(cmd);
534 if (created)
535 cmdq_free_state(state);
536 return (first);
539 /* Fill in flag for a command. */
540 static enum cmd_retval
541 cmdq_find_flag(struct cmdq_item *item, struct cmd_find_state *fs,
542 const struct cmd_entry_flag *flag)
544 const char *value;
546 if (flag->flag == 0) {
547 cmd_find_from_client(fs, item->target_client, 0);
548 return (CMD_RETURN_NORMAL);
551 value = args_get(cmd_get_args(item->cmd), flag->flag);
552 if (cmd_find_target(fs, item, value, flag->type, flag->flags) != 0) {
553 cmd_find_clear_state(fs, 0);
554 return (CMD_RETURN_ERROR);
556 return (CMD_RETURN_NORMAL);
559 /* Add message with command. */
560 static void
561 cmdq_add_message(struct cmdq_item *item)
563 struct client *c = item->client;
564 struct cmdq_state *state = item->state;
565 const char *key;
566 char *tmp;
567 uid_t uid;
568 struct passwd *pw;
569 char *user = NULL;
571 tmp = cmd_print(item->cmd);
572 if (c != NULL) {
573 uid = proc_get_peer_uid(c->peer);
574 if (uid != (uid_t)-1 && uid != getuid()) {
575 if ((pw = getpwuid(uid)) != NULL)
576 xasprintf(&user, "[%s]", pw->pw_name);
577 else
578 user = xstrdup("[unknown]");
579 } else
580 user = xstrdup("");
581 if (c->session != NULL && state->event.key != KEYC_NONE) {
582 key = key_string_lookup_key(state->event.key, 0);
583 server_add_message("%s%s key %s: %s", c->name, user,
584 key, tmp);
585 } else {
586 server_add_message("%s%s command: %s", c->name, user,
587 tmp);
589 free(user);
590 } else
591 server_add_message("command: %s", tmp);
592 free(tmp);
595 /* Fire command on command queue. */
596 static enum cmd_retval
597 cmdq_fire_command(struct cmdq_item *item)
599 const char *name = cmdq_name(item->client);
600 struct cmdq_state *state = item->state;
601 struct cmd *cmd = item->cmd;
602 struct args *args = cmd_get_args(cmd);
603 const struct cmd_entry *entry = cmd_get_entry(cmd);
604 struct client *tc, *saved = item->client;
605 enum cmd_retval retval;
606 struct cmd_find_state *fsp, fs;
607 int flags, quiet = 0;
608 char *tmp;
610 if (cfg_finished)
611 cmdq_add_message(item);
612 if (log_get_level() > 1) {
613 tmp = cmd_print(cmd);
614 log_debug("%s %s: (%u) %s", __func__, name, item->group, tmp);
615 free(tmp);
618 flags = !!(state->flags & CMDQ_STATE_CONTROL);
619 cmdq_guard(item, "begin", flags);
621 if (item->client == NULL)
622 item->client = cmd_find_client(item, NULL, 1);
624 if (entry->flags & CMD_CLIENT_CANFAIL)
625 quiet = 1;
626 if (entry->flags & CMD_CLIENT_CFLAG) {
627 tc = cmd_find_client(item, args_get(args, 'c'), quiet);
628 if (tc == NULL && !quiet) {
629 retval = CMD_RETURN_ERROR;
630 goto out;
632 } else if (entry->flags & CMD_CLIENT_TFLAG) {
633 tc = cmd_find_client(item, args_get(args, 't'), quiet);
634 if (tc == NULL && !quiet) {
635 retval = CMD_RETURN_ERROR;
636 goto out;
638 } else
639 tc = cmd_find_client(item, NULL, 1);
640 item->target_client = tc;
642 retval = cmdq_find_flag(item, &item->source, &entry->source);
643 if (retval == CMD_RETURN_ERROR)
644 goto out;
645 retval = cmdq_find_flag(item, &item->target, &entry->target);
646 if (retval == CMD_RETURN_ERROR)
647 goto out;
649 retval = entry->exec(cmd, item);
650 if (retval == CMD_RETURN_ERROR)
651 goto out;
653 if (entry->flags & CMD_AFTERHOOK) {
654 if (cmd_find_valid_state(&item->target))
655 fsp = &item->target;
656 else if (cmd_find_valid_state(&item->state->current))
657 fsp = &item->state->current;
658 else if (cmd_find_from_client(&fs, item->client, 0) == 0)
659 fsp = &fs;
660 else
661 goto out;
662 cmdq_insert_hook(fsp->s, item, fsp, "after-%s", entry->name);
665 out:
666 item->client = saved;
667 if (retval == CMD_RETURN_ERROR)
668 cmdq_guard(item, "error", flags);
669 else
670 cmdq_guard(item, "end", flags);
671 return (retval);
674 /* Get a callback for the command queue. */
675 struct cmdq_item *
676 cmdq_get_callback1(const char *name, cmdq_cb cb, void *data)
678 struct cmdq_item *item;
680 item = xcalloc(1, sizeof *item);
681 xasprintf(&item->name, "[%s/%p]", name, item);
682 item->type = CMDQ_CALLBACK;
684 item->group = 0;
685 item->state = cmdq_new_state(NULL, NULL, 0);
687 item->cb = cb;
688 item->data = data;
690 return (item);
693 /* Generic error callback. */
694 static enum cmd_retval
695 cmdq_error_callback(struct cmdq_item *item, void *data)
697 char *error = data;
699 cmdq_error(item, "%s", error);
700 free(error);
702 return (CMD_RETURN_NORMAL);
705 /* Get an error callback for the command queue. */
706 struct cmdq_item *
707 cmdq_get_error(const char *error)
709 return (cmdq_get_callback(cmdq_error_callback, xstrdup(error)));
712 /* Fire callback on callback queue. */
713 static enum cmd_retval
714 cmdq_fire_callback(struct cmdq_item *item)
716 return (item->cb(item, item->data));
719 /* Process next item on command queue. */
720 u_int
721 cmdq_next(struct client *c)
723 struct cmdq_list *queue = cmdq_get(c);
724 const char *name = cmdq_name(c);
725 struct cmdq_item *item;
726 enum cmd_retval retval;
727 u_int items = 0;
728 static u_int number;
730 if (TAILQ_EMPTY(&queue->list)) {
731 log_debug("%s %s: empty", __func__, name);
732 return (0);
734 if (TAILQ_FIRST(&queue->list)->flags & CMDQ_WAITING) {
735 log_debug("%s %s: waiting", __func__, name);
736 return (0);
739 log_debug("%s %s: enter", __func__, name);
740 for (;;) {
741 item = queue->item = TAILQ_FIRST(&queue->list);
742 if (item == NULL)
743 break;
744 log_debug("%s %s: %s (%d), flags %x", __func__, name,
745 item->name, item->type, item->flags);
748 * Any item with the waiting flag set waits until an external
749 * event clears the flag (for example, a job - look at
750 * run-shell).
752 if (item->flags & CMDQ_WAITING)
753 goto waiting;
756 * Items are only fired once, once the fired flag is set, a
757 * waiting flag can only be cleared by an external event.
759 if (~item->flags & CMDQ_FIRED) {
760 item->time = time(NULL);
761 item->number = ++number;
763 switch (item->type) {
764 case CMDQ_COMMAND:
765 retval = cmdq_fire_command(item);
768 * If a command returns an error, remove any
769 * subsequent commands in the same group.
771 if (retval == CMD_RETURN_ERROR)
772 cmdq_remove_group(item);
773 break;
774 case CMDQ_CALLBACK:
775 retval = cmdq_fire_callback(item);
776 break;
777 default:
778 retval = CMD_RETURN_ERROR;
779 break;
781 item->flags |= CMDQ_FIRED;
783 if (retval == CMD_RETURN_WAIT) {
784 item->flags |= CMDQ_WAITING;
785 goto waiting;
787 items++;
789 cmdq_remove(item);
791 queue->item = NULL;
793 log_debug("%s %s: exit (empty)", __func__, name);
794 return (items);
796 waiting:
797 log_debug("%s %s: exit (wait)", __func__, name);
798 return (items);
801 /* Get running item if any. */
802 struct cmdq_item *
803 cmdq_running(struct client *c)
805 struct cmdq_list *queue = cmdq_get(c);
807 if (queue->item == NULL)
808 return (NULL);
809 if (queue->item->flags & CMDQ_WAITING)
810 return (NULL);
811 return (queue->item);
814 /* Print a guard line. */
815 void
816 cmdq_guard(struct cmdq_item *item, const char *guard, int flags)
818 struct client *c = item->client;
819 long t = item->time;
820 u_int number = item->number;
822 if (c != NULL && (c->flags & CLIENT_CONTROL))
823 control_write(c, "%%%s %ld %u %d", guard, t, number, flags);
826 /* Show message from command. */
827 void
828 cmdq_print_data(struct cmdq_item *item, int parse, struct evbuffer *evb)
830 server_client_print(item->client, parse, evb);
833 /* Show message from command. */
834 void
835 cmdq_print(struct cmdq_item *item, const char *fmt, ...)
837 va_list ap;
838 struct evbuffer *evb;
840 evb = evbuffer_new();
841 if (evb == NULL)
842 fatalx("out of memory");
844 va_start(ap, fmt);
845 evbuffer_add_vprintf(evb, fmt, ap);
846 va_end(ap);
848 cmdq_print_data(item, 0, evb);
849 evbuffer_free(evb);
852 /* Show error from command. */
853 void
854 cmdq_error(struct cmdq_item *item, const char *fmt, ...)
856 struct client *c = item->client;
857 struct cmd *cmd = item->cmd;
858 va_list ap;
859 char *msg, *tmp;
860 const char *file;
861 u_int line;
863 va_start(ap, fmt);
864 xvasprintf(&msg, fmt, ap);
865 va_end(ap);
867 log_debug("%s: %s", __func__, msg);
869 if (c == NULL) {
870 cmd_get_source(cmd, &file, &line);
871 cfg_add_cause("%s:%u: %s", file, line, msg);
872 } else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
873 server_add_message("%s message: %s", c->name, msg);
874 if (~c->flags & CLIENT_UTF8) {
875 tmp = msg;
876 msg = utf8_sanitize(tmp);
877 free(tmp);
879 if (c->flags & CLIENT_CONTROL)
880 control_write(c, "%s", msg);
881 else
882 file_error(c, "%s\n", msg);
883 c->retval = 1;
884 } else {
885 *msg = toupper((u_char) *msg);
886 status_message_set(c, -1, 1, 0, "%s", msg);
889 free(msg);