Annotation of src/usr.bin/tmux/cmd.c, Revision 1.157
1.157 ! nicm 1: /* $OpenBSD: cmd.c,v 1.156 2020/01/04 18:01:56 nicm Exp $ */
1.1 nicm 2:
3: /*
1.120 nicm 4: * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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.61 nicm 24: #include <pwd.h>
1.1 nicm 25: #include <stdlib.h>
26: #include <string.h>
27: #include <unistd.h>
28:
29: #include "tmux.h"
1.105 nicm 30:
31: extern const struct cmd_entry cmd_attach_session_entry;
32: extern const struct cmd_entry cmd_bind_key_entry;
33: extern const struct cmd_entry cmd_break_pane_entry;
34: extern const struct cmd_entry cmd_capture_pane_entry;
35: extern const struct cmd_entry cmd_choose_buffer_entry;
36: extern const struct cmd_entry cmd_choose_client_entry;
37: extern const struct cmd_entry cmd_choose_tree_entry;
38: extern const struct cmd_entry cmd_clear_history_entry;
39: extern const struct cmd_entry cmd_clock_mode_entry;
40: extern const struct cmd_entry cmd_command_prompt_entry;
41: extern const struct cmd_entry cmd_confirm_before_entry;
42: extern const struct cmd_entry cmd_copy_mode_entry;
43: extern const struct cmd_entry cmd_delete_buffer_entry;
44: extern const struct cmd_entry cmd_detach_client_entry;
1.148 nicm 45: extern const struct cmd_entry cmd_display_menu_entry;
1.105 nicm 46: extern const struct cmd_entry cmd_display_message_entry;
1.157 ! nicm 47: extern const struct cmd_entry cmd_display_popup_entry;
1.105 nicm 48: extern const struct cmd_entry cmd_display_panes_entry;
49: extern const struct cmd_entry cmd_down_pane_entry;
50: extern const struct cmd_entry cmd_find_window_entry;
51: extern const struct cmd_entry cmd_has_session_entry;
52: extern const struct cmd_entry cmd_if_shell_entry;
53: extern const struct cmd_entry cmd_join_pane_entry;
54: extern const struct cmd_entry cmd_kill_pane_entry;
55: extern const struct cmd_entry cmd_kill_server_entry;
56: extern const struct cmd_entry cmd_kill_session_entry;
57: extern const struct cmd_entry cmd_kill_window_entry;
58: extern const struct cmd_entry cmd_last_pane_entry;
59: extern const struct cmd_entry cmd_last_window_entry;
60: extern const struct cmd_entry cmd_link_window_entry;
61: extern const struct cmd_entry cmd_list_buffers_entry;
62: extern const struct cmd_entry cmd_list_clients_entry;
63: extern const struct cmd_entry cmd_list_commands_entry;
64: extern const struct cmd_entry cmd_list_keys_entry;
65: extern const struct cmd_entry cmd_list_panes_entry;
66: extern const struct cmd_entry cmd_list_sessions_entry;
67: extern const struct cmd_entry cmd_list_windows_entry;
68: extern const struct cmd_entry cmd_load_buffer_entry;
69: extern const struct cmd_entry cmd_lock_client_entry;
70: extern const struct cmd_entry cmd_lock_server_entry;
71: extern const struct cmd_entry cmd_lock_session_entry;
72: extern const struct cmd_entry cmd_move_pane_entry;
73: extern const struct cmd_entry cmd_move_window_entry;
74: extern const struct cmd_entry cmd_new_session_entry;
75: extern const struct cmd_entry cmd_new_window_entry;
76: extern const struct cmd_entry cmd_next_layout_entry;
77: extern const struct cmd_entry cmd_next_window_entry;
78: extern const struct cmd_entry cmd_paste_buffer_entry;
79: extern const struct cmd_entry cmd_pipe_pane_entry;
80: extern const struct cmd_entry cmd_previous_layout_entry;
81: extern const struct cmd_entry cmd_previous_window_entry;
82: extern const struct cmd_entry cmd_refresh_client_entry;
83: extern const struct cmd_entry cmd_rename_session_entry;
84: extern const struct cmd_entry cmd_rename_window_entry;
85: extern const struct cmd_entry cmd_resize_pane_entry;
1.141 nicm 86: extern const struct cmd_entry cmd_resize_window_entry;
1.105 nicm 87: extern const struct cmd_entry cmd_respawn_pane_entry;
88: extern const struct cmd_entry cmd_respawn_window_entry;
89: extern const struct cmd_entry cmd_rotate_window_entry;
90: extern const struct cmd_entry cmd_run_shell_entry;
91: extern const struct cmd_entry cmd_save_buffer_entry;
92: extern const struct cmd_entry cmd_select_layout_entry;
93: extern const struct cmd_entry cmd_select_pane_entry;
94: extern const struct cmd_entry cmd_select_window_entry;
95: extern const struct cmd_entry cmd_send_keys_entry;
96: extern const struct cmd_entry cmd_send_prefix_entry;
97: extern const struct cmd_entry cmd_set_buffer_entry;
98: extern const struct cmd_entry cmd_set_environment_entry;
1.107 nicm 99: extern const struct cmd_entry cmd_set_hook_entry;
1.105 nicm 100: extern const struct cmd_entry cmd_set_option_entry;
101: extern const struct cmd_entry cmd_set_window_option_entry;
102: extern const struct cmd_entry cmd_show_buffer_entry;
103: extern const struct cmd_entry cmd_show_environment_entry;
1.107 nicm 104: extern const struct cmd_entry cmd_show_hooks_entry;
1.105 nicm 105: extern const struct cmd_entry cmd_show_messages_entry;
106: extern const struct cmd_entry cmd_show_options_entry;
107: extern const struct cmd_entry cmd_show_window_options_entry;
108: extern const struct cmd_entry cmd_source_file_entry;
109: extern const struct cmd_entry cmd_split_window_entry;
110: extern const struct cmd_entry cmd_start_server_entry;
111: extern const struct cmd_entry cmd_suspend_client_entry;
112: extern const struct cmd_entry cmd_swap_pane_entry;
113: extern const struct cmd_entry cmd_swap_window_entry;
114: extern const struct cmd_entry cmd_switch_client_entry;
115: extern const struct cmd_entry cmd_unbind_key_entry;
116: extern const struct cmd_entry cmd_unlink_window_entry;
117: extern const struct cmd_entry cmd_up_pane_entry;
118: extern const struct cmd_entry cmd_wait_for_entry;
1.1 nicm 119:
120: const struct cmd_entry *cmd_table[] = {
121: &cmd_attach_session_entry,
122: &cmd_bind_key_entry,
123: &cmd_break_pane_entry,
1.34 nicm 124: &cmd_capture_pane_entry,
1.42 nicm 125: &cmd_choose_buffer_entry,
1.15 nicm 126: &cmd_choose_client_entry,
1.67 nicm 127: &cmd_choose_tree_entry,
1.1 nicm 128: &cmd_clear_history_entry,
129: &cmd_clock_mode_entry,
130: &cmd_command_prompt_entry,
131: &cmd_confirm_before_entry,
132: &cmd_copy_mode_entry,
133: &cmd_delete_buffer_entry,
134: &cmd_detach_client_entry,
1.148 nicm 135: &cmd_display_menu_entry,
1.7 nicm 136: &cmd_display_message_entry,
1.157 ! nicm 137: &cmd_display_popup_entry,
1.16 nicm 138: &cmd_display_panes_entry,
1.1 nicm 139: &cmd_find_window_entry,
140: &cmd_has_session_entry,
1.4 nicm 141: &cmd_if_shell_entry,
1.37 nicm 142: &cmd_join_pane_entry,
1.1 nicm 143: &cmd_kill_pane_entry,
144: &cmd_kill_server_entry,
145: &cmd_kill_session_entry,
146: &cmd_kill_window_entry,
1.45 nicm 147: &cmd_last_pane_entry,
1.1 nicm 148: &cmd_last_window_entry,
149: &cmd_link_window_entry,
150: &cmd_list_buffers_entry,
151: &cmd_list_clients_entry,
152: &cmd_list_commands_entry,
153: &cmd_list_keys_entry,
1.23 nicm 154: &cmd_list_panes_entry,
1.1 nicm 155: &cmd_list_sessions_entry,
156: &cmd_list_windows_entry,
157: &cmd_load_buffer_entry,
1.19 nicm 158: &cmd_lock_client_entry,
1.1 nicm 159: &cmd_lock_server_entry,
1.19 nicm 160: &cmd_lock_session_entry,
1.63 nicm 161: &cmd_move_pane_entry,
1.1 nicm 162: &cmd_move_window_entry,
163: &cmd_new_session_entry,
164: &cmd_new_window_entry,
165: &cmd_next_layout_entry,
166: &cmd_next_window_entry,
167: &cmd_paste_buffer_entry,
1.24 nicm 168: &cmd_pipe_pane_entry,
1.1 nicm 169: &cmd_previous_layout_entry,
170: &cmd_previous_window_entry,
171: &cmd_refresh_client_entry,
172: &cmd_rename_session_entry,
173: &cmd_rename_window_entry,
174: &cmd_resize_pane_entry,
1.141 nicm 175: &cmd_resize_window_entry,
1.56 nicm 176: &cmd_respawn_pane_entry,
1.1 nicm 177: &cmd_respawn_window_entry,
178: &cmd_rotate_window_entry,
1.17 nicm 179: &cmd_run_shell_entry,
1.1 nicm 180: &cmd_save_buffer_entry,
181: &cmd_select_layout_entry,
182: &cmd_select_pane_entry,
183: &cmd_select_window_entry,
184: &cmd_send_keys_entry,
185: &cmd_send_prefix_entry,
186: &cmd_set_buffer_entry,
1.13 nicm 187: &cmd_set_environment_entry,
1.107 nicm 188: &cmd_set_hook_entry,
1.1 nicm 189: &cmd_set_option_entry,
190: &cmd_set_window_option_entry,
191: &cmd_show_buffer_entry,
1.13 nicm 192: &cmd_show_environment_entry,
1.107 nicm 193: &cmd_show_hooks_entry,
1.32 nicm 194: &cmd_show_messages_entry,
1.1 nicm 195: &cmd_show_options_entry,
196: &cmd_show_window_options_entry,
197: &cmd_source_file_entry,
198: &cmd_split_window_entry,
199: &cmd_start_server_entry,
200: &cmd_suspend_client_entry,
201: &cmd_swap_pane_entry,
202: &cmd_swap_window_entry,
203: &cmd_switch_client_entry,
204: &cmd_unbind_key_entry,
205: &cmd_unlink_window_entry,
1.82 nicm 206: &cmd_wait_for_entry,
1.1 nicm 207: NULL
208: };
209:
1.151 nicm 210: static u_int cmd_list_next_group = 1;
211:
1.150 nicm 212: void printflike(3, 4)
213: cmd_log_argv(int argc, char **argv, const char *fmt, ...)
1.140 nicm 214: {
1.150 nicm 215: char *prefix;
216: va_list ap;
217: int i;
218:
219: va_start(ap, fmt);
220: xvasprintf(&prefix, fmt, ap);
221: va_end(ap);
1.140 nicm 222:
223: for (i = 0; i < argc; i++)
224: log_debug("%s: argv[%d]=%s", prefix, i, argv[i]);
1.150 nicm 225: free(prefix);
1.140 nicm 226: }
227:
1.149 nicm 228: void
229: cmd_prepend_argv(int *argc, char ***argv, char *arg)
230: {
231: char **new_argv;
232: int i;
233:
234: new_argv = xreallocarray(NULL, (*argc) + 1, sizeof *new_argv);
235: new_argv[0] = xstrdup(arg);
236: for (i = 0; i < *argc; i++)
237: new_argv[1 + i] = (*argv)[i];
238:
239: free(*argv);
240: *argv = new_argv;
241: (*argc)++;
242: }
243:
244: void
245: cmd_append_argv(int *argc, char ***argv, char *arg)
246: {
247: *argv = xreallocarray(*argv, (*argc) + 1, sizeof **argv);
248: (*argv)[(*argc)++] = xstrdup(arg);
249: }
250:
1.10 nicm 251: int
252: cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
253: {
254: size_t arglen;
255: int i;
1.91 nicm 256:
257: if (argc == 0)
258: return (0);
1.150 nicm 259: cmd_log_argv(argc, argv, "%s", __func__);
1.10 nicm 260:
261: *buf = '\0';
262: for (i = 0; i < argc; i++) {
263: if (strlcpy(buf, argv[i], len) >= len)
264: return (-1);
265: arglen = strlen(argv[i]) + 1;
266: buf += arglen;
267: len -= arglen;
268: }
269:
270: return (0);
271: }
272:
273: int
274: cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
275: {
276: int i;
277: size_t arglen;
278:
279: if (argc == 0)
280: return (0);
281: *argv = xcalloc(argc, sizeof **argv);
282:
283: buf[len - 1] = '\0';
284: for (i = 0; i < argc; i++) {
285: if (len == 0) {
286: cmd_free_argv(argc, *argv);
287: return (-1);
288: }
289:
290: arglen = strlen(buf) + 1;
291: (*argv)[i] = xstrdup(buf);
1.140 nicm 292:
1.10 nicm 293: buf += arglen;
294: len -= arglen;
295: }
1.150 nicm 296: cmd_log_argv(argc, *argv, "%s", __func__);
1.10 nicm 297:
298: return (0);
1.46 nicm 299: }
300:
301: char **
1.93 nicm 302: cmd_copy_argv(int argc, char **argv)
1.46 nicm 303: {
304: char **new_argv;
305: int i;
306:
307: if (argc == 0)
308: return (NULL);
1.94 nicm 309: new_argv = xcalloc(argc + 1, sizeof *new_argv);
1.46 nicm 310: for (i = 0; i < argc; i++) {
311: if (argv[i] != NULL)
312: new_argv[i] = xstrdup(argv[i]);
313: }
314: return (new_argv);
1.10 nicm 315: }
316:
317: void
318: cmd_free_argv(int argc, char **argv)
319: {
320: int i;
321:
322: if (argc == 0)
1.35 nicm 323: return;
1.68 nicm 324: for (i = 0; i < argc; i++)
325: free(argv[i]);
326: free(argv);
1.94 nicm 327: }
328:
329: char *
330: cmd_stringify_argv(int argc, char **argv)
331: {
332: char *buf;
333: int i;
334: size_t len;
335:
336: if (argc == 0)
337: return (xstrdup(""));
338:
339: len = 0;
340: buf = NULL;
341:
342: for (i = 0; i < argc; i++) {
343: len += strlen(argv[i]) + 1;
1.98 nicm 344: buf = xrealloc(buf, len);
1.94 nicm 345:
346: if (i == 0)
347: *buf = '\0';
348: else
349: strlcat(buf, " ", len);
350: strlcat(buf, argv[i], len);
351: }
352: return (buf);
1.10 nicm 353: }
354:
1.149 nicm 355: char *
356: cmd_get_alias(const char *name)
1.135 nicm 357: {
1.149 nicm 358: struct options_entry *o;
359: struct options_array_item *a;
360: union options_value *ov;
361: size_t wanted, n;
362: const char *equals;
1.135 nicm 363:
364: o = options_get_only(global_options, "command-alias");
1.142 nicm 365: if (o == NULL)
1.149 nicm 366: return (NULL);
367: wanted = strlen(name);
1.135 nicm 368:
1.142 nicm 369: a = options_array_first(o);
370: while (a != NULL) {
1.143 nicm 371: ov = options_array_item_value(a);
1.149 nicm 372:
373: equals = strchr(ov->string, '=');
374: if (equals != NULL) {
375: n = equals - ov->string;
376: if (n == wanted && strncmp(name, ov->string, n) == 0)
377: return (xstrdup(equals + 1));
378: }
379:
1.142 nicm 380: a = options_array_next(a);
1.135 nicm 381: }
1.149 nicm 382: return (NULL);
383: }
1.135 nicm 384:
1.149 nicm 385: static const struct cmd_entry *
386: cmd_find(const char *name, char **cause)
387: {
1.152 nicm 388: const struct cmd_entry **loop, *entry, *found = NULL;
389: int ambiguous;
1.155 nicm 390: char s[8192];
1.135 nicm 391:
1.149 nicm 392: ambiguous = 0;
393: for (loop = cmd_table; *loop != NULL; loop++) {
394: entry = *loop;
395: if (entry->alias != NULL && strcmp(entry->alias, name) == 0) {
396: ambiguous = 0;
397: found = entry;
398: break;
399: }
1.135 nicm 400:
1.149 nicm 401: if (strncmp(entry->name, name, strlen(name)) != 0)
402: continue;
403: if (found != NULL)
404: ambiguous = 1;
405: found = entry;
1.135 nicm 406:
1.149 nicm 407: if (strcmp(entry->name, name) == 0)
408: break;
409: }
410: if (ambiguous)
411: goto ambiguous;
412: if (found == NULL) {
413: xasprintf(cause, "unknown command: %s", name);
414: return (NULL);
415: }
416: return (found);
1.135 nicm 417:
1.149 nicm 418: ambiguous:
419: *s = '\0';
420: for (loop = cmd_table; *loop != NULL; loop++) {
421: entry = *loop;
422: if (strncmp(entry->name, name, strlen(name)) != 0)
423: continue;
424: if (strlcat(s, entry->name, sizeof s) >= sizeof s)
425: break;
426: if (strlcat(s, ", ", sizeof s) >= sizeof s)
427: break;
428: }
429: s[strlen(s) - 2] = '\0';
430: xasprintf(cause, "ambiguous command: %s, could be: %s", name, s);
431: return (NULL);
1.135 nicm 432: }
433:
1.1 nicm 434: struct cmd *
1.80 nicm 435: cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause)
1.1 nicm 436: {
1.149 nicm 437: const struct cmd_entry *entry;
1.135 nicm 438: const char *name;
1.27 deraadt 439: struct cmd *cmd;
1.49 nicm 440: struct args *args;
1.1 nicm 441:
1.2 nicm 442: if (argc == 0) {
443: xasprintf(cause, "no command");
1.1 nicm 444: return (NULL);
1.2 nicm 445: }
1.135 nicm 446: name = argv[0];
1.1 nicm 447:
1.149 nicm 448: entry = cmd_find(name, cause);
449: if (entry == NULL)
1.1 nicm 450: return (NULL);
1.150 nicm 451: cmd_log_argv(argc, argv, "%s: %s", __func__, entry->name);
1.1 nicm 452:
1.117 nicm 453: args = args_parse(entry->args.template, argc, argv);
1.49 nicm 454: if (args == NULL)
455: goto usage;
1.117 nicm 456: if (entry->args.lower != -1 && args->argc < entry->args.lower)
1.49 nicm 457: goto usage;
1.117 nicm 458: if (entry->args.upper != -1 && args->argc > entry->args.upper)
1.49 nicm 459: goto usage;
1.1 nicm 460:
1.80 nicm 461: cmd = xcalloc(1, sizeof *cmd);
1.1 nicm 462: cmd->entry = entry;
1.49 nicm 463: cmd->args = args;
1.80 nicm 464:
465: if (file != NULL)
466: cmd->file = xstrdup(file);
467: cmd->line = line;
468:
1.149 nicm 469: cmd->alias = NULL;
470: cmd->argc = argc;
471: cmd->argv = cmd_copy_argv(argc, argv);
472:
1.1 nicm 473: return (cmd);
474:
475: usage:
1.49 nicm 476: if (args != NULL)
477: args_free(args);
1.1 nicm 478: xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
479: return (NULL);
1.149 nicm 480: }
481:
482: void
483: cmd_free(struct cmd *cmd)
484: {
485: free(cmd->alias);
486: cmd_free_argv(cmd->argc, cmd->argv);
487:
488: free(cmd->file);
489:
490: args_free(cmd->args);
491: free(cmd);
1.1 nicm 492: }
493:
1.106 nicm 494: char *
495: cmd_print(struct cmd *cmd)
1.1 nicm 496: {
1.106 nicm 497: char *out, *s;
498:
499: s = args_print(cmd->args);
500: if (*s != '\0')
501: xasprintf(&out, "%s %s", cmd->entry->name, s);
502: else
503: out = xstrdup(cmd->entry->name);
504: free(s);
1.49 nicm 505:
1.106 nicm 506: return (out);
1.151 nicm 507: }
508:
509: struct cmd_list *
510: cmd_list_new(void)
511: {
512: struct cmd_list *cmdlist;
513:
514: cmdlist = xcalloc(1, sizeof *cmdlist);
515: cmdlist->references = 1;
516: cmdlist->group = cmd_list_next_group++;
517: TAILQ_INIT(&cmdlist->list);
518: return (cmdlist);
519: }
520:
521: void
522: cmd_list_append(struct cmd_list *cmdlist, struct cmd *cmd)
523: {
524: cmd->group = cmdlist->group;
525: TAILQ_INSERT_TAIL(&cmdlist->list, cmd, qentry);
526: }
527:
528: void
529: cmd_list_move(struct cmd_list *cmdlist, struct cmd_list *from)
530: {
531: struct cmd *cmd, *cmd1;
532:
533: TAILQ_FOREACH_SAFE(cmd, &from->list, qentry, cmd1) {
534: TAILQ_REMOVE(&from->list, cmd, qentry);
535: TAILQ_INSERT_TAIL(&cmdlist->list, cmd, qentry);
536: }
537: cmdlist->group = cmd_list_next_group++;
538: }
539:
540: void
541: cmd_list_free(struct cmd_list *cmdlist)
542: {
543: struct cmd *cmd, *cmd1;
544:
545: if (--cmdlist->references != 0)
546: return;
547:
548: TAILQ_FOREACH_SAFE(cmd, &cmdlist->list, qentry, cmd1) {
549: TAILQ_REMOVE(&cmdlist->list, cmd, qentry);
550: cmd_free(cmd);
551: }
552:
553: free(cmdlist);
554: }
555:
556: char *
557: cmd_list_print(struct cmd_list *cmdlist, int escaped)
558: {
559: struct cmd *cmd;
560: char *buf, *this;
561: size_t len;
562:
563: len = 1;
564: buf = xcalloc(1, len);
565:
566: TAILQ_FOREACH(cmd, &cmdlist->list, qentry) {
567: this = cmd_print(cmd);
568:
569: len += strlen(this) + 4;
570: buf = xrealloc(buf, len);
571:
572: strlcat(buf, this, len);
573: if (TAILQ_NEXT(cmd, qentry) != NULL) {
574: if (escaped)
575: strlcat(buf, " \\; ", len);
576: else
577: strlcat(buf, " ; ", len);
578: }
579:
580: free(this);
581: }
582:
583: return (buf);
1.1 nicm 584: }
585:
1.99 nicm 586: /* Adjust current mouse position for a pane. */
587: int
588: cmd_mouse_at(struct window_pane *wp, struct mouse_event *m, u_int *xp,
589: u_int *yp, int last)
590: {
591: u_int x, y;
592:
593: if (last) {
1.145 nicm 594: x = m->lx + m->ox;
595: y = m->ly + m->oy;
1.99 nicm 596: } else {
1.146 nicm 597: x = m->x + m->ox;
598: y = m->y + m->oy;
1.99 nicm 599: }
1.145 nicm 600: log_debug("%s: x=%u, y=%u%s", __func__, x, y, last ? " (last)" : "");
1.99 nicm 601:
1.153 nicm 602: if (m->statusat == 0 && y >= m->statuslines)
603: y -= m->statuslines;
1.99 nicm 604:
605: if (x < wp->xoff || x >= wp->xoff + wp->sx)
606: return (-1);
607: if (y < wp->yoff || y >= wp->yoff + wp->sy)
608: return (-1);
609:
1.123 nicm 610: if (xp != NULL)
611: *xp = x - wp->xoff;
612: if (yp != NULL)
613: *yp = y - wp->yoff;
1.99 nicm 614: return (0);
615: }
616:
617: /* Get current mouse window if any. */
618: struct winlink *
619: cmd_mouse_window(struct mouse_event *m, struct session **sp)
620: {
621: struct session *s;
622: struct window *w;
1.147 nicm 623: struct winlink *wl;
1.99 nicm 624:
1.147 nicm 625: if (!m->valid)
1.99 nicm 626: return (NULL);
1.147 nicm 627: if (m->s == -1 || (s = session_find_by_id(m->s)) == NULL)
1.99 nicm 628: return (NULL);
1.147 nicm 629: if (m->w == -1)
630: wl = s->curw;
631: else {
632: if ((w = window_find_by_id(m->w)) == NULL)
633: return (NULL);
634: wl = winlink_find_by_window(&s->windows, w);
635: }
1.99 nicm 636: if (sp != NULL)
637: *sp = s;
1.147 nicm 638: return (wl);
1.99 nicm 639: }
640:
641: /* Get current mouse pane if any. */
642: struct window_pane *
1.104 nicm 643: cmd_mouse_pane(struct mouse_event *m, struct session **sp,
644: struct winlink **wlp)
1.99 nicm 645: {
646: struct winlink *wl;
647: struct window_pane *wp;
648:
649: if ((wl = cmd_mouse_window(m, sp)) == NULL)
650: return (NULL);
651: if ((wp = window_pane_find_by_id(m->wp)) == NULL)
652: return (NULL);
653: if (!window_has_pane(wl->window, wp))
654: return (NULL);
655:
656: if (wlp != NULL)
657: *wlp = wl;
1.43 nicm 658: return (wp);
1.15 nicm 659: }
660:
661: /* Replace the first %% or %idx in template by s. */
662: char *
1.75 nicm 663: cmd_template_replace(const char *template, const char *s, int idx)
1.15 nicm 664: {
1.75 nicm 665: char ch, *buf;
1.156 nicm 666: const char *ptr, *cp, quote[] = "\"\\$;~";
1.127 nicm 667: int replaced, quoted;
1.84 nicm 668: size_t len;
1.15 nicm 669:
1.76 nicm 670: if (strchr(template, '%') == NULL)
1.15 nicm 671: return (xstrdup(template));
672:
673: buf = xmalloc(1);
674: *buf = '\0';
675: len = 0;
676: replaced = 0;
677:
678: ptr = template;
679: while (*ptr != '\0') {
680: switch (ch = *ptr++) {
681: case '%':
682: if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
683: if (*ptr != '%' || replaced)
684: break;
685: replaced = 1;
686: }
687: ptr++;
688:
1.127 nicm 689: quoted = (*ptr == '%');
690: if (quoted)
691: ptr++;
692:
1.132 nicm 693: buf = xrealloc(buf, len + (strlen(s) * 3) + 1);
1.127 nicm 694: for (cp = s; *cp != '\0'; cp++) {
1.131 nicm 695: if (quoted && strchr(quote, *cp) != NULL)
1.127 nicm 696: buf[len++] = '\\';
697: buf[len++] = *cp;
698: }
699: buf[len] = '\0';
1.15 nicm 700: continue;
701: }
1.98 nicm 702: buf = xrealloc(buf, len + 2);
1.15 nicm 703: buf[len++] = ch;
704: buf[len] = '\0';
705: }
706:
707: return (buf);
1.1 nicm 708: }