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