Annotation of src/usr.bin/tmux/cmd.c, Revision 1.110
1.110 ! nicm 1: /* $OpenBSD: cmd.c,v 1.109 2015/12/13 15:00:37 nicm Exp $ */
1.1 nicm 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:
1.5 nicm 22: #include <fnmatch.h>
23: #include <paths.h>
1.61 nicm 24: #include <pwd.h>
1.1 nicm 25: #include <stdlib.h>
26: #include <string.h>
27: #include <unistd.h>
28:
29: #include "tmux.h"
1.105 nicm 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;
1.107 nicm 99: extern const struct cmd_entry cmd_set_hook_entry;
1.105 nicm 100: extern const struct cmd_entry cmd_set_option_entry;
101: extern const struct cmd_entry cmd_set_window_option_entry;
102: extern const struct cmd_entry cmd_show_buffer_entry;
103: extern const struct cmd_entry cmd_show_environment_entry;
1.107 nicm 104: extern const struct cmd_entry cmd_show_hooks_entry;
1.105 nicm 105: extern const struct cmd_entry cmd_show_messages_entry;
106: extern const struct cmd_entry cmd_show_options_entry;
107: extern const struct cmd_entry cmd_show_window_options_entry;
108: extern const struct cmd_entry cmd_source_file_entry;
109: extern const struct cmd_entry cmd_split_window_entry;
110: extern const struct cmd_entry cmd_start_server_entry;
111: extern const struct cmd_entry cmd_suspend_client_entry;
112: extern const struct cmd_entry cmd_swap_pane_entry;
113: extern const struct cmd_entry cmd_swap_window_entry;
114: extern const struct cmd_entry cmd_switch_client_entry;
115: extern const struct cmd_entry cmd_unbind_key_entry;
116: extern const struct cmd_entry cmd_unlink_window_entry;
117: extern const struct cmd_entry cmd_up_pane_entry;
118: extern const struct cmd_entry cmd_wait_for_entry;
1.1 nicm 119:
120: const struct cmd_entry *cmd_table[] = {
121: &cmd_attach_session_entry,
122: &cmd_bind_key_entry,
123: &cmd_break_pane_entry,
1.34 nicm 124: &cmd_capture_pane_entry,
1.42 nicm 125: &cmd_choose_buffer_entry,
1.15 nicm 126: &cmd_choose_client_entry,
1.1 nicm 127: &cmd_choose_session_entry,
1.67 nicm 128: &cmd_choose_tree_entry,
1.1 nicm 129: &cmd_choose_window_entry,
130: &cmd_clear_history_entry,
131: &cmd_clock_mode_entry,
132: &cmd_command_prompt_entry,
133: &cmd_confirm_before_entry,
134: &cmd_copy_mode_entry,
135: &cmd_delete_buffer_entry,
136: &cmd_detach_client_entry,
1.7 nicm 137: &cmd_display_message_entry,
1.16 nicm 138: &cmd_display_panes_entry,
1.1 nicm 139: &cmd_find_window_entry,
140: &cmd_has_session_entry,
1.4 nicm 141: &cmd_if_shell_entry,
1.37 nicm 142: &cmd_join_pane_entry,
1.1 nicm 143: &cmd_kill_pane_entry,
144: &cmd_kill_server_entry,
145: &cmd_kill_session_entry,
146: &cmd_kill_window_entry,
1.45 nicm 147: &cmd_last_pane_entry,
1.1 nicm 148: &cmd_last_window_entry,
149: &cmd_link_window_entry,
150: &cmd_list_buffers_entry,
151: &cmd_list_clients_entry,
152: &cmd_list_commands_entry,
153: &cmd_list_keys_entry,
1.23 nicm 154: &cmd_list_panes_entry,
1.1 nicm 155: &cmd_list_sessions_entry,
156: &cmd_list_windows_entry,
157: &cmd_load_buffer_entry,
1.19 nicm 158: &cmd_lock_client_entry,
1.1 nicm 159: &cmd_lock_server_entry,
1.19 nicm 160: &cmd_lock_session_entry,
1.63 nicm 161: &cmd_move_pane_entry,
1.1 nicm 162: &cmd_move_window_entry,
163: &cmd_new_session_entry,
164: &cmd_new_window_entry,
165: &cmd_next_layout_entry,
166: &cmd_next_window_entry,
167: &cmd_paste_buffer_entry,
1.24 nicm 168: &cmd_pipe_pane_entry,
1.1 nicm 169: &cmd_previous_layout_entry,
170: &cmd_previous_window_entry,
171: &cmd_refresh_client_entry,
172: &cmd_rename_session_entry,
173: &cmd_rename_window_entry,
174: &cmd_resize_pane_entry,
1.56 nicm 175: &cmd_respawn_pane_entry,
1.1 nicm 176: &cmd_respawn_window_entry,
177: &cmd_rotate_window_entry,
1.17 nicm 178: &cmd_run_shell_entry,
1.1 nicm 179: &cmd_save_buffer_entry,
180: &cmd_select_layout_entry,
181: &cmd_select_pane_entry,
182: &cmd_select_window_entry,
183: &cmd_send_keys_entry,
184: &cmd_send_prefix_entry,
185: &cmd_server_info_entry,
186: &cmd_set_buffer_entry,
1.13 nicm 187: &cmd_set_environment_entry,
1.107 nicm 188: &cmd_set_hook_entry,
1.1 nicm 189: &cmd_set_option_entry,
190: &cmd_set_window_option_entry,
191: &cmd_show_buffer_entry,
1.13 nicm 192: &cmd_show_environment_entry,
1.107 nicm 193: &cmd_show_hooks_entry,
1.32 nicm 194: &cmd_show_messages_entry,
1.1 nicm 195: &cmd_show_options_entry,
196: &cmd_show_window_options_entry,
197: &cmd_source_file_entry,
198: &cmd_split_window_entry,
199: &cmd_start_server_entry,
200: &cmd_suspend_client_entry,
201: &cmd_swap_pane_entry,
202: &cmd_swap_window_entry,
203: &cmd_switch_client_entry,
204: &cmd_unbind_key_entry,
205: &cmd_unlink_window_entry,
1.82 nicm 206: &cmd_wait_for_entry,
1.1 nicm 207: NULL
208: };
209:
1.108 nicm 210: static void cmd_clear_state(struct cmd_state *);
211: static struct client *cmd_get_state_client(struct cmd_q *, int);
212: static int cmd_set_state_flag(struct cmd *, struct cmd_q *, char);
213:
1.10 nicm 214: int
215: cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
216: {
217: size_t arglen;
218: int i;
1.91 nicm 219:
220: if (argc == 0)
221: return (0);
1.10 nicm 222:
223: *buf = '\0';
224: for (i = 0; i < argc; i++) {
225: if (strlcpy(buf, argv[i], len) >= len)
226: return (-1);
227: arglen = strlen(argv[i]) + 1;
228: buf += arglen;
229: len -= arglen;
230: }
231:
232: return (0);
233: }
234:
235: int
236: cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
237: {
238: int i;
239: size_t arglen;
240:
241: if (argc == 0)
242: return (0);
243: *argv = xcalloc(argc, sizeof **argv);
244:
245: buf[len - 1] = '\0';
246: for (i = 0; i < argc; i++) {
247: if (len == 0) {
248: cmd_free_argv(argc, *argv);
249: return (-1);
250: }
251:
252: arglen = strlen(buf) + 1;
253: (*argv)[i] = xstrdup(buf);
254: buf += arglen;
255: len -= arglen;
256: }
257:
258: return (0);
1.46 nicm 259: }
260:
261: char **
1.93 nicm 262: cmd_copy_argv(int argc, char **argv)
1.46 nicm 263: {
264: char **new_argv;
265: int i;
266:
267: if (argc == 0)
268: return (NULL);
1.94 nicm 269: new_argv = xcalloc(argc + 1, sizeof *new_argv);
1.46 nicm 270: for (i = 0; i < argc; i++) {
271: if (argv[i] != NULL)
272: new_argv[i] = xstrdup(argv[i]);
273: }
274: return (new_argv);
1.10 nicm 275: }
276:
277: void
278: cmd_free_argv(int argc, char **argv)
279: {
280: int i;
281:
282: if (argc == 0)
1.35 nicm 283: return;
1.68 nicm 284: for (i = 0; i < argc; i++)
285: free(argv[i]);
286: free(argv);
1.94 nicm 287: }
288:
289: char *
290: cmd_stringify_argv(int argc, char **argv)
291: {
292: char *buf;
293: int i;
294: size_t len;
295:
296: if (argc == 0)
297: return (xstrdup(""));
298:
299: len = 0;
300: buf = NULL;
301:
302: for (i = 0; i < argc; i++) {
303: len += strlen(argv[i]) + 1;
1.98 nicm 304: buf = xrealloc(buf, len);
1.94 nicm 305:
306: if (i == 0)
307: *buf = '\0';
308: else
309: strlcat(buf, " ", len);
310: strlcat(buf, argv[i], len);
311: }
312: return (buf);
1.10 nicm 313: }
314:
1.1 nicm 315: struct cmd *
1.80 nicm 316: cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause)
1.1 nicm 317: {
318: const struct cmd_entry **entryp, *entry;
1.27 deraadt 319: struct cmd *cmd;
1.49 nicm 320: struct args *args;
1.1 nicm 321: char s[BUFSIZ];
1.49 nicm 322: int ambiguous = 0;
1.1 nicm 323:
324: *cause = NULL;
1.2 nicm 325: if (argc == 0) {
326: xasprintf(cause, "no command");
1.1 nicm 327: return (NULL);
1.2 nicm 328: }
1.1 nicm 329:
330: entry = NULL;
331: for (entryp = cmd_table; *entryp != NULL; entryp++) {
332: if ((*entryp)->alias != NULL &&
333: strcmp((*entryp)->alias, argv[0]) == 0) {
1.3 nicm 334: ambiguous = 0;
1.1 nicm 335: entry = *entryp;
336: break;
337: }
338:
339: if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
340: continue;
341: if (entry != NULL)
1.3 nicm 342: ambiguous = 1;
1.1 nicm 343: entry = *entryp;
344:
345: /* Bail now if an exact match. */
346: if (strcmp(entry->name, argv[0]) == 0)
347: break;
348: }
1.3 nicm 349: if (ambiguous)
350: goto ambiguous;
1.1 nicm 351: if (entry == NULL) {
352: xasprintf(cause, "unknown command: %s", argv[0]);
353: return (NULL);
354: }
355:
1.49 nicm 356: args = args_parse(entry->args_template, argc, argv);
357: if (args == NULL)
358: goto usage;
359: if (entry->args_lower != -1 && args->argc < entry->args_lower)
360: goto usage;
361: if (entry->args_upper != -1 && args->argc > entry->args_upper)
362: goto usage;
1.1 nicm 363:
1.80 nicm 364: cmd = xcalloc(1, sizeof *cmd);
1.1 nicm 365: cmd->entry = entry;
1.49 nicm 366: cmd->args = args;
1.80 nicm 367:
368: if (file != NULL)
369: cmd->file = xstrdup(file);
370: cmd->line = line;
371:
1.1 nicm 372: return (cmd);
373:
374: ambiguous:
375: *s = '\0';
376: for (entryp = cmd_table; *entryp != NULL; entryp++) {
377: if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
378: continue;
379: if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s)
380: break;
381: if (strlcat(s, ", ", sizeof s) >= sizeof s)
382: break;
383: }
384: s[strlen(s) - 2] = '\0';
385: xasprintf(cause, "ambiguous command: %s, could be: %s", argv[0], s);
386: return (NULL);
387:
388: usage:
1.49 nicm 389: if (args != NULL)
390: args_free(args);
1.1 nicm 391: xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
392: return (NULL);
1.108 nicm 393: }
394:
395: static void
396: cmd_clear_state(struct cmd_state *state)
397: {
398: state->c = NULL;
399:
400: state->tflag.s = NULL;
401: state->tflag.wl = NULL;
402: state->tflag.wp = NULL;
403: state->tflag.idx = -1;
404:
405: state->sflag.s = NULL;
406: state->sflag.wl = NULL;
407: state->sflag.wp = NULL;
408: state->sflag.idx = -1;
409: }
410:
411: static struct client *
412: cmd_get_state_client(struct cmd_q *cmdq, int quiet)
413: {
414: struct cmd *cmd = cmdq->cmd;
415: struct args *args = cmd->args;
416:
417: switch (cmd->entry->flags & (CMD_CLIENT_C|CMD_CLIENT_T)) {
418: case 0:
419: return (cmd_find_client(cmdq, NULL, 1));
420: case CMD_CLIENT_C:
421: return (cmd_find_client(cmdq, args_get(args, 'c'), quiet));
422: case CMD_CLIENT_T:
423: return (cmd_find_client(cmdq, args_get(args, 't'), quiet));
424: default:
425: fatalx("both -t and -c for %s", cmd->entry->name);
426: }
427: }
428:
429: static int
430: cmd_set_state_flag(struct cmd *cmd, struct cmd_q *cmdq, char c)
431: {
432: struct cmd_state *state = &cmdq->state;
433: struct cmd_state_flag *statef = NULL;
434: const char *flag;
435: int flags = cmd->entry->flags, everything = 0;
1.110 ! nicm 436: int allflags = 0, targetflags;
1.108 nicm 437: struct session *s;
438: struct window *w;
439: struct winlink *wl;
440: struct window_pane *wp;
1.110 ! nicm 441: struct cmd_find_state *fs;
1.108 nicm 442:
1.110 ! nicm 443: /* Set up state for either -t or -s. */
1.108 nicm 444: if (c == 't') {
445: statef = &cmdq->state.tflag;
446: allflags = CMD_ALL_T;
447: } else if (c == 's') {
448: statef = &cmdq->state.sflag;
449: allflags = CMD_ALL_S;
450: }
451:
452: /*
453: * If the command wants something and no argument is present, use the
454: * base command instead.
455: */
456: flag = args_get(cmd->args, c);
457: if (flag == NULL) {
458: if ((flags & allflags) == 0)
459: return (0); /* doesn't care about flag */
460: cmd = cmdq->cmd;
461: everything = 1;
462: flag = args_get(cmd->args, c);
463: }
464:
465: /*
466: * If no flag and the current command is allowed to fail, just skip to
467: * fill in as much we can, otherwise continue and fail later if needed.
468: */
469: if (flag == NULL && (flags & CMD_CANFAIL))
470: goto complete_everything;
471:
472: /* Fill in state using command (current or base) flags. */
1.110 ! nicm 473: if (flags & CMD_PREFERUNATTACHED)
! 474: targetflags = CMD_FIND_PREFER_UNATTACHED;
! 475: else
! 476: targetflags = 0;
1.108 nicm 477: switch (cmd->entry->flags & allflags) {
478: case 0:
479: break;
480: case CMD_SESSION_T|CMD_PANE_T:
481: case CMD_SESSION_S|CMD_PANE_S:
482: if (flag != NULL && flag[strcspn(flag, ":.")] != '\0') {
1.110 ! nicm 483: fs = cmd_find_target(cmdq, flag, CMD_FIND_PANE,
! 484: targetflags);
! 485: if (fs == NULL)
1.108 nicm 486: return (-1);
1.110 ! nicm 487: statef->s = fs->s;
! 488: statef->wl = fs->wl;
! 489: statef->wp = fs->wp;
1.108 nicm 490: } else {
1.110 ! nicm 491: fs = cmd_find_target(cmdq, flag, CMD_FIND_SESSION,
! 492: targetflags);
! 493: if (fs == NULL)
1.108 nicm 494: return (-1);
1.110 ! nicm 495: statef->s = fs->s;
1.108 nicm 496:
497: if (flag == NULL) {
1.110 ! nicm 498: statef->wl = statef->s->curw;
! 499: statef->wp = statef->s->curw->window->active;
1.108 nicm 500: } else {
1.110 ! nicm 501: s = statef->s;
1.108 nicm 502: if ((w = window_find_by_id_str(flag)) != NULL)
503: wp = w->active;
504: else {
505: wp = window_pane_find_by_id_str(flag);
506: if (wp != NULL)
507: w = wp->window;
508: }
509: wl = winlink_find_by_window(&s->windows, w);
510: if (wl != NULL) {
511: statef->wl = wl;
512: statef->wp = wp;
513: }
514: }
515: }
516: break;
517: case CMD_MOVEW_R|CMD_INDEX_T:
518: case CMD_MOVEW_R|CMD_INDEX_S:
1.110 ! nicm 519: fs = cmd_find_target(cmdq, flag, CMD_FIND_SESSION, targetflags);
! 520: if (fs != NULL)
! 521: statef->s = fs->s;
! 522: else {
! 523: fs = cmd_find_target(cmdq, flag, CMD_FIND_WINDOW,
! 524: CMD_FIND_WINDOW_INDEX);
! 525: if (fs == NULL)
1.108 nicm 526: return (-1);
1.110 ! nicm 527: statef->s = fs->s;
! 528: statef->idx = fs->idx;
1.108 nicm 529: }
530: break;
531: case CMD_SESSION_T:
532: case CMD_SESSION_S:
1.110 ! nicm 533: fs = cmd_find_target(cmdq, flag, CMD_FIND_SESSION, targetflags);
! 534: if (fs == NULL)
1.108 nicm 535: return (-1);
1.110 ! nicm 536: statef->s = fs->s;
1.108 nicm 537: break;
1.110 ! nicm 538: case CMD_WINDOW_MARKED_T:
! 539: case CMD_WINDOW_MARKED_S:
! 540: targetflags |= CMD_FIND_DEFAULT_MARKED;
! 541: /* FALLTHROUGH */
1.108 nicm 542: case CMD_WINDOW_T:
543: case CMD_WINDOW_S:
1.110 ! nicm 544: fs = cmd_find_target(cmdq, flag, CMD_FIND_WINDOW, targetflags);
! 545: if (fs == NULL)
1.108 nicm 546: return (-1);
1.110 ! nicm 547: statef->s = fs->s;
! 548: statef->wl = fs->wl;
1.108 nicm 549: break;
1.110 ! nicm 550: case CMD_PANE_MARKED_T:
! 551: case CMD_PANE_MARKED_S:
! 552: targetflags |= CMD_FIND_DEFAULT_MARKED;
! 553: /* FALLTHROUGH */
1.108 nicm 554: case CMD_PANE_T:
555: case CMD_PANE_S:
1.110 ! nicm 556: fs = cmd_find_target(cmdq, flag, CMD_FIND_PANE, targetflags);
! 557: if (fs == NULL)
1.108 nicm 558: return (-1);
1.110 ! nicm 559: statef->s = fs->s;
! 560: statef->wl = fs->wl;
! 561: statef->wp = fs->wp;
1.108 nicm 562: break;
563: case CMD_INDEX_T:
564: case CMD_INDEX_S:
1.110 ! nicm 565: fs = cmd_find_target(cmdq, flag, CMD_FIND_WINDOW,
! 566: CMD_FIND_WINDOW_INDEX);
! 567: if (fs == NULL)
1.108 nicm 568: return (-1);
1.110 ! nicm 569: statef->s = fs->s;
! 570: statef->idx = fs->idx;
1.108 nicm 571: break;
572: default:
573: fatalx("too many -%c for %s", c, cmd->entry->name);
574: }
575:
576: /*
577: * If this is still the current command, it wants what it asked for and
578: * nothing more. If it's the base command, fill in as much as possible
579: * because the current command may have different flags.
580: */
581: if (!everything)
582: return (0);
583:
584: complete_everything:
585: if (statef->s == NULL) {
586: if (state->c != NULL)
587: statef->s = state->c->session;
1.110 ! nicm 588: if (statef->s == NULL) {
! 589: fs = cmd_find_target(cmdq, NULL, CMD_FIND_SESSION,
! 590: CMD_FIND_QUIET);
! 591: if (fs != NULL)
! 592: statef->s = fs->s;
! 593: }
1.108 nicm 594: if (statef->s == NULL) {
595: if (flags & CMD_CANFAIL)
596: return (0);
597: cmdq_error(cmdq, "no current session");
598: return (-1);
599: }
600: }
1.110 ! nicm 601: if (statef->wl == NULL) {
! 602: fs = cmd_find_target(cmdq, flag, CMD_FIND_WINDOW, 0);
! 603: if (fs != NULL) {
! 604: statef->s = fs->s;
! 605: statef->wl = fs->wl;
! 606: }
! 607: }
! 608: if (statef->wp == NULL) {
! 609: fs = cmd_find_target(cmdq, flag, CMD_FIND_PANE, 0);
! 610: if (fs != NULL) {
! 611: statef->s = fs->s;
! 612: statef->wl = fs->wl;
! 613: statef->wp = fs->wp;
! 614: }
! 615: }
1.108 nicm 616: return (0);
617: }
618:
619: int
620: cmd_prepare_state(struct cmd *cmd, struct cmd_q *cmdq)
621: {
622: struct cmd_state *state = &cmdq->state;
623: struct args *args = cmd->args;
624: const char *cflag, *tflag;
625: char *tmp;
626: int error;
627:
628: tmp = cmd_print(cmd);
1.109 nicm 629: log_debug("preparing state for: %s (client %p)", tmp, cmdq->client);
1.108 nicm 630: free(tmp);
631:
632: /* Start with an empty state. */
633: cmd_clear_state(state);
634:
635: /*
636: * If the command wants a client and provides -c or -t, use it. If not,
637: * try the base command instead via cmd_get_state_client. No client is
638: * allowed if no flags, otherwise it must be available.
639: */
640: switch (cmd->entry->flags & (CMD_CLIENT_C|CMD_CLIENT_T)) {
641: case 0:
642: state->c = cmd_get_state_client(cmdq, 1);
643: break;
644: case CMD_CLIENT_C:
645: cflag = args_get(args, 'c');
646: if (cflag == NULL)
647: state->c = cmd_get_state_client(cmdq, 0);
648: else
649: state->c = cmd_find_client(cmdq, cflag, 0);
650: if (state->c == NULL)
651: return (-1);
652: break;
653: case CMD_CLIENT_T:
654: tflag = args_get(args, 't');
655: if (tflag == NULL)
656: state->c = cmd_get_state_client(cmdq, 0);
657: else
658: state->c = cmd_find_client(cmdq, tflag, 0);
659: if (state->c == NULL)
660: return (-1);
661: break;
662: default:
663: fatalx("both -c and -t for %s", cmd->entry->name);
664: }
665:
666: error = cmd_set_state_flag(cmd, cmdq, 't');
667: if (error == 0)
668: error = cmd_set_state_flag(cmd, cmdq, 's');
669: return (error);
1.1 nicm 670: }
671:
1.106 nicm 672: char *
673: cmd_print(struct cmd *cmd)
1.1 nicm 674: {
1.106 nicm 675: char *out, *s;
676:
677: s = args_print(cmd->args);
678: if (*s != '\0')
679: xasprintf(&out, "%s %s", cmd->entry->name, s);
680: else
681: out = xstrdup(cmd->entry->name);
682: free(s);
1.49 nicm 683:
1.106 nicm 684: return (out);
1.1 nicm 685: }
686:
1.99 nicm 687: /* Adjust current mouse position for a pane. */
688: int
689: cmd_mouse_at(struct window_pane *wp, struct mouse_event *m, u_int *xp,
690: u_int *yp, int last)
691: {
692: u_int x, y;
693:
694: if (last) {
695: x = m->lx;
696: y = m->ly;
697: } else {
698: x = m->x;
699: y = m->y;
700: }
701:
702: if (m->statusat == 0 && y > 0)
703: y--;
704: else if (m->statusat > 0 && y >= (u_int)m->statusat)
705: y = m->statusat - 1;
706:
707: if (x < wp->xoff || x >= wp->xoff + wp->sx)
708: return (-1);
709: if (y < wp->yoff || y >= wp->yoff + wp->sy)
710: return (-1);
711:
712: *xp = x - wp->xoff;
713: *yp = y - wp->yoff;
714: return (0);
715: }
716:
717: /* Get current mouse window if any. */
718: struct winlink *
719: cmd_mouse_window(struct mouse_event *m, struct session **sp)
720: {
721: struct session *s;
722: struct window *w;
723:
724: if (!m->valid || m->s == -1 || m->w == -1)
725: return (NULL);
726: if ((s = session_find_by_id(m->s)) == NULL)
727: return (NULL);
728: if ((w = window_find_by_id(m->w)) == NULL)
729: return (NULL);
730:
731: if (sp != NULL)
732: *sp = s;
733: return (winlink_find_by_window(&s->windows, w));
734: }
735:
736: /* Get current mouse pane if any. */
737: struct window_pane *
1.104 nicm 738: cmd_mouse_pane(struct mouse_event *m, struct session **sp,
739: struct winlink **wlp)
1.99 nicm 740: {
741: struct winlink *wl;
742: struct window_pane *wp;
743:
744: if ((wl = cmd_mouse_window(m, sp)) == NULL)
745: return (NULL);
746: if ((wp = window_pane_find_by_id(m->wp)) == NULL)
747: return (NULL);
748: if (!window_has_pane(wl->window, wp))
749: return (NULL);
750:
751: if (wlp != NULL)
752: *wlp = wl;
1.43 nicm 753: return (wp);
1.15 nicm 754: }
755:
756: /* Replace the first %% or %idx in template by s. */
757: char *
1.75 nicm 758: cmd_template_replace(const char *template, const char *s, int idx)
1.15 nicm 759: {
1.75 nicm 760: char ch, *buf;
761: const char *ptr;
1.84 nicm 762: int replaced;
763: size_t len;
1.15 nicm 764:
1.76 nicm 765: if (strchr(template, '%') == NULL)
1.15 nicm 766: return (xstrdup(template));
767:
768: buf = xmalloc(1);
769: *buf = '\0';
770: len = 0;
771: replaced = 0;
772:
773: ptr = template;
774: while (*ptr != '\0') {
775: switch (ch = *ptr++) {
776: case '%':
777: if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
778: if (*ptr != '%' || replaced)
779: break;
780: replaced = 1;
781: }
782: ptr++;
783:
784: len += strlen(s);
1.98 nicm 785: buf = xrealloc(buf, len + 1);
1.15 nicm 786: strlcat(buf, s, len + 1);
787: continue;
788: }
1.98 nicm 789: buf = xrealloc(buf, len + 2);
1.15 nicm 790: buf[len++] = ch;
791: buf[len] = '\0';
792: }
793:
794: return (buf);
1.1 nicm 795: }