mouse-select-pane has to redraw the borders now too.
[tmux-openbsd.git] / cmd.c
blob72fec8484261b1dc603cfc131f07b421d398dcb2
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
20 #include <sys/time.h>
22 #include <fnmatch.h>
23 #include <paths.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
28 #include "tmux.h"
30 const struct cmd_entry *cmd_table[] = {
31 &cmd_attach_session_entry,
32 &cmd_bind_key_entry,
33 &cmd_break_pane_entry,
34 &cmd_capture_pane_entry,
35 &cmd_choose_client_entry,
36 &cmd_choose_session_entry,
37 &cmd_choose_window_entry,
38 &cmd_clear_history_entry,
39 &cmd_clock_mode_entry,
40 &cmd_command_prompt_entry,
41 &cmd_confirm_before_entry,
42 &cmd_copy_buffer_entry,
43 &cmd_copy_mode_entry,
44 &cmd_delete_buffer_entry,
45 &cmd_detach_client_entry,
46 &cmd_display_message_entry,
47 &cmd_display_panes_entry,
48 &cmd_down_pane_entry,
49 &cmd_find_window_entry,
50 &cmd_has_session_entry,
51 &cmd_if_shell_entry,
52 &cmd_join_pane_entry,
53 &cmd_kill_pane_entry,
54 &cmd_kill_server_entry,
55 &cmd_kill_session_entry,
56 &cmd_kill_window_entry,
57 &cmd_last_window_entry,
58 &cmd_link_window_entry,
59 &cmd_list_buffers_entry,
60 &cmd_list_clients_entry,
61 &cmd_list_commands_entry,
62 &cmd_list_keys_entry,
63 &cmd_list_panes_entry,
64 &cmd_list_sessions_entry,
65 &cmd_list_windows_entry,
66 &cmd_load_buffer_entry,
67 &cmd_lock_client_entry,
68 &cmd_lock_server_entry,
69 &cmd_lock_session_entry,
70 &cmd_move_window_entry,
71 &cmd_new_session_entry,
72 &cmd_new_window_entry,
73 &cmd_next_layout_entry,
74 &cmd_next_window_entry,
75 &cmd_paste_buffer_entry,
76 &cmd_pipe_pane_entry,
77 &cmd_previous_layout_entry,
78 &cmd_previous_window_entry,
79 &cmd_refresh_client_entry,
80 &cmd_rename_session_entry,
81 &cmd_rename_window_entry,
82 &cmd_resize_pane_entry,
83 &cmd_respawn_window_entry,
84 &cmd_rotate_window_entry,
85 &cmd_run_shell_entry,
86 &cmd_save_buffer_entry,
87 &cmd_select_layout_entry,
88 &cmd_select_pane_entry,
89 &cmd_select_prompt_entry,
90 &cmd_select_window_entry,
91 &cmd_send_keys_entry,
92 &cmd_send_prefix_entry,
93 &cmd_server_info_entry,
94 &cmd_set_buffer_entry,
95 &cmd_set_environment_entry,
96 &cmd_set_option_entry,
97 &cmd_set_window_option_entry,
98 &cmd_show_buffer_entry,
99 &cmd_show_environment_entry,
100 &cmd_show_messages_entry,
101 &cmd_show_options_entry,
102 &cmd_show_window_options_entry,
103 &cmd_source_file_entry,
104 &cmd_split_window_entry,
105 &cmd_start_server_entry,
106 &cmd_suspend_client_entry,
107 &cmd_swap_pane_entry,
108 &cmd_swap_window_entry,
109 &cmd_switch_client_entry,
110 &cmd_unbind_key_entry,
111 &cmd_unlink_window_entry,
112 &cmd_up_pane_entry,
113 NULL
116 struct session *cmd_choose_session(struct sessions *);
117 struct client *cmd_choose_client(struct clients *);
118 struct client *cmd_lookup_client(const char *);
119 struct session *cmd_lookup_session(const char *, int *);
120 struct winlink *cmd_lookup_window(struct session *, const char *, int *);
121 int cmd_lookup_index(struct session *, const char *, int *);
124 cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
126 size_t arglen;
127 int i;
129 *buf = '\0';
130 for (i = 0; i < argc; i++) {
131 if (strlcpy(buf, argv[i], len) >= len)
132 return (-1);
133 arglen = strlen(argv[i]) + 1;
134 buf += arglen;
135 len -= arglen;
138 return (0);
142 cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
144 int i;
145 size_t arglen;
147 if (argc == 0)
148 return (0);
149 *argv = xcalloc(argc, sizeof **argv);
151 buf[len - 1] = '\0';
152 for (i = 0; i < argc; i++) {
153 if (len == 0) {
154 cmd_free_argv(argc, *argv);
155 return (-1);
158 arglen = strlen(buf) + 1;
159 (*argv)[i] = xstrdup(buf);
160 buf += arglen;
161 len -= arglen;
164 return (0);
167 void
168 cmd_free_argv(int argc, char **argv)
170 int i;
172 if (argc == 0)
173 return;
174 for (i = 0; i < argc; i++) {
175 if (argv[i] != NULL)
176 xfree(argv[i]);
178 xfree(argv);
181 struct cmd *
182 cmd_parse(int argc, char **argv, char **cause)
184 const struct cmd_entry **entryp, *entry;
185 struct cmd *cmd;
186 char s[BUFSIZ];
187 int opt, ambiguous = 0;
189 *cause = NULL;
190 if (argc == 0) {
191 xasprintf(cause, "no command");
192 return (NULL);
195 entry = NULL;
196 for (entryp = cmd_table; *entryp != NULL; entryp++) {
197 if ((*entryp)->alias != NULL &&
198 strcmp((*entryp)->alias, argv[0]) == 0) {
199 ambiguous = 0;
200 entry = *entryp;
201 break;
204 if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
205 continue;
206 if (entry != NULL)
207 ambiguous = 1;
208 entry = *entryp;
210 /* Bail now if an exact match. */
211 if (strcmp(entry->name, argv[0]) == 0)
212 break;
214 if (ambiguous)
215 goto ambiguous;
216 if (entry == NULL) {
217 xasprintf(cause, "unknown command: %s", argv[0]);
218 return (NULL);
221 optreset = 1;
222 optind = 1;
223 if (entry->parse == NULL) {
224 while ((opt = getopt(argc, argv, "")) != -1) {
225 switch (opt) {
226 default:
227 goto usage;
230 argc -= optind;
231 argv += optind;
232 if (argc != 0)
233 goto usage;
236 cmd = xmalloc(sizeof *cmd);
237 cmd->entry = entry;
238 cmd->data = NULL;
239 if (entry->parse != NULL) {
240 if (entry->parse(cmd, argc, argv, cause) != 0) {
241 xfree(cmd);
242 return (NULL);
245 return (cmd);
247 ambiguous:
248 *s = '\0';
249 for (entryp = cmd_table; *entryp != NULL; entryp++) {
250 if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
251 continue;
252 if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s)
253 break;
254 if (strlcat(s, ", ", sizeof s) >= sizeof s)
255 break;
257 s[strlen(s) - 2] = '\0';
258 xasprintf(cause, "ambiguous command: %s, could be: %s", argv[0], s);
259 return (NULL);
261 usage:
262 xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
263 return (NULL);
267 cmd_exec(struct cmd *cmd, struct cmd_ctx *ctx)
269 return (cmd->entry->exec(cmd, ctx));
272 void
273 cmd_free(struct cmd *cmd)
275 if (cmd->data != NULL && cmd->entry->free != NULL)
276 cmd->entry->free(cmd);
277 xfree(cmd);
280 size_t
281 cmd_print(struct cmd *cmd, char *buf, size_t len)
283 if (cmd->entry->print == NULL)
284 return (xsnprintf(buf, len, "%s", cmd->entry->name));
285 return (cmd->entry->print(cmd, buf, len));
289 * Figure out the current session. Use: 1) the current session, if the command
290 * context has one; 2) the most recently used session containing the pty of the
291 * calling client, if any; 3) the session specified in the TMUX variable from
292 * the environment (as passed from the client); 4) the most recently used
293 * session from all sessions.
295 struct session *
296 cmd_current_session(struct cmd_ctx *ctx)
298 struct msg_command_data *data = ctx->msgdata;
299 struct client *c = ctx->cmdclient;
300 struct session *s;
301 struct sessions ss;
302 struct winlink *wl;
303 struct window_pane *wp;
304 u_int i;
305 int found;
307 if (ctx->curclient != NULL && ctx->curclient->session != NULL)
308 return (ctx->curclient->session);
311 * If the name of the calling client's pty is know, build a list of the
312 * sessions that contain it and if any choose either the first or the
313 * newest.
315 if (c != NULL && c->tty.path != NULL) {
316 ARRAY_INIT(&ss);
317 for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
318 if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
319 continue;
320 found = 0;
321 RB_FOREACH(wl, winlinks, &s->windows) {
322 TAILQ_FOREACH(wp, &wl->window->panes, entry) {
323 if (strcmp(wp->tty, c->tty.path) == 0) {
324 found = 1;
325 break;
328 if (found)
329 break;
331 if (found)
332 ARRAY_ADD(&ss, s);
335 s = cmd_choose_session(&ss);
336 ARRAY_FREE(&ss);
337 if (s != NULL)
338 return (s);
341 /* Use the session from the TMUX environment variable. */
342 if (data != NULL && data->pid != -1) {
343 if (data->pid != getpid())
344 return (NULL);
345 if (data->idx > ARRAY_LENGTH(&sessions))
346 return (NULL);
347 if ((s = ARRAY_ITEM(&sessions, data->idx)) == NULL)
348 return (NULL);
349 return (s);
352 return (cmd_choose_session(&sessions));
355 /* Find the most recently used session from a list. */
356 struct session *
357 cmd_choose_session(struct sessions *ss)
359 struct session *s, *sbest;
360 struct timeval *tv = NULL;
361 u_int i;
363 sbest = NULL;
364 for (i = 0; i < ARRAY_LENGTH(ss); i++) {
365 if ((s = ARRAY_ITEM(ss, i)) == NULL)
366 continue;
368 if (tv == NULL || timercmp(&s->activity_time, tv, >)) {
369 sbest = s;
370 tv = &s->activity_time;
374 return (sbest);
378 * Find the current client. First try the current client if set, then pick the
379 * most recently used of the clients attached to the current session if any,
380 * then of all clients.
382 struct client *
383 cmd_current_client(struct cmd_ctx *ctx)
385 struct session *s;
386 struct client *c;
387 struct clients cc;
388 u_int i;
390 if (ctx->curclient != NULL)
391 return (ctx->curclient);
394 * No current client set. Find the current session and return the
395 * newest of its clients.
397 s = cmd_current_session(ctx);
398 if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
399 ARRAY_INIT(&cc);
400 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
401 if ((c = ARRAY_ITEM(&clients, i)) == NULL)
402 continue;
403 if (s == c->session)
404 ARRAY_ADD(&cc, c);
407 c = cmd_choose_client(&cc);
408 ARRAY_FREE(&cc);
409 if (c != NULL)
410 return (c);
413 return (cmd_choose_client(&clients));
416 /* Choose the most recently used client from a list. */
417 struct client *
418 cmd_choose_client(struct clients *cc)
420 struct client *c, *cbest;
421 struct timeval *tv = NULL;
422 u_int i;
424 cbest = NULL;
425 for (i = 0; i < ARRAY_LENGTH(cc); i++) {
426 if ((c = ARRAY_ITEM(cc, i)) == NULL)
427 continue;
428 if (c->session == NULL)
429 continue;
431 if (tv == NULL || timercmp(&c->activity_time, tv, >)) {
432 cbest = c;
433 tv = &c->activity_time;
437 return (cbest);
440 /* Find the target client or report an error and return NULL. */
441 struct client *
442 cmd_find_client(struct cmd_ctx *ctx, const char *arg)
444 struct client *c;
445 char *tmparg;
446 size_t arglen;
448 /* A NULL argument means the current client. */
449 if (arg == NULL)
450 return (cmd_current_client(ctx));
451 tmparg = xstrdup(arg);
453 /* Trim a single trailing colon if any. */
454 arglen = strlen(tmparg);
455 if (arglen != 0 && tmparg[arglen - 1] == ':')
456 tmparg[arglen - 1] = '\0';
458 /* Find the client, if any. */
459 c = cmd_lookup_client(tmparg);
461 /* If no client found, report an error. */
462 if (c == NULL)
463 ctx->error(ctx, "client not found: %s", tmparg);
465 xfree(tmparg);
466 return (c);
470 * Lookup a client by device path. Either of a full match and a match without a
471 * leading _PATH_DEV ("/dev/") is accepted.
473 struct client *
474 cmd_lookup_client(const char *name)
476 struct client *c;
477 const char *path;
478 u_int i;
480 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
481 c = ARRAY_ITEM(&clients, i);
482 if (c == NULL || c->session == NULL)
483 continue;
484 path = c->tty.path;
486 /* Check for exact matches. */
487 if (strcmp(name, path) == 0)
488 return (c);
490 /* Check without leading /dev if present. */
491 if (strncmp(path, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0)
492 continue;
493 if (strcmp(name, path + (sizeof _PATH_DEV) - 1) == 0)
494 return (c);
497 return (NULL);
500 /* Lookup a session by name. If no session is found, NULL is returned. */
501 struct session *
502 cmd_lookup_session(const char *name, int *ambiguous)
504 struct session *s, *sfound;
505 u_int i;
507 *ambiguous = 0;
510 * Look for matches. First look for exact matches - session names must
511 * be unique so an exact match can't be ambigious and can just be
512 * returned.
514 for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
515 if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
516 continue;
517 if (strcmp(name, s->name) == 0)
518 return (s);
522 * Otherwise look for partial matches, returning early if it is found to
523 * be ambiguous.
525 sfound = NULL;
526 for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
527 if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
528 continue;
529 if (strncmp(name, s->name, strlen(name)) == 0 ||
530 fnmatch(name, s->name, 0) == 0) {
531 if (sfound != NULL) {
532 *ambiguous = 1;
533 return (NULL);
535 sfound = s;
538 return (sfound);
542 * Lookup a window or return -1 if not found or ambigious. First try as an
543 * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
544 * idx if the window index is a valid number but there is now window with that
545 * index.
547 struct winlink *
548 cmd_lookup_window(struct session *s, const char *name, int *ambiguous)
550 struct winlink *wl, *wlfound;
551 const char *errstr;
552 u_int idx;
554 *ambiguous = 0;
556 /* First see if this is a valid window index in this session. */
557 idx = strtonum(name, 0, INT_MAX, &errstr);
558 if (errstr == NULL) {
559 if ((wl = winlink_find_by_index(&s->windows, idx)) != NULL)
560 return (wl);
563 /* Look for exact matches, error if more than one. */
564 wlfound = NULL;
565 RB_FOREACH(wl, winlinks, &s->windows) {
566 if (strcmp(name, wl->window->name) == 0) {
567 if (wlfound != NULL) {
568 *ambiguous = 1;
569 return (NULL);
571 wlfound = wl;
574 if (wlfound != NULL)
575 return (wlfound);
577 /* Now look for pattern matches, again error if multiple. */
578 wlfound = NULL;
579 RB_FOREACH(wl, winlinks, &s->windows) {
580 if (strncmp(name, wl->window->name, strlen(name)) == 0 ||
581 fnmatch(name, wl->window->name, 0) == 0) {
582 if (wlfound != NULL) {
583 *ambiguous = 1;
584 return (NULL);
586 wlfound = wl;
589 if (wlfound != NULL)
590 return (wlfound);
592 return (NULL);
596 * Find a window index - if the window doesn't exist, check if it is a
597 * potential index and return it anyway.
600 cmd_lookup_index(struct session *s, const char *name, int *ambiguous)
602 struct winlink *wl;
603 const char *errstr;
604 u_int idx;
606 if ((wl = cmd_lookup_window(s, name, ambiguous)) != NULL)
607 return (wl->idx);
608 if (*ambiguous)
609 return (-1);
611 idx = strtonum(name, 0, INT_MAX, &errstr);
612 if (errstr == NULL)
613 return (idx);
615 return (-1);
618 /* Find the target session or report an error and return NULL. */
619 struct session *
620 cmd_find_session(struct cmd_ctx *ctx, const char *arg)
622 struct session *s;
623 struct client *c;
624 char *tmparg;
625 size_t arglen;
626 int ambiguous;
628 /* A NULL argument means the current session. */
629 if (arg == NULL)
630 return (cmd_current_session(ctx));
631 tmparg = xstrdup(arg);
633 /* Trim a single trailing colon if any. */
634 arglen = strlen(tmparg);
635 if (arglen != 0 && tmparg[arglen - 1] == ':')
636 tmparg[arglen - 1] = '\0';
638 /* Find the session, if any. */
639 s = cmd_lookup_session(tmparg, &ambiguous);
641 /* If it doesn't, try to match it as a client. */
642 if (s == NULL && (c = cmd_lookup_client(tmparg)) != NULL)
643 s = c->session;
645 /* If no session found, report an error. */
646 if (s == NULL) {
647 if (ambiguous)
648 ctx->error(ctx, "more than one session: %s", tmparg);
649 else
650 ctx->error(ctx, "session not found: %s", tmparg);
653 xfree(tmparg);
654 return (s);
657 /* Find the target session and window or report an error and return NULL. */
658 struct winlink *
659 cmd_find_window(struct cmd_ctx *ctx, const char *arg, struct session **sp)
661 struct session *s;
662 struct winlink *wl;
663 const char *winptr;
664 char *sessptr = NULL;
665 int ambiguous = 0;
668 * Find the current session. There must always be a current session, if
669 * it can't be found, report an error.
671 if ((s = cmd_current_session(ctx)) == NULL) {
672 ctx->error(ctx, "can't establish current session");
673 return (NULL);
676 /* A NULL argument means the current session and window. */
677 if (arg == NULL) {
678 if (sp != NULL)
679 *sp = s;
680 return (s->curw);
683 /* Time to look at the argument. If it is empty, that is an error. */
684 if (*arg == '\0')
685 goto not_found;
687 /* Find the separating colon and split into window and session. */
688 winptr = strchr(arg, ':');
689 if (winptr == NULL)
690 goto no_colon;
691 winptr++; /* skip : */
692 sessptr = xstrdup(arg);
693 *strchr(sessptr, ':') = '\0';
695 /* Try to lookup the session if present. */
696 if (*sessptr != '\0') {
697 if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
698 goto no_session;
700 if (sp != NULL)
701 *sp = s;
704 * Then work out the window. An empty string is the current window,
705 * otherwise try to look it up in the session.
707 if (*winptr == '\0')
708 wl = s->curw;
709 else if ((wl = cmd_lookup_window(s, winptr, &ambiguous)) == NULL)
710 goto not_found;
712 if (sessptr != NULL)
713 xfree(sessptr);
714 return (wl);
716 no_colon:
717 /* No colon in the string, first try as a window then as a session. */
718 if ((wl = cmd_lookup_window(s, arg, &ambiguous)) == NULL) {
719 if (ambiguous)
720 goto not_found;
721 if ((s = cmd_lookup_session(arg, &ambiguous)) == NULL)
722 goto no_session;
723 wl = s->curw;
726 if (sp != NULL)
727 *sp = s;
729 return (wl);
731 no_session:
732 if (ambiguous)
733 ctx->error(ctx, "multiple sessions: %s", arg);
734 else
735 ctx->error(ctx, "session not found: %s", arg);
736 if (sessptr != NULL)
737 xfree(sessptr);
738 return (NULL);
740 not_found:
741 if (ambiguous)
742 ctx->error(ctx, "multiple windows: %s", arg);
743 else
744 ctx->error(ctx, "window not found: %s", arg);
745 if (sessptr != NULL)
746 xfree(sessptr);
747 return (NULL);
751 * Find the target session and window index, whether or not it exists in the
752 * session. Return -2 on error or -1 if no window index is specified. This is
753 * used when parsing an argument for a window target that may not exist (for
754 * example if it is going to be created).
757 cmd_find_index(struct cmd_ctx *ctx, const char *arg, struct session **sp)
759 struct session *s;
760 const char *winptr;
761 char *sessptr = NULL;
762 int idx, ambiguous = 0;
765 * Find the current session. There must always be a current session, if
766 * it can't be found, report an error.
768 if ((s = cmd_current_session(ctx)) == NULL) {
769 ctx->error(ctx, "can't establish current session");
770 return (-2);
773 /* A NULL argument means the current session and "no window" (-1). */
774 if (arg == NULL) {
775 if (sp != NULL)
776 *sp = s;
777 return (-1);
780 /* Time to look at the argument. If it is empty, that is an error. */
781 if (*arg == '\0')
782 goto not_found;
784 /* Find the separating colon. If none, assume the current session. */
785 winptr = strchr(arg, ':');
786 if (winptr == NULL)
787 goto no_colon;
788 winptr++; /* skip : */
789 sessptr = xstrdup(arg);
790 *strchr(sessptr, ':') = '\0';
792 /* Try to lookup the session if present. */
793 if (sessptr != NULL && *sessptr != '\0') {
794 if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
795 goto no_session;
797 if (sp != NULL)
798 *sp = s;
801 * Then work out the window. An empty string is a new window otherwise
802 * try to look it up in the session.
804 if (*winptr == '\0')
805 idx = -1;
806 else if ((idx = cmd_lookup_index(s, winptr, &ambiguous)) == -1) {
807 if (ambiguous)
808 goto not_found;
809 ctx->error(ctx, "invalid index: %s", arg);
810 idx = -2;
813 if (sessptr != NULL)
814 xfree(sessptr);
815 return (idx);
817 no_colon:
818 /* No colon in the string, first try as a window then as a session. */
819 if ((idx = cmd_lookup_index(s, arg, &ambiguous)) == -1) {
820 if (ambiguous)
821 goto not_found;
822 if ((s = cmd_lookup_session(arg, &ambiguous)) == NULL)
823 goto no_session;
824 idx = -1;
827 if (sp != NULL)
828 *sp = s;
830 return (idx);
832 no_session:
833 if (ambiguous)
834 ctx->error(ctx, "multiple sessions: %s", arg);
835 else
836 ctx->error(ctx, "session not found: %s", arg);
837 if (sessptr != NULL)
838 xfree(sessptr);
839 return (-2);
841 not_found:
842 if (ambiguous)
843 ctx->error(ctx, "multiple windows: %s", arg);
844 else
845 ctx->error(ctx, "window not found: %s", arg);
846 if (sessptr != NULL)
847 xfree(sessptr);
848 return (-2);
852 * Find the target session, window and pane number or report an error and
853 * return NULL. The pane number is separated from the session:window by a .,
854 * such as mysession:mywindow.0.
856 struct winlink *
857 cmd_find_pane(struct cmd_ctx *ctx,
858 const char *arg, struct session **sp, struct window_pane **wpp)
860 struct session *s;
861 struct winlink *wl;
862 struct layout_cell *lc;
863 const char *period, *errstr;
864 char *winptr, *paneptr;
865 u_int idx;
867 /* Get the current session. */
868 if ((s = cmd_current_session(ctx)) == NULL) {
869 ctx->error(ctx, "can't establish current session");
870 return (NULL);
872 if (sp != NULL)
873 *sp = s;
875 /* A NULL argument means the current session, window and pane. */
876 if (arg == NULL) {
877 *wpp = s->curw->window->active;
878 return (s->curw);
881 /* Look for a separating period. */
882 if ((period = strrchr(arg, '.')) == NULL)
883 goto no_period;
885 /* Pull out the window part and parse it. */
886 winptr = xstrdup(arg);
887 winptr[period - arg] = '\0';
888 if (*winptr == '\0')
889 wl = s->curw;
890 else if ((wl = cmd_find_window(ctx, winptr, sp)) == NULL)
891 goto error;
893 /* Find the pane section and look it up. */
894 paneptr = winptr + (period - arg) + 1;
895 if (*paneptr == '\0')
896 *wpp = wl->window->active;
897 else {
898 idx = strtonum(paneptr, 0, INT_MAX, &errstr);
899 if (errstr != NULL)
900 goto lookup_string;
901 *wpp = window_pane_at_index(wl->window, idx);
902 if (*wpp == NULL)
903 goto lookup_string;
906 xfree(winptr);
907 return (wl);
909 lookup_string:
910 /* Try pane string description. */
911 if ((lc = layout_find_string(s->curw->window, paneptr)) == NULL) {
912 ctx->error(ctx, "can't find pane: %s", paneptr);
913 goto error;
915 *wpp = lc->wp;
917 xfree(winptr);
918 return (s->curw);
920 no_period:
921 /* Try as a pane number alone. */
922 idx = strtonum(arg, 0, INT_MAX, &errstr);
923 if (errstr != NULL)
924 goto lookup_window;
926 /* Try index in the current session and window. */
927 if ((*wpp = window_pane_at_index(s->curw->window, idx)) == NULL)
928 goto lookup_window;
930 return (s->curw);
932 lookup_window:
933 /* Try pane string description. */
934 if ((lc = layout_find_string(s->curw->window, arg)) != NULL) {
935 *wpp = lc->wp;
936 return (s->curw);
939 /* Try as a window and use the active pane. */
940 if ((wl = cmd_find_window(ctx, arg, sp)) != NULL)
941 *wpp = wl->window->active;
942 return (wl);
944 error:
945 xfree(winptr);
946 return (NULL);
949 /* Replace the first %% or %idx in template by s. */
950 char *
951 cmd_template_replace(char *template, const char *s, int idx)
953 char ch;
954 char *buf, *ptr;
955 int replaced;
956 size_t len;
958 if (strstr(template, "%") == NULL)
959 return (xstrdup(template));
961 buf = xmalloc(1);
962 *buf = '\0';
963 len = 0;
964 replaced = 0;
966 ptr = template;
967 while (*ptr != '\0') {
968 switch (ch = *ptr++) {
969 case '%':
970 if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
971 if (*ptr != '%' || replaced)
972 break;
973 replaced = 1;
975 ptr++;
977 len += strlen(s);
978 buf = xrealloc(buf, 1, len + 1);
979 strlcat(buf, s, len + 1);
980 continue;
982 buf = xrealloc(buf, 1, len + 2);
983 buf[len++] = ch;
984 buf[len] = '\0';
987 return (buf);