1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2012 Intel Corporation. All rights reserved.
*
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <ctype.h>
#include <readline/readline.h>
#include "display.h"
static char *saved_prompt = NULL;
static int saved_point = 0;
static rl_prompt_input_func saved_func = NULL;
static void *saved_user_data = NULL;
void rl_printf(const char *fmt, ...)
{
va_list args;
bool save_input;
char *saved_line;
int saved_point;
save_input = !RL_ISSTATE(RL_STATE_DONE);
if (save_input) {
saved_point = rl_point;
saved_line = rl_copy_text(0, rl_end);
rl_save_prompt();
rl_replace_line("", 0);
rl_redisplay();
}
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
if (save_input) {
rl_restore_prompt();
rl_replace_line(saved_line, 0);
rl_point = saved_point;
rl_forced_update_display();
free(saved_line);
}
}
void rl_hexdump(const unsigned char *buf, size_t len)
{
static const char hexdigits[] = "0123456789abcdef";
char str[68];
size_t i;
if (!len)
return;
str[0] = ' ';
for (i = 0; i < len; i++) {
str[((i % 16) * 3) + 1] = ' ';
str[((i % 16) * 3) + 2] = hexdigits[buf[i] >> 4];
str[((i % 16) * 3) + 3] = hexdigits[buf[i] & 0xf];
str[(i % 16) + 51] = isprint(buf[i]) ? buf[i] : '.';
if ((i + 1) % 16 == 0) {
str[49] = ' ';
str[50] = ' ';
str[67] = '\0';
rl_printf("%s\n", str);
str[0] = ' ';
}
}
if (i % 16 > 0) {
size_t j;
for (j = (i % 16); j < 16; j++) {
str[(j * 3) + 1] = ' ';
str[(j * 3) + 2] = ' ';
str[(j * 3) + 3] = ' ';
str[j + 51] = ' ';
}
str[49] = ' ';
str[50] = ' ';
str[67] = '\0';
rl_printf("%s\n", str);
}
}
void rl_prompt_input(const char *label, const char *msg,
rl_prompt_input_func func, void *user_data)
{
char prompt[256];
/* Normal use should not prompt for user input to the value a second
* time before it releases the prompt, but we take a safe action. */
if (saved_prompt)
return;
saved_point = rl_point;
saved_prompt = strdup(rl_prompt);
saved_func = func;
saved_user_data = user_data;
rl_set_prompt("");
rl_redisplay();
memset(prompt, 0, sizeof(prompt));
snprintf(prompt, sizeof(prompt), COLOR_RED "[%s]" COLOR_OFF " %s ",
label, msg);
rl_set_prompt(prompt);
rl_replace_line("", 0);
rl_redisplay();
}
int rl_release_prompt(const char *input)
{
rl_prompt_input_func func;
void *user_data;
if (!saved_prompt)
return -1;
/* This will cause rl_expand_prompt to re-run over the last prompt, but
* our prompt doesn't expand anyway. */
rl_set_prompt(saved_prompt);
rl_replace_line("", 0);
rl_point = saved_point;
rl_redisplay();
free(saved_prompt);
saved_prompt = NULL;
func = saved_func;
user_data = saved_user_data;
saved_func = NULL;
saved_user_data = NULL;
func(input, user_data);
return 0;
}
|