status.c revision 1.154
1/* $OpenBSD: status.c,v 1.154 2016/10/12 13:03:27 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 <errno.h>
23#include <limits.h>
24#include <stdarg.h>
25#include <stdlib.h>
26#include <string.h>
27#include <time.h>
28#include <unistd.h>
29
30#include "tmux.h"
31
32static char	*status_redraw_get_left(struct client *, time_t,
33		     struct grid_cell *, size_t *);
34static char	*status_redraw_get_right(struct client *, time_t,
35		     struct grid_cell *, size_t *);
36static char	*status_print(struct client *, struct winlink *, time_t,
37		     struct grid_cell *);
38static char	*status_replace(struct client *, struct winlink *, const char *,
39		     time_t);
40static void	 status_message_callback(int, short, void *);
41static void	 status_timer_callback(int, short, void *);
42
43static char	*status_prompt_find_history_file(void);
44static const char *status_prompt_up_history(u_int *);
45static const char *status_prompt_down_history(u_int *);
46static void	 status_prompt_add_history(const char *);
47
48static const char **status_prompt_complete_list(u_int *, const char *);
49static char	*status_prompt_complete_prefix(const char **, u_int);
50static char	*status_prompt_complete(struct session *, const char *);
51
52/* Status prompt history. */
53#define PROMPT_HISTORY 100
54static char	**status_prompt_hlist;
55static u_int	  status_prompt_hsize;
56
57/* Find the history file to load/save from/to. */
58static char *
59status_prompt_find_history_file(void)
60{
61	const char	*home, *history_file;
62	char		*path;
63
64	history_file = options_get_string(global_options, "history-file");
65	if (*history_file == '\0')
66		return (NULL);
67	if (*history_file == '/')
68		return (xstrdup(history_file));
69
70	if (history_file[0] != '~' || history_file[1] != '/')
71		return (NULL);
72	if ((home = find_home()) == NULL)
73		return (NULL);
74	xasprintf(&path, "%s%s", home, history_file + 1);
75	return (path);
76}
77
78/* Load status prompt history from file. */
79void
80status_prompt_load_history(void)
81{
82	FILE	*f;
83	char	*history_file, *line, *tmp;
84	size_t	 length;
85
86	if ((history_file = status_prompt_find_history_file()) == NULL)
87		return;
88	log_debug("loading history from %s", history_file);
89
90	f = fopen(history_file, "r");
91	if (f == NULL) {
92		log_debug("%s: %s", history_file, strerror(errno));
93		free(history_file);
94		return;
95	}
96	free(history_file);
97
98	for (;;) {
99		if ((line = fgetln(f, &length)) == NULL)
100			break;
101
102		if (length > 0) {
103			if (line[length - 1] == '\n') {
104				line[length - 1] = '\0';
105				status_prompt_add_history(line);
106			} else {
107				tmp = xmalloc(length + 1);
108				memcpy(tmp, line, length);
109				tmp[length] = '\0';
110				status_prompt_add_history(tmp);
111				free(tmp);
112			}
113		}
114	}
115	fclose(f);
116}
117
118/* Save status prompt history to file. */
119void
120status_prompt_save_history(void)
121{
122	FILE	*f;
123	u_int	 i;
124	char	*history_file;
125
126	if ((history_file = status_prompt_find_history_file()) == NULL)
127		return;
128	log_debug("saving history to %s", history_file);
129
130	f = fopen(history_file, "w");
131	if (f == NULL) {
132		log_debug("%s: %s", history_file, strerror(errno));
133		free(history_file);
134		return;
135	}
136	free(history_file);
137
138	for (i = 0; i < status_prompt_hsize; i++) {
139		fputs(status_prompt_hlist[i], f);
140		fputc('\n', f);
141	}
142	fclose(f);
143
144}
145
146/* Status timer callback. */
147static void
148status_timer_callback(__unused int fd, __unused short events, void *arg)
149{
150	struct client	*c = arg;
151	struct session	*s = c->session;
152	struct timeval	 tv;
153
154	evtimer_del(&c->status_timer);
155
156	if (s == NULL)
157		return;
158
159	if (c->message_string == NULL && c->prompt_string == NULL)
160		c->flags |= CLIENT_STATUS;
161
162	timerclear(&tv);
163	tv.tv_sec = options_get_number(s->options, "status-interval");
164
165	if (tv.tv_sec != 0)
166		evtimer_add(&c->status_timer, &tv);
167	log_debug("client %p, status interval %d", c, (int)tv.tv_sec);
168}
169
170/* Start status timer for client. */
171void
172status_timer_start(struct client *c)
173{
174	struct session	*s = c->session;
175
176	if (event_initialized(&c->status_timer))
177		evtimer_del(&c->status_timer);
178	else
179		evtimer_set(&c->status_timer, status_timer_callback, c);
180
181	if (s != NULL && options_get_number(s->options, "status"))
182		status_timer_callback(-1, 0, c);
183}
184
185/* Start status timer for all clients. */
186void
187status_timer_start_all(void)
188{
189	struct client	*c;
190
191	TAILQ_FOREACH(c, &clients, entry)
192		status_timer_start(c);
193}
194
195/* Get screen line of status line. -1 means off. */
196int
197status_at_line(struct client *c)
198{
199	struct session	*s = c->session;
200
201	if (!options_get_number(s->options, "status"))
202		return (-1);
203
204	if (options_get_number(s->options, "status-position") == 0)
205		return (0);
206	return (c->tty.sy - 1);
207}
208
209/* Retrieve options for left string. */
210static char *
211status_redraw_get_left(struct client *c, time_t t, struct grid_cell *gc,
212    size_t *size)
213{
214	struct session	*s = c->session;
215	const char	*template;
216	char		*left;
217	size_t		 leftlen;
218
219	style_apply_update(gc, s->options, "status-left-style");
220
221	template = options_get_string(s->options, "status-left");
222	left = status_replace(c, NULL, template, t);
223
224	*size = options_get_number(s->options, "status-left-length");
225	leftlen = screen_write_cstrlen("%s", left);
226	if (leftlen < *size)
227		*size = leftlen;
228	return (left);
229}
230
231/* Retrieve options for right string. */
232static char *
233status_redraw_get_right(struct client *c, time_t t, struct grid_cell *gc,
234    size_t *size)
235{
236	struct session	*s = c->session;
237	const char	*template;
238	char		*right;
239	size_t		 rightlen;
240
241	style_apply_update(gc, s->options, "status-right-style");
242
243	template = options_get_string(s->options, "status-right");
244	right = status_replace(c, NULL, template, t);
245
246	*size = options_get_number(s->options, "status-right-length");
247	rightlen = screen_write_cstrlen("%s", right);
248	if (rightlen < *size)
249		*size = rightlen;
250	return (right);
251}
252
253/* Get window at window list position. */
254struct window *
255status_get_window_at(struct client *c, u_int x)
256{
257	struct session	*s = c->session;
258	struct winlink	*wl;
259	struct options	*oo;
260	const char	*sep;
261	size_t		 seplen;
262
263	x += c->wlmouse;
264	RB_FOREACH(wl, winlinks, &s->windows) {
265		oo = wl->window->options;
266
267		sep = options_get_string(oo, "window-status-separator");
268		seplen = screen_write_cstrlen("%s", sep);
269
270		if (x < wl->status_width)
271			return (wl->window);
272		x -= wl->status_width + seplen;
273	}
274	return (NULL);
275}
276
277/* Draw status for client on the last lines of given context. */
278int
279status_redraw(struct client *c)
280{
281	struct screen_write_ctx	ctx;
282	struct session	       *s = c->session;
283	struct winlink	       *wl;
284	struct screen		old_status, window_list;
285	struct grid_cell	stdgc, lgc, rgc, gc;
286	struct options	       *oo;
287	time_t			t;
288	char		       *left, *right, *sep;
289	u_int			offset, needed;
290	u_int			wlstart, wlwidth, wlavailable, wloffset, wlsize;
291	size_t			llen, rlen, seplen;
292	int			larrow, rarrow;
293
294	/* No status line? */
295	if (c->tty.sy == 0 || !options_get_number(s->options, "status"))
296		return (1);
297	left = right = NULL;
298	larrow = rarrow = 0;
299
300	/* Store current time. */
301	t = time(NULL);
302
303	/* Set up default colour. */
304	style_apply(&stdgc, s->options, "status-style");
305
306	/* Create the target screen. */
307	memcpy(&old_status, &c->status, sizeof old_status);
308	screen_init(&c->status, c->tty.sx, 1, 0);
309	screen_write_start(&ctx, NULL, &c->status);
310	for (offset = 0; offset < c->tty.sx; offset++)
311		screen_write_putc(&ctx, &stdgc, ' ');
312	screen_write_stop(&ctx);
313
314	/* If the height is one line, blank status line. */
315	if (c->tty.sy <= 1)
316		goto out;
317
318	/* Work out left and right strings. */
319	memcpy(&lgc, &stdgc, sizeof lgc);
320	left = status_redraw_get_left(c, t, &lgc, &llen);
321	memcpy(&rgc, &stdgc, sizeof rgc);
322	right = status_redraw_get_right(c, t, &rgc, &rlen);
323
324	/*
325	 * Figure out how much space we have for the window list. If there
326	 * isn't enough space, just show a blank status line.
327	 */
328	needed = 0;
329	if (llen != 0)
330		needed += llen;
331	if (rlen != 0)
332		needed += rlen;
333	if (c->tty.sx == 0 || c->tty.sx <= needed)
334		goto out;
335	wlavailable = c->tty.sx - needed;
336
337	/* Calculate the total size needed for the window list. */
338	wlstart = wloffset = wlwidth = 0;
339	RB_FOREACH(wl, winlinks, &s->windows) {
340		free(wl->status_text);
341		memcpy(&wl->status_cell, &stdgc, sizeof wl->status_cell);
342		wl->status_text = status_print(c, wl, t, &wl->status_cell);
343		wl->status_width = screen_write_cstrlen("%s", wl->status_text);
344
345		if (wl == s->curw)
346			wloffset = wlwidth;
347
348		oo = wl->window->options;
349		sep = options_get_string(oo, "window-status-separator");
350		seplen = screen_write_cstrlen("%s", sep);
351		wlwidth += wl->status_width + seplen;
352	}
353
354	/* Create a new screen for the window list. */
355	screen_init(&window_list, wlwidth, 1, 0);
356
357	/* And draw the window list into it. */
358	screen_write_start(&ctx, NULL, &window_list);
359	RB_FOREACH(wl, winlinks, &s->windows) {
360		screen_write_cnputs(&ctx, -1, &wl->status_cell, "%s",
361		    wl->status_text);
362
363		oo = wl->window->options;
364		sep = options_get_string(oo, "window-status-separator");
365		screen_write_cnputs(&ctx, -1, &stdgc, "%s", sep);
366	}
367	screen_write_stop(&ctx);
368
369	/* If there is enough space for the total width, skip to draw now. */
370	if (wlwidth <= wlavailable)
371		goto draw;
372
373	/* Find size of current window text. */
374	wlsize = s->curw->status_width;
375
376	/*
377	 * If the current window is already on screen, good to draw from the
378	 * start and just leave off the end.
379	 */
380	if (wloffset + wlsize < wlavailable) {
381		if (wlavailable > 0) {
382			rarrow = 1;
383			wlavailable--;
384		}
385		wlwidth = wlavailable;
386	} else {
387		/*
388		 * Work out how many characters we need to omit from the
389		 * start. There are wlavailable characters to fill, and
390		 * wloffset + wlsize must be the last. So, the start character
391		 * is wloffset + wlsize - wlavailable.
392		 */
393		if (wlavailable > 0) {
394			larrow = 1;
395			wlavailable--;
396		}
397
398		wlstart = wloffset + wlsize - wlavailable;
399		if (wlavailable > 0 && wlwidth > wlstart + wlavailable + 1) {
400			rarrow = 1;
401			wlstart++;
402			wlavailable--;
403		}
404		wlwidth = wlavailable;
405	}
406
407	/* Bail if anything is now too small too. */
408	if (wlwidth == 0 || wlavailable == 0) {
409		screen_free(&window_list);
410		goto out;
411	}
412
413	/*
414	 * Now the start position is known, work out the state of the left and
415	 * right arrows.
416	 */
417	offset = 0;
418	RB_FOREACH(wl, winlinks, &s->windows) {
419		if (wl->flags & WINLINK_ALERTFLAGS &&
420		    larrow == 1 && offset < wlstart)
421			larrow = -1;
422
423		offset += wl->status_width;
424
425		if (wl->flags & WINLINK_ALERTFLAGS &&
426		    rarrow == 1 && offset > wlstart + wlwidth)
427			rarrow = -1;
428	}
429
430draw:
431	/* Begin drawing. */
432	screen_write_start(&ctx, NULL, &c->status);
433
434	/* Draw the left string and arrow. */
435	screen_write_cursormove(&ctx, 0, 0);
436	if (llen != 0)
437		screen_write_cnputs(&ctx, llen, &lgc, "%s", left);
438	if (larrow != 0) {
439		memcpy(&gc, &stdgc, sizeof gc);
440		if (larrow == -1)
441			gc.attr ^= GRID_ATTR_REVERSE;
442		screen_write_putc(&ctx, &gc, '<');
443	}
444
445	/* Draw the right string and arrow. */
446	if (rarrow != 0) {
447		screen_write_cursormove(&ctx, c->tty.sx - rlen - 1, 0);
448		memcpy(&gc, &stdgc, sizeof gc);
449		if (rarrow == -1)
450			gc.attr ^= GRID_ATTR_REVERSE;
451		screen_write_putc(&ctx, &gc, '>');
452	} else
453		screen_write_cursormove(&ctx, c->tty.sx - rlen, 0);
454	if (rlen != 0)
455		screen_write_cnputs(&ctx, rlen, &rgc, "%s", right);
456
457	/* Figure out the offset for the window list. */
458	if (llen != 0)
459		wloffset = llen;
460	else
461		wloffset = 0;
462	if (wlwidth < wlavailable) {
463		switch (options_get_number(s->options, "status-justify")) {
464		case 1:	/* centred */
465			wloffset += (wlavailable - wlwidth) / 2;
466			break;
467		case 2:	/* right */
468			wloffset += (wlavailable - wlwidth);
469			break;
470		}
471	}
472	if (larrow != 0)
473		wloffset++;
474
475	/* Copy the window list. */
476	c->wlmouse = -wloffset + wlstart;
477	screen_write_cursormove(&ctx, wloffset, 0);
478	screen_write_copy(&ctx, &window_list, wlstart, 0, wlwidth, 1);
479	screen_free(&window_list);
480
481	screen_write_stop(&ctx);
482
483out:
484	free(left);
485	free(right);
486
487	if (grid_compare(c->status.grid, old_status.grid) == 0) {
488		screen_free(&old_status);
489		return (0);
490	}
491	screen_free(&old_status);
492	return (1);
493}
494
495/* Replace special sequences in fmt. */
496static char *
497status_replace(struct client *c, struct winlink *wl, const char *fmt, time_t t)
498{
499	struct format_tree	*ft;
500	char			*expanded;
501
502	if (fmt == NULL)
503		return (xstrdup(""));
504
505	if (c->flags & CLIENT_STATUSFORCE)
506		ft = format_create(NULL, FORMAT_STATUS|FORMAT_FORCE);
507	else
508		ft = format_create(NULL, FORMAT_STATUS);
509	format_defaults(ft, c, NULL, wl, NULL);
510
511	expanded = format_expand_time(ft, fmt, t);
512
513	format_free(ft);
514	return (expanded);
515}
516
517/* Return winlink status line entry and adjust gc as necessary. */
518static char *
519status_print(struct client *c, struct winlink *wl, time_t t,
520    struct grid_cell *gc)
521{
522	struct options	*oo = wl->window->options;
523	struct session	*s = c->session;
524	const char	*fmt;
525	char   		*text;
526
527	style_apply_update(gc, oo, "window-status-style");
528	fmt = options_get_string(oo, "window-status-format");
529	if (wl == s->curw) {
530		style_apply_update(gc, oo, "window-status-current-style");
531		fmt = options_get_string(oo, "window-status-current-format");
532	}
533	if (wl == TAILQ_FIRST(&s->lastw))
534		style_apply_update(gc, oo, "window-status-last-style");
535
536	if (wl->flags & WINLINK_BELL)
537		style_apply_update(gc, oo, "window-status-bell-style");
538	else if (wl->flags & (WINLINK_ACTIVITY|WINLINK_SILENCE))
539		style_apply_update(gc, oo, "window-status-activity-style");
540
541	text = status_replace(c, wl, fmt, t);
542	return (text);
543}
544
545/* Set a status line message. */
546void
547status_message_set(struct client *c, const char *fmt, ...)
548{
549	struct timeval		 tv;
550	struct message_entry	*msg, *msg1;
551	va_list			 ap;
552	int			 delay;
553	u_int			 limit;
554
555	limit = options_get_number(global_options, "message-limit");
556
557	status_prompt_clear(c);
558	status_message_clear(c);
559
560	va_start(ap, fmt);
561	xvasprintf(&c->message_string, fmt, ap);
562	va_end(ap);
563
564	msg = xcalloc(1, sizeof *msg);
565	msg->msg_time = time(NULL);
566	msg->msg_num = c->message_next++;
567	msg->msg = xstrdup(c->message_string);
568	TAILQ_INSERT_TAIL(&c->message_log, msg, entry);
569
570	TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
571		if (msg->msg_num + limit >= c->message_next)
572			break;
573		free(msg->msg);
574		TAILQ_REMOVE(&c->message_log, msg, entry);
575		free(msg);
576	}
577
578	delay = options_get_number(c->session->options, "display-time");
579	if (delay > 0) {
580		tv.tv_sec = delay / 1000;
581		tv.tv_usec = (delay % 1000) * 1000L;
582
583		if (event_initialized(&c->message_timer))
584			evtimer_del(&c->message_timer);
585		evtimer_set(&c->message_timer, status_message_callback, c);
586		evtimer_add(&c->message_timer, &tv);
587	}
588
589	c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
590	c->flags |= CLIENT_STATUS;
591}
592
593/* Clear status line message. */
594void
595status_message_clear(struct client *c)
596{
597	if (c->message_string == NULL)
598		return;
599
600	free(c->message_string);
601	c->message_string = NULL;
602
603	c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
604	c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */
605
606	screen_reinit(&c->status);
607}
608
609/* Clear status line message after timer expires. */
610static void
611status_message_callback(__unused int fd, __unused short event, void *data)
612{
613	struct client	*c = data;
614
615	status_message_clear(c);
616}
617
618/* Draw client message on status line of present else on last line. */
619int
620status_message_redraw(struct client *c)
621{
622	struct screen_write_ctx		ctx;
623	struct session		       *s = c->session;
624	struct screen		        old_status;
625	size_t			        len;
626	struct grid_cell		gc;
627
628	if (c->tty.sx == 0 || c->tty.sy == 0)
629		return (0);
630	memcpy(&old_status, &c->status, sizeof old_status);
631	screen_init(&c->status, c->tty.sx, 1, 0);
632
633	len = screen_write_strlen("%s", c->message_string);
634	if (len > c->tty.sx)
635		len = c->tty.sx;
636
637	style_apply(&gc, s->options, "message-style");
638
639	screen_write_start(&ctx, NULL, &c->status);
640
641	screen_write_cursormove(&ctx, 0, 0);
642	screen_write_nputs(&ctx, len, &gc, "%s", c->message_string);
643	for (; len < c->tty.sx; len++)
644		screen_write_putc(&ctx, &gc, ' ');
645
646	screen_write_stop(&ctx);
647
648	if (grid_compare(c->status.grid, old_status.grid) == 0) {
649		screen_free(&old_status);
650		return (0);
651	}
652	screen_free(&old_status);
653	return (1);
654}
655
656/* Enable status line prompt. */
657void
658status_prompt_set(struct client *c, const char *msg, const char *input,
659    int (*callbackfn)(void *, const char *), void (*freefn)(void *),
660    void *data, int flags)
661{
662	struct format_tree	*ft;
663	int			 keys;
664	time_t			 t;
665	char			*tmp;
666
667	ft = format_create(NULL, 0);
668	format_defaults(ft, c, NULL, NULL, NULL);
669
670	t = time(NULL);
671	tmp = format_expand_time(ft, input, t);
672
673	status_message_clear(c);
674	status_prompt_clear(c);
675
676	c->prompt_string = format_expand_time(ft, msg, t);
677
678	c->prompt_buffer = utf8_fromcstr(tmp);
679	c->prompt_index = utf8_strlen(c->prompt_buffer);
680
681	c->prompt_callbackfn = callbackfn;
682	c->prompt_freefn = freefn;
683	c->prompt_data = data;
684
685	c->prompt_hindex = 0;
686
687	c->prompt_flags = flags;
688
689	keys = options_get_number(c->session->options, "status-keys");
690	if (keys == MODEKEY_EMACS)
691		mode_key_init(&c->prompt_mdata, &mode_key_tree_emacs_edit);
692	else
693		mode_key_init(&c->prompt_mdata, &mode_key_tree_vi_edit);
694
695	c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
696	c->flags |= CLIENT_STATUS;
697
698	free(tmp);
699	format_free(ft);
700}
701
702/* Remove status line prompt. */
703void
704status_prompt_clear(struct client *c)
705{
706	if (c->prompt_string == NULL)
707		return;
708
709	if (c->prompt_freefn != NULL && c->prompt_data != NULL)
710		c->prompt_freefn(c->prompt_data);
711
712	free(c->prompt_string);
713	c->prompt_string = NULL;
714
715	free(c->prompt_buffer);
716	c->prompt_buffer = NULL;
717
718	c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
719	c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */
720
721	screen_reinit(&c->status);
722}
723
724/* Update status line prompt with a new prompt string. */
725void
726status_prompt_update(struct client *c, const char *msg, const char *input)
727{
728	struct format_tree	*ft;
729	time_t			 t;
730	char			*tmp;
731
732	ft = format_create(NULL, 0);
733	format_defaults(ft, c, NULL, NULL, NULL);
734
735	t = time(NULL);
736	tmp = format_expand_time(ft, input, t);
737
738	free(c->prompt_string);
739	c->prompt_string = format_expand_time(ft, msg, t);
740
741	free(c->prompt_buffer);
742	c->prompt_buffer = utf8_fromcstr(tmp);
743	c->prompt_index = utf8_strlen(c->prompt_buffer);
744
745	c->prompt_hindex = 0;
746
747	c->flags |= CLIENT_STATUS;
748
749	free(tmp);
750	format_free(ft);
751}
752
753/* Draw client prompt on status line of present else on last line. */
754int
755status_prompt_redraw(struct client *c)
756{
757	struct screen_write_ctx	 ctx;
758	struct session		*s = c->session;
759	struct screen		 old_status;
760	u_int			 i, offset, left, start, pcursor, pwidth, width;
761	struct grid_cell	 gc, cursorgc;
762
763	if (c->tty.sx == 0 || c->tty.sy == 0)
764		return (0);
765	memcpy(&old_status, &c->status, sizeof old_status);
766	screen_init(&c->status, c->tty.sx, 1, 0);
767
768	if (c->prompt_mdata.mode == 1)
769		style_apply(&gc, s->options, "message-command-style");
770	else
771		style_apply(&gc, s->options, "message-style");
772
773	memcpy(&cursorgc, &gc, sizeof cursorgc);
774	cursorgc.attr ^= GRID_ATTR_REVERSE;
775
776	start = screen_write_strlen("%s", c->prompt_string);
777	if (start > c->tty.sx)
778		start = c->tty.sx;
779
780	screen_write_start(&ctx, NULL, &c->status);
781	screen_write_cursormove(&ctx, 0, 0);
782	screen_write_nputs(&ctx, start, &gc, "%s", c->prompt_string);
783	while (c->status.cx < screen_size_x(&c->status))
784		screen_write_putc(&ctx, &gc, ' ');
785	screen_write_cursormove(&ctx, start, 0);
786
787	left = c->tty.sx - start;
788	if (left == 0)
789		goto finished;
790
791	pcursor = utf8_strwidth(c->prompt_buffer, c->prompt_index);
792	pwidth = utf8_strwidth(c->prompt_buffer, -1);
793	if (pcursor >= left) {
794		/*
795		 * The cursor would be outside the screen so start drawing
796		 * with it on the right.
797		 */
798		offset = (pcursor - left) + 1;
799		pwidth = left;
800	} else
801		offset = 0;
802	if (pwidth > left)
803		pwidth = left;
804
805	width = 0;
806	for (i = 0; c->prompt_buffer[i].size != 0; i++) {
807		if (width < offset) {
808			width += c->prompt_buffer[i].width;
809			continue;
810		}
811		if (width >= offset + pwidth)
812			break;
813		width += c->prompt_buffer[i].width;
814		if (width > offset + pwidth)
815			break;
816
817		if (i != c->prompt_index) {
818			utf8_copy(&gc.data, &c->prompt_buffer[i]);
819			screen_write_cell(&ctx, &gc);
820		} else {
821			utf8_copy(&cursorgc.data, &c->prompt_buffer[i]);
822			screen_write_cell(&ctx, &cursorgc);
823		}
824	}
825	if (c->status.cx < screen_size_x(&c->status) && c->prompt_index >= i)
826		screen_write_putc(&ctx, &cursorgc, ' ');
827
828finished:
829	screen_write_stop(&ctx);
830
831	if (grid_compare(c->status.grid, old_status.grid) == 0) {
832		screen_free(&old_status);
833		return (0);
834	}
835	screen_free(&old_status);
836	return (1);
837}
838
839/* Is this a separator? */
840static int
841status_prompt_in_list(const char *ws, const struct utf8_data *ud)
842{
843	if (ud->size != 1 || ud->width != 1)
844		return (0);
845	return (strchr(ws, *ud->data) != NULL);
846}
847
848/* Is this a space? */
849static int
850status_prompt_space(const struct utf8_data *ud)
851{
852	if (ud->size != 1 || ud->width != 1)
853		return (0);
854	return (*ud->data == ' ');
855}
856
857/* Handle keys in prompt. */
858int
859status_prompt_key(struct client *c, key_code key)
860{
861	struct options		*oo = c->session->options;
862	struct paste_buffer	*pb;
863	char			*s, word[64];
864	const char		*histstr, *bufdata, *ws = NULL;
865	u_char			 ch;
866	size_t			 size, n, off, idx, bufsize, used;
867	struct utf8_data	 tmp, *first, *last, *ud;
868
869	size = utf8_strlen(c->prompt_buffer);
870
871	if (c->prompt_flags & PROMPT_NUMERIC) {
872		if (key >= '0' && key <= '9')
873			goto append_key;
874		s = utf8_tocstr(c->prompt_buffer);
875		c->prompt_callbackfn(c->prompt_data, s);
876		status_prompt_clear(c);
877		free(s);
878		return (1);
879	}
880
881	switch (mode_key_lookup(&c->prompt_mdata, key)) {
882	case MODEKEYEDIT_CURSORLEFT:
883		if (c->prompt_index > 0) {
884			c->prompt_index--;
885			c->flags |= CLIENT_STATUS;
886		}
887		break;
888	case MODEKEYEDIT_SWITCHMODE:
889		c->flags |= CLIENT_STATUS;
890		break;
891	case MODEKEYEDIT_SWITCHMODEAPPEND:
892		c->flags |= CLIENT_STATUS;
893		/* FALLTHROUGH */
894	case MODEKEYEDIT_CURSORRIGHT:
895		if (c->prompt_index < size) {
896			c->prompt_index++;
897			c->flags |= CLIENT_STATUS;
898		}
899		break;
900	case MODEKEYEDIT_SWITCHMODEBEGINLINE:
901		c->flags |= CLIENT_STATUS;
902		/* FALLTHROUGH */
903	case MODEKEYEDIT_STARTOFLINE:
904		if (c->prompt_index != 0) {
905			c->prompt_index = 0;
906			c->flags |= CLIENT_STATUS;
907		}
908		break;
909	case MODEKEYEDIT_SWITCHMODEAPPENDLINE:
910		c->flags |= CLIENT_STATUS;
911		/* FALLTHROUGH */
912	case MODEKEYEDIT_ENDOFLINE:
913		if (c->prompt_index != size) {
914			c->prompt_index = size;
915			c->flags |= CLIENT_STATUS;
916		}
917		break;
918	case MODEKEYEDIT_COMPLETE:
919		if (c->prompt_buffer[0].size == 0)
920			break;
921
922		idx = c->prompt_index;
923		if (idx != 0)
924			idx--;
925
926		/* Find the word we are in. */
927		first = &c->prompt_buffer[idx];
928		while (first > c->prompt_buffer && !status_prompt_space(first))
929			first--;
930		while (first->size != 0 && status_prompt_space(first))
931			first++;
932		last = &c->prompt_buffer[idx];
933		while (last->size != 0 && !status_prompt_space(last))
934			last++;
935		while (last > c->prompt_buffer && status_prompt_space(last))
936			last--;
937		if (last->size != 0)
938			last++;
939		if (last <= first)
940			break;
941
942		used = 0;
943		for (ud = first; ud < last; ud++) {
944			if (used + ud->size >= sizeof word)
945				break;
946			memcpy(word + used, ud->data, ud->size);
947			used += ud->size;
948		}
949		if (ud != last)
950			break;
951		word[used] = '\0';
952
953		/* And try to complete it. */
954		if ((s = status_prompt_complete(c->session, word)) == NULL)
955			break;
956
957		/* Trim out word. */
958		n = size - (last - c->prompt_buffer) + 1; /* with \0 */
959		memmove(first, last, n * sizeof *c->prompt_buffer);
960		size -= last - first;
961
962		/* Insert the new word. */
963		size += strlen(s);
964		off = first - c->prompt_buffer;
965		c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 1,
966		    sizeof *c->prompt_buffer);
967		first = c->prompt_buffer + off;
968		memmove(first + strlen(s), first, n * sizeof *c->prompt_buffer);
969		for (idx = 0; idx < strlen(s); idx++)
970			utf8_set(&first[idx], s[idx]);
971
972		c->prompt_index = (first - c->prompt_buffer) + strlen(s);
973		free(s);
974
975		c->flags |= CLIENT_STATUS;
976		break;
977	case MODEKEYEDIT_BACKSPACE:
978		if (c->prompt_index != 0) {
979			if (c->prompt_index == size)
980				c->prompt_buffer[--c->prompt_index].size = 0;
981			else {
982				memmove(c->prompt_buffer + c->prompt_index - 1,
983				    c->prompt_buffer + c->prompt_index,
984				    (size + 1 - c->prompt_index) *
985				    sizeof *c->prompt_buffer);
986				c->prompt_index--;
987			}
988			c->flags |= CLIENT_STATUS;
989		}
990		break;
991	case MODEKEYEDIT_DELETE:
992	case MODEKEYEDIT_SWITCHMODESUBSTITUTE:
993		if (c->prompt_index != size) {
994			memmove(c->prompt_buffer + c->prompt_index,
995			    c->prompt_buffer + c->prompt_index + 1,
996			    (size + 1 - c->prompt_index) *
997			    sizeof *c->prompt_buffer);
998			c->flags |= CLIENT_STATUS;
999		}
1000		break;
1001	case MODEKEYEDIT_DELETELINE:
1002	case MODEKEYEDIT_SWITCHMODESUBSTITUTELINE:
1003		c->prompt_buffer[0].size = 0;
1004		c->prompt_index = 0;
1005		c->flags |= CLIENT_STATUS;
1006		break;
1007	case MODEKEYEDIT_DELETETOENDOFLINE:
1008	case MODEKEYEDIT_SWITCHMODECHANGELINE:
1009		if (c->prompt_index < size) {
1010			c->prompt_buffer[c->prompt_index].size = 0;
1011			c->flags |= CLIENT_STATUS;
1012		}
1013		break;
1014	case MODEKEYEDIT_DELETEWORD:
1015		ws = options_get_string(oo, "word-separators");
1016		idx = c->prompt_index;
1017
1018		/* Find a non-separator. */
1019		while (idx != 0) {
1020			idx--;
1021			if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1022				break;
1023		}
1024
1025		/* Find the separator at the beginning of the word. */
1026		while (idx != 0) {
1027			idx--;
1028			if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) {
1029				/* Go back to the word. */
1030				idx++;
1031				break;
1032			}
1033		}
1034
1035		memmove(c->prompt_buffer + idx,
1036		    c->prompt_buffer + c->prompt_index,
1037		    (size + 1 - c->prompt_index) *
1038		    sizeof *c->prompt_buffer);
1039		memset(c->prompt_buffer + size - (c->prompt_index - idx),
1040		    '\0', (c->prompt_index - idx) * sizeof *c->prompt_buffer);
1041		c->prompt_index = idx;
1042
1043		c->flags |= CLIENT_STATUS;
1044		break;
1045	case MODEKEYEDIT_NEXTSPACE:
1046		ws = " ";
1047		/* FALLTHROUGH */
1048	case MODEKEYEDIT_NEXTWORD:
1049		if (ws == NULL)
1050			ws = options_get_string(oo, "word-separators");
1051
1052		/* Find a separator. */
1053		while (c->prompt_index != size) {
1054			idx = ++c->prompt_index;
1055			if (status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1056				break;
1057		}
1058
1059		/* Find the word right after the separator. */
1060		while (c->prompt_index != size) {
1061			idx = ++c->prompt_index;
1062			if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1063				break;
1064		}
1065
1066		c->flags |= CLIENT_STATUS;
1067		break;
1068	case MODEKEYEDIT_NEXTSPACEEND:
1069		ws = " ";
1070		/* FALLTHROUGH */
1071	case MODEKEYEDIT_NEXTWORDEND:
1072		if (ws == NULL)
1073			ws = options_get_string(oo, "word-separators");
1074
1075		/* Find a word. */
1076		while (c->prompt_index != size) {
1077			idx = ++c->prompt_index;
1078			if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1079				break;
1080		}
1081
1082		/* Find the separator at the end of the word. */
1083		while (c->prompt_index != size) {
1084			idx = ++c->prompt_index;
1085			if (status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1086				break;
1087		}
1088
1089		/* Back up to the end-of-word like vi. */
1090		if (options_get_number(oo, "status-keys") == MODEKEY_VI &&
1091		    c->prompt_index != 0)
1092			c->prompt_index--;
1093
1094		c->flags |= CLIENT_STATUS;
1095		break;
1096	case MODEKEYEDIT_PREVIOUSSPACE:
1097		ws = " ";
1098		/* FALLTHROUGH */
1099	case MODEKEYEDIT_PREVIOUSWORD:
1100		if (ws == NULL)
1101			ws = options_get_string(oo, "word-separators");
1102
1103		/* Find a non-separator. */
1104		while (c->prompt_index != 0) {
1105			idx = --c->prompt_index;
1106			if (!status_prompt_in_list(ws, &c->prompt_buffer[idx]))
1107				break;
1108		}
1109
1110		/* Find the separator at the beginning of the word. */
1111		while (c->prompt_index != 0) {
1112			idx = --c->prompt_index;
1113			if (status_prompt_in_list(ws, &c->prompt_buffer[idx])) {
1114				/* Go back to the word. */
1115				c->prompt_index++;
1116				break;
1117			}
1118		}
1119
1120		c->flags |= CLIENT_STATUS;
1121		break;
1122	case MODEKEYEDIT_HISTORYUP:
1123		histstr = status_prompt_up_history(&c->prompt_hindex);
1124		if (histstr == NULL)
1125			break;
1126		free(c->prompt_buffer);
1127		c->prompt_buffer = utf8_fromcstr(histstr);
1128		c->prompt_index = utf8_strlen(c->prompt_buffer);
1129		c->flags |= CLIENT_STATUS;
1130		break;
1131	case MODEKEYEDIT_HISTORYDOWN:
1132		histstr = status_prompt_down_history(&c->prompt_hindex);
1133		if (histstr == NULL)
1134			break;
1135		free(c->prompt_buffer);
1136		c->prompt_buffer = utf8_fromcstr(histstr);
1137		c->prompt_index = utf8_strlen(c->prompt_buffer);
1138		c->flags |= CLIENT_STATUS;
1139		break;
1140	case MODEKEYEDIT_PASTE:
1141		if ((pb = paste_get_top(NULL)) == NULL)
1142			break;
1143		bufdata = paste_buffer_data(pb, &bufsize);
1144		for (n = 0; n < bufsize; n++) {
1145			ch = (u_char)bufdata[n];
1146			if (ch < 32 || ch >= 127)
1147				break;
1148		}
1149
1150		c->prompt_buffer = xreallocarray(c->prompt_buffer, size + n + 1,
1151		    sizeof *c->prompt_buffer);
1152		if (c->prompt_index == size) {
1153			for (idx = 0; idx < n; idx++) {
1154				ud = &c->prompt_buffer[c->prompt_index + idx];
1155				utf8_set(ud, bufdata[idx]);
1156			}
1157			c->prompt_index += n;
1158			c->prompt_buffer[c->prompt_index].size = 0;
1159		} else {
1160			memmove(c->prompt_buffer + c->prompt_index + n,
1161			    c->prompt_buffer + c->prompt_index,
1162			    (size + 1 - c->prompt_index) *
1163			    sizeof *c->prompt_buffer);
1164			for (idx = 0; idx < n; idx++) {
1165				ud = &c->prompt_buffer[c->prompt_index + idx];
1166				utf8_set(ud, bufdata[idx]);
1167			}
1168			c->prompt_index += n;
1169		}
1170
1171		c->flags |= CLIENT_STATUS;
1172		break;
1173	case MODEKEYEDIT_TRANSPOSECHARS:
1174		idx = c->prompt_index;
1175		if (idx < size)
1176			idx++;
1177		if (idx >= 2) {
1178			utf8_copy(&tmp, &c->prompt_buffer[idx - 2]);
1179			utf8_copy(&c->prompt_buffer[idx - 2],
1180			    &c->prompt_buffer[idx - 1]);
1181			utf8_copy(&c->prompt_buffer[idx - 1], &tmp);
1182			c->prompt_index = idx;
1183			c->flags |= CLIENT_STATUS;
1184		}
1185		break;
1186	case MODEKEYEDIT_ENTER:
1187		s = utf8_tocstr(c->prompt_buffer);
1188		if (*s != '\0')
1189			status_prompt_add_history(s);
1190		if (c->prompt_callbackfn(c->prompt_data, s) == 0)
1191			status_prompt_clear(c);
1192		free(s);
1193		break;
1194	case MODEKEYEDIT_CANCEL:
1195		if (c->prompt_callbackfn(c->prompt_data, NULL) == 0)
1196			status_prompt_clear(c);
1197		break;
1198	case MODEKEY_OTHER:
1199		break;
1200	default:
1201		return (0);
1202	}
1203
1204append_key:
1205	if (key <= 0x1f || key >= KEYC_BASE)
1206		return (0);
1207	if (utf8_split(key, &tmp) != UTF8_DONE)
1208		return (0);
1209
1210	c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 2,
1211	    sizeof *c->prompt_buffer);
1212
1213	if (c->prompt_index == size) {
1214		utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
1215		c->prompt_index++;
1216		c->prompt_buffer[c->prompt_index].size = 0;
1217	} else {
1218		memmove(c->prompt_buffer + c->prompt_index + 1,
1219		    c->prompt_buffer + c->prompt_index,
1220		    (size + 1 - c->prompt_index) *
1221		    sizeof *c->prompt_buffer);
1222		utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
1223		c->prompt_index++;
1224	}
1225
1226	if (c->prompt_flags & PROMPT_SINGLE) {
1227		s = utf8_tocstr(c->prompt_buffer);
1228		if (strlen(s) != 1)
1229			status_prompt_clear(c);
1230		else if (c->prompt_callbackfn(c->prompt_data, s) == 0)
1231			status_prompt_clear(c);
1232		free(s);
1233	}
1234
1235	c->flags |= CLIENT_STATUS;
1236	return (0);
1237}
1238
1239/* Get previous line from the history. */
1240static const char *
1241status_prompt_up_history(u_int *idx)
1242{
1243	/*
1244	 * History runs from 0 to size - 1. Index is from 0 to size. Zero is
1245	 * empty.
1246	 */
1247
1248	if (status_prompt_hsize == 0 || *idx == status_prompt_hsize)
1249		return (NULL);
1250	(*idx)++;
1251	return (status_prompt_hlist[status_prompt_hsize - *idx]);
1252}
1253
1254/* Get next line from the history. */
1255static const char *
1256status_prompt_down_history(u_int *idx)
1257{
1258	if (status_prompt_hsize == 0 || *idx == 0)
1259		return ("");
1260	(*idx)--;
1261	if (*idx == 0)
1262		return ("");
1263	return (status_prompt_hlist[status_prompt_hsize - *idx]);
1264}
1265
1266/* Add line to the history. */
1267static void
1268status_prompt_add_history(const char *line)
1269{
1270	size_t	size;
1271
1272	if (status_prompt_hsize > 0 &&
1273	    strcmp(status_prompt_hlist[status_prompt_hsize - 1], line) == 0)
1274		return;
1275
1276	if (status_prompt_hsize == PROMPT_HISTORY) {
1277		free(status_prompt_hlist[0]);
1278
1279		size = (PROMPT_HISTORY - 1) * sizeof *status_prompt_hlist;
1280		memmove(&status_prompt_hlist[0], &status_prompt_hlist[1], size);
1281
1282		status_prompt_hlist[status_prompt_hsize - 1] = xstrdup(line);
1283		return;
1284	}
1285
1286	status_prompt_hlist = xreallocarray(status_prompt_hlist,
1287	    status_prompt_hsize + 1, sizeof *status_prompt_hlist);
1288	status_prompt_hlist[status_prompt_hsize++] = xstrdup(line);
1289}
1290
1291/* Build completion list. */
1292static const char **
1293status_prompt_complete_list(u_int *size, const char *s)
1294{
1295	const char				**list = NULL, **layout;
1296	const struct cmd_entry			**cmdent;
1297	const struct options_table_entry	 *oe;
1298	const char				 *layouts[] = {
1299		"even-horizontal", "even-vertical", "main-horizontal",
1300		"main-vertical", "tiled", NULL
1301	};
1302
1303	*size = 0;
1304	for (cmdent = cmd_table; *cmdent != NULL; cmdent++) {
1305		if (strncmp((*cmdent)->name, s, strlen(s)) == 0) {
1306			list = xreallocarray(list, (*size) + 1, sizeof *list);
1307			list[(*size)++] = (*cmdent)->name;
1308		}
1309	}
1310	for (oe = options_table; oe->name != NULL; oe++) {
1311		if (strncmp(oe->name, s, strlen(s)) == 0) {
1312			list = xreallocarray(list, (*size) + 1, sizeof *list);
1313			list[(*size)++] = oe->name;
1314		}
1315	}
1316	for (layout = layouts; *layout != NULL; layout++) {
1317		if (strncmp(*layout, s, strlen(s)) == 0) {
1318			list = xreallocarray(list, (*size) + 1, sizeof *list);
1319			list[(*size)++] = *layout;
1320		}
1321	}
1322	return (list);
1323}
1324
1325/* Find longest prefix. */
1326static char *
1327status_prompt_complete_prefix(const char **list, u_int size)
1328{
1329	char	 *out;
1330	u_int	  i;
1331	size_t	  j;
1332
1333	out = xstrdup(list[0]);
1334	for (i = 1; i < size; i++) {
1335		j = strlen(list[i]);
1336		if (j > strlen(out))
1337			j = strlen(out);
1338		for (; j > 0; j--) {
1339			if (out[j - 1] != list[i][j - 1])
1340				out[j - 1] = '\0';
1341		}
1342	}
1343	return (out);
1344}
1345
1346/* Complete word. */
1347static char *
1348status_prompt_complete(struct session *session, const char *s)
1349{
1350	const char	**list = NULL, *colon;
1351	u_int		  size = 0, i;
1352	struct session	 *s_loop;
1353	struct winlink	 *wl;
1354	struct window	 *w;
1355	char		 *copy, *out, *tmp;
1356
1357	if (*s == '\0')
1358		return (NULL);
1359	out = NULL;
1360
1361	if (strncmp(s, "-t", 2) != 0 && strncmp(s, "-s", 2) != 0) {
1362		list = status_prompt_complete_list(&size, s);
1363		if (size == 0)
1364			out = NULL;
1365		else if (size == 1)
1366			xasprintf(&out, "%s ", list[0]);
1367		else
1368			out = status_prompt_complete_prefix(list, size);
1369		free(list);
1370		return (out);
1371	}
1372	copy = xstrdup(s);
1373
1374	colon = ":";
1375	if (copy[strlen(copy) - 1] == ':')
1376		copy[strlen(copy) - 1] = '\0';
1377	else
1378		colon = "";
1379	s = copy + 2;
1380
1381	RB_FOREACH(s_loop, sessions, &sessions) {
1382		if (strncmp(s_loop->name, s, strlen(s)) == 0) {
1383			list = xreallocarray(list, size + 2, sizeof *list);
1384			list[size++] = s_loop->name;
1385		}
1386	}
1387	if (size == 1) {
1388		out = xstrdup(list[0]);
1389		if (session_find(list[0]) != NULL)
1390			colon = ":";
1391	} else if (size != 0)
1392		out = status_prompt_complete_prefix(list, size);
1393	if (out != NULL) {
1394		xasprintf(&tmp, "-%c%s%s", copy[1], out, colon);
1395		out = tmp;
1396		goto found;
1397	}
1398
1399	colon = "";
1400	if (*s == ':') {
1401		RB_FOREACH(wl, winlinks, &session->windows) {
1402			xasprintf(&tmp, ":%s", wl->window->name);
1403			if (strncmp(tmp, s, strlen(s)) == 0){
1404				list = xreallocarray(list, size + 1,
1405				    sizeof *list);
1406				list[size++] = tmp;
1407				continue;
1408			}
1409			free(tmp);
1410
1411			xasprintf(&tmp, ":%d", wl->idx);
1412			if (strncmp(tmp, s, strlen(s)) == 0) {
1413				list = xreallocarray(list, size + 1,
1414				    sizeof *list);
1415				list[size++] = tmp;
1416				continue;
1417			}
1418			free(tmp);
1419		}
1420	} else {
1421		RB_FOREACH(s_loop, sessions, &sessions) {
1422			RB_FOREACH(wl, winlinks, &s_loop->windows) {
1423				w = wl->window;
1424
1425				xasprintf(&tmp, "%s:%s", s_loop->name, w->name);
1426				if (strncmp(tmp, s, strlen(s)) == 0) {
1427					list = xreallocarray(list, size + 1,
1428					    sizeof *list);
1429					list[size++] = tmp;
1430					continue;
1431				}
1432				free(tmp);
1433
1434				xasprintf(&tmp, "%s:%d", s_loop->name, wl->idx);
1435				if (strncmp(tmp, s, strlen(s)) == 0) {
1436					list = xreallocarray(list, size + 1,
1437					    sizeof *list);
1438					list[size++] = tmp;
1439					continue;
1440				}
1441				free(tmp);
1442			}
1443		}
1444	}
1445	if (size == 1) {
1446		out = xstrdup(list[0]);
1447		colon = " ";
1448	} else if (size != 0)
1449		out = status_prompt_complete_prefix(list, size);
1450	if (out != NULL) {
1451		xasprintf(&tmp, "-%c%s%s", copy[1], out, colon);
1452		out = tmp;
1453	}
1454
1455	for (i = 0; i < size; i++)
1456		free((void *)list[i]);
1457
1458found:
1459	free(copy);
1460	free(list);
1461	return (out);
1462}
1463