Annotation of src/usr.bin/tmux/cmd.c, Revision 1.80
1.80 ! nicm 1: /* $OpenBSD: cmd.c,v 1.79 2013/03/24 09:27:20 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"
30:
31: const struct cmd_entry *cmd_table[] = {
32: &cmd_attach_session_entry,
33: &cmd_bind_key_entry,
34: &cmd_break_pane_entry,
1.34 nicm 35: &cmd_capture_pane_entry,
1.42 nicm 36: &cmd_choose_buffer_entry,
1.15 nicm 37: &cmd_choose_client_entry,
1.70 nicm 38: &cmd_choose_list_entry,
1.1 nicm 39: &cmd_choose_session_entry,
1.67 nicm 40: &cmd_choose_tree_entry,
1.1 nicm 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,
1.7 nicm 49: &cmd_display_message_entry,
1.16 nicm 50: &cmd_display_panes_entry,
1.1 nicm 51: &cmd_find_window_entry,
52: &cmd_has_session_entry,
1.4 nicm 53: &cmd_if_shell_entry,
1.37 nicm 54: &cmd_join_pane_entry,
1.1 nicm 55: &cmd_kill_pane_entry,
56: &cmd_kill_server_entry,
57: &cmd_kill_session_entry,
58: &cmd_kill_window_entry,
1.45 nicm 59: &cmd_last_pane_entry,
1.1 nicm 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,
1.23 nicm 66: &cmd_list_panes_entry,
1.1 nicm 67: &cmd_list_sessions_entry,
68: &cmd_list_windows_entry,
69: &cmd_load_buffer_entry,
1.19 nicm 70: &cmd_lock_client_entry,
1.1 nicm 71: &cmd_lock_server_entry,
1.19 nicm 72: &cmd_lock_session_entry,
1.63 nicm 73: &cmd_move_pane_entry,
1.1 nicm 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,
1.24 nicm 80: &cmd_pipe_pane_entry,
1.1 nicm 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,
1.56 nicm 87: &cmd_respawn_pane_entry,
1.1 nicm 88: &cmd_respawn_window_entry,
89: &cmd_rotate_window_entry,
1.17 nicm 90: &cmd_run_shell_entry,
1.1 nicm 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,
1.13 nicm 99: &cmd_set_environment_entry,
1.1 nicm 100: &cmd_set_option_entry,
101: &cmd_set_window_option_entry,
102: &cmd_show_buffer_entry,
1.13 nicm 103: &cmd_show_environment_entry,
1.32 nicm 104: &cmd_show_messages_entry,
1.1 nicm 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: NULL
117: };
118:
1.72 nicm 119: int cmd_session_better(struct session *, struct session *, int);
1.47 nicm 120: struct session *cmd_choose_session_list(struct sessionslist *);
1.52 nicm 121: struct session *cmd_choose_session(int);
1.31 nicm 122: struct client *cmd_choose_client(struct clients *);
1.5 nicm 123: struct client *cmd_lookup_client(const char *);
124: struct session *cmd_lookup_session(const char *, int *);
125: struct winlink *cmd_lookup_window(struct session *, const char *, int *);
1.8 nicm 126: int cmd_lookup_index(struct session *, const char *, int *);
1.51 nicm 127: struct window_pane *cmd_lookup_paneid(const char *);
1.60 nicm 128: struct winlink *cmd_lookup_winlink_windowid(struct session *, const char *);
129: struct window *cmd_lookup_windowid(const char *);
1.80 ! nicm 130: struct session *cmd_window_session(struct cmd_q *, struct window *,
! 131: struct winlink **);
1.43 nicm 132: struct winlink *cmd_find_window_offset(const char *, struct session *, int *);
133: int cmd_find_index_offset(const char *, struct session *, int *);
1.51 nicm 134: struct window_pane *cmd_find_pane_offset(const char *, struct winlink *);
1.5 nicm 135:
1.10 nicm 136: int
137: cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
138: {
139: size_t arglen;
140: int i;
141:
142: *buf = '\0';
143: for (i = 0; i < argc; i++) {
144: if (strlcpy(buf, argv[i], len) >= len)
145: return (-1);
146: arglen = strlen(argv[i]) + 1;
147: buf += arglen;
148: len -= arglen;
149: }
150:
151: return (0);
152: }
153:
154: int
155: cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
156: {
157: int i;
158: size_t arglen;
159:
160: if (argc == 0)
161: return (0);
162: *argv = xcalloc(argc, sizeof **argv);
163:
164: buf[len - 1] = '\0';
165: for (i = 0; i < argc; i++) {
166: if (len == 0) {
167: cmd_free_argv(argc, *argv);
168: return (-1);
169: }
170:
171: arglen = strlen(buf) + 1;
172: (*argv)[i] = xstrdup(buf);
173: buf += arglen;
174: len -= arglen;
175: }
176:
177: return (0);
1.46 nicm 178: }
179:
180: char **
1.49 nicm 181: cmd_copy_argv(int argc, char *const *argv)
1.46 nicm 182: {
183: char **new_argv;
184: int i;
185:
186: if (argc == 0)
187: return (NULL);
188: new_argv = xcalloc(argc, sizeof *new_argv);
189: for (i = 0; i < argc; i++) {
190: if (argv[i] != NULL)
191: new_argv[i] = xstrdup(argv[i]);
192: }
193: return (new_argv);
1.10 nicm 194: }
195:
196: void
197: cmd_free_argv(int argc, char **argv)
198: {
199: int i;
200:
201: if (argc == 0)
1.35 nicm 202: return;
1.68 nicm 203: for (i = 0; i < argc; i++)
204: free(argv[i]);
205: free(argv);
1.10 nicm 206: }
207:
1.1 nicm 208: struct cmd *
1.80 ! nicm 209: cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause)
1.1 nicm 210: {
211: const struct cmd_entry **entryp, *entry;
1.27 deraadt 212: struct cmd *cmd;
1.49 nicm 213: struct args *args;
1.1 nicm 214: char s[BUFSIZ];
1.49 nicm 215: int ambiguous = 0;
1.1 nicm 216:
217: *cause = NULL;
1.2 nicm 218: if (argc == 0) {
219: xasprintf(cause, "no command");
1.1 nicm 220: return (NULL);
1.2 nicm 221: }
1.1 nicm 222:
223: entry = NULL;
224: for (entryp = cmd_table; *entryp != NULL; entryp++) {
225: if ((*entryp)->alias != NULL &&
226: strcmp((*entryp)->alias, argv[0]) == 0) {
1.3 nicm 227: ambiguous = 0;
1.1 nicm 228: entry = *entryp;
229: break;
230: }
231:
232: if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
233: continue;
234: if (entry != NULL)
1.3 nicm 235: ambiguous = 1;
1.1 nicm 236: entry = *entryp;
237:
238: /* Bail now if an exact match. */
239: if (strcmp(entry->name, argv[0]) == 0)
240: break;
241: }
1.3 nicm 242: if (ambiguous)
243: goto ambiguous;
1.1 nicm 244: if (entry == NULL) {
245: xasprintf(cause, "unknown command: %s", argv[0]);
246: return (NULL);
247: }
248:
1.49 nicm 249: args = args_parse(entry->args_template, argc, argv);
250: if (args == NULL)
251: goto usage;
252: if (entry->args_lower != -1 && args->argc < entry->args_lower)
253: goto usage;
254: if (entry->args_upper != -1 && args->argc > entry->args_upper)
255: goto usage;
256: if (entry->check != NULL && entry->check(args) != 0)
257: goto usage;
1.1 nicm 258:
1.80 ! nicm 259: cmd = xcalloc(1, sizeof *cmd);
1.1 nicm 260: cmd->entry = entry;
1.49 nicm 261: cmd->args = args;
1.80 ! nicm 262:
! 263: if (file != NULL)
! 264: cmd->file = xstrdup(file);
! 265: cmd->line = line;
! 266:
1.1 nicm 267: return (cmd);
268:
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;
278: }
279: s[strlen(s) - 2] = '\0';
280: xasprintf(cause, "ambiguous command: %s, could be: %s", argv[0], s);
281: return (NULL);
282:
283: usage:
1.49 nicm 284: if (args != NULL)
285: args_free(args);
1.1 nicm 286: xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
287: return (NULL);
288: }
289:
290: size_t
291: cmd_print(struct cmd *cmd, char *buf, size_t len)
292: {
1.49 nicm 293: size_t off, used;
294:
295: off = xsnprintf(buf, len, "%s ", cmd->entry->name);
296: if (off < len) {
297: used = args_print(cmd->args, buf + off, len - off);
298: if (used == 0)
1.64 nicm 299: off--;
300: else
1.49 nicm 301: off += used;
1.64 nicm 302: buf[off] = '\0';
1.49 nicm 303: }
304: return (off);
1.1 nicm 305: }
306:
1.5 nicm 307: /*
308: * Figure out the current session. Use: 1) the current session, if the command
1.31 nicm 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.
1.5 nicm 313: */
1.1 nicm 314: struct session *
1.80 ! nicm 315: cmd_current_session(struct cmd_q *cmdq, int prefer_unattached)
1.1 nicm 316: {
1.80 ! nicm 317: struct msg_command_data *data = cmdq->msgdata;
! 318: struct client *c = cmdq->client;
1.5 nicm 319: struct session *s;
1.47 nicm 320: struct sessionslist ss;
1.11 nicm 321: struct winlink *wl;
322: struct window_pane *wp;
1.80 ! nicm 323: const char *path;
1.11 nicm 324: int found;
1.1 nicm 325:
1.80 ! nicm 326: if (c != NULL && c->session != NULL)
! 327: return (c->session);
1.1 nicm 328:
1.11 nicm 329: /*
330: * If the name of the calling client's pty is know, build a list of the
331: * sessions that contain it and if any choose either the first or the
332: * newest.
333: */
1.80 ! nicm 334: path = c == NULL ? NULL : c->tty.path;
! 335: if (path != NULL) {
1.11 nicm 336: ARRAY_INIT(&ss);
1.47 nicm 337: RB_FOREACH(s, sessions, &sessions) {
1.11 nicm 338: found = 0;
339: RB_FOREACH(wl, winlinks, &s->windows) {
340: TAILQ_FOREACH(wp, &wl->window->panes, entry) {
1.80 ! nicm 341: if (strcmp(wp->tty, path) == 0) {
1.11 nicm 342: found = 1;
343: break;
344: }
345: }
346: if (found)
347: break;
348: }
349: if (found)
350: ARRAY_ADD(&ss, s);
351: }
352:
1.47 nicm 353: s = cmd_choose_session_list(&ss);
1.11 nicm 354: ARRAY_FREE(&ss);
355: if (s != NULL)
356: return (s);
357: }
358:
359: /* Use the session from the TMUX environment variable. */
1.50 nicm 360: if (data != NULL && data->pid == getpid() && data->idx != -1) {
1.47 nicm 361: s = session_find_by_index(data->idx);
362: if (s != NULL)
363: return (s);
364: }
365:
1.52 nicm 366: return (cmd_choose_session(prefer_unattached));
1.47 nicm 367: }
368:
1.72 nicm 369: /* Is this session better? */
370: int
371: cmd_session_better(struct session *s, struct session *best,
372: int prefer_unattached)
373: {
374: if (best == NULL)
1.74 nicm 375: return (1);
1.72 nicm 376: if (prefer_unattached) {
377: if (!(best->flags & SESSION_UNATTACHED) &&
378: (s->flags & SESSION_UNATTACHED))
1.74 nicm 379: return (1);
1.72 nicm 380: else if ((best->flags & SESSION_UNATTACHED) &&
381: !(s->flags & SESSION_UNATTACHED))
1.74 nicm 382: return (0);
1.72 nicm 383: }
384: return (timercmp(&s->activity_time, &best->activity_time, >));
385: }
386:
1.52 nicm 387: /*
388: * Find the most recently used session, preferring unattached if the flag is
389: * set.
390: */
1.47 nicm 391: struct session *
1.52 nicm 392: cmd_choose_session(int prefer_unattached)
1.47 nicm 393: {
1.72 nicm 394: struct session *s, *best;
1.47 nicm 395:
1.72 nicm 396: best = NULL;
1.47 nicm 397: RB_FOREACH(s, sessions, &sessions) {
1.72 nicm 398: if (cmd_session_better(s, best, prefer_unattached))
399: best = s;
1.47 nicm 400: }
1.72 nicm 401: return (best);
1.5 nicm 402: }
403:
1.31 nicm 404: /* Find the most recently used session from a list. */
1.5 nicm 405: struct session *
1.47 nicm 406: cmd_choose_session_list(struct sessionslist *ss)
1.5 nicm 407: {
1.31 nicm 408: struct session *s, *sbest;
1.5 nicm 409: struct timeval *tv = NULL;
410: u_int i;
411:
1.31 nicm 412: sbest = NULL;
1.11 nicm 413: for (i = 0; i < ARRAY_LENGTH(ss); i++) {
414: if ((s = ARRAY_ITEM(ss, i)) == NULL)
1.5 nicm 415: continue;
416:
1.31 nicm 417: if (tv == NULL || timercmp(&s->activity_time, tv, >)) {
418: sbest = s;
419: tv = &s->activity_time;
1.1 nicm 420: }
421: }
1.5 nicm 422:
1.31 nicm 423: return (sbest);
1.1 nicm 424: }
425:
1.30 nicm 426: /*
427: * Find the current client. First try the current client if set, then pick the
1.31 nicm 428: * most recently used of the clients attached to the current session if any,
429: * then of all clients.
1.30 nicm 430: */
431: struct client *
1.80 ! nicm 432: cmd_current_client(struct cmd_q *cmdq)
1.30 nicm 433: {
434: struct session *s;
435: struct client *c;
436: struct clients cc;
437: u_int i;
438:
1.80 ! nicm 439: if (cmdq->client != NULL && cmdq->client->session != NULL)
! 440: return (cmdq->client);
1.30 nicm 441:
442: /*
443: * No current client set. Find the current session and return the
444: * newest of its clients.
445: */
1.80 ! nicm 446: s = cmd_current_session(cmdq, 0);
1.30 nicm 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);
454: }
455:
1.31 nicm 456: c = cmd_choose_client(&cc);
1.30 nicm 457: ARRAY_FREE(&cc);
458: if (c != NULL)
459: return (c);
460: }
461:
1.31 nicm 462: return (cmd_choose_client(&clients));
1.30 nicm 463: }
464:
1.31 nicm 465: /* Choose the most recently used client from a list. */
1.20 nicm 466: struct client *
1.31 nicm 467: cmd_choose_client(struct clients *cc)
1.20 nicm 468: {
1.31 nicm 469: struct client *c, *cbest;
1.20 nicm 470: struct timeval *tv = NULL;
471: u_int i;
472:
1.31 nicm 473: cbest = NULL;
1.30 nicm 474: for (i = 0; i < ARRAY_LENGTH(cc); i++) {
475: if ((c = ARRAY_ITEM(cc, i)) == NULL)
1.20 nicm 476: continue;
477: if (c->session == NULL)
478: continue;
479:
1.31 nicm 480: if (tv == NULL || timercmp(&c->activity_time, tv, >)) {
481: cbest = c;
482: tv = &c->activity_time;
1.20 nicm 483: }
484: }
485:
1.31 nicm 486: return (cbest);
1.20 nicm 487: }
488:
1.5 nicm 489: /* Find the target client or report an error and return NULL. */
1.1 nicm 490: struct client *
1.80 ! nicm 491: cmd_find_client(struct cmd_q *cmdq, const char *arg, int quiet)
1.1 nicm 492: {
1.30 nicm 493: struct client *c;
1.5 nicm 494: char *tmparg;
495: size_t arglen;
1.1 nicm 496:
1.5 nicm 497: /* A NULL argument means the current client. */
1.79 nicm 498: if (arg == NULL) {
1.80 ! nicm 499: c = cmd_current_client(cmdq);
1.79 nicm 500: if (c == NULL && !quiet)
1.80 ! nicm 501: cmdq_error(cmdq, "no clients");
1.79 nicm 502: return (c);
503: }
1.5 nicm 504: tmparg = xstrdup(arg);
505:
506: /* Trim a single trailing colon if any. */
507: arglen = strlen(tmparg);
508: if (arglen != 0 && tmparg[arglen - 1] == ':')
509: tmparg[arglen - 1] = '\0';
510:
511: /* Find the client, if any. */
512: c = cmd_lookup_client(tmparg);
513:
514: /* If no client found, report an error. */
1.79 nicm 515: if (c == NULL && !quiet)
1.80 ! nicm 516: cmdq_error(cmdq, "client not found: %s", tmparg);
1.5 nicm 517:
1.68 nicm 518: free(tmparg);
1.5 nicm 519: return (c);
520: }
521:
522: /*
523: * Lookup a client by device path. Either of a full match and a match without a
524: * leading _PATH_DEV ("/dev/") is accepted.
525: */
526: struct client *
527: cmd_lookup_client(const char *name)
528: {
529: struct client *c;
530: const char *path;
531: u_int i;
532:
533: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1.25 nicm 534: c = ARRAY_ITEM(&clients, i);
535: if (c == NULL || c->session == NULL)
1.5 nicm 536: continue;
537: path = c->tty.path;
538:
539: /* Check for exact matches. */
540: if (strcmp(name, path) == 0)
541: return (c);
542:
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);
548: }
549:
550: return (NULL);
551: }
552:
553: /* Lookup a session by name. If no session is found, NULL is returned. */
554: struct session *
555: cmd_lookup_session(const char *name, int *ambiguous)
556: {
557: struct session *s, *sfound;
558:
559: *ambiguous = 0;
560:
561: /*
1.28 nicm 562: * Look for matches. First look for exact matches - session names must
563: * be unique so an exact match can't be ambigious and can just be
564: * returned.
1.5 nicm 565: */
1.47 nicm 566: if ((s = session_find(name)) != NULL)
567: return (s);
1.28 nicm 568:
569: /*
570: * Otherwise look for partial matches, returning early if it is found to
571: * be ambiguous.
572: */
573: sfound = NULL;
1.47 nicm 574: RB_FOREACH(s, sessions, &sessions) {
1.5 nicm 575: if (strncmp(name, s->name, strlen(name)) == 0 ||
576: fnmatch(name, s->name, 0) == 0) {
577: if (sfound != NULL) {
578: *ambiguous = 1;
579: return (NULL);
580: }
581: sfound = s;
582: }
583: }
1.35 nicm 584: return (sfound);
1.5 nicm 585: }
586:
587: /*
1.8 nicm 588: * Lookup a window or return -1 if not found or ambigious. First try as an
589: * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
1.41 nicm 590: * idx if the window index is a valid number but there is no window with that
1.8 nicm 591: * index.
1.5 nicm 592: */
593: struct winlink *
594: cmd_lookup_window(struct session *s, const char *name, int *ambiguous)
595: {
596: struct winlink *wl, *wlfound;
597: const char *errstr;
598: u_int idx;
599:
600: *ambiguous = 0;
601:
1.60 nicm 602: /* Try as a window id. */
603: if ((wl = cmd_lookup_winlink_windowid(s, name)) != NULL)
604: return (wl);
605:
1.5 nicm 606: /* First see if this is a valid window index in this session. */
607: idx = strtonum(name, 0, INT_MAX, &errstr);
608: if (errstr == NULL) {
609: if ((wl = winlink_find_by_index(&s->windows, idx)) != NULL)
610: return (wl);
611: }
1.35 nicm 612:
1.5 nicm 613: /* Look for exact matches, error if more than one. */
614: wlfound = NULL;
615: RB_FOREACH(wl, winlinks, &s->windows) {
1.8 nicm 616: if (strcmp(name, wl->window->name) == 0) {
1.5 nicm 617: if (wlfound != NULL) {
618: *ambiguous = 1;
619: return (NULL);
620: }
621: wlfound = wl;
1.1 nicm 622: }
623: }
1.5 nicm 624: if (wlfound != NULL)
625: return (wlfound);
626:
627: /* Now look for pattern matches, again error if multiple. */
628: wlfound = NULL;
629: RB_FOREACH(wl, winlinks, &s->windows) {
1.8 nicm 630: if (strncmp(name, wl->window->name, strlen(name)) == 0 ||
631: fnmatch(name, wl->window->name, 0) == 0) {
1.5 nicm 632: if (wlfound != NULL) {
633: *ambiguous = 1;
634: return (NULL);
635: }
636: wlfound = wl;
637: }
1.35 nicm 638: }
1.5 nicm 639: if (wlfound != NULL)
640: return (wlfound);
641:
642: return (NULL);
1.1 nicm 643: }
644:
1.8 nicm 645: /*
646: * Find a window index - if the window doesn't exist, check if it is a
647: * potential index and return it anyway.
648: */
649: int
650: cmd_lookup_index(struct session *s, const char *name, int *ambiguous)
651: {
652: struct winlink *wl;
653: const char *errstr;
654: u_int idx;
655:
656: if ((wl = cmd_lookup_window(s, name, ambiguous)) != NULL)
657: return (wl->idx);
658: if (*ambiguous)
659: return (-1);
660:
661: idx = strtonum(name, 0, INT_MAX, &errstr);
662: if (errstr == NULL)
663: return (idx);
664:
665: return (-1);
666: }
667:
1.60 nicm 668: /* Lookup pane id. An initial % means a pane id. */
1.51 nicm 669: struct window_pane *
670: cmd_lookup_paneid(const char *arg)
671: {
672: const char *errstr;
673: u_int paneid;
674:
675: if (*arg != '%')
676: return (NULL);
677:
678: paneid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
679: if (errstr != NULL)
680: return (NULL);
681: return (window_pane_find_by_id(paneid));
682: }
683:
1.60 nicm 684: /* Lookup window id in a session. An initial @ means a window id. */
685: struct winlink *
686: cmd_lookup_winlink_windowid(struct session *s, const char *arg)
687: {
688: const char *errstr;
689: u_int windowid;
690:
691: if (*arg != '@')
692: return (NULL);
693:
694: windowid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
695: if (errstr != NULL)
696: return (NULL);
697: return (winlink_find_by_window_id(&s->windows, windowid));
698: }
699:
700: /* Lookup window id. An initial @ means a window id. */
701: struct window *
702: cmd_lookup_windowid(const char *arg)
703: {
704: const char *errstr;
705: u_int windowid;
706:
707: if (*arg != '@')
708: return (NULL);
709:
710: windowid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
711: if (errstr != NULL)
712: return (NULL);
713: return (window_find_by_id(windowid));
714: }
715:
716: /* Find session and winlink for window. */
1.51 nicm 717: struct session *
1.80 ! nicm 718: cmd_window_session(struct cmd_q *cmdq, struct window *w, struct winlink **wlp)
1.51 nicm 719: {
720: struct session *s;
721: struct sessionslist ss;
722: struct winlink *wl;
723:
1.60 nicm 724: /* If this window is in the current session, return that winlink. */
1.80 ! nicm 725: s = cmd_current_session(cmdq, 0);
1.51 nicm 726: if (s != NULL) {
1.60 nicm 727: wl = winlink_find_by_window(&s->windows, w);
1.51 nicm 728: if (wl != NULL) {
729: if (wlp != NULL)
730: *wlp = wl;
731: return (s);
732: }
733: }
734:
1.60 nicm 735: /* Otherwise choose from all sessions with this window. */
1.51 nicm 736: ARRAY_INIT(&ss);
737: RB_FOREACH(s, sessions, &sessions) {
1.60 nicm 738: if (winlink_find_by_window(&s->windows, w) != NULL)
1.51 nicm 739: ARRAY_ADD(&ss, s);
740: }
741: s = cmd_choose_session_list(&ss);
742: ARRAY_FREE(&ss);
743: if (wlp != NULL)
1.60 nicm 744: *wlp = winlink_find_by_window(&s->windows, w);
1.51 nicm 745: return (s);
746: }
747:
1.5 nicm 748: /* Find the target session or report an error and return NULL. */
1.1 nicm 749: struct session *
1.80 ! nicm 750: cmd_find_session(struct cmd_q *cmdq, const char *arg, int prefer_unattached)
1.1 nicm 751: {
1.51 nicm 752: struct session *s;
753: struct window_pane *wp;
1.60 nicm 754: struct window *w;
1.51 nicm 755: struct client *c;
756: char *tmparg;
757: size_t arglen;
758: int ambiguous;
1.1 nicm 759:
1.5 nicm 760: /* A NULL argument means the current session. */
1.1 nicm 761: if (arg == NULL)
1.80 ! nicm 762: return (cmd_current_session(cmdq, prefer_unattached));
1.5 nicm 763:
1.60 nicm 764: /* Lookup as pane id or window id. */
1.51 nicm 765: if ((wp = cmd_lookup_paneid(arg)) != NULL)
1.80 ! nicm 766: return (cmd_window_session(cmdq, wp->window, NULL));
1.60 nicm 767: if ((w = cmd_lookup_windowid(arg)) != NULL)
1.80 ! nicm 768: return (cmd_window_session(cmdq, w, NULL));
1.51 nicm 769:
1.5 nicm 770: /* Trim a single trailing colon if any. */
1.54 nicm 771: tmparg = xstrdup(arg);
1.5 nicm 772: arglen = strlen(tmparg);
773: if (arglen != 0 && tmparg[arglen - 1] == ':')
774: tmparg[arglen - 1] = '\0';
775:
1.53 nicm 776: /* An empty session name is the current session. */
777: if (*tmparg == '\0') {
1.68 nicm 778: free(tmparg);
1.80 ! nicm 779: return (cmd_current_session(cmdq, prefer_unattached));
1.53 nicm 780: }
781:
1.5 nicm 782: /* Find the session, if any. */
783: s = cmd_lookup_session(tmparg, &ambiguous);
784:
785: /* If it doesn't, try to match it as a client. */
786: if (s == NULL && (c = cmd_lookup_client(tmparg)) != NULL)
787: s = c->session;
788:
789: /* If no session found, report an error. */
790: if (s == NULL) {
791: if (ambiguous)
1.80 ! nicm 792: cmdq_error(cmdq, "more than one session: %s", tmparg);
1.5 nicm 793: else
1.80 ! nicm 794: cmdq_error(cmdq, "session not found: %s", tmparg);
1.1 nicm 795: }
1.5 nicm 796:
1.68 nicm 797: free(tmparg);
1.1 nicm 798: return (s);
799: }
800:
1.5 nicm 801: /* Find the target session and window or report an error and return NULL. */
1.1 nicm 802: struct winlink *
1.80 ! nicm 803: cmd_find_window(struct cmd_q *cmdq, const char *arg, struct session **sp)
1.1 nicm 804: {
1.51 nicm 805: struct session *s;
806: struct winlink *wl;
807: struct window_pane *wp;
808: const char *winptr;
809: char *sessptr = NULL;
810: int ambiguous = 0;
1.5 nicm 811:
812: /*
813: * Find the current session. There must always be a current session, if
814: * it can't be found, report an error.
815: */
1.80 ! nicm 816: if ((s = cmd_current_session(cmdq, 0)) == NULL) {
! 817: cmdq_error(cmdq, "can't establish current session");
1.5 nicm 818: return (NULL);
819: }
820:
821: /* A NULL argument means the current session and window. */
822: if (arg == NULL) {
823: if (sp != NULL)
824: *sp = s;
825: return (s->curw);
826: }
827:
1.51 nicm 828: /* Lookup as pane id. */
829: if ((wp = cmd_lookup_paneid(arg)) != NULL) {
1.80 ! nicm 830: s = cmd_window_session(cmdq, wp->window, &wl);
1.51 nicm 831: if (sp != NULL)
832: *sp = s;
833: return (wl);
834: }
835:
1.5 nicm 836: /* Time to look at the argument. If it is empty, that is an error. */
837: if (*arg == '\0')
838: goto not_found;
839:
1.8 nicm 840: /* Find the separating colon and split into window and session. */
1.5 nicm 841: winptr = strchr(arg, ':');
842: if (winptr == NULL)
1.8 nicm 843: goto no_colon;
844: winptr++; /* skip : */
845: sessptr = xstrdup(arg);
846: *strchr(sessptr, ':') = '\0';
1.5 nicm 847:
848: /* Try to lookup the session if present. */
1.8 nicm 849: if (*sessptr != '\0') {
850: if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
1.5 nicm 851: goto no_session;
852: }
853: if (sp != NULL)
854: *sp = s;
855:
856: /*
857: * Then work out the window. An empty string is the current window,
1.38 nicm 858: * otherwise try special cases then to look it up in the session.
1.5 nicm 859: */
1.8 nicm 860: if (*winptr == '\0')
1.5 nicm 861: wl = s->curw;
1.38 nicm 862: else if (winptr[0] == '!' && winptr[1] == '\0')
863: wl = TAILQ_FIRST(&s->lastw);
1.73 nicm 864: else if (winptr[0] == '^' && winptr[1] == '\0')
865: wl = RB_MIN(winlinks, &s->windows);
866: else if (winptr[0] == '$' && winptr[1] == '\0')
867: wl = RB_MAX(winlinks, &s->windows);
1.43 nicm 868: else if (winptr[0] == '+' || winptr[0] == '-')
869: wl = cmd_find_window_offset(winptr, s, &ambiguous);
870: else
1.38 nicm 871: wl = cmd_lookup_window(s, winptr, &ambiguous);
872: if (wl == NULL)
1.5 nicm 873: goto not_found;
1.35 nicm 874:
1.5 nicm 875: if (sessptr != NULL)
1.68 nicm 876: free(sessptr);
1.5 nicm 877: return (wl);
878:
1.8 nicm 879: no_colon:
1.38 nicm 880: /*
881: * No colon in the string, first try special cases, then as a window
882: * and lastly as a session.
883: */
884: if (arg[0] == '!' && arg[1] == '\0') {
885: if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
886: goto not_found;
1.41 nicm 887: } else if (arg[0] == '+' || arg[0] == '-') {
1.43 nicm 888: if ((wl = cmd_find_window_offset(arg, s, &ambiguous)) == NULL)
1.41 nicm 889: goto lookup_session;
890: } else if ((wl = cmd_lookup_window(s, arg, &ambiguous)) == NULL)
891: goto lookup_session;
1.8 nicm 892:
893: if (sp != NULL)
894: *sp = s;
895:
896: return (wl);
897:
1.41 nicm 898: lookup_session:
899: if (ambiguous)
900: goto not_found;
1.53 nicm 901: if (*arg != '\0' && (s = cmd_lookup_session(arg, &ambiguous)) == NULL)
1.41 nicm 902: goto no_session;
903:
904: if (sp != NULL)
905: *sp = s;
906:
907: return (s->curw);
908:
1.5 nicm 909: no_session:
910: if (ambiguous)
1.80 ! nicm 911: cmdq_error(cmdq, "multiple sessions: %s", arg);
1.5 nicm 912: else
1.80 ! nicm 913: cmdq_error(cmdq, "session not found: %s", arg);
1.68 nicm 914: free(sessptr);
1.5 nicm 915: return (NULL);
916:
917: not_found:
918: if (ambiguous)
1.80 ! nicm 919: cmdq_error(cmdq, "multiple windows: %s", arg);
1.5 nicm 920: else
1.80 ! nicm 921: cmdq_error(cmdq, "window not found: %s", arg);
1.68 nicm 922: free(sessptr);
1.5 nicm 923: return (NULL);
924: }
1.1 nicm 925:
1.43 nicm 926: struct winlink *
927: cmd_find_window_offset(const char *winptr, struct session *s, int *ambiguous)
928: {
929: struct winlink *wl;
930: int offset = 1;
931:
932: if (winptr[1] != '\0')
933: offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
934: if (offset == 0)
935: wl = cmd_lookup_window(s, winptr, ambiguous);
936: else {
937: if (winptr[0] == '+')
938: wl = winlink_next_by_number(s->curw, s, offset);
939: else
940: wl = winlink_previous_by_number(s->curw, s, offset);
941: }
942:
943: return (wl);
944: }
945:
1.5 nicm 946: /*
947: * Find the target session and window index, whether or not it exists in the
948: * session. Return -2 on error or -1 if no window index is specified. This is
1.8 nicm 949: * used when parsing an argument for a window target that may not exist (for
950: * example if it is going to be created).
1.5 nicm 951: */
952: int
1.80 ! nicm 953: cmd_find_index(struct cmd_q *cmdq, const char *arg, struct session **sp)
1.5 nicm 954: {
955: struct session *s;
1.38 nicm 956: struct winlink *wl;
1.8 nicm 957: const char *winptr;
1.5 nicm 958: char *sessptr = NULL;
959: int idx, ambiguous = 0;
960:
961: /*
962: * Find the current session. There must always be a current session, if
963: * it can't be found, report an error.
964: */
1.80 ! nicm 965: if ((s = cmd_current_session(cmdq, 0)) == NULL) {
! 966: cmdq_error(cmdq, "can't establish current session");
1.9 nicm 967: return (-2);
1.1 nicm 968: }
1.5 nicm 969:
970: /* A NULL argument means the current session and "no window" (-1). */
971: if (arg == NULL) {
972: if (sp != NULL)
973: *sp = s;
974: return (-1);
975: }
976:
977: /* Time to look at the argument. If it is empty, that is an error. */
978: if (*arg == '\0')
979: goto not_found;
980:
981: /* Find the separating colon. If none, assume the current session. */
982: winptr = strchr(arg, ':');
983: if (winptr == NULL)
1.8 nicm 984: goto no_colon;
985: winptr++; /* skip : */
986: sessptr = xstrdup(arg);
987: *strchr(sessptr, ':') = '\0';
1.5 nicm 988:
989: /* Try to lookup the session if present. */
990: if (sessptr != NULL && *sessptr != '\0') {
1.8 nicm 991: if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
1.5 nicm 992: goto no_session;
993: }
1.1 nicm 994: if (sp != NULL)
995: *sp = s;
996:
1.5 nicm 997: /*
1.8 nicm 998: * Then work out the window. An empty string is a new window otherwise
999: * try to look it up in the session.
1.5 nicm 1000: */
1.8 nicm 1001: if (*winptr == '\0')
1.38 nicm 1002: idx = -1;
1003: else if (winptr[0] == '!' && winptr[1] == '\0') {
1004: if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
1005: goto not_found;
1006: idx = wl->idx;
1.41 nicm 1007: } else if (winptr[0] == '+' || winptr[0] == '-') {
1.43 nicm 1008: if ((idx = cmd_find_index_offset(winptr, s, &ambiguous)) < 0)
1.41 nicm 1009: goto invalid_index;
1010: } else if ((idx = cmd_lookup_index(s, winptr, &ambiguous)) == -1)
1011: goto invalid_index;
1.35 nicm 1012:
1.68 nicm 1013: free(sessptr);
1.5 nicm 1014: return (idx);
1015:
1.8 nicm 1016: no_colon:
1.38 nicm 1017: /*
1018: * No colon in the string, first try special cases, then as a window
1019: * and lastly as a session.
1020: */
1021: if (arg[0] == '!' && arg[1] == '\0') {
1022: if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
1023: goto not_found;
1024: idx = wl->idx;
1.41 nicm 1025: } else if (arg[0] == '+' || arg[0] == '-') {
1.43 nicm 1026: if ((idx = cmd_find_index_offset(arg, s, &ambiguous)) < 0)
1.41 nicm 1027: goto lookup_session;
1028: } else if ((idx = cmd_lookup_index(s, arg, &ambiguous)) == -1)
1029: goto lookup_session;
1.8 nicm 1030:
1031: if (sp != NULL)
1032: *sp = s;
1033:
1034: return (idx);
1035:
1.41 nicm 1036: lookup_session:
1037: if (ambiguous)
1038: goto not_found;
1.53 nicm 1039: if (*arg != '\0' && (s = cmd_lookup_session(arg, &ambiguous)) == NULL)
1.41 nicm 1040: goto no_session;
1041:
1042: if (sp != NULL)
1043: *sp = s;
1044:
1045: return (-1);
1046:
1.5 nicm 1047: no_session:
1048: if (ambiguous)
1.80 ! nicm 1049: cmdq_error(cmdq, "multiple sessions: %s", arg);
1.5 nicm 1050: else
1.80 ! nicm 1051: cmdq_error(cmdq, "session not found: %s", arg);
1.68 nicm 1052: free(sessptr);
1.5 nicm 1053: return (-2);
1054:
1.41 nicm 1055: invalid_index:
1056: if (ambiguous)
1057: goto not_found;
1.80 ! nicm 1058: cmdq_error(cmdq, "invalid index: %s", arg);
1.41 nicm 1059:
1.68 nicm 1060: free(sessptr);
1.41 nicm 1061: return (-2);
1062:
1.5 nicm 1063: not_found:
1064: if (ambiguous)
1.80 ! nicm 1065: cmdq_error(cmdq, "multiple windows: %s", arg);
1.1 nicm 1066: else
1.80 ! nicm 1067: cmdq_error(cmdq, "window not found: %s", arg);
1.68 nicm 1068: free(sessptr);
1.5 nicm 1069: return (-2);
1.12 nicm 1070: }
1071:
1.43 nicm 1072: int
1073: cmd_find_index_offset(const char *winptr, struct session *s, int *ambiguous)
1074: {
1075: int idx, offset = 1;
1076:
1077: if (winptr[1] != '\0')
1078: offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
1079: if (offset == 0)
1080: idx = cmd_lookup_index(s, winptr, ambiguous);
1081: else {
1082: if (winptr[0] == '+') {
1083: if (s->curw->idx == INT_MAX)
1084: idx = cmd_lookup_index(s, winptr, ambiguous);
1085: else
1086: idx = s->curw->idx + offset;
1087: } else {
1088: if (s->curw->idx == 0)
1089: idx = cmd_lookup_index(s, winptr, ambiguous);
1090: else
1091: idx = s->curw->idx - offset;
1092: }
1093: }
1094:
1095: return (idx);
1096: }
1097:
1.12 nicm 1098: /*
1099: * Find the target session, window and pane number or report an error and
1100: * return NULL. The pane number is separated from the session:window by a .,
1101: * such as mysession:mywindow.0.
1102: */
1103: struct winlink *
1.80 ! nicm 1104: cmd_find_pane(struct cmd_q *cmdq,
1.12 nicm 1105: const char *arg, struct session **sp, struct window_pane **wpp)
1106: {
1.55 nicm 1107: struct session *s;
1108: struct winlink *wl;
1109: const char *period, *errstr;
1110: char *winptr, *paneptr;
1111: u_int idx;
1.12 nicm 1112:
1113: /* Get the current session. */
1.80 ! nicm 1114: if ((s = cmd_current_session(cmdq, 0)) == NULL) {
! 1115: cmdq_error(cmdq, "can't establish current session");
1.12 nicm 1116: return (NULL);
1117: }
1118: if (sp != NULL)
1119: *sp = s;
1120:
1121: /* A NULL argument means the current session, window and pane. */
1122: if (arg == NULL) {
1123: *wpp = s->curw->window->active;
1124: return (s->curw);
1.51 nicm 1125: }
1126:
1127: /* Lookup as pane id. */
1128: if ((*wpp = cmd_lookup_paneid(arg)) != NULL) {
1.80 ! nicm 1129: s = cmd_window_session(cmdq, (*wpp)->window, &wl);
1.51 nicm 1130: if (sp != NULL)
1131: *sp = s;
1132: return (wl);
1.12 nicm 1133: }
1134:
1135: /* Look for a separating period. */
1136: if ((period = strrchr(arg, '.')) == NULL)
1137: goto no_period;
1138:
1139: /* Pull out the window part and parse it. */
1140: winptr = xstrdup(arg);
1141: winptr[period - arg] = '\0';
1142: if (*winptr == '\0')
1143: wl = s->curw;
1.80 ! nicm 1144: else if ((wl = cmd_find_window(cmdq, winptr, sp)) == NULL)
1.12 nicm 1145: goto error;
1146:
1147: /* Find the pane section and look it up. */
1148: paneptr = winptr + (period - arg) + 1;
1149: if (*paneptr == '\0')
1150: *wpp = wl->window->active;
1.43 nicm 1151: else if (paneptr[0] == '+' || paneptr[0] == '-')
1152: *wpp = cmd_find_pane_offset(paneptr, wl);
1153: else {
1.12 nicm 1154: idx = strtonum(paneptr, 0, INT_MAX, &errstr);
1.36 nicm 1155: if (errstr != NULL)
1156: goto lookup_string;
1.12 nicm 1157: *wpp = window_pane_at_index(wl->window, idx);
1.36 nicm 1158: if (*wpp == NULL)
1159: goto lookup_string;
1.12 nicm 1160: }
1161:
1.68 nicm 1162: free(winptr);
1.12 nicm 1163: return (wl);
1164:
1.36 nicm 1165: lookup_string:
1166: /* Try pane string description. */
1.55 nicm 1167: if ((*wpp = window_find_string(wl->window, paneptr)) == NULL) {
1.80 ! nicm 1168: cmdq_error(cmdq, "can't find pane: %s", paneptr);
1.36 nicm 1169: goto error;
1170: }
1171:
1.68 nicm 1172: free(winptr);
1.39 nicm 1173: return (wl);
1.36 nicm 1174:
1.12 nicm 1175: no_period:
1176: /* Try as a pane number alone. */
1177: idx = strtonum(arg, 0, INT_MAX, &errstr);
1178: if (errstr != NULL)
1179: goto lookup_window;
1180:
1181: /* Try index in the current session and window. */
1182: if ((*wpp = window_pane_at_index(s->curw->window, idx)) == NULL)
1183: goto lookup_window;
1.35 nicm 1184:
1.12 nicm 1185: return (s->curw);
1186:
1187: lookup_window:
1.36 nicm 1188: /* Try pane string description. */
1.55 nicm 1189: if ((*wpp = window_find_string(s->curw->window, arg)) != NULL)
1.36 nicm 1190: return (s->curw);
1191:
1.12 nicm 1192: /* Try as a window and use the active pane. */
1.80 ! nicm 1193: if ((wl = cmd_find_window(cmdq, arg, sp)) != NULL)
1.12 nicm 1194: *wpp = wl->window->active;
1195: return (wl);
1.35 nicm 1196:
1.12 nicm 1197: error:
1.68 nicm 1198: free(winptr);
1.12 nicm 1199: return (NULL);
1.43 nicm 1200: }
1201:
1202: struct window_pane *
1203: cmd_find_pane_offset(const char *paneptr, struct winlink *wl)
1204: {
1205: struct window *w = wl->window;
1206: struct window_pane *wp = w->active;
1207: u_int offset = 1;
1208:
1209: if (paneptr[1] != '\0')
1210: offset = strtonum(paneptr + 1, 1, INT_MAX, NULL);
1211: if (offset > 0) {
1212: if (paneptr[0] == '+')
1213: wp = window_pane_next_by_number(w, wp, offset);
1214: else
1215: wp = window_pane_previous_by_number(w, wp, offset);
1216: }
1217:
1218: return (wp);
1.15 nicm 1219: }
1220:
1221: /* Replace the first %% or %idx in template by s. */
1222: char *
1.75 nicm 1223: cmd_template_replace(const char *template, const char *s, int idx)
1.15 nicm 1224: {
1.75 nicm 1225: char ch, *buf;
1226: const char *ptr;
1227: int replaced;
1228: size_t len;
1.15 nicm 1229:
1.76 nicm 1230: if (strchr(template, '%') == NULL)
1.15 nicm 1231: return (xstrdup(template));
1232:
1233: buf = xmalloc(1);
1234: *buf = '\0';
1235: len = 0;
1236: replaced = 0;
1237:
1238: ptr = template;
1239: while (*ptr != '\0') {
1240: switch (ch = *ptr++) {
1241: case '%':
1242: if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
1243: if (*ptr != '%' || replaced)
1244: break;
1245: replaced = 1;
1246: }
1247: ptr++;
1248:
1249: len += strlen(s);
1250: buf = xrealloc(buf, 1, len + 1);
1251: strlcat(buf, s, len + 1);
1252: continue;
1253: }
1254: buf = xrealloc(buf, 1, len + 2);
1255: buf[len++] = ch;
1256: buf[len] = '\0';
1257: }
1258:
1259: return (buf);
1.57 nicm 1260: }
1261:
1.61 nicm 1262: /*
1263: * Return the default path for a new pane, using the given path or the
1264: * default-path option if it is NULL. Several special values are accepted: the
1265: * empty string or relative path for the current pane's working directory, ~
1266: * for the user's home, - for the session working directory, . for the tmux
1267: * server's working directory. The default on failure is the session's working
1268: * directory.
1269: */
1.59 nicm 1270: const char *
1.80 ! nicm 1271: cmd_get_default_path(struct cmd_q *cmdq, const char *cwd)
1.57 nicm 1272: {
1.80 ! nicm 1273: struct client *c = cmdq->client;
1.57 nicm 1274: struct session *s;
1.58 stsp 1275: struct environ_entry *envent;
1.61 nicm 1276: const char *root;
1277: char tmp[MAXPATHLEN];
1278: struct passwd *pw;
1279: int n;
1280: size_t skip;
1281: static char path[MAXPATHLEN];
1.57 nicm 1282:
1.80 ! nicm 1283: if ((s = cmd_current_session(cmdq, 0)) == NULL)
1.57 nicm 1284: return (NULL);
1285:
1.61 nicm 1286: if (cwd == NULL)
1287: cwd = options_get_string(&s->options, "default-path");
1288:
1289: skip = 1;
1290: if (strcmp(cwd, "$HOME") == 0 || strncmp(cwd, "$HOME/", 6) == 0) {
1291: /* User's home directory - $HOME. */
1292: skip = 5;
1293: goto find_home;
1294: } else if (cwd[0] == '~' && (cwd[1] == '\0' || cwd[1] == '/')) {
1295: /* User's home directory - ~. */
1296: goto find_home;
1297: } else if (cwd[0] == '-' && (cwd[1] == '\0' || cwd[1] == '/')) {
1298: /* Session working directory. */
1299: root = s->cwd;
1300: goto complete_path;
1.66 nicm 1301: } else if (cwd[0] == '.' && (cwd[1] == '\0' || cwd[1] == '/')) {
1.61 nicm 1302: /* Server working directory. */
1303: if (getcwd(tmp, sizeof tmp) != NULL) {
1304: root = tmp;
1305: goto complete_path;
1.57 nicm 1306: }
1307: return (s->cwd);
1.61 nicm 1308: } else if (*cwd == '/') {
1309: /* Absolute path. */
1310: return (cwd);
1311: } else {
1312: /* Empty or relative path. */
1.80 ! nicm 1313: if (c != NULL && c->session == NULL && c->cwd != NULL)
! 1314: root = c->cwd;
! 1315: else if (s->curw != NULL)
1.71 nicm 1316: root = get_proc_cwd(s->curw->window->active->fd);
1.61 nicm 1317: else
1318: return (s->cwd);
1319: skip = 0;
1.62 nicm 1320: if (root != NULL)
1321: goto complete_path;
1.57 nicm 1322: }
1.61 nicm 1323:
1324: return (s->cwd);
1325:
1326: find_home:
1327: envent = environ_find(&global_environ, "HOME");
1328: if (envent != NULL && *envent->value != '\0')
1329: root = envent->value;
1330: else if ((pw = getpwuid(getuid())) != NULL)
1331: root = pw->pw_dir;
1332: else
1333: return (s->cwd);
1334:
1335: complete_path:
1.65 nicm 1336: if (root[skip] == '\0') {
1337: strlcpy(path, root, sizeof path);
1338: return (path);
1339: }
1.61 nicm 1340: n = snprintf(path, sizeof path, "%s/%s", root, cwd + skip);
1341: if (n > 0 && (size_t)n < sizeof path)
1342: return (path);
1343: return (s->cwd);
1.1 nicm 1344: }