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