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