Annotation of src/usr.bin/tmux/cmd.c, Revision 1.31
1.31 ! nicm 1: /* $OpenBSD: cmd.c,v 1.30 2009/11/03 20:59:22 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.15 nicm 34: &cmd_choose_client_entry,
1.1 nicm 35: &cmd_choose_session_entry,
36: &cmd_choose_window_entry,
37: &cmd_clear_history_entry,
38: &cmd_clock_mode_entry,
39: &cmd_command_prompt_entry,
40: &cmd_confirm_before_entry,
41: &cmd_copy_buffer_entry,
42: &cmd_copy_mode_entry,
43: &cmd_delete_buffer_entry,
44: &cmd_detach_client_entry,
1.7 nicm 45: &cmd_display_message_entry,
1.16 nicm 46: &cmd_display_panes_entry,
1.1 nicm 47: &cmd_down_pane_entry,
48: &cmd_find_window_entry,
49: &cmd_has_session_entry,
1.4 nicm 50: &cmd_if_shell_entry,
1.1 nicm 51: &cmd_kill_pane_entry,
52: &cmd_kill_server_entry,
53: &cmd_kill_session_entry,
54: &cmd_kill_window_entry,
55: &cmd_last_window_entry,
56: &cmd_link_window_entry,
57: &cmd_list_buffers_entry,
58: &cmd_list_clients_entry,
59: &cmd_list_commands_entry,
60: &cmd_list_keys_entry,
1.23 nicm 61: &cmd_list_panes_entry,
1.1 nicm 62: &cmd_list_sessions_entry,
63: &cmd_list_windows_entry,
64: &cmd_load_buffer_entry,
1.19 nicm 65: &cmd_lock_client_entry,
1.1 nicm 66: &cmd_lock_server_entry,
1.19 nicm 67: &cmd_lock_session_entry,
1.1 nicm 68: &cmd_move_window_entry,
69: &cmd_new_session_entry,
70: &cmd_new_window_entry,
71: &cmd_next_layout_entry,
72: &cmd_next_window_entry,
73: &cmd_paste_buffer_entry,
1.24 nicm 74: &cmd_pipe_pane_entry,
1.1 nicm 75: &cmd_previous_layout_entry,
76: &cmd_previous_window_entry,
77: &cmd_refresh_client_entry,
78: &cmd_rename_session_entry,
79: &cmd_rename_window_entry,
80: &cmd_resize_pane_entry,
81: &cmd_respawn_window_entry,
82: &cmd_rotate_window_entry,
1.17 nicm 83: &cmd_run_shell_entry,
1.1 nicm 84: &cmd_save_buffer_entry,
85: &cmd_select_layout_entry,
86: &cmd_select_pane_entry,
87: &cmd_select_prompt_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.1 nicm 98: &cmd_show_options_entry,
99: &cmd_show_window_options_entry,
100: &cmd_source_file_entry,
101: &cmd_split_window_entry,
102: &cmd_start_server_entry,
103: &cmd_suspend_client_entry,
104: &cmd_swap_pane_entry,
105: &cmd_swap_window_entry,
106: &cmd_switch_client_entry,
107: &cmd_unbind_key_entry,
108: &cmd_unlink_window_entry,
109: &cmd_up_pane_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)
170: return;
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: {
280: if (cmd->entry->print == NULL) {
281: return (xsnprintf(buf, len, "%s", cmd->entry->name));
282: }
283: return (cmd->entry->print(cmd, buf, len));
284: }
285:
1.5 nicm 286: /*
287: * Figure out the current session. Use: 1) the current session, if the command
1.31 ! nicm 288: * context has one; 2) the most recently used session containing the pty of the
! 289: * calling client, if any; 3) the session specified in the TMUX variable from
! 290: * the environment (as passed from the client); 4) the most recently used
! 291: * session from all sessions.
1.5 nicm 292: */
1.1 nicm 293: struct session *
294: cmd_current_session(struct cmd_ctx *ctx)
295: {
296: struct msg_command_data *data = ctx->msgdata;
1.11 nicm 297: struct client *c = ctx->cmdclient;
1.5 nicm 298: struct session *s;
1.11 nicm 299: struct sessions ss;
300: struct winlink *wl;
301: struct window_pane *wp;
302: u_int i;
303: int found;
1.1 nicm 304:
1.14 nicm 305: if (ctx->curclient != NULL && ctx->curclient->session != NULL)
306: return (ctx->curclient->session);
1.1 nicm 307:
1.11 nicm 308: /*
309: * If the name of the calling client's pty is know, build a list of the
310: * sessions that contain it and if any choose either the first or the
311: * newest.
312: */
313: if (c != NULL && c->tty.path != NULL) {
314: ARRAY_INIT(&ss);
315: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
316: if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
317: continue;
318: found = 0;
319: RB_FOREACH(wl, winlinks, &s->windows) {
320: TAILQ_FOREACH(wp, &wl->window->panes, entry) {
321: if (strcmp(wp->tty, c->tty.path) == 0) {
322: found = 1;
323: break;
324: }
325: }
326: if (found)
327: break;
328: }
329: if (found)
330: ARRAY_ADD(&ss, s);
331: }
332:
1.31 ! nicm 333: s = cmd_choose_session(&ss);
1.11 nicm 334: ARRAY_FREE(&ss);
335: if (s != NULL)
336: return (s);
337: }
338:
339: /* Use the session from the TMUX environment variable. */
1.1 nicm 340: if (data != NULL && data->pid != -1) {
1.5 nicm 341: if (data->pid != getpid())
1.1 nicm 342: return (NULL);
1.5 nicm 343: if (data->idx > ARRAY_LENGTH(&sessions))
1.1 nicm 344: return (NULL);
1.5 nicm 345: if ((s = ARRAY_ITEM(&sessions, data->idx)) == NULL)
1.1 nicm 346: return (NULL);
347: return (s);
348: }
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: */
512: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
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;
524: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
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: }
536: return (sfound);
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
542: * idx if the window index is a valid number but there is now window with that
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: }
560:
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: }
586: }
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,
703: * otherwise try to look it up in the session.
704: */
1.8 nicm 705: if (*winptr == '\0')
1.5 nicm 706: wl = s->curw;
707: else if ((wl = cmd_lookup_window(s, winptr, &ambiguous)) == NULL)
708: goto not_found;
709:
710: if (sessptr != NULL)
711: xfree(sessptr);
712: return (wl);
713:
1.8 nicm 714: no_colon:
715: /* No colon in the string, first try as a window then as a session. */
716: if ((wl = cmd_lookup_window(s, arg, &ambiguous)) == NULL) {
717: if (ambiguous)
718: goto not_found;
719: if ((s = cmd_lookup_session(arg, &ambiguous)) == NULL)
720: goto no_session;
721: wl = s->curw;
722: }
723:
724: if (sp != NULL)
725: *sp = s;
726:
727: return (wl);
728:
1.5 nicm 729: no_session:
730: if (ambiguous)
1.8 nicm 731: ctx->error(ctx, "multiple sessions: %s", arg);
1.5 nicm 732: else
1.8 nicm 733: ctx->error(ctx, "session not found: %s", arg);
1.5 nicm 734: if (sessptr != NULL)
735: xfree(sessptr);
736: return (NULL);
737:
738: not_found:
739: if (ambiguous)
740: ctx->error(ctx, "multiple windows: %s", arg);
741: else
742: ctx->error(ctx, "window not found: %s", arg);
743: if (sessptr != NULL)
744: xfree(sessptr);
745: return (NULL);
746: }
1.1 nicm 747:
1.5 nicm 748: /*
749: * Find the target session and window index, whether or not it exists in the
750: * session. Return -2 on error or -1 if no window index is specified. This is
1.8 nicm 751: * used when parsing an argument for a window target that may not exist (for
752: * example if it is going to be created).
1.5 nicm 753: */
754: int
755: cmd_find_index(struct cmd_ctx *ctx, const char *arg, struct session **sp)
756: {
757: struct session *s;
1.8 nicm 758: const char *winptr;
1.5 nicm 759: char *sessptr = NULL;
760: int idx, ambiguous = 0;
761:
762: /*
763: * Find the current session. There must always be a current session, if
764: * it can't be found, report an error.
765: */
766: if ((s = cmd_current_session(ctx)) == NULL) {
767: ctx->error(ctx, "can't establish current session");
1.9 nicm 768: return (-2);
1.1 nicm 769: }
1.5 nicm 770:
771: /* A NULL argument means the current session and "no window" (-1). */
772: if (arg == NULL) {
773: if (sp != NULL)
774: *sp = s;
775: return (-1);
776: }
777:
778: /* Time to look at the argument. If it is empty, that is an error. */
779: if (*arg == '\0')
780: goto not_found;
781:
782: /* Find the separating colon. If none, assume the current session. */
783: winptr = strchr(arg, ':');
784: if (winptr == NULL)
1.8 nicm 785: goto no_colon;
786: winptr++; /* skip : */
787: sessptr = xstrdup(arg);
788: *strchr(sessptr, ':') = '\0';
1.5 nicm 789:
790: /* Try to lookup the session if present. */
791: if (sessptr != NULL && *sessptr != '\0') {
1.8 nicm 792: if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
1.5 nicm 793: goto no_session;
794: }
1.1 nicm 795: if (sp != NULL)
796: *sp = s;
797:
1.5 nicm 798: /*
1.8 nicm 799: * Then work out the window. An empty string is a new window otherwise
800: * try to look it up in the session.
1.5 nicm 801: */
1.8 nicm 802: if (*winptr == '\0')
803: idx = -1;
804: else if ((idx = cmd_lookup_index(s, winptr, &ambiguous)) == -1) {
1.5 nicm 805: if (ambiguous)
806: goto not_found;
1.8 nicm 807: ctx->error(ctx, "invalid index: %s", arg);
808: idx = -2;
809: }
1.5 nicm 810:
811: if (sessptr != NULL)
812: xfree(sessptr);
813: return (idx);
814:
1.8 nicm 815: no_colon:
816: /* No colon in the string, first try as a window then as a session. */
817: if ((idx = cmd_lookup_index(s, arg, &ambiguous)) == -1) {
818: if (ambiguous)
819: goto not_found;
820: if ((s = cmd_lookup_session(arg, &ambiguous)) == NULL)
821: goto no_session;
822: idx = -1;
823: }
824:
825: if (sp != NULL)
826: *sp = s;
827:
828: return (idx);
829:
1.5 nicm 830: no_session:
831: if (ambiguous)
1.8 nicm 832: ctx->error(ctx, "multiple sessions: %s", arg);
1.5 nicm 833: else
1.8 nicm 834: ctx->error(ctx, "session not found: %s", arg);
1.5 nicm 835: if (sessptr != NULL)
836: xfree(sessptr);
837: return (-2);
838:
839: not_found:
840: if (ambiguous)
841: ctx->error(ctx, "multiple windows: %s", arg);
1.1 nicm 842: else
1.5 nicm 843: ctx->error(ctx, "window not found: %s", arg);
844: if (sessptr != NULL)
845: xfree(sessptr);
846: return (-2);
1.12 nicm 847: }
848:
849: /*
850: * Find the target session, window and pane number or report an error and
851: * return NULL. The pane number is separated from the session:window by a .,
852: * such as mysession:mywindow.0.
853: */
854: struct winlink *
855: cmd_find_pane(struct cmd_ctx *ctx,
856: const char *arg, struct session **sp, struct window_pane **wpp)
857: {
858: struct session *s;
859: struct winlink *wl;
860: const char *period;
861: char *winptr, *paneptr;
862: const char *errstr;
863: u_int idx;
864:
865: /* Get the current session. */
866: if ((s = cmd_current_session(ctx)) == NULL) {
1.27 deraadt 867: ctx->error(ctx, "can't establish current session");
1.12 nicm 868: return (NULL);
869: }
870: if (sp != NULL)
871: *sp = s;
872:
873: /* A NULL argument means the current session, window and pane. */
874: if (arg == NULL) {
875: *wpp = s->curw->window->active;
876: return (s->curw);
877: }
878:
879: /* Look for a separating period. */
880: if ((period = strrchr(arg, '.')) == NULL)
881: goto no_period;
882:
883: /* Pull out the window part and parse it. */
884: winptr = xstrdup(arg);
885: winptr[period - arg] = '\0';
886: if (*winptr == '\0')
887: wl = s->curw;
888: else if ((wl = cmd_find_window(ctx, winptr, sp)) == NULL)
889: goto error;
890:
891: /* Find the pane section and look it up. */
892: paneptr = winptr + (period - arg) + 1;
893: if (*paneptr == '\0')
894: *wpp = wl->window->active;
895: else {
896: idx = strtonum(paneptr, 0, INT_MAX, &errstr);
897: if (errstr != NULL) {
898: ctx->error(ctx, "pane %s: %s", errstr, paneptr);
899: goto error;
900: }
901: *wpp = window_pane_at_index(wl->window, idx);
902: if (*wpp == NULL) {
903: ctx->error(ctx, "no such pane: %u", idx);
904: goto error;
905: }
906: }
907:
908: xfree(winptr);
909: return (wl);
910:
911: no_period:
912: /* Try as a pane number alone. */
913: idx = strtonum(arg, 0, INT_MAX, &errstr);
914: if (errstr != NULL)
915: goto lookup_window;
916:
917: /* Try index in the current session and window. */
918: if ((*wpp = window_pane_at_index(s->curw->window, idx)) == NULL)
919: goto lookup_window;
920:
921: return (s->curw);
922:
923: lookup_window:
924: /* Try as a window and use the active pane. */
925: if ((wl = cmd_find_window(ctx, arg, sp)) != NULL)
926: *wpp = wl->window->active;
927: return (wl);
928:
929: error:
930: xfree(winptr);
931: return (NULL);
1.15 nicm 932: }
933:
934: /* Replace the first %% or %idx in template by s. */
935: char *
936: cmd_template_replace(char *template, const char *s, int idx)
937: {
938: char ch;
939: char *buf, *ptr;
940: int replaced;
941: size_t len;
942:
943: if (strstr(template, "%") == NULL)
944: return (xstrdup(template));
945:
946: buf = xmalloc(1);
947: *buf = '\0';
948: len = 0;
949: replaced = 0;
950:
951: ptr = template;
952: while (*ptr != '\0') {
953: switch (ch = *ptr++) {
954: case '%':
955: if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
956: if (*ptr != '%' || replaced)
957: break;
958: replaced = 1;
959: }
960: ptr++;
961:
962: len += strlen(s);
963: buf = xrealloc(buf, 1, len + 1);
964: strlcat(buf, s, len + 1);
965: continue;
966: }
967: buf = xrealloc(buf, 1, len + 2);
968: buf[len++] = ch;
969: buf[len] = '\0';
970: }
971:
972: return (buf);
1.1 nicm 973: }