readline: Extract readline_add_completion_of() from monitor

monitor/misc.h has static add_completion_option().  It's useful
elsewhere in the monitor.  Since it's not monitor-specific, move it to
util/readline.c renamed to readline_add_completion_of(), and put it to
use.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20230124121946.1139465-7-armbru@redhat.com>
This commit is contained in:
Markus Armbruster 2023-01-24 13:19:20 +01:00
parent 444ee02c5f
commit 52f50b1e9f
4 changed files with 41 additions and 66 deletions

View file

@ -44,6 +44,8 @@ typedef struct ReadLineState {
} ReadLineState; } ReadLineState;
void readline_add_completion(ReadLineState *rs, const char *str); void readline_add_completion(ReadLineState *rs, const char *str);
void readline_add_completion_of(ReadLineState *rs,
const char *pfx, const char *str);
void readline_set_completion_index(ReadLineState *rs, int completion_index); void readline_set_completion_index(ReadLineState *rs, int completion_index);
const char *readline_get_history(ReadLineState *rs, unsigned int index); const char *readline_get_history(ReadLineState *rs, unsigned int index);

View file

@ -1189,8 +1189,8 @@ static void cmd_completion(MonitorHMP *mon, const char *name, const char *list)
} }
memcpy(cmd, pstart, len); memcpy(cmd, pstart, len);
cmd[len] = '\0'; cmd[len] = '\0';
if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) { if (name[0] == '\0') {
readline_add_completion(mon->rs, cmd); readline_add_completion_of(mon->rs, name, cmd);
} }
if (*p == '\0') { if (*p == '\0') {
break; break;
@ -1270,7 +1270,7 @@ static void monitor_find_completion_by_table(MonitorHMP *mon,
{ {
const char *cmdname; const char *cmdname;
int i; int i;
const char *ptype, *old_ptype, *str, *name; const char *ptype, *old_ptype, *str;
const HMPCommand *cmd; const HMPCommand *cmd;
BlockBackend *blk = NULL; BlockBackend *blk = NULL;
@ -1335,10 +1335,8 @@ static void monitor_find_completion_by_table(MonitorHMP *mon,
/* block device name completion */ /* block device name completion */
readline_set_completion_index(mon->rs, strlen(str)); readline_set_completion_index(mon->rs, strlen(str));
while ((blk = blk_next(blk)) != NULL) { while ((blk = blk_next(blk)) != NULL) {
name = blk_name(blk); if (str[0] == '\0') {
if (str[0] == '\0' || readline_add_completion_of(mon->rs, str, blk_name(blk));
!strncmp(name, str, strlen(str))) {
readline_add_completion(mon->rs, name);
} }
} }
break; break;

View file

@ -1350,14 +1350,6 @@ int get_monitor_def(Monitor *mon, int64_t *pval, const char *name)
return ret; return ret;
} }
static void add_completion_option(ReadLineState *rs, const char *str,
const char *option)
{
if (!strncmp(option, str, strlen(str))) {
readline_add_completion(rs, option);
}
}
void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str) void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
{ {
size_t len; size_t len;
@ -1369,7 +1361,7 @@ void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
len = strlen(str); len = strlen(str);
readline_set_completion_index(rs, len); readline_set_completion_index(rs, len);
for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) { for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
add_completion_option(rs, str, NetClientDriver_str(i)); readline_add_completion_of(rs, str, NetClientDriver_str(i));
} }
} }
@ -1386,14 +1378,12 @@ void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
readline_set_completion_index(rs, len); readline_set_completion_index(rs, len);
list = elt = object_class_get_list(TYPE_DEVICE, false); list = elt = object_class_get_list(TYPE_DEVICE, false);
while (elt) { while (elt) {
const char *name;
DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data, DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
TYPE_DEVICE); TYPE_DEVICE);
name = object_class_get_name(OBJECT_CLASS(dc));
if (dc->user_creatable if (dc->user_creatable) {
&& !strncmp(name, str, len)) { readline_add_completion_of(rs, str,
readline_add_completion(rs, name); object_class_get_name(OBJECT_CLASS(dc)));
} }
elt = elt->next; elt = elt->next;
} }
@ -1416,8 +1406,8 @@ void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
const char *name; const char *name;
name = object_class_get_name(OBJECT_CLASS(elt->data)); name = object_class_get_name(OBJECT_CLASS(elt->data));
if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) { if (strcmp(name, TYPE_USER_CREATABLE)) {
readline_add_completion(rs, name); readline_add_completion_of(rs, str, name);
} }
elt = elt->next; elt = elt->next;
} }
@ -1450,7 +1440,7 @@ static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
} }
static void peripheral_device_del_completion(ReadLineState *rs, static void peripheral_device_del_completion(ReadLineState *rs,
const char *str, size_t len) const char *str)
{ {
Object *peripheral = container_get(qdev_get_machine(), "/peripheral"); Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
GSList *list, *item; GSList *list, *item;
@ -1463,8 +1453,8 @@ static void peripheral_device_del_completion(ReadLineState *rs,
for (item = list; item; item = g_slist_next(item)) { for (item = list; item; item = g_slist_next(item)) {
DeviceState *dev = item->data; DeviceState *dev = item->data;
if (dev->id && !strncmp(str, dev->id, len)) { if (dev->id) {
readline_add_completion(rs, dev->id); readline_add_completion_of(rs, str, dev->id);
} }
} }
@ -1473,15 +1463,12 @@ static void peripheral_device_del_completion(ReadLineState *rs,
void device_del_completion(ReadLineState *rs, int nb_args, const char *str) void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
{ {
size_t len;
if (nb_args != 2) { if (nb_args != 2) {
return; return;
} }
len = strlen(str); readline_set_completion_index(rs, strlen(str));
readline_set_completion_index(rs, len); peripheral_device_del_completion(rs, str);
peripheral_device_del_completion(rs, str, len);
} }
void object_del_completion(ReadLineState *rs, int nb_args, const char *str) void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
@ -1499,9 +1486,8 @@ void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
while (list) { while (list) {
ObjectPropertyInfo *info = list->value; ObjectPropertyInfo *info = list->value;
if (!strncmp(info->type, "child<", 5) if (!strncmp(info->type, "child<", 5)) {
&& !strncmp(info->name, str, len)) { readline_add_completion_of(rs, str, info->name);
readline_add_completion(rs, info->name);
} }
list = list->next; list = list->next;
} }
@ -1521,14 +1507,11 @@ void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
NET_CLIENT_DRIVER_NONE, NET_CLIENT_DRIVER_NONE,
MAX_QUEUE_NUM); MAX_QUEUE_NUM);
for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) { for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
const char *name = ncs[i]->name; readline_add_completion_of(rs, str, ncs[i]->name);
if (!strncmp(str, name, len)) {
readline_add_completion(rs, name);
}
} }
} else if (nb_args == 3) { } else if (nb_args == 3) {
add_completion_option(rs, str, "on"); readline_add_completion_of(rs, str, "on");
add_completion_option(rs, str, "off"); readline_add_completion_of(rs, str, "off");
} }
} }
@ -1546,12 +1529,8 @@ void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC, count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
MAX_QUEUE_NUM); MAX_QUEUE_NUM);
for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) { for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
const char *name = ncs[i]->name;
if (strncmp(str, name, len)) {
continue;
}
if (ncs[i]->is_netdev) { if (ncs[i]->is_netdev) {
readline_add_completion(rs, name); readline_add_completion_of(rs, str, ncs[i]->name);
} }
} }
} }
@ -1590,8 +1569,8 @@ void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
} }
g_free(pattern); g_free(pattern);
} else if (nb_args == 3) { } else if (nb_args == 3) {
add_completion_option(rs, str, "on"); readline_add_completion_of(rs, str, "on");
add_completion_option(rs, str, "off"); readline_add_completion_of(rs, str, "off");
} }
} }
@ -1604,7 +1583,7 @@ void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
} }
readline_set_completion_index(rs, strlen(str)); readline_set_completion_index(rs, strlen(str));
for (i = 0; i < WATCHDOG_ACTION__MAX; i++) { for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
add_completion_option(rs, str, WatchdogAction_str(i)); readline_add_completion_of(rs, str, WatchdogAction_str(i));
} }
} }
@ -1618,14 +1597,11 @@ void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
if (nb_args == 2) { if (nb_args == 2) {
int i; int i;
for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) { for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
const char *name = MigrationCapability_str(i); readline_add_completion_of(rs, str, MigrationCapability_str(i));
if (!strncmp(str, name, len)) {
readline_add_completion(rs, name);
}
} }
} else if (nb_args == 3) { } else if (nb_args == 3) {
add_completion_option(rs, str, "on"); readline_add_completion_of(rs, str, "on");
add_completion_option(rs, str, "off"); readline_add_completion_of(rs, str, "off");
} }
} }
@ -1639,10 +1615,7 @@ void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
if (nb_args == 2) { if (nb_args == 2) {
int i; int i;
for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) { for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
const char *name = MigrationParameter_str(i); readline_add_completion_of(rs, str, MigrationParameter_str(i));
if (!strncmp(str, name, len)) {
readline_add_completion(rs, name);
}
} }
} }
} }
@ -1672,14 +1645,8 @@ static void vm_completion(ReadLineState *rs, const char *str)
snapshot = snapshots; snapshot = snapshots;
while (snapshot) { while (snapshot) {
char *completion = snapshot->value->name; readline_add_completion_of(rs, str, snapshot->value->name);
if (!strncmp(str, completion, len)) { readline_add_completion_of(rs, str, snapshot->value->id);
readline_add_completion(rs, completion);
}
completion = snapshot->value->id;
if (!strncmp(str, completion, len)) {
readline_add_completion(rs, completion);
}
snapshot = snapshot->next; snapshot = snapshot->next;
} }
qapi_free_SnapshotInfoList(snapshots); qapi_free_SnapshotInfoList(snapshots);

View file

@ -286,6 +286,14 @@ void readline_add_completion(ReadLineState *rs, const char *str)
} }
} }
void readline_add_completion_of(ReadLineState *rs,
const char *pfx, const char *str)
{
if (!strncmp(str, pfx, strlen(pfx))) {
readline_add_completion(rs, str);
}
}
void readline_set_completion_index(ReadLineState *rs, int index) void readline_set_completion_index(ReadLineState *rs, int index)
{ {
rs->completion_index = index; rs->completion_index = index;