1 /* $OpenBSD: cmd.c,v 1.105 2015/09/01 09:48:34 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> 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_session_entry; 38 extern const struct cmd_entry cmd_choose_tree_entry; 39 extern const struct cmd_entry cmd_choose_window_entry; 40 extern const struct cmd_entry cmd_clear_history_entry; 41 extern const struct cmd_entry cmd_clock_mode_entry; 42 extern const struct cmd_entry cmd_command_prompt_entry; 43 extern const struct cmd_entry cmd_confirm_before_entry; 44 extern const struct cmd_entry cmd_copy_mode_entry; 45 extern const struct cmd_entry cmd_delete_buffer_entry; 46 extern const struct cmd_entry cmd_detach_client_entry; 47 extern const struct cmd_entry cmd_display_message_entry; 48 extern const struct cmd_entry cmd_display_panes_entry; 49 extern const struct cmd_entry cmd_down_pane_entry; 50 extern const struct cmd_entry cmd_find_window_entry; 51 extern const struct cmd_entry cmd_has_session_entry; 52 extern const struct cmd_entry cmd_if_shell_entry; 53 extern const struct cmd_entry cmd_join_pane_entry; 54 extern const struct cmd_entry cmd_kill_pane_entry; 55 extern const struct cmd_entry cmd_kill_server_entry; 56 extern const struct cmd_entry cmd_kill_session_entry; 57 extern const struct cmd_entry cmd_kill_window_entry; 58 extern const struct cmd_entry cmd_last_pane_entry; 59 extern const struct cmd_entry cmd_last_window_entry; 60 extern const struct cmd_entry cmd_link_window_entry; 61 extern const struct cmd_entry cmd_list_buffers_entry; 62 extern const struct cmd_entry cmd_list_clients_entry; 63 extern const struct cmd_entry cmd_list_commands_entry; 64 extern const struct cmd_entry cmd_list_keys_entry; 65 extern const struct cmd_entry cmd_list_panes_entry; 66 extern const struct cmd_entry cmd_list_sessions_entry; 67 extern const struct cmd_entry cmd_list_windows_entry; 68 extern const struct cmd_entry cmd_load_buffer_entry; 69 extern const struct cmd_entry cmd_lock_client_entry; 70 extern const struct cmd_entry cmd_lock_server_entry; 71 extern const struct cmd_entry cmd_lock_session_entry; 72 extern const struct cmd_entry cmd_move_pane_entry; 73 extern const struct cmd_entry cmd_move_window_entry; 74 extern const struct cmd_entry cmd_new_session_entry; 75 extern const struct cmd_entry cmd_new_window_entry; 76 extern const struct cmd_entry cmd_next_layout_entry; 77 extern const struct cmd_entry cmd_next_window_entry; 78 extern const struct cmd_entry cmd_paste_buffer_entry; 79 extern const struct cmd_entry cmd_pipe_pane_entry; 80 extern const struct cmd_entry cmd_previous_layout_entry; 81 extern const struct cmd_entry cmd_previous_window_entry; 82 extern const struct cmd_entry cmd_refresh_client_entry; 83 extern const struct cmd_entry cmd_rename_session_entry; 84 extern const struct cmd_entry cmd_rename_window_entry; 85 extern const struct cmd_entry cmd_resize_pane_entry; 86 extern const struct cmd_entry cmd_respawn_pane_entry; 87 extern const struct cmd_entry cmd_respawn_window_entry; 88 extern const struct cmd_entry cmd_rotate_window_entry; 89 extern const struct cmd_entry cmd_run_shell_entry; 90 extern const struct cmd_entry cmd_save_buffer_entry; 91 extern const struct cmd_entry cmd_select_layout_entry; 92 extern const struct cmd_entry cmd_select_pane_entry; 93 extern const struct cmd_entry cmd_select_window_entry; 94 extern const struct cmd_entry cmd_send_keys_entry; 95 extern const struct cmd_entry cmd_send_prefix_entry; 96 extern const struct cmd_entry cmd_server_info_entry; 97 extern const struct cmd_entry cmd_set_buffer_entry; 98 extern const struct cmd_entry cmd_set_environment_entry; 99 extern const struct cmd_entry cmd_set_option_entry; 100 extern const struct cmd_entry cmd_set_window_option_entry; 101 extern const struct cmd_entry cmd_show_buffer_entry; 102 extern const struct cmd_entry cmd_show_environment_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_session_entry, 126 &cmd_choose_tree_entry, 127 &cmd_choose_window_entry, 128 &cmd_clear_history_entry, 129 &cmd_clock_mode_entry, 130 &cmd_command_prompt_entry, 131 &cmd_confirm_before_entry, 132 &cmd_copy_mode_entry, 133 &cmd_delete_buffer_entry, 134 &cmd_detach_client_entry, 135 &cmd_display_message_entry, 136 &cmd_display_panes_entry, 137 &cmd_find_window_entry, 138 &cmd_has_session_entry, 139 &cmd_if_shell_entry, 140 &cmd_join_pane_entry, 141 &cmd_kill_pane_entry, 142 &cmd_kill_server_entry, 143 &cmd_kill_session_entry, 144 &cmd_kill_window_entry, 145 &cmd_last_pane_entry, 146 &cmd_last_window_entry, 147 &cmd_link_window_entry, 148 &cmd_list_buffers_entry, 149 &cmd_list_clients_entry, 150 &cmd_list_commands_entry, 151 &cmd_list_keys_entry, 152 &cmd_list_panes_entry, 153 &cmd_list_sessions_entry, 154 &cmd_list_windows_entry, 155 &cmd_load_buffer_entry, 156 &cmd_lock_client_entry, 157 &cmd_lock_server_entry, 158 &cmd_lock_session_entry, 159 &cmd_move_pane_entry, 160 &cmd_move_window_entry, 161 &cmd_new_session_entry, 162 &cmd_new_window_entry, 163 &cmd_next_layout_entry, 164 &cmd_next_window_entry, 165 &cmd_paste_buffer_entry, 166 &cmd_pipe_pane_entry, 167 &cmd_previous_layout_entry, 168 &cmd_previous_window_entry, 169 &cmd_refresh_client_entry, 170 &cmd_rename_session_entry, 171 &cmd_rename_window_entry, 172 &cmd_resize_pane_entry, 173 &cmd_respawn_pane_entry, 174 &cmd_respawn_window_entry, 175 &cmd_rotate_window_entry, 176 &cmd_run_shell_entry, 177 &cmd_save_buffer_entry, 178 &cmd_select_layout_entry, 179 &cmd_select_pane_entry, 180 &cmd_select_window_entry, 181 &cmd_send_keys_entry, 182 &cmd_send_prefix_entry, 183 &cmd_server_info_entry, 184 &cmd_set_buffer_entry, 185 &cmd_set_environment_entry, 186 &cmd_set_option_entry, 187 &cmd_set_window_option_entry, 188 &cmd_show_buffer_entry, 189 &cmd_show_environment_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 int 207 cmd_pack_argv(int argc, char **argv, char *buf, size_t len) 208 { 209 size_t arglen; 210 int i; 211 212 if (argc == 0) 213 return (0); 214 215 *buf = '\0'; 216 for (i = 0; i < argc; i++) { 217 if (strlcpy(buf, argv[i], len) >= len) 218 return (-1); 219 arglen = strlen(argv[i]) + 1; 220 buf += arglen; 221 len -= arglen; 222 } 223 224 return (0); 225 } 226 227 int 228 cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv) 229 { 230 int i; 231 size_t arglen; 232 233 if (argc == 0) 234 return (0); 235 *argv = xcalloc(argc, sizeof **argv); 236 237 buf[len - 1] = '\0'; 238 for (i = 0; i < argc; i++) { 239 if (len == 0) { 240 cmd_free_argv(argc, *argv); 241 return (-1); 242 } 243 244 arglen = strlen(buf) + 1; 245 (*argv)[i] = xstrdup(buf); 246 buf += arglen; 247 len -= arglen; 248 } 249 250 return (0); 251 } 252 253 char ** 254 cmd_copy_argv(int argc, char **argv) 255 { 256 char **new_argv; 257 int i; 258 259 if (argc == 0) 260 return (NULL); 261 new_argv = xcalloc(argc + 1, sizeof *new_argv); 262 for (i = 0; i < argc; i++) { 263 if (argv[i] != NULL) 264 new_argv[i] = xstrdup(argv[i]); 265 } 266 return (new_argv); 267 } 268 269 void 270 cmd_free_argv(int argc, char **argv) 271 { 272 int i; 273 274 if (argc == 0) 275 return; 276 for (i = 0; i < argc; i++) 277 free(argv[i]); 278 free(argv); 279 } 280 281 char * 282 cmd_stringify_argv(int argc, char **argv) 283 { 284 char *buf; 285 int i; 286 size_t len; 287 288 if (argc == 0) 289 return (xstrdup("")); 290 291 len = 0; 292 buf = NULL; 293 294 for (i = 0; i < argc; i++) { 295 len += strlen(argv[i]) + 1; 296 buf = xrealloc(buf, len); 297 298 if (i == 0) 299 *buf = '\0'; 300 else 301 strlcat(buf, " ", len); 302 strlcat(buf, argv[i], len); 303 } 304 return (buf); 305 } 306 307 struct cmd * 308 cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause) 309 { 310 const struct cmd_entry **entryp, *entry; 311 struct cmd *cmd; 312 struct args *args; 313 char s[BUFSIZ]; 314 int ambiguous = 0; 315 316 *cause = NULL; 317 if (argc == 0) { 318 xasprintf(cause, "no command"); 319 return (NULL); 320 } 321 322 entry = NULL; 323 for (entryp = cmd_table; *entryp != NULL; entryp++) { 324 if ((*entryp)->alias != NULL && 325 strcmp((*entryp)->alias, argv[0]) == 0) { 326 ambiguous = 0; 327 entry = *entryp; 328 break; 329 } 330 331 if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0) 332 continue; 333 if (entry != NULL) 334 ambiguous = 1; 335 entry = *entryp; 336 337 /* Bail now if an exact match. */ 338 if (strcmp(entry->name, argv[0]) == 0) 339 break; 340 } 341 if (ambiguous) 342 goto ambiguous; 343 if (entry == NULL) { 344 xasprintf(cause, "unknown command: %s", argv[0]); 345 return (NULL); 346 } 347 348 args = args_parse(entry->args_template, argc, argv); 349 if (args == NULL) 350 goto usage; 351 if (entry->args_lower != -1 && args->argc < entry->args_lower) 352 goto usage; 353 if (entry->args_upper != -1 && args->argc > entry->args_upper) 354 goto usage; 355 356 cmd = xcalloc(1, sizeof *cmd); 357 cmd->entry = entry; 358 cmd->args = args; 359 360 if (file != NULL) 361 cmd->file = xstrdup(file); 362 cmd->line = line; 363 364 return (cmd); 365 366 ambiguous: 367 *s = '\0'; 368 for (entryp = cmd_table; *entryp != NULL; entryp++) { 369 if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0) 370 continue; 371 if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s) 372 break; 373 if (strlcat(s, ", ", sizeof s) >= sizeof s) 374 break; 375 } 376 s[strlen(s) - 2] = '\0'; 377 xasprintf(cause, "ambiguous command: %s, could be: %s", argv[0], s); 378 return (NULL); 379 380 usage: 381 if (args != NULL) 382 args_free(args); 383 xasprintf(cause, "usage: %s %s", entry->name, entry->usage); 384 return (NULL); 385 } 386 387 size_t 388 cmd_print(struct cmd *cmd, char *buf, size_t len) 389 { 390 size_t off, used; 391 392 off = xsnprintf(buf, len, "%s ", cmd->entry->name); 393 if (off + 1 < len) { 394 used = args_print(cmd->args, buf + off, len - off - 1); 395 if (used == 0) 396 off--; 397 else 398 off += used; 399 buf[off] = '\0'; 400 } 401 return (off); 402 } 403 404 /* Adjust current mouse position for a pane. */ 405 int 406 cmd_mouse_at(struct window_pane *wp, struct mouse_event *m, u_int *xp, 407 u_int *yp, int last) 408 { 409 u_int x, y; 410 411 if (last) { 412 x = m->lx; 413 y = m->ly; 414 } else { 415 x = m->x; 416 y = m->y; 417 } 418 419 if (m->statusat == 0 && y > 0) 420 y--; 421 else if (m->statusat > 0 && y >= (u_int)m->statusat) 422 y = m->statusat - 1; 423 424 if (x < wp->xoff || x >= wp->xoff + wp->sx) 425 return (-1); 426 if (y < wp->yoff || y >= wp->yoff + wp->sy) 427 return (-1); 428 429 *xp = x - wp->xoff; 430 *yp = y - wp->yoff; 431 return (0); 432 } 433 434 /* Get current mouse window if any. */ 435 struct winlink * 436 cmd_mouse_window(struct mouse_event *m, struct session **sp) 437 { 438 struct session *s; 439 struct window *w; 440 441 if (!m->valid || m->s == -1 || m->w == -1) 442 return (NULL); 443 if ((s = session_find_by_id(m->s)) == NULL) 444 return (NULL); 445 if ((w = window_find_by_id(m->w)) == NULL) 446 return (NULL); 447 448 if (sp != NULL) 449 *sp = s; 450 return (winlink_find_by_window(&s->windows, w)); 451 } 452 453 /* Get current mouse pane if any. */ 454 struct window_pane * 455 cmd_mouse_pane(struct mouse_event *m, struct session **sp, 456 struct winlink **wlp) 457 { 458 struct winlink *wl; 459 struct window_pane *wp; 460 461 if ((wl = cmd_mouse_window(m, sp)) == NULL) 462 return (NULL); 463 if ((wp = window_pane_find_by_id(m->wp)) == NULL) 464 return (NULL); 465 if (!window_has_pane(wl->window, wp)) 466 return (NULL); 467 468 if (wlp != NULL) 469 *wlp = wl; 470 return (wp); 471 } 472 473 /* Replace the first %% or %idx in template by s. */ 474 char * 475 cmd_template_replace(const char *template, const char *s, int idx) 476 { 477 char ch, *buf; 478 const char *ptr; 479 int replaced; 480 size_t len; 481 482 if (strchr(template, '%') == NULL) 483 return (xstrdup(template)); 484 485 buf = xmalloc(1); 486 *buf = '\0'; 487 len = 0; 488 replaced = 0; 489 490 ptr = template; 491 while (*ptr != '\0') { 492 switch (ch = *ptr++) { 493 case '%': 494 if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) { 495 if (*ptr != '%' || replaced) 496 break; 497 replaced = 1; 498 } 499 ptr++; 500 501 len += strlen(s); 502 buf = xrealloc(buf, len + 1); 503 strlcat(buf, s, len + 1); 504 continue; 505 } 506 buf = xrealloc(buf, len + 2); 507 buf[len++] = ch; 508 buf[len] = '\0'; 509 } 510 511 return (buf); 512 } 513