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