Annotation of src/usr.bin/tmux/cmd.c, Revision 1.45
1.45 ! nicm 1: /* $OpenBSD: cmd.c,v 1.44 2010/10/16 07:57:42 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.1 nicm 24: #include <stdlib.h>
25: #include <string.h>
26: #include <unistd.h>
27:
28: #include "tmux.h"
29:
30: const struct cmd_entry *cmd_table[] = {
31: &cmd_attach_session_entry,
32: &cmd_bind_key_entry,
33: &cmd_break_pane_entry,
1.34 nicm 34: &cmd_capture_pane_entry,
1.42 nicm 35: &cmd_choose_buffer_entry,
1.15 nicm 36: &cmd_choose_client_entry,
1.1 nicm 37: &cmd_choose_session_entry,
38: &cmd_choose_window_entry,
39: &cmd_clear_history_entry,
40: &cmd_clock_mode_entry,
41: &cmd_command_prompt_entry,
42: &cmd_confirm_before_entry,
43: &cmd_copy_buffer_entry,
44: &cmd_copy_mode_entry,
45: &cmd_delete_buffer_entry,
46: &cmd_detach_client_entry,
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.1 nicm 71: &cmd_move_window_entry,
72: &cmd_new_session_entry,
73: &cmd_new_window_entry,
74: &cmd_next_layout_entry,
75: &cmd_next_window_entry,
76: &cmd_paste_buffer_entry,
1.24 nicm 77: &cmd_pipe_pane_entry,
1.1 nicm 78: &cmd_previous_layout_entry,
79: &cmd_previous_window_entry,
80: &cmd_refresh_client_entry,
81: &cmd_rename_session_entry,
82: &cmd_rename_window_entry,
83: &cmd_resize_pane_entry,
84: &cmd_respawn_window_entry,
85: &cmd_rotate_window_entry,
1.17 nicm 86: &cmd_run_shell_entry,
1.1 nicm 87: &cmd_save_buffer_entry,
88: &cmd_select_layout_entry,
89: &cmd_select_pane_entry,
90: &cmd_select_window_entry,
91: &cmd_send_keys_entry,
92: &cmd_send_prefix_entry,
93: &cmd_server_info_entry,
94: &cmd_set_buffer_entry,
1.13 nicm 95: &cmd_set_environment_entry,
1.1 nicm 96: &cmd_set_option_entry,
97: &cmd_set_window_option_entry,
98: &cmd_show_buffer_entry,
1.13 nicm 99: &cmd_show_environment_entry,
1.32 nicm 100: &cmd_show_messages_entry,
1.1 nicm 101: &cmd_show_options_entry,
102: &cmd_show_window_options_entry,
103: &cmd_source_file_entry,
104: &cmd_split_window_entry,
105: &cmd_start_server_entry,
106: &cmd_suspend_client_entry,
107: &cmd_swap_pane_entry,
108: &cmd_swap_window_entry,
109: &cmd_switch_client_entry,
110: &cmd_unbind_key_entry,
111: &cmd_unlink_window_entry,
112: NULL
113: };
114:
1.31 nicm 115: struct session *cmd_choose_session(struct sessions *);
116: struct client *cmd_choose_client(struct clients *);
1.5 nicm 117: struct client *cmd_lookup_client(const char *);
118: struct session *cmd_lookup_session(const char *, int *);
119: struct winlink *cmd_lookup_window(struct session *, const char *, int *);
1.8 nicm 120: int cmd_lookup_index(struct session *, const char *, int *);
1.43 nicm 121: struct winlink *cmd_find_window_offset(const char *, struct session *, int *);
122: int cmd_find_index_offset(const char *, struct session *, int *);
123: struct window_pane *cmd_find_pane_offset(const char *, struct winlink *);
1.5 nicm 124:
1.10 nicm 125: int
126: cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
127: {
128: size_t arglen;
129: int i;
130:
131: *buf = '\0';
132: for (i = 0; i < argc; i++) {
133: if (strlcpy(buf, argv[i], len) >= len)
134: return (-1);
135: arglen = strlen(argv[i]) + 1;
136: buf += arglen;
137: len -= arglen;
138: }
139:
140: return (0);
141: }
142:
143: int
144: cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
145: {
146: int i;
147: size_t arglen;
148:
149: if (argc == 0)
150: return (0);
151: *argv = xcalloc(argc, sizeof **argv);
152:
153: buf[len - 1] = '\0';
154: for (i = 0; i < argc; i++) {
155: if (len == 0) {
156: cmd_free_argv(argc, *argv);
157: return (-1);
158: }
159:
160: arglen = strlen(buf) + 1;
161: (*argv)[i] = xstrdup(buf);
162: buf += arglen;
163: len -= arglen;
164: }
165:
166: return (0);
167: }
168:
169: void
170: cmd_free_argv(int argc, char **argv)
171: {
172: int i;
173:
174: if (argc == 0)
1.35 nicm 175: return;
1.10 nicm 176: for (i = 0; i < argc; i++) {
177: if (argv[i] != NULL)
178: xfree(argv[i]);
179: }
180: xfree(argv);
181: }
182:
1.1 nicm 183: struct cmd *
184: cmd_parse(int argc, char **argv, char **cause)
185: {
186: const struct cmd_entry **entryp, *entry;
1.27 deraadt 187: struct cmd *cmd;
1.1 nicm 188: char s[BUFSIZ];
1.3 nicm 189: int opt, ambiguous = 0;
1.1 nicm 190:
191: *cause = NULL;
1.2 nicm 192: if (argc == 0) {
193: xasprintf(cause, "no command");
1.1 nicm 194: return (NULL);
1.2 nicm 195: }
1.1 nicm 196:
197: entry = NULL;
198: for (entryp = cmd_table; *entryp != NULL; entryp++) {
199: if ((*entryp)->alias != NULL &&
200: strcmp((*entryp)->alias, argv[0]) == 0) {
1.3 nicm 201: ambiguous = 0;
1.1 nicm 202: entry = *entryp;
203: break;
204: }
205:
206: if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
207: continue;
208: if (entry != NULL)
1.3 nicm 209: ambiguous = 1;
1.1 nicm 210: entry = *entryp;
211:
212: /* Bail now if an exact match. */
213: if (strcmp(entry->name, argv[0]) == 0)
214: break;
215: }
1.3 nicm 216: if (ambiguous)
217: goto ambiguous;
1.1 nicm 218: if (entry == NULL) {
219: xasprintf(cause, "unknown command: %s", argv[0]);
220: return (NULL);
221: }
222:
223: optreset = 1;
224: optind = 1;
225: if (entry->parse == NULL) {
226: while ((opt = getopt(argc, argv, "")) != -1) {
227: switch (opt) {
228: default:
229: goto usage;
230: }
231: }
232: argc -= optind;
233: argv += optind;
234: if (argc != 0)
235: goto usage;
236: }
237:
238: cmd = xmalloc(sizeof *cmd);
239: cmd->entry = entry;
240: cmd->data = NULL;
241: if (entry->parse != NULL) {
242: if (entry->parse(cmd, argc, argv, cause) != 0) {
243: xfree(cmd);
244: return (NULL);
245: }
246: }
247: return (cmd);
248:
249: ambiguous:
250: *s = '\0';
251: for (entryp = cmd_table; *entryp != NULL; entryp++) {
252: if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
253: continue;
254: if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s)
255: break;
256: if (strlcat(s, ", ", sizeof s) >= sizeof s)
257: break;
258: }
259: s[strlen(s) - 2] = '\0';
260: xasprintf(cause, "ambiguous command: %s, could be: %s", argv[0], s);
261: return (NULL);
262:
263: usage:
264: xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
265: return (NULL);
266: }
267:
268: int
269: cmd_exec(struct cmd *cmd, struct cmd_ctx *ctx)
270: {
271: return (cmd->entry->exec(cmd, ctx));
272: }
273:
274: void
275: cmd_free(struct cmd *cmd)
276: {
277: if (cmd->data != NULL && cmd->entry->free != NULL)
278: cmd->entry->free(cmd);
279: xfree(cmd);
280: }
281:
282: size_t
283: cmd_print(struct cmd *cmd, char *buf, size_t len)
284: {
1.33 nicm 285: if (cmd->entry->print == NULL)
1.1 nicm 286: return (xsnprintf(buf, len, "%s", cmd->entry->name));
287: return (cmd->entry->print(cmd, buf, len));
288: }
289:
1.5 nicm 290: /*
291: * Figure out the current session. Use: 1) the current session, if the command
1.31 nicm 292: * context has one; 2) the most recently used session containing the pty of the
293: * calling client, if any; 3) the session specified in the TMUX variable from
294: * the environment (as passed from the client); 4) the most recently used
295: * session from all sessions.
1.5 nicm 296: */
1.1 nicm 297: struct session *
298: cmd_current_session(struct cmd_ctx *ctx)
299: {
300: struct msg_command_data *data = ctx->msgdata;
1.11 nicm 301: struct client *c = ctx->cmdclient;
1.5 nicm 302: struct session *s;
1.11 nicm 303: struct sessions ss;
304: struct winlink *wl;
305: struct window_pane *wp;
306: u_int i;
307: int found;
1.1 nicm 308:
1.14 nicm 309: if (ctx->curclient != NULL && ctx->curclient->session != NULL)
310: return (ctx->curclient->session);
1.1 nicm 311:
1.11 nicm 312: /*
313: * If the name of the calling client's pty is know, build a list of the
314: * sessions that contain it and if any choose either the first or the
315: * newest.
316: */
317: if (c != NULL && c->tty.path != NULL) {
318: ARRAY_INIT(&ss);
319: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
320: if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
321: continue;
322: found = 0;
323: RB_FOREACH(wl, winlinks, &s->windows) {
324: TAILQ_FOREACH(wp, &wl->window->panes, entry) {
325: if (strcmp(wp->tty, c->tty.path) == 0) {
326: found = 1;
327: break;
328: }
329: }
330: if (found)
331: break;
332: }
333: if (found)
334: ARRAY_ADD(&ss, s);
335: }
336:
1.31 nicm 337: s = cmd_choose_session(&ss);
1.11 nicm 338: ARRAY_FREE(&ss);
339: if (s != NULL)
340: return (s);
341: }
342:
343: /* Use the session from the TMUX environment variable. */
1.44 nicm 344: if (data != NULL &&
345: data->pid == getpid() &&
346: data->idx <= ARRAY_LENGTH(&sessions) &&
347: (s = ARRAY_ITEM(&sessions, data->idx)) != NULL)
1.1 nicm 348: return (s);
349:
1.31 nicm 350: return (cmd_choose_session(&sessions));
1.5 nicm 351: }
352:
1.31 nicm 353: /* Find the most recently used session from a list. */
1.5 nicm 354: struct session *
1.31 nicm 355: cmd_choose_session(struct sessions *ss)
1.5 nicm 356: {
1.31 nicm 357: struct session *s, *sbest;
1.5 nicm 358: struct timeval *tv = NULL;
359: u_int i;
360:
1.31 nicm 361: sbest = NULL;
1.11 nicm 362: for (i = 0; i < ARRAY_LENGTH(ss); i++) {
363: if ((s = ARRAY_ITEM(ss, i)) == NULL)
1.5 nicm 364: continue;
365:
1.31 nicm 366: if (tv == NULL || timercmp(&s->activity_time, tv, >)) {
367: sbest = s;
368: tv = &s->activity_time;
1.1 nicm 369: }
370: }
1.5 nicm 371:
1.31 nicm 372: return (sbest);
1.1 nicm 373: }
374:
1.30 nicm 375: /*
376: * Find the current client. First try the current client if set, then pick the
1.31 nicm 377: * most recently used of the clients attached to the current session if any,
378: * then of all clients.
1.30 nicm 379: */
380: struct client *
381: cmd_current_client(struct cmd_ctx *ctx)
382: {
383: struct session *s;
384: struct client *c;
385: struct clients cc;
386: u_int i;
387:
388: if (ctx->curclient != NULL)
389: return (ctx->curclient);
390:
391: /*
392: * No current client set. Find the current session and return the
393: * newest of its clients.
394: */
395: s = cmd_current_session(ctx);
396: if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
397: ARRAY_INIT(&cc);
398: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
399: if ((c = ARRAY_ITEM(&clients, i)) == NULL)
400: continue;
401: if (s == c->session)
402: ARRAY_ADD(&cc, c);
403: }
404:
1.31 nicm 405: c = cmd_choose_client(&cc);
1.30 nicm 406: ARRAY_FREE(&cc);
407: if (c != NULL)
408: return (c);
409: }
410:
1.31 nicm 411: return (cmd_choose_client(&clients));
1.30 nicm 412: }
413:
1.31 nicm 414: /* Choose the most recently used client from a list. */
1.20 nicm 415: struct client *
1.31 nicm 416: cmd_choose_client(struct clients *cc)
1.20 nicm 417: {
1.31 nicm 418: struct client *c, *cbest;
1.20 nicm 419: struct timeval *tv = NULL;
420: u_int i;
421:
1.31 nicm 422: cbest = NULL;
1.30 nicm 423: for (i = 0; i < ARRAY_LENGTH(cc); i++) {
424: if ((c = ARRAY_ITEM(cc, i)) == NULL)
1.20 nicm 425: continue;
426: if (c->session == NULL)
427: continue;
428:
1.31 nicm 429: if (tv == NULL || timercmp(&c->activity_time, tv, >)) {
430: cbest = c;
431: tv = &c->activity_time;
1.20 nicm 432: }
433: }
434:
1.31 nicm 435: return (cbest);
1.20 nicm 436: }
437:
1.5 nicm 438: /* Find the target client or report an error and return NULL. */
1.1 nicm 439: struct client *
440: cmd_find_client(struct cmd_ctx *ctx, const char *arg)
441: {
1.30 nicm 442: struct client *c;
1.5 nicm 443: char *tmparg;
444: size_t arglen;
1.1 nicm 445:
1.5 nicm 446: /* A NULL argument means the current client. */
1.30 nicm 447: if (arg == NULL)
448: return (cmd_current_client(ctx));
1.5 nicm 449: tmparg = xstrdup(arg);
450:
451: /* Trim a single trailing colon if any. */
452: arglen = strlen(tmparg);
453: if (arglen != 0 && tmparg[arglen - 1] == ':')
454: tmparg[arglen - 1] = '\0';
455:
456: /* Find the client, if any. */
457: c = cmd_lookup_client(tmparg);
458:
459: /* If no client found, report an error. */
460: if (c == NULL)
461: ctx->error(ctx, "client not found: %s", tmparg);
462:
463: xfree(tmparg);
464: return (c);
465: }
466:
467: /*
468: * Lookup a client by device path. Either of a full match and a match without a
469: * leading _PATH_DEV ("/dev/") is accepted.
470: */
471: struct client *
472: cmd_lookup_client(const char *name)
473: {
474: struct client *c;
475: const char *path;
476: u_int i;
477:
478: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1.25 nicm 479: c = ARRAY_ITEM(&clients, i);
480: if (c == NULL || c->session == NULL)
1.5 nicm 481: continue;
482: path = c->tty.path;
483:
484: /* Check for exact matches. */
485: if (strcmp(name, path) == 0)
486: return (c);
487:
488: /* Check without leading /dev if present. */
489: if (strncmp(path, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0)
490: continue;
491: if (strcmp(name, path + (sizeof _PATH_DEV) - 1) == 0)
492: return (c);
493: }
494:
495: return (NULL);
496: }
497:
498: /* Lookup a session by name. If no session is found, NULL is returned. */
499: struct session *
500: cmd_lookup_session(const char *name, int *ambiguous)
501: {
502: struct session *s, *sfound;
503: u_int i;
504:
505: *ambiguous = 0;
506:
507: /*
1.28 nicm 508: * Look for matches. First look for exact matches - session names must
509: * be unique so an exact match can't be ambigious and can just be
510: * returned.
1.5 nicm 511: */
1.35 nicm 512: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1.5 nicm 513: if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
514: continue;
515: if (strcmp(name, s->name) == 0)
516: return (s);
1.28 nicm 517: }
518:
519: /*
520: * Otherwise look for partial matches, returning early if it is found to
521: * be ambiguous.
522: */
523: sfound = NULL;
1.35 nicm 524: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1.28 nicm 525: if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
526: continue;
1.5 nicm 527: if (strncmp(name, s->name, strlen(name)) == 0 ||
528: fnmatch(name, s->name, 0) == 0) {
529: if (sfound != NULL) {
530: *ambiguous = 1;
531: return (NULL);
532: }
533: sfound = s;
534: }
535: }
1.35 nicm 536: return (sfound);
1.5 nicm 537: }
538:
539: /*
1.8 nicm 540: * Lookup a window or return -1 if not found or ambigious. First try as an
541: * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
1.41 nicm 542: * idx if the window index is a valid number but there is no window with that
1.8 nicm 543: * index.
1.5 nicm 544: */
545: struct winlink *
546: cmd_lookup_window(struct session *s, const char *name, int *ambiguous)
547: {
548: struct winlink *wl, *wlfound;
549: const char *errstr;
550: u_int idx;
551:
552: *ambiguous = 0;
553:
554: /* First see if this is a valid window index in this session. */
555: idx = strtonum(name, 0, INT_MAX, &errstr);
556: if (errstr == NULL) {
557: if ((wl = winlink_find_by_index(&s->windows, idx)) != NULL)
558: return (wl);
559: }
1.35 nicm 560:
1.5 nicm 561: /* Look for exact matches, error if more than one. */
562: wlfound = NULL;
563: RB_FOREACH(wl, winlinks, &s->windows) {
1.8 nicm 564: if (strcmp(name, wl->window->name) == 0) {
1.5 nicm 565: if (wlfound != NULL) {
566: *ambiguous = 1;
567: return (NULL);
568: }
569: wlfound = wl;
1.1 nicm 570: }
571: }
1.5 nicm 572: if (wlfound != NULL)
573: return (wlfound);
574:
575: /* Now look for pattern matches, again error if multiple. */
576: wlfound = NULL;
577: RB_FOREACH(wl, winlinks, &s->windows) {
1.8 nicm 578: if (strncmp(name, wl->window->name, strlen(name)) == 0 ||
579: fnmatch(name, wl->window->name, 0) == 0) {
1.5 nicm 580: if (wlfound != NULL) {
581: *ambiguous = 1;
582: return (NULL);
583: }
584: wlfound = wl;
585: }
1.35 nicm 586: }
1.5 nicm 587: if (wlfound != NULL)
588: return (wlfound);
589:
590: return (NULL);
1.1 nicm 591: }
592:
1.8 nicm 593: /*
594: * Find a window index - if the window doesn't exist, check if it is a
595: * potential index and return it anyway.
596: */
597: int
598: cmd_lookup_index(struct session *s, const char *name, int *ambiguous)
599: {
600: struct winlink *wl;
601: const char *errstr;
602: u_int idx;
603:
604: if ((wl = cmd_lookup_window(s, name, ambiguous)) != NULL)
605: return (wl->idx);
606: if (*ambiguous)
607: return (-1);
608:
609: idx = strtonum(name, 0, INT_MAX, &errstr);
610: if (errstr == NULL)
611: return (idx);
612:
613: return (-1);
614: }
615:
1.5 nicm 616: /* Find the target session or report an error and return NULL. */
1.1 nicm 617: struct session *
618: cmd_find_session(struct cmd_ctx *ctx, const char *arg)
619: {
620: struct session *s;
1.5 nicm 621: struct client *c;
622: char *tmparg;
623: size_t arglen;
624: int ambiguous;
1.1 nicm 625:
1.5 nicm 626: /* A NULL argument means the current session. */
1.1 nicm 627: if (arg == NULL)
1.5 nicm 628: return (cmd_current_session(ctx));
629: tmparg = xstrdup(arg);
630:
631: /* Trim a single trailing colon if any. */
632: arglen = strlen(tmparg);
633: if (arglen != 0 && tmparg[arglen - 1] == ':')
634: tmparg[arglen - 1] = '\0';
635:
636: /* Find the session, if any. */
637: s = cmd_lookup_session(tmparg, &ambiguous);
638:
639: /* If it doesn't, try to match it as a client. */
640: if (s == NULL && (c = cmd_lookup_client(tmparg)) != NULL)
641: s = c->session;
642:
643: /* If no session found, report an error. */
644: if (s == NULL) {
645: if (ambiguous)
646: ctx->error(ctx, "more than one session: %s", tmparg);
647: else
648: ctx->error(ctx, "session not found: %s", tmparg);
1.1 nicm 649: }
1.5 nicm 650:
651: xfree(tmparg);
1.1 nicm 652: return (s);
653: }
654:
1.5 nicm 655: /* Find the target session and window or report an error and return NULL. */
1.1 nicm 656: struct winlink *
657: cmd_find_window(struct cmd_ctx *ctx, const char *arg, struct session **sp)
658: {
659: struct session *s;
660: struct winlink *wl;
1.5 nicm 661: const char *winptr;
662: char *sessptr = NULL;
663: int ambiguous = 0;
664:
665: /*
666: * Find the current session. There must always be a current session, if
667: * it can't be found, report an error.
668: */
669: if ((s = cmd_current_session(ctx)) == NULL) {
670: ctx->error(ctx, "can't establish current session");
671: return (NULL);
672: }
673:
674: /* A NULL argument means the current session and window. */
675: if (arg == NULL) {
676: if (sp != NULL)
677: *sp = s;
678: return (s->curw);
679: }
680:
681: /* Time to look at the argument. If it is empty, that is an error. */
682: if (*arg == '\0')
683: goto not_found;
684:
1.8 nicm 685: /* Find the separating colon and split into window and session. */
1.5 nicm 686: winptr = strchr(arg, ':');
687: if (winptr == NULL)
1.8 nicm 688: goto no_colon;
689: winptr++; /* skip : */
690: sessptr = xstrdup(arg);
691: *strchr(sessptr, ':') = '\0';
1.5 nicm 692:
693: /* Try to lookup the session if present. */
1.8 nicm 694: if (*sessptr != '\0') {
695: if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
1.5 nicm 696: goto no_session;
697: }
698: if (sp != NULL)
699: *sp = s;
700:
701: /*
702: * Then work out the window. An empty string is the current window,
1.38 nicm 703: * otherwise try special cases then to look it up in the session.
1.5 nicm 704: */
1.8 nicm 705: if (*winptr == '\0')
1.5 nicm 706: wl = s->curw;
1.38 nicm 707: else if (winptr[0] == '!' && winptr[1] == '\0')
708: wl = TAILQ_FIRST(&s->lastw);
1.43 nicm 709: else if (winptr[0] == '+' || winptr[0] == '-')
710: wl = cmd_find_window_offset(winptr, s, &ambiguous);
711: else
1.38 nicm 712: wl = cmd_lookup_window(s, winptr, &ambiguous);
713: if (wl == NULL)
1.5 nicm 714: goto not_found;
1.35 nicm 715:
1.5 nicm 716: if (sessptr != NULL)
717: xfree(sessptr);
718: return (wl);
719:
1.8 nicm 720: no_colon:
1.38 nicm 721: /*
722: * No colon in the string, first try special cases, then as a window
723: * and lastly as a session.
724: */
725: if (arg[0] == '!' && arg[1] == '\0') {
726: if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
727: goto not_found;
1.41 nicm 728: } else if (arg[0] == '+' || arg[0] == '-') {
1.43 nicm 729: if ((wl = cmd_find_window_offset(arg, s, &ambiguous)) == NULL)
1.41 nicm 730: goto lookup_session;
731: } else if ((wl = cmd_lookup_window(s, arg, &ambiguous)) == NULL)
732: goto lookup_session;
1.8 nicm 733:
734: if (sp != NULL)
735: *sp = s;
736:
737: return (wl);
738:
1.41 nicm 739: lookup_session:
740: if (ambiguous)
741: goto not_found;
742: if ((s = cmd_lookup_session(arg, &ambiguous)) == NULL)
743: goto no_session;
744:
745: if (sp != NULL)
746: *sp = s;
747:
748: return (s->curw);
749:
1.5 nicm 750: no_session:
751: if (ambiguous)
1.8 nicm 752: ctx->error(ctx, "multiple sessions: %s", arg);
1.5 nicm 753: else
1.8 nicm 754: ctx->error(ctx, "session not found: %s", arg);
1.5 nicm 755: if (sessptr != NULL)
756: xfree(sessptr);
757: return (NULL);
758:
759: not_found:
760: if (ambiguous)
761: ctx->error(ctx, "multiple windows: %s", arg);
762: else
763: ctx->error(ctx, "window not found: %s", arg);
764: if (sessptr != NULL)
765: xfree(sessptr);
766: return (NULL);
767: }
1.1 nicm 768:
1.43 nicm 769: struct winlink *
770: cmd_find_window_offset(const char *winptr, struct session *s, int *ambiguous)
771: {
772: struct winlink *wl;
773: int offset = 1;
774:
775: if (winptr[1] != '\0')
776: offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
777: if (offset == 0)
778: wl = cmd_lookup_window(s, winptr, ambiguous);
779: else {
780: if (winptr[0] == '+')
781: wl = winlink_next_by_number(s->curw, s, offset);
782: else
783: wl = winlink_previous_by_number(s->curw, s, offset);
784: }
785:
786: return (wl);
787: }
788:
1.5 nicm 789: /*
790: * Find the target session and window index, whether or not it exists in the
791: * session. Return -2 on error or -1 if no window index is specified. This is
1.8 nicm 792: * used when parsing an argument for a window target that may not exist (for
793: * example if it is going to be created).
1.5 nicm 794: */
795: int
796: cmd_find_index(struct cmd_ctx *ctx, const char *arg, struct session **sp)
797: {
798: struct session *s;
1.38 nicm 799: struct winlink *wl;
1.8 nicm 800: const char *winptr;
1.5 nicm 801: char *sessptr = NULL;
802: int idx, ambiguous = 0;
803:
804: /*
805: * Find the current session. There must always be a current session, if
806: * it can't be found, report an error.
807: */
808: if ((s = cmd_current_session(ctx)) == NULL) {
809: ctx->error(ctx, "can't establish current session");
1.9 nicm 810: return (-2);
1.1 nicm 811: }
1.5 nicm 812:
813: /* A NULL argument means the current session and "no window" (-1). */
814: if (arg == NULL) {
815: if (sp != NULL)
816: *sp = s;
817: return (-1);
818: }
819:
820: /* Time to look at the argument. If it is empty, that is an error. */
821: if (*arg == '\0')
822: goto not_found;
823:
824: /* Find the separating colon. If none, assume the current session. */
825: winptr = strchr(arg, ':');
826: if (winptr == NULL)
1.8 nicm 827: goto no_colon;
828: winptr++; /* skip : */
829: sessptr = xstrdup(arg);
830: *strchr(sessptr, ':') = '\0';
1.5 nicm 831:
832: /* Try to lookup the session if present. */
833: if (sessptr != NULL && *sessptr != '\0') {
1.8 nicm 834: if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
1.5 nicm 835: goto no_session;
836: }
1.1 nicm 837: if (sp != NULL)
838: *sp = s;
839:
1.5 nicm 840: /*
1.8 nicm 841: * Then work out the window. An empty string is a new window otherwise
842: * try to look it up in the session.
1.5 nicm 843: */
1.8 nicm 844: if (*winptr == '\0')
1.38 nicm 845: idx = -1;
846: else if (winptr[0] == '!' && winptr[1] == '\0') {
847: if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
848: goto not_found;
849: idx = wl->idx;
1.41 nicm 850: } else if (winptr[0] == '+' || winptr[0] == '-') {
1.43 nicm 851: if ((idx = cmd_find_index_offset(winptr, s, &ambiguous)) < 0)
1.41 nicm 852: goto invalid_index;
853: } else if ((idx = cmd_lookup_index(s, winptr, &ambiguous)) == -1)
854: goto invalid_index;
1.35 nicm 855:
1.5 nicm 856: if (sessptr != NULL)
857: xfree(sessptr);
858: return (idx);
859:
1.8 nicm 860: no_colon:
1.38 nicm 861: /*
862: * No colon in the string, first try special cases, then as a window
863: * and lastly as a session.
864: */
865: if (arg[0] == '!' && arg[1] == '\0') {
866: if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
867: goto not_found;
868: idx = wl->idx;
1.41 nicm 869: } else if (arg[0] == '+' || arg[0] == '-') {
1.43 nicm 870: if ((idx = cmd_find_index_offset(arg, s, &ambiguous)) < 0)
1.41 nicm 871: goto lookup_session;
872: } else if ((idx = cmd_lookup_index(s, arg, &ambiguous)) == -1)
873: goto lookup_session;
1.8 nicm 874:
875: if (sp != NULL)
876: *sp = s;
877:
878: return (idx);
879:
1.41 nicm 880: lookup_session:
881: if (ambiguous)
882: goto not_found;
883: if ((s = cmd_lookup_session(arg, &ambiguous)) == NULL)
884: goto no_session;
885:
886: if (sp != NULL)
887: *sp = s;
888:
889: return (-1);
890:
1.5 nicm 891: no_session:
892: if (ambiguous)
1.35 nicm 893: ctx->error(ctx, "multiple sessions: %s", arg);
1.5 nicm 894: else
1.8 nicm 895: ctx->error(ctx, "session not found: %s", arg);
1.5 nicm 896: if (sessptr != NULL)
897: xfree(sessptr);
898: return (-2);
899:
1.41 nicm 900: invalid_index:
901: if (ambiguous)
902: goto not_found;
903: ctx->error(ctx, "invalid index: %s", arg);
904:
905: if (sessptr != NULL)
906: xfree(sessptr);
907: return (-2);
908:
1.5 nicm 909: not_found:
910: if (ambiguous)
911: ctx->error(ctx, "multiple windows: %s", arg);
1.1 nicm 912: else
1.5 nicm 913: ctx->error(ctx, "window not found: %s", arg);
914: if (sessptr != NULL)
915: xfree(sessptr);
916: return (-2);
1.12 nicm 917: }
918:
1.43 nicm 919: int
920: cmd_find_index_offset(const char *winptr, struct session *s, int *ambiguous)
921: {
922: int idx, offset = 1;
923:
924: if (winptr[1] != '\0')
925: offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
926: if (offset == 0)
927: idx = cmd_lookup_index(s, winptr, ambiguous);
928: else {
929: if (winptr[0] == '+') {
930: if (s->curw->idx == INT_MAX)
931: idx = cmd_lookup_index(s, winptr, ambiguous);
932: else
933: idx = s->curw->idx + offset;
934: } else {
935: if (s->curw->idx == 0)
936: idx = cmd_lookup_index(s, winptr, ambiguous);
937: else
938: idx = s->curw->idx - offset;
939: }
940: }
941:
942: return (idx);
943: }
944:
1.12 nicm 945: /*
946: * Find the target session, window and pane number or report an error and
947: * return NULL. The pane number is separated from the session:window by a .,
948: * such as mysession:mywindow.0.
949: */
950: struct winlink *
951: cmd_find_pane(struct cmd_ctx *ctx,
952: const char *arg, struct session **sp, struct window_pane **wpp)
953: {
1.36 nicm 954: struct session *s;
955: struct winlink *wl;
956: struct layout_cell *lc;
957: const char *period, *errstr;
958: char *winptr, *paneptr;
1.43 nicm 959: u_int idx;
1.12 nicm 960:
961: /* Get the current session. */
962: if ((s = cmd_current_session(ctx)) == NULL) {
1.27 deraadt 963: ctx->error(ctx, "can't establish current session");
1.12 nicm 964: return (NULL);
965: }
966: if (sp != NULL)
967: *sp = s;
968:
969: /* A NULL argument means the current session, window and pane. */
970: if (arg == NULL) {
971: *wpp = s->curw->window->active;
972: return (s->curw);
973: }
974:
975: /* Look for a separating period. */
976: if ((period = strrchr(arg, '.')) == NULL)
977: goto no_period;
978:
979: /* Pull out the window part and parse it. */
980: winptr = xstrdup(arg);
981: winptr[period - arg] = '\0';
982: if (*winptr == '\0')
983: wl = s->curw;
984: else if ((wl = cmd_find_window(ctx, winptr, sp)) == NULL)
985: goto error;
986:
987: /* Find the pane section and look it up. */
988: paneptr = winptr + (period - arg) + 1;
989: if (*paneptr == '\0')
990: *wpp = wl->window->active;
1.43 nicm 991: else if (paneptr[0] == '+' || paneptr[0] == '-')
992: *wpp = cmd_find_pane_offset(paneptr, wl);
993: else {
1.12 nicm 994: idx = strtonum(paneptr, 0, INT_MAX, &errstr);
1.36 nicm 995: if (errstr != NULL)
996: goto lookup_string;
1.12 nicm 997: *wpp = window_pane_at_index(wl->window, idx);
1.36 nicm 998: if (*wpp == NULL)
999: goto lookup_string;
1.12 nicm 1000: }
1001:
1002: xfree(winptr);
1003: return (wl);
1004:
1.36 nicm 1005: lookup_string:
1006: /* Try pane string description. */
1.39 nicm 1007: if ((lc = layout_find_string(wl->window, paneptr)) == NULL) {
1.36 nicm 1008: ctx->error(ctx, "can't find pane: %s", paneptr);
1009: goto error;
1010: }
1011: *wpp = lc->wp;
1012:
1013: xfree(winptr);
1.39 nicm 1014: return (wl);
1.36 nicm 1015:
1.12 nicm 1016: no_period:
1017: /* Try as a pane number alone. */
1018: idx = strtonum(arg, 0, INT_MAX, &errstr);
1019: if (errstr != NULL)
1020: goto lookup_window;
1021:
1022: /* Try index in the current session and window. */
1023: if ((*wpp = window_pane_at_index(s->curw->window, idx)) == NULL)
1024: goto lookup_window;
1.35 nicm 1025:
1.12 nicm 1026: return (s->curw);
1027:
1028: lookup_window:
1.36 nicm 1029: /* Try pane string description. */
1030: if ((lc = layout_find_string(s->curw->window, arg)) != NULL) {
1031: *wpp = lc->wp;
1032: return (s->curw);
1033: }
1034:
1.12 nicm 1035: /* Try as a window and use the active pane. */
1036: if ((wl = cmd_find_window(ctx, arg, sp)) != NULL)
1037: *wpp = wl->window->active;
1038: return (wl);
1.35 nicm 1039:
1.12 nicm 1040: error:
1041: xfree(winptr);
1042: return (NULL);
1.43 nicm 1043: }
1044:
1045: struct window_pane *
1046: cmd_find_pane_offset(const char *paneptr, struct winlink *wl)
1047: {
1048: struct window *w = wl->window;
1049: struct window_pane *wp = w->active;
1050: u_int offset = 1;
1051:
1052: if (paneptr[1] != '\0')
1053: offset = strtonum(paneptr + 1, 1, INT_MAX, NULL);
1054: if (offset > 0) {
1055: if (paneptr[0] == '+')
1056: wp = window_pane_next_by_number(w, wp, offset);
1057: else
1058: wp = window_pane_previous_by_number(w, wp, offset);
1059: }
1060:
1061: return (wp);
1.15 nicm 1062: }
1063:
1064: /* Replace the first %% or %idx in template by s. */
1065: char *
1066: cmd_template_replace(char *template, const char *s, int idx)
1067: {
1068: char ch;
1069: char *buf, *ptr;
1070: int replaced;
1071: size_t len;
1072:
1073: if (strstr(template, "%") == NULL)
1074: return (xstrdup(template));
1075:
1076: buf = xmalloc(1);
1077: *buf = '\0';
1078: len = 0;
1079: replaced = 0;
1080:
1081: ptr = template;
1082: while (*ptr != '\0') {
1083: switch (ch = *ptr++) {
1084: case '%':
1085: if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
1086: if (*ptr != '%' || replaced)
1087: break;
1088: replaced = 1;
1089: }
1090: ptr++;
1091:
1092: len += strlen(s);
1093: buf = xrealloc(buf, 1, len + 1);
1094: strlcat(buf, s, len + 1);
1095: continue;
1096: }
1097: buf = xrealloc(buf, 1, len + 2);
1098: buf[len++] = ch;
1099: buf[len] = '\0';
1100: }
1101:
1102: return (buf);
1.1 nicm 1103: }