Annotation of src/usr.bin/tmux/cmd.c, Revision 1.109
1.109 ! nicm 1: /* $OpenBSD: cmd.c,v 1.108 2015/12/13 14:32:38 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;
436: int allflags = 0;
437: int prefer = !!(flags & CMD_PREFERUNATTACHED);
438: struct session *s;
439: struct window *w;
440: struct winlink *wl;
441: struct window_pane *wp;
442:
443: if (c == 't') {
444: statef = &cmdq->state.tflag;
445: allflags = CMD_ALL_T;
446: } else if (c == 's') {
447: statef = &cmdq->state.sflag;
448: allflags = CMD_ALL_S;
449: }
450:
451: /*
452: * If the command wants something and no argument is present, use the
453: * base command instead.
454: */
455: flag = args_get(cmd->args, c);
456: if (flag == NULL) {
457: if ((flags & allflags) == 0)
458: return (0); /* doesn't care about flag */
459: cmd = cmdq->cmd;
460: everything = 1;
461: flag = args_get(cmd->args, c);
462: }
463:
464: /*
465: * If no flag and the current command is allowed to fail, just skip to
466: * fill in as much we can, otherwise continue and fail later if needed.
467: */
468: if (flag == NULL && (flags & CMD_CANFAIL))
469: goto complete_everything;
470:
471: /* Fill in state using command (current or base) flags. */
472: switch (cmd->entry->flags & allflags) {
473: case 0:
474: break;
475: case CMD_SESSION_T|CMD_PANE_T:
476: case CMD_SESSION_S|CMD_PANE_S:
477: if (flag != NULL && flag[strcspn(flag, ":.")] != '\0') {
478: statef->wl = cmd_find_pane(cmdq, flag, &statef->s,
479: &statef->wp);
480: if (statef->wl == NULL)
481: return (-1);
482: } else {
483: statef->s = cmd_find_session(cmdq, flag, prefer);
484: if (statef->s == NULL)
485: return (-1);
486:
487: s = statef->s;
488: if (flag == NULL) {
489: statef->wl = s->curw;
490: statef->wp = s->curw->window->active;
491: } else {
492: if ((w = window_find_by_id_str(flag)) != NULL)
493: wp = w->active;
494: else {
495: wp = window_pane_find_by_id_str(flag);
496: if (wp != NULL)
497: w = wp->window;
498: }
499: wl = winlink_find_by_window(&s->windows, w);
500: if (wl != NULL) {
501: statef->wl = wl;
502: statef->wp = wp;
503: }
504: }
505: }
506: break;
507: case CMD_MOVEW_R|CMD_INDEX_T:
508: case CMD_MOVEW_R|CMD_INDEX_S:
509: statef->s = cmd_find_session(cmdq, flag, prefer);
510: if (statef->s == NULL) {
511: statef->idx = cmd_find_index(cmdq, flag, &statef->s);
512: if (statef->idx == -2)
513: return (-1);
514: }
515: break;
516: case CMD_SESSION_T:
517: case CMD_SESSION_S:
518: statef->s = cmd_find_session(cmdq, flag, prefer);
519: if (statef->s == NULL)
520: return (-1);
521: break;
522: case CMD_WINDOW_T:
523: case CMD_WINDOW_S:
524: statef->wl = cmd_find_window(cmdq, flag, &statef->s);
525: if (statef->wl == NULL)
526: return (-1);
527: break;
528: case CMD_WINDOW_MARKED_T:
529: case CMD_WINDOW_MARKED_S:
530: statef->wl = cmd_find_window_marked(cmdq, flag, &statef->s);
531: if (statef->wl == NULL)
532: return (-1);
533: break;
534: case CMD_PANE_T:
535: case CMD_PANE_S:
536: statef->wl = cmd_find_pane(cmdq, flag, &statef->s,
537: &statef->wp);
538: if (statef->wl == NULL)
539: return (-1);
540: break;
1.109 ! nicm 541: case CMD_PANE_MARKED_T:
1.108 nicm 542: case CMD_PANE_MARKED_S:
543: statef->wl = cmd_find_pane_marked(cmdq, flag, &statef->s,
544: &statef->wp);
545: if (statef->wl == NULL)
546: return (-1);
547: break;
548: case CMD_INDEX_T:
549: case CMD_INDEX_S:
550: statef->idx = cmd_find_index(cmdq, flag, &statef->s);
551: if (statef->idx == -2)
552: return (-1);
553: break;
554: default:
555: fatalx("too many -%c for %s", c, cmd->entry->name);
556: }
557:
558: /*
559: * If this is still the current command, it wants what it asked for and
560: * nothing more. If it's the base command, fill in as much as possible
561: * because the current command may have different flags.
562: */
563: if (!everything)
564: return (0);
565:
566: complete_everything:
567: if (statef->s == NULL) {
568: if (state->c != NULL)
569: statef->s = state->c->session;
570: if (statef->s == NULL)
571: statef->s = cmd_find_current(cmdq);
572: if (statef->s == NULL) {
573: if (flags & CMD_CANFAIL)
574: return (0);
575:
576: cmdq_error(cmdq, "no current session");
577: return (-1);
578: }
579: }
580: if (statef->wl == NULL)
581: statef->wl = cmd_find_window(cmdq, flag, &statef->s);
582: if (statef->wp == NULL)
583: statef->wl = cmd_find_pane(cmdq, flag, &statef->s, &statef->wp);
584:
585: return (0);
586: }
587:
588: int
589: cmd_prepare_state(struct cmd *cmd, struct cmd_q *cmdq)
590: {
591: struct cmd_state *state = &cmdq->state;
592: struct args *args = cmd->args;
593: const char *cflag, *tflag;
594: char *tmp;
595: int error;
596:
597: tmp = cmd_print(cmd);
1.109 ! nicm 598: log_debug("preparing state for: %s (client %p)", tmp, cmdq->client);
1.108 nicm 599: free(tmp);
600:
601: /* Start with an empty state. */
602: cmd_clear_state(state);
603:
604: /*
605: * If the command wants a client and provides -c or -t, use it. If not,
606: * try the base command instead via cmd_get_state_client. No client is
607: * allowed if no flags, otherwise it must be available.
608: */
609: switch (cmd->entry->flags & (CMD_CLIENT_C|CMD_CLIENT_T)) {
610: case 0:
611: state->c = cmd_get_state_client(cmdq, 1);
612: break;
613: case CMD_CLIENT_C:
614: cflag = args_get(args, 'c');
615: if (cflag == NULL)
616: state->c = cmd_get_state_client(cmdq, 0);
617: else
618: state->c = cmd_find_client(cmdq, cflag, 0);
619: if (state->c == NULL)
620: return (-1);
621: break;
622: case CMD_CLIENT_T:
623: tflag = args_get(args, 't');
624: if (tflag == NULL)
625: state->c = cmd_get_state_client(cmdq, 0);
626: else
627: state->c = cmd_find_client(cmdq, tflag, 0);
628: if (state->c == NULL)
629: return (-1);
630: break;
631: default:
632: fatalx("both -c and -t for %s", cmd->entry->name);
633: }
634:
635: error = cmd_set_state_flag(cmd, cmdq, 't');
636: if (error == 0)
637: error = cmd_set_state_flag(cmd, cmdq, 's');
638: return (error);
1.1 nicm 639: }
640:
1.106 nicm 641: char *
642: cmd_print(struct cmd *cmd)
1.1 nicm 643: {
1.106 nicm 644: char *out, *s;
645:
646: s = args_print(cmd->args);
647: if (*s != '\0')
648: xasprintf(&out, "%s %s", cmd->entry->name, s);
649: else
650: out = xstrdup(cmd->entry->name);
651: free(s);
1.49 nicm 652:
1.106 nicm 653: return (out);
1.1 nicm 654: }
655:
1.99 nicm 656: /* Adjust current mouse position for a pane. */
657: int
658: cmd_mouse_at(struct window_pane *wp, struct mouse_event *m, u_int *xp,
659: u_int *yp, int last)
660: {
661: u_int x, y;
662:
663: if (last) {
664: x = m->lx;
665: y = m->ly;
666: } else {
667: x = m->x;
668: y = m->y;
669: }
670:
671: if (m->statusat == 0 && y > 0)
672: y--;
673: else if (m->statusat > 0 && y >= (u_int)m->statusat)
674: y = m->statusat - 1;
675:
676: if (x < wp->xoff || x >= wp->xoff + wp->sx)
677: return (-1);
678: if (y < wp->yoff || y >= wp->yoff + wp->sy)
679: return (-1);
680:
681: *xp = x - wp->xoff;
682: *yp = y - wp->yoff;
683: return (0);
684: }
685:
686: /* Get current mouse window if any. */
687: struct winlink *
688: cmd_mouse_window(struct mouse_event *m, struct session **sp)
689: {
690: struct session *s;
691: struct window *w;
692:
693: if (!m->valid || m->s == -1 || m->w == -1)
694: return (NULL);
695: if ((s = session_find_by_id(m->s)) == NULL)
696: return (NULL);
697: if ((w = window_find_by_id(m->w)) == NULL)
698: return (NULL);
699:
700: if (sp != NULL)
701: *sp = s;
702: return (winlink_find_by_window(&s->windows, w));
703: }
704:
705: /* Get current mouse pane if any. */
706: struct window_pane *
1.104 nicm 707: cmd_mouse_pane(struct mouse_event *m, struct session **sp,
708: struct winlink **wlp)
1.99 nicm 709: {
710: struct winlink *wl;
711: struct window_pane *wp;
712:
713: if ((wl = cmd_mouse_window(m, sp)) == NULL)
714: return (NULL);
715: if ((wp = window_pane_find_by_id(m->wp)) == NULL)
716: return (NULL);
717: if (!window_has_pane(wl->window, wp))
718: return (NULL);
719:
720: if (wlp != NULL)
721: *wlp = wl;
1.43 nicm 722: return (wp);
1.15 nicm 723: }
724:
725: /* Replace the first %% or %idx in template by s. */
726: char *
1.75 nicm 727: cmd_template_replace(const char *template, const char *s, int idx)
1.15 nicm 728: {
1.75 nicm 729: char ch, *buf;
730: const char *ptr;
1.84 nicm 731: int replaced;
732: size_t len;
1.15 nicm 733:
1.76 nicm 734: if (strchr(template, '%') == NULL)
1.15 nicm 735: return (xstrdup(template));
736:
737: buf = xmalloc(1);
738: *buf = '\0';
739: len = 0;
740: replaced = 0;
741:
742: ptr = template;
743: while (*ptr != '\0') {
744: switch (ch = *ptr++) {
745: case '%':
746: if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
747: if (*ptr != '%' || replaced)
748: break;
749: replaced = 1;
750: }
751: ptr++;
752:
753: len += strlen(s);
1.98 nicm 754: buf = xrealloc(buf, len + 1);
1.15 nicm 755: strlcat(buf, s, len + 1);
756: continue;
757: }
1.98 nicm 758: buf = xrealloc(buf, len + 2);
1.15 nicm 759: buf[len++] = ch;
760: buf[len] = '\0';
761: }
762:
763: return (buf);
1.1 nicm 764: }