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