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