Alter how tmux handles the working directory to internally use file
[tmux-openbsd.git] / cmd.c
blob2aa5f514dfb57a7ac1de9228f30d98c1a38dbe5f
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 <pwd.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #include "tmux.h"
31 const struct cmd_entry *cmd_table[] = {
32 &cmd_attach_session_entry,
33 &cmd_bind_key_entry,
34 &cmd_break_pane_entry,
35 &cmd_capture_pane_entry,
36 &cmd_choose_buffer_entry,
37 &cmd_choose_client_entry,
38 &cmd_choose_list_entry,
39 &cmd_choose_session_entry,
40 &cmd_choose_tree_entry,
41 &cmd_choose_window_entry,
42 &cmd_clear_history_entry,
43 &cmd_clock_mode_entry,
44 &cmd_command_prompt_entry,
45 &cmd_confirm_before_entry,
46 &cmd_copy_mode_entry,
47 &cmd_delete_buffer_entry,
48 &cmd_detach_client_entry,
49 &cmd_display_message_entry,
50 &cmd_display_panes_entry,
51 &cmd_find_window_entry,
52 &cmd_has_session_entry,
53 &cmd_if_shell_entry,
54 &cmd_join_pane_entry,
55 &cmd_kill_pane_entry,
56 &cmd_kill_server_entry,
57 &cmd_kill_session_entry,
58 &cmd_kill_window_entry,
59 &cmd_last_pane_entry,
60 &cmd_last_window_entry,
61 &cmd_link_window_entry,
62 &cmd_list_buffers_entry,
63 &cmd_list_clients_entry,
64 &cmd_list_commands_entry,
65 &cmd_list_keys_entry,
66 &cmd_list_panes_entry,
67 &cmd_list_sessions_entry,
68 &cmd_list_windows_entry,
69 &cmd_load_buffer_entry,
70 &cmd_lock_client_entry,
71 &cmd_lock_server_entry,
72 &cmd_lock_session_entry,
73 &cmd_move_pane_entry,
74 &cmd_move_window_entry,
75 &cmd_new_session_entry,
76 &cmd_new_window_entry,
77 &cmd_next_layout_entry,
78 &cmd_next_window_entry,
79 &cmd_paste_buffer_entry,
80 &cmd_pipe_pane_entry,
81 &cmd_previous_layout_entry,
82 &cmd_previous_window_entry,
83 &cmd_refresh_client_entry,
84 &cmd_rename_session_entry,
85 &cmd_rename_window_entry,
86 &cmd_resize_pane_entry,
87 &cmd_respawn_pane_entry,
88 &cmd_respawn_window_entry,
89 &cmd_rotate_window_entry,
90 &cmd_run_shell_entry,
91 &cmd_save_buffer_entry,
92 &cmd_select_layout_entry,
93 &cmd_select_pane_entry,
94 &cmd_select_window_entry,
95 &cmd_send_keys_entry,
96 &cmd_send_prefix_entry,
97 &cmd_server_info_entry,
98 &cmd_set_buffer_entry,
99 &cmd_set_environment_entry,
100 &cmd_set_option_entry,
101 &cmd_set_window_option_entry,
102 &cmd_show_buffer_entry,
103 &cmd_show_environment_entry,
104 &cmd_show_messages_entry,
105 &cmd_show_options_entry,
106 &cmd_show_window_options_entry,
107 &cmd_source_file_entry,
108 &cmd_split_window_entry,
109 &cmd_start_server_entry,
110 &cmd_suspend_client_entry,
111 &cmd_swap_pane_entry,
112 &cmd_swap_window_entry,
113 &cmd_switch_client_entry,
114 &cmd_unbind_key_entry,
115 &cmd_unlink_window_entry,
116 &cmd_wait_for_entry,
117 NULL
120 int cmd_session_better(struct session *, struct session *, int);
121 struct session *cmd_choose_session_list(struct sessionslist *);
122 struct session *cmd_choose_session(int);
123 struct client *cmd_choose_client(struct clients *);
124 struct client *cmd_lookup_client(const char *);
125 struct session *cmd_lookup_session(const char *, int *);
126 struct session *cmd_lookup_session_id(const char *);
127 struct winlink *cmd_lookup_window(struct session *, const char *, int *);
128 int cmd_lookup_index(struct session *, const char *, int *);
129 struct window_pane *cmd_lookup_paneid(const char *);
130 struct winlink *cmd_lookup_winlink_windowid(struct session *, const char *);
131 struct window *cmd_lookup_windowid(const char *);
132 struct session *cmd_window_session(struct cmd_q *, struct window *,
133 struct winlink **);
134 struct winlink *cmd_find_window_offset(const char *, struct session *, int *);
135 int cmd_find_index_offset(const char *, struct session *, int *);
136 struct window_pane *cmd_find_pane_offset(const char *, struct winlink *);
139 cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
141 size_t arglen;
142 int i;
144 *buf = '\0';
145 for (i = 0; i < argc; i++) {
146 if (strlcpy(buf, argv[i], len) >= len)
147 return (-1);
148 arglen = strlen(argv[i]) + 1;
149 buf += arglen;
150 len -= arglen;
153 return (0);
157 cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
159 int i;
160 size_t arglen;
162 if (argc == 0)
163 return (0);
164 *argv = xcalloc(argc, sizeof **argv);
166 buf[len - 1] = '\0';
167 for (i = 0; i < argc; i++) {
168 if (len == 0) {
169 cmd_free_argv(argc, *argv);
170 return (-1);
173 arglen = strlen(buf) + 1;
174 (*argv)[i] = xstrdup(buf);
175 buf += arglen;
176 len -= arglen;
179 return (0);
182 char **
183 cmd_copy_argv(int argc, char *const *argv)
185 char **new_argv;
186 int i;
188 if (argc == 0)
189 return (NULL);
190 new_argv = xcalloc(argc, sizeof *new_argv);
191 for (i = 0; i < argc; i++) {
192 if (argv[i] != NULL)
193 new_argv[i] = xstrdup(argv[i]);
195 return (new_argv);
198 void
199 cmd_free_argv(int argc, char **argv)
201 int i;
203 if (argc == 0)
204 return;
205 for (i = 0; i < argc; i++)
206 free(argv[i]);
207 free(argv);
210 struct cmd *
211 cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause)
213 const struct cmd_entry **entryp, *entry;
214 struct cmd *cmd;
215 struct args *args;
216 char s[BUFSIZ];
217 int ambiguous = 0;
219 *cause = NULL;
220 if (argc == 0) {
221 xasprintf(cause, "no command");
222 return (NULL);
225 entry = NULL;
226 for (entryp = cmd_table; *entryp != NULL; entryp++) {
227 if ((*entryp)->alias != NULL &&
228 strcmp((*entryp)->alias, argv[0]) == 0) {
229 ambiguous = 0;
230 entry = *entryp;
231 break;
234 if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
235 continue;
236 if (entry != NULL)
237 ambiguous = 1;
238 entry = *entryp;
240 /* Bail now if an exact match. */
241 if (strcmp(entry->name, argv[0]) == 0)
242 break;
244 if (ambiguous)
245 goto ambiguous;
246 if (entry == NULL) {
247 xasprintf(cause, "unknown command: %s", argv[0]);
248 return (NULL);
251 args = args_parse(entry->args_template, argc, argv);
252 if (args == NULL)
253 goto usage;
254 if (entry->args_lower != -1 && args->argc < entry->args_lower)
255 goto usage;
256 if (entry->args_upper != -1 && args->argc > entry->args_upper)
257 goto usage;
259 cmd = xcalloc(1, sizeof *cmd);
260 cmd->entry = entry;
261 cmd->args = args;
263 if (file != NULL)
264 cmd->file = xstrdup(file);
265 cmd->line = line;
267 return (cmd);
269 ambiguous:
270 *s = '\0';
271 for (entryp = cmd_table; *entryp != NULL; entryp++) {
272 if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
273 continue;
274 if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s)
275 break;
276 if (strlcat(s, ", ", sizeof s) >= sizeof s)
277 break;
279 s[strlen(s) - 2] = '\0';
280 xasprintf(cause, "ambiguous command: %s, could be: %s", argv[0], s);
281 return (NULL);
283 usage:
284 if (args != NULL)
285 args_free(args);
286 xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
287 return (NULL);
290 size_t
291 cmd_print(struct cmd *cmd, char *buf, size_t len)
293 size_t off, used;
295 off = xsnprintf(buf, len, "%s ", cmd->entry->name);
296 if (off + 1 < len) {
297 used = args_print(cmd->args, buf + off, len - off - 1);
298 if (used == 0)
299 off--;
300 else
301 off += used;
302 buf[off] = '\0';
304 return (off);
308 * Figure out the current session. Use: 1) the current session, if the command
309 * context has one; 2) the most recently used session containing the pty of the
310 * calling client, if any; 3) the session specified in the TMUX variable from
311 * the environment (as passed from the client); 4) the most recently used
312 * session from all sessions.
314 struct session *
315 cmd_current_session(struct cmd_q *cmdq, int prefer_unattached)
317 struct msg_command_data *data = cmdq->msgdata;
318 struct client *c = cmdq->client;
319 struct session *s;
320 struct sessionslist ss;
321 struct winlink *wl;
322 struct window_pane *wp;
323 const char *path;
324 int found;
326 if (c != NULL && c->session != NULL)
327 return (c->session);
330 * If the name of the calling client's pty is known, build a list of
331 * the sessions that contain it and if any choose either the first or
332 * the newest.
334 path = c == NULL ? NULL : c->tty.path;
335 if (path != NULL) {
336 ARRAY_INIT(&ss);
337 RB_FOREACH(s, sessions, &sessions) {
338 found = 0;
339 RB_FOREACH(wl, winlinks, &s->windows) {
340 TAILQ_FOREACH(wp, &wl->window->panes, entry) {
341 if (strcmp(wp->tty, path) == 0) {
342 found = 1;
343 break;
346 if (found)
347 break;
349 if (found)
350 ARRAY_ADD(&ss, s);
353 s = cmd_choose_session_list(&ss);
354 ARRAY_FREE(&ss);
355 if (s != NULL)
356 return (s);
359 /* Use the session from the TMUX environment variable. */
360 if (data != NULL && data->pid == getpid() && data->session_id != -1) {
361 s = session_find_by_id(data->session_id);
362 if (s != NULL)
363 return (s);
366 return (cmd_choose_session(prefer_unattached));
369 /* Is this session better? */
371 cmd_session_better(struct session *s, struct session *best,
372 int prefer_unattached)
374 if (best == NULL)
375 return (1);
376 if (prefer_unattached) {
377 if (!(best->flags & SESSION_UNATTACHED) &&
378 (s->flags & SESSION_UNATTACHED))
379 return (1);
380 else if ((best->flags & SESSION_UNATTACHED) &&
381 !(s->flags & SESSION_UNATTACHED))
382 return (0);
384 return (timercmp(&s->activity_time, &best->activity_time, >));
388 * Find the most recently used session, preferring unattached if the flag is
389 * set.
391 struct session *
392 cmd_choose_session(int prefer_unattached)
394 struct session *s, *best;
396 best = NULL;
397 RB_FOREACH(s, sessions, &sessions) {
398 if (cmd_session_better(s, best, prefer_unattached))
399 best = s;
401 return (best);
404 /* Find the most recently used session from a list. */
405 struct session *
406 cmd_choose_session_list(struct sessionslist *ss)
408 struct session *s, *sbest;
409 struct timeval *tv = NULL;
410 u_int i;
412 sbest = NULL;
413 for (i = 0; i < ARRAY_LENGTH(ss); i++) {
414 if ((s = ARRAY_ITEM(ss, i)) == NULL)
415 continue;
417 if (tv == NULL || timercmp(&s->activity_time, tv, >)) {
418 sbest = s;
419 tv = &s->activity_time;
423 return (sbest);
427 * Find the current client. First try the current client if set, then pick the
428 * most recently used of the clients attached to the current session if any,
429 * then of all clients.
431 struct client *
432 cmd_current_client(struct cmd_q *cmdq)
434 struct session *s;
435 struct client *c;
436 struct clients cc;
437 u_int i;
439 if (cmdq->client != NULL && cmdq->client->session != NULL)
440 return (cmdq->client);
443 * No current client set. Find the current session and return the
444 * newest of its clients.
446 s = cmd_current_session(cmdq, 0);
447 if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
448 ARRAY_INIT(&cc);
449 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
450 if ((c = ARRAY_ITEM(&clients, i)) == NULL)
451 continue;
452 if (s == c->session)
453 ARRAY_ADD(&cc, c);
456 c = cmd_choose_client(&cc);
457 ARRAY_FREE(&cc);
458 if (c != NULL)
459 return (c);
462 return (cmd_choose_client(&clients));
465 /* Choose the most recently used client from a list. */
466 struct client *
467 cmd_choose_client(struct clients *cc)
469 struct client *c, *cbest;
470 struct timeval *tv = NULL;
471 u_int i;
473 cbest = NULL;
474 for (i = 0; i < ARRAY_LENGTH(cc); i++) {
475 if ((c = ARRAY_ITEM(cc, i)) == NULL)
476 continue;
477 if (c->session == NULL)
478 continue;
480 if (tv == NULL || timercmp(&c->activity_time, tv, >)) {
481 cbest = c;
482 tv = &c->activity_time;
486 return (cbest);
489 /* Find the target client or report an error and return NULL. */
490 struct client *
491 cmd_find_client(struct cmd_q *cmdq, const char *arg, int quiet)
493 struct client *c;
494 char *tmparg;
495 size_t arglen;
497 /* A NULL argument means the current client. */
498 if (arg == NULL) {
499 c = cmd_current_client(cmdq);
500 if (c == NULL && !quiet)
501 cmdq_error(cmdq, "no clients");
502 return (c);
504 tmparg = xstrdup(arg);
506 /* Trim a single trailing colon if any. */
507 arglen = strlen(tmparg);
508 if (arglen != 0 && tmparg[arglen - 1] == ':')
509 tmparg[arglen - 1] = '\0';
511 /* Find the client, if any. */
512 c = cmd_lookup_client(tmparg);
514 /* If no client found, report an error. */
515 if (c == NULL && !quiet)
516 cmdq_error(cmdq, "client not found: %s", tmparg);
518 free(tmparg);
519 return (c);
523 * Lookup a client by device path. Either of a full match and a match without a
524 * leading _PATH_DEV ("/dev/") is accepted.
526 struct client *
527 cmd_lookup_client(const char *name)
529 struct client *c;
530 const char *path;
531 u_int i;
533 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
534 c = ARRAY_ITEM(&clients, i);
535 if (c == NULL || c->session == NULL || c->tty.path == NULL)
536 continue;
537 path = c->tty.path;
539 /* Check for exact matches. */
540 if (strcmp(name, path) == 0)
541 return (c);
543 /* Check without leading /dev if present. */
544 if (strncmp(path, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0)
545 continue;
546 if (strcmp(name, path + (sizeof _PATH_DEV) - 1) == 0)
547 return (c);
550 return (NULL);
553 /* Find the target session or report an error and return NULL. */
554 struct session *
555 cmd_lookup_session_id(const char *arg)
557 char *endptr;
558 long id;
560 if (arg[0] != '$')
561 return (NULL);
562 id = strtol(arg + 1, &endptr, 10);
563 if (arg[1] != '\0' && *endptr == '\0')
564 return (session_find_by_id(id));
565 return (NULL);
568 /* Lookup a session by name. If no session is found, NULL is returned. */
569 struct session *
570 cmd_lookup_session(const char *name, int *ambiguous)
572 struct session *s, *sfound;
574 *ambiguous = 0;
576 /* Look for $id first. */
577 if ((s = cmd_lookup_session_id(name)) != NULL)
578 return (s);
581 * Look for matches. First look for exact matches - session names must
582 * be unique so an exact match can't be ambigious and can just be
583 * returned.
585 if ((s = session_find(name)) != NULL)
586 return (s);
589 * Otherwise look for partial matches, returning early if it is found to
590 * be ambiguous.
592 sfound = NULL;
593 RB_FOREACH(s, sessions, &sessions) {
594 if (strncmp(name, s->name, strlen(name)) == 0 ||
595 fnmatch(name, s->name, 0) == 0) {
596 if (sfound != NULL) {
597 *ambiguous = 1;
598 return (NULL);
600 sfound = s;
603 return (sfound);
607 * Lookup a window or return -1 if not found or ambigious. First try as an
608 * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
609 * idx if the window index is a valid number but there is no window with that
610 * index.
612 struct winlink *
613 cmd_lookup_window(struct session *s, const char *name, int *ambiguous)
615 struct winlink *wl, *wlfound;
616 const char *errstr;
617 u_int idx;
619 *ambiguous = 0;
621 /* Try as a window id. */
622 if ((wl = cmd_lookup_winlink_windowid(s, name)) != NULL)
623 return (wl);
625 /* First see if this is a valid window index in this session. */
626 idx = strtonum(name, 0, INT_MAX, &errstr);
627 if (errstr == NULL) {
628 if ((wl = winlink_find_by_index(&s->windows, idx)) != NULL)
629 return (wl);
632 /* Look for exact matches, error if more than one. */
633 wlfound = NULL;
634 RB_FOREACH(wl, winlinks, &s->windows) {
635 if (strcmp(name, wl->window->name) == 0) {
636 if (wlfound != NULL) {
637 *ambiguous = 1;
638 return (NULL);
640 wlfound = wl;
643 if (wlfound != NULL)
644 return (wlfound);
646 /* Now look for pattern matches, again error if multiple. */
647 wlfound = NULL;
648 RB_FOREACH(wl, winlinks, &s->windows) {
649 if (strncmp(name, wl->window->name, strlen(name)) == 0 ||
650 fnmatch(name, wl->window->name, 0) == 0) {
651 if (wlfound != NULL) {
652 *ambiguous = 1;
653 return (NULL);
655 wlfound = wl;
658 if (wlfound != NULL)
659 return (wlfound);
661 return (NULL);
665 * Find a window index - if the window doesn't exist, check if it is a
666 * potential index and return it anyway.
669 cmd_lookup_index(struct session *s, const char *name, int *ambiguous)
671 struct winlink *wl;
672 const char *errstr;
673 u_int idx;
675 if ((wl = cmd_lookup_window(s, name, ambiguous)) != NULL)
676 return (wl->idx);
677 if (*ambiguous)
678 return (-1);
680 idx = strtonum(name, 0, INT_MAX, &errstr);
681 if (errstr == NULL)
682 return (idx);
684 return (-1);
687 /* Lookup pane id. An initial % means a pane id. */
688 struct window_pane *
689 cmd_lookup_paneid(const char *arg)
691 const char *errstr;
692 u_int paneid;
694 if (*arg != '%')
695 return (NULL);
697 paneid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
698 if (errstr != NULL)
699 return (NULL);
700 return (window_pane_find_by_id(paneid));
703 /* Lookup window id in a session. An initial @ means a window id. */
704 struct winlink *
705 cmd_lookup_winlink_windowid(struct session *s, const char *arg)
707 const char *errstr;
708 u_int windowid;
710 if (*arg != '@')
711 return (NULL);
713 windowid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
714 if (errstr != NULL)
715 return (NULL);
716 return (winlink_find_by_window_id(&s->windows, windowid));
719 /* Lookup window id. An initial @ means a window id. */
720 struct window *
721 cmd_lookup_windowid(const char *arg)
723 const char *errstr;
724 u_int windowid;
726 if (*arg != '@')
727 return (NULL);
729 windowid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
730 if (errstr != NULL)
731 return (NULL);
732 return (window_find_by_id(windowid));
735 /* Find session and winlink for window. */
736 struct session *
737 cmd_window_session(struct cmd_q *cmdq, struct window *w, struct winlink **wlp)
739 struct session *s;
740 struct sessionslist ss;
741 struct winlink *wl;
743 /* If this window is in the current session, return that winlink. */
744 s = cmd_current_session(cmdq, 0);
745 if (s != NULL) {
746 wl = winlink_find_by_window(&s->windows, w);
747 if (wl != NULL) {
748 if (wlp != NULL)
749 *wlp = wl;
750 return (s);
754 /* Otherwise choose from all sessions with this window. */
755 ARRAY_INIT(&ss);
756 RB_FOREACH(s, sessions, &sessions) {
757 if (winlink_find_by_window(&s->windows, w) != NULL)
758 ARRAY_ADD(&ss, s);
760 s = cmd_choose_session_list(&ss);
761 ARRAY_FREE(&ss);
762 if (wlp != NULL)
763 *wlp = winlink_find_by_window(&s->windows, w);
764 return (s);
767 /* Find the target session or report an error and return NULL. */
768 struct session *
769 cmd_find_session(struct cmd_q *cmdq, const char *arg, int prefer_unattached)
771 struct session *s;
772 struct window_pane *wp;
773 struct window *w;
774 struct client *c;
775 char *tmparg;
776 size_t arglen;
777 int ambiguous;
779 /* A NULL argument means the current session. */
780 if (arg == NULL)
781 return (cmd_current_session(cmdq, prefer_unattached));
783 /* Lookup as pane id or window id. */
784 if ((wp = cmd_lookup_paneid(arg)) != NULL)
785 return (cmd_window_session(cmdq, wp->window, NULL));
786 if ((w = cmd_lookup_windowid(arg)) != NULL)
787 return (cmd_window_session(cmdq, w, NULL));
789 /* Trim a single trailing colon if any. */
790 tmparg = xstrdup(arg);
791 arglen = strlen(tmparg);
792 if (arglen != 0 && tmparg[arglen - 1] == ':')
793 tmparg[arglen - 1] = '\0';
795 /* An empty session name is the current session. */
796 if (*tmparg == '\0') {
797 free(tmparg);
798 return (cmd_current_session(cmdq, prefer_unattached));
801 /* Find the session, if any. */
802 s = cmd_lookup_session(tmparg, &ambiguous);
804 /* If it doesn't, try to match it as a client. */
805 if (s == NULL && (c = cmd_lookup_client(tmparg)) != NULL)
806 s = c->session;
808 /* If no session found, report an error. */
809 if (s == NULL) {
810 if (ambiguous)
811 cmdq_error(cmdq, "more than one session: %s", tmparg);
812 else
813 cmdq_error(cmdq, "session not found: %s", tmparg);
816 free(tmparg);
817 return (s);
820 /* Find the target session and window or report an error and return NULL. */
821 struct winlink *
822 cmd_find_window(struct cmd_q *cmdq, const char *arg, struct session **sp)
824 struct session *s;
825 struct winlink *wl;
826 struct window_pane *wp;
827 const char *winptr;
828 char *sessptr = NULL;
829 int ambiguous = 0;
832 * Find the current session. There must always be a current session, if
833 * it can't be found, report an error.
835 if ((s = cmd_current_session(cmdq, 0)) == NULL) {
836 cmdq_error(cmdq, "can't establish current session");
837 return (NULL);
840 /* A NULL argument means the current session and window. */
841 if (arg == NULL) {
842 if (sp != NULL)
843 *sp = s;
844 return (s->curw);
847 /* Lookup as pane id. */
848 if ((wp = cmd_lookup_paneid(arg)) != NULL) {
849 s = cmd_window_session(cmdq, wp->window, &wl);
850 if (sp != NULL)
851 *sp = s;
852 return (wl);
855 /* Time to look at the argument. If it is empty, that is an error. */
856 if (*arg == '\0')
857 goto not_found;
859 /* Find the separating colon and split into window and session. */
860 winptr = strchr(arg, ':');
861 if (winptr == NULL)
862 goto no_colon;
863 winptr++; /* skip : */
864 sessptr = xstrdup(arg);
865 *strchr(sessptr, ':') = '\0';
867 /* Try to lookup the session if present. */
868 if (*sessptr != '\0') {
869 if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
870 goto no_session;
872 if (sp != NULL)
873 *sp = s;
876 * Then work out the window. An empty string is the current window,
877 * otherwise try special cases then to look it up in the session.
879 if (*winptr == '\0')
880 wl = s->curw;
881 else if (winptr[0] == '!' && winptr[1] == '\0')
882 wl = TAILQ_FIRST(&s->lastw);
883 else if (winptr[0] == '^' && winptr[1] == '\0')
884 wl = RB_MIN(winlinks, &s->windows);
885 else if (winptr[0] == '$' && winptr[1] == '\0')
886 wl = RB_MAX(winlinks, &s->windows);
887 else if (winptr[0] == '+' || winptr[0] == '-')
888 wl = cmd_find_window_offset(winptr, s, &ambiguous);
889 else
890 wl = cmd_lookup_window(s, winptr, &ambiguous);
891 if (wl == NULL)
892 goto not_found;
894 if (sessptr != NULL)
895 free(sessptr);
896 return (wl);
898 no_colon:
900 * No colon in the string, first try special cases, then as a window
901 * and lastly as a session.
903 if (arg[0] == '!' && arg[1] == '\0') {
904 if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
905 goto not_found;
906 } else if (arg[0] == '+' || arg[0] == '-') {
907 if ((wl = cmd_find_window_offset(arg, s, &ambiguous)) == NULL)
908 goto lookup_session;
909 } else if ((wl = cmd_lookup_window(s, arg, &ambiguous)) == NULL)
910 goto lookup_session;
912 if (sp != NULL)
913 *sp = s;
915 return (wl);
917 lookup_session:
918 if (ambiguous)
919 goto not_found;
920 if (*arg != '\0' && (s = cmd_lookup_session(arg, &ambiguous)) == NULL)
921 goto no_session;
923 if (sp != NULL)
924 *sp = s;
926 return (s->curw);
928 no_session:
929 if (ambiguous)
930 cmdq_error(cmdq, "multiple sessions: %s", arg);
931 else
932 cmdq_error(cmdq, "session not found: %s", arg);
933 free(sessptr);
934 return (NULL);
936 not_found:
937 if (ambiguous)
938 cmdq_error(cmdq, "multiple windows: %s", arg);
939 else
940 cmdq_error(cmdq, "window not found: %s", arg);
941 free(sessptr);
942 return (NULL);
945 struct winlink *
946 cmd_find_window_offset(const char *winptr, struct session *s, int *ambiguous)
948 struct winlink *wl;
949 int offset = 1;
951 if (winptr[1] != '\0')
952 offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
953 if (offset == 0)
954 wl = cmd_lookup_window(s, winptr, ambiguous);
955 else {
956 if (winptr[0] == '+')
957 wl = winlink_next_by_number(s->curw, s, offset);
958 else
959 wl = winlink_previous_by_number(s->curw, s, offset);
962 return (wl);
966 * Find the target session and window index, whether or not it exists in the
967 * session. Return -2 on error or -1 if no window index is specified. This is
968 * used when parsing an argument for a window target that may not exist (for
969 * example if it is going to be created).
972 cmd_find_index(struct cmd_q *cmdq, const char *arg, struct session **sp)
974 struct session *s;
975 struct winlink *wl;
976 const char *winptr;
977 char *sessptr = NULL;
978 int idx, ambiguous = 0;
981 * Find the current session. There must always be a current session, if
982 * it can't be found, report an error.
984 if ((s = cmd_current_session(cmdq, 0)) == NULL) {
985 cmdq_error(cmdq, "can't establish current session");
986 return (-2);
989 /* A NULL argument means the current session and "no window" (-1). */
990 if (arg == NULL) {
991 if (sp != NULL)
992 *sp = s;
993 return (-1);
996 /* Time to look at the argument. If it is empty, that is an error. */
997 if (*arg == '\0')
998 goto not_found;
1000 /* Find the separating colon. If none, assume the current session. */
1001 winptr = strchr(arg, ':');
1002 if (winptr == NULL)
1003 goto no_colon;
1004 winptr++; /* skip : */
1005 sessptr = xstrdup(arg);
1006 *strchr(sessptr, ':') = '\0';
1008 /* Try to lookup the session if present. */
1009 if (sessptr != NULL && *sessptr != '\0') {
1010 if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
1011 goto no_session;
1013 if (sp != NULL)
1014 *sp = s;
1017 * Then work out the window. An empty string is a new window otherwise
1018 * try to look it up in the session.
1020 if (*winptr == '\0')
1021 idx = -1;
1022 else if (winptr[0] == '!' && winptr[1] == '\0') {
1023 if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
1024 goto not_found;
1025 idx = wl->idx;
1026 } else if (winptr[0] == '+' || winptr[0] == '-') {
1027 if ((idx = cmd_find_index_offset(winptr, s, &ambiguous)) < 0)
1028 goto invalid_index;
1029 } else if ((idx = cmd_lookup_index(s, winptr, &ambiguous)) == -1)
1030 goto invalid_index;
1032 free(sessptr);
1033 return (idx);
1035 no_colon:
1037 * No colon in the string, first try special cases, then as a window
1038 * and lastly as a session.
1040 if (arg[0] == '!' && arg[1] == '\0') {
1041 if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
1042 goto not_found;
1043 idx = wl->idx;
1044 } else if (arg[0] == '+' || arg[0] == '-') {
1045 if ((idx = cmd_find_index_offset(arg, s, &ambiguous)) < 0)
1046 goto lookup_session;
1047 } else if ((idx = cmd_lookup_index(s, arg, &ambiguous)) == -1)
1048 goto lookup_session;
1050 if (sp != NULL)
1051 *sp = s;
1053 return (idx);
1055 lookup_session:
1056 if (ambiguous)
1057 goto not_found;
1058 if (*arg != '\0' && (s = cmd_lookup_session(arg, &ambiguous)) == NULL)
1059 goto no_session;
1061 if (sp != NULL)
1062 *sp = s;
1064 return (-1);
1066 no_session:
1067 if (ambiguous)
1068 cmdq_error(cmdq, "multiple sessions: %s", arg);
1069 else
1070 cmdq_error(cmdq, "session not found: %s", arg);
1071 free(sessptr);
1072 return (-2);
1074 invalid_index:
1075 if (ambiguous)
1076 goto not_found;
1077 cmdq_error(cmdq, "invalid index: %s", arg);
1079 free(sessptr);
1080 return (-2);
1082 not_found:
1083 if (ambiguous)
1084 cmdq_error(cmdq, "multiple windows: %s", arg);
1085 else
1086 cmdq_error(cmdq, "window not found: %s", arg);
1087 free(sessptr);
1088 return (-2);
1092 cmd_find_index_offset(const char *winptr, struct session *s, int *ambiguous)
1094 int idx, offset = 1;
1096 if (winptr[1] != '\0')
1097 offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
1098 if (offset == 0)
1099 idx = cmd_lookup_index(s, winptr, ambiguous);
1100 else {
1101 if (winptr[0] == '+') {
1102 if (s->curw->idx == INT_MAX)
1103 idx = cmd_lookup_index(s, winptr, ambiguous);
1104 else
1105 idx = s->curw->idx + offset;
1106 } else {
1107 if (s->curw->idx == 0)
1108 idx = cmd_lookup_index(s, winptr, ambiguous);
1109 else
1110 idx = s->curw->idx - offset;
1114 return (idx);
1118 * Find the target session, window and pane number or report an error and
1119 * return NULL. The pane number is separated from the session:window by a .,
1120 * such as mysession:mywindow.0.
1122 struct winlink *
1123 cmd_find_pane(struct cmd_q *cmdq,
1124 const char *arg, struct session **sp, struct window_pane **wpp)
1126 struct session *s;
1127 struct winlink *wl;
1128 const char *period, *errstr;
1129 char *winptr, *paneptr;
1130 u_int idx;
1132 /* Get the current session. */
1133 if ((s = cmd_current_session(cmdq, 0)) == NULL) {
1134 cmdq_error(cmdq, "can't establish current session");
1135 return (NULL);
1137 if (sp != NULL)
1138 *sp = s;
1140 /* A NULL argument means the current session, window and pane. */
1141 if (arg == NULL) {
1142 *wpp = s->curw->window->active;
1143 return (s->curw);
1146 /* Lookup as pane id. */
1147 if ((*wpp = cmd_lookup_paneid(arg)) != NULL) {
1148 s = cmd_window_session(cmdq, (*wpp)->window, &wl);
1149 if (sp != NULL)
1150 *sp = s;
1151 return (wl);
1154 /* Look for a separating period. */
1155 if ((period = strrchr(arg, '.')) == NULL)
1156 goto no_period;
1158 /* Pull out the window part and parse it. */
1159 winptr = xstrdup(arg);
1160 winptr[period - arg] = '\0';
1161 if (*winptr == '\0')
1162 wl = s->curw;
1163 else if ((wl = cmd_find_window(cmdq, winptr, sp)) == NULL)
1164 goto error;
1166 /* Find the pane section and look it up. */
1167 paneptr = winptr + (period - arg) + 1;
1168 if (*paneptr == '\0')
1169 *wpp = wl->window->active;
1170 else if (paneptr[0] == '+' || paneptr[0] == '-')
1171 *wpp = cmd_find_pane_offset(paneptr, wl);
1172 else {
1173 idx = strtonum(paneptr, 0, INT_MAX, &errstr);
1174 if (errstr != NULL)
1175 goto lookup_string;
1176 *wpp = window_pane_at_index(wl->window, idx);
1177 if (*wpp == NULL)
1178 goto lookup_string;
1181 free(winptr);
1182 return (wl);
1184 lookup_string:
1185 /* Try pane string description. */
1186 if ((*wpp = window_find_string(wl->window, paneptr)) == NULL) {
1187 cmdq_error(cmdq, "can't find pane: %s", paneptr);
1188 goto error;
1191 free(winptr);
1192 return (wl);
1194 no_period:
1195 /* Try as a pane number alone. */
1196 idx = strtonum(arg, 0, INT_MAX, &errstr);
1197 if (errstr != NULL)
1198 goto lookup_window;
1200 /* Try index in the current session and window. */
1201 if ((*wpp = window_pane_at_index(s->curw->window, idx)) == NULL)
1202 goto lookup_window;
1204 return (s->curw);
1206 lookup_window:
1207 /* Try pane string description. */
1208 if ((*wpp = window_find_string(s->curw->window, arg)) != NULL)
1209 return (s->curw);
1211 /* Try as a window and use the active pane. */
1212 if ((wl = cmd_find_window(cmdq, arg, sp)) != NULL)
1213 *wpp = wl->window->active;
1214 return (wl);
1216 error:
1217 free(winptr);
1218 return (NULL);
1221 struct window_pane *
1222 cmd_find_pane_offset(const char *paneptr, struct winlink *wl)
1224 struct window *w = wl->window;
1225 struct window_pane *wp = w->active;
1226 u_int offset = 1;
1228 if (paneptr[1] != '\0')
1229 offset = strtonum(paneptr + 1, 1, INT_MAX, NULL);
1230 if (offset > 0) {
1231 if (paneptr[0] == '+')
1232 wp = window_pane_next_by_number(w, wp, offset);
1233 else
1234 wp = window_pane_previous_by_number(w, wp, offset);
1237 return (wp);
1240 /* Replace the first %% or %idx in template by s. */
1241 char *
1242 cmd_template_replace(const char *template, const char *s, int idx)
1244 char ch, *buf;
1245 const char *ptr;
1246 int replaced;
1247 size_t len;
1249 if (strchr(template, '%') == NULL)
1250 return (xstrdup(template));
1252 buf = xmalloc(1);
1253 *buf = '\0';
1254 len = 0;
1255 replaced = 0;
1257 ptr = template;
1258 while (*ptr != '\0') {
1259 switch (ch = *ptr++) {
1260 case '%':
1261 if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
1262 if (*ptr != '%' || replaced)
1263 break;
1264 replaced = 1;
1266 ptr++;
1268 len += strlen(s);
1269 buf = xrealloc(buf, 1, len + 1);
1270 strlcat(buf, s, len + 1);
1271 continue;
1273 buf = xrealloc(buf, 1, len + 2);
1274 buf[len++] = ch;
1275 buf[len] = '\0';
1278 return (buf);
1282 * Return the default path for a new pane. Several special values are accepted:
1283 * the empty string or relative path for the current working directory,
1284 * ~ for the user's home, - for the base working directory, . for the server
1285 * working directory.
1287 const char *
1288 cmd_default_path(const char *base, const char *current, const char *in)
1290 const char *root;
1291 struct environ_entry *envent;
1292 char tmp[MAXPATHLEN];
1293 struct passwd *pw;
1294 int n;
1295 size_t skip;
1296 static char path[MAXPATHLEN];
1298 skip = 1;
1299 if (strcmp(in, "$HOME") == 0 || strncmp(in, "$HOME/", 6) == 0) {
1300 /* User's home directory - $HOME. */
1301 skip = 5;
1302 goto find_home;
1303 } else if (in[0] == '~' && (in[1] == '\0' || in[1] == '/')) {
1304 /* User's home directory - ~. */
1305 goto find_home;
1306 } else if (in[0] == '-' && (in[1] == '\0' || in[1] == '/')) {
1307 /* Base working directory. */
1308 root = base;
1309 goto complete_path;
1310 } else if (in[0] == '.' && (in[1] == '\0' || in[1] == '/')) {
1311 /* Server working directory. */
1312 if (getcwd(tmp, sizeof tmp) != NULL) {
1313 root = tmp;
1314 goto complete_path;
1316 return ("/");
1317 } else if (*in == '/') {
1318 /* Absolute path. */
1319 return (in);
1320 } else {
1321 /* Empty or relative path. */
1322 if (current != NULL)
1323 root = current;
1324 else
1325 return (base);
1326 skip = 0;
1327 goto complete_path;
1330 return (base);
1332 find_home:
1333 envent = environ_find(&global_environ, "HOME");
1334 if (envent != NULL && *envent->value != '\0')
1335 root = envent->value;
1336 else if ((pw = getpwuid(getuid())) != NULL)
1337 root = pw->pw_dir;
1338 else
1339 return (base);
1341 complete_path:
1342 if (root[skip] == '\0') {
1343 strlcpy(path, root, sizeof path);
1344 return (path);
1346 n = snprintf(path, sizeof path, "%s/%s", root, in + skip);
1347 if (n > 0 && (size_t)n < sizeof path)
1348 return (path);
1349 return (base);