xref: /netbsd-src/external/bsd/tmux/dist/cmd.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1 /* $OpenBSD$ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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 
22 #include <fnmatch.h>
23 #include <pwd.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 
28 #include "tmux.h"
29 
30 extern const struct cmd_entry cmd_attach_session_entry;
31 extern const struct cmd_entry cmd_bind_key_entry;
32 extern const struct cmd_entry cmd_break_pane_entry;
33 extern const struct cmd_entry cmd_capture_pane_entry;
34 extern const struct cmd_entry cmd_choose_buffer_entry;
35 extern const struct cmd_entry cmd_choose_client_entry;
36 extern const struct cmd_entry cmd_choose_tree_entry;
37 extern const struct cmd_entry cmd_clear_history_entry;
38 extern const struct cmd_entry cmd_clock_mode_entry;
39 extern const struct cmd_entry cmd_command_prompt_entry;
40 extern const struct cmd_entry cmd_confirm_before_entry;
41 extern const struct cmd_entry cmd_copy_mode_entry;
42 extern const struct cmd_entry cmd_delete_buffer_entry;
43 extern const struct cmd_entry cmd_detach_client_entry;
44 extern const struct cmd_entry cmd_display_message_entry;
45 extern const struct cmd_entry cmd_display_panes_entry;
46 extern const struct cmd_entry cmd_down_pane_entry;
47 extern const struct cmd_entry cmd_find_window_entry;
48 extern const struct cmd_entry cmd_has_session_entry;
49 extern const struct cmd_entry cmd_if_shell_entry;
50 extern const struct cmd_entry cmd_join_pane_entry;
51 extern const struct cmd_entry cmd_kill_pane_entry;
52 extern const struct cmd_entry cmd_kill_server_entry;
53 extern const struct cmd_entry cmd_kill_session_entry;
54 extern const struct cmd_entry cmd_kill_window_entry;
55 extern const struct cmd_entry cmd_last_pane_entry;
56 extern const struct cmd_entry cmd_last_window_entry;
57 extern const struct cmd_entry cmd_link_window_entry;
58 extern const struct cmd_entry cmd_list_buffers_entry;
59 extern const struct cmd_entry cmd_list_clients_entry;
60 extern const struct cmd_entry cmd_list_commands_entry;
61 extern const struct cmd_entry cmd_list_keys_entry;
62 extern const struct cmd_entry cmd_list_panes_entry;
63 extern const struct cmd_entry cmd_list_sessions_entry;
64 extern const struct cmd_entry cmd_list_windows_entry;
65 extern const struct cmd_entry cmd_load_buffer_entry;
66 extern const struct cmd_entry cmd_lock_client_entry;
67 extern const struct cmd_entry cmd_lock_server_entry;
68 extern const struct cmd_entry cmd_lock_session_entry;
69 extern const struct cmd_entry cmd_move_pane_entry;
70 extern const struct cmd_entry cmd_move_window_entry;
71 extern const struct cmd_entry cmd_new_session_entry;
72 extern const struct cmd_entry cmd_new_window_entry;
73 extern const struct cmd_entry cmd_next_layout_entry;
74 extern const struct cmd_entry cmd_next_window_entry;
75 extern const struct cmd_entry cmd_paste_buffer_entry;
76 extern const struct cmd_entry cmd_pipe_pane_entry;
77 extern const struct cmd_entry cmd_previous_layout_entry;
78 extern const struct cmd_entry cmd_previous_window_entry;
79 extern const struct cmd_entry cmd_refresh_client_entry;
80 extern const struct cmd_entry cmd_rename_session_entry;
81 extern const struct cmd_entry cmd_rename_window_entry;
82 extern const struct cmd_entry cmd_resize_pane_entry;
83 extern const struct cmd_entry cmd_respawn_pane_entry;
84 extern const struct cmd_entry cmd_respawn_window_entry;
85 extern const struct cmd_entry cmd_rotate_window_entry;
86 extern const struct cmd_entry cmd_run_shell_entry;
87 extern const struct cmd_entry cmd_save_buffer_entry;
88 extern const struct cmd_entry cmd_select_layout_entry;
89 extern const struct cmd_entry cmd_select_pane_entry;
90 extern const struct cmd_entry cmd_select_window_entry;
91 extern const struct cmd_entry cmd_send_keys_entry;
92 extern const struct cmd_entry cmd_send_prefix_entry;
93 extern const struct cmd_entry cmd_set_buffer_entry;
94 extern const struct cmd_entry cmd_set_environment_entry;
95 extern const struct cmd_entry cmd_set_hook_entry;
96 extern const struct cmd_entry cmd_set_option_entry;
97 extern const struct cmd_entry cmd_set_window_option_entry;
98 extern const struct cmd_entry cmd_show_buffer_entry;
99 extern const struct cmd_entry cmd_show_environment_entry;
100 extern const struct cmd_entry cmd_show_hooks_entry;
101 extern const struct cmd_entry cmd_show_messages_entry;
102 extern const struct cmd_entry cmd_show_options_entry;
103 extern const struct cmd_entry cmd_show_window_options_entry;
104 extern const struct cmd_entry cmd_source_file_entry;
105 extern const struct cmd_entry cmd_split_window_entry;
106 extern const struct cmd_entry cmd_start_server_entry;
107 extern const struct cmd_entry cmd_suspend_client_entry;
108 extern const struct cmd_entry cmd_swap_pane_entry;
109 extern const struct cmd_entry cmd_swap_window_entry;
110 extern const struct cmd_entry cmd_switch_client_entry;
111 extern const struct cmd_entry cmd_unbind_key_entry;
112 extern const struct cmd_entry cmd_unlink_window_entry;
113 extern const struct cmd_entry cmd_up_pane_entry;
114 extern const struct cmd_entry cmd_wait_for_entry;
115 
116 const struct cmd_entry *cmd_table[] = {
117 	&cmd_attach_session_entry,
118 	&cmd_bind_key_entry,
119 	&cmd_break_pane_entry,
120 	&cmd_capture_pane_entry,
121 	&cmd_choose_buffer_entry,
122 	&cmd_choose_client_entry,
123 	&cmd_choose_tree_entry,
124 	&cmd_clear_history_entry,
125 	&cmd_clock_mode_entry,
126 	&cmd_command_prompt_entry,
127 	&cmd_confirm_before_entry,
128 	&cmd_copy_mode_entry,
129 	&cmd_delete_buffer_entry,
130 	&cmd_detach_client_entry,
131 	&cmd_display_message_entry,
132 	&cmd_display_panes_entry,
133 	&cmd_find_window_entry,
134 	&cmd_has_session_entry,
135 	&cmd_if_shell_entry,
136 	&cmd_join_pane_entry,
137 	&cmd_kill_pane_entry,
138 	&cmd_kill_server_entry,
139 	&cmd_kill_session_entry,
140 	&cmd_kill_window_entry,
141 	&cmd_last_pane_entry,
142 	&cmd_last_window_entry,
143 	&cmd_link_window_entry,
144 	&cmd_list_buffers_entry,
145 	&cmd_list_clients_entry,
146 	&cmd_list_commands_entry,
147 	&cmd_list_keys_entry,
148 	&cmd_list_panes_entry,
149 	&cmd_list_sessions_entry,
150 	&cmd_list_windows_entry,
151 	&cmd_load_buffer_entry,
152 	&cmd_lock_client_entry,
153 	&cmd_lock_server_entry,
154 	&cmd_lock_session_entry,
155 	&cmd_move_pane_entry,
156 	&cmd_move_window_entry,
157 	&cmd_new_session_entry,
158 	&cmd_new_window_entry,
159 	&cmd_next_layout_entry,
160 	&cmd_next_window_entry,
161 	&cmd_paste_buffer_entry,
162 	&cmd_pipe_pane_entry,
163 	&cmd_previous_layout_entry,
164 	&cmd_previous_window_entry,
165 	&cmd_refresh_client_entry,
166 	&cmd_rename_session_entry,
167 	&cmd_rename_window_entry,
168 	&cmd_resize_pane_entry,
169 	&cmd_respawn_pane_entry,
170 	&cmd_respawn_window_entry,
171 	&cmd_rotate_window_entry,
172 	&cmd_run_shell_entry,
173 	&cmd_save_buffer_entry,
174 	&cmd_select_layout_entry,
175 	&cmd_select_pane_entry,
176 	&cmd_select_window_entry,
177 	&cmd_send_keys_entry,
178 	&cmd_send_prefix_entry,
179 	&cmd_set_buffer_entry,
180 	&cmd_set_environment_entry,
181 	&cmd_set_hook_entry,
182 	&cmd_set_option_entry,
183 	&cmd_set_window_option_entry,
184 	&cmd_show_buffer_entry,
185 	&cmd_show_environment_entry,
186 	&cmd_show_hooks_entry,
187 	&cmd_show_messages_entry,
188 	&cmd_show_options_entry,
189 	&cmd_show_window_options_entry,
190 	&cmd_source_file_entry,
191 	&cmd_split_window_entry,
192 	&cmd_start_server_entry,
193 	&cmd_suspend_client_entry,
194 	&cmd_swap_pane_entry,
195 	&cmd_swap_window_entry,
196 	&cmd_switch_client_entry,
197 	&cmd_unbind_key_entry,
198 	&cmd_unlink_window_entry,
199 	&cmd_wait_for_entry,
200 	NULL
201 };
202 
203 void
204 cmd_log_argv(int argc, char **argv, const char *prefix)
205 {
206 	int	i;
207 
208 	for (i = 0; i < argc; i++)
209 		log_debug("%s: argv[%d]=%s", prefix, i, argv[i]);
210 }
211 
212 int
213 cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
214 {
215 	size_t	arglen;
216 	int	i;
217 
218 	if (argc == 0)
219 		return (0);
220 	cmd_log_argv(argc, argv, __func__);
221 
222 	*buf = '\0';
223 	for (i = 0; i < argc; i++) {
224 		if (strlcpy(buf, argv[i], len) >= len)
225 			return (-1);
226 		arglen = strlen(argv[i]) + 1;
227 		buf += arglen;
228 		len -= arglen;
229 	}
230 
231 	return (0);
232 }
233 
234 int
235 cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
236 {
237 	int	i;
238 	size_t	arglen;
239 
240 	if (argc == 0)
241 		return (0);
242 	*argv = xcalloc(argc, sizeof **argv);
243 
244 	buf[len - 1] = '\0';
245 	for (i = 0; i < argc; i++) {
246 		if (len == 0) {
247 			cmd_free_argv(argc, *argv);
248 			return (-1);
249 		}
250 
251 		arglen = strlen(buf) + 1;
252 		(*argv)[i] = xstrdup(buf);
253 
254 		buf += arglen;
255 		len -= arglen;
256 	}
257 	cmd_log_argv(argc, *argv, __func__);
258 
259 	return (0);
260 }
261 
262 char **
263 cmd_copy_argv(int argc, char **argv)
264 {
265 	char	**new_argv;
266 	int	  i;
267 
268 	if (argc == 0)
269 		return (NULL);
270 	new_argv = xcalloc(argc + 1, sizeof *new_argv);
271 	for (i = 0; i < argc; i++) {
272 		if (argv[i] != NULL)
273 			new_argv[i] = xstrdup(argv[i]);
274 	}
275 	return (new_argv);
276 }
277 
278 void
279 cmd_free_argv(int argc, char **argv)
280 {
281 	int	i;
282 
283 	if (argc == 0)
284 		return;
285 	for (i = 0; i < argc; i++)
286 		free(argv[i]);
287 	free(argv);
288 }
289 
290 char *
291 cmd_stringify_argv(int argc, char **argv)
292 {
293 	char	*buf;
294 	int	 i;
295 	size_t	 len;
296 
297 	if (argc == 0)
298 		return (xstrdup(""));
299 
300 	len = 0;
301 	buf = NULL;
302 
303 	for (i = 0; i < argc; i++) {
304 		len += strlen(argv[i]) + 1;
305 		buf = xrealloc(buf, len);
306 
307 		if (i == 0)
308 			*buf = '\0';
309 		else
310 			strlcat(buf, " ", len);
311 		strlcat(buf, argv[i], len);
312 	}
313 	return (buf);
314 }
315 
316 static int
317 cmd_try_alias(int *argc, char ***argv)
318 {
319 	struct options_entry	 *o;
320 	int			  old_argc = *argc, new_argc;
321 	char			**old_argv = *argv, **new_argv;
322 	u_int			  size, idx;
323 	int			  i;
324 	size_t			  wanted;
325 	const char		 *s, *cp = NULL;
326 
327 	o = options_get_only(global_options, "command-alias");
328 	if (o == NULL || options_array_size(o, &size) == -1 || size == 0)
329 		return (-1);
330 
331 	wanted = strlen(old_argv[0]);
332 	for (idx = 0; idx < size; idx++) {
333 		s = options_array_get(o, idx);
334 		if (s == NULL)
335 			continue;
336 
337 		cp = strchr(s, '=');
338 		if (cp == NULL || (size_t)(cp - s) != wanted)
339 			continue;
340 		if (strncmp(old_argv[0], s, wanted) == 0)
341 			break;
342 	}
343 	if (idx == size)
344 		return (-1);
345 
346 	if (cmd_string_split(cp + 1, &new_argc, &new_argv) != 0)
347 		return (-1);
348 
349 	*argc = new_argc + old_argc - 1;
350 	*argv = xcalloc((*argc) + 1, sizeof **argv);
351 
352 	for (i = 0; i < new_argc; i++)
353 		(*argv)[i] = xstrdup(new_argv[i]);
354 	for (i = 1; i < old_argc; i++)
355 		(*argv)[new_argc + i - 1] = xstrdup(old_argv[i]);
356 
357 	log_debug("alias: %s=%s", old_argv[0], cp + 1);
358 	for (i = 0; i < *argc; i++)
359 		log_debug("alias: argv[%d] = %s", i, (*argv)[i]);
360 
361 	cmd_free_argv(new_argc, new_argv);
362 	return (0);
363 }
364 
365 struct cmd *
366 cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause)
367 {
368 	const char		*name;
369 	const struct cmd_entry **entryp, *entry;
370 	struct cmd		*cmd;
371 	struct args		*args;
372 	char			 s[BUFSIZ];
373 	int			 ambiguous, allocated = 0;
374 
375 	*cause = NULL;
376 	if (argc == 0) {
377 		xasprintf(cause, "no command");
378 		return (NULL);
379 	}
380 	name = argv[0];
381 
382 retry:
383 	ambiguous = 0;
384 	entry = NULL;
385 	for (entryp = cmd_table; *entryp != NULL; entryp++) {
386 		if ((*entryp)->alias != NULL &&
387 		    strcmp((*entryp)->alias, argv[0]) == 0) {
388 			ambiguous = 0;
389 			entry = *entryp;
390 			break;
391 		}
392 
393 		if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
394 			continue;
395 		if (entry != NULL)
396 			ambiguous = 1;
397 		entry = *entryp;
398 
399 		/* Bail now if an exact match. */
400 		if (strcmp(entry->name, argv[0]) == 0)
401 			break;
402 	}
403 	if ((ambiguous || entry == NULL) &&
404 	    server_proc != NULL &&
405 	    !allocated &&
406 	    cmd_try_alias(&argc, &argv) == 0) {
407 		allocated = 1;
408 		goto retry;
409 	}
410 	if (ambiguous)
411 		goto ambiguous;
412 	if (entry == NULL) {
413 		xasprintf(cause, "unknown command: %s", name);
414 		return (NULL);
415 	}
416 	cmd_log_argv(argc, argv, entry->name);
417 
418 	args = args_parse(entry->args.template, argc, argv);
419 	if (args == NULL)
420 		goto usage;
421 	if (entry->args.lower != -1 && args->argc < entry->args.lower)
422 		goto usage;
423 	if (entry->args.upper != -1 && args->argc > entry->args.upper)
424 		goto usage;
425 
426 	cmd = xcalloc(1, sizeof *cmd);
427 	cmd->entry = entry;
428 	cmd->args = args;
429 
430 	if (file != NULL)
431 		cmd->file = xstrdup(file);
432 	cmd->line = line;
433 
434 	if (allocated)
435 		cmd_free_argv(argc, argv);
436 	return (cmd);
437 
438 ambiguous:
439 	*s = '\0';
440 	for (entryp = cmd_table; *entryp != NULL; entryp++) {
441 		if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
442 			continue;
443 		if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s)
444 			break;
445 		if (strlcat(s, ", ", sizeof s) >= sizeof s)
446 			break;
447 	}
448 	s[strlen(s) - 2] = '\0';
449 	xasprintf(cause, "ambiguous command: %s, could be: %s", name, s);
450 	return (NULL);
451 
452 usage:
453 	if (args != NULL)
454 		args_free(args);
455 	xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
456 	return (NULL);
457 }
458 
459 char *
460 cmd_print(struct cmd *cmd)
461 {
462 	char	*out, *s;
463 
464 	s = args_print(cmd->args);
465 	if (*s != '\0')
466 		xasprintf(&out, "%s %s", cmd->entry->name, s);
467 	else
468 		out = xstrdup(cmd->entry->name);
469 	free(s);
470 
471 	return (out);
472 }
473 
474 /* Adjust current mouse position for a pane. */
475 int
476 cmd_mouse_at(struct window_pane *wp, struct mouse_event *m, u_int *xp,
477     u_int *yp, int last)
478 {
479 	u_int	x, y;
480 
481 	if (last) {
482 		x = m->lx;
483 		y = m->ly;
484 	} else {
485 		x = m->x;
486 		y = m->y;
487 	}
488 
489 	if (m->statusat == 0 && y > 0)
490 		y--;
491 	else if (m->statusat > 0 && y >= (u_int)m->statusat)
492 		y = m->statusat - 1;
493 
494 	if (x < wp->xoff || x >= wp->xoff + wp->sx)
495 		return (-1);
496 	if (y < wp->yoff || y >= wp->yoff + wp->sy)
497 		return (-1);
498 
499 	if (xp != NULL)
500 		*xp = x - wp->xoff;
501 	if (yp != NULL)
502 		*yp = y - wp->yoff;
503 	return (0);
504 }
505 
506 /* Get current mouse window if any. */
507 struct winlink *
508 cmd_mouse_window(struct mouse_event *m, struct session **sp)
509 {
510 	struct session	*s;
511 	struct window	*w;
512 
513 	if (!m->valid || m->s == -1 || m->w == -1)
514 		return (NULL);
515 	if ((s = session_find_by_id(m->s)) == NULL)
516 		return (NULL);
517 	if ((w = window_find_by_id(m->w)) == NULL)
518 		return (NULL);
519 
520 	if (sp != NULL)
521 		*sp = s;
522 	return (winlink_find_by_window(&s->windows, w));
523 }
524 
525 /* Get current mouse pane if any. */
526 struct window_pane *
527 cmd_mouse_pane(struct mouse_event *m, struct session **sp,
528     struct winlink **wlp)
529 {
530 	struct winlink		*wl;
531 	struct window_pane     	*wp;
532 
533 	if ((wl = cmd_mouse_window(m, sp)) == NULL)
534 		return (NULL);
535 	if ((wp = window_pane_find_by_id(m->wp)) == NULL)
536 		return (NULL);
537 	if (!window_has_pane(wl->window, wp))
538 		return (NULL);
539 
540 	if (wlp != NULL)
541 		*wlp = wl;
542 	return (wp);
543 }
544 
545 /* Replace the first %% or %idx in template by s. */
546 char *
547 cmd_template_replace(const char *template, const char *s, int idx)
548 {
549 	char		 ch, *buf;
550 	const char	*ptr, *cp, quote[] = "\"\\$";
551 	int		 replaced, quoted;
552 	size_t		 len;
553 
554 	if (strchr(template, '%') == NULL)
555 		return (xstrdup(template));
556 
557 	buf = xmalloc(1);
558 	*buf = '\0';
559 	len = 0;
560 	replaced = 0;
561 
562 	ptr = template;
563 	while (*ptr != '\0') {
564 		switch (ch = *ptr++) {
565 		case '%':
566 			if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
567 				if (*ptr != '%' || replaced)
568 					break;
569 				replaced = 1;
570 			}
571 			ptr++;
572 
573 			quoted = (*ptr == '%');
574 			if (quoted)
575 				ptr++;
576 
577 			buf = xrealloc(buf, len + (strlen(s) * 3) + 1);
578 			for (cp = s; *cp != '\0'; cp++) {
579 				if (quoted && strchr(quote, *cp) != NULL)
580 					buf[len++] = '\\';
581 				if (quoted && *cp == ';') {
582 					buf[len++] = '\\';
583 					buf[len++] = '\\';
584 				}
585 				buf[len++] = *cp;
586 			}
587 			buf[len] = '\0';
588 			continue;
589 		}
590 		buf = xrealloc(buf, len + 2);
591 		buf[len++] = ch;
592 		buf[len] = '\0';
593 	}
594 
595 	return (buf);
596 }
597