Annotation of src/usr.bin/tmux/cmd.c, Revision 1.160
1.159 nicm 1: /* $OpenBSD: cmd.c,v 1.158 2020/04/13 08:26:27 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.158 nicm 210: /* Instance of a command. */
211: struct cmd {
212: const struct cmd_entry *entry;
213: struct args *args;
214: u_int group;
215:
216: char *file;
217: u_int line;
218:
219: char *alias;
220: int argc;
221: char **argv;
222:
223: TAILQ_ENTRY(cmd) qentry;
224: };
225: TAILQ_HEAD(cmds, cmd);
226:
227: /* Next group number for new command list. */
1.151 nicm 228: static u_int cmd_list_next_group = 1;
229:
1.158 nicm 230: /* Log an argument vector. */
1.150 nicm 231: void printflike(3, 4)
232: cmd_log_argv(int argc, char **argv, const char *fmt, ...)
1.140 nicm 233: {
1.150 nicm 234: char *prefix;
235: va_list ap;
236: int i;
237:
238: va_start(ap, fmt);
239: xvasprintf(&prefix, fmt, ap);
240: va_end(ap);
1.140 nicm 241:
242: for (i = 0; i < argc; i++)
243: log_debug("%s: argv[%d]=%s", prefix, i, argv[i]);
1.150 nicm 244: free(prefix);
1.140 nicm 245: }
246:
1.158 nicm 247: /* Prepend to an argument vector. */
1.149 nicm 248: void
249: cmd_prepend_argv(int *argc, char ***argv, char *arg)
250: {
251: char **new_argv;
252: int i;
253:
254: new_argv = xreallocarray(NULL, (*argc) + 1, sizeof *new_argv);
255: new_argv[0] = xstrdup(arg);
256: for (i = 0; i < *argc; i++)
257: new_argv[1 + i] = (*argv)[i];
258:
259: free(*argv);
260: *argv = new_argv;
261: (*argc)++;
262: }
263:
1.158 nicm 264: /* Append to an argument vector. */
1.149 nicm 265: void
266: cmd_append_argv(int *argc, char ***argv, char *arg)
267: {
268: *argv = xreallocarray(*argv, (*argc) + 1, sizeof **argv);
269: (*argv)[(*argc)++] = xstrdup(arg);
270: }
271:
1.158 nicm 272: /* Pack an argument vector up into a buffer. */
1.10 nicm 273: int
274: cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
275: {
276: size_t arglen;
277: int i;
1.91 nicm 278:
279: if (argc == 0)
280: return (0);
1.150 nicm 281: cmd_log_argv(argc, argv, "%s", __func__);
1.10 nicm 282:
283: *buf = '\0';
284: for (i = 0; i < argc; i++) {
285: if (strlcpy(buf, argv[i], len) >= len)
286: return (-1);
287: arglen = strlen(argv[i]) + 1;
288: buf += arglen;
289: len -= arglen;
290: }
291:
292: return (0);
293: }
294:
1.158 nicm 295: /* Unpack an argument vector from a packed buffer. */
1.10 nicm 296: int
297: cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
298: {
299: int i;
300: size_t arglen;
301:
302: if (argc == 0)
303: return (0);
304: *argv = xcalloc(argc, sizeof **argv);
305:
306: buf[len - 1] = '\0';
307: for (i = 0; i < argc; i++) {
308: if (len == 0) {
309: cmd_free_argv(argc, *argv);
310: return (-1);
311: }
312:
313: arglen = strlen(buf) + 1;
314: (*argv)[i] = xstrdup(buf);
1.140 nicm 315:
1.10 nicm 316: buf += arglen;
317: len -= arglen;
318: }
1.150 nicm 319: cmd_log_argv(argc, *argv, "%s", __func__);
1.10 nicm 320:
321: return (0);
1.46 nicm 322: }
323:
1.158 nicm 324: /* Copy an argument vector, ensuring it is terminated by NULL. */
1.46 nicm 325: char **
1.93 nicm 326: cmd_copy_argv(int argc, char **argv)
1.46 nicm 327: {
328: char **new_argv;
329: int i;
330:
331: if (argc == 0)
332: return (NULL);
1.94 nicm 333: new_argv = xcalloc(argc + 1, sizeof *new_argv);
1.46 nicm 334: for (i = 0; i < argc; i++) {
335: if (argv[i] != NULL)
336: new_argv[i] = xstrdup(argv[i]);
337: }
338: return (new_argv);
1.10 nicm 339: }
340:
1.158 nicm 341: /* Free an argument vector. */
1.10 nicm 342: void
343: cmd_free_argv(int argc, char **argv)
344: {
345: int i;
346:
347: if (argc == 0)
1.35 nicm 348: return;
1.68 nicm 349: for (i = 0; i < argc; i++)
350: free(argv[i]);
351: free(argv);
1.94 nicm 352: }
353:
1.158 nicm 354: /* Convert argument vector to a string. */
1.94 nicm 355: char *
356: cmd_stringify_argv(int argc, char **argv)
357: {
358: char *buf;
359: int i;
360: size_t len;
361:
362: if (argc == 0)
363: return (xstrdup(""));
364:
365: len = 0;
366: buf = NULL;
367:
368: for (i = 0; i < argc; i++) {
369: len += strlen(argv[i]) + 1;
1.98 nicm 370: buf = xrealloc(buf, len);
1.94 nicm 371:
372: if (i == 0)
373: *buf = '\0';
374: else
375: strlcat(buf, " ", len);
376: strlcat(buf, argv[i], len);
377: }
378: return (buf);
1.10 nicm 379: }
380:
1.158 nicm 381: /* Get entry for command. */
382: const struct cmd_entry *
383: cmd_get_entry(struct cmd *cmd)
384: {
385: return (cmd->entry);
386: }
387:
388: /* Get arguments for command. */
389: struct args *
390: cmd_get_args(struct cmd *cmd)
391: {
392: return (cmd->args);
393: }
394:
1.159 nicm 395: /* Get group for command. */
396: u_int
397: cmd_get_group(struct cmd *cmd)
398: {
399: return (cmd->group);
400: }
401:
1.158 nicm 402: /* Get file and line for command. */
403: void
404: cmd_get_source(struct cmd *cmd, const char **file, u_int *line)
405: {
406: if (file != NULL)
407: *file = cmd->file;
408: if (line != NULL)
409: *line = cmd->line;
410: }
411:
412: /* Look for an alias for a command. */
1.149 nicm 413: char *
414: cmd_get_alias(const char *name)
1.135 nicm 415: {
1.149 nicm 416: struct options_entry *o;
417: struct options_array_item *a;
418: union options_value *ov;
419: size_t wanted, n;
420: const char *equals;
1.135 nicm 421:
422: o = options_get_only(global_options, "command-alias");
1.142 nicm 423: if (o == NULL)
1.149 nicm 424: return (NULL);
425: wanted = strlen(name);
1.135 nicm 426:
1.142 nicm 427: a = options_array_first(o);
428: while (a != NULL) {
1.143 nicm 429: ov = options_array_item_value(a);
1.149 nicm 430:
431: equals = strchr(ov->string, '=');
432: if (equals != NULL) {
433: n = equals - ov->string;
434: if (n == wanted && strncmp(name, ov->string, n) == 0)
435: return (xstrdup(equals + 1));
436: }
437:
1.142 nicm 438: a = options_array_next(a);
1.135 nicm 439: }
1.149 nicm 440: return (NULL);
441: }
1.135 nicm 442:
1.158 nicm 443: /* Look up a command entry by name. */
1.149 nicm 444: static const struct cmd_entry *
445: cmd_find(const char *name, char **cause)
446: {
1.152 nicm 447: const struct cmd_entry **loop, *entry, *found = NULL;
448: int ambiguous;
1.155 nicm 449: char s[8192];
1.135 nicm 450:
1.149 nicm 451: ambiguous = 0;
452: for (loop = cmd_table; *loop != NULL; loop++) {
453: entry = *loop;
454: if (entry->alias != NULL && strcmp(entry->alias, name) == 0) {
455: ambiguous = 0;
456: found = entry;
457: break;
458: }
1.135 nicm 459:
1.149 nicm 460: if (strncmp(entry->name, name, strlen(name)) != 0)
461: continue;
462: if (found != NULL)
463: ambiguous = 1;
464: found = entry;
1.135 nicm 465:
1.149 nicm 466: if (strcmp(entry->name, name) == 0)
467: break;
468: }
469: if (ambiguous)
470: goto ambiguous;
471: if (found == NULL) {
472: xasprintf(cause, "unknown command: %s", name);
473: return (NULL);
474: }
475: return (found);
1.135 nicm 476:
1.149 nicm 477: ambiguous:
478: *s = '\0';
479: for (loop = cmd_table; *loop != NULL; loop++) {
480: entry = *loop;
481: if (strncmp(entry->name, name, strlen(name)) != 0)
482: continue;
483: if (strlcat(s, entry->name, sizeof s) >= sizeof s)
484: break;
485: if (strlcat(s, ", ", sizeof s) >= sizeof s)
486: break;
487: }
488: s[strlen(s) - 2] = '\0';
489: xasprintf(cause, "ambiguous command: %s, could be: %s", name, s);
490: return (NULL);
1.135 nicm 491: }
492:
1.158 nicm 493: /* Parse a single command from an argument vector. */
1.1 nicm 494: struct cmd *
1.80 nicm 495: cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause)
1.1 nicm 496: {
1.149 nicm 497: const struct cmd_entry *entry;
1.135 nicm 498: const char *name;
1.27 deraadt 499: struct cmd *cmd;
1.49 nicm 500: struct args *args;
1.1 nicm 501:
1.2 nicm 502: if (argc == 0) {
503: xasprintf(cause, "no command");
1.1 nicm 504: return (NULL);
1.2 nicm 505: }
1.135 nicm 506: name = argv[0];
1.1 nicm 507:
1.149 nicm 508: entry = cmd_find(name, cause);
509: if (entry == NULL)
1.1 nicm 510: return (NULL);
1.150 nicm 511: cmd_log_argv(argc, argv, "%s: %s", __func__, entry->name);
1.1 nicm 512:
1.117 nicm 513: args = args_parse(entry->args.template, argc, argv);
1.49 nicm 514: if (args == NULL)
515: goto usage;
1.117 nicm 516: if (entry->args.lower != -1 && args->argc < entry->args.lower)
1.49 nicm 517: goto usage;
1.117 nicm 518: if (entry->args.upper != -1 && args->argc > entry->args.upper)
1.49 nicm 519: goto usage;
1.1 nicm 520:
1.80 nicm 521: cmd = xcalloc(1, sizeof *cmd);
1.1 nicm 522: cmd->entry = entry;
1.49 nicm 523: cmd->args = args;
1.80 nicm 524:
525: if (file != NULL)
526: cmd->file = xstrdup(file);
527: cmd->line = line;
528:
1.149 nicm 529: cmd->alias = NULL;
530: cmd->argc = argc;
531: cmd->argv = cmd_copy_argv(argc, argv);
532:
1.1 nicm 533: return (cmd);
534:
535: usage:
1.49 nicm 536: if (args != NULL)
537: args_free(args);
1.1 nicm 538: xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
539: return (NULL);
1.149 nicm 540: }
541:
1.158 nicm 542: /* Free a command. */
1.149 nicm 543: void
544: cmd_free(struct cmd *cmd)
545: {
546: free(cmd->alias);
547: cmd_free_argv(cmd->argc, cmd->argv);
548:
549: free(cmd->file);
550:
551: args_free(cmd->args);
552: free(cmd);
1.1 nicm 553: }
554:
1.158 nicm 555: /* Get a command as a string. */
1.106 nicm 556: char *
557: cmd_print(struct cmd *cmd)
1.1 nicm 558: {
1.106 nicm 559: char *out, *s;
560:
561: s = args_print(cmd->args);
562: if (*s != '\0')
563: xasprintf(&out, "%s %s", cmd->entry->name, s);
564: else
565: out = xstrdup(cmd->entry->name);
566: free(s);
1.49 nicm 567:
1.106 nicm 568: return (out);
1.151 nicm 569: }
570:
1.158 nicm 571: /* Create a new command list. */
1.151 nicm 572: struct cmd_list *
573: cmd_list_new(void)
574: {
575: struct cmd_list *cmdlist;
576:
577: cmdlist = xcalloc(1, sizeof *cmdlist);
578: cmdlist->references = 1;
579: cmdlist->group = cmd_list_next_group++;
1.158 nicm 580: cmdlist->list = xcalloc(1, sizeof *cmdlist->list);
581: TAILQ_INIT(cmdlist->list);
1.151 nicm 582: return (cmdlist);
583: }
584:
1.158 nicm 585: /* Append a command to a command list. */
1.151 nicm 586: void
587: cmd_list_append(struct cmd_list *cmdlist, struct cmd *cmd)
588: {
589: cmd->group = cmdlist->group;
1.158 nicm 590: TAILQ_INSERT_TAIL(cmdlist->list, cmd, qentry);
1.151 nicm 591: }
592:
1.158 nicm 593: /* Move all commands from one command list to another */
1.151 nicm 594: void
595: cmd_list_move(struct cmd_list *cmdlist, struct cmd_list *from)
596: {
597: struct cmd *cmd, *cmd1;
598:
1.158 nicm 599: TAILQ_FOREACH_SAFE(cmd, from->list, qentry, cmd1) {
600: TAILQ_REMOVE(from->list, cmd, qentry);
601: TAILQ_INSERT_TAIL(cmdlist->list, cmd, qentry);
1.151 nicm 602: }
603: cmdlist->group = cmd_list_next_group++;
604: }
605:
1.158 nicm 606: /* Free a command list. */
1.151 nicm 607: void
608: cmd_list_free(struct cmd_list *cmdlist)
609: {
610: struct cmd *cmd, *cmd1;
611:
612: if (--cmdlist->references != 0)
613: return;
614:
1.158 nicm 615: TAILQ_FOREACH_SAFE(cmd, cmdlist->list, qentry, cmd1) {
616: TAILQ_REMOVE(cmdlist->list, cmd, qentry);
1.151 nicm 617: cmd_free(cmd);
618: }
1.158 nicm 619: free(cmdlist->list);
1.151 nicm 620: free(cmdlist);
621: }
622:
1.158 nicm 623: /* Get a command list as a string. */
1.151 nicm 624: char *
625: cmd_list_print(struct cmd_list *cmdlist, int escaped)
626: {
1.160 ! nicm 627: struct cmd *cmd, *next;
1.151 nicm 628: char *buf, *this;
629: size_t len;
630:
631: len = 1;
632: buf = xcalloc(1, len);
633:
1.158 nicm 634: TAILQ_FOREACH(cmd, cmdlist->list, qentry) {
1.151 nicm 635: this = cmd_print(cmd);
636:
1.160 ! nicm 637: len += strlen(this) + 6;
1.151 nicm 638: buf = xrealloc(buf, len);
639:
640: strlcat(buf, this, len);
1.160 ! nicm 641:
! 642: next = TAILQ_NEXT(cmd, qentry);
! 643: if (next != NULL) {
! 644: if (cmd->group != next->group) {
! 645: if (escaped)
! 646: strlcat(buf, " \\;\\; ", len);
! 647: else
! 648: strlcat(buf, " ;; ", len);
! 649: } else {
! 650: if (escaped)
! 651: strlcat(buf, " \\; ", len);
! 652: else
! 653: strlcat(buf, " ; ", len);
! 654: }
1.151 nicm 655: }
656:
657: free(this);
658: }
659:
660: return (buf);
1.158 nicm 661: }
662:
663: /* Get first command in list. */
664: struct cmd *
1.159 nicm 665: cmd_list_first(struct cmd_list *cmdlist)
1.158 nicm 666: {
1.159 nicm 667: return (TAILQ_FIRST(cmdlist->list));
1.158 nicm 668: }
669:
670: /* Get next command in list. */
671: struct cmd *
1.159 nicm 672: cmd_list_next(struct cmd *cmd)
1.158 nicm 673: {
1.159 nicm 674: return (TAILQ_NEXT(cmd, qentry));
1.158 nicm 675: }
676:
677: /* Do all of the commands in this command list have this flag? */
678: int
679: cmd_list_all_have(struct cmd_list *cmdlist, int flag)
680: {
681: struct cmd *cmd;
682:
683: TAILQ_FOREACH(cmd, cmdlist->list, qentry) {
684: if (~cmd->entry->flags & flag)
685: return (0);
686: }
687: return (1);
688: }
689:
690: /* Do any of the commands in this command list have this flag? */
691: int
692: cmd_list_any_have(struct cmd_list *cmdlist, int flag)
693: {
694: struct cmd *cmd;
695:
696: TAILQ_FOREACH(cmd, cmdlist->list, qentry) {
697: if (cmd->entry->flags & flag)
698: return (1);
699: }
700: return (0);
1.1 nicm 701: }
702:
1.99 nicm 703: /* Adjust current mouse position for a pane. */
704: int
705: cmd_mouse_at(struct window_pane *wp, struct mouse_event *m, u_int *xp,
706: u_int *yp, int last)
707: {
708: u_int x, y;
709:
710: if (last) {
1.145 nicm 711: x = m->lx + m->ox;
712: y = m->ly + m->oy;
1.99 nicm 713: } else {
1.146 nicm 714: x = m->x + m->ox;
715: y = m->y + m->oy;
1.99 nicm 716: }
1.145 nicm 717: log_debug("%s: x=%u, y=%u%s", __func__, x, y, last ? " (last)" : "");
1.99 nicm 718:
1.153 nicm 719: if (m->statusat == 0 && y >= m->statuslines)
720: y -= m->statuslines;
1.99 nicm 721:
722: if (x < wp->xoff || x >= wp->xoff + wp->sx)
723: return (-1);
724: if (y < wp->yoff || y >= wp->yoff + wp->sy)
725: return (-1);
726:
1.123 nicm 727: if (xp != NULL)
728: *xp = x - wp->xoff;
729: if (yp != NULL)
730: *yp = y - wp->yoff;
1.99 nicm 731: return (0);
732: }
733:
734: /* Get current mouse window if any. */
735: struct winlink *
736: cmd_mouse_window(struct mouse_event *m, struct session **sp)
737: {
738: struct session *s;
739: struct window *w;
1.147 nicm 740: struct winlink *wl;
1.99 nicm 741:
1.147 nicm 742: if (!m->valid)
1.99 nicm 743: return (NULL);
1.147 nicm 744: if (m->s == -1 || (s = session_find_by_id(m->s)) == NULL)
1.99 nicm 745: return (NULL);
1.147 nicm 746: if (m->w == -1)
747: wl = s->curw;
748: else {
749: if ((w = window_find_by_id(m->w)) == NULL)
750: return (NULL);
751: wl = winlink_find_by_window(&s->windows, w);
752: }
1.99 nicm 753: if (sp != NULL)
754: *sp = s;
1.147 nicm 755: return (wl);
1.99 nicm 756: }
757:
758: /* Get current mouse pane if any. */
759: struct window_pane *
1.104 nicm 760: cmd_mouse_pane(struct mouse_event *m, struct session **sp,
761: struct winlink **wlp)
1.99 nicm 762: {
763: struct winlink *wl;
764: struct window_pane *wp;
765:
766: if ((wl = cmd_mouse_window(m, sp)) == NULL)
767: return (NULL);
768: if ((wp = window_pane_find_by_id(m->wp)) == NULL)
769: return (NULL);
770: if (!window_has_pane(wl->window, wp))
771: return (NULL);
772:
773: if (wlp != NULL)
774: *wlp = wl;
1.43 nicm 775: return (wp);
1.15 nicm 776: }
777:
778: /* Replace the first %% or %idx in template by s. */
779: char *
1.75 nicm 780: cmd_template_replace(const char *template, const char *s, int idx)
1.15 nicm 781: {
1.75 nicm 782: char ch, *buf;
1.156 nicm 783: const char *ptr, *cp, quote[] = "\"\\$;~";
1.127 nicm 784: int replaced, quoted;
1.84 nicm 785: size_t len;
1.15 nicm 786:
1.76 nicm 787: if (strchr(template, '%') == NULL)
1.15 nicm 788: return (xstrdup(template));
789:
790: buf = xmalloc(1);
791: *buf = '\0';
792: len = 0;
793: replaced = 0;
794:
795: ptr = template;
796: while (*ptr != '\0') {
797: switch (ch = *ptr++) {
798: case '%':
799: if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
800: if (*ptr != '%' || replaced)
801: break;
802: replaced = 1;
803: }
804: ptr++;
805:
1.127 nicm 806: quoted = (*ptr == '%');
807: if (quoted)
808: ptr++;
809:
1.132 nicm 810: buf = xrealloc(buf, len + (strlen(s) * 3) + 1);
1.127 nicm 811: for (cp = s; *cp != '\0'; cp++) {
1.131 nicm 812: if (quoted && strchr(quote, *cp) != NULL)
1.127 nicm 813: buf[len++] = '\\';
814: buf[len++] = *cp;
815: }
816: buf[len] = '\0';
1.15 nicm 817: continue;
818: }
1.98 nicm 819: buf = xrealloc(buf, len + 2);
1.15 nicm 820: buf[len++] = ch;
821: buf[len] = '\0';
822: }
823:
824: return (buf);
1.1 nicm 825: }