1 /* $OpenBSD: cmd.c,v 1.146 2019/05/03 18:42:40 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2007 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 #include <sys/time.h> 21 22 #include <fnmatch.h> 23 #include <paths.h> 24 #include <pwd.h> 25 #include <stdlib.h> 26 #include <string.h> 27 #include <unistd.h> 28 29 #include "tmux.h" 30 31 extern const struct cmd_entry cmd_attach_session_entry; 32 extern const struct cmd_entry cmd_bind_key_entry; 33 extern const struct cmd_entry cmd_break_pane_entry; 34 extern const struct cmd_entry cmd_capture_pane_entry; 35 extern const struct cmd_entry cmd_choose_buffer_entry; 36 extern const struct cmd_entry cmd_choose_client_entry; 37 extern const struct cmd_entry cmd_choose_tree_entry; 38 extern const struct cmd_entry cmd_clear_history_entry; 39 extern const struct cmd_entry cmd_clock_mode_entry; 40 extern const struct cmd_entry cmd_command_prompt_entry; 41 extern const struct cmd_entry cmd_confirm_before_entry; 42 extern const struct cmd_entry cmd_copy_mode_entry; 43 extern const struct cmd_entry cmd_delete_buffer_entry; 44 extern const struct cmd_entry cmd_detach_client_entry; 45 extern const struct cmd_entry cmd_display_message_entry; 46 extern const struct cmd_entry cmd_display_panes_entry; 47 extern const struct cmd_entry cmd_down_pane_entry; 48 extern const struct cmd_entry cmd_find_window_entry; 49 extern const struct cmd_entry cmd_has_session_entry; 50 extern const struct cmd_entry cmd_if_shell_entry; 51 extern const struct cmd_entry cmd_join_pane_entry; 52 extern const struct cmd_entry cmd_kill_pane_entry; 53 extern const struct cmd_entry cmd_kill_server_entry; 54 extern const struct cmd_entry cmd_kill_session_entry; 55 extern const struct cmd_entry cmd_kill_window_entry; 56 extern const struct cmd_entry cmd_last_pane_entry; 57 extern const struct cmd_entry cmd_last_window_entry; 58 extern const struct cmd_entry cmd_link_window_entry; 59 extern const struct cmd_entry cmd_list_buffers_entry; 60 extern const struct cmd_entry cmd_list_clients_entry; 61 extern const struct cmd_entry cmd_list_commands_entry; 62 extern const struct cmd_entry cmd_list_keys_entry; 63 extern const struct cmd_entry cmd_list_panes_entry; 64 extern const struct cmd_entry cmd_list_sessions_entry; 65 extern const struct cmd_entry cmd_list_windows_entry; 66 extern const struct cmd_entry cmd_load_buffer_entry; 67 extern const struct cmd_entry cmd_lock_client_entry; 68 extern const struct cmd_entry cmd_lock_server_entry; 69 extern const struct cmd_entry cmd_lock_session_entry; 70 extern const struct cmd_entry cmd_move_pane_entry; 71 extern const struct cmd_entry cmd_move_window_entry; 72 extern const struct cmd_entry cmd_new_session_entry; 73 extern const struct cmd_entry cmd_new_window_entry; 74 extern const struct cmd_entry cmd_next_layout_entry; 75 extern const struct cmd_entry cmd_next_window_entry; 76 extern const struct cmd_entry cmd_paste_buffer_entry; 77 extern const struct cmd_entry cmd_pipe_pane_entry; 78 extern const struct cmd_entry cmd_previous_layout_entry; 79 extern const struct cmd_entry cmd_previous_window_entry; 80 extern const struct cmd_entry cmd_refresh_client_entry; 81 extern const struct cmd_entry cmd_rename_session_entry; 82 extern const struct cmd_entry cmd_rename_window_entry; 83 extern const struct cmd_entry cmd_resize_pane_entry; 84 extern const struct cmd_entry cmd_resize_window_entry; 85 extern const struct cmd_entry cmd_respawn_pane_entry; 86 extern const struct cmd_entry cmd_respawn_window_entry; 87 extern const struct cmd_entry cmd_rotate_window_entry; 88 extern const struct cmd_entry cmd_run_shell_entry; 89 extern const struct cmd_entry cmd_save_buffer_entry; 90 extern const struct cmd_entry cmd_select_layout_entry; 91 extern const struct cmd_entry cmd_select_pane_entry; 92 extern const struct cmd_entry cmd_select_window_entry; 93 extern const struct cmd_entry cmd_send_keys_entry; 94 extern const struct cmd_entry cmd_send_prefix_entry; 95 extern const struct cmd_entry cmd_set_buffer_entry; 96 extern const struct cmd_entry cmd_set_environment_entry; 97 extern const struct cmd_entry cmd_set_hook_entry; 98 extern const struct cmd_entry cmd_set_option_entry; 99 extern const struct cmd_entry cmd_set_window_option_entry; 100 extern const struct cmd_entry cmd_show_buffer_entry; 101 extern const struct cmd_entry cmd_show_environment_entry; 102 extern const struct cmd_entry cmd_show_hooks_entry; 103 extern const struct cmd_entry cmd_show_messages_entry; 104 extern const struct cmd_entry cmd_show_options_entry; 105 extern const struct cmd_entry cmd_show_window_options_entry; 106 extern const struct cmd_entry cmd_source_file_entry; 107 extern const struct cmd_entry cmd_split_window_entry; 108 extern const struct cmd_entry cmd_start_server_entry; 109 extern const struct cmd_entry cmd_suspend_client_entry; 110 extern const struct cmd_entry cmd_swap_pane_entry; 111 extern const struct cmd_entry cmd_swap_window_entry; 112 extern const struct cmd_entry cmd_switch_client_entry; 113 extern const struct cmd_entry cmd_unbind_key_entry; 114 extern const struct cmd_entry cmd_unlink_window_entry; 115 extern const struct cmd_entry cmd_up_pane_entry; 116 extern const struct cmd_entry cmd_wait_for_entry; 117 118 const struct cmd_entry *cmd_table[] = { 119 &cmd_attach_session_entry, 120 &cmd_bind_key_entry, 121 &cmd_break_pane_entry, 122 &cmd_capture_pane_entry, 123 &cmd_choose_buffer_entry, 124 &cmd_choose_client_entry, 125 &cmd_choose_tree_entry, 126 &cmd_clear_history_entry, 127 &cmd_clock_mode_entry, 128 &cmd_command_prompt_entry, 129 &cmd_confirm_before_entry, 130 &cmd_copy_mode_entry, 131 &cmd_delete_buffer_entry, 132 &cmd_detach_client_entry, 133 &cmd_display_message_entry, 134 &cmd_display_panes_entry, 135 &cmd_find_window_entry, 136 &cmd_has_session_entry, 137 &cmd_if_shell_entry, 138 &cmd_join_pane_entry, 139 &cmd_kill_pane_entry, 140 &cmd_kill_server_entry, 141 &cmd_kill_session_entry, 142 &cmd_kill_window_entry, 143 &cmd_last_pane_entry, 144 &cmd_last_window_entry, 145 &cmd_link_window_entry, 146 &cmd_list_buffers_entry, 147 &cmd_list_clients_entry, 148 &cmd_list_commands_entry, 149 &cmd_list_keys_entry, 150 &cmd_list_panes_entry, 151 &cmd_list_sessions_entry, 152 &cmd_list_windows_entry, 153 &cmd_load_buffer_entry, 154 &cmd_lock_client_entry, 155 &cmd_lock_server_entry, 156 &cmd_lock_session_entry, 157 &cmd_move_pane_entry, 158 &cmd_move_window_entry, 159 &cmd_new_session_entry, 160 &cmd_new_window_entry, 161 &cmd_next_layout_entry, 162 &cmd_next_window_entry, 163 &cmd_paste_buffer_entry, 164 &cmd_pipe_pane_entry, 165 &cmd_previous_layout_entry, 166 &cmd_previous_window_entry, 167 &cmd_refresh_client_entry, 168 &cmd_rename_session_entry, 169 &cmd_rename_window_entry, 170 &cmd_resize_pane_entry, 171 &cmd_resize_window_entry, 172 &cmd_respawn_pane_entry, 173 &cmd_respawn_window_entry, 174 &cmd_rotate_window_entry, 175 &cmd_run_shell_entry, 176 &cmd_save_buffer_entry, 177 &cmd_select_layout_entry, 178 &cmd_select_pane_entry, 179 &cmd_select_window_entry, 180 &cmd_send_keys_entry, 181 &cmd_send_prefix_entry, 182 &cmd_set_buffer_entry, 183 &cmd_set_environment_entry, 184 &cmd_set_hook_entry, 185 &cmd_set_option_entry, 186 &cmd_set_window_option_entry, 187 &cmd_show_buffer_entry, 188 &cmd_show_environment_entry, 189 &cmd_show_hooks_entry, 190 &cmd_show_messages_entry, 191 &cmd_show_options_entry, 192 &cmd_show_window_options_entry, 193 &cmd_source_file_entry, 194 &cmd_split_window_entry, 195 &cmd_start_server_entry, 196 &cmd_suspend_client_entry, 197 &cmd_swap_pane_entry, 198 &cmd_swap_window_entry, 199 &cmd_switch_client_entry, 200 &cmd_unbind_key_entry, 201 &cmd_unlink_window_entry, 202 &cmd_wait_for_entry, 203 NULL 204 }; 205 206 void 207 cmd_log_argv(int argc, char **argv, const char *prefix) 208 { 209 int i; 210 211 for (i = 0; i < argc; i++) 212 log_debug("%s: argv[%d]=%s", prefix, i, argv[i]); 213 } 214 215 int 216 cmd_pack_argv(int argc, char **argv, char *buf, size_t len) 217 { 218 size_t arglen; 219 int i; 220 221 if (argc == 0) 222 return (0); 223 cmd_log_argv(argc, argv, __func__); 224 225 *buf = '\0'; 226 for (i = 0; i < argc; i++) { 227 if (strlcpy(buf, argv[i], len) >= len) 228 return (-1); 229 arglen = strlen(argv[i]) + 1; 230 buf += arglen; 231 len -= arglen; 232 } 233 234 return (0); 235 } 236 237 int 238 cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv) 239 { 240 int i; 241 size_t arglen; 242 243 if (argc == 0) 244 return (0); 245 *argv = xcalloc(argc, sizeof **argv); 246 247 buf[len - 1] = '\0'; 248 for (i = 0; i < argc; i++) { 249 if (len == 0) { 250 cmd_free_argv(argc, *argv); 251 return (-1); 252 } 253 254 arglen = strlen(buf) + 1; 255 (*argv)[i] = xstrdup(buf); 256 257 buf += arglen; 258 len -= arglen; 259 } 260 cmd_log_argv(argc, *argv, __func__); 261 262 return (0); 263 } 264 265 char ** 266 cmd_copy_argv(int argc, char **argv) 267 { 268 char **new_argv; 269 int i; 270 271 if (argc == 0) 272 return (NULL); 273 new_argv = xcalloc(argc + 1, sizeof *new_argv); 274 for (i = 0; i < argc; i++) { 275 if (argv[i] != NULL) 276 new_argv[i] = xstrdup(argv[i]); 277 } 278 return (new_argv); 279 } 280 281 void 282 cmd_free_argv(int argc, char **argv) 283 { 284 int i; 285 286 if (argc == 0) 287 return; 288 for (i = 0; i < argc; i++) 289 free(argv[i]); 290 free(argv); 291 } 292 293 char * 294 cmd_stringify_argv(int argc, char **argv) 295 { 296 char *buf; 297 int i; 298 size_t len; 299 300 if (argc == 0) 301 return (xstrdup("")); 302 303 len = 0; 304 buf = NULL; 305 306 for (i = 0; i < argc; i++) { 307 len += strlen(argv[i]) + 1; 308 buf = xrealloc(buf, len); 309 310 if (i == 0) 311 *buf = '\0'; 312 else 313 strlcat(buf, " ", len); 314 strlcat(buf, argv[i], len); 315 } 316 return (buf); 317 } 318 319 static int 320 cmd_try_alias(int *argc, char ***argv) 321 { 322 struct options_entry *o; 323 struct options_array_item *a; 324 union options_value *ov; 325 int old_argc = *argc, new_argc, i; 326 char **old_argv = *argv, **new_argv; 327 size_t wanted; 328 const char *cp = NULL; 329 330 o = options_get_only(global_options, "command-alias"); 331 if (o == NULL) 332 return (-1); 333 wanted = strlen(old_argv[0]); 334 335 a = options_array_first(o); 336 while (a != NULL) { 337 ov = options_array_item_value(a); 338 cp = strchr(ov->string, '='); 339 if (cp != NULL && 340 (size_t)(cp - ov->string) == wanted && 341 strncmp(old_argv[0], ov->string, wanted) == 0) 342 break; 343 a = options_array_next(a); 344 } 345 if (a == NULL) 346 return (-1); 347 348 if (cmd_string_split(cp + 1, &new_argc, &new_argv) != 0) 349 return (-1); 350 351 *argc = new_argc + old_argc - 1; 352 *argv = xcalloc((*argc) + 1, sizeof **argv); 353 354 for (i = 0; i < new_argc; i++) 355 (*argv)[i] = xstrdup(new_argv[i]); 356 for (i = 1; i < old_argc; i++) 357 (*argv)[new_argc + i - 1] = xstrdup(old_argv[i]); 358 359 log_debug("alias: %s=%s", old_argv[0], cp + 1); 360 for (i = 0; i < *argc; i++) 361 log_debug("alias: argv[%d] = %s", i, (*argv)[i]); 362 363 cmd_free_argv(new_argc, new_argv); 364 return (0); 365 } 366 367 struct cmd * 368 cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause) 369 { 370 const char *name; 371 const struct cmd_entry **entryp, *entry; 372 struct cmd *cmd; 373 struct args *args; 374 char s[BUFSIZ]; 375 int ambiguous, allocated = 0; 376 377 *cause = NULL; 378 if (argc == 0) { 379 xasprintf(cause, "no command"); 380 return (NULL); 381 } 382 name = argv[0]; 383 384 retry: 385 ambiguous = 0; 386 entry = NULL; 387 for (entryp = cmd_table; *entryp != NULL; entryp++) { 388 if ((*entryp)->alias != NULL && 389 strcmp((*entryp)->alias, argv[0]) == 0) { 390 ambiguous = 0; 391 entry = *entryp; 392 break; 393 } 394 395 if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0) 396 continue; 397 if (entry != NULL) 398 ambiguous = 1; 399 entry = *entryp; 400 401 /* Bail now if an exact match. */ 402 if (strcmp(entry->name, argv[0]) == 0) 403 break; 404 } 405 if ((ambiguous || entry == NULL) && 406 server_proc != NULL && 407 !allocated && 408 cmd_try_alias(&argc, &argv) == 0) { 409 allocated = 1; 410 goto retry; 411 } 412 if (ambiguous) 413 goto ambiguous; 414 if (entry == NULL) { 415 xasprintf(cause, "unknown command: %s", name); 416 return (NULL); 417 } 418 cmd_log_argv(argc, argv, entry->name); 419 420 args = args_parse(entry->args.template, argc, argv); 421 if (args == NULL) 422 goto usage; 423 if (entry->args.lower != -1 && args->argc < entry->args.lower) 424 goto usage; 425 if (entry->args.upper != -1 && args->argc > entry->args.upper) 426 goto usage; 427 428 cmd = xcalloc(1, sizeof *cmd); 429 cmd->entry = entry; 430 cmd->args = args; 431 432 if (file != NULL) 433 cmd->file = xstrdup(file); 434 cmd->line = line; 435 436 if (allocated) 437 cmd_free_argv(argc, argv); 438 return (cmd); 439 440 ambiguous: 441 *s = '\0'; 442 for (entryp = cmd_table; *entryp != NULL; entryp++) { 443 if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0) 444 continue; 445 if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s) 446 break; 447 if (strlcat(s, ", ", sizeof s) >= sizeof s) 448 break; 449 } 450 s[strlen(s) - 2] = '\0'; 451 xasprintf(cause, "ambiguous command: %s, could be: %s", name, s); 452 return (NULL); 453 454 usage: 455 if (args != NULL) 456 args_free(args); 457 xasprintf(cause, "usage: %s %s", entry->name, entry->usage); 458 return (NULL); 459 } 460 461 char * 462 cmd_print(struct cmd *cmd) 463 { 464 char *out, *s; 465 466 s = args_print(cmd->args); 467 if (*s != '\0') 468 xasprintf(&out, "%s %s", cmd->entry->name, s); 469 else 470 out = xstrdup(cmd->entry->name); 471 free(s); 472 473 return (out); 474 } 475 476 /* Adjust current mouse position for a pane. */ 477 int 478 cmd_mouse_at(struct window_pane *wp, struct mouse_event *m, u_int *xp, 479 u_int *yp, int last) 480 { 481 u_int x, y; 482 483 if (last) { 484 x = m->lx + m->ox; 485 y = m->ly + m->oy; 486 } else { 487 x = m->x + m->ox; 488 y = m->y + m->oy; 489 } 490 log_debug("%s: x=%u, y=%u%s", __func__, x, y, last ? " (last)" : ""); 491 492 if (m->statusat == 0 && y > 0) 493 y--; 494 495 if (x < wp->xoff || x >= wp->xoff + wp->sx) 496 return (-1); 497 if (y < wp->yoff || y >= wp->yoff + wp->sy) 498 return (-1); 499 500 if (xp != NULL) 501 *xp = x - wp->xoff; 502 if (yp != NULL) 503 *yp = y - wp->yoff; 504 return (0); 505 } 506 507 /* Get current mouse window if any. */ 508 struct winlink * 509 cmd_mouse_window(struct mouse_event *m, struct session **sp) 510 { 511 struct session *s; 512 struct window *w; 513 514 if (!m->valid || m->s == -1 || m->w == -1) 515 return (NULL); 516 if ((s = session_find_by_id(m->s)) == NULL) 517 return (NULL); 518 if ((w = window_find_by_id(m->w)) == NULL) 519 return (NULL); 520 521 if (sp != NULL) 522 *sp = s; 523 return (winlink_find_by_window(&s->windows, w)); 524 } 525 526 /* Get current mouse pane if any. */ 527 struct window_pane * 528 cmd_mouse_pane(struct mouse_event *m, struct session **sp, 529 struct winlink **wlp) 530 { 531 struct winlink *wl; 532 struct window_pane *wp; 533 534 if ((wl = cmd_mouse_window(m, sp)) == NULL) 535 return (NULL); 536 if ((wp = window_pane_find_by_id(m->wp)) == NULL) 537 return (NULL); 538 if (!window_has_pane(wl->window, wp)) 539 return (NULL); 540 541 if (wlp != NULL) 542 *wlp = wl; 543 return (wp); 544 } 545 546 /* Replace the first %% or %idx in template by s. */ 547 char * 548 cmd_template_replace(const char *template, const char *s, int idx) 549 { 550 char ch, *buf; 551 const char *ptr, *cp, quote[] = "\"\\$"; 552 int replaced, quoted; 553 size_t len; 554 555 if (strchr(template, '%') == NULL) 556 return (xstrdup(template)); 557 558 buf = xmalloc(1); 559 *buf = '\0'; 560 len = 0; 561 replaced = 0; 562 563 ptr = template; 564 while (*ptr != '\0') { 565 switch (ch = *ptr++) { 566 case '%': 567 if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) { 568 if (*ptr != '%' || replaced) 569 break; 570 replaced = 1; 571 } 572 ptr++; 573 574 quoted = (*ptr == '%'); 575 if (quoted) 576 ptr++; 577 578 buf = xrealloc(buf, len + (strlen(s) * 3) + 1); 579 for (cp = s; *cp != '\0'; cp++) { 580 if (quoted && strchr(quote, *cp) != NULL) 581 buf[len++] = '\\'; 582 if (quoted && *cp == ';') { 583 buf[len++] = '\\'; 584 buf[len++] = '\\'; 585 } 586 buf[len++] = *cp; 587 } 588 buf[len] = '\0'; 589 continue; 590 } 591 buf = xrealloc(buf, len + 2); 592 buf[len++] = ch; 593 buf[len] = '\0'; 594 } 595 596 return (buf); 597 } 598