Use UTF-8 line drawing characters on UTF-8 terminals. Fixes some stupid
[tmux-openbsd.git] / cmd.c
blob2643bb127ec5fdee46ffd10baaa6bd1548bfafe3
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_buffer_entry,
36 &cmd_choose_client_entry,
37 &cmd_choose_session_entry,
38 &cmd_choose_window_entry,
39 &cmd_clear_history_entry,
40 &cmd_clock_mode_entry,
41 &cmd_command_prompt_entry,
42 &cmd_confirm_before_entry,
43 &cmd_copy_buffer_entry,
44 &cmd_copy_mode_entry,
45 &cmd_delete_buffer_entry,
46 &cmd_detach_client_entry,
47 &cmd_display_message_entry,
48 &cmd_display_panes_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_window_entry,
90 &cmd_send_keys_entry,
91 &cmd_send_prefix_entry,
92 &cmd_server_info_entry,
93 &cmd_set_buffer_entry,
94 &cmd_set_environment_entry,
95 &cmd_set_option_entry,
96 &cmd_set_window_option_entry,
97 &cmd_show_buffer_entry,
98 &cmd_show_environment_entry,
99 &cmd_show_messages_entry,
100 &cmd_show_options_entry,
101 &cmd_show_window_options_entry,
102 &cmd_source_file_entry,
103 &cmd_split_window_entry,
104 &cmd_start_server_entry,
105 &cmd_suspend_client_entry,
106 &cmd_swap_pane_entry,
107 &cmd_swap_window_entry,
108 &cmd_switch_client_entry,
109 &cmd_unbind_key_entry,
110 &cmd_unlink_window_entry,
111 NULL
114 struct session *cmd_choose_session(struct sessions *);
115 struct client *cmd_choose_client(struct clients *);
116 struct client *cmd_lookup_client(const char *);
117 struct session *cmd_lookup_session(const char *, int *);
118 struct winlink *cmd_lookup_window(struct session *, const char *, int *);
119 int cmd_lookup_index(struct session *, const char *, int *);
120 struct winlink *cmd_find_window_offset(const char *, struct session *, int *);
121 int cmd_find_index_offset(const char *, struct session *, int *);
122 struct window_pane *cmd_find_pane_offset(const char *, struct winlink *);
125 cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
127 size_t arglen;
128 int i;
130 *buf = '\0';
131 for (i = 0; i < argc; i++) {
132 if (strlcpy(buf, argv[i], len) >= len)
133 return (-1);
134 arglen = strlen(argv[i]) + 1;
135 buf += arglen;
136 len -= arglen;
139 return (0);
143 cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
145 int i;
146 size_t arglen;
148 if (argc == 0)
149 return (0);
150 *argv = xcalloc(argc, sizeof **argv);
152 buf[len - 1] = '\0';
153 for (i = 0; i < argc; i++) {
154 if (len == 0) {
155 cmd_free_argv(argc, *argv);
156 return (-1);
159 arglen = strlen(buf) + 1;
160 (*argv)[i] = xstrdup(buf);
161 buf += arglen;
162 len -= arglen;
165 return (0);
168 void
169 cmd_free_argv(int argc, char **argv)
171 int i;
173 if (argc == 0)
174 return;
175 for (i = 0; i < argc; i++) {
176 if (argv[i] != NULL)
177 xfree(argv[i]);
179 xfree(argv);
182 struct cmd *
183 cmd_parse(int argc, char **argv, char **cause)
185 const struct cmd_entry **entryp, *entry;
186 struct cmd *cmd;
187 char s[BUFSIZ];
188 int opt, ambiguous = 0;
190 *cause = NULL;
191 if (argc == 0) {
192 xasprintf(cause, "no command");
193 return (NULL);
196 entry = NULL;
197 for (entryp = cmd_table; *entryp != NULL; entryp++) {
198 if ((*entryp)->alias != NULL &&
199 strcmp((*entryp)->alias, argv[0]) == 0) {
200 ambiguous = 0;
201 entry = *entryp;
202 break;
205 if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
206 continue;
207 if (entry != NULL)
208 ambiguous = 1;
209 entry = *entryp;
211 /* Bail now if an exact match. */
212 if (strcmp(entry->name, argv[0]) == 0)
213 break;
215 if (ambiguous)
216 goto ambiguous;
217 if (entry == NULL) {
218 xasprintf(cause, "unknown command: %s", argv[0]);
219 return (NULL);
222 optreset = 1;
223 optind = 1;
224 if (entry->parse == NULL) {
225 while ((opt = getopt(argc, argv, "")) != -1) {
226 switch (opt) {
227 default:
228 goto usage;
231 argc -= optind;
232 argv += optind;
233 if (argc != 0)
234 goto usage;
237 cmd = xmalloc(sizeof *cmd);
238 cmd->entry = entry;
239 cmd->data = NULL;
240 if (entry->parse != NULL) {
241 if (entry->parse(cmd, argc, argv, cause) != 0) {
242 xfree(cmd);
243 return (NULL);
246 return (cmd);
248 ambiguous:
249 *s = '\0';
250 for (entryp = cmd_table; *entryp != NULL; entryp++) {
251 if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
252 continue;
253 if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s)
254 break;
255 if (strlcat(s, ", ", sizeof s) >= sizeof s)
256 break;
258 s[strlen(s) - 2] = '\0';
259 xasprintf(cause, "ambiguous command: %s, could be: %s", argv[0], s);
260 return (NULL);
262 usage:
263 xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
264 return (NULL);
268 cmd_exec(struct cmd *cmd, struct cmd_ctx *ctx)
270 return (cmd->entry->exec(cmd, ctx));
273 void
274 cmd_free(struct cmd *cmd)
276 if (cmd->data != NULL && cmd->entry->free != NULL)
277 cmd->entry->free(cmd);
278 xfree(cmd);
281 size_t
282 cmd_print(struct cmd *cmd, char *buf, size_t len)
284 if (cmd->entry->print == NULL)
285 return (xsnprintf(buf, len, "%s", cmd->entry->name));
286 return (cmd->entry->print(cmd, buf, len));
290 * Figure out the current session. Use: 1) the current session, if the command
291 * context has one; 2) the most recently used session containing the pty of the
292 * calling client, if any; 3) the session specified in the TMUX variable from
293 * the environment (as passed from the client); 4) the most recently used
294 * session from all sessions.
296 struct session *
297 cmd_current_session(struct cmd_ctx *ctx)
299 struct msg_command_data *data = ctx->msgdata;
300 struct client *c = ctx->cmdclient;
301 struct session *s;
302 struct sessions ss;
303 struct winlink *wl;
304 struct window_pane *wp;
305 u_int i;
306 int found;
308 if (ctx->curclient != NULL && ctx->curclient->session != NULL)
309 return (ctx->curclient->session);
312 * If the name of the calling client's pty is know, build a list of the
313 * sessions that contain it and if any choose either the first or the
314 * newest.
316 if (c != NULL && c->tty.path != NULL) {
317 ARRAY_INIT(&ss);
318 for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
319 if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
320 continue;
321 found = 0;
322 RB_FOREACH(wl, winlinks, &s->windows) {
323 TAILQ_FOREACH(wp, &wl->window->panes, entry) {
324 if (strcmp(wp->tty, c->tty.path) == 0) {
325 found = 1;
326 break;
329 if (found)
330 break;
332 if (found)
333 ARRAY_ADD(&ss, s);
336 s = cmd_choose_session(&ss);
337 ARRAY_FREE(&ss);
338 if (s != NULL)
339 return (s);
342 /* Use the session from the TMUX environment variable. */
343 if (data != NULL && data->pid != -1) {
344 if (data->pid != getpid())
345 return (NULL);
346 if (data->idx > ARRAY_LENGTH(&sessions))
347 return (NULL);
348 if ((s = ARRAY_ITEM(&sessions, data->idx)) == NULL)
349 return (NULL);
350 return (s);
353 return (cmd_choose_session(&sessions));
356 /* Find the most recently used session from a list. */
357 struct session *
358 cmd_choose_session(struct sessions *ss)
360 struct session *s, *sbest;
361 struct timeval *tv = NULL;
362 u_int i;
364 sbest = NULL;
365 for (i = 0; i < ARRAY_LENGTH(ss); i++) {
366 if ((s = ARRAY_ITEM(ss, i)) == NULL)
367 continue;
369 if (tv == NULL || timercmp(&s->activity_time, tv, >)) {
370 sbest = s;
371 tv = &s->activity_time;
375 return (sbest);
379 * Find the current client. First try the current client if set, then pick the
380 * most recently used of the clients attached to the current session if any,
381 * then of all clients.
383 struct client *
384 cmd_current_client(struct cmd_ctx *ctx)
386 struct session *s;
387 struct client *c;
388 struct clients cc;
389 u_int i;
391 if (ctx->curclient != NULL)
392 return (ctx->curclient);
395 * No current client set. Find the current session and return the
396 * newest of its clients.
398 s = cmd_current_session(ctx);
399 if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
400 ARRAY_INIT(&cc);
401 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
402 if ((c = ARRAY_ITEM(&clients, i)) == NULL)
403 continue;
404 if (s == c->session)
405 ARRAY_ADD(&cc, c);
408 c = cmd_choose_client(&cc);
409 ARRAY_FREE(&cc);
410 if (c != NULL)
411 return (c);
414 return (cmd_choose_client(&clients));
417 /* Choose the most recently used client from a list. */
418 struct client *
419 cmd_choose_client(struct clients *cc)
421 struct client *c, *cbest;
422 struct timeval *tv = NULL;
423 u_int i;
425 cbest = NULL;
426 for (i = 0; i < ARRAY_LENGTH(cc); i++) {
427 if ((c = ARRAY_ITEM(cc, i)) == NULL)
428 continue;
429 if (c->session == NULL)
430 continue;
432 if (tv == NULL || timercmp(&c->activity_time, tv, >)) {
433 cbest = c;
434 tv = &c->activity_time;
438 return (cbest);
441 /* Find the target client or report an error and return NULL. */
442 struct client *
443 cmd_find_client(struct cmd_ctx *ctx, const char *arg)
445 struct client *c;
446 char *tmparg;
447 size_t arglen;
449 /* A NULL argument means the current client. */
450 if (arg == NULL)
451 return (cmd_current_client(ctx));
452 tmparg = xstrdup(arg);
454 /* Trim a single trailing colon if any. */
455 arglen = strlen(tmparg);
456 if (arglen != 0 && tmparg[arglen - 1] == ':')
457 tmparg[arglen - 1] = '\0';
459 /* Find the client, if any. */
460 c = cmd_lookup_client(tmparg);
462 /* If no client found, report an error. */
463 if (c == NULL)
464 ctx->error(ctx, "client not found: %s", tmparg);
466 xfree(tmparg);
467 return (c);
471 * Lookup a client by device path. Either of a full match and a match without a
472 * leading _PATH_DEV ("/dev/") is accepted.
474 struct client *
475 cmd_lookup_client(const char *name)
477 struct client *c;
478 const char *path;
479 u_int i;
481 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
482 c = ARRAY_ITEM(&clients, i);
483 if (c == NULL || c->session == NULL)
484 continue;
485 path = c->tty.path;
487 /* Check for exact matches. */
488 if (strcmp(name, path) == 0)
489 return (c);
491 /* Check without leading /dev if present. */
492 if (strncmp(path, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0)
493 continue;
494 if (strcmp(name, path + (sizeof _PATH_DEV) - 1) == 0)
495 return (c);
498 return (NULL);
501 /* Lookup a session by name. If no session is found, NULL is returned. */
502 struct session *
503 cmd_lookup_session(const char *name, int *ambiguous)
505 struct session *s, *sfound;
506 u_int i;
508 *ambiguous = 0;
511 * Look for matches. First look for exact matches - session names must
512 * be unique so an exact match can't be ambigious and can just be
513 * returned.
515 for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
516 if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
517 continue;
518 if (strcmp(name, s->name) == 0)
519 return (s);
523 * Otherwise look for partial matches, returning early if it is found to
524 * be ambiguous.
526 sfound = NULL;
527 for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
528 if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
529 continue;
530 if (strncmp(name, s->name, strlen(name)) == 0 ||
531 fnmatch(name, s->name, 0) == 0) {
532 if (sfound != NULL) {
533 *ambiguous = 1;
534 return (NULL);
536 sfound = s;
539 return (sfound);
543 * Lookup a window or return -1 if not found or ambigious. First try as an
544 * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
545 * idx if the window index is a valid number but there is no window with that
546 * index.
548 struct winlink *
549 cmd_lookup_window(struct session *s, const char *name, int *ambiguous)
551 struct winlink *wl, *wlfound;
552 const char *errstr;
553 u_int idx;
555 *ambiguous = 0;
557 /* First see if this is a valid window index in this session. */
558 idx = strtonum(name, 0, INT_MAX, &errstr);
559 if (errstr == NULL) {
560 if ((wl = winlink_find_by_index(&s->windows, idx)) != NULL)
561 return (wl);
564 /* Look for exact matches, error if more than one. */
565 wlfound = NULL;
566 RB_FOREACH(wl, winlinks, &s->windows) {
567 if (strcmp(name, wl->window->name) == 0) {
568 if (wlfound != NULL) {
569 *ambiguous = 1;
570 return (NULL);
572 wlfound = wl;
575 if (wlfound != NULL)
576 return (wlfound);
578 /* Now look for pattern matches, again error if multiple. */
579 wlfound = NULL;
580 RB_FOREACH(wl, winlinks, &s->windows) {
581 if (strncmp(name, wl->window->name, strlen(name)) == 0 ||
582 fnmatch(name, wl->window->name, 0) == 0) {
583 if (wlfound != NULL) {
584 *ambiguous = 1;
585 return (NULL);
587 wlfound = wl;
590 if (wlfound != NULL)
591 return (wlfound);
593 return (NULL);
597 * Find a window index - if the window doesn't exist, check if it is a
598 * potential index and return it anyway.
601 cmd_lookup_index(struct session *s, const char *name, int *ambiguous)
603 struct winlink *wl;
604 const char *errstr;
605 u_int idx;
607 if ((wl = cmd_lookup_window(s, name, ambiguous)) != NULL)
608 return (wl->idx);
609 if (*ambiguous)
610 return (-1);
612 idx = strtonum(name, 0, INT_MAX, &errstr);
613 if (errstr == NULL)
614 return (idx);
616 return (-1);
619 /* Find the target session or report an error and return NULL. */
620 struct session *
621 cmd_find_session(struct cmd_ctx *ctx, const char *arg)
623 struct session *s;
624 struct client *c;
625 char *tmparg;
626 size_t arglen;
627 int ambiguous;
629 /* A NULL argument means the current session. */
630 if (arg == NULL)
631 return (cmd_current_session(ctx));
632 tmparg = xstrdup(arg);
634 /* Trim a single trailing colon if any. */
635 arglen = strlen(tmparg);
636 if (arglen != 0 && tmparg[arglen - 1] == ':')
637 tmparg[arglen - 1] = '\0';
639 /* Find the session, if any. */
640 s = cmd_lookup_session(tmparg, &ambiguous);
642 /* If it doesn't, try to match it as a client. */
643 if (s == NULL && (c = cmd_lookup_client(tmparg)) != NULL)
644 s = c->session;
646 /* If no session found, report an error. */
647 if (s == NULL) {
648 if (ambiguous)
649 ctx->error(ctx, "more than one session: %s", tmparg);
650 else
651 ctx->error(ctx, "session not found: %s", tmparg);
654 xfree(tmparg);
655 return (s);
658 /* Find the target session and window or report an error and return NULL. */
659 struct winlink *
660 cmd_find_window(struct cmd_ctx *ctx, const char *arg, struct session **sp)
662 struct session *s;
663 struct winlink *wl;
664 const char *winptr;
665 char *sessptr = NULL;
666 int ambiguous = 0;
669 * Find the current session. There must always be a current session, if
670 * it can't be found, report an error.
672 if ((s = cmd_current_session(ctx)) == NULL) {
673 ctx->error(ctx, "can't establish current session");
674 return (NULL);
677 /* A NULL argument means the current session and window. */
678 if (arg == NULL) {
679 if (sp != NULL)
680 *sp = s;
681 return (s->curw);
684 /* Time to look at the argument. If it is empty, that is an error. */
685 if (*arg == '\0')
686 goto not_found;
688 /* Find the separating colon and split into window and session. */
689 winptr = strchr(arg, ':');
690 if (winptr == NULL)
691 goto no_colon;
692 winptr++; /* skip : */
693 sessptr = xstrdup(arg);
694 *strchr(sessptr, ':') = '\0';
696 /* Try to lookup the session if present. */
697 if (*sessptr != '\0') {
698 if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
699 goto no_session;
701 if (sp != NULL)
702 *sp = s;
705 * Then work out the window. An empty string is the current window,
706 * otherwise try special cases then to look it up in the session.
708 if (*winptr == '\0')
709 wl = s->curw;
710 else if (winptr[0] == '!' && winptr[1] == '\0')
711 wl = TAILQ_FIRST(&s->lastw);
712 else if (winptr[0] == '+' || winptr[0] == '-')
713 wl = cmd_find_window_offset(winptr, s, &ambiguous);
714 else
715 wl = cmd_lookup_window(s, winptr, &ambiguous);
716 if (wl == NULL)
717 goto not_found;
719 if (sessptr != NULL)
720 xfree(sessptr);
721 return (wl);
723 no_colon:
725 * No colon in the string, first try special cases, then as a window
726 * and lastly as a session.
728 if (arg[0] == '!' && arg[1] == '\0') {
729 if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
730 goto not_found;
731 } else if (arg[0] == '+' || arg[0] == '-') {
732 if ((wl = cmd_find_window_offset(arg, s, &ambiguous)) == NULL)
733 goto lookup_session;
734 } else if ((wl = cmd_lookup_window(s, arg, &ambiguous)) == NULL)
735 goto lookup_session;
737 if (sp != NULL)
738 *sp = s;
740 return (wl);
742 lookup_session:
743 if (ambiguous)
744 goto not_found;
745 if ((s = cmd_lookup_session(arg, &ambiguous)) == NULL)
746 goto no_session;
748 if (sp != NULL)
749 *sp = s;
751 return (s->curw);
753 no_session:
754 if (ambiguous)
755 ctx->error(ctx, "multiple sessions: %s", arg);
756 else
757 ctx->error(ctx, "session not found: %s", arg);
758 if (sessptr != NULL)
759 xfree(sessptr);
760 return (NULL);
762 not_found:
763 if (ambiguous)
764 ctx->error(ctx, "multiple windows: %s", arg);
765 else
766 ctx->error(ctx, "window not found: %s", arg);
767 if (sessptr != NULL)
768 xfree(sessptr);
769 return (NULL);
772 struct winlink *
773 cmd_find_window_offset(const char *winptr, struct session *s, int *ambiguous)
775 struct winlink *wl;
776 int offset = 1;
778 if (winptr[1] != '\0')
779 offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
780 if (offset == 0)
781 wl = cmd_lookup_window(s, winptr, ambiguous);
782 else {
783 if (winptr[0] == '+')
784 wl = winlink_next_by_number(s->curw, s, offset);
785 else
786 wl = winlink_previous_by_number(s->curw, s, offset);
789 return (wl);
793 * Find the target session and window index, whether or not it exists in the
794 * session. Return -2 on error or -1 if no window index is specified. This is
795 * used when parsing an argument for a window target that may not exist (for
796 * example if it is going to be created).
799 cmd_find_index(struct cmd_ctx *ctx, const char *arg, struct session **sp)
801 struct session *s;
802 struct winlink *wl;
803 const char *winptr;
804 char *sessptr = NULL;
805 int idx, ambiguous = 0;
808 * Find the current session. There must always be a current session, if
809 * it can't be found, report an error.
811 if ((s = cmd_current_session(ctx)) == NULL) {
812 ctx->error(ctx, "can't establish current session");
813 return (-2);
816 /* A NULL argument means the current session and "no window" (-1). */
817 if (arg == NULL) {
818 if (sp != NULL)
819 *sp = s;
820 return (-1);
823 /* Time to look at the argument. If it is empty, that is an error. */
824 if (*arg == '\0')
825 goto not_found;
827 /* Find the separating colon. If none, assume the current session. */
828 winptr = strchr(arg, ':');
829 if (winptr == NULL)
830 goto no_colon;
831 winptr++; /* skip : */
832 sessptr = xstrdup(arg);
833 *strchr(sessptr, ':') = '\0';
835 /* Try to lookup the session if present. */
836 if (sessptr != NULL && *sessptr != '\0') {
837 if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
838 goto no_session;
840 if (sp != NULL)
841 *sp = s;
844 * Then work out the window. An empty string is a new window otherwise
845 * try to look it up in the session.
847 if (*winptr == '\0')
848 idx = -1;
849 else if (winptr[0] == '!' && winptr[1] == '\0') {
850 if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
851 goto not_found;
852 idx = wl->idx;
853 } else if (winptr[0] == '+' || winptr[0] == '-') {
854 if ((idx = cmd_find_index_offset(winptr, s, &ambiguous)) < 0)
855 goto invalid_index;
856 } else if ((idx = cmd_lookup_index(s, winptr, &ambiguous)) == -1)
857 goto invalid_index;
859 if (sessptr != NULL)
860 xfree(sessptr);
861 return (idx);
863 no_colon:
865 * No colon in the string, first try special cases, then as a window
866 * and lastly as a session.
868 if (arg[0] == '!' && arg[1] == '\0') {
869 if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
870 goto not_found;
871 idx = wl->idx;
872 } else if (arg[0] == '+' || arg[0] == '-') {
873 if ((idx = cmd_find_index_offset(arg, s, &ambiguous)) < 0)
874 goto lookup_session;
875 } else if ((idx = cmd_lookup_index(s, arg, &ambiguous)) == -1)
876 goto lookup_session;
878 if (sp != NULL)
879 *sp = s;
881 return (idx);
883 lookup_session:
884 if (ambiguous)
885 goto not_found;
886 if ((s = cmd_lookup_session(arg, &ambiguous)) == NULL)
887 goto no_session;
889 if (sp != NULL)
890 *sp = s;
892 return (-1);
894 no_session:
895 if (ambiguous)
896 ctx->error(ctx, "multiple sessions: %s", arg);
897 else
898 ctx->error(ctx, "session not found: %s", arg);
899 if (sessptr != NULL)
900 xfree(sessptr);
901 return (-2);
903 invalid_index:
904 if (ambiguous)
905 goto not_found;
906 ctx->error(ctx, "invalid index: %s", arg);
908 if (sessptr != NULL)
909 xfree(sessptr);
910 return (-2);
912 not_found:
913 if (ambiguous)
914 ctx->error(ctx, "multiple windows: %s", arg);
915 else
916 ctx->error(ctx, "window not found: %s", arg);
917 if (sessptr != NULL)
918 xfree(sessptr);
919 return (-2);
923 cmd_find_index_offset(const char *winptr, struct session *s, int *ambiguous)
925 int idx, offset = 1;
927 if (winptr[1] != '\0')
928 offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
929 if (offset == 0)
930 idx = cmd_lookup_index(s, winptr, ambiguous);
931 else {
932 if (winptr[0] == '+') {
933 if (s->curw->idx == INT_MAX)
934 idx = cmd_lookup_index(s, winptr, ambiguous);
935 else
936 idx = s->curw->idx + offset;
937 } else {
938 if (s->curw->idx == 0)
939 idx = cmd_lookup_index(s, winptr, ambiguous);
940 else
941 idx = s->curw->idx - offset;
945 return (idx);
949 * Find the target session, window and pane number or report an error and
950 * return NULL. The pane number is separated from the session:window by a .,
951 * such as mysession:mywindow.0.
953 struct winlink *
954 cmd_find_pane(struct cmd_ctx *ctx,
955 const char *arg, struct session **sp, struct window_pane **wpp)
957 struct session *s;
958 struct winlink *wl;
959 struct layout_cell *lc;
960 const char *period, *errstr;
961 char *winptr, *paneptr;
962 u_int idx;
964 /* Get the current session. */
965 if ((s = cmd_current_session(ctx)) == NULL) {
966 ctx->error(ctx, "can't establish current session");
967 return (NULL);
969 if (sp != NULL)
970 *sp = s;
972 /* A NULL argument means the current session, window and pane. */
973 if (arg == NULL) {
974 *wpp = s->curw->window->active;
975 return (s->curw);
978 /* Look for a separating period. */
979 if ((period = strrchr(arg, '.')) == NULL)
980 goto no_period;
982 /* Pull out the window part and parse it. */
983 winptr = xstrdup(arg);
984 winptr[period - arg] = '\0';
985 if (*winptr == '\0')
986 wl = s->curw;
987 else if ((wl = cmd_find_window(ctx, winptr, sp)) == NULL)
988 goto error;
990 /* Find the pane section and look it up. */
991 paneptr = winptr + (period - arg) + 1;
992 if (*paneptr == '\0')
993 *wpp = wl->window->active;
994 else if (paneptr[0] == '+' || paneptr[0] == '-')
995 *wpp = cmd_find_pane_offset(paneptr, wl);
996 else {
997 idx = strtonum(paneptr, 0, INT_MAX, &errstr);
998 if (errstr != NULL)
999 goto lookup_string;
1000 *wpp = window_pane_at_index(wl->window, idx);
1001 if (*wpp == NULL)
1002 goto lookup_string;
1005 xfree(winptr);
1006 return (wl);
1008 lookup_string:
1009 /* Try pane string description. */
1010 if ((lc = layout_find_string(wl->window, paneptr)) == NULL) {
1011 ctx->error(ctx, "can't find pane: %s", paneptr);
1012 goto error;
1014 *wpp = lc->wp;
1016 xfree(winptr);
1017 return (wl);
1019 no_period:
1020 /* Try as a pane number alone. */
1021 idx = strtonum(arg, 0, INT_MAX, &errstr);
1022 if (errstr != NULL)
1023 goto lookup_window;
1025 /* Try index in the current session and window. */
1026 if ((*wpp = window_pane_at_index(s->curw->window, idx)) == NULL)
1027 goto lookup_window;
1029 return (s->curw);
1031 lookup_window:
1032 /* Try pane string description. */
1033 if ((lc = layout_find_string(s->curw->window, arg)) != NULL) {
1034 *wpp = lc->wp;
1035 return (s->curw);
1038 /* Try as a window and use the active pane. */
1039 if ((wl = cmd_find_window(ctx, arg, sp)) != NULL)
1040 *wpp = wl->window->active;
1041 return (wl);
1043 error:
1044 xfree(winptr);
1045 return (NULL);
1048 struct window_pane *
1049 cmd_find_pane_offset(const char *paneptr, struct winlink *wl)
1051 struct window *w = wl->window;
1052 struct window_pane *wp = w->active;
1053 u_int offset = 1;
1055 if (paneptr[1] != '\0')
1056 offset = strtonum(paneptr + 1, 1, INT_MAX, NULL);
1057 if (offset > 0) {
1058 if (paneptr[0] == '+')
1059 wp = window_pane_next_by_number(w, wp, offset);
1060 else
1061 wp = window_pane_previous_by_number(w, wp, offset);
1064 return (wp);
1067 /* Replace the first %% or %idx in template by s. */
1068 char *
1069 cmd_template_replace(char *template, const char *s, int idx)
1071 char ch;
1072 char *buf, *ptr;
1073 int replaced;
1074 size_t len;
1076 if (strstr(template, "%") == NULL)
1077 return (xstrdup(template));
1079 buf = xmalloc(1);
1080 *buf = '\0';
1081 len = 0;
1082 replaced = 0;
1084 ptr = template;
1085 while (*ptr != '\0') {
1086 switch (ch = *ptr++) {
1087 case '%':
1088 if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
1089 if (*ptr != '%' || replaced)
1090 break;
1091 replaced = 1;
1093 ptr++;
1095 len += strlen(s);
1096 buf = xrealloc(buf, 1, len + 1);
1097 strlcat(buf, s, len + 1);
1098 continue;
1100 buf = xrealloc(buf, 1, len + 2);
1101 buf[len++] = ch;
1102 buf[len] = '\0';
1105 return (buf);