eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

display.c (20659B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2017-2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #define _GNU_SOURCE
     28 #include <stdio.h>
     29 #include <signal.h>
     30 #include <sys/stat.h>
     31 #include <sys/ioctl.h>
     32 #include <unistd.h>
     33 #include <wchar.h>
     34 
     35 #include <readline/history.h>
     36 #include <readline/readline.h>
     37 #include <ell/ell.h>
     38 
     39 #include "client/agent.h"
     40 #include "client/command.h"
     41 #include "client/display.h"
     42 
     43 #define IWD_PROMPT \
     44 	"\001" COLOR_GREEN("\002" "[iwd]" "\001") "\002" "# "
     45 #define LINE_LEN 81
     46 
     47 static struct l_signal *window_change_signal;
     48 static struct l_io *io;
     49 static char dashed_line[LINE_LEN] = { [0 ... LINE_LEN - 2] = '-' };
     50 static char empty_line[LINE_LEN] = { [0 ... LINE_LEN - 2] = ' ' };
     51 static struct l_timeout *refresh_timeout;
     52 static struct saved_input *agent_saved_input;
     53 
     54 static struct display_refresh {
     55 	bool enabled;
     56 	char *family;
     57 	char *entity;
     58 	const struct command *cmd;
     59 	char **argv;
     60 	int argc;
     61 	size_t undo_lines;
     62 	struct l_queue *redo_entries;
     63 	bool recording;
     64 } display_refresh = { .enabled = true };
     65 
     66 struct saved_input {
     67 	char *line;
     68 	int point;
     69 };
     70 
     71 static struct saved_input *save_input(void)
     72 {
     73 	struct saved_input *input;
     74 
     75 	if (RL_ISSTATE(RL_STATE_DONE))
     76 		return NULL;
     77 
     78 	input = l_new(struct saved_input, 1);
     79 
     80 	input->point = rl_point;
     81 	input->line = rl_copy_text(0, rl_end);
     82 	rl_save_prompt();
     83 	rl_replace_line("", 0);
     84 	rl_redisplay();
     85 
     86 	return input;
     87 }
     88 
     89 static void restore_input(struct saved_input *input)
     90 {
     91 	if (!input)
     92 		return;
     93 
     94 	rl_restore_prompt();
     95 	rl_replace_line(input->line, 0);
     96 	rl_point = input->point;
     97 	rl_forced_update_display();
     98 
     99 	l_free(input->line);
    100 	l_free(input);
    101 }
    102 
    103 static void display_refresh_undo_lines(void)
    104 {
    105 	size_t num_lines = display_refresh.undo_lines;
    106 
    107 	printf("\033[%dA", (int) num_lines);
    108 
    109 	do {
    110 		printf("%s\n", empty_line);
    111 	} while (--display_refresh.undo_lines);
    112 
    113 	printf("\033[%dA", (int) num_lines);
    114 }
    115 
    116 static void display_refresh_redo_lines(void)
    117 {
    118 	const struct l_queue_entry *entry;
    119 	struct saved_input *input;
    120 
    121 	input = save_input();
    122 
    123 	for (entry = l_queue_get_entries(display_refresh.redo_entries); entry;
    124 							entry = entry->next) {
    125 		char *line = entry->data;
    126 
    127 		printf("%s", line);
    128 
    129 		display_refresh.undo_lines++;
    130 	}
    131 
    132 	restore_input(input);
    133 	display_refresh.recording = true;
    134 
    135 	l_timeout_modify(refresh_timeout, 1);
    136 }
    137 
    138 void display_refresh_reset(void)
    139 {
    140 	l_free(display_refresh.family);
    141 	display_refresh.family = NULL;
    142 
    143 	l_free(display_refresh.entity);
    144 	display_refresh.entity = NULL;
    145 
    146 	display_refresh.cmd = NULL;
    147 
    148 	l_strfreev(display_refresh.argv);
    149 	display_refresh.argv = NULL;
    150 	display_refresh.argc = 0;
    151 
    152 	display_refresh.undo_lines = 0;
    153 	display_refresh.recording = false;
    154 
    155 	l_queue_clear(display_refresh.redo_entries, l_free);
    156 }
    157 
    158 void display_refresh_set_cmd(const char *family, const char *entity,
    159 				const struct command *cmd,
    160 				char **argv, int argc)
    161 {
    162 	int i;
    163 
    164 	if (cmd->refreshable) {
    165 		l_free(display_refresh.family);
    166 		display_refresh.family = l_strdup(family);
    167 
    168 		l_free(display_refresh.entity);
    169 		display_refresh.entity = l_strdup(entity);
    170 
    171 		display_refresh.cmd = cmd;
    172 
    173 		l_strfreev(display_refresh.argv);
    174 		display_refresh.argc = argc;
    175 
    176 		display_refresh.argv = l_new(char *, argc + 1);
    177 
    178 		for (i = 0; i < argc; i++)
    179 			display_refresh.argv[i] = l_strdup(argv[i]);
    180 
    181 		l_queue_clear(display_refresh.redo_entries, l_free);
    182 
    183 		display_refresh.recording = false;
    184 		display_refresh.undo_lines = 0;
    185 
    186 		return;
    187 	}
    188 
    189 	if (display_refresh.family && family &&
    190 				!strcmp(display_refresh.family, family)) {
    191 		struct l_string *buf = l_string_new(128);
    192 		L_AUTO_FREE_VAR(char *, args);
    193 		char *prompt;
    194 
    195 		for (i = 0; i < argc; i++) {
    196 			bool needs_quotes = false;
    197 			char *p;
    198 
    199 			for (p = argv[i]; *p != '\0'; p++) {
    200 				if (*p != ' ')
    201 					continue;
    202 
    203 				needs_quotes = true;
    204 				break;
    205 			}
    206 
    207 			if (needs_quotes)
    208 				l_string_append_printf(buf, "\"%s\" ", argv[i]);
    209 			else
    210 				l_string_append_printf(buf, "%s ", argv[i]);
    211 		}
    212 
    213 		args = l_string_unwrap(buf);
    214 
    215 		prompt = l_strdup_printf(IWD_PROMPT"%s%s%s %s %s\n", family,
    216 						entity ? " " : "",
    217 						entity ? : "",
    218 						cmd->cmd ? : "", args ? : "");
    219 
    220 		l_queue_push_tail(display_refresh.redo_entries, prompt);
    221 		display_refresh.undo_lines++;
    222 
    223 		display_refresh.recording = true;
    224 	} else {
    225 		display_refresh_reset();
    226 	}
    227 }
    228 
    229 static void display_refresh_check_feasibility(void)
    230 {
    231 	const struct winsize ws;
    232 
    233 	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0)
    234 		return;
    235 
    236 	if (ws.ws_col < LINE_LEN - 1) {
    237 		if (display_refresh.enabled) {
    238 			display_refresh.recording = false;
    239 			display(COLOR_YELLOW("Auto-refresh is disabled. "
    240 				"Enlarge window width to at least %u to enable."
    241 				"\n"), LINE_LEN - 1);
    242 			display_refresh.recording = true;
    243 		}
    244 
    245 		display_refresh.enabled = false;
    246 	} else {
    247 		display_refresh.enabled = true;
    248 	}
    249 }
    250 
    251 static void display_refresh_check_applicability(void)
    252 {
    253 	if (display_refresh.enabled && display_refresh.cmd)
    254 		display_refresh_redo_lines();
    255 	else if (display_refresh.cmd)
    256 		display_refresh_timeout_set();
    257 }
    258 
    259 static void timeout_callback(struct l_timeout *timeout, void *user_data)
    260 {
    261 	struct saved_input *input;
    262 
    263 	if (!display_refresh.enabled || !display_refresh.cmd) {
    264 		if (display_refresh.cmd)
    265 			display_refresh_timeout_set();
    266 
    267 		return;
    268 	}
    269 
    270 	input = save_input();
    271 	display_refresh_undo_lines();
    272 	restore_input(input);
    273 
    274 	display_refresh.recording = false;
    275 	display_refresh.cmd->function(display_refresh.entity,
    276 						display_refresh.argv,
    277 						display_refresh.argc);
    278 }
    279 
    280 void display_refresh_timeout_set(void)
    281 {
    282 	if (refresh_timeout)
    283 		l_timeout_modify(refresh_timeout, 1);
    284 	else
    285 		refresh_timeout = l_timeout_create(1, timeout_callback,
    286 							NULL, NULL);
    287 }
    288 
    289 static void display_text(const char *text)
    290 {
    291 	struct saved_input *input = save_input();
    292 
    293 	printf("%s", text);
    294 
    295 	restore_input(input);
    296 
    297 	if (!display_refresh.cmd)
    298 		return;
    299 
    300 	display_refresh.undo_lines++;
    301 
    302 	if (display_refresh.recording)
    303 		l_queue_push_tail(display_refresh.redo_entries, l_strdup(text));
    304 }
    305 
    306 void display(const char *fmt, ...)
    307 {
    308 	va_list args;
    309 	char *text;
    310 
    311 	va_start(args, fmt);
    312 	text = l_strdup_vprintf(fmt, args);
    313 	va_end(args);
    314 
    315 	display_text(text);
    316 
    317 	l_free(text);
    318 }
    319 
    320 void display_error(const char *error)
    321 {
    322 	char *text = l_strdup_printf(COLOR_RED("%s\n"), error);
    323 
    324 	display_text(text);
    325 
    326 	l_free(text);
    327 }
    328 
    329 static char get_flasher(void)
    330 {
    331 	static char c;
    332 
    333 	if (c == ' ')
    334 		c = '*';
    335 	else
    336 		c = ' ';
    337 
    338 	return c;
    339 }
    340 
    341 void display_table_header(const char *caption, const char *fmt, ...)
    342 {
    343 	va_list args;
    344 	char *text;
    345 	char *body;
    346 	int caption_pos =
    347 		(int) ((sizeof(dashed_line) - 1) / 2 + strlen(caption) / 2);
    348 
    349 	text = l_strdup_printf("%*s" COLOR_BOLDGRAY("%*c") "\n",
    350 				caption_pos, caption,
    351 				LINE_LEN - 2 - caption_pos,
    352 				display_refresh.cmd ? get_flasher() : ' ');
    353 	display_text(text);
    354 	l_free(text);
    355 
    356 	text = l_strdup_printf(COLOR_GRAY("%s\n"), dashed_line);
    357 	display_text(text);
    358 	l_free(text);
    359 
    360 	va_start(args, fmt);
    361 	text = l_strdup_vprintf(fmt, args);
    362 	va_end(args);
    363 
    364 	body = l_strdup_printf(COLOR_BOLDGRAY("%s\n"), text);
    365 	display_text(body);
    366 	l_free(body);
    367 	l_free(text);
    368 
    369 	text = l_strdup_printf(COLOR_GRAY("%s\n"), dashed_line);
    370 	display_text(text);
    371 	l_free(text);
    372 }
    373 
    374 void display_table_footer(void)
    375 {
    376 	display_text("\n");
    377 
    378 	display_refresh_check_applicability();
    379 }
    380 
    381 static unsigned int color_end(char *s)
    382 {
    383 	char *start = s;
    384 
    385 	while (*s != 'm' && *s != '\0')
    386 		s++;
    387 
    388 	return s - start + 1;
    389 }
    390 
    391 /*
    392  * Finds last space in 's' before 'width' characters, terminates at that index,
    393  * and returns a new string to be printed on the next line.
    394  *
    395  * 'new_width' will be updated to include extra bytes for color escapes or
    396  * wide characters if found.
    397  *
    398  * Any colored escapes found are set to 'color_out' so they can be re-enabled
    399  * on the next line.
    400  */
    401 static char* next_line(char *s, unsigned int width, unsigned int *new_width,
    402 			char **color_out)
    403 {
    404 	unsigned int i = 0;
    405 	int last_space = -1;
    406 	int last_color = -1;
    407 	unsigned int s_len = strlen(s);
    408 	unsigned int color_adjust = 0;
    409 	char *ret;
    410 
    411 	*new_width = width;
    412 	*color_out = NULL;
    413 
    414 	/* Find the last space before 'max', as well as any color */
    415 	while (i <= *new_width && i < s_len) {
    416 		int sequence_len;
    417 		int sequence_columns;
    418 		wchar_t w;
    419 
    420 		if (s[i] == 0x1b) {
    421 			sequence_len = color_end(s + i);
    422 			/* color escape won't count for column width */
    423 			sequence_columns = 0;
    424 			last_color = i;
    425 
    426 			/*
    427 			 * Color after a space. If the line gets broken this
    428 			 * will need to be removed off new_width since it will
    429 			 * appear on the next line.
    430 			 */
    431 			if (last_space != -1)
    432 				color_adjust += sequence_len;
    433 
    434 		} else {
    435 			if (s[i] == ' ') {
    436 				last_space = i;
    437 				/* Any past colors will appear on this line */
    438 				color_adjust = 0;
    439 			}
    440 
    441 			sequence_len = l_utf8_get_codepoint(&s[i], s_len - i,
    442 									&w);
    443 			sequence_columns = wcwidth(w);
    444 		}
    445 
    446 		/* Compensate max bytes */
    447 		*new_width += sequence_len - sequence_columns;
    448 		i += sequence_len;
    449 	}
    450 
    451 	/* Reached the end of the string within the column bounds */
    452 	if (i <= *new_width)
    453 		return NULL;
    454 
    455 	/* Not anywhere nice to split the line */
    456 	if (last_space == -1)
    457 		last_space = *new_width;
    458 
    459 	/*
    460 	 * Only set the color if it occurred prior to the last space. If after,
    461 	 * it will get picked up on the next line.
    462 	 */
    463 	if (last_color != -1 && last_space >= last_color)
    464 		*color_out = l_strndup(s + last_color,
    465 					color_end(s + last_color));
    466 	else if (last_color != -1 && last_space < last_color)
    467 		*new_width -= color_adjust;
    468 
    469 	ret = l_strdup(s + last_space + 1);
    470 
    471 	s[last_space + 1] = '\0';
    472 
    473 	return ret;
    474 }
    475 
    476 struct table_entry {
    477 	unsigned int width;
    478 	char *next;
    479 	char *color;
    480 };
    481 
    482 /*
    483  * Appends the next line from 'e' to 'line_buf'. 'done' is only set false when
    484  * there are more lines needed for the current entry.
    485  */
    486 static int entry_append(struct table_entry *e, char *line_buf)
    487 {
    488 	char *value = e->next;
    489 	unsigned int ret = 0;
    490 	unsigned int new_width;
    491 
    492 	/* Empty line */
    493 	if (!value)
    494 		return sprintf(line_buf, "%-*s  ", e->width, "");
    495 
    496 	/* Color from previous line */
    497 	if (e->color) {
    498 		ret = sprintf(line_buf, "%s", e->color);
    499 		l_free(e->color);
    500 		e->color = NULL;
    501 	}
    502 
    503 	/* Advance entry to next line, and terminate current */
    504 	e->next = next_line(value, e->width, &new_width, &e->color);
    505 
    506 	/* Append current line */
    507 	ret += sprintf(line_buf + ret, "%-*s  ", new_width, value);
    508 
    509 	l_free(value);
    510 
    511 	/* Un-color output for next column */
    512 	if (e->color)
    513 		ret += sprintf(line_buf + ret, "%s", COLOR_OFF);
    514 
    515 	return ret;
    516 }
    517 
    518 static bool entries_done(unsigned int num, struct table_entry *e)
    519 {
    520 	unsigned int i;
    521 
    522 	for (i = 0; i < num; i++)
    523 		if (e[i].next)
    524 			return false;
    525 
    526 	return true;
    527 }
    528 
    529 /*
    530  * Expects an initial margin, number of columns in table, then row data:
    531  *
    532  * <row width>, <row data>, ...
    533  *
    534  * The data string can be of any length, and will be split into new lines of
    535  * length <row width>.
    536  */
    537 void display_table_row(const char *margin, unsigned int ncolumns, ...)
    538 {
    539 	char buf[512];
    540 	char *str = buf;
    541 	unsigned int i;
    542 	struct table_entry entries[ncolumns];
    543 	va_list va;
    544 
    545 	memset(&entries[0], 0, sizeof(entries));
    546 
    547 	va_start(va, ncolumns);
    548 
    549 	str += sprintf(str, "%s", margin);
    550 
    551 	for (i = 0; i < ncolumns; i++) {
    552 		struct table_entry *e = &entries[i];
    553 		char *v;
    554 
    555 		e->width = va_arg(va, unsigned int);
    556 		v = va_arg(va, char *);
    557 
    558 		if (!l_utf8_validate(v, strlen(v), NULL)) {
    559 			display_error("Invalid utf-8 string!");
    560 			goto done;
    561 		}
    562 
    563 		e->next = l_strdup(v);
    564 
    565 		str += entry_append(e, str);
    566 	}
    567 
    568 	display("%s\n", buf);
    569 	str = buf;
    570 
    571 	/*
    572 	 * The first column should now be indented, which effects the entry
    573 	 * width. Subtract this indentation only from the first column.
    574 	 */
    575 	entries[0].width -= strlen(margin) * 2;
    576 
    577 	while (!entries_done(ncolumns, &entries[0])) {
    578 		for (i = 0; i < ncolumns; i++) {
    579 			struct table_entry *e = &entries[i];
    580 
    581 			if (i == 0)
    582 				str += sprintf(str, "%s%s%s", margin,
    583 						margin, margin);
    584 
    585 			str += entry_append(e, str);
    586 		}
    587 
    588 		display("%s\n", buf);
    589 		str = buf;
    590 	}
    591 
    592 done:
    593 	va_end(va);
    594 
    595 	for (i = 0; i < ncolumns; i++) {
    596 		if (entries[i].color)
    597 			l_free(entries[i].color);
    598 
    599 		if (entries[i].next)
    600 			l_free(entries[i].next);
    601 	}
    602 }
    603 
    604 void display_command_line(const char *command_family, const struct command *cmd)
    605 {
    606 	char *cmd_line = l_strdup_printf("%s%s%s%s%s%s%s",
    607 				command_family ? : "",
    608 				command_family ? " " : "",
    609 				cmd->entity ? : "",
    610 				cmd->entity  ? " " : "",
    611 				cmd->cmd,
    612 				cmd->arg ? " " : "",
    613 				cmd->arg ? : "");
    614 
    615 	display_table_row(MARGIN, 2, 50, cmd_line, 30, cmd->desc);
    616 
    617 	l_free(cmd_line);
    618 }
    619 
    620 static void display_completion_matches(char **matches, int num_matches,
    621 								int max_length)
    622 {
    623 	char *prompt;
    624 	char *entry;
    625 	char line[LINE_LEN];
    626 	size_t index;
    627 	size_t line_used;
    628 	char *input = rl_copy_text(0, rl_end);
    629 
    630 	prompt = l_strdup_printf("%s%s\n", IWD_PROMPT, input);
    631 	l_free(input);
    632 
    633 	display_text(prompt);
    634 	l_free(prompt);
    635 
    636 	for (index = 1, line_used = 0; matches[index]; index++) {
    637 		if ((line_used + max_length + 1) >= (LINE_LEN - 1)) {
    638 			strcpy(&line[line_used], "\n");
    639 
    640 			display_text(line);
    641 
    642 			line_used = 0;
    643 		}
    644 
    645 		entry = l_strdup_printf("%-*s ", max_length, matches[index]);
    646 		l_strlcpy(&line[line_used], entry, sizeof(line) - line_used);
    647 		l_free(entry);
    648 
    649 		line_used += max_length + 1;
    650 	}
    651 
    652 	strcpy(&line[line_used], "\n");
    653 
    654 	display_text(line);
    655 }
    656 
    657 #define MAX_PASSPHRASE_LEN 63
    658 
    659 static struct masked_input {
    660 	bool use_mask;
    661 	char passphrase[MAX_PASSPHRASE_LEN + 1];
    662 	uint8_t point;
    663 	uint8_t end;
    664 } masked_input;
    665 
    666 static void mask_input(void)
    667 {
    668 	if (!masked_input.use_mask)
    669 		return;
    670 
    671 	if (rl_end > MAX_PASSPHRASE_LEN) {
    672 		rl_end = MAX_PASSPHRASE_LEN;
    673 		rl_point = masked_input.point;
    674 
    675 		goto set_mask;
    676 	}
    677 
    678 	if (masked_input.end == rl_end) {
    679 		/* Moving cursor. */
    680 		goto done;
    681 	} else if (masked_input.end < rl_end) {
    682 		/* Insertion. */
    683 		memcpy(masked_input.passphrase + rl_point,
    684 				masked_input.passphrase + masked_input.point,
    685 				masked_input.end - masked_input.point);
    686 		memcpy(masked_input.passphrase + masked_input.point,
    687 				rl_line_buffer + masked_input.point,
    688 				rl_point - masked_input.point);
    689 	} else {
    690 		/* Deletion. */
    691 		if (masked_input.point > rl_point)
    692 			/* Backspace key. */
    693 			memcpy(masked_input.passphrase + rl_point,
    694 				masked_input.passphrase + masked_input.point,
    695 				masked_input.end - masked_input.point);
    696 		else
    697 			/* Delete key. */
    698 			memcpy(masked_input.passphrase + rl_point,
    699 				masked_input.passphrase + masked_input.point
    700 									+ 1,
    701 				rl_end - rl_point);
    702 		memset(masked_input.passphrase + rl_end, 0,
    703 				masked_input.end - rl_end);
    704 	}
    705 
    706 set_mask:
    707 	memset(rl_line_buffer, '*', rl_end);
    708 	rl_line_buffer[rl_end] = '\0';
    709 
    710 	rl_redisplay();
    711 
    712 	masked_input.end = rl_end;
    713 done:
    714 	masked_input.point = rl_point;
    715 }
    716 
    717 static void reset_masked_input(void)
    718 {
    719 	memset(masked_input.passphrase, 0, MAX_PASSPHRASE_LEN + 1);
    720 	masked_input.point = 0;
    721 	masked_input.end = 0;
    722 }
    723 
    724 static void readline_callback(char *prompt)
    725 {
    726 	char **argv;
    727 	int argc;
    728 
    729 	HIST_ENTRY *previous_prompt;
    730 
    731 	if (agent_prompt(masked_input.use_mask ?
    732 					masked_input.passphrase : prompt))
    733 		goto done;
    734 
    735 	if (!prompt) {
    736 		display_quit();
    737 
    738 		l_main_quit();
    739 
    740 		return;
    741 	}
    742 
    743 	if (!strlen(prompt))
    744 		goto done;
    745 
    746 	previous_prompt = history_get(history_base + history_length - 1);
    747 	if (!previous_prompt || strcmp(previous_prompt->line, prompt)) {
    748 		add_history(prompt);
    749 	}
    750 
    751 	argv = l_parse_args(prompt, &argc);
    752 	if (!argv) {
    753 		display("Invalid command\n");
    754 		goto done;
    755 	}
    756 
    757 	command_process_prompt(argv, argc);
    758 
    759 	l_strfreev(argv);
    760 done:
    761 	l_free(prompt);
    762 }
    763 
    764 bool display_agent_is_active(void)
    765 {
    766 	if (agent_saved_input)
    767 		return true;
    768 
    769 	return false;
    770 }
    771 
    772 static bool read_handler(struct l_io *io, void *user_data)
    773 {
    774 	rl_callback_read_char();
    775 
    776 	if (display_agent_is_active() || !command_is_interactive_mode())
    777 		mask_input();
    778 
    779 	return true;
    780 }
    781 
    782 static void disconnect_callback(struct l_io *io, void *user_data)
    783 {
    784 	l_main_quit();
    785 }
    786 
    787 void display_enable_cmd_prompt(void)
    788 {
    789 	if (!io)
    790 		io = l_io_new(fileno(stdin));
    791 
    792 	l_io_set_read_handler(io, read_handler, NULL, NULL);
    793 	l_io_set_disconnect_handler(io, disconnect_callback, NULL, NULL);
    794 
    795 	rl_set_prompt(IWD_PROMPT);
    796 
    797 	/*
    798 	 * The following sequence of rl_* commands forces readline to properly
    799 	 * update its internal state and re-display the new prompt.
    800 	 */
    801 	rl_save_prompt();
    802 	rl_redisplay();
    803 	rl_restore_prompt();
    804 	rl_forced_update_display();
    805 }
    806 
    807 void display_disable_cmd_prompt(void)
    808 {
    809 	display_refresh_reset();
    810 
    811 	rl_set_prompt("Waiting for IWD to start...");
    812 	printf("\r");
    813 	rl_on_new_line();
    814 	rl_redisplay();
    815 }
    816 
    817 void display_agent_prompt(const char *label, bool mask_input)
    818 {
    819 	char *prompt;
    820 
    821 	masked_input.use_mask = mask_input;
    822 
    823 	if (mask_input)
    824 		reset_masked_input();
    825 
    826 	prompt = l_strdup_printf(COLOR_BLUE("%s "), label);
    827 
    828 	if (command_is_interactive_mode()) {
    829 		if (agent_saved_input) {
    830 			l_free(prompt);
    831 			return;
    832 		}
    833 
    834 		agent_saved_input = l_new(struct saved_input, 1);
    835 
    836 		agent_saved_input->point = rl_point;
    837 		agent_saved_input->line = rl_copy_text(0, rl_end);
    838 		rl_set_prompt("");
    839 		rl_replace_line("", 0);
    840 		rl_redisplay();
    841 
    842 		rl_erase_empty_line = 0;
    843 		rl_set_prompt(prompt);
    844 	} else {
    845 		rl_callback_handler_install(prompt, readline_callback);
    846 
    847 		if (!io)
    848 			io = l_io_new(fileno(stdin));
    849 
    850 		l_io_set_read_handler(io, read_handler, NULL, NULL);
    851 	}
    852 
    853 	l_free(prompt);
    854 
    855 	rl_redisplay();
    856 }
    857 
    858 void display_agent_prompt_release(const char *label)
    859 {
    860 	if (!command_is_interactive_mode()) {
    861 		rl_callback_handler_remove();
    862 		l_io_destroy(io);
    863 
    864 		return;
    865 	}
    866 
    867 	if (!agent_saved_input)
    868 		return;
    869 
    870 	if (display_refresh.cmd) {
    871 		char *text = rl_copy_text(0, rl_end);
    872 		char *prompt = l_strdup_printf(COLOR_BLUE("%s ")
    873 						"%s\n", label, text);
    874 		l_free(text);
    875 
    876 		l_queue_push_tail(display_refresh.redo_entries, prompt);
    877 		display_refresh.undo_lines++;
    878 	}
    879 
    880 	rl_erase_empty_line = 1;
    881 
    882 	rl_replace_line(agent_saved_input->line, 0);
    883 	rl_point = agent_saved_input->point;
    884 
    885 	l_free(agent_saved_input->line);
    886 	l_free(agent_saved_input);
    887 	agent_saved_input = NULL;
    888 
    889 	rl_set_prompt(IWD_PROMPT);
    890 }
    891 
    892 void display_quit(void)
    893 {
    894 	if (command_is_interactive_mode())
    895 		rl_crlf();
    896 }
    897 
    898 static void window_change_signal_handler(void *user_data)
    899 {
    900 	display_refresh_check_feasibility();
    901 }
    902 
    903 static char *history_path;
    904 
    905 void display_init(void)
    906 {
    907 	const char *data_home;
    908 	char *data_path;
    909 
    910 	display_refresh.redo_entries = l_queue_new();
    911 
    912 	stifle_history(24);
    913 
    914 	data_home = getenv("XDG_DATA_HOME");
    915 	if (!data_home || *data_home != '/') {
    916 		const char *home_path;
    917 
    918 		home_path = getenv("HOME");
    919 		if (home_path)
    920 			data_path = l_strdup_printf("%s/%s/iwctl",
    921 						home_path, ".local/share");
    922 		else
    923 			data_path = NULL;
    924 	} else {
    925 		data_path = l_strdup_printf("%s/iwctl", data_home);
    926 	}
    927 
    928 	if (data_path) {
    929 		/*
    930 		 * If mkdir succeeds that means its a new directory, no need
    931 		 * to read the history since it doesn't exist
    932 		 */
    933 		if (mkdir(data_path, 0700) != 0) {
    934 			history_path = l_strdup_printf("%s/history", data_path);
    935 			read_history(history_path);
    936 		}
    937 
    938 		l_free(data_path);
    939 	} else {
    940 		history_path = NULL;
    941 	}
    942 
    943 	setlinebuf(stdout);
    944 
    945 	window_change_signal =
    946 		l_signal_create(SIGWINCH, window_change_signal_handler, NULL,
    947 									NULL);
    948 
    949 	rl_attempted_completion_function = command_completion;
    950 	rl_completion_display_matches_hook = display_completion_matches;
    951 
    952 	rl_completer_quote_characters = "\"";
    953 	rl_erase_empty_line = 1;
    954 	rl_callback_handler_install("Waiting for IWD to start...",
    955 							readline_callback);
    956 
    957 	rl_redisplay();
    958 
    959 	display_refresh_check_feasibility();
    960 }
    961 
    962 void display_exit(void)
    963 {
    964 	if (agent_saved_input) {
    965 		l_free(agent_saved_input->line);
    966 		l_free(agent_saved_input);
    967 		agent_saved_input = NULL;
    968 	}
    969 
    970 	l_timeout_remove(refresh_timeout);
    971 	refresh_timeout = NULL;
    972 
    973 	l_queue_destroy(display_refresh.redo_entries, l_free);
    974 
    975 	rl_callback_handler_remove();
    976 
    977 	l_io_destroy(io);
    978 
    979 	l_signal_remove(window_change_signal);
    980 
    981 	if (history_path)
    982 		write_history(history_path);
    983 
    984 	l_free(history_path);
    985 
    986 	display_quit();
    987 }