GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tmux/cmd.c Lines: 0 250 0.0 %
Date: 2016-12-06 Branches: 0 195 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: cmd.c,v 1.122 2016/03/01 12:06:07 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_session_entry;
38
extern const struct cmd_entry cmd_choose_tree_entry;
39
extern const struct cmd_entry cmd_choose_window_entry;
40
extern const struct cmd_entry cmd_clear_history_entry;
41
extern const struct cmd_entry cmd_clock_mode_entry;
42
extern const struct cmd_entry cmd_command_prompt_entry;
43
extern const struct cmd_entry cmd_confirm_before_entry;
44
extern const struct cmd_entry cmd_copy_mode_entry;
45
extern const struct cmd_entry cmd_delete_buffer_entry;
46
extern const struct cmd_entry cmd_detach_client_entry;
47
extern const struct cmd_entry cmd_display_message_entry;
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;
86
extern const struct cmd_entry cmd_respawn_pane_entry;
87
extern const struct cmd_entry cmd_respawn_window_entry;
88
extern const struct cmd_entry cmd_rotate_window_entry;
89
extern const struct cmd_entry cmd_run_shell_entry;
90
extern const struct cmd_entry cmd_save_buffer_entry;
91
extern const struct cmd_entry cmd_select_layout_entry;
92
extern const struct cmd_entry cmd_select_pane_entry;
93
extern const struct cmd_entry cmd_select_window_entry;
94
extern const struct cmd_entry cmd_send_keys_entry;
95
extern const struct cmd_entry cmd_send_prefix_entry;
96
extern const struct cmd_entry cmd_server_info_entry;
97
extern const struct cmd_entry cmd_set_buffer_entry;
98
extern const struct cmd_entry cmd_set_environment_entry;
99
extern const struct cmd_entry cmd_set_hook_entry;
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;
104
extern const struct cmd_entry cmd_show_hooks_entry;
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;
119
120
const struct cmd_entry *cmd_table[] = {
121
	&cmd_attach_session_entry,
122
	&cmd_bind_key_entry,
123
	&cmd_break_pane_entry,
124
	&cmd_capture_pane_entry,
125
	&cmd_choose_buffer_entry,
126
	&cmd_choose_client_entry,
127
	&cmd_choose_session_entry,
128
	&cmd_choose_tree_entry,
129
	&cmd_choose_window_entry,
130
	&cmd_clear_history_entry,
131
	&cmd_clock_mode_entry,
132
	&cmd_command_prompt_entry,
133
	&cmd_confirm_before_entry,
134
	&cmd_copy_mode_entry,
135
	&cmd_delete_buffer_entry,
136
	&cmd_detach_client_entry,
137
	&cmd_display_message_entry,
138
	&cmd_display_panes_entry,
139
	&cmd_find_window_entry,
140
	&cmd_has_session_entry,
141
	&cmd_if_shell_entry,
142
	&cmd_join_pane_entry,
143
	&cmd_kill_pane_entry,
144
	&cmd_kill_server_entry,
145
	&cmd_kill_session_entry,
146
	&cmd_kill_window_entry,
147
	&cmd_last_pane_entry,
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,
154
	&cmd_list_panes_entry,
155
	&cmd_list_sessions_entry,
156
	&cmd_list_windows_entry,
157
	&cmd_load_buffer_entry,
158
	&cmd_lock_client_entry,
159
	&cmd_lock_server_entry,
160
	&cmd_lock_session_entry,
161
	&cmd_move_pane_entry,
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,
168
	&cmd_pipe_pane_entry,
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,
175
	&cmd_respawn_pane_entry,
176
	&cmd_respawn_window_entry,
177
	&cmd_rotate_window_entry,
178
	&cmd_run_shell_entry,
179
	&cmd_save_buffer_entry,
180
	&cmd_select_layout_entry,
181
	&cmd_select_pane_entry,
182
	&cmd_select_window_entry,
183
	&cmd_send_keys_entry,
184
	&cmd_send_prefix_entry,
185
	&cmd_server_info_entry,
186
	&cmd_set_buffer_entry,
187
	&cmd_set_environment_entry,
188
	&cmd_set_hook_entry,
189
	&cmd_set_option_entry,
190
	&cmd_set_window_option_entry,
191
	&cmd_show_buffer_entry,
192
	&cmd_show_environment_entry,
193
	&cmd_show_hooks_entry,
194
	&cmd_show_messages_entry,
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,
206
	&cmd_wait_for_entry,
207
	NULL
208
};
209
210
int
211
cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
212
{
213
	size_t	arglen;
214
	int	i;
215
216
	if (argc == 0)
217
		return (0);
218
219
	*buf = '\0';
220
	for (i = 0; i < argc; i++) {
221
		if (strlcpy(buf, argv[i], len) >= len)
222
			return (-1);
223
		arglen = strlen(argv[i]) + 1;
224
		buf += arglen;
225
		len -= arglen;
226
	}
227
228
	return (0);
229
}
230
231
int
232
cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
233
{
234
	int	i;
235
	size_t	arglen;
236
237
	if (argc == 0)
238
		return (0);
239
	*argv = xcalloc(argc, sizeof **argv);
240
241
	buf[len - 1] = '\0';
242
	for (i = 0; i < argc; i++) {
243
		if (len == 0) {
244
			cmd_free_argv(argc, *argv);
245
			return (-1);
246
		}
247
248
		arglen = strlen(buf) + 1;
249
		(*argv)[i] = xstrdup(buf);
250
		buf += arglen;
251
		len -= arglen;
252
	}
253
254
	return (0);
255
}
256
257
char **
258
cmd_copy_argv(int argc, char **argv)
259
{
260
	char	**new_argv;
261
	int	  i;
262
263
	if (argc == 0)
264
		return (NULL);
265
	new_argv = xcalloc(argc + 1, sizeof *new_argv);
266
	for (i = 0; i < argc; i++) {
267
		if (argv[i] != NULL)
268
			new_argv[i] = xstrdup(argv[i]);
269
	}
270
	return (new_argv);
271
}
272
273
void
274
cmd_free_argv(int argc, char **argv)
275
{
276
	int	i;
277
278
	if (argc == 0)
279
		return;
280
	for (i = 0; i < argc; i++)
281
		free(argv[i]);
282
	free(argv);
283
}
284
285
char *
286
cmd_stringify_argv(int argc, char **argv)
287
{
288
	char	*buf;
289
	int	 i;
290
	size_t	 len;
291
292
	if (argc == 0)
293
		return (xstrdup(""));
294
295
	len = 0;
296
	buf = NULL;
297
298
	for (i = 0; i < argc; i++) {
299
		len += strlen(argv[i]) + 1;
300
		buf = xrealloc(buf, len);
301
302
		if (i == 0)
303
			*buf = '\0';
304
		else
305
			strlcat(buf, " ", len);
306
		strlcat(buf, argv[i], len);
307
	}
308
	return (buf);
309
}
310
311
struct cmd *
312
cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause)
313
{
314
	const struct cmd_entry **entryp, *entry;
315
	struct cmd		*cmd;
316
	struct args		*args;
317
	char			 s[BUFSIZ];
318
	int			 ambiguous = 0;
319
320
	*cause = NULL;
321
	if (argc == 0) {
322
		xasprintf(cause, "no command");
323
		return (NULL);
324
	}
325
326
	entry = NULL;
327
	for (entryp = cmd_table; *entryp != NULL; entryp++) {
328
		if ((*entryp)->alias != NULL &&
329
		    strcmp((*entryp)->alias, argv[0]) == 0) {
330
			ambiguous = 0;
331
			entry = *entryp;
332
			break;
333
		}
334
335
		if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
336
			continue;
337
		if (entry != NULL)
338
			ambiguous = 1;
339
		entry = *entryp;
340
341
		/* Bail now if an exact match. */
342
		if (strcmp(entry->name, argv[0]) == 0)
343
			break;
344
	}
345
	if (ambiguous)
346
		goto ambiguous;
347
	if (entry == NULL) {
348
		xasprintf(cause, "unknown command: %s", argv[0]);
349
		return (NULL);
350
	}
351
352
	args = args_parse(entry->args.template, argc, argv);
353
	if (args == NULL)
354
		goto usage;
355
	if (entry->args.lower != -1 && args->argc < entry->args.lower)
356
		goto usage;
357
	if (entry->args.upper != -1 && args->argc > entry->args.upper)
358
		goto usage;
359
360
	cmd = xcalloc(1, sizeof *cmd);
361
	cmd->entry = entry;
362
	cmd->args = args;
363
364
	if (file != NULL)
365
		cmd->file = xstrdup(file);
366
	cmd->line = line;
367
368
	return (cmd);
369
370
ambiguous:
371
	*s = '\0';
372
	for (entryp = cmd_table; *entryp != NULL; entryp++) {
373
		if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
374
			continue;
375
		if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s)
376
			break;
377
		if (strlcat(s, ", ", sizeof s) >= sizeof s)
378
			break;
379
	}
380
	s[strlen(s) - 2] = '\0';
381
	xasprintf(cause, "ambiguous command: %s, could be: %s", argv[0], s);
382
	return (NULL);
383
384
usage:
385
	if (args != NULL)
386
		args_free(args);
387
	xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
388
	return (NULL);
389
}
390
391
static int
392
cmd_prepare_state_flag(char c, const char *target, enum cmd_entry_flag flag,
393
    struct cmd_q *cmdq, struct cmd_q *parent)
394
{
395
	int			 targetflags, error;
396
	struct cmd_find_state	*fs = NULL;
397
	struct cmd_find_state	*current = NULL;
398
	struct cmd_find_state	 tmp;
399
400
	if (flag == CMD_NONE ||
401
	    flag == CMD_CLIENT ||
402
	    flag == CMD_CLIENT_CANFAIL)
403
		return (0);
404
405
	if (c == 't')
406
		fs = &cmdq->state.tflag;
407
	else if (c == 's')
408
		fs = &cmdq->state.sflag;
409
410
	if (flag == CMD_SESSION_WITHPANE) {
411
		if (target != NULL && target[strcspn(target, ":.")] != '\0')
412
			flag = CMD_PANE;
413
		else
414
			flag = CMD_SESSION;
415
	}
416
417
	targetflags = 0;
418
	switch (flag) {
419
	case CMD_SESSION:
420
	case CMD_SESSION_CANFAIL:
421
	case CMD_SESSION_PREFERUNATTACHED:
422
		if (flag == CMD_SESSION_CANFAIL)
423
			targetflags |= CMD_FIND_QUIET;
424
		if (flag == CMD_SESSION_PREFERUNATTACHED)
425
			targetflags |= CMD_FIND_PREFER_UNATTACHED;
426
		break;
427
	case CMD_MOVEW_R:
428
		flag = CMD_WINDOW_INDEX;
429
		/* FALLTHROUGH */
430
	case CMD_WINDOW:
431
	case CMD_WINDOW_CANFAIL:
432
	case CMD_WINDOW_MARKED:
433
	case CMD_WINDOW_INDEX:
434
		if (flag == CMD_WINDOW_CANFAIL)
435
			targetflags |= CMD_FIND_QUIET;
436
		if (flag == CMD_WINDOW_MARKED)
437
			targetflags |= CMD_FIND_DEFAULT_MARKED;
438
		if (flag == CMD_WINDOW_INDEX)
439
			targetflags |= CMD_FIND_WINDOW_INDEX;
440
		break;
441
	case CMD_PANE:
442
	case CMD_PANE_CANFAIL:
443
	case CMD_PANE_MARKED:
444
		if (flag == CMD_PANE_CANFAIL)
445
			targetflags |= CMD_FIND_QUIET;
446
		if (flag == CMD_PANE_MARKED)
447
			targetflags |= CMD_FIND_DEFAULT_MARKED;
448
		break;
449
	default:
450
		fatalx("unknown %cflag %d", c, flag);
451
	}
452
453
	log_debug("%s: flag %c %d %#x", __func__, c, flag, targetflags);
454
	if (parent != NULL) {
455
		if (c == 't')
456
			current = &parent->state.tflag;
457
		else if (c == 's')
458
			current = &parent->state.sflag;
459
	} else {
460
		error = cmd_find_current(&tmp, cmdq, targetflags);
461
		if (error != 0 && ~targetflags & CMD_FIND_QUIET)
462
			return (-1);
463
		current = &tmp;
464
	}
465
466
	switch (flag) {
467
	case CMD_NONE:
468
	case CMD_CLIENT:
469
	case CMD_CLIENT_CANFAIL:
470
		return (0);
471
	case CMD_SESSION:
472
	case CMD_SESSION_CANFAIL:
473
	case CMD_SESSION_PREFERUNATTACHED:
474
	case CMD_SESSION_WITHPANE:
475
		error = cmd_find_target(fs, current, cmdq, target,
476
		    CMD_FIND_SESSION, targetflags);
477
		if (error != 0 && ~targetflags & CMD_FIND_QUIET)
478
			return (-1);
479
		break;
480
	case CMD_MOVEW_R:
481
		error = cmd_find_target(fs, current, cmdq, target,
482
		    CMD_FIND_SESSION, CMD_FIND_QUIET);
483
		if (error == 0)
484
			break;
485
		/* FALLTHROUGH */
486
	case CMD_WINDOW:
487
	case CMD_WINDOW_CANFAIL:
488
	case CMD_WINDOW_MARKED:
489
	case CMD_WINDOW_INDEX:
490
		error = cmd_find_target(fs, current, cmdq, target,
491
		    CMD_FIND_WINDOW, targetflags);
492
		if (error != 0 && ~targetflags & CMD_FIND_QUIET)
493
			return (-1);
494
		break;
495
	case CMD_PANE:
496
	case CMD_PANE_CANFAIL:
497
	case CMD_PANE_MARKED:
498
		error = cmd_find_target(fs, current, cmdq, target,
499
		    CMD_FIND_PANE, targetflags);
500
		if (error != 0 && ~targetflags & CMD_FIND_QUIET)
501
			return (-1);
502
		break;
503
	default:
504
		fatalx("unknown %cflag %d", c, flag);
505
	}
506
	return (0);
507
}
508
509
int
510
cmd_prepare_state(struct cmd *cmd, struct cmd_q *cmdq, struct cmd_q *parent)
511
{
512
	const struct cmd_entry		*entry = cmd->entry;
513
	struct cmd_state		*state = &cmdq->state;
514
	char				*tmp;
515
	enum cmd_entry_flag		 flag;
516
	const char			*s;
517
	int				 error;
518
519
	tmp = cmd_print(cmd);
520
	log_debug("preparing state for %s (client %p)", tmp, cmdq->client);
521
	free(tmp);
522
523
	state->c = NULL;
524
	cmd_find_clear_state(&state->tflag, NULL, 0);
525
	cmd_find_clear_state(&state->sflag, NULL, 0);
526
527
	flag = cmd->entry->cflag;
528
	if (flag == CMD_NONE) {
529
		flag = cmd->entry->tflag;
530
		if (flag == CMD_CLIENT || flag == CMD_CLIENT_CANFAIL)
531
			s = args_get(cmd->args, 't');
532
		else
533
			s = NULL;
534
	} else
535
		s = args_get(cmd->args, 'c');
536
	switch (flag) {
537
	case CMD_CLIENT:
538
		state->c = cmd_find_client(cmdq, s, 0);
539
		if (state->c == NULL)
540
			return (-1);
541
		break;
542
	default:
543
		state->c = cmd_find_client(cmdq, s, 1);
544
		break;
545
	}
546
547
	s = args_get(cmd->args, 't');
548
	log_debug("preparing -t state: target %s", s == NULL ? "none" : s);
549
550
	error = cmd_prepare_state_flag('t', s, entry->tflag, cmdq, parent);
551
	if (error != 0)
552
		return (error);
553
554
	s = args_get(cmd->args, 's');
555
	log_debug("preparing -s state: target %s", s == NULL ? "none" : s);
556
557
	error = cmd_prepare_state_flag('s', s, entry->sflag, cmdq, parent);
558
	if (error != 0)
559
		return (error);
560
561
	return (0);
562
}
563
564
char *
565
cmd_print(struct cmd *cmd)
566
{
567
	char	*out, *s;
568
569
	s = args_print(cmd->args);
570
	if (*s != '\0')
571
		xasprintf(&out, "%s %s", cmd->entry->name, s);
572
	else
573
		out = xstrdup(cmd->entry->name);
574
	free(s);
575
576
	return (out);
577
}
578
579
/* Adjust current mouse position for a pane. */
580
int
581
cmd_mouse_at(struct window_pane *wp, struct mouse_event *m, u_int *xp,
582
    u_int *yp, int last)
583
{
584
	u_int	x, y;
585
586
	if (last) {
587
		x = m->lx;
588
		y = m->ly;
589
	} else {
590
		x = m->x;
591
		y = m->y;
592
	}
593
594
	if (m->statusat == 0 && y > 0)
595
		y--;
596
	else if (m->statusat > 0 && y >= (u_int)m->statusat)
597
		y = m->statusat - 1;
598
599
	if (x < wp->xoff || x >= wp->xoff + wp->sx)
600
		return (-1);
601
	if (y < wp->yoff || y >= wp->yoff + wp->sy)
602
		return (-1);
603
604
	*xp = x - wp->xoff;
605
	*yp = y - wp->yoff;
606
	return (0);
607
}
608
609
/* Get current mouse window if any. */
610
struct winlink *
611
cmd_mouse_window(struct mouse_event *m, struct session **sp)
612
{
613
	struct session	*s;
614
	struct window	*w;
615
616
	if (!m->valid || m->s == -1 || m->w == -1)
617
		return (NULL);
618
	if ((s = session_find_by_id(m->s)) == NULL)
619
		return (NULL);
620
	if ((w = window_find_by_id(m->w)) == NULL)
621
		return (NULL);
622
623
	if (sp != NULL)
624
		*sp = s;
625
	return (winlink_find_by_window(&s->windows, w));
626
}
627
628
/* Get current mouse pane if any. */
629
struct window_pane *
630
cmd_mouse_pane(struct mouse_event *m, struct session **sp,
631
    struct winlink **wlp)
632
{
633
	struct winlink		*wl;
634
	struct window_pane     	*wp;
635
636
	if ((wl = cmd_mouse_window(m, sp)) == NULL)
637
		return (NULL);
638
	if ((wp = window_pane_find_by_id(m->wp)) == NULL)
639
		return (NULL);
640
	if (!window_has_pane(wl->window, wp))
641
		return (NULL);
642
643
	if (wlp != NULL)
644
		*wlp = wl;
645
	return (wp);
646
}
647
648
/* Replace the first %% or %idx in template by s. */
649
char *
650
cmd_template_replace(const char *template, const char *s, int idx)
651
{
652
	char		 ch, *buf;
653
	const char	*ptr;
654
	int		 replaced;
655
	size_t		 len;
656
657
	if (strchr(template, '%') == NULL)
658
		return (xstrdup(template));
659
660
	buf = xmalloc(1);
661
	*buf = '\0';
662
	len = 0;
663
	replaced = 0;
664
665
	ptr = template;
666
	while (*ptr != '\0') {
667
		switch (ch = *ptr++) {
668
		case '%':
669
			if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
670
				if (*ptr != '%' || replaced)
671
					break;
672
				replaced = 1;
673
			}
674
			ptr++;
675
676
			len += strlen(s);
677
			buf = xrealloc(buf, len + 1);
678
			strlcat(buf, s, len + 1);
679
			continue;
680
		}
681
		buf = xrealloc(buf, len + 2);
682
		buf[len++] = ch;
683
		buf[len] = '\0';
684
	}
685
686
	return (buf);
687
}