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