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