2004-08-02 01:52:19 +04:00
|
|
|
/*
|
|
|
|
* QEMU readline utility
|
2007-09-17 01:08:06 +04:00
|
|
|
*
|
2004-08-02 01:52:19 +04:00
|
|
|
* Copyright (c) 2003-2004 Fabrice Bellard
|
2007-09-17 01:08:06 +04:00
|
|
|
*
|
2004-08-02 01:52:19 +04:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2013-11-14 14:54:14 +04:00
|
|
|
|
2016-01-29 20:49:55 +03:00
|
|
|
#include "qemu/osdep.h"
|
2013-11-14 14:54:15 +04:00
|
|
|
#include "qemu/readline.h"
|
2019-05-23 17:35:06 +03:00
|
|
|
#include "qemu/ctype.h"
|
2016-03-20 20:16:19 +03:00
|
|
|
#include "qemu/cutils.h"
|
2004-08-02 01:52:19 +04:00
|
|
|
|
|
|
|
#define IS_NORM 0
|
|
|
|
#define IS_ESC 1
|
|
|
|
#define IS_CSI 2
|
2013-05-14 14:15:54 +04:00
|
|
|
#define IS_SS3 3
|
2004-08-02 01:52:19 +04:00
|
|
|
|
2009-03-06 02:01:37 +03:00
|
|
|
void readline_show_prompt(ReadLineState *rs)
|
2004-08-02 01:52:19 +04:00
|
|
|
{
|
2013-11-14 14:54:14 +04:00
|
|
|
rs->printf_func(rs->opaque, "%s", rs->prompt);
|
|
|
|
rs->flush_func(rs->opaque);
|
2009-03-06 02:01:37 +03:00
|
|
|
rs->last_cmd_buf_index = 0;
|
|
|
|
rs->last_cmd_buf_size = 0;
|
|
|
|
rs->esc_state = IS_NORM;
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* update the displayed command line */
|
2009-03-06 02:01:37 +03:00
|
|
|
static void readline_update(ReadLineState *rs)
|
2004-08-02 01:52:19 +04:00
|
|
|
{
|
|
|
|
int i, delta, len;
|
|
|
|
|
2009-03-06 02:01:37 +03:00
|
|
|
if (rs->cmd_buf_size != rs->last_cmd_buf_size ||
|
|
|
|
memcmp(rs->cmd_buf, rs->last_cmd_buf, rs->cmd_buf_size) != 0) {
|
2019-04-01 05:44:05 +03:00
|
|
|
for (i = 0; i < rs->last_cmd_buf_index; i++) {
|
2013-11-14 14:54:14 +04:00
|
|
|
rs->printf_func(rs->opaque, "\033[D");
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
2009-03-06 02:01:37 +03:00
|
|
|
rs->cmd_buf[rs->cmd_buf_size] = '\0';
|
|
|
|
if (rs->read_password) {
|
|
|
|
len = strlen(rs->cmd_buf);
|
2019-03-30 14:21:42 +03:00
|
|
|
for (i = 0; i < len; i++) {
|
2013-11-14 14:54:14 +04:00
|
|
|
rs->printf_func(rs->opaque, "*");
|
2019-03-30 14:21:42 +03:00
|
|
|
}
|
2004-08-02 01:52:19 +04:00
|
|
|
} else {
|
2013-11-14 14:54:14 +04:00
|
|
|
rs->printf_func(rs->opaque, "%s", rs->cmd_buf);
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
2013-11-14 14:54:14 +04:00
|
|
|
rs->printf_func(rs->opaque, "\033[K");
|
2009-03-06 02:01:37 +03:00
|
|
|
memcpy(rs->last_cmd_buf, rs->cmd_buf, rs->cmd_buf_size);
|
|
|
|
rs->last_cmd_buf_size = rs->cmd_buf_size;
|
|
|
|
rs->last_cmd_buf_index = rs->cmd_buf_size;
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
2009-03-06 02:01:37 +03:00
|
|
|
if (rs->cmd_buf_index != rs->last_cmd_buf_index) {
|
|
|
|
delta = rs->cmd_buf_index - rs->last_cmd_buf_index;
|
2004-08-02 01:52:19 +04:00
|
|
|
if (delta > 0) {
|
2019-04-01 05:44:05 +03:00
|
|
|
for (i = 0; i < delta; i++) {
|
2013-11-14 14:54:14 +04:00
|
|
|
rs->printf_func(rs->opaque, "\033[C");
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
delta = -delta;
|
2019-04-01 05:44:05 +03:00
|
|
|
for (i = 0; i < delta; i++) {
|
2013-11-14 14:54:14 +04:00
|
|
|
rs->printf_func(rs->opaque, "\033[D");
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
|
|
|
}
|
2009-03-06 02:01:37 +03:00
|
|
|
rs->last_cmd_buf_index = rs->cmd_buf_index;
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
2013-11-14 14:54:14 +04:00
|
|
|
rs->flush_func(rs->opaque);
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
|
|
|
|
2009-03-06 02:01:37 +03:00
|
|
|
static void readline_insert_char(ReadLineState *rs, int ch)
|
2004-08-02 01:52:19 +04:00
|
|
|
{
|
2009-03-06 02:01:37 +03:00
|
|
|
if (rs->cmd_buf_index < READLINE_CMD_BUF_SIZE) {
|
|
|
|
memmove(rs->cmd_buf + rs->cmd_buf_index + 1,
|
|
|
|
rs->cmd_buf + rs->cmd_buf_index,
|
|
|
|
rs->cmd_buf_size - rs->cmd_buf_index);
|
|
|
|
rs->cmd_buf[rs->cmd_buf_index] = ch;
|
|
|
|
rs->cmd_buf_size++;
|
|
|
|
rs->cmd_buf_index++;
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-06 02:01:37 +03:00
|
|
|
static void readline_backward_char(ReadLineState *rs)
|
2004-08-02 01:52:19 +04:00
|
|
|
{
|
2009-03-06 02:01:37 +03:00
|
|
|
if (rs->cmd_buf_index > 0) {
|
|
|
|
rs->cmd_buf_index--;
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-06 02:01:37 +03:00
|
|
|
static void readline_forward_char(ReadLineState *rs)
|
2004-08-02 01:52:19 +04:00
|
|
|
{
|
2009-03-06 02:01:37 +03:00
|
|
|
if (rs->cmd_buf_index < rs->cmd_buf_size) {
|
|
|
|
rs->cmd_buf_index++;
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-06 02:01:37 +03:00
|
|
|
static void readline_delete_char(ReadLineState *rs)
|
2004-08-02 01:52:19 +04:00
|
|
|
{
|
2009-03-06 02:01:37 +03:00
|
|
|
if (rs->cmd_buf_index < rs->cmd_buf_size) {
|
|
|
|
memmove(rs->cmd_buf + rs->cmd_buf_index,
|
|
|
|
rs->cmd_buf + rs->cmd_buf_index + 1,
|
|
|
|
rs->cmd_buf_size - rs->cmd_buf_index - 1);
|
|
|
|
rs->cmd_buf_size--;
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-06 02:01:37 +03:00
|
|
|
static void readline_backspace(ReadLineState *rs)
|
2004-08-02 01:52:19 +04:00
|
|
|
{
|
2009-03-06 02:01:37 +03:00
|
|
|
if (rs->cmd_buf_index > 0) {
|
|
|
|
readline_backward_char(rs);
|
|
|
|
readline_delete_char(rs);
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-06 02:01:37 +03:00
|
|
|
static void readline_backword(ReadLineState *rs)
|
2007-06-25 15:36:50 +04:00
|
|
|
{
|
|
|
|
int start;
|
|
|
|
|
2009-03-06 02:01:37 +03:00
|
|
|
if (rs->cmd_buf_index == 0 || rs->cmd_buf_index > rs->cmd_buf_size) {
|
2007-06-25 15:36:50 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-03-06 02:01:37 +03:00
|
|
|
start = rs->cmd_buf_index - 1;
|
2007-06-25 15:36:50 +04:00
|
|
|
|
|
|
|
/* find first word (backwards) */
|
|
|
|
while (start > 0) {
|
2009-03-06 02:01:37 +03:00
|
|
|
if (!qemu_isspace(rs->cmd_buf[start])) {
|
2007-06-25 15:36:50 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
--start;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find first space (backwards) */
|
|
|
|
while (start > 0) {
|
2009-03-06 02:01:37 +03:00
|
|
|
if (qemu_isspace(rs->cmd_buf[start])) {
|
2007-06-25 15:36:50 +04:00
|
|
|
++start;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
--start;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove word */
|
2009-03-06 02:01:37 +03:00
|
|
|
if (start < rs->cmd_buf_index) {
|
|
|
|
memmove(rs->cmd_buf + start,
|
|
|
|
rs->cmd_buf + rs->cmd_buf_index,
|
|
|
|
rs->cmd_buf_size - rs->cmd_buf_index);
|
|
|
|
rs->cmd_buf_size -= rs->cmd_buf_index - start;
|
|
|
|
rs->cmd_buf_index = start;
|
2007-06-25 15:36:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-06 02:01:37 +03:00
|
|
|
static void readline_bol(ReadLineState *rs)
|
2004-08-02 01:52:19 +04:00
|
|
|
{
|
2009-03-06 02:01:37 +03:00
|
|
|
rs->cmd_buf_index = 0;
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
|
|
|
|
2009-03-06 02:01:37 +03:00
|
|
|
static void readline_eol(ReadLineState *rs)
|
2004-08-02 01:52:19 +04:00
|
|
|
{
|
2009-03-06 02:01:37 +03:00
|
|
|
rs->cmd_buf_index = rs->cmd_buf_size;
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
|
|
|
|
2009-03-06 02:01:37 +03:00
|
|
|
static void readline_up_char(ReadLineState *rs)
|
2004-08-02 01:52:19 +04:00
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
|
2019-03-30 14:21:42 +03:00
|
|
|
if (rs->hist_entry == 0) {
|
2019-04-01 05:44:06 +03:00
|
|
|
return;
|
2019-03-30 14:21:42 +03:00
|
|
|
}
|
2009-03-06 02:01:37 +03:00
|
|
|
if (rs->hist_entry == -1) {
|
2019-04-01 05:44:06 +03:00
|
|
|
/* Find latest entry */
|
|
|
|
for (idx = 0; idx < READLINE_MAX_CMDS; idx++) {
|
2019-03-30 14:21:42 +03:00
|
|
|
if (rs->history[idx] == NULL) {
|
2019-04-01 05:44:06 +03:00
|
|
|
break;
|
2019-03-30 14:21:42 +03:00
|
|
|
}
|
2019-04-01 05:44:06 +03:00
|
|
|
}
|
|
|
|
rs->hist_entry = idx;
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
2009-03-06 02:01:37 +03:00
|
|
|
rs->hist_entry--;
|
|
|
|
if (rs->hist_entry >= 0) {
|
2019-04-01 05:44:06 +03:00
|
|
|
pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf),
|
2009-03-06 02:01:37 +03:00
|
|
|
rs->history[rs->hist_entry]);
|
2019-04-01 05:44:06 +03:00
|
|
|
rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf);
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-06 02:01:37 +03:00
|
|
|
static void readline_down_char(ReadLineState *rs)
|
2004-08-02 01:52:19 +04:00
|
|
|
{
|
2019-03-30 14:21:42 +03:00
|
|
|
if (rs->hist_entry == -1) {
|
2009-03-13 18:02:28 +03:00
|
|
|
return;
|
2019-03-30 14:21:42 +03:00
|
|
|
}
|
2009-03-13 18:02:28 +03:00
|
|
|
if (rs->hist_entry < READLINE_MAX_CMDS - 1 &&
|
|
|
|
rs->history[++rs->hist_entry] != NULL) {
|
2019-04-01 05:44:06 +03:00
|
|
|
pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf),
|
2009-03-06 02:01:37 +03:00
|
|
|
rs->history[rs->hist_entry]);
|
2004-08-02 01:52:19 +04:00
|
|
|
} else {
|
2009-03-13 18:02:28 +03:00
|
|
|
rs->cmd_buf[0] = 0;
|
2019-04-01 05:44:06 +03:00
|
|
|
rs->hist_entry = -1;
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
2009-03-06 02:01:37 +03:00
|
|
|
rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf);
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
|
|
|
|
2009-03-06 02:01:37 +03:00
|
|
|
static void readline_hist_add(ReadLineState *rs, const char *cmdline)
|
2004-08-02 01:52:19 +04:00
|
|
|
{
|
|
|
|
char *hist_entry, *new_entry;
|
|
|
|
int idx;
|
|
|
|
|
2019-03-30 14:21:42 +03:00
|
|
|
if (cmdline[0] == '\0') {
|
2019-04-01 05:44:06 +03:00
|
|
|
return;
|
2019-03-30 14:21:42 +03:00
|
|
|
}
|
2004-08-02 01:52:19 +04:00
|
|
|
new_entry = NULL;
|
2009-03-06 02:01:37 +03:00
|
|
|
if (rs->hist_entry != -1) {
|
2019-04-01 05:44:06 +03:00
|
|
|
/* We were editing an existing history entry: replace it */
|
|
|
|
hist_entry = rs->history[rs->hist_entry];
|
|
|
|
idx = rs->hist_entry;
|
|
|
|
if (strcmp(hist_entry, cmdline) == 0) {
|
|
|
|
goto same_entry;
|
|
|
|
}
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
|
|
|
/* Search cmdline in history buffers */
|
2009-03-06 02:01:37 +03:00
|
|
|
for (idx = 0; idx < READLINE_MAX_CMDS; idx++) {
|
2019-04-01 05:44:06 +03:00
|
|
|
hist_entry = rs->history[idx];
|
2019-03-30 14:21:42 +03:00
|
|
|
if (hist_entry == NULL) {
|
2019-04-01 05:44:06 +03:00
|
|
|
break;
|
2019-03-30 14:21:42 +03:00
|
|
|
}
|
2019-04-01 05:44:06 +03:00
|
|
|
if (strcmp(hist_entry, cmdline) == 0) {
|
|
|
|
same_entry:
|
2020-12-03 16:50:43 +03:00
|
|
|
if (idx == READLINE_MAX_CMDS - 1) {
|
|
|
|
return;
|
|
|
|
}
|
2019-04-01 05:44:06 +03:00
|
|
|
new_entry = hist_entry;
|
|
|
|
/* Put this entry at the end of history */
|
|
|
|
memmove(&rs->history[idx], &rs->history[idx + 1],
|
|
|
|
(READLINE_MAX_CMDS - (idx + 1)) * sizeof(char *));
|
|
|
|
rs->history[READLINE_MAX_CMDS - 1] = NULL;
|
|
|
|
for (; idx < READLINE_MAX_CMDS; idx++) {
|
2019-03-30 14:21:42 +03:00
|
|
|
if (rs->history[idx] == NULL) {
|
2019-04-01 05:44:06 +03:00
|
|
|
break;
|
2019-03-30 14:21:42 +03:00
|
|
|
}
|
2019-04-01 05:44:06 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
2009-03-06 02:01:37 +03:00
|
|
|
if (idx == READLINE_MAX_CMDS) {
|
2019-04-01 05:44:06 +03:00
|
|
|
/* Need to get one free slot */
|
2013-01-22 14:07:59 +04:00
|
|
|
g_free(rs->history[0]);
|
2019-04-01 05:44:06 +03:00
|
|
|
memmove(rs->history, &rs->history[1],
|
|
|
|
(READLINE_MAX_CMDS - 1) * sizeof(char *));
|
|
|
|
rs->history[READLINE_MAX_CMDS - 1] = NULL;
|
|
|
|
idx = READLINE_MAX_CMDS - 1;
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
2019-03-30 14:21:42 +03:00
|
|
|
if (new_entry == NULL) {
|
2013-01-22 14:07:59 +04:00
|
|
|
new_entry = g_strdup(cmdline);
|
2019-03-30 14:21:42 +03:00
|
|
|
}
|
2009-03-06 02:01:37 +03:00
|
|
|
rs->history[idx] = new_entry;
|
|
|
|
rs->hist_entry = -1;
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* completion support */
|
|
|
|
|
2009-03-06 02:01:37 +03:00
|
|
|
void readline_add_completion(ReadLineState *rs, const char *str)
|
2004-08-02 01:52:19 +04:00
|
|
|
{
|
2009-03-06 02:01:37 +03:00
|
|
|
if (rs->nb_completions < READLINE_MAX_COMPLETIONS) {
|
2014-05-28 02:39:35 +04:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < rs->nb_completions; i++) {
|
|
|
|
if (!strcmp(rs->completions[i], str)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-08-21 07:09:37 +04:00
|
|
|
rs->completions[rs->nb_completions++] = g_strdup(str);
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 15:19:20 +03:00
|
|
|
void readline_add_completion_of(ReadLineState *rs,
|
|
|
|
const char *pfx, const char *str)
|
|
|
|
{
|
|
|
|
if (!strncmp(str, pfx, strlen(pfx))) {
|
|
|
|
readline_add_completion(rs, str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-06 02:01:37 +03:00
|
|
|
void readline_set_completion_index(ReadLineState *rs, int index)
|
2009-03-06 02:01:23 +03:00
|
|
|
{
|
2009-03-06 02:01:37 +03:00
|
|
|
rs->completion_index = index;
|
2009-03-06 02:01:23 +03:00
|
|
|
}
|
|
|
|
|
2014-05-01 18:26:42 +04:00
|
|
|
static int completion_comp(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
return strcmp(*(const char **) a, *(const char **) b);
|
|
|
|
}
|
|
|
|
|
2009-03-06 02:01:37 +03:00
|
|
|
static void readline_completion(ReadLineState *rs)
|
2004-08-02 01:52:19 +04:00
|
|
|
{
|
2007-09-25 18:45:23 +04:00
|
|
|
int len, i, j, max_width, nb_cols, max_prefix;
|
2004-08-02 01:52:19 +04:00
|
|
|
char *cmdline;
|
|
|
|
|
2009-03-06 02:01:37 +03:00
|
|
|
rs->nb_completions = 0;
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2014-05-05 13:23:50 +04:00
|
|
|
cmdline = g_strndup(rs->cmd_buf, rs->cmd_buf_index);
|
2013-11-14 14:54:14 +04:00
|
|
|
rs->completion_finder(rs->opaque, cmdline);
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(cmdline);
|
2004-08-02 01:52:19 +04:00
|
|
|
|
|
|
|
/* no completion found */
|
2019-03-30 14:21:42 +03:00
|
|
|
if (rs->nb_completions <= 0) {
|
2004-08-02 01:52:19 +04:00
|
|
|
return;
|
2019-03-30 14:21:42 +03:00
|
|
|
}
|
2009-03-06 02:01:37 +03:00
|
|
|
if (rs->nb_completions == 1) {
|
|
|
|
len = strlen(rs->completions[0]);
|
2019-04-01 05:44:05 +03:00
|
|
|
for (i = rs->completion_index; i < len; i++) {
|
2009-03-06 02:01:37 +03:00
|
|
|
readline_insert_char(rs, rs->completions[0][i]);
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
|
|
|
/* extra space for next argument. XXX: make it more generic */
|
2019-03-30 14:21:42 +03:00
|
|
|
if (len > 0 && rs->completions[0][len - 1] != '/') {
|
2009-03-06 02:01:37 +03:00
|
|
|
readline_insert_char(rs, ' ');
|
2019-03-30 14:21:42 +03:00
|
|
|
}
|
2004-08-02 01:52:19 +04:00
|
|
|
} else {
|
2014-05-01 18:26:42 +04:00
|
|
|
qsort(rs->completions, rs->nb_completions, sizeof(char *),
|
|
|
|
completion_comp);
|
2013-11-14 14:54:14 +04:00
|
|
|
rs->printf_func(rs->opaque, "\n");
|
2004-08-02 01:52:19 +04:00
|
|
|
max_width = 0;
|
2019-04-01 05:44:05 +03:00
|
|
|
max_prefix = 0;
|
|
|
|
for (i = 0; i < rs->nb_completions; i++) {
|
2009-03-06 02:01:37 +03:00
|
|
|
len = strlen(rs->completions[i]);
|
2019-04-01 05:44:05 +03:00
|
|
|
if (i == 0) {
|
2007-09-25 18:45:23 +04:00
|
|
|
max_prefix = len;
|
|
|
|
} else {
|
2019-03-30 14:21:42 +03:00
|
|
|
if (len < max_prefix) {
|
2007-09-25 18:45:23 +04:00
|
|
|
max_prefix = len;
|
2019-03-30 14:21:42 +03:00
|
|
|
}
|
2019-04-01 05:44:05 +03:00
|
|
|
for (j = 0; j < max_prefix; j++) {
|
2019-03-30 14:21:42 +03:00
|
|
|
if (rs->completions[i][j] != rs->completions[0][j]) {
|
2007-09-25 18:45:23 +04:00
|
|
|
max_prefix = j;
|
2019-03-30 14:21:42 +03:00
|
|
|
}
|
2007-09-25 18:45:23 +04:00
|
|
|
}
|
|
|
|
}
|
2019-03-30 14:21:42 +03:00
|
|
|
if (len > max_width) {
|
2004-08-02 01:52:19 +04:00
|
|
|
max_width = len;
|
2019-03-30 14:21:42 +03:00
|
|
|
}
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
2019-04-01 05:44:05 +03:00
|
|
|
if (max_prefix > 0)
|
|
|
|
for (i = rs->completion_index; i < max_prefix; i++) {
|
2009-03-06 02:01:37 +03:00
|
|
|
readline_insert_char(rs, rs->completions[0][i]);
|
2007-09-25 18:45:23 +04:00
|
|
|
}
|
2004-08-02 01:52:19 +04:00
|
|
|
max_width += 2;
|
2019-03-30 14:21:42 +03:00
|
|
|
if (max_width < 10) {
|
2004-08-02 01:52:19 +04:00
|
|
|
max_width = 10;
|
2019-03-30 14:21:42 +03:00
|
|
|
} else if (max_width > 80) {
|
2004-08-02 01:52:19 +04:00
|
|
|
max_width = 80;
|
2019-03-30 14:21:42 +03:00
|
|
|
}
|
2004-08-02 01:52:19 +04:00
|
|
|
nb_cols = 80 / max_width;
|
|
|
|
j = 0;
|
2019-04-01 05:44:05 +03:00
|
|
|
for (i = 0; i < rs->nb_completions; i++) {
|
2013-11-14 14:54:14 +04:00
|
|
|
rs->printf_func(rs->opaque, "%-*s", max_width, rs->completions[i]);
|
2009-03-06 02:01:37 +03:00
|
|
|
if (++j == nb_cols || i == (rs->nb_completions - 1)) {
|
2013-11-14 14:54:14 +04:00
|
|
|
rs->printf_func(rs->opaque, "\n");
|
2004-08-02 01:52:19 +04:00
|
|
|
j = 0;
|
|
|
|
}
|
|
|
|
}
|
2009-03-06 02:01:37 +03:00
|
|
|
readline_show_prompt(rs);
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
2012-05-14 13:47:20 +04:00
|
|
|
for (i = 0; i < rs->nb_completions; i++) {
|
|
|
|
g_free(rs->completions[i]);
|
|
|
|
}
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
|
|
|
|
2014-06-01 15:53:35 +04:00
|
|
|
static void readline_clear_screen(ReadLineState *rs)
|
|
|
|
{
|
|
|
|
rs->printf_func(rs->opaque, "\033[2J\033[1;1H");
|
|
|
|
readline_show_prompt(rs);
|
|
|
|
}
|
|
|
|
|
2004-08-02 01:52:19 +04:00
|
|
|
/* return true if command handled */
|
2009-03-06 02:01:37 +03:00
|
|
|
void readline_handle_byte(ReadLineState *rs, int ch)
|
2004-08-02 01:52:19 +04:00
|
|
|
{
|
2019-04-01 05:44:05 +03:00
|
|
|
switch (rs->esc_state) {
|
2004-08-02 01:52:19 +04:00
|
|
|
case IS_NORM:
|
2019-04-01 05:44:05 +03:00
|
|
|
switch (ch) {
|
2004-08-02 01:52:19 +04:00
|
|
|
case 1:
|
2009-03-06 02:01:37 +03:00
|
|
|
readline_bol(rs);
|
2004-08-02 01:52:19 +04:00
|
|
|
break;
|
|
|
|
case 4:
|
2009-03-06 02:01:37 +03:00
|
|
|
readline_delete_char(rs);
|
2004-08-02 01:52:19 +04:00
|
|
|
break;
|
|
|
|
case 5:
|
2009-03-06 02:01:37 +03:00
|
|
|
readline_eol(rs);
|
2004-08-02 01:52:19 +04:00
|
|
|
break;
|
|
|
|
case 9:
|
2009-03-06 02:01:37 +03:00
|
|
|
readline_completion(rs);
|
2004-08-02 01:52:19 +04:00
|
|
|
break;
|
2014-06-01 15:53:35 +04:00
|
|
|
case 12:
|
|
|
|
readline_clear_screen(rs);
|
|
|
|
break;
|
2004-08-02 01:52:19 +04:00
|
|
|
case 10:
|
|
|
|
case 13:
|
2009-03-06 02:01:37 +03:00
|
|
|
rs->cmd_buf[rs->cmd_buf_size] = '\0';
|
2019-03-30 14:21:42 +03:00
|
|
|
if (!rs->read_password) {
|
2009-03-06 02:01:37 +03:00
|
|
|
readline_hist_add(rs, rs->cmd_buf);
|
2019-03-30 14:21:42 +03:00
|
|
|
}
|
2013-11-14 14:54:14 +04:00
|
|
|
rs->printf_func(rs->opaque, "\n");
|
2009-03-06 02:01:37 +03:00
|
|
|
rs->cmd_buf_index = 0;
|
|
|
|
rs->cmd_buf_size = 0;
|
|
|
|
rs->last_cmd_buf_index = 0;
|
|
|
|
rs->last_cmd_buf_size = 0;
|
2013-11-14 14:54:14 +04:00
|
|
|
rs->readline_func(rs->opaque, rs->cmd_buf, rs->readline_opaque);
|
2004-08-02 01:52:19 +04:00
|
|
|
break;
|
2007-06-25 15:36:50 +04:00
|
|
|
case 23:
|
|
|
|
/* ^W */
|
2009-03-06 02:01:37 +03:00
|
|
|
readline_backword(rs);
|
2007-06-25 15:36:50 +04:00
|
|
|
break;
|
2004-08-02 01:52:19 +04:00
|
|
|
case 27:
|
2009-03-06 02:01:37 +03:00
|
|
|
rs->esc_state = IS_ESC;
|
2004-08-02 01:52:19 +04:00
|
|
|
break;
|
|
|
|
case 127:
|
|
|
|
case 8:
|
2009-03-06 02:01:37 +03:00
|
|
|
readline_backspace(rs);
|
2004-08-02 01:52:19 +04:00
|
|
|
break;
|
2019-04-01 05:44:06 +03:00
|
|
|
case 155:
|
2009-03-06 02:01:37 +03:00
|
|
|
rs->esc_state = IS_CSI;
|
2019-04-01 05:44:06 +03:00
|
|
|
break;
|
2004-08-02 01:52:19 +04:00
|
|
|
default:
|
|
|
|
if (ch >= 32) {
|
2009-03-06 02:01:37 +03:00
|
|
|
readline_insert_char(rs, ch);
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IS_ESC:
|
|
|
|
if (ch == '[') {
|
2009-03-06 02:01:37 +03:00
|
|
|
rs->esc_state = IS_CSI;
|
|
|
|
rs->esc_param = 0;
|
2013-05-14 14:15:54 +04:00
|
|
|
} else if (ch == 'O') {
|
|
|
|
rs->esc_state = IS_SS3;
|
|
|
|
rs->esc_param = 0;
|
2004-08-02 01:52:19 +04:00
|
|
|
} else {
|
2009-03-06 02:01:37 +03:00
|
|
|
rs->esc_state = IS_NORM;
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IS_CSI:
|
2019-04-01 05:44:05 +03:00
|
|
|
switch (ch) {
|
2019-04-01 05:44:06 +03:00
|
|
|
case 'A':
|
|
|
|
case 'F':
|
|
|
|
readline_up_char(rs);
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
case 'E':
|
|
|
|
readline_down_char(rs);
|
|
|
|
break;
|
2004-08-02 01:52:19 +04:00
|
|
|
case 'D':
|
2009-03-06 02:01:37 +03:00
|
|
|
readline_backward_char(rs);
|
2004-08-02 01:52:19 +04:00
|
|
|
break;
|
|
|
|
case 'C':
|
2009-03-06 02:01:37 +03:00
|
|
|
readline_forward_char(rs);
|
2004-08-02 01:52:19 +04:00
|
|
|
break;
|
|
|
|
case '0' ... '9':
|
2009-03-06 02:01:37 +03:00
|
|
|
rs->esc_param = rs->esc_param * 10 + (ch - '0');
|
2004-08-02 01:52:19 +04:00
|
|
|
goto the_end;
|
|
|
|
case '~':
|
2019-04-01 05:44:05 +03:00
|
|
|
switch (rs->esc_param) {
|
2004-08-02 01:52:19 +04:00
|
|
|
case 1:
|
2009-03-06 02:01:37 +03:00
|
|
|
readline_bol(rs);
|
2004-08-02 01:52:19 +04:00
|
|
|
break;
|
|
|
|
case 3:
|
2009-03-06 02:01:37 +03:00
|
|
|
readline_delete_char(rs);
|
2004-08-02 01:52:19 +04:00
|
|
|
break;
|
|
|
|
case 4:
|
2009-03-06 02:01:37 +03:00
|
|
|
readline_eol(rs);
|
2004-08-02 01:52:19 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-03-06 02:01:37 +03:00
|
|
|
rs->esc_state = IS_NORM;
|
2004-08-02 01:52:19 +04:00
|
|
|
the_end:
|
|
|
|
break;
|
2013-05-14 14:15:54 +04:00
|
|
|
case IS_SS3:
|
2019-04-01 05:44:05 +03:00
|
|
|
switch (ch) {
|
2013-05-14 14:15:54 +04:00
|
|
|
case 'F':
|
|
|
|
readline_eol(rs);
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
readline_bol(rs);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rs->esc_state = IS_NORM;
|
|
|
|
break;
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
2009-03-06 02:01:37 +03:00
|
|
|
readline_update(rs);
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
|
|
|
|
2009-03-06 02:01:37 +03:00
|
|
|
void readline_start(ReadLineState *rs, const char *prompt, int read_password,
|
2004-08-02 01:52:19 +04:00
|
|
|
ReadLineFunc *readline_func, void *opaque)
|
|
|
|
{
|
2009-03-06 02:01:37 +03:00
|
|
|
pstrcpy(rs->prompt, sizeof(rs->prompt), prompt);
|
|
|
|
rs->readline_func = readline_func;
|
|
|
|
rs->readline_opaque = opaque;
|
|
|
|
rs->read_password = read_password;
|
2009-03-06 02:01:47 +03:00
|
|
|
readline_restart(rs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void readline_restart(ReadLineState *rs)
|
|
|
|
{
|
2009-03-06 02:01:37 +03:00
|
|
|
rs->cmd_buf_index = 0;
|
|
|
|
rs->cmd_buf_size = 0;
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|
|
|
|
|
2009-03-06 02:01:37 +03:00
|
|
|
const char *readline_get_history(ReadLineState *rs, unsigned int index)
|
2004-08-02 01:52:19 +04:00
|
|
|
{
|
2019-03-30 14:21:42 +03:00
|
|
|
if (index >= READLINE_MAX_CMDS) {
|
2004-08-02 01:52:19 +04:00
|
|
|
return NULL;
|
2019-03-30 14:21:42 +03:00
|
|
|
}
|
2009-03-06 02:01:37 +03:00
|
|
|
return rs->history[index];
|
|
|
|
}
|
|
|
|
|
2018-01-04 19:05:15 +03:00
|
|
|
void readline_free(ReadLineState *rs)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!rs) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (i = 0; i < READLINE_MAX_CMDS; i++) {
|
|
|
|
g_free(rs->history[i]);
|
|
|
|
}
|
|
|
|
g_free(rs);
|
|
|
|
}
|
|
|
|
|
2013-11-14 14:54:14 +04:00
|
|
|
ReadLineState *readline_init(ReadLinePrintfFunc *printf_func,
|
|
|
|
ReadLineFlushFunc *flush_func,
|
|
|
|
void *opaque,
|
2009-03-06 02:01:37 +03:00
|
|
|
ReadLineCompletionFunc *completion_finder)
|
|
|
|
{
|
2018-01-04 19:05:15 +03:00
|
|
|
ReadLineState *rs = g_new0(ReadLineState, 1);
|
2009-03-06 02:01:37 +03:00
|
|
|
|
|
|
|
rs->hist_entry = -1;
|
2013-11-14 14:54:14 +04:00
|
|
|
rs->opaque = opaque;
|
|
|
|
rs->printf_func = printf_func;
|
|
|
|
rs->flush_func = flush_func;
|
2009-03-06 02:01:37 +03:00
|
|
|
rs->completion_finder = completion_finder;
|
|
|
|
|
|
|
|
return rs;
|
2004-08-02 01:52:19 +04:00
|
|
|
}
|