GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tmux/cmd.c Lines: 50 197 25.4 %
Date: 2017-11-07 Branches: 28 166 16.9 %

Line Branch Exec Source
1
/* $OpenBSD: cmd.c,v 1.139 2017/05/30 21:44:59 nicm Exp $ */
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 <paths.h>
24
#include <pwd.h>
25
#include <stdlib.h>
26
#include <string.h>
27
#include <unistd.h>
28
29
#include "tmux.h"
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;
45
extern const struct cmd_entry cmd_display_message_entry;
46
extern const struct cmd_entry cmd_display_panes_entry;
47
extern const struct cmd_entry cmd_down_pane_entry;
48
extern const struct cmd_entry cmd_find_window_entry;
49
extern const struct cmd_entry cmd_has_session_entry;
50
extern const struct cmd_entry cmd_if_shell_entry;
51
extern const struct cmd_entry cmd_join_pane_entry;
52
extern const struct cmd_entry cmd_kill_pane_entry;
53
extern const struct cmd_entry cmd_kill_server_entry;
54
extern const struct cmd_entry cmd_kill_session_entry;
55
extern const struct cmd_entry cmd_kill_window_entry;
56
extern const struct cmd_entry cmd_last_pane_entry;
57
extern const struct cmd_entry cmd_last_window_entry;
58
extern const struct cmd_entry cmd_link_window_entry;
59
extern const struct cmd_entry cmd_list_buffers_entry;
60
extern const struct cmd_entry cmd_list_clients_entry;
61
extern const struct cmd_entry cmd_list_commands_entry;
62
extern const struct cmd_entry cmd_list_keys_entry;
63
extern const struct cmd_entry cmd_list_panes_entry;
64
extern const struct cmd_entry cmd_list_sessions_entry;
65
extern const struct cmd_entry cmd_list_windows_entry;
66
extern const struct cmd_entry cmd_load_buffer_entry;
67
extern const struct cmd_entry cmd_lock_client_entry;
68
extern const struct cmd_entry cmd_lock_server_entry;
69
extern const struct cmd_entry cmd_lock_session_entry;
70
extern const struct cmd_entry cmd_move_pane_entry;
71
extern const struct cmd_entry cmd_move_window_entry;
72
extern const struct cmd_entry cmd_new_session_entry;
73
extern const struct cmd_entry cmd_new_window_entry;
74
extern const struct cmd_entry cmd_next_layout_entry;
75
extern const struct cmd_entry cmd_next_window_entry;
76
extern const struct cmd_entry cmd_paste_buffer_entry;
77
extern const struct cmd_entry cmd_pipe_pane_entry;
78
extern const struct cmd_entry cmd_previous_layout_entry;
79
extern const struct cmd_entry cmd_previous_window_entry;
80
extern const struct cmd_entry cmd_refresh_client_entry;
81
extern const struct cmd_entry cmd_rename_session_entry;
82
extern const struct cmd_entry cmd_rename_window_entry;
83
extern const struct cmd_entry cmd_resize_pane_entry;
84
extern const struct cmd_entry cmd_respawn_pane_entry;
85
extern const struct cmd_entry cmd_respawn_window_entry;
86
extern const struct cmd_entry cmd_rotate_window_entry;
87
extern const struct cmd_entry cmd_run_shell_entry;
88
extern const struct cmd_entry cmd_save_buffer_entry;
89
extern const struct cmd_entry cmd_select_layout_entry;
90
extern const struct cmd_entry cmd_select_pane_entry;
91
extern const struct cmd_entry cmd_select_window_entry;
92
extern const struct cmd_entry cmd_send_keys_entry;
93
extern const struct cmd_entry cmd_send_prefix_entry;
94
extern const struct cmd_entry cmd_set_buffer_entry;
95
extern const struct cmd_entry cmd_set_environment_entry;
96
extern const struct cmd_entry cmd_set_hook_entry;
97
extern const struct cmd_entry cmd_set_option_entry;
98
extern const struct cmd_entry cmd_set_window_option_entry;
99
extern const struct cmd_entry cmd_show_buffer_entry;
100
extern const struct cmd_entry cmd_show_environment_entry;
101
extern const struct cmd_entry cmd_show_hooks_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_tree_entry,
125
	&cmd_clear_history_entry,
126
	&cmd_clock_mode_entry,
127
	&cmd_command_prompt_entry,
128
	&cmd_confirm_before_entry,
129
	&cmd_copy_mode_entry,
130
	&cmd_delete_buffer_entry,
131
	&cmd_detach_client_entry,
132
	&cmd_display_message_entry,
133
	&cmd_display_panes_entry,
134
	&cmd_find_window_entry,
135
	&cmd_has_session_entry,
136
	&cmd_if_shell_entry,
137
	&cmd_join_pane_entry,
138
	&cmd_kill_pane_entry,
139
	&cmd_kill_server_entry,
140
	&cmd_kill_session_entry,
141
	&cmd_kill_window_entry,
142
	&cmd_last_pane_entry,
143
	&cmd_last_window_entry,
144
	&cmd_link_window_entry,
145
	&cmd_list_buffers_entry,
146
	&cmd_list_clients_entry,
147
	&cmd_list_commands_entry,
148
	&cmd_list_keys_entry,
149
	&cmd_list_panes_entry,
150
	&cmd_list_sessions_entry,
151
	&cmd_list_windows_entry,
152
	&cmd_load_buffer_entry,
153
	&cmd_lock_client_entry,
154
	&cmd_lock_server_entry,
155
	&cmd_lock_session_entry,
156
	&cmd_move_pane_entry,
157
	&cmd_move_window_entry,
158
	&cmd_new_session_entry,
159
	&cmd_new_window_entry,
160
	&cmd_next_layout_entry,
161
	&cmd_next_window_entry,
162
	&cmd_paste_buffer_entry,
163
	&cmd_pipe_pane_entry,
164
	&cmd_previous_layout_entry,
165
	&cmd_previous_window_entry,
166
	&cmd_refresh_client_entry,
167
	&cmd_rename_session_entry,
168
	&cmd_rename_window_entry,
169
	&cmd_resize_pane_entry,
170
	&cmd_respawn_pane_entry,
171
	&cmd_respawn_window_entry,
172
	&cmd_rotate_window_entry,
173
	&cmd_run_shell_entry,
174
	&cmd_save_buffer_entry,
175
	&cmd_select_layout_entry,
176
	&cmd_select_pane_entry,
177
	&cmd_select_window_entry,
178
	&cmd_send_keys_entry,
179
	&cmd_send_prefix_entry,
180
	&cmd_set_buffer_entry,
181
	&cmd_set_environment_entry,
182
	&cmd_set_hook_entry,
183
	&cmd_set_option_entry,
184
	&cmd_set_window_option_entry,
185
	&cmd_show_buffer_entry,
186
	&cmd_show_environment_entry,
187
	&cmd_show_hooks_entry,
188
	&cmd_show_messages_entry,
189
	&cmd_show_options_entry,
190
	&cmd_show_window_options_entry,
191
	&cmd_source_file_entry,
192
	&cmd_split_window_entry,
193
	&cmd_start_server_entry,
194
	&cmd_suspend_client_entry,
195
	&cmd_swap_pane_entry,
196
	&cmd_swap_window_entry,
197
	&cmd_switch_client_entry,
198
	&cmd_unbind_key_entry,
199
	&cmd_unlink_window_entry,
200
	&cmd_wait_for_entry,
201
	NULL
202
};
203
204
int
205
cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
206
{
207
	size_t	arglen;
208
	int	i;
209
210
12
	if (argc == 0)
211
1
		return (0);
212
213
5
	*buf = '\0';
214
20
	for (i = 0; i < argc; i++) {
215
5
		if (strlcpy(buf, argv[i], len) >= len)
216
			return (-1);
217
5
		arglen = strlen(argv[i]) + 1;
218
5
		buf += arglen;
219
5
		len -= arglen;
220
	}
221
222
5
	return (0);
223
6
}
224
225
int
226
cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
227
{
228
	int	i;
229
	size_t	arglen;
230
231
	if (argc == 0)
232
		return (0);
233
	*argv = xcalloc(argc, sizeof **argv);
234
235
	buf[len - 1] = '\0';
236
	for (i = 0; i < argc; i++) {
237
		if (len == 0) {
238
			cmd_free_argv(argc, *argv);
239
			return (-1);
240
		}
241
242
		arglen = strlen(buf) + 1;
243
		(*argv)[i] = xstrdup(buf);
244
		buf += arglen;
245
		len -= arglen;
246
	}
247
248
	return (0);
249
}
250
251
char **
252
cmd_copy_argv(int argc, char **argv)
253
{
254
	char	**new_argv;
255
	int	  i;
256
257
20
	if (argc == 0)
258
5
		return (NULL);
259
5
	new_argv = xcalloc(argc + 1, sizeof *new_argv);
260
20
	for (i = 0; i < argc; i++) {
261
5
		if (argv[i] != NULL)
262
5
			new_argv[i] = xstrdup(argv[i]);
263
	}
264
5
	return (new_argv);
265
10
}
266
267
void
268
cmd_free_argv(int argc, char **argv)
269
{
270
	int	i;
271
272
20
	if (argc == 0)
273
5
		return;
274
20
	for (i = 0; i < argc; i++)
275
5
		free(argv[i]);
276
5
	free(argv);
277
15
}
278
279
char *
280
cmd_stringify_argv(int argc, char **argv)
281
{
282
	char	*buf;
283
	int	 i;
284
	size_t	 len;
285
286
	if (argc == 0)
287
		return (xstrdup(""));
288
289
	len = 0;
290
	buf = NULL;
291
292
	for (i = 0; i < argc; i++) {
293
		len += strlen(argv[i]) + 1;
294
		buf = xrealloc(buf, len);
295
296
		if (i == 0)
297
			*buf = '\0';
298
		else
299
			strlcat(buf, " ", len);
300
		strlcat(buf, argv[i], len);
301
	}
302
	return (buf);
303
}
304
305
static int
306
cmd_try_alias(int *argc, char ***argv)
307
{
308
	struct options_entry	 *o;
309
	int			  old_argc = *argc, new_argc;
310
	char			**old_argv = *argv, **new_argv;
311
	u_int			  size, idx;
312
	int			  i;
313
	size_t			  wanted;
314
	const char		 *s, *cp = NULL;
315
316
	o = options_get_only(global_options, "command-alias");
317
	if (o == NULL || options_array_size(o, &size) == -1 || size == 0)
318
		return (-1);
319
320
	wanted = strlen(old_argv[0]);
321
	for (idx = 0; idx < size; idx++) {
322
		s = options_array_get(o, idx);
323
		if (s == NULL)
324
			continue;
325
326
		cp = strchr(s, '=');
327
		if (cp == NULL || (size_t)(cp - s) != wanted)
328
			continue;
329
		if (strncmp(old_argv[0], s, wanted) == 0)
330
			break;
331
	}
332
	if (idx == size)
333
		return (-1);
334
335
	if (cmd_string_split(cp + 1, &new_argc, &new_argv) != 0)
336
		return (-1);
337
338
	*argc = new_argc + old_argc - 1;
339
	*argv = xcalloc((*argc) + 1, sizeof **argv);
340
341
	for (i = 0; i < new_argc; i++)
342
		(*argv)[i] = xstrdup(new_argv[i]);
343
	for (i = 1; i < old_argc; i++)
344
		(*argv)[new_argc + i - 1] = xstrdup(old_argv[i]);
345
346
	log_debug("alias: %s=%s", old_argv[0], cp + 1);
347
	for (i = 0; i < *argc; i++)
348
		log_debug("alias: argv[%d] = %s", i, (*argv)[i]);
349
350
	cmd_free_argv(new_argc, new_argv);
351
	return (0);
352
}
353
354
struct cmd *
355
cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause)
356
{
357
	const char		*name;
358
	const struct cmd_entry **entryp, *entry;
359
	struct cmd		*cmd;
360
	struct args		*args;
361
5
	char			 s[BUFSIZ];
362
	int			 ambiguous, allocated = 0;
363
364
5
	*cause = NULL;
365
5
	if (argc == 0) {
366
		xasprintf(cause, "no command");
367
		return (NULL);
368
	}
369
5
	name = argv[0];
370
371
retry:
372
	ambiguous = 0;
373
	entry = NULL;
374
10
	for (entryp = cmd_table; *entryp != NULL; entryp++) {
375

10
		if ((*entryp)->alias != NULL &&
376
5
		    strcmp((*entryp)->alias, argv[0]) == 0) {
377
			ambiguous = 0;
378
5
			entry = *entryp;
379
5
			break;
380
		}
381
382
		if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
383
			continue;
384
		if (entry != NULL)
385
			ambiguous = 1;
386
		entry = *entryp;
387
388
		/* Bail now if an exact match. */
389
		if (strcmp(entry->name, argv[0]) == 0)
390
			break;
391
	}
392

15
	if ((ambiguous || entry == NULL) &&
393
5
	    server_proc != NULL &&
394
5
	    !allocated &&
395
	    cmd_try_alias(&argc, &argv) == 0) {
396
		allocated = 1;
397
		goto retry;
398
	}
399
5
	if (ambiguous)
400
		goto ambiguous;
401
5
	if (entry == NULL) {
402
		xasprintf(cause, "unknown command: %s", name);
403
		return (NULL);
404
	}
405
406
5
	args = args_parse(entry->args.template, argc, argv);
407
5
	if (args == NULL)
408
		goto usage;
409

10
	if (entry->args.lower != -1 && args->argc < entry->args.lower)
410
		goto usage;
411

10
	if (entry->args.upper != -1 && args->argc > entry->args.upper)
412
		goto usage;
413
414
5
	cmd = xcalloc(1, sizeof *cmd);
415
5
	cmd->entry = entry;
416
5
	cmd->args = args;
417
418
5
	if (file != NULL)
419
		cmd->file = xstrdup(file);
420
5
	cmd->line = line;
421
422
5
	if (allocated)
423
		cmd_free_argv(argc, argv);
424
5
	return (cmd);
425
426
ambiguous:
427
	*s = '\0';
428
	for (entryp = cmd_table; *entryp != NULL; entryp++) {
429
		if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
430
			continue;
431
		if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s)
432
			break;
433
		if (strlcat(s, ", ", sizeof s) >= sizeof s)
434
			break;
435
	}
436
	s[strlen(s) - 2] = '\0';
437
	xasprintf(cause, "ambiguous command: %s, could be: %s", name, s);
438
	return (NULL);
439
440
usage:
441
	if (args != NULL)
442
		args_free(args);
443
	xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
444
	return (NULL);
445
5
}
446
447
char *
448
cmd_print(struct cmd *cmd)
449
{
450
	char	*out, *s;
451
452
	s = args_print(cmd->args);
453
	if (*s != '\0')
454
		xasprintf(&out, "%s %s", cmd->entry->name, s);
455
	else
456
		out = xstrdup(cmd->entry->name);
457
	free(s);
458
459
	return (out);
460
}
461
462
/* Adjust current mouse position for a pane. */
463
int
464
cmd_mouse_at(struct window_pane *wp, struct mouse_event *m, u_int *xp,
465
    u_int *yp, int last)
466
{
467
	u_int	x, y;
468
469
	if (last) {
470
		x = m->lx;
471
		y = m->ly;
472
	} else {
473
		x = m->x;
474
		y = m->y;
475
	}
476
477
	if (m->statusat == 0 && y > 0)
478
		y--;
479
	else if (m->statusat > 0 && y >= (u_int)m->statusat)
480
		y = m->statusat - 1;
481
482
	if (x < wp->xoff || x >= wp->xoff + wp->sx)
483
		return (-1);
484
	if (y < wp->yoff || y >= wp->yoff + wp->sy)
485
		return (-1);
486
487
	if (xp != NULL)
488
		*xp = x - wp->xoff;
489
	if (yp != NULL)
490
		*yp = y - wp->yoff;
491
	return (0);
492
}
493
494
/* Get current mouse window if any. */
495
struct winlink *
496
cmd_mouse_window(struct mouse_event *m, struct session **sp)
497
{
498
	struct session	*s;
499
	struct window	*w;
500
501
	if (!m->valid || m->s == -1 || m->w == -1)
502
		return (NULL);
503
	if ((s = session_find_by_id(m->s)) == NULL)
504
		return (NULL);
505
	if ((w = window_find_by_id(m->w)) == NULL)
506
		return (NULL);
507
508
	if (sp != NULL)
509
		*sp = s;
510
	return (winlink_find_by_window(&s->windows, w));
511
}
512
513
/* Get current mouse pane if any. */
514
struct window_pane *
515
cmd_mouse_pane(struct mouse_event *m, struct session **sp,
516
    struct winlink **wlp)
517
{
518
	struct winlink		*wl;
519
	struct window_pane     	*wp;
520
521
	if ((wl = cmd_mouse_window(m, sp)) == NULL)
522
		return (NULL);
523
	if ((wp = window_pane_find_by_id(m->wp)) == NULL)
524
		return (NULL);
525
	if (!window_has_pane(wl->window, wp))
526
		return (NULL);
527
528
	if (wlp != NULL)
529
		*wlp = wl;
530
	return (wp);
531
}
532
533
/* Replace the first %% or %idx in template by s. */
534
char *
535
cmd_template_replace(const char *template, const char *s, int idx)
536
{
537
	char		 ch, *buf;
538
	const char	*ptr, *cp, quote[] = "\"\\$";
539
	int		 replaced, quoted;
540
	size_t		 len;
541
542
	if (strchr(template, '%') == NULL)
543
		return (xstrdup(template));
544
545
	buf = xmalloc(1);
546
	*buf = '\0';
547
	len = 0;
548
	replaced = 0;
549
550
	ptr = template;
551
	while (*ptr != '\0') {
552
		switch (ch = *ptr++) {
553
		case '%':
554
			if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
555
				if (*ptr != '%' || replaced)
556
					break;
557
				replaced = 1;
558
			}
559
			ptr++;
560
561
			quoted = (*ptr == '%');
562
			if (quoted)
563
				ptr++;
564
565
			buf = xrealloc(buf, len + (strlen(s) * 3) + 1);
566
			for (cp = s; *cp != '\0'; cp++) {
567
				if (quoted && strchr(quote, *cp) != NULL)
568
					buf[len++] = '\\';
569
				if (quoted && *cp == ';') {
570
					buf[len++] = '\\';
571
					buf[len++] = '\\';
572
				}
573
				buf[len++] = *cp;
574
			}
575
			buf[len] = '\0';
576
			continue;
577
		}
578
		buf = xrealloc(buf, len + 2);
579
		buf[len++] = ch;
580
		buf[len] = '\0';
581
	}
582
583
	return (buf);
584
}