1 /* $OpenBSD: cmd-queue.c,v 1.90 2020/04/14 06:00:52 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2013 Nicholas Marriott <nicholas.marriott@gmail.com> 5 * 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. 9 * 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. 17 */ 18 19 #include <sys/types.h> 20 21 #include <ctype.h> 22 #include <stdlib.h> 23 #include <string.h> 24 #include <time.h> 25 26 #include "tmux.h" 27 28 /* Command queue flags. */ 29 #define CMDQ_FIRED 0x1 30 #define CMDQ_WAITING 0x2 31 32 /* Command queue item type. */ 33 enum cmdq_type { 34 CMDQ_COMMAND, 35 CMDQ_CALLBACK, 36 }; 37 38 /* Command queue item. */ 39 struct cmdq_item { 40 char *name; 41 struct cmdq_list *queue; 42 struct cmdq_item *next; 43 44 struct client *client; 45 struct client *target_client; 46 47 enum cmdq_type type; 48 u_int group; 49 50 u_int number; 51 time_t time; 52 53 int flags; 54 55 struct cmdq_state *state; 56 struct cmd_find_state source; 57 struct cmd_find_state target; 58 59 struct cmd_list *cmdlist; 60 struct cmd *cmd; 61 62 cmdq_cb cb; 63 void *data; 64 65 TAILQ_ENTRY(cmdq_item) entry; 66 }; 67 TAILQ_HEAD(cmdq_item_list, cmdq_item); 68 69 /* 70 * Command queue state. This is the context for commands on the command queue. 71 * It holds information about how the commands were fired (the key and flags), 72 * any additional formats for the commands, and the current default target. 73 * Multiple commands can share the same state and a command may update the 74 * default target. 75 */ 76 struct cmdq_state { 77 int references; 78 int flags; 79 80 struct format_tree *formats; 81 82 struct key_event event; 83 struct cmd_find_state current; 84 }; 85 86 /* Command queue. */ 87 struct cmdq_list { 88 struct cmdq_item *item; 89 struct cmdq_item_list list; 90 }; 91 92 /* Get command queue name. */ 93 static const char * 94 cmdq_name(struct client *c) 95 { 96 static char s[256]; 97 98 if (c == NULL) 99 return ("<global>"); 100 if (c->name != NULL) 101 xsnprintf(s, sizeof s, "<%s>", c->name); 102 else 103 xsnprintf(s, sizeof s, "<%p>", c); 104 return (s); 105 } 106 107 /* Get command queue from client. */ 108 static struct cmdq_list * 109 cmdq_get(struct client *c) 110 { 111 static struct cmdq_list *global_queue; 112 113 if (c == NULL) { 114 if (global_queue == NULL) 115 global_queue = cmdq_new(); 116 return (global_queue); 117 } 118 return (c->queue); 119 } 120 121 /* Create a queue. */ 122 struct cmdq_list * 123 cmdq_new(void) 124 { 125 struct cmdq_list *queue; 126 127 queue = xcalloc (1, sizeof *queue); 128 TAILQ_INIT (&queue->list); 129 return (queue); 130 } 131 132 /* Free a queue. */ 133 void 134 cmdq_free(struct cmdq_list *queue) 135 { 136 if (!TAILQ_EMPTY(&queue->list)) 137 fatalx("queue not empty"); 138 free(queue); 139 } 140 141 /* Get item name. */ 142 const char * 143 cmdq_get_name(struct cmdq_item *item) 144 { 145 return (item->name); 146 } 147 148 /* Get item client. */ 149 struct client * 150 cmdq_get_client(struct cmdq_item *item) 151 { 152 return (item->client); 153 } 154 155 /* Get item target client. */ 156 struct client * 157 cmdq_get_target_client(struct cmdq_item *item) 158 { 159 return (item->target_client); 160 } 161 162 /* Get item state. */ 163 struct cmdq_state * 164 cmdq_get_state(struct cmdq_item *item) 165 { 166 return (item->state); 167 } 168 169 /* Get item target. */ 170 struct cmd_find_state * 171 cmdq_get_target(struct cmdq_item *item) 172 { 173 return (&item->target); 174 } 175 176 /* Get item source. */ 177 struct cmd_find_state * 178 cmdq_get_source(struct cmdq_item *item) 179 { 180 return (&item->source); 181 } 182 183 /* Get state event. */ 184 struct key_event * 185 cmdq_get_event(struct cmdq_item *item) 186 { 187 return (&item->state->event); 188 } 189 190 /* Get state current target. */ 191 struct cmd_find_state * 192 cmdq_get_current(struct cmdq_item *item) 193 { 194 return (&item->state->current); 195 } 196 197 /* Get state flags. */ 198 int 199 cmdq_get_flags(struct cmdq_item *item) 200 { 201 return (item->state->flags); 202 } 203 204 /* Create a new state. */ 205 struct cmdq_state * 206 cmdq_new_state(struct cmd_find_state *current, struct key_event *event, 207 int flags) 208 { 209 struct cmdq_state *state; 210 211 state = xcalloc(1, sizeof *state); 212 state->references = 1; 213 state->flags = flags; 214 215 if (event != NULL) 216 memcpy(&state->event, event, sizeof state->event); 217 else 218 state->event.key = KEYC_NONE; 219 if (current != NULL && cmd_find_valid_state(current)) 220 cmd_find_copy_state(&state->current, current); 221 else 222 cmd_find_clear_state(&state->current, 0); 223 224 return (state); 225 } 226 227 /* Add a reference to a state. */ 228 struct cmdq_state * 229 cmdq_link_state(struct cmdq_state *state) 230 { 231 state->references++; 232 return (state); 233 } 234 235 /* Make a copy of a state. */ 236 struct cmdq_state * 237 cmdq_copy_state(struct cmdq_state *state) 238 { 239 return (cmdq_new_state(&state->current, &state->event, state->flags)); 240 } 241 242 /* Free a state. */ 243 void 244 cmdq_free_state(struct cmdq_state *state) 245 { 246 if (--state->references == 0) 247 free(state); 248 } 249 250 /* Add a format to command queue. */ 251 void 252 cmdq_add_format(struct cmdq_state *state, const char *key, const char *fmt, ...) 253 { 254 va_list ap; 255 char *value; 256 257 va_start(ap, fmt); 258 xvasprintf(&value, fmt, ap); 259 va_end(ap); 260 261 if (state->formats == NULL) 262 state->formats = format_create(NULL, NULL, FORMAT_NONE, 0); 263 format_add(state->formats, key, "%s", value); 264 265 free(value); 266 } 267 268 /* Merge formats from item. */ 269 void 270 cmdq_merge_formats(struct cmdq_item *item, struct format_tree *ft) 271 { 272 const struct cmd_entry *entry; 273 274 if (item->cmd != NULL) { 275 entry = cmd_get_entry (item->cmd); 276 format_add(ft, "command", "%s", entry->name); 277 } 278 if (item->state->formats != NULL) 279 format_merge(ft, item->state->formats); 280 } 281 282 /* Append an item. */ 283 struct cmdq_item * 284 cmdq_append(struct client *c, struct cmdq_item *item) 285 { 286 struct cmdq_list *queue = cmdq_get(c); 287 struct cmdq_item *next; 288 289 do { 290 next = item->next; 291 item->next = NULL; 292 293 if (c != NULL) 294 c->references++; 295 item->client = c; 296 297 item->queue = queue; 298 TAILQ_INSERT_TAIL(&queue->list, item, entry); 299 log_debug("%s %s: %s", __func__, cmdq_name(c), item->name); 300 301 item = next; 302 } while (item != NULL); 303 return (TAILQ_LAST(&queue->list, cmdq_item_list)); 304 } 305 306 /* Insert an item. */ 307 struct cmdq_item * 308 cmdq_insert_after(struct cmdq_item *after, struct cmdq_item *item) 309 { 310 struct client *c = after->client; 311 struct cmdq_list *queue = after->queue; 312 struct cmdq_item *next; 313 314 do { 315 next = item->next; 316 item->next = after->next; 317 after->next = item; 318 319 if (c != NULL) 320 c->references++; 321 item->client = c; 322 323 item->queue = queue; 324 TAILQ_INSERT_AFTER(&queue->list, after, item, entry); 325 log_debug("%s %s: %s after %s", __func__, cmdq_name(c), 326 item->name, after->name); 327 328 after = item; 329 item = next; 330 } while (item != NULL); 331 return (after); 332 } 333 334 /* Insert a hook. */ 335 void 336 cmdq_insert_hook(struct session *s, struct cmdq_item *item, 337 struct cmd_find_state *current, const char *fmt, ...) 338 { 339 struct cmdq_state *state = item->state; 340 struct options *oo; 341 va_list ap; 342 char *name; 343 struct cmdq_item *new_item; 344 struct cmdq_state *new_state; 345 struct options_entry *o; 346 struct options_array_item *a; 347 struct cmd_list *cmdlist; 348 349 if (item->state->flags & CMDQ_STATE_NOHOOKS) 350 return; 351 if (s == NULL) 352 oo = global_s_options; 353 else 354 oo = s->options; 355 356 va_start(ap, fmt); 357 xvasprintf(&name, fmt, ap); 358 va_end(ap); 359 360 o = options_get(oo, name); 361 if (o == NULL) { 362 free(name); 363 return; 364 } 365 log_debug("running hook %s (parent %p)", name, item); 366 367 /* 368 * The hooks get a new state because they should not update the current 369 * target or formats for any subsequent commands. 370 */ 371 new_state = cmdq_new_state(current, &state->event, CMDQ_STATE_NOHOOKS); 372 cmdq_add_format(new_state, "hook", "%s", name); 373 374 a = options_array_first(o); 375 while (a != NULL) { 376 cmdlist = options_array_item_value(a)->cmdlist; 377 if (cmdlist != NULL) { 378 new_item = cmdq_get_command(cmdlist, new_state); 379 if (item != NULL) 380 item = cmdq_insert_after(item, new_item); 381 else 382 item = cmdq_append(NULL, new_item); 383 } 384 a = options_array_next(a); 385 } 386 387 cmdq_free_state(new_state); 388 free(name); 389 } 390 391 /* Continue processing command queue. */ 392 void 393 cmdq_continue(struct cmdq_item *item) 394 { 395 item->flags &= ~CMDQ_WAITING; 396 } 397 398 /* Remove an item. */ 399 static void 400 cmdq_remove(struct cmdq_item *item) 401 { 402 if (item->client != NULL) 403 server_client_unref(item->client); 404 if (item->cmdlist != NULL) 405 cmd_list_free(item->cmdlist); 406 cmdq_free_state(item->state); 407 408 TAILQ_REMOVE(&item->queue->list, item, entry); 409 410 free(item->name); 411 free(item); 412 } 413 414 /* Remove all subsequent items that match this item's group. */ 415 static void 416 cmdq_remove_group(struct cmdq_item *item) 417 { 418 struct cmdq_item *this, *next; 419 420 if (item->group == 0) 421 return; 422 this = TAILQ_NEXT(item, entry); 423 while (this != NULL) { 424 next = TAILQ_NEXT(this, entry); 425 if (this->group == item->group) 426 cmdq_remove(this); 427 this = next; 428 } 429 } 430 431 /* Get a command for the command queue. */ 432 struct cmdq_item * 433 cmdq_get_command(struct cmd_list *cmdlist, struct cmdq_state *state) 434 { 435 struct cmdq_item *item, *first = NULL, *last = NULL; 436 struct cmd *cmd; 437 const struct cmd_entry *entry; 438 int created = 0; 439 440 if (state == NULL) { 441 state = cmdq_new_state(NULL, NULL, 0); 442 created = 1; 443 } 444 445 cmd = cmd_list_first(cmdlist); 446 while (cmd != NULL) { 447 entry = cmd_get_entry(cmd); 448 449 item = xcalloc(1, sizeof *item); 450 xasprintf(&item->name, "[%s/%p]", entry->name, item); 451 item->type = CMDQ_COMMAND; 452 453 item->group = cmd_get_group(cmd); 454 item->state = cmdq_link_state(state); 455 456 item->cmdlist = cmdlist; 457 item->cmd = cmd; 458 459 cmdlist->references++; 460 log_debug("%s: %s group %u", __func__, item->name, item->group); 461 462 if (first == NULL) 463 first = item; 464 if (last != NULL) 465 last->next = item; 466 last = item; 467 468 cmd = cmd_list_next(cmd); 469 } 470 471 if (created) 472 cmdq_free_state(state); 473 return (first); 474 } 475 476 /* Fill in flag for a command. */ 477 static enum cmd_retval 478 cmdq_find_flag(struct cmdq_item *item, struct cmd_find_state *fs, 479 const struct cmd_entry_flag *flag) 480 { 481 const char *value; 482 483 if (flag->flag == 0) { 484 cmd_find_clear_state(fs, 0); 485 return (CMD_RETURN_NORMAL); 486 } 487 488 value = args_get(cmd_get_args(item->cmd), flag->flag); 489 if (cmd_find_target(fs, item, value, flag->type, flag->flags) != 0) { 490 cmd_find_clear_state(fs, 0); 491 return (CMD_RETURN_ERROR); 492 } 493 return (CMD_RETURN_NORMAL); 494 } 495 496 /* Fire command on command queue. */ 497 static enum cmd_retval 498 cmdq_fire_command(struct cmdq_item *item) 499 { 500 const char *name = cmdq_name(item->client); 501 struct cmdq_state *state = item->state; 502 struct cmd *cmd = item->cmd; 503 struct args *args = cmd_get_args(cmd); 504 const struct cmd_entry *entry = cmd_get_entry(cmd); 505 struct client *tc, *saved = item->client; 506 enum cmd_retval retval; 507 struct cmd_find_state *fsp, fs; 508 int flags, quiet = 0; 509 char *tmp; 510 511 if (log_get_level() > 1) { 512 tmp = cmd_print(cmd); 513 log_debug("%s %s: (%u) %s", __func__, name, item->group, tmp); 514 free(tmp); 515 } 516 517 flags = !!(state->flags & CMDQ_STATE_CONTROL); 518 cmdq_guard(item, "begin", flags); 519 520 if (item->client == NULL) 521 item->client = cmd_find_client(item, NULL, 1); 522 523 if (entry->flags & CMD_CLIENT_CANFAIL) 524 quiet = 1; 525 if (entry->flags & CMD_CLIENT_CFLAG) { 526 tc = cmd_find_client(item, args_get(args, 'c'), quiet); 527 if (tc == NULL && !quiet) { 528 retval = CMD_RETURN_ERROR; 529 goto out; 530 } 531 } else if (entry->flags & CMD_CLIENT_TFLAG) { 532 tc = cmd_find_client(item, args_get(args, 't'), quiet); 533 if (tc == NULL && !quiet) { 534 retval = CMD_RETURN_ERROR; 535 goto out; 536 } 537 } else 538 tc = cmd_find_client(item, NULL, 1); 539 item->target_client = tc; 540 541 retval = cmdq_find_flag(item, &item->source, &entry->source); 542 if (retval == CMD_RETURN_ERROR) 543 goto out; 544 retval = cmdq_find_flag(item, &item->target, &entry->target); 545 if (retval == CMD_RETURN_ERROR) 546 goto out; 547 548 549 retval = entry->exec(cmd, item); 550 if (retval == CMD_RETURN_ERROR) 551 goto out; 552 553 if (entry->flags & CMD_AFTERHOOK) { 554 if (cmd_find_valid_state(&item->target)) 555 fsp = &item->target; 556 else if (cmd_find_valid_state(&item->state->current)) 557 fsp = &item->state->current; 558 else if (cmd_find_from_client(&fs, item->client, 0) == 0) 559 fsp = &fs; 560 else 561 goto out; 562 cmdq_insert_hook(fsp->s, item, fsp, "after-%s", entry->name); 563 } 564 565 out: 566 item->client = saved; 567 if (retval == CMD_RETURN_ERROR) 568 cmdq_guard(item, "error", flags); 569 else 570 cmdq_guard(item, "end", flags); 571 return (retval); 572 } 573 574 /* Get a callback for the command queue. */ 575 struct cmdq_item * 576 cmdq_get_callback1(const char *name, cmdq_cb cb, void *data) 577 { 578 struct cmdq_item *item; 579 580 item = xcalloc(1, sizeof *item); 581 xasprintf(&item->name, "[%s/%p]", name, item); 582 item->type = CMDQ_CALLBACK; 583 584 item->group = 0; 585 item->state = cmdq_new_state(NULL, NULL, 0); 586 587 item->cb = cb; 588 item->data = data; 589 590 return (item); 591 } 592 593 /* Generic error callback. */ 594 static enum cmd_retval 595 cmdq_error_callback(struct cmdq_item *item, void *data) 596 { 597 char *error = data; 598 599 cmdq_error(item, "%s", error); 600 free(error); 601 602 return (CMD_RETURN_NORMAL); 603 } 604 605 /* Get an error callback for the command queue. */ 606 struct cmdq_item * 607 cmdq_get_error(const char *error) 608 { 609 return (cmdq_get_callback(cmdq_error_callback, xstrdup(error))); 610 } 611 612 /* Fire callback on callback queue. */ 613 static enum cmd_retval 614 cmdq_fire_callback(struct cmdq_item *item) 615 { 616 return (item->cb(item, item->data)); 617 } 618 619 /* Process next item on command queue. */ 620 u_int 621 cmdq_next(struct client *c) 622 { 623 struct cmdq_list *queue = cmdq_get(c); 624 const char *name = cmdq_name(c); 625 struct cmdq_item *item; 626 enum cmd_retval retval; 627 u_int items = 0; 628 static u_int number; 629 630 if (TAILQ_EMPTY(&queue->list)) { 631 log_debug("%s %s: empty", __func__, name); 632 return (0); 633 } 634 if (TAILQ_FIRST(&queue->list)->flags & CMDQ_WAITING) { 635 log_debug("%s %s: waiting", __func__, name); 636 return (0); 637 } 638 639 log_debug("%s %s: enter", __func__, name); 640 for (;;) { 641 item = queue->item = TAILQ_FIRST(&queue->list); 642 if (item == NULL) 643 break; 644 log_debug("%s %s: %s (%d), flags %x", __func__, name, 645 item->name, item->type, item->flags); 646 647 /* 648 * Any item with the waiting flag set waits until an external 649 * event clears the flag (for example, a job - look at 650 * run-shell). 651 */ 652 if (item->flags & CMDQ_WAITING) 653 goto waiting; 654 655 /* 656 * Items are only fired once, once the fired flag is set, a 657 * waiting flag can only be cleared by an external event. 658 */ 659 if (~item->flags & CMDQ_FIRED) { 660 item->time = time(NULL); 661 item->number = ++number; 662 663 switch (item->type) { 664 case CMDQ_COMMAND: 665 retval = cmdq_fire_command(item); 666 667 /* 668 * If a command returns an error, remove any 669 * subsequent commands in the same group. 670 */ 671 if (retval == CMD_RETURN_ERROR) 672 cmdq_remove_group(item); 673 break; 674 case CMDQ_CALLBACK: 675 retval = cmdq_fire_callback(item); 676 break; 677 default: 678 retval = CMD_RETURN_ERROR; 679 break; 680 } 681 item->flags |= CMDQ_FIRED; 682 683 if (retval == CMD_RETURN_WAIT) { 684 item->flags |= CMDQ_WAITING; 685 goto waiting; 686 } 687 items++; 688 } 689 cmdq_remove(item); 690 } 691 queue->item = NULL; 692 693 log_debug("%s %s: exit (empty)", __func__, name); 694 return (items); 695 696 waiting: 697 log_debug("%s %s: exit (wait)", __func__, name); 698 return (items); 699 } 700 701 /* Get running item if any. */ 702 struct cmdq_item * 703 cmdq_running(struct client *c) 704 { 705 struct cmdq_list *queue = cmdq_get(c); 706 707 return (queue->item); 708 } 709 710 /* Print a guard line. */ 711 void 712 cmdq_guard(struct cmdq_item *item, const char *guard, int flags) 713 { 714 struct client *c = item->client; 715 long t = item->time; 716 u_int number = item->number; 717 718 if (c != NULL && (c->flags & CLIENT_CONTROL)) 719 file_print(c, "%%%s %ld %u %d\n", guard, t, number, flags); 720 } 721 722 /* Show message from command. */ 723 void 724 cmdq_print(struct cmdq_item *item, const char *fmt, ...) 725 { 726 struct client *c = item->client; 727 struct window_pane *wp; 728 struct window_mode_entry *wme; 729 va_list ap; 730 char *tmp, *msg; 731 732 va_start(ap, fmt); 733 xvasprintf(&msg, fmt, ap); 734 va_end(ap); 735 736 log_debug("%s: %s", __func__, msg); 737 738 if (c == NULL) 739 /* nothing */; 740 else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) { 741 if (~c->flags & CLIENT_UTF8) { 742 tmp = msg; 743 msg = utf8_sanitize(tmp); 744 free(tmp); 745 } 746 file_print(c, "%s\n", msg); 747 } else { 748 wp = c->session->curw->window->active; 749 wme = TAILQ_FIRST(&wp->modes); 750 if (wme == NULL || wme->mode != &window_view_mode) { 751 window_pane_set_mode(wp, NULL, &window_view_mode, NULL, 752 NULL); 753 } 754 window_copy_add(wp, "%s", msg); 755 } 756 757 free(msg); 758 } 759 760 /* Show error from command. */ 761 void 762 cmdq_error(struct cmdq_item *item, const char *fmt, ...) 763 { 764 struct client *c = item->client; 765 struct cmd *cmd = item->cmd; 766 va_list ap; 767 char *msg, *tmp; 768 const char *file; 769 u_int line; 770 771 va_start(ap, fmt); 772 xvasprintf(&msg, fmt, ap); 773 va_end(ap); 774 775 log_debug("%s: %s", __func__, msg); 776 777 if (c == NULL) { 778 cmd_get_source(cmd, &file, &line); 779 cfg_add_cause("%s:%u: %s", file, line, msg); 780 } else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) { 781 if (~c->flags & CLIENT_UTF8) { 782 tmp = msg; 783 msg = utf8_sanitize(tmp); 784 free(tmp); 785 } 786 if (c->flags & CLIENT_CONTROL) 787 file_print(c, "%s\n", msg); 788 else 789 file_error(c, "%s\n", msg); 790 c->retval = 1; 791 } else { 792 *msg = toupper((u_char) *msg); 793 status_message_set(c, "%s", msg); 794 } 795 796 free(msg); 797 } 798