1 /* $OpenBSD: status.c,v 1.250 2024/10/28 08:11:59 nicm Exp $ */
2
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sys/types.h>
20 #include <sys/time.h>
21
22 #include <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
32 static void status_message_callback(int, short, void *);
33 static void status_timer_callback(int, short, void *);
34
35 static char *status_prompt_find_history_file(void);
36 static const char *status_prompt_up_history(u_int *, u_int);
37 static const char *status_prompt_down_history(u_int *, u_int);
38 static void status_prompt_add_history(const char *, u_int);
39
40 static char *status_prompt_complete(struct client *, const char *, u_int);
41 static char *status_prompt_complete_window_menu(struct client *,
42 struct session *, const char *, u_int, char);
43
44 struct status_prompt_menu {
45 struct client *c;
46 u_int start;
47 u_int size;
48 char **list;
49 char flag;
50 };
51
52 static const char *prompt_type_strings[] = {
53 "command",
54 "search",
55 "target",
56 "window-target"
57 };
58
59 /* Status prompt history. */
60 char **status_prompt_hlist[PROMPT_NTYPES];
61 u_int status_prompt_hsize[PROMPT_NTYPES];
62
63 /* Find the history file to load/save from/to. */
64 static char *
status_prompt_find_history_file(void)65 status_prompt_find_history_file(void)
66 {
67 const char *home, *history_file;
68 char *path;
69
70 history_file = options_get_string(global_options, "history-file");
71 if (*history_file == '\0')
72 return (NULL);
73 if (*history_file == '/')
74 return (xstrdup(history_file));
75
76 if (history_file[0] != '~' || history_file[1] != '/')
77 return (NULL);
78 if ((home = find_home()) == NULL)
79 return (NULL);
80 xasprintf(&path, "%s%s", home, history_file + 1);
81 return (path);
82 }
83
84 /* Add loaded history item to the appropriate list. */
85 static void
status_prompt_add_typed_history(char * line)86 status_prompt_add_typed_history(char *line)
87 {
88 char *typestr;
89 enum prompt_type type = PROMPT_TYPE_INVALID;
90
91 typestr = strsep(&line, ":");
92 if (line != NULL)
93 type = status_prompt_type(typestr);
94 if (type == PROMPT_TYPE_INVALID) {
95 /*
96 * Invalid types are not expected, but this provides backward
97 * compatibility with old history files.
98 */
99 if (line != NULL)
100 *(--line) = ':';
101 status_prompt_add_history(typestr, PROMPT_TYPE_COMMAND);
102 } else
103 status_prompt_add_history(line, type);
104 }
105
106 /* Load status prompt history from file. */
107 void
status_prompt_load_history(void)108 status_prompt_load_history(void)
109 {
110 FILE *f;
111 char *history_file, *line, *tmp;
112 size_t length;
113
114 if ((history_file = status_prompt_find_history_file()) == NULL)
115 return;
116 log_debug("loading history from %s", history_file);
117
118 f = fopen(history_file, "r");
119 if (f == NULL) {
120 log_debug("%s: %s", history_file, strerror(errno));
121 free(history_file);
122 return;
123 }
124 free(history_file);
125
126 for (;;) {
127 if ((line = fgetln(f, &length)) == NULL)
128 break;
129
130 if (length > 0) {
131 if (line[length - 1] == '\n') {
132 line[length - 1] = '\0';
133 status_prompt_add_typed_history(line);
134 } else {
135 tmp = xmalloc(length + 1);
136 memcpy(tmp, line, length);
137 tmp[length] = '\0';
138 status_prompt_add_typed_history(tmp);
139 free(tmp);
140 }
141 }
142 }
143 fclose(f);
144 }
145
146 /* Save status prompt history to file. */
147 void
status_prompt_save_history(void)148 status_prompt_save_history(void)
149 {
150 FILE *f;
151 u_int i, type;
152 char *history_file;
153
154 if ((history_file = status_prompt_find_history_file()) == NULL)
155 return;
156 log_debug("saving history to %s", history_file);
157
158 f = fopen(history_file, "w");
159 if (f == NULL) {
160 log_debug("%s: %s", history_file, strerror(errno));
161 free(history_file);
162 return;
163 }
164 free(history_file);
165
166 for (type = 0; type < PROMPT_NTYPES; type++) {
167 for (i = 0; i < status_prompt_hsize[type]; i++) {
168 fputs(prompt_type_strings[type], f);
169 fputc(':', f);
170 fputs(status_prompt_hlist[type][i], f);
171 fputc('\n', f);
172 }
173 }
174 fclose(f);
175
176 }
177
178 /* Status timer callback. */
179 static void
status_timer_callback(__unused int fd,__unused short events,void * arg)180 status_timer_callback(__unused int fd, __unused short events, void *arg)
181 {
182 struct client *c = arg;
183 struct session *s = c->session;
184 struct timeval tv;
185
186 evtimer_del(&c->status.timer);
187
188 if (s == NULL)
189 return;
190
191 if (c->message_string == NULL && c->prompt_string == NULL)
192 c->flags |= CLIENT_REDRAWSTATUS;
193
194 timerclear(&tv);
195 tv.tv_sec = options_get_number(s->options, "status-interval");
196
197 if (tv.tv_sec != 0)
198 evtimer_add(&c->status.timer, &tv);
199 log_debug("client %p, status interval %d", c, (int)tv.tv_sec);
200 }
201
202 /* Start status timer for client. */
203 void
status_timer_start(struct client * c)204 status_timer_start(struct client *c)
205 {
206 struct session *s = c->session;
207
208 if (event_initialized(&c->status.timer))
209 evtimer_del(&c->status.timer);
210 else
211 evtimer_set(&c->status.timer, status_timer_callback, c);
212
213 if (s != NULL && options_get_number(s->options, "status"))
214 status_timer_callback(-1, 0, c);
215 }
216
217 /* Start status timer for all clients. */
218 void
status_timer_start_all(void)219 status_timer_start_all(void)
220 {
221 struct client *c;
222
223 TAILQ_FOREACH(c, &clients, entry)
224 status_timer_start(c);
225 }
226
227 /* Update status cache. */
228 void
status_update_cache(struct session * s)229 status_update_cache(struct session *s)
230 {
231 s->statuslines = options_get_number(s->options, "status");
232 if (s->statuslines == 0)
233 s->statusat = -1;
234 else if (options_get_number(s->options, "status-position") == 0)
235 s->statusat = 0;
236 else
237 s->statusat = 1;
238 }
239
240 /* Get screen line of status line. -1 means off. */
241 int
status_at_line(struct client * c)242 status_at_line(struct client *c)
243 {
244 struct session *s = c->session;
245
246 if (c->flags & (CLIENT_STATUSOFF|CLIENT_CONTROL))
247 return (-1);
248 if (s->statusat != 1)
249 return (s->statusat);
250 return (c->tty.sy - status_line_size(c));
251 }
252
253 /* Get size of status line for client's session. 0 means off. */
254 u_int
status_line_size(struct client * c)255 status_line_size(struct client *c)
256 {
257 struct session *s = c->session;
258
259 if (c->flags & (CLIENT_STATUSOFF|CLIENT_CONTROL))
260 return (0);
261 if (s == NULL)
262 return (options_get_number(global_s_options, "status"));
263 return (s->statuslines);
264 }
265
266 /* Get the prompt line number for client's session. 1 means at the bottom. */
267 static u_int
status_prompt_line_at(struct client * c)268 status_prompt_line_at(struct client *c)
269 {
270 struct session *s = c->session;
271
272 if (c->flags & (CLIENT_STATUSOFF|CLIENT_CONTROL))
273 return (1);
274 return (options_get_number(s->options, "message-line"));
275 }
276
277 /* Get window at window list position. */
278 struct style_range *
status_get_range(struct client * c,u_int x,u_int y)279 status_get_range(struct client *c, u_int x, u_int y)
280 {
281 struct status_line *sl = &c->status;
282 struct style_range *sr;
283
284 if (y >= nitems(sl->entries))
285 return (NULL);
286 TAILQ_FOREACH(sr, &sl->entries[y].ranges, entry) {
287 if (x >= sr->start && x < sr->end)
288 return (sr);
289 }
290 return (NULL);
291 }
292
293 /* Free all ranges. */
294 static void
status_free_ranges(struct style_ranges * srs)295 status_free_ranges(struct style_ranges *srs)
296 {
297 struct style_range *sr, *sr1;
298
299 TAILQ_FOREACH_SAFE(sr, srs, entry, sr1) {
300 TAILQ_REMOVE(srs, sr, entry);
301 free(sr);
302 }
303 }
304
305 /* Save old status line. */
306 static void
status_push_screen(struct client * c)307 status_push_screen(struct client *c)
308 {
309 struct status_line *sl = &c->status;
310
311 if (sl->active == &sl->screen) {
312 sl->active = xmalloc(sizeof *sl->active);
313 screen_init(sl->active, c->tty.sx, status_line_size(c), 0);
314 }
315 sl->references++;
316 }
317
318 /* Restore old status line. */
319 static void
status_pop_screen(struct client * c)320 status_pop_screen(struct client *c)
321 {
322 struct status_line *sl = &c->status;
323
324 if (--sl->references == 0) {
325 screen_free(sl->active);
326 free(sl->active);
327 sl->active = &sl->screen;
328 }
329 }
330
331 /* Initialize status line. */
332 void
status_init(struct client * c)333 status_init(struct client *c)
334 {
335 struct status_line *sl = &c->status;
336 u_int i;
337
338 for (i = 0; i < nitems(sl->entries); i++)
339 TAILQ_INIT(&sl->entries[i].ranges);
340
341 screen_init(&sl->screen, c->tty.sx, 1, 0);
342 sl->active = &sl->screen;
343 }
344
345 /* Free status line. */
346 void
status_free(struct client * c)347 status_free(struct client *c)
348 {
349 struct status_line *sl = &c->status;
350 u_int i;
351
352 for (i = 0; i < nitems(sl->entries); i++) {
353 status_free_ranges(&sl->entries[i].ranges);
354 free((void *)sl->entries[i].expanded);
355 }
356
357 if (event_initialized(&sl->timer))
358 evtimer_del(&sl->timer);
359
360 if (sl->active != &sl->screen) {
361 screen_free(sl->active);
362 free(sl->active);
363 }
364 screen_free(&sl->screen);
365 }
366
367 /* Draw status line for client. */
368 int
status_redraw(struct client * c)369 status_redraw(struct client *c)
370 {
371 struct status_line *sl = &c->status;
372 struct status_line_entry *sle;
373 struct session *s = c->session;
374 struct screen_write_ctx ctx;
375 struct grid_cell gc;
376 u_int lines, i, n, width = c->tty.sx;
377 int flags, force = 0, changed = 0, fg, bg;
378 struct options_entry *o;
379 union options_value *ov;
380 struct format_tree *ft;
381 char *expanded;
382
383 log_debug("%s enter", __func__);
384
385 /* Shouldn't get here if not the active screen. */
386 if (sl->active != &sl->screen)
387 fatalx("not the active screen");
388
389 /* No status line? */
390 lines = status_line_size(c);
391 if (c->tty.sy == 0 || lines == 0)
392 return (1);
393
394 /* Create format tree. */
395 flags = FORMAT_STATUS;
396 if (c->flags & CLIENT_STATUSFORCE)
397 flags |= FORMAT_FORCE;
398 ft = format_create(c, NULL, FORMAT_NONE, flags);
399 format_defaults(ft, c, NULL, NULL, NULL);
400
401 /* Set up default colour. */
402 style_apply(&gc, s->options, "status-style", ft);
403 fg = options_get_number(s->options, "status-fg");
404 if (!COLOUR_DEFAULT(fg))
405 gc.fg = fg;
406 bg = options_get_number(s->options, "status-bg");
407 if (!COLOUR_DEFAULT(bg))
408 gc.bg = bg;
409 if (!grid_cells_equal(&gc, &sl->style)) {
410 force = 1;
411 memcpy(&sl->style, &gc, sizeof sl->style);
412 }
413
414 /* Resize the target screen. */
415 if (screen_size_x(&sl->screen) != width ||
416 screen_size_y(&sl->screen) != lines) {
417 screen_resize(&sl->screen, width, lines, 0);
418 changed = force = 1;
419 }
420 screen_write_start(&ctx, &sl->screen);
421
422 /* Write the status lines. */
423 o = options_get(s->options, "status-format");
424 if (o == NULL) {
425 for (n = 0; n < width * lines; n++)
426 screen_write_putc(&ctx, &gc, ' ');
427 } else {
428 for (i = 0; i < lines; i++) {
429 screen_write_cursormove(&ctx, 0, i, 0);
430
431 ov = options_array_get(o, i);
432 if (ov == NULL) {
433 for (n = 0; n < width; n++)
434 screen_write_putc(&ctx, &gc, ' ');
435 continue;
436 }
437 sle = &sl->entries[i];
438
439 expanded = format_expand_time(ft, ov->string);
440 if (!force &&
441 sle->expanded != NULL &&
442 strcmp(expanded, sle->expanded) == 0) {
443 free(expanded);
444 continue;
445 }
446 changed = 1;
447
448 for (n = 0; n < width; n++)
449 screen_write_putc(&ctx, &gc, ' ');
450 screen_write_cursormove(&ctx, 0, i, 0);
451
452 status_free_ranges(&sle->ranges);
453 format_draw(&ctx, &gc, width, expanded, &sle->ranges,
454 0);
455
456 free(sle->expanded);
457 sle->expanded = expanded;
458 }
459 }
460 screen_write_stop(&ctx);
461
462 /* Free the format tree. */
463 format_free(ft);
464
465 /* Return if the status line has changed. */
466 log_debug("%s exit: force=%d, changed=%d", __func__, force, changed);
467 return (force || changed);
468 }
469
470 /* Set a status line message. */
471 void
status_message_set(struct client * c,int delay,int ignore_styles,int ignore_keys,const char * fmt,...)472 status_message_set(struct client *c, int delay, int ignore_styles,
473 int ignore_keys, const char *fmt, ...)
474 {
475 struct timeval tv;
476 va_list ap;
477 char *s;
478
479 va_start(ap, fmt);
480 xvasprintf(&s, fmt, ap);
481 va_end(ap);
482
483 log_debug("%s: %s", __func__, s);
484
485 if (c == NULL) {
486 server_add_message("message: %s", s);
487 free(s);
488 return;
489 }
490
491 status_message_clear(c);
492 status_push_screen(c);
493 c->message_string = s;
494 server_add_message("%s message: %s", c->name, s);
495
496 /*
497 * With delay -1, the display-time option is used; zero means wait for
498 * key press; more than zero is the actual delay time in milliseconds.
499 */
500 if (delay == -1)
501 delay = options_get_number(c->session->options, "display-time");
502 if (delay > 0) {
503 tv.tv_sec = delay / 1000;
504 tv.tv_usec = (delay % 1000) * 1000L;
505
506 if (event_initialized(&c->message_timer))
507 evtimer_del(&c->message_timer);
508 evtimer_set(&c->message_timer, status_message_callback, c);
509
510 evtimer_add(&c->message_timer, &tv);
511 }
512
513 if (delay != 0)
514 c->message_ignore_keys = ignore_keys;
515 c->message_ignore_styles = ignore_styles;
516
517 c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
518 c->flags |= CLIENT_REDRAWSTATUS;
519 }
520
521 /* Clear status line message. */
522 void
status_message_clear(struct client * c)523 status_message_clear(struct client *c)
524 {
525 if (c->message_string == NULL)
526 return;
527
528 free(c->message_string);
529 c->message_string = NULL;
530
531 if (c->prompt_string == NULL)
532 c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
533 c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */
534
535 status_pop_screen(c);
536 }
537
538 /* Clear status line message after timer expires. */
539 static void
status_message_callback(__unused int fd,__unused short event,void * data)540 status_message_callback(__unused int fd, __unused short event, void *data)
541 {
542 struct client *c = data;
543
544 status_message_clear(c);
545 }
546
547 /* Draw client message on status line of present else on last line. */
548 int
status_message_redraw(struct client * c)549 status_message_redraw(struct client *c)
550 {
551 struct status_line *sl = &c->status;
552 struct screen_write_ctx ctx;
553 struct session *s = c->session;
554 struct screen old_screen;
555 size_t len;
556 u_int lines, offset, messageline;
557 struct grid_cell gc;
558 struct format_tree *ft;
559
560 if (c->tty.sx == 0 || c->tty.sy == 0)
561 return (0);
562 memcpy(&old_screen, sl->active, sizeof old_screen);
563
564 lines = status_line_size(c);
565 if (lines <= 1)
566 lines = 1;
567 screen_init(sl->active, c->tty.sx, lines, 0);
568
569 messageline = status_prompt_line_at(c);
570 if (messageline > lines - 1)
571 messageline = lines - 1;
572
573 len = screen_write_strlen("%s", c->message_string);
574 if (len > c->tty.sx)
575 len = c->tty.sx;
576
577 ft = format_create_defaults(NULL, c, NULL, NULL, NULL);
578 style_apply(&gc, s->options, "message-style", ft);
579 format_free(ft);
580
581 screen_write_start(&ctx, sl->active);
582 screen_write_fast_copy(&ctx, &sl->screen, 0, 0, c->tty.sx, lines);
583 screen_write_cursormove(&ctx, 0, messageline, 0);
584 for (offset = 0; offset < c->tty.sx; offset++)
585 screen_write_putc(&ctx, &gc, ' ');
586 screen_write_cursormove(&ctx, 0, messageline, 0);
587 if (c->message_ignore_styles)
588 screen_write_nputs(&ctx, len, &gc, "%s", c->message_string);
589 else
590 format_draw(&ctx, &gc, c->tty.sx, c->message_string, NULL, 0);
591 screen_write_stop(&ctx);
592
593 if (grid_compare(sl->active->grid, old_screen.grid) == 0) {
594 screen_free(&old_screen);
595 return (0);
596 }
597 screen_free(&old_screen);
598 return (1);
599 }
600
601 /* Accept prompt immediately. */
602 static enum cmd_retval
status_prompt_accept(__unused struct cmdq_item * item,void * data)603 status_prompt_accept(__unused struct cmdq_item *item, void *data)
604 {
605 struct client *c = data;
606
607 if (c->prompt_string != NULL) {
608 c->prompt_inputcb(c, c->prompt_data, "y", 1);
609 status_prompt_clear(c);
610 }
611 return (CMD_RETURN_NORMAL);
612 }
613
614 /* Enable status line prompt. */
615 void
status_prompt_set(struct client * c,struct cmd_find_state * fs,const char * msg,const char * input,prompt_input_cb inputcb,prompt_free_cb freecb,void * data,int flags,enum prompt_type prompt_type)616 status_prompt_set(struct client *c, struct cmd_find_state *fs,
617 const char *msg, const char *input, prompt_input_cb inputcb,
618 prompt_free_cb freecb, void *data, int flags, enum prompt_type prompt_type)
619 {
620 struct format_tree *ft;
621 char *tmp;
622
623 server_client_clear_overlay(c);
624
625 if (fs != NULL)
626 ft = format_create_from_state(NULL, c, fs);
627 else
628 ft = format_create_defaults(NULL, c, NULL, NULL, NULL);
629
630 if (input == NULL)
631 input = "";
632 if (flags & PROMPT_NOFORMAT)
633 tmp = xstrdup(input);
634 else
635 tmp = format_expand_time(ft, input);
636
637 status_message_clear(c);
638 status_prompt_clear(c);
639 status_push_screen(c);
640
641 c->prompt_string = format_expand_time(ft, msg);
642
643 if (flags & PROMPT_INCREMENTAL) {
644 c->prompt_last = xstrdup(tmp);
645 c->prompt_buffer = utf8_fromcstr("");
646 } else {
647 c->prompt_last = NULL;
648 c->prompt_buffer = utf8_fromcstr(tmp);
649 }
650 c->prompt_index = utf8_strlen(c->prompt_buffer);
651
652 c->prompt_inputcb = inputcb;
653 c->prompt_freecb = freecb;
654 c->prompt_data = data;
655
656 memset(c->prompt_hindex, 0, sizeof c->prompt_hindex);
657
658 c->prompt_flags = flags;
659 c->prompt_type = prompt_type;
660 c->prompt_mode = PROMPT_ENTRY;
661
662 if (~flags & PROMPT_INCREMENTAL)
663 c->tty.flags |= TTY_FREEZE;
664 c->flags |= CLIENT_REDRAWSTATUS;
665
666 if (flags & PROMPT_INCREMENTAL)
667 c->prompt_inputcb(c, c->prompt_data, "=", 0);
668
669 free(tmp);
670 format_free(ft);
671
672 if ((flags & PROMPT_SINGLE) && (flags & PROMPT_ACCEPT))
673 cmdq_append(c, cmdq_get_callback(status_prompt_accept, c));
674 }
675
676 /* Remove status line prompt. */
677 void
status_prompt_clear(struct client * c)678 status_prompt_clear(struct client *c)
679 {
680 if (c->prompt_string == NULL)
681 return;
682
683 if (c->prompt_freecb != NULL && c->prompt_data != NULL)
684 c->prompt_freecb(c->prompt_data);
685
686 free(c->prompt_last);
687 c->prompt_last = NULL;
688
689 free(c->prompt_string);
690 c->prompt_string = NULL;
691
692 free(c->prompt_buffer);
693 c->prompt_buffer = NULL;
694
695 free(c->prompt_saved);
696 c->prompt_saved = NULL;
697
698 c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
699 c->flags |= CLIENT_ALLREDRAWFLAGS; /* was frozen and may have changed */
700
701 status_pop_screen(c);
702 }
703
704 /* Update status line prompt with a new prompt string. */
705 void
status_prompt_update(struct client * c,const char * msg,const char * input)706 status_prompt_update(struct client *c, const char *msg, const char *input)
707 {
708 struct format_tree *ft;
709 char *tmp;
710
711 ft = format_create(c, NULL, FORMAT_NONE, 0);
712 format_defaults(ft, c, NULL, NULL, NULL);
713
714 tmp = format_expand_time(ft, input);
715
716 free(c->prompt_string);
717 c->prompt_string = format_expand_time(ft, msg);
718
719 free(c->prompt_buffer);
720 c->prompt_buffer = utf8_fromcstr(tmp);
721 c->prompt_index = utf8_strlen(c->prompt_buffer);
722
723 memset(c->prompt_hindex, 0, sizeof c->prompt_hindex);
724
725 c->flags |= CLIENT_REDRAWSTATUS;
726
727 free(tmp);
728 format_free(ft);
729 }
730
731 /* Redraw character. Return 1 if can continue redrawing, 0 otherwise. */
732 static int
status_prompt_redraw_character(struct screen_write_ctx * ctx,u_int offset,u_int pwidth,u_int * width,struct grid_cell * gc,const struct utf8_data * ud)733 status_prompt_redraw_character(struct screen_write_ctx *ctx, u_int offset,
734 u_int pwidth, u_int *width, struct grid_cell *gc,
735 const struct utf8_data *ud)
736 {
737 u_char ch;
738
739 if (*width < offset) {
740 *width += ud->width;
741 return (1);
742 }
743 if (*width >= offset + pwidth)
744 return (0);
745 *width += ud->width;
746 if (*width > offset + pwidth)
747 return (0);
748
749 ch = *ud->data;
750 if (ud->size == 1 && (ch <= 0x1f || ch == 0x7f)) {
751 gc->data.data[0] = '^';
752 gc->data.data[1] = (ch == 0x7f) ? '?' : ch|0x40;
753 gc->data.size = gc->data.have = 2;
754 gc->data.width = 2;
755 } else
756 utf8_copy(&gc->data, ud);
757 screen_write_cell(ctx, gc);
758 return (1);
759 }
760
761 /*
762 * Redraw quote indicator '^' if necessary. Return 1 if can continue redrawing,
763 * 0 otherwise.
764 */
765 static int
status_prompt_redraw_quote(const struct client * c,u_int pcursor,struct screen_write_ctx * ctx,u_int offset,u_int pwidth,u_int * width,struct grid_cell * gc)766 status_prompt_redraw_quote(const struct client *c, u_int pcursor,
767 struct screen_write_ctx *ctx, u_int offset, u_int pwidth, u_int *width,
768 struct grid_cell *gc)
769 {
770 struct utf8_data ud;
771
772 if (c->prompt_flags & PROMPT_QUOTENEXT && ctx->s->cx == pcursor + 1) {
773 utf8_set(&ud, '^');
774 return (status_prompt_redraw_character(ctx, offset, pwidth,
775 width, gc, &ud));
776 }
777 return (1);
778 }
779
780 /* Draw client prompt on status line of present else on last line. */
781 int
status_prompt_redraw(struct client * c)782 status_prompt_redraw(struct client *c)
783 {
784 struct status_line *sl = &c->status;
785 struct screen_write_ctx ctx;
786 struct session *s = c->session;
787 struct screen old_screen;
788 u_int i, lines, offset, left, start, width, n;
789 u_int pcursor, pwidth, promptline;
790 struct grid_cell gc;
791 struct format_tree *ft;
792
793 if (c->tty.sx == 0 || c->tty.sy == 0)
794 return (0);
795 memcpy(&old_screen, sl->active, sizeof old_screen);
796
797 lines = status_line_size(c);
798 if (lines <= 1)
799 lines = 1;
800 screen_init(sl->active, c->tty.sx, lines, 0);
801
802 n = options_get_number(s->options, "prompt-cursor-colour");
803 sl->active->default_ccolour = n;
804 n = options_get_number(s->options, "prompt-cursor-style");
805 screen_set_cursor_style(n, &sl->active->default_cstyle,
806 &sl->active->default_mode);
807
808 promptline = status_prompt_line_at(c);
809 if (promptline > lines - 1)
810 promptline = lines - 1;
811
812 ft = format_create_defaults(NULL, c, NULL, NULL, NULL);
813 if (c->prompt_mode == PROMPT_COMMAND)
814 style_apply(&gc, s->options, "message-command-style", ft);
815 else
816 style_apply(&gc, s->options, "message-style", ft);
817 format_free(ft);
818
819 start = format_width(c->prompt_string);
820 if (start > c->tty.sx)
821 start = c->tty.sx;
822
823 screen_write_start(&ctx, sl->active);
824 screen_write_fast_copy(&ctx, &sl->screen, 0, 0, c->tty.sx, lines);
825 screen_write_cursormove(&ctx, 0, promptline, 0);
826 for (offset = 0; offset < c->tty.sx; offset++)
827 screen_write_putc(&ctx, &gc, ' ');
828 screen_write_cursormove(&ctx, 0, promptline, 0);
829 format_draw(&ctx, &gc, start, c->prompt_string, NULL, 0);
830 screen_write_cursormove(&ctx, start, promptline, 0);
831
832 left = c->tty.sx - start;
833 if (left == 0)
834 goto finished;
835
836 pcursor = utf8_strwidth(c->prompt_buffer, c->prompt_index);
837 pwidth = utf8_strwidth(c->prompt_buffer, -1);
838 if (c->prompt_flags & PROMPT_QUOTENEXT)
839 pwidth++;
840 if (pcursor >= left) {
841 /*
842 * The cursor would be outside the screen so start drawing
843 * with it on the right.
844 */
845 offset = (pcursor - left) + 1;
846 pwidth = left;
847 } else
848 offset = 0;
849 if (pwidth > left)
850 pwidth = left;
851 c->prompt_cursor = start + pcursor - offset;
852
853 width = 0;
854 for (i = 0; c->prompt_buffer[i].size != 0; i++) {
855 if (!status_prompt_redraw_quote(c, pcursor, &ctx, offset,
856 pwidth, &width, &gc))
857 break;
858 if (!status_prompt_redraw_character(&ctx, offset, pwidth,
859 &width, &gc, &c->prompt_buffer[i]))
860 break;
861 }
862 status_prompt_redraw_quote(c, pcursor, &ctx, offset, pwidth, &width,
863 &gc);
864
865 finished:
866 screen_write_stop(&ctx);
867
868 if (grid_compare(sl->active->grid, old_screen.grid) == 0) {
869 screen_free(&old_screen);
870 return (0);
871 }
872 screen_free(&old_screen);
873 return (1);
874 }
875
876 /* Is this a separator? */
877 static int
status_prompt_in_list(const char * ws,const struct utf8_data * ud)878 status_prompt_in_list(const char *ws, const struct utf8_data *ud)
879 {
880 if (ud->size != 1 || ud->width != 1)
881 return (0);
882 return (strchr(ws, *ud->data) != NULL);
883 }
884
885 /* Is this a space? */
886 static int
status_prompt_space(const struct utf8_data * ud)887 status_prompt_space(const struct utf8_data *ud)
888 {
889 if (ud->size != 1 || ud->width != 1)
890 return (0);
891 return (*ud->data == ' ');
892 }
893
894 /*
895 * Translate key from vi to emacs. Return 0 to drop key, 1 to process the key
896 * as an emacs key; return 2 to append to the buffer.
897 */
898 static int
status_prompt_translate_key(struct client * c,key_code key,key_code * new_key)899 status_prompt_translate_key(struct client *c, key_code key, key_code *new_key)
900 {
901 if (c->prompt_mode == PROMPT_ENTRY) {
902 switch (key) {
903 case 'a'|KEYC_CTRL:
904 case 'c'|KEYC_CTRL:
905 case 'e'|KEYC_CTRL:
906 case 'g'|KEYC_CTRL:
907 case 'h'|KEYC_CTRL:
908 case '\011': /* Tab */
909 case 'k'|KEYC_CTRL:
910 case 'n'|KEYC_CTRL:
911 case 'p'|KEYC_CTRL:
912 case 't'|KEYC_CTRL:
913 case 'u'|KEYC_CTRL:
914 case 'v'|KEYC_CTRL:
915 case 'w'|KEYC_CTRL:
916 case 'y'|KEYC_CTRL:
917 case '\n':
918 case '\r':
919 case KEYC_LEFT|KEYC_CTRL:
920 case KEYC_RIGHT|KEYC_CTRL:
921 case KEYC_BSPACE:
922 case KEYC_DC:
923 case KEYC_DOWN:
924 case KEYC_END:
925 case KEYC_HOME:
926 case KEYC_LEFT:
927 case KEYC_RIGHT:
928 case KEYC_UP:
929 *new_key = key;
930 return (1);
931 case '\033': /* Escape */
932 c->prompt_mode = PROMPT_COMMAND;
933 c->flags |= CLIENT_REDRAWSTATUS;
934 return (0);
935 }
936 *new_key = key;
937 return (2);
938 }
939
940 switch (key) {
941 case KEYC_BSPACE:
942 *new_key = KEYC_LEFT;
943 return (1);
944 case 'A':
945 case 'I':
946 case 'C':
947 case 's':
948 case 'a':
949 c->prompt_mode = PROMPT_ENTRY;
950 c->flags |= CLIENT_REDRAWSTATUS;
951 break; /* switch mode and... */
952 case 'S':
953 c->prompt_mode = PROMPT_ENTRY;
954 c->flags |= CLIENT_REDRAWSTATUS;
955 *new_key = 'u'|KEYC_CTRL;
956 return (1);
957 case 'i':
958 case '\033': /* Escape */
959 c->prompt_mode = PROMPT_ENTRY;
960 c->flags |= CLIENT_REDRAWSTATUS;
961 return (0);
962 }
963
964 switch (key) {
965 case 'A':
966 case '$':
967 *new_key = KEYC_END;
968 return (1);
969 case 'I':
970 case '0':
971 case '^':
972 *new_key = KEYC_HOME;
973 return (1);
974 case 'C':
975 case 'D':
976 *new_key = 'k'|KEYC_CTRL;
977 return (1);
978 case KEYC_BSPACE:
979 case 'X':
980 *new_key = KEYC_BSPACE;
981 return (1);
982 case 'b':
983 *new_key = 'b'|KEYC_META;
984 return (1);
985 case 'B':
986 *new_key = 'B'|KEYC_VI;
987 return (1);
988 case 'd':
989 *new_key = 'u'|KEYC_CTRL;
990 return (1);
991 case 'e':
992 *new_key = 'e'|KEYC_VI;
993 return (1);
994 case 'E':
995 *new_key = 'E'|KEYC_VI;
996 return (1);
997 case 'w':
998 *new_key = 'w'|KEYC_VI;
999 return (1);
1000 case 'W':
1001 *new_key = 'W'|KEYC_VI;
1002 return (1);
1003 case 'p':
1004 *new_key = 'y'|KEYC_CTRL;
1005 return (1);
1006 case 'q':
1007 *new_key = 'c'|KEYC_CTRL;
1008 return (1);
1009 case 's':
1010 case KEYC_DC:
1011 case 'x':
1012 *new_key = KEYC_DC;
1013 return (1);
1014 case KEYC_DOWN:
1015 case 'j':
1016 *new_key = KEYC_DOWN;
1017 return (1);
1018 case KEYC_LEFT:
1019 case 'h':
1020 *new_key = KEYC_LEFT;
1021 return (1);
1022 case 'a':
1023 case KEYC_RIGHT:
1024 case 'l':
1025 *new_key = KEYC_RIGHT;
1026 return (1);
1027 case KEYC_UP:
1028 case 'k':
1029 *new_key = KEYC_UP;
1030 return (1);
1031 case 'h'|KEYC_CTRL:
1032 case 'c'|KEYC_CTRL:
1033 case '\n':
1034 case '\r':
1035 return (1);
1036 }
1037 return (0);
1038 }
1039
1040 /* Paste into prompt. */
1041 static int
status_prompt_paste(struct client * c)1042 status_prompt_paste(struct client *c)
1043 {
1044 struct paste_buffer *pb;
1045 const char *bufdata;
1046 size_t size, n, bufsize;
1047 u_int i;
1048 struct utf8_data *ud, *udp;
1049 enum utf8_state more;
1050
1051 size = utf8_strlen(c->prompt_buffer);
1052 if (c->prompt_saved != NULL) {
1053 ud = c->prompt_saved;
1054 n = utf8_strlen(c->prompt_saved);
1055 } else {
1056 if ((pb = paste_get_top(NULL)) == NULL)
1057 return (0);
1058 bufdata = paste_buffer_data(pb, &bufsize);
1059 ud = udp = xreallocarray(NULL, bufsize + 1, sizeof *ud);
1060 for (i = 0; i != bufsize; /* nothing */) {
1061 more = utf8_open(udp, bufdata[i]);
1062 if (more == UTF8_MORE) {
1063 while (++i != bufsize && more == UTF8_MORE)
1064 more = utf8_append(udp, bufdata[i]);
1065 if (more == UTF8_DONE) {
1066 udp++;
1067 continue;
1068 }
1069 i -= udp->have;
1070 }
1071 if (bufdata[i] <= 31 || bufdata[i] >= 127)
1072 break;
1073 utf8_set(udp, bufdata[i]);
1074 udp++;
1075 i++;
1076 }
1077 udp->size = 0;
1078 n = udp - ud;
1079 }
1080 if (n != 0) {
1081 c->prompt_buffer = xreallocarray(c->prompt_buffer, size + n + 1,
1082 sizeof *c->prompt_buffer);
1083 if (c->prompt_index == size) {
1084 memcpy(c->prompt_buffer + c->prompt_index, ud,
1085 n * sizeof *c->prompt_buffer);
1086 c->prompt_index += n;
1087 c->prompt_buffer[c->prompt_index].size = 0;
1088 } else {
1089 memmove(c->prompt_buffer + c->prompt_index + n,
1090 c->prompt_buffer + c->prompt_index,
1091 (size + 1 - c->prompt_index) *
1092 sizeof *c->prompt_buffer);
1093 memcpy(c->prompt_buffer + c->prompt_index, ud,
1094 n * sizeof *c->prompt_buffer);
1095 c->prompt_index += n;
1096 }
1097 }
1098 if (ud != c->prompt_saved)
1099 free(ud);
1100 return (1);
1101 }
1102
1103 /* Finish completion. */
1104 static int
status_prompt_replace_complete(struct client * c,const char * s)1105 status_prompt_replace_complete(struct client *c, const char *s)
1106 {
1107 char word[64], *allocated = NULL;
1108 size_t size, n, off, idx, used;
1109 struct utf8_data *first, *last, *ud;
1110
1111 /* Work out where the cursor currently is. */
1112 idx = c->prompt_index;
1113 if (idx != 0)
1114 idx--;
1115 size = utf8_strlen(c->prompt_buffer);
1116
1117 /* Find the word we are in. */
1118 first = &c->prompt_buffer[idx];
1119 while (first > c->prompt_buffer && !status_prompt_space(first))
1120 first--;
1121 while (first->size != 0 && status_prompt_space(first))
1122 first++;
1123 last = &c->prompt_buffer[idx];
1124 while (last->size != 0 && !status_prompt_space(last))
1125 last++;
1126 while (last > c->prompt_buffer && status_prompt_space(last))
1127 last--;
1128 if (last->size != 0)
1129 last++;
1130 if (last < first)
1131 return (0);
1132 if (s == NULL) {
1133 used = 0;
1134 for (ud = first; ud < last; ud++) {
1135 if (used + ud->size >= sizeof word)
1136 break;
1137 memcpy(word + used, ud->data, ud->size);
1138 used += ud->size;
1139 }
1140 if (ud != last)
1141 return (0);
1142 word[used] = '\0';
1143 }
1144
1145 /* Try to complete it. */
1146 if (s == NULL) {
1147 allocated = status_prompt_complete(c, word,
1148 first - c->prompt_buffer);
1149 if (allocated == NULL)
1150 return (0);
1151 s = allocated;
1152 }
1153
1154 /* Trim out word. */
1155 n = size - (last - c->prompt_buffer) + 1; /* with \0 */
1156 memmove(first, last, n * sizeof *c->prompt_buffer);
1157 size -= last - first;
1158
1159 /* Insert the new word. */
1160 size += strlen(s);
1161 off = first - c->prompt_buffer;
1162 c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 1,
1163 sizeof *c->prompt_buffer);
1164 first = c->prompt_buffer + off;
1165 memmove(first + strlen(s), first, n * sizeof *c->prompt_buffer);
1166 for (idx = 0; idx < strlen(s); idx++)
1167 utf8_set(&first[idx], s[idx]);
1168 c->prompt_index = (first - c->prompt_buffer) + strlen(s);
1169
1170 free(allocated);
1171 return (1);
1172 }
1173
1174 /* Prompt forward to the next beginning of a word. */
1175 static void
status_prompt_forward_word(struct client * c,size_t size,int vi,const char * separators)1176 status_prompt_forward_word(struct client *c, size_t size, int vi,
1177 const char *separators)
1178 {
1179 size_t idx = c->prompt_index;
1180 int word_is_separators;
1181
1182 /* In emacs mode, skip until the first non-whitespace character. */
1183 if (!vi)
1184 while (idx != size &&
1185 status_prompt_space(&c->prompt_buffer[idx]))
1186 idx++;
1187
1188 /* Can't move forward if we're already at the end. */
1189 if (idx == size) {
1190 c->prompt_index = idx;
1191 return;
1192 }
1193
1194 /* Determine the current character class (separators or not). */
1195 word_is_separators = status_prompt_in_list(separators,
1196 &c->prompt_buffer[idx]) &&
1197 !status_prompt_space(&c->prompt_buffer[idx]);
1198
1199 /* Skip ahead until the first space or opposite character class. */
1200 do {
1201 idx++;
1202 if (status_prompt_space(&c->prompt_buffer[idx])) {
1203 /* In vi mode, go to the start of the next word. */
1204 if (vi)
1205 while (idx != size &&
1206 status_prompt_space(&c->prompt_buffer[idx]))
1207 idx++;
1208 break;
1209 }
1210 } while (idx != size && word_is_separators == status_prompt_in_list(
1211 separators, &c->prompt_buffer[idx]));
1212
1213 c->prompt_index = idx;
1214 }
1215
1216 /* Prompt forward to the next end of a word. */
1217 static void
status_prompt_end_word(struct client * c,size_t size,const char * separators)1218 status_prompt_end_word(struct client *c, size_t size, const char *separators)
1219 {
1220 size_t idx = c->prompt_index;
1221 int word_is_separators;
1222
1223 /* Can't move forward if we're already at the end. */
1224 if (idx == size)
1225 return;
1226
1227 /* Find the next word. */
1228 do {
1229 idx++;
1230 if (idx == size) {
1231 c->prompt_index = idx;
1232 return;
1233 }
1234 } while (status_prompt_space(&c->prompt_buffer[idx]));
1235
1236 /* Determine the character class (separators or not). */
1237 word_is_separators = status_prompt_in_list(separators,
1238 &c->prompt_buffer[idx]);
1239
1240 /* Skip ahead until the next space or opposite character class. */
1241 do {
1242 idx++;
1243 if (idx == size)
1244 break;
1245 } while (!status_prompt_space(&c->prompt_buffer[idx]) &&
1246 word_is_separators == status_prompt_in_list(separators,
1247 &c->prompt_buffer[idx]));
1248
1249 /* Back up to the previous character to stop at the end of the word. */
1250 c->prompt_index = idx - 1;
1251 }
1252
1253 /* Prompt backward to the previous beginning of a word. */
1254 static void
status_prompt_backward_word(struct client * c,const char * separators)1255 status_prompt_backward_word(struct client *c, const char *separators)
1256 {
1257 size_t idx = c->prompt_index;
1258 int word_is_separators;
1259
1260 /* Find non-whitespace. */
1261 while (idx != 0) {
1262 --idx;
1263 if (!status_prompt_space(&c->prompt_buffer[idx]))
1264 break;
1265 }
1266 word_is_separators = status_prompt_in_list(separators,
1267 &c->prompt_buffer[idx]);
1268
1269 /* Find the character before the beginning of the word. */
1270 while (idx != 0) {
1271 --idx;
1272 if (status_prompt_space(&c->prompt_buffer[idx]) ||
1273 word_is_separators != status_prompt_in_list(separators,
1274 &c->prompt_buffer[idx])) {
1275 /* Go back to the word. */
1276 idx++;
1277 break;
1278 }
1279 }
1280 c->prompt_index = idx;
1281 }
1282
1283 /* Handle keys in prompt. */
1284 int
status_prompt_key(struct client * c,key_code key)1285 status_prompt_key(struct client *c, key_code key)
1286 {
1287 struct options *oo = c->session->options;
1288 char *s, *cp, prefix = '=';
1289 const char *histstr, *separators = NULL, *keystring;
1290 size_t size, idx;
1291 struct utf8_data tmp;
1292 int keys, word_is_separators;
1293
1294 if (c->prompt_flags & PROMPT_KEY) {
1295 keystring = key_string_lookup_key(key, 0);
1296 c->prompt_inputcb(c, c->prompt_data, keystring, 1);
1297 status_prompt_clear(c);
1298 return (0);
1299 }
1300 size = utf8_strlen(c->prompt_buffer);
1301
1302 if (c->prompt_flags & PROMPT_NUMERIC) {
1303 if (key >= '0' && key <= '9')
1304 goto append_key;
1305 s = utf8_tocstr(c->prompt_buffer);
1306 c->prompt_inputcb(c, c->prompt_data, s, 1);
1307 status_prompt_clear(c);
1308 free(s);
1309 return (1);
1310 }
1311 key &= ~KEYC_MASK_FLAGS;
1312
1313 if (c->prompt_flags & (PROMPT_SINGLE|PROMPT_QUOTENEXT)) {
1314 if ((key & KEYC_MASK_KEY) == KEYC_BSPACE)
1315 key = 0x7f;
1316 else if ((key & KEYC_MASK_KEY) > 0x7f) {
1317 if (!KEYC_IS_UNICODE(key))
1318 return (0);
1319 key &= KEYC_MASK_KEY;
1320 } else
1321 key &= (key & KEYC_CTRL) ? 0x1f : KEYC_MASK_KEY;
1322 c->prompt_flags &= ~PROMPT_QUOTENEXT;
1323 goto append_key;
1324 }
1325
1326 keys = options_get_number(c->session->options, "status-keys");
1327 if (keys == MODEKEY_VI) {
1328 switch (status_prompt_translate_key(c, key, &key)) {
1329 case 1:
1330 goto process_key;
1331 case 2:
1332 goto append_key;
1333 default:
1334 return (0);
1335 }
1336 }
1337
1338 process_key:
1339 switch (key) {
1340 case KEYC_LEFT:
1341 case 'b'|KEYC_CTRL:
1342 if (c->prompt_index > 0) {
1343 c->prompt_index--;
1344 break;
1345 }
1346 break;
1347 case KEYC_RIGHT:
1348 case 'f'|KEYC_CTRL:
1349 if (c->prompt_index < size) {
1350 c->prompt_index++;
1351 break;
1352 }
1353 break;
1354 case KEYC_HOME:
1355 case 'a'|KEYC_CTRL:
1356 if (c->prompt_index != 0) {
1357 c->prompt_index = 0;
1358 break;
1359 }
1360 break;
1361 case KEYC_END:
1362 case 'e'|KEYC_CTRL:
1363 if (c->prompt_index != size) {
1364 c->prompt_index = size;
1365 break;
1366 }
1367 break;
1368 case '\011': /* Tab */
1369 if (status_prompt_replace_complete(c, NULL))
1370 goto changed;
1371 break;
1372 case KEYC_BSPACE:
1373 case 'h'|KEYC_CTRL:
1374 if (c->prompt_index != 0) {
1375 if (c->prompt_index == size)
1376 c->prompt_buffer[--c->prompt_index].size = 0;
1377 else {
1378 memmove(c->prompt_buffer + c->prompt_index - 1,
1379 c->prompt_buffer + c->prompt_index,
1380 (size + 1 - c->prompt_index) *
1381 sizeof *c->prompt_buffer);
1382 c->prompt_index--;
1383 }
1384 goto changed;
1385 }
1386 break;
1387 case KEYC_DC:
1388 case 'd'|KEYC_CTRL:
1389 if (c->prompt_index != size) {
1390 memmove(c->prompt_buffer + c->prompt_index,
1391 c->prompt_buffer + c->prompt_index + 1,
1392 (size + 1 - c->prompt_index) *
1393 sizeof *c->prompt_buffer);
1394 goto changed;
1395 }
1396 break;
1397 case 'u'|KEYC_CTRL:
1398 c->prompt_buffer[0].size = 0;
1399 c->prompt_index = 0;
1400 goto changed;
1401 case 'k'|KEYC_CTRL:
1402 if (c->prompt_index < size) {
1403 c->prompt_buffer[c->prompt_index].size = 0;
1404 goto changed;
1405 }
1406 break;
1407 case 'w'|KEYC_CTRL:
1408 separators = options_get_string(oo, "word-separators");
1409 idx = c->prompt_index;
1410
1411 /* Find non-whitespace. */
1412 while (idx != 0) {
1413 idx--;
1414 if (!status_prompt_space(&c->prompt_buffer[idx]))
1415 break;
1416 }
1417 word_is_separators = status_prompt_in_list(separators,
1418 &c->prompt_buffer[idx]);
1419
1420 /* Find the character before the beginning of the word. */
1421 while (idx != 0) {
1422 idx--;
1423 if (status_prompt_space(&c->prompt_buffer[idx]) ||
1424 word_is_separators != status_prompt_in_list(
1425 separators, &c->prompt_buffer[idx])) {
1426 /* Go back to the word. */
1427 idx++;
1428 break;
1429 }
1430 }
1431
1432 free(c->prompt_saved);
1433 c->prompt_saved = xcalloc(sizeof *c->prompt_buffer,
1434 (c->prompt_index - idx) + 1);
1435 memcpy(c->prompt_saved, c->prompt_buffer + idx,
1436 (c->prompt_index - idx) * sizeof *c->prompt_buffer);
1437
1438 memmove(c->prompt_buffer + idx,
1439 c->prompt_buffer + c->prompt_index,
1440 (size + 1 - c->prompt_index) *
1441 sizeof *c->prompt_buffer);
1442 memset(c->prompt_buffer + size - (c->prompt_index - idx),
1443 '\0', (c->prompt_index - idx) * sizeof *c->prompt_buffer);
1444 c->prompt_index = idx;
1445
1446 goto changed;
1447 case KEYC_RIGHT|KEYC_CTRL:
1448 case 'f'|KEYC_META:
1449 separators = options_get_string(oo, "word-separators");
1450 status_prompt_forward_word(c, size, 0, separators);
1451 goto changed;
1452 case 'E'|KEYC_VI:
1453 status_prompt_end_word(c, size, "");
1454 goto changed;
1455 case 'e'|KEYC_VI:
1456 separators = options_get_string(oo, "word-separators");
1457 status_prompt_end_word(c, size, separators);
1458 goto changed;
1459 case 'W'|KEYC_VI:
1460 status_prompt_forward_word(c, size, 1, "");
1461 goto changed;
1462 case 'w'|KEYC_VI:
1463 separators = options_get_string(oo, "word-separators");
1464 status_prompt_forward_word(c, size, 1, separators);
1465 goto changed;
1466 case 'B'|KEYC_VI:
1467 status_prompt_backward_word(c, "");
1468 goto changed;
1469 case KEYC_LEFT|KEYC_CTRL:
1470 case 'b'|KEYC_META:
1471 separators = options_get_string(oo, "word-separators");
1472 status_prompt_backward_word(c, separators);
1473 goto changed;
1474 case KEYC_UP:
1475 case 'p'|KEYC_CTRL:
1476 histstr = status_prompt_up_history(c->prompt_hindex,
1477 c->prompt_type);
1478 if (histstr == NULL)
1479 break;
1480 free(c->prompt_buffer);
1481 c->prompt_buffer = utf8_fromcstr(histstr);
1482 c->prompt_index = utf8_strlen(c->prompt_buffer);
1483 goto changed;
1484 case KEYC_DOWN:
1485 case 'n'|KEYC_CTRL:
1486 histstr = status_prompt_down_history(c->prompt_hindex,
1487 c->prompt_type);
1488 if (histstr == NULL)
1489 break;
1490 free(c->prompt_buffer);
1491 c->prompt_buffer = utf8_fromcstr(histstr);
1492 c->prompt_index = utf8_strlen(c->prompt_buffer);
1493 goto changed;
1494 case 'y'|KEYC_CTRL:
1495 if (status_prompt_paste(c))
1496 goto changed;
1497 break;
1498 case 't'|KEYC_CTRL:
1499 idx = c->prompt_index;
1500 if (idx < size)
1501 idx++;
1502 if (idx >= 2) {
1503 utf8_copy(&tmp, &c->prompt_buffer[idx - 2]);
1504 utf8_copy(&c->prompt_buffer[idx - 2],
1505 &c->prompt_buffer[idx - 1]);
1506 utf8_copy(&c->prompt_buffer[idx - 1], &tmp);
1507 c->prompt_index = idx;
1508 goto changed;
1509 }
1510 break;
1511 case '\r':
1512 case '\n':
1513 s = utf8_tocstr(c->prompt_buffer);
1514 if (*s != '\0')
1515 status_prompt_add_history(s, c->prompt_type);
1516 if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
1517 status_prompt_clear(c);
1518 free(s);
1519 break;
1520 case '\033': /* Escape */
1521 case 'c'|KEYC_CTRL:
1522 case 'g'|KEYC_CTRL:
1523 if (c->prompt_inputcb(c, c->prompt_data, NULL, 1) == 0)
1524 status_prompt_clear(c);
1525 break;
1526 case 'r'|KEYC_CTRL:
1527 if (~c->prompt_flags & PROMPT_INCREMENTAL)
1528 break;
1529 if (c->prompt_buffer[0].size == 0) {
1530 prefix = '=';
1531 free(c->prompt_buffer);
1532 c->prompt_buffer = utf8_fromcstr(c->prompt_last);
1533 c->prompt_index = utf8_strlen(c->prompt_buffer);
1534 } else
1535 prefix = '-';
1536 goto changed;
1537 case 's'|KEYC_CTRL:
1538 if (~c->prompt_flags & PROMPT_INCREMENTAL)
1539 break;
1540 if (c->prompt_buffer[0].size == 0) {
1541 prefix = '=';
1542 free(c->prompt_buffer);
1543 c->prompt_buffer = utf8_fromcstr(c->prompt_last);
1544 c->prompt_index = utf8_strlen(c->prompt_buffer);
1545 } else
1546 prefix = '+';
1547 goto changed;
1548 case 'v'|KEYC_CTRL:
1549 c->prompt_flags |= PROMPT_QUOTENEXT;
1550 break;
1551 default:
1552 goto append_key;
1553 }
1554
1555 c->flags |= CLIENT_REDRAWSTATUS;
1556 return (0);
1557
1558 append_key:
1559 if (key <= 0x7f) {
1560 utf8_set(&tmp, key);
1561 if (key <= 0x1f || key == 0x7f)
1562 tmp.width = 2;
1563 } else if (KEYC_IS_UNICODE(key))
1564 utf8_to_data(key, &tmp);
1565 else
1566 return (0);
1567
1568 c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 2,
1569 sizeof *c->prompt_buffer);
1570
1571 if (c->prompt_index == size) {
1572 utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
1573 c->prompt_index++;
1574 c->prompt_buffer[c->prompt_index].size = 0;
1575 } else {
1576 memmove(c->prompt_buffer + c->prompt_index + 1,
1577 c->prompt_buffer + c->prompt_index,
1578 (size + 1 - c->prompt_index) *
1579 sizeof *c->prompt_buffer);
1580 utf8_copy(&c->prompt_buffer[c->prompt_index], &tmp);
1581 c->prompt_index++;
1582 }
1583
1584 if (c->prompt_flags & PROMPT_SINGLE) {
1585 if (utf8_strlen(c->prompt_buffer) != 1)
1586 status_prompt_clear(c);
1587 else {
1588 s = utf8_tocstr(c->prompt_buffer);
1589 if (c->prompt_inputcb(c, c->prompt_data, s, 1) == 0)
1590 status_prompt_clear(c);
1591 free(s);
1592 }
1593 }
1594
1595 changed:
1596 c->flags |= CLIENT_REDRAWSTATUS;
1597 if (c->prompt_flags & PROMPT_INCREMENTAL) {
1598 s = utf8_tocstr(c->prompt_buffer);
1599 xasprintf(&cp, "%c%s", prefix, s);
1600 c->prompt_inputcb(c, c->prompt_data, cp, 0);
1601 free(cp);
1602 free(s);
1603 }
1604 return (0);
1605 }
1606
1607 /* Get previous line from the history. */
1608 static const char *
status_prompt_up_history(u_int * idx,u_int type)1609 status_prompt_up_history(u_int *idx, u_int type)
1610 {
1611 /*
1612 * History runs from 0 to size - 1. Index is from 0 to size. Zero is
1613 * empty.
1614 */
1615
1616 if (status_prompt_hsize[type] == 0 ||
1617 idx[type] == status_prompt_hsize[type])
1618 return (NULL);
1619 idx[type]++;
1620 return (status_prompt_hlist[type][status_prompt_hsize[type] - idx[type]]);
1621 }
1622
1623 /* Get next line from the history. */
1624 static const char *
status_prompt_down_history(u_int * idx,u_int type)1625 status_prompt_down_history(u_int *idx, u_int type)
1626 {
1627 if (status_prompt_hsize[type] == 0 || idx[type] == 0)
1628 return ("");
1629 idx[type]--;
1630 if (idx[type] == 0)
1631 return ("");
1632 return (status_prompt_hlist[type][status_prompt_hsize[type] - idx[type]]);
1633 }
1634
1635 /* Add line to the history. */
1636 static void
status_prompt_add_history(const char * line,u_int type)1637 status_prompt_add_history(const char *line, u_int type)
1638 {
1639 u_int i, oldsize, newsize, freecount, hlimit, new = 1;
1640 size_t movesize;
1641
1642 oldsize = status_prompt_hsize[type];
1643 if (oldsize > 0 &&
1644 strcmp(status_prompt_hlist[type][oldsize - 1], line) == 0)
1645 new = 0;
1646
1647 hlimit = options_get_number(global_options, "prompt-history-limit");
1648 if (hlimit > oldsize) {
1649 if (new == 0)
1650 return;
1651 newsize = oldsize + new;
1652 } else {
1653 newsize = hlimit;
1654 freecount = oldsize + new - newsize;
1655 if (freecount > oldsize)
1656 freecount = oldsize;
1657 if (freecount == 0)
1658 return;
1659 for (i = 0; i < freecount; i++)
1660 free(status_prompt_hlist[type][i]);
1661 movesize = (oldsize - freecount) *
1662 sizeof *status_prompt_hlist[type];
1663 if (movesize > 0) {
1664 memmove(&status_prompt_hlist[type][0],
1665 &status_prompt_hlist[type][freecount], movesize);
1666 }
1667 }
1668
1669 if (newsize == 0) {
1670 free(status_prompt_hlist[type]);
1671 status_prompt_hlist[type] = NULL;
1672 } else if (newsize != oldsize) {
1673 status_prompt_hlist[type] =
1674 xreallocarray(status_prompt_hlist[type], newsize,
1675 sizeof *status_prompt_hlist[type]);
1676 }
1677
1678 if (new == 1 && newsize > 0)
1679 status_prompt_hlist[type][newsize - 1] = xstrdup(line);
1680 status_prompt_hsize[type] = newsize;
1681 }
1682
1683 /* Add to completion list. */
1684 static void
status_prompt_add_list(char *** list,u_int * size,const char * s)1685 status_prompt_add_list(char ***list, u_int *size, const char *s)
1686 {
1687 u_int i;
1688
1689 for (i = 0; i < *size; i++) {
1690 if (strcmp((*list)[i], s) == 0)
1691 return;
1692 }
1693 *list = xreallocarray(*list, (*size) + 1, sizeof **list);
1694 (*list)[(*size)++] = xstrdup(s);
1695 }
1696
1697 /* Build completion list. */
1698 static char **
status_prompt_complete_list(u_int * size,const char * s,int at_start)1699 status_prompt_complete_list(u_int *size, const char *s, int at_start)
1700 {
1701 char **list = NULL, *tmp;
1702 const char **layout, *value, *cp;
1703 const struct cmd_entry **cmdent;
1704 const struct options_table_entry *oe;
1705 size_t slen = strlen(s), valuelen;
1706 struct options_entry *o;
1707 struct options_array_item *a;
1708 const char *layouts[] = {
1709 "even-horizontal", "even-vertical",
1710 "main-horizontal", "main-horizontal-mirrored",
1711 "main-vertical", "main-vertical-mirrored", "tiled", NULL
1712 };
1713
1714 *size = 0;
1715 for (cmdent = cmd_table; *cmdent != NULL; cmdent++) {
1716 if (strncmp((*cmdent)->name, s, slen) == 0)
1717 status_prompt_add_list(&list, size, (*cmdent)->name);
1718 if ((*cmdent)->alias != NULL &&
1719 strncmp((*cmdent)->alias, s, slen) == 0)
1720 status_prompt_add_list(&list, size, (*cmdent)->alias);
1721 }
1722 o = options_get_only(global_options, "command-alias");
1723 if (o != NULL) {
1724 a = options_array_first(o);
1725 while (a != NULL) {
1726 value = options_array_item_value(a)->string;
1727 if ((cp = strchr(value, '=')) == NULL)
1728 goto next;
1729 valuelen = cp - value;
1730 if (slen > valuelen || strncmp(value, s, slen) != 0)
1731 goto next;
1732
1733 xasprintf(&tmp, "%.*s", (int)valuelen, value);
1734 status_prompt_add_list(&list, size, tmp);
1735 free(tmp);
1736
1737 next:
1738 a = options_array_next(a);
1739 }
1740 }
1741 if (at_start)
1742 return (list);
1743 for (oe = options_table; oe->name != NULL; oe++) {
1744 if (strncmp(oe->name, s, slen) == 0)
1745 status_prompt_add_list(&list, size, oe->name);
1746 }
1747 for (layout = layouts; *layout != NULL; layout++) {
1748 if (strncmp(*layout, s, slen) == 0)
1749 status_prompt_add_list(&list, size, *layout);
1750 }
1751 return (list);
1752 }
1753
1754 /* Find longest prefix. */
1755 static char *
status_prompt_complete_prefix(char ** list,u_int size)1756 status_prompt_complete_prefix(char **list, u_int size)
1757 {
1758 char *out;
1759 u_int i;
1760 size_t j;
1761
1762 if (list == NULL || size == 0)
1763 return (NULL);
1764 out = xstrdup(list[0]);
1765 for (i = 1; i < size; i++) {
1766 j = strlen(list[i]);
1767 if (j > strlen(out))
1768 j = strlen(out);
1769 for (; j > 0; j--) {
1770 if (out[j - 1] != list[i][j - 1])
1771 out[j - 1] = '\0';
1772 }
1773 }
1774 return (out);
1775 }
1776
1777 /* Complete word menu callback. */
1778 static void
status_prompt_menu_callback(__unused struct menu * menu,u_int idx,key_code key,void * data)1779 status_prompt_menu_callback(__unused struct menu *menu, u_int idx, key_code key,
1780 void *data)
1781 {
1782 struct status_prompt_menu *spm = data;
1783 struct client *c = spm->c;
1784 u_int i;
1785 char *s;
1786
1787 if (key != KEYC_NONE) {
1788 idx += spm->start;
1789 if (spm->flag == '\0')
1790 s = xstrdup(spm->list[idx]);
1791 else
1792 xasprintf(&s, "-%c%s", spm->flag, spm->list[idx]);
1793 if (c->prompt_type == PROMPT_TYPE_WINDOW_TARGET) {
1794 free(c->prompt_buffer);
1795 c->prompt_buffer = utf8_fromcstr(s);
1796 c->prompt_index = utf8_strlen(c->prompt_buffer);
1797 c->flags |= CLIENT_REDRAWSTATUS;
1798 } else if (status_prompt_replace_complete(c, s))
1799 c->flags |= CLIENT_REDRAWSTATUS;
1800 free(s);
1801 }
1802
1803 for (i = 0; i < spm->size; i++)
1804 free(spm->list[i]);
1805 free(spm->list);
1806 }
1807
1808 /* Show complete word menu. */
1809 static int
status_prompt_complete_list_menu(struct client * c,char ** list,u_int size,u_int offset,char flag)1810 status_prompt_complete_list_menu(struct client *c, char **list, u_int size,
1811 u_int offset, char flag)
1812 {
1813 struct menu *menu;
1814 struct menu_item item;
1815 struct status_prompt_menu *spm;
1816 u_int lines = status_line_size(c), height, i;
1817 u_int py;
1818
1819 if (size <= 1)
1820 return (0);
1821 if (c->tty.sy - lines < 3)
1822 return (0);
1823
1824 spm = xmalloc(sizeof *spm);
1825 spm->c = c;
1826 spm->size = size;
1827 spm->list = list;
1828 spm->flag = flag;
1829
1830 height = c->tty.sy - lines - 2;
1831 if (height > 10)
1832 height = 10;
1833 if (height > size)
1834 height = size;
1835 spm->start = size - height;
1836
1837 menu = menu_create("");
1838 for (i = spm->start; i < size; i++) {
1839 item.name = list[i];
1840 item.key = '0' + (i - spm->start);
1841 item.command = NULL;
1842 menu_add_item(menu, &item, NULL, c, NULL);
1843 }
1844
1845 if (options_get_number(c->session->options, "status-position") == 0)
1846 py = lines;
1847 else
1848 py = c->tty.sy - 3 - height;
1849 offset += utf8_cstrwidth(c->prompt_string);
1850 if (offset > 2)
1851 offset -= 2;
1852 else
1853 offset = 0;
1854
1855 if (menu_display(menu, MENU_NOMOUSE|MENU_TAB, 0, NULL, offset, py, c,
1856 BOX_LINES_DEFAULT, NULL, NULL, NULL, NULL,
1857 status_prompt_menu_callback, spm) != 0) {
1858 menu_free(menu);
1859 free(spm);
1860 return (0);
1861 }
1862 return (1);
1863 }
1864
1865 /* Show complete word menu. */
1866 static char *
status_prompt_complete_window_menu(struct client * c,struct session * s,const char * word,u_int offset,char flag)1867 status_prompt_complete_window_menu(struct client *c, struct session *s,
1868 const char *word, u_int offset, char flag)
1869 {
1870 struct menu *menu;
1871 struct menu_item item;
1872 struct status_prompt_menu *spm;
1873 struct winlink *wl;
1874 char **list = NULL, *tmp;
1875 u_int lines = status_line_size(c), height;
1876 u_int py, size = 0;
1877
1878 if (c->tty.sy - lines < 3)
1879 return (NULL);
1880
1881 spm = xmalloc(sizeof *spm);
1882 spm->c = c;
1883 spm->flag = flag;
1884
1885 height = c->tty.sy - lines - 2;
1886 if (height > 10)
1887 height = 10;
1888 spm->start = 0;
1889
1890 menu = menu_create("");
1891 RB_FOREACH(wl, winlinks, &s->windows) {
1892 if (word != NULL && *word != '\0') {
1893 xasprintf(&tmp, "%d", wl->idx);
1894 if (strncmp(tmp, word, strlen(word)) != 0) {
1895 free(tmp);
1896 continue;
1897 }
1898 free(tmp);
1899 }
1900
1901 list = xreallocarray(list, size + 1, sizeof *list);
1902 if (c->prompt_type == PROMPT_TYPE_WINDOW_TARGET) {
1903 xasprintf(&tmp, "%d (%s)", wl->idx, wl->window->name);
1904 xasprintf(&list[size++], "%d", wl->idx);
1905 } else {
1906 xasprintf(&tmp, "%s:%d (%s)", s->name, wl->idx,
1907 wl->window->name);
1908 xasprintf(&list[size++], "%s:%d", s->name, wl->idx);
1909 }
1910 item.name = tmp;
1911 item.key = '0' + size - 1;
1912 item.command = NULL;
1913 menu_add_item(menu, &item, NULL, c, NULL);
1914 free(tmp);
1915
1916 if (size == height)
1917 break;
1918 }
1919 if (size == 0) {
1920 menu_free(menu);
1921 free(spm);
1922 return (NULL);
1923 }
1924 if (size == 1) {
1925 menu_free(menu);
1926 if (flag != '\0') {
1927 xasprintf(&tmp, "-%c%s", flag, list[0]);
1928 free(list[0]);
1929 } else
1930 tmp = list[0];
1931 free(list);
1932 free(spm);
1933 return (tmp);
1934 }
1935 if (height > size)
1936 height = size;
1937
1938 spm->size = size;
1939 spm->list = list;
1940
1941 if (options_get_number(c->session->options, "status-position") == 0)
1942 py = lines;
1943 else
1944 py = c->tty.sy - 3 - height;
1945 offset += utf8_cstrwidth(c->prompt_string);
1946 if (offset > 2)
1947 offset -= 2;
1948 else
1949 offset = 0;
1950
1951 if (menu_display(menu, MENU_NOMOUSE|MENU_TAB, 0, NULL, offset, py, c,
1952 BOX_LINES_DEFAULT, NULL, NULL, NULL, NULL,
1953 status_prompt_menu_callback, spm) != 0) {
1954 menu_free(menu);
1955 free(spm);
1956 return (NULL);
1957 }
1958 return (NULL);
1959 }
1960
1961 /* Sort complete list. */
1962 static int
status_prompt_complete_sort(const void * a,const void * b)1963 status_prompt_complete_sort(const void *a, const void *b)
1964 {
1965 const char **aa = (const char **)a, **bb = (const char **)b;
1966
1967 return (strcmp(*aa, *bb));
1968 }
1969
1970 /* Complete a session. */
1971 static char *
status_prompt_complete_session(char *** list,u_int * size,const char * s,char flag)1972 status_prompt_complete_session(char ***list, u_int *size, const char *s,
1973 char flag)
1974 {
1975 struct session *loop;
1976 char *out, *tmp, n[11];
1977
1978 RB_FOREACH(loop, sessions, &sessions) {
1979 if (*s == '\0' || strncmp(loop->name, s, strlen(s)) == 0) {
1980 *list = xreallocarray(*list, (*size) + 2,
1981 sizeof **list);
1982 xasprintf(&(*list)[(*size)++], "%s:", loop->name);
1983 } else if (*s == '$') {
1984 xsnprintf(n, sizeof n, "%u", loop->id);
1985 if (s[1] == '\0' ||
1986 strncmp(n, s + 1, strlen(s) - 1) == 0) {
1987 *list = xreallocarray(*list, (*size) + 2,
1988 sizeof **list);
1989 xasprintf(&(*list)[(*size)++], "$%s:", n);
1990 }
1991 }
1992 }
1993 out = status_prompt_complete_prefix(*list, *size);
1994 if (out != NULL && flag != '\0') {
1995 xasprintf(&tmp, "-%c%s", flag, out);
1996 free(out);
1997 out = tmp;
1998 }
1999 return (out);
2000 }
2001
2002 /* Complete word. */
2003 static char *
status_prompt_complete(struct client * c,const char * word,u_int offset)2004 status_prompt_complete(struct client *c, const char *word, u_int offset)
2005 {
2006 struct session *session;
2007 const char *s, *colon;
2008 char **list = NULL, *copy = NULL, *out = NULL;
2009 char flag = '\0';
2010 u_int size = 0, i;
2011
2012 if (*word == '\0' &&
2013 c->prompt_type != PROMPT_TYPE_TARGET &&
2014 c->prompt_type != PROMPT_TYPE_WINDOW_TARGET)
2015 return (NULL);
2016
2017 if (c->prompt_type != PROMPT_TYPE_TARGET &&
2018 c->prompt_type != PROMPT_TYPE_WINDOW_TARGET &&
2019 strncmp(word, "-t", 2) != 0 &&
2020 strncmp(word, "-s", 2) != 0) {
2021 list = status_prompt_complete_list(&size, word, offset == 0);
2022 if (size == 0)
2023 out = NULL;
2024 else if (size == 1)
2025 xasprintf(&out, "%s ", list[0]);
2026 else
2027 out = status_prompt_complete_prefix(list, size);
2028 goto found;
2029 }
2030
2031 if (c->prompt_type == PROMPT_TYPE_TARGET ||
2032 c->prompt_type == PROMPT_TYPE_WINDOW_TARGET) {
2033 s = word;
2034 flag = '\0';
2035 } else {
2036 s = word + 2;
2037 flag = word[1];
2038 offset += 2;
2039 }
2040
2041 /* If this is a window completion, open the window menu. */
2042 if (c->prompt_type == PROMPT_TYPE_WINDOW_TARGET) {
2043 out = status_prompt_complete_window_menu(c, c->session, s,
2044 offset, '\0');
2045 goto found;
2046 }
2047 colon = strchr(s, ':');
2048
2049 /* If there is no colon, complete as a session. */
2050 if (colon == NULL) {
2051 out = status_prompt_complete_session(&list, &size, s, flag);
2052 goto found;
2053 }
2054
2055 /* If there is a colon but no period, find session and show a menu. */
2056 if (strchr(colon + 1, '.') == NULL) {
2057 if (*s == ':')
2058 session = c->session;
2059 else {
2060 copy = xstrdup(s);
2061 *strchr(copy, ':') = '\0';
2062 session = session_find(copy);
2063 free(copy);
2064 if (session == NULL)
2065 goto found;
2066 }
2067 out = status_prompt_complete_window_menu(c, session, colon + 1,
2068 offset, flag);
2069 if (out == NULL)
2070 return (NULL);
2071 }
2072
2073 found:
2074 if (size != 0) {
2075 qsort(list, size, sizeof *list, status_prompt_complete_sort);
2076 for (i = 0; i < size; i++)
2077 log_debug("complete %u: %s", i, list[i]);
2078 }
2079
2080 if (out != NULL && strcmp(word, out) == 0) {
2081 free(out);
2082 out = NULL;
2083 }
2084 if (out != NULL ||
2085 !status_prompt_complete_list_menu(c, list, size, offset, flag)) {
2086 for (i = 0; i < size; i++)
2087 free(list[i]);
2088 free(list);
2089 }
2090 return (out);
2091 }
2092
2093 /* Return the type of the prompt as an enum. */
2094 enum prompt_type
status_prompt_type(const char * type)2095 status_prompt_type(const char *type)
2096 {
2097 u_int i;
2098
2099 for (i = 0; i < PROMPT_NTYPES; i++) {
2100 if (strcmp(type, status_prompt_type_string(i)) == 0)
2101 return (i);
2102 }
2103 return (PROMPT_TYPE_INVALID);
2104 }
2105
2106 /* Accessor for prompt_type_strings. */
2107 const char *
status_prompt_type_string(u_int type)2108 status_prompt_type_string(u_int type)
2109 {
2110 if (type >= PROMPT_NTYPES)
2111 return ("invalid");
2112 return (prompt_type_strings[type]);
2113 }
2114