Annotation of src/usr.bin/tmux/cmd.c, Revision 1.115
1.115 ! nicm 1: /* $OpenBSD: cmd.c,v 1.114 2015/12/13 18:27:47 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.114 nicm 210: static void cmd_clear_state(struct cmd_state *);
211: static int cmd_set_state_flag(struct cmd *, struct cmd_q *, char);
1.108 nicm 212:
1.10 nicm 213: int
214: cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
215: {
216: size_t arglen;
217: int i;
1.91 nicm 218:
219: if (argc == 0)
220: return (0);
1.10 nicm 221:
222: *buf = '\0';
223: for (i = 0; i < argc; i++) {
224: if (strlcpy(buf, argv[i], len) >= len)
225: return (-1);
226: arglen = strlen(argv[i]) + 1;
227: buf += arglen;
228: len -= arglen;
229: }
230:
231: return (0);
232: }
233:
234: int
235: cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
236: {
237: int i;
238: size_t arglen;
239:
240: if (argc == 0)
241: return (0);
242: *argv = xcalloc(argc, sizeof **argv);
243:
244: buf[len - 1] = '\0';
245: for (i = 0; i < argc; i++) {
246: if (len == 0) {
247: cmd_free_argv(argc, *argv);
248: return (-1);
249: }
250:
251: arglen = strlen(buf) + 1;
252: (*argv)[i] = xstrdup(buf);
253: buf += arglen;
254: len -= arglen;
255: }
256:
257: return (0);
1.46 nicm 258: }
259:
260: char **
1.93 nicm 261: cmd_copy_argv(int argc, char **argv)
1.46 nicm 262: {
263: char **new_argv;
264: int i;
265:
266: if (argc == 0)
267: return (NULL);
1.94 nicm 268: new_argv = xcalloc(argc + 1, sizeof *new_argv);
1.46 nicm 269: for (i = 0; i < argc; i++) {
270: if (argv[i] != NULL)
271: new_argv[i] = xstrdup(argv[i]);
272: }
273: return (new_argv);
1.10 nicm 274: }
275:
276: void
277: cmd_free_argv(int argc, char **argv)
278: {
279: int i;
280:
281: if (argc == 0)
1.35 nicm 282: return;
1.68 nicm 283: for (i = 0; i < argc; i++)
284: free(argv[i]);
285: free(argv);
1.94 nicm 286: }
287:
288: char *
289: cmd_stringify_argv(int argc, char **argv)
290: {
291: char *buf;
292: int i;
293: size_t len;
294:
295: if (argc == 0)
296: return (xstrdup(""));
297:
298: len = 0;
299: buf = NULL;
300:
301: for (i = 0; i < argc; i++) {
302: len += strlen(argv[i]) + 1;
1.98 nicm 303: buf = xrealloc(buf, len);
1.94 nicm 304:
305: if (i == 0)
306: *buf = '\0';
307: else
308: strlcat(buf, " ", len);
309: strlcat(buf, argv[i], len);
310: }
311: return (buf);
1.10 nicm 312: }
313:
1.1 nicm 314: struct cmd *
1.80 nicm 315: cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause)
1.1 nicm 316: {
317: const struct cmd_entry **entryp, *entry;
1.27 deraadt 318: struct cmd *cmd;
1.49 nicm 319: struct args *args;
1.1 nicm 320: char s[BUFSIZ];
1.49 nicm 321: int ambiguous = 0;
1.1 nicm 322:
323: *cause = NULL;
1.2 nicm 324: if (argc == 0) {
325: xasprintf(cause, "no command");
1.1 nicm 326: return (NULL);
1.2 nicm 327: }
1.1 nicm 328:
329: entry = NULL;
330: for (entryp = cmd_table; *entryp != NULL; entryp++) {
331: if ((*entryp)->alias != NULL &&
332: strcmp((*entryp)->alias, argv[0]) == 0) {
1.3 nicm 333: ambiguous = 0;
1.1 nicm 334: entry = *entryp;
335: break;
336: }
337:
338: if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
339: continue;
340: if (entry != NULL)
1.3 nicm 341: ambiguous = 1;
1.1 nicm 342: entry = *entryp;
343:
344: /* Bail now if an exact match. */
345: if (strcmp(entry->name, argv[0]) == 0)
346: break;
347: }
1.3 nicm 348: if (ambiguous)
349: goto ambiguous;
1.1 nicm 350: if (entry == NULL) {
351: xasprintf(cause, "unknown command: %s", argv[0]);
352: return (NULL);
353: }
354:
1.49 nicm 355: args = args_parse(entry->args_template, argc, argv);
356: if (args == NULL)
357: goto usage;
358: if (entry->args_lower != -1 && args->argc < entry->args_lower)
359: goto usage;
360: if (entry->args_upper != -1 && args->argc > entry->args_upper)
361: goto usage;
1.1 nicm 362:
1.80 nicm 363: cmd = xcalloc(1, sizeof *cmd);
1.1 nicm 364: cmd->entry = entry;
1.49 nicm 365: cmd->args = args;
1.80 nicm 366:
367: if (file != NULL)
368: cmd->file = xstrdup(file);
369: cmd->line = line;
370:
1.1 nicm 371: return (cmd);
372:
373: ambiguous:
374: *s = '\0';
375: for (entryp = cmd_table; *entryp != NULL; entryp++) {
376: if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
377: continue;
378: if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s)
379: break;
380: if (strlcat(s, ", ", sizeof s) >= sizeof s)
381: break;
382: }
383: s[strlen(s) - 2] = '\0';
384: xasprintf(cause, "ambiguous command: %s, could be: %s", argv[0], s);
385: return (NULL);
386:
387: usage:
1.49 nicm 388: if (args != NULL)
389: args_free(args);
1.1 nicm 390: xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
391: return (NULL);
1.108 nicm 392: }
393:
394: static void
395: cmd_clear_state(struct cmd_state *state)
396: {
397: state->c = NULL;
398:
399: state->tflag.s = NULL;
400: state->tflag.wl = NULL;
401: state->tflag.wp = NULL;
402: state->tflag.idx = -1;
403:
404: state->sflag.s = NULL;
405: state->sflag.wl = NULL;
406: state->sflag.wp = NULL;
407: state->sflag.idx = -1;
408: }
409:
410: static int
411: cmd_set_state_flag(struct cmd *cmd, struct cmd_q *cmdq, char c)
412: {
413: struct cmd_state *state = &cmdq->state;
1.112 nicm 414: struct cmd_find_state *fsf = NULL;
1.108 nicm 415: const char *flag;
416: int flags = cmd->entry->flags, everything = 0;
1.111 nicm 417: int allflags = 0, targetflags, error;
1.108 nicm 418: struct session *s;
419: struct window *w;
420: struct winlink *wl;
421: struct window_pane *wp;
422:
1.110 nicm 423: /* Set up state for either -t or -s. */
1.108 nicm 424: if (c == 't') {
1.112 nicm 425: fsf = &cmdq->state.tflag;
1.108 nicm 426: allflags = CMD_ALL_T;
427: } else if (c == 's') {
1.112 nicm 428: fsf = &cmdq->state.sflag;
1.108 nicm 429: allflags = CMD_ALL_S;
430: }
431:
432: /*
433: * If the command wants something and no argument is present, use the
434: * base command instead.
435: */
436: flag = args_get(cmd->args, c);
437: if (flag == NULL) {
438: if ((flags & allflags) == 0)
439: return (0); /* doesn't care about flag */
440: cmd = cmdq->cmd;
441: everything = 1;
442: flag = args_get(cmd->args, c);
443: }
444:
445: /*
446: * If no flag and the current command is allowed to fail, just skip to
447: * fill in as much we can, otherwise continue and fail later if needed.
448: */
449: if (flag == NULL && (flags & CMD_CANFAIL))
450: goto complete_everything;
451:
452: /* Fill in state using command (current or base) flags. */
1.110 nicm 453: if (flags & CMD_PREFERUNATTACHED)
454: targetflags = CMD_FIND_PREFER_UNATTACHED;
455: else
456: targetflags = 0;
1.108 nicm 457: switch (cmd->entry->flags & allflags) {
458: case 0:
459: break;
460: case CMD_SESSION_T|CMD_PANE_T:
461: case CMD_SESSION_S|CMD_PANE_S:
462: if (flag != NULL && flag[strcspn(flag, ":.")] != '\0') {
1.112 nicm 463: error = cmd_find_target(fsf, cmdq, flag, CMD_FIND_PANE,
1.110 nicm 464: targetflags);
1.111 nicm 465: if (error != 0)
1.108 nicm 466: return (-1);
467: } else {
1.112 nicm 468: error = cmd_find_target(fsf, cmdq, flag,
1.111 nicm 469: CMD_FIND_SESSION, targetflags);
470: if (error != 0)
1.108 nicm 471: return (-1);
472:
473: if (flag == NULL) {
1.112 nicm 474: fsf->wl = fsf->s->curw;
475: fsf->wp = fsf->s->curw->window->active;
1.108 nicm 476: } else {
1.112 nicm 477: s = fsf->s;
1.108 nicm 478: if ((w = window_find_by_id_str(flag)) != NULL)
479: wp = w->active;
480: else {
481: wp = window_pane_find_by_id_str(flag);
482: if (wp != NULL)
483: w = wp->window;
484: }
485: wl = winlink_find_by_window(&s->windows, w);
486: if (wl != NULL) {
1.112 nicm 487: fsf->wl = wl;
488: fsf->wp = wp;
1.108 nicm 489: }
490: }
491: }
492: break;
493: case CMD_MOVEW_R|CMD_INDEX_T:
494: case CMD_MOVEW_R|CMD_INDEX_S:
1.112 nicm 495: error = cmd_find_target(fsf, cmdq, flag, CMD_FIND_SESSION,
1.113 nicm 496: targetflags|CMD_FIND_QUIET);
1.112 nicm 497: if (error != 0) {
498: error = cmd_find_target(fsf, cmdq, flag,
1.111 nicm 499: CMD_FIND_WINDOW, CMD_FIND_WINDOW_INDEX);
500: if (error != 0)
1.108 nicm 501: return (-1);
502: }
503: break;
504: case CMD_SESSION_T:
505: case CMD_SESSION_S:
1.112 nicm 506: error = cmd_find_target(fsf, cmdq, flag, CMD_FIND_SESSION,
1.111 nicm 507: targetflags);
508: if (error != 0)
1.108 nicm 509: return (-1);
510: break;
1.110 nicm 511: case CMD_WINDOW_MARKED_T:
512: case CMD_WINDOW_MARKED_S:
513: targetflags |= CMD_FIND_DEFAULT_MARKED;
514: /* FALLTHROUGH */
1.108 nicm 515: case CMD_WINDOW_T:
516: case CMD_WINDOW_S:
1.112 nicm 517: error = cmd_find_target(fsf, cmdq, flag, CMD_FIND_WINDOW,
1.111 nicm 518: targetflags);
519: if (error != 0)
1.108 nicm 520: return (-1);
521: break;
1.110 nicm 522: case CMD_PANE_MARKED_T:
523: case CMD_PANE_MARKED_S:
524: targetflags |= CMD_FIND_DEFAULT_MARKED;
525: /* FALLTHROUGH */
1.108 nicm 526: case CMD_PANE_T:
527: case CMD_PANE_S:
1.112 nicm 528: error = cmd_find_target(fsf, cmdq, flag, CMD_FIND_PANE,
1.111 nicm 529: targetflags);
530: if (error != 0)
1.108 nicm 531: return (-1);
532: break;
533: case CMD_INDEX_T:
534: case CMD_INDEX_S:
1.112 nicm 535: error = cmd_find_target(fsf, cmdq, flag, CMD_FIND_WINDOW,
1.110 nicm 536: CMD_FIND_WINDOW_INDEX);
1.111 nicm 537: if (error != 0)
1.108 nicm 538: return (-1);
539: break;
540: default:
541: fatalx("too many -%c for %s", c, cmd->entry->name);
542: }
543:
544: /*
545: * If this is still the current command, it wants what it asked for and
546: * nothing more. If it's the base command, fill in as much as possible
547: * because the current command may have different flags.
548: */
549: if (!everything)
550: return (0);
551:
552: complete_everything:
1.112 nicm 553: if (fsf->s == NULL) {
1.108 nicm 554: if (state->c != NULL)
1.112 nicm 555: fsf->s = state->c->session;
556: if (fsf->s == NULL) {
557: error = cmd_find_target(fsf, cmdq, NULL,
1.111 nicm 558: CMD_FIND_SESSION, CMD_FIND_QUIET);
1.112 nicm 559: if (error != 0)
560: fsf->s = NULL;
1.110 nicm 561: }
1.112 nicm 562: if (fsf->s == NULL) {
1.108 nicm 563: if (flags & CMD_CANFAIL)
564: return (0);
565: cmdq_error(cmdq, "no current session");
566: return (-1);
567: }
568: }
1.112 nicm 569: if (fsf->wl == NULL) {
570: error = cmd_find_target(fsf, cmdq, flag, CMD_FIND_WINDOW, 0);
571: if (error != 0)
572: return (-1);
1.110 nicm 573: }
1.112 nicm 574: if (fsf->wp == NULL) {
575: error = cmd_find_target(fsf, cmdq, flag, CMD_FIND_PANE, 0);
576: if (error != 0)
577: return (-1);
1.110 nicm 578: }
1.108 nicm 579: return (0);
580: }
581:
582: int
583: cmd_prepare_state(struct cmd *cmd, struct cmd_q *cmdq)
584: {
585: struct cmd_state *state = &cmdq->state;
586: struct args *args = cmd->args;
587: char *tmp;
1.115 ! nicm 588: int error, quiet;
1.108 nicm 589:
590: tmp = cmd_print(cmd);
1.109 nicm 591: log_debug("preparing state for: %s (client %p)", tmp, cmdq->client);
1.108 nicm 592: free(tmp);
593:
594: /* Start with an empty state. */
595: cmd_clear_state(state);
596:
1.115 ! nicm 597: /* No error messages if can fail. */
! 598: quiet = 0;
! 599: if (cmd->entry->flags & CMD_CLIENT_CANFAIL)
! 600: quiet = 1;
! 601:
1.108 nicm 602: /*
603: * If the command wants a client and provides -c or -t, use it. If not,
604: * try the base command instead via cmd_get_state_client. No client is
605: * allowed if no flags, otherwise it must be available.
606: */
607: switch (cmd->entry->flags & (CMD_CLIENT_C|CMD_CLIENT_T)) {
608: case 0:
1.114 nicm 609: state->c = cmd_find_client(cmdq, NULL, 1);
1.108 nicm 610: break;
611: case CMD_CLIENT_C:
1.115 ! nicm 612: state->c = cmd_find_client(cmdq, args_get(args, 'c'), quiet);
! 613: if (!quiet && state->c == NULL)
1.108 nicm 614: return (-1);
615: break;
616: case CMD_CLIENT_T:
1.115 ! nicm 617: state->c = cmd_find_client(cmdq, args_get(args, 't'), quiet);
! 618: if (!quiet && state->c == NULL)
1.108 nicm 619: return (-1);
620: break;
621: default:
622: fatalx("both -c and -t for %s", cmd->entry->name);
623: }
624:
625: error = cmd_set_state_flag(cmd, cmdq, 't');
626: if (error == 0)
627: error = cmd_set_state_flag(cmd, cmdq, 's');
628: return (error);
1.1 nicm 629: }
630:
1.106 nicm 631: char *
632: cmd_print(struct cmd *cmd)
1.1 nicm 633: {
1.106 nicm 634: char *out, *s;
635:
636: s = args_print(cmd->args);
637: if (*s != '\0')
638: xasprintf(&out, "%s %s", cmd->entry->name, s);
639: else
640: out = xstrdup(cmd->entry->name);
641: free(s);
1.49 nicm 642:
1.106 nicm 643: return (out);
1.1 nicm 644: }
645:
1.99 nicm 646: /* Adjust current mouse position for a pane. */
647: int
648: cmd_mouse_at(struct window_pane *wp, struct mouse_event *m, u_int *xp,
649: u_int *yp, int last)
650: {
651: u_int x, y;
652:
653: if (last) {
654: x = m->lx;
655: y = m->ly;
656: } else {
657: x = m->x;
658: y = m->y;
659: }
660:
661: if (m->statusat == 0 && y > 0)
662: y--;
663: else if (m->statusat > 0 && y >= (u_int)m->statusat)
664: y = m->statusat - 1;
665:
666: if (x < wp->xoff || x >= wp->xoff + wp->sx)
667: return (-1);
668: if (y < wp->yoff || y >= wp->yoff + wp->sy)
669: return (-1);
670:
671: *xp = x - wp->xoff;
672: *yp = y - wp->yoff;
673: return (0);
674: }
675:
676: /* Get current mouse window if any. */
677: struct winlink *
678: cmd_mouse_window(struct mouse_event *m, struct session **sp)
679: {
680: struct session *s;
681: struct window *w;
682:
683: if (!m->valid || m->s == -1 || m->w == -1)
684: return (NULL);
685: if ((s = session_find_by_id(m->s)) == NULL)
686: return (NULL);
687: if ((w = window_find_by_id(m->w)) == NULL)
688: return (NULL);
689:
690: if (sp != NULL)
691: *sp = s;
692: return (winlink_find_by_window(&s->windows, w));
693: }
694:
695: /* Get current mouse pane if any. */
696: struct window_pane *
1.104 nicm 697: cmd_mouse_pane(struct mouse_event *m, struct session **sp,
698: struct winlink **wlp)
1.99 nicm 699: {
700: struct winlink *wl;
701: struct window_pane *wp;
702:
703: if ((wl = cmd_mouse_window(m, sp)) == NULL)
704: return (NULL);
705: if ((wp = window_pane_find_by_id(m->wp)) == NULL)
706: return (NULL);
707: if (!window_has_pane(wl->window, wp))
708: return (NULL);
709:
710: if (wlp != NULL)
711: *wlp = wl;
1.43 nicm 712: return (wp);
1.15 nicm 713: }
714:
715: /* Replace the first %% or %idx in template by s. */
716: char *
1.75 nicm 717: cmd_template_replace(const char *template, const char *s, int idx)
1.15 nicm 718: {
1.75 nicm 719: char ch, *buf;
720: const char *ptr;
1.84 nicm 721: int replaced;
722: size_t len;
1.15 nicm 723:
1.76 nicm 724: if (strchr(template, '%') == NULL)
1.15 nicm 725: return (xstrdup(template));
726:
727: buf = xmalloc(1);
728: *buf = '\0';
729: len = 0;
730: replaced = 0;
731:
732: ptr = template;
733: while (*ptr != '\0') {
734: switch (ch = *ptr++) {
735: case '%':
736: if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
737: if (*ptr != '%' || replaced)
738: break;
739: replaced = 1;
740: }
741: ptr++;
742:
743: len += strlen(s);
1.98 nicm 744: buf = xrealloc(buf, len + 1);
1.15 nicm 745: strlcat(buf, s, len + 1);
746: continue;
747: }
1.98 nicm 748: buf = xrealloc(buf, len + 2);
1.15 nicm 749: buf[len++] = ch;
750: buf[len] = '\0';
751: }
752:
753: return (buf);
1.1 nicm 754: }