2015-06-16 00:53:58 +08:00
|
|
|
/*
|
|
|
|
* vsh.c: common data to be used by clients to exercise the libvirt API
|
|
|
|
*
|
2019-03-23 03:02:39 +08:00
|
|
|
* Copyright (C) 2005-2019 Red Hat, Inc.
|
2015-06-16 00:53:58 +08:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include "vsh.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#if WITH_READLINE
|
2020-09-02 22:37:04 +08:00
|
|
|
/* In order to have proper rl_message declaration with older
|
|
|
|
* versions of readline, we have to declare this. See 9ea3424a178
|
|
|
|
* for more info. */
|
|
|
|
# define HAVE_STDARG_H
|
2015-06-16 00:53:58 +08:00
|
|
|
# include <readline/readline.h>
|
|
|
|
# include <readline/history.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "internal.h"
|
|
|
|
#include "virbuffer.h"
|
|
|
|
#include "viralloc.h"
|
|
|
|
#include "virfile.h"
|
|
|
|
#include "virthread.h"
|
|
|
|
#include "vircommand.h"
|
|
|
|
#include "virstring.h"
|
2020-02-17 05:59:28 +08:00
|
|
|
#include "virutil.h"
|
2015-06-16 00:53:58 +08:00
|
|
|
|
2016-09-06 20:04:37 +08:00
|
|
|
#ifdef WITH_READLINE
|
|
|
|
/* For autocompletion */
|
2017-11-07 17:41:00 +08:00
|
|
|
vshControl *autoCompleteOpaque;
|
2016-09-06 20:04:37 +08:00
|
|
|
#endif
|
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
/* NOTE: It would be much nicer to have these two as part of vshControl
|
|
|
|
* structure, unfortunately readline doesn't support passing opaque data
|
|
|
|
* and only relies on static data accessible from the user-side callback
|
|
|
|
*/
|
|
|
|
const vshCmdGrp *cmdGroups;
|
|
|
|
|
2016-02-12 21:00:28 +08:00
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
double
|
|
|
|
vshPrettyCapacity(unsigned long long val, const char **unit)
|
|
|
|
{
|
|
|
|
double limit = 1024;
|
|
|
|
|
|
|
|
if (val < limit) {
|
|
|
|
*unit = "B";
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
limit *= 1024;
|
|
|
|
if (val < limit) {
|
|
|
|
*unit = "KiB";
|
|
|
|
return val / (limit / 1024);
|
|
|
|
}
|
|
|
|
limit *= 1024;
|
|
|
|
if (val < limit) {
|
|
|
|
*unit = "MiB";
|
|
|
|
return val / (limit / 1024);
|
|
|
|
}
|
|
|
|
limit *= 1024;
|
|
|
|
if (val < limit) {
|
|
|
|
*unit = "GiB";
|
|
|
|
return val / (limit / 1024);
|
|
|
|
}
|
|
|
|
limit *= 1024;
|
|
|
|
if (val < limit) {
|
|
|
|
*unit = "TiB";
|
|
|
|
return val / (limit / 1024);
|
|
|
|
}
|
|
|
|
limit *= 1024;
|
|
|
|
if (val < limit) {
|
|
|
|
*unit = "PiB";
|
|
|
|
return val / (limit / 1024);
|
|
|
|
}
|
|
|
|
limit *= 1024;
|
|
|
|
*unit = "EiB";
|
|
|
|
return val / (limit / 1024);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
vshNameSorter(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const char **sa = (const char**)a;
|
|
|
|
const char **sb = (const char**)b;
|
|
|
|
|
|
|
|
return vshStrcasecmp(*sa, *sb);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert the strings separated by ',' into array. The returned
|
|
|
|
* array is a NULL terminated string list. The caller has to free
|
2020-08-03 06:01:11 +08:00
|
|
|
* the array using g_strfreev or a similar method.
|
2015-06-16 00:53:58 +08:00
|
|
|
*
|
|
|
|
* Returns the length of the filled array on success, or -1
|
|
|
|
* on error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vshStringToArray(const char *str,
|
|
|
|
char ***array)
|
|
|
|
{
|
2021-08-11 21:04:14 +08:00
|
|
|
g_auto(GStrv) tmp = NULL;
|
|
|
|
GStrv n;
|
|
|
|
size_t ntoks = 0;
|
|
|
|
bool concat = false;
|
|
|
|
|
|
|
|
tmp = g_strsplit(str, ",", 0);
|
|
|
|
|
|
|
|
*array = g_new0(char *, g_strv_length(tmp) + 1);
|
|
|
|
(*array)[ntoks++] = g_strdup(tmp[0]);
|
|
|
|
|
|
|
|
/* undo splitting of comma escape (',,') by concatenating back on empty strings */
|
|
|
|
for (n = tmp + 1; n[0]; n++) {
|
|
|
|
if (concat) {
|
|
|
|
g_autofree char *old = (*array)[ntoks - 1];
|
2015-06-16 00:53:58 +08:00
|
|
|
|
2021-08-11 21:04:14 +08:00
|
|
|
(*array)[ntoks - 1] = g_strconcat(old, ",", n[0], NULL);
|
|
|
|
concat = false;
|
2015-06-16 00:53:58 +08:00
|
|
|
continue;
|
|
|
|
}
|
2021-08-11 21:04:14 +08:00
|
|
|
|
|
|
|
if (strlen(n[0]) == 0) {
|
|
|
|
concat = true;
|
|
|
|
} else {
|
|
|
|
(*array)[ntoks++] = g_strdup(n[0]);
|
|
|
|
}
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
2021-08-11 21:04:14 +08:00
|
|
|
/* corner case of ending with a single comma */
|
|
|
|
if (concat)
|
|
|
|
(*array)[ntoks++] = g_strdup("");
|
|
|
|
|
|
|
|
return ntoks;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virErrorPtr last_error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Quieten libvirt until we're done with the command.
|
|
|
|
*/
|
|
|
|
void
|
2019-10-14 20:44:29 +08:00
|
|
|
vshErrorHandler(void *opaque G_GNUC_UNUSED,
|
|
|
|
virErrorPtr error G_GNUC_UNUSED)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
|
|
|
virFreeError(last_error);
|
|
|
|
last_error = virSaveLastError();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store a libvirt error that is from a helper API that doesn't raise errors
|
|
|
|
* so it doesn't get overwritten */
|
|
|
|
void
|
|
|
|
vshSaveLibvirtError(void)
|
|
|
|
{
|
|
|
|
virFreeError(last_error);
|
|
|
|
last_error = virSaveLastError();
|
|
|
|
}
|
|
|
|
|
2017-04-11 23:23:23 +08:00
|
|
|
|
|
|
|
/* Store libvirt error from helper API but don't overwrite existing errors */
|
|
|
|
void
|
|
|
|
vshSaveLibvirtHelperError(void)
|
|
|
|
{
|
|
|
|
if (last_error)
|
|
|
|
return;
|
|
|
|
|
2018-05-05 20:04:21 +08:00
|
|
|
if (virGetLastErrorCode() == VIR_ERR_OK)
|
2017-04-11 23:23:23 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
vshSaveLibvirtError();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
/*
|
|
|
|
* Reset libvirt error on graceful fallback paths
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vshResetLibvirtError(void)
|
|
|
|
{
|
2022-01-29 01:42:45 +08:00
|
|
|
g_clear_pointer(&last_error, virFreeError);
|
2018-10-12 00:29:58 +08:00
|
|
|
virResetLastError();
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Report an error when a command finishes. This is better than before
|
|
|
|
* (when correct operation would report errors), but it has some
|
|
|
|
* problems: we lose the smarter formatting of virDefaultErrorFunc(),
|
|
|
|
* and it can become harder to debug problems, if errors get reported
|
|
|
|
* twice during one command. This case shouldn't really happen anyway,
|
|
|
|
* and it's IMHO a bug that libvirt does that sometimes.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vshReportError(vshControl *ctl)
|
|
|
|
{
|
|
|
|
if (last_error == NULL) {
|
|
|
|
/* Calling directly into libvirt util functions won't trigger the
|
|
|
|
* error callback (which sets last_error), so check it ourselves.
|
|
|
|
*
|
|
|
|
* If the returned error has CODE_OK, this most likely means that
|
|
|
|
* no error was ever raised, so just ignore */
|
|
|
|
last_error = virSaveLastError();
|
|
|
|
if (!last_error || last_error->code == VIR_ERR_OK)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last_error->code == VIR_ERR_OK) {
|
|
|
|
vshError(ctl, "%s", _("unknown error"));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
vshError(ctl, "%s", last_error->message);
|
|
|
|
|
|
|
|
out:
|
|
|
|
vshResetLibvirtError();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Detection of disconnections and automatic reconnection support
|
|
|
|
*/
|
|
|
|
static int disconnected; /* we may have been disconnected */
|
|
|
|
|
2024-03-01 23:39:39 +08:00
|
|
|
/* Check if the internal command definitions are correct.
|
|
|
|
* None of the errors are to be marked as translatable. */
|
2015-06-16 00:53:58 +08:00
|
|
|
static int
|
2019-03-12 10:17:33 +08:00
|
|
|
vshCmddefCheckInternals(vshControl *ctl,
|
2021-03-04 22:47:28 +08:00
|
|
|
const vshCmdDef *cmd,
|
2024-03-14 04:13:44 +08:00
|
|
|
bool missingCompleters,
|
|
|
|
int brokenPositionals)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
|
|
|
size_t i;
|
2020-11-12 20:30:29 +08:00
|
|
|
bool seenOptionalOption = false;
|
2024-03-14 23:21:58 +08:00
|
|
|
const char *seenOptionalPositionalOption = NULL;
|
2021-03-04 22:47:28 +08:00
|
|
|
g_auto(virBuffer) complbuf = VIR_BUFFER_INITIALIZER;
|
2024-03-14 04:13:44 +08:00
|
|
|
g_auto(virBuffer) posbuf = VIR_BUFFER_INITIALIZER;
|
2016-09-19 19:10:38 +08:00
|
|
|
|
2016-09-07 23:30:48 +08:00
|
|
|
/* in order to perform the validation resolve the alias first */
|
2024-03-02 06:14:51 +08:00
|
|
|
if (cmd->alias) {
|
2021-09-16 21:44:25 +08:00
|
|
|
const vshCmdDef *alias;
|
|
|
|
|
|
|
|
if (!(alias = vshCmddefSearch(cmd->alias))) {
|
2024-03-01 23:39:39 +08:00
|
|
|
vshError(ctl, "command alias '%s' is pointing to a non-existent command '%s'",
|
2021-09-16 21:44:25 +08:00
|
|
|
cmd->name, cmd->alias);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-03-02 06:14:51 +08:00
|
|
|
if (alias->alias) {
|
2024-03-01 23:39:39 +08:00
|
|
|
vshError(ctl, "command alias '%s' is pointing to another command alias '%s'",
|
2021-09-16 21:44:25 +08:00
|
|
|
cmd->name, cmd->alias);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-09-17 15:52:43 +08:00
|
|
|
if (cmd->handler) {
|
2024-03-01 23:39:39 +08:00
|
|
|
vshError(ctl, "command '%s' has handler set", cmd->name);
|
2021-09-17 15:52:43 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd->opts) {
|
2024-03-01 23:39:39 +08:00
|
|
|
vshError(ctl, "command '%s' has options set", cmd->name);
|
2021-09-17 15:52:43 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd->info) {
|
2024-03-01 23:39:39 +08:00
|
|
|
vshError(ctl, "command '%s' has info set", cmd->name);
|
2021-09-17 15:52:43 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-03-02 06:14:51 +08:00
|
|
|
if (cmd->flags != 0) {
|
2024-03-01 23:39:39 +08:00
|
|
|
vshError(ctl, "command '%s' has multiple flags set", cmd->name);
|
2021-09-17 15:52:43 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-09-16 21:44:25 +08:00
|
|
|
/* we don't need to continue as the real command will be checked separately */
|
|
|
|
return 0;
|
2016-09-07 23:30:48 +08:00
|
|
|
}
|
|
|
|
|
2016-09-19 19:10:38 +08:00
|
|
|
/* Each command has to provide a non-empty help string. */
|
2024-03-04 20:21:50 +08:00
|
|
|
if (!cmd->info || !cmd->info->help || !*cmd->info->help) {
|
2024-03-01 23:39:39 +08:00
|
|
|
vshError(ctl, "command '%s' lacks help", cmd->name);
|
2016-09-19 19:10:38 +08:00
|
|
|
return -1;
|
2019-03-12 10:17:33 +08:00
|
|
|
}
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
if (!cmd->opts)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; cmd->opts[i].name; i++) {
|
|
|
|
const vshCmdOptDef *opt = &cmd->opts[i];
|
|
|
|
|
2019-03-12 10:17:33 +08:00
|
|
|
if (i > 63) {
|
2024-03-01 23:39:39 +08:00
|
|
|
vshError(ctl, "command '%s' has too many options", cmd->name);
|
2015-06-16 00:53:58 +08:00
|
|
|
return -1; /* too many options */
|
2019-03-12 10:17:33 +08:00
|
|
|
}
|
2017-11-16 20:38:27 +08:00
|
|
|
|
2024-03-01 19:35:46 +08:00
|
|
|
if (missingCompleters && !opt->completer) {
|
|
|
|
switch (opt->type) {
|
|
|
|
case VSH_OT_STRING:
|
|
|
|
case VSH_OT_ARGV:
|
|
|
|
virBufferStrcat(&complbuf, opt->name, ", ", NULL);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VSH_OT_BOOL:
|
|
|
|
/* only name is completed */
|
|
|
|
case VSH_OT_INT:
|
|
|
|
/* no point in completing numbers */
|
|
|
|
case VSH_OT_ALIAS:
|
|
|
|
/* alias is handled in the referenced command */
|
|
|
|
case VSH_OT_NONE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-03-04 22:47:28 +08:00
|
|
|
|
2024-03-14 04:13:44 +08:00
|
|
|
if (brokenPositionals >= 0) {
|
|
|
|
switch (opt->type) {
|
|
|
|
case VSH_OT_INT:
|
|
|
|
case VSH_OT_STRING:
|
|
|
|
case VSH_OT_ARGV:
|
|
|
|
if (brokenPositionals == 0 ||
|
|
|
|
brokenPositionals == opt->type) {
|
2024-03-14 05:31:20 +08:00
|
|
|
if (!(opt->flags & VSH_OFLAG_REQ_OPT) &&
|
|
|
|
!(opt->positional || opt->unwanted_positional))
|
2024-03-14 04:13:44 +08:00
|
|
|
virBufferStrcat(&posbuf, opt->name, ", ", NULL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VSH_OT_BOOL:
|
|
|
|
/* only name is completed */
|
|
|
|
/* no point in completing numbers */
|
|
|
|
case VSH_OT_ALIAS:
|
|
|
|
/* alias is handled in the referenced command */
|
|
|
|
case VSH_OT_NONE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-14 23:21:58 +08:00
|
|
|
/* allow at most one optional positional option */
|
|
|
|
if (opt->positional && !opt->required) {
|
|
|
|
if (seenOptionalPositionalOption) {
|
|
|
|
vshError(ctl, "multiple optional positional arguments (%s, %s) of command '%s' are not allowed",
|
|
|
|
seenOptionalPositionalOption, opt->name, cmd->name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
seenOptionalPositionalOption = opt->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* all optional positional arguments must be defined after the required ones */
|
|
|
|
if (seenOptionalPositionalOption && opt->positional && opt->required) {
|
|
|
|
vshError(ctl, "required positional argument '%s' declared after an optional positional argument '%s' of command '%s'",
|
|
|
|
opt->name, seenOptionalPositionalOption, cmd->name);
|
2024-03-05 23:17:21 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-03-14 17:31:17 +08:00
|
|
|
/* Mandate no completer flags if no completer is specified */
|
|
|
|
if (opt->completer_flags != 0 && !opt->completer) {
|
|
|
|
vshError(ctl, "completer_flags of argument '%s' of command '%s' must be 0 if no completer is used",
|
|
|
|
opt->name, cmd->name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-03-14 05:31:20 +08:00
|
|
|
if (opt->unwanted_positional && opt->positional) {
|
|
|
|
vshError(ctl, "unwanted_positional flag of argument '%s' of command '%s' must not be used together with positional",
|
|
|
|
opt->name, cmd->name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-11-16 20:38:27 +08:00
|
|
|
switch (opt->type) {
|
2024-03-04 22:40:40 +08:00
|
|
|
case VSH_OT_NONE:
|
|
|
|
vshError(ctl, "invalid type 'NONE' of option '%s' of command '%s'",
|
|
|
|
opt->name, cmd->name);
|
|
|
|
return -1;
|
|
|
|
|
2017-11-16 20:38:27 +08:00
|
|
|
case VSH_OT_BOOL:
|
2024-03-14 17:31:17 +08:00
|
|
|
if (opt->completer) {
|
2024-03-01 23:39:39 +08:00
|
|
|
vshError(ctl, "bool parameter '%s' of command '%s' has completer set",
|
2021-09-17 15:58:03 +08:00
|
|
|
opt->name, cmd->name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-03-14 05:31:20 +08:00
|
|
|
if (opt->positional || opt->unwanted_positional) {
|
2024-03-14 17:31:17 +08:00
|
|
|
vshError(ctl, "boolean parameter '%s' of command '%s' must not be positional",
|
|
|
|
opt->name, cmd->name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-03-06 00:21:05 +08:00
|
|
|
if (opt->required) {
|
|
|
|
vshError(ctl, "parameter '%s' of command '%s' misused 'required' flag",
|
2020-11-12 19:04:11 +08:00
|
|
|
opt->name, cmd->name);
|
2024-03-05 23:07:36 +08:00
|
|
|
return -1; /* bool can't be mandatory */
|
2019-03-12 10:17:33 +08:00
|
|
|
}
|
2020-11-12 20:30:29 +08:00
|
|
|
|
2017-11-16 20:38:27 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VSH_OT_ALIAS: {
|
2015-06-16 00:53:58 +08:00
|
|
|
size_t j;
|
2021-08-11 00:56:18 +08:00
|
|
|
g_autofree char *name = NULL;
|
2015-06-16 00:53:58 +08:00
|
|
|
char *p;
|
|
|
|
|
2024-03-14 17:31:17 +08:00
|
|
|
if (opt->required ||
|
|
|
|
opt->positional ||
|
2024-03-14 05:31:20 +08:00
|
|
|
opt->unwanted_positional ||
|
2024-03-14 17:31:17 +08:00
|
|
|
opt->completer ||
|
|
|
|
opt->flags ||
|
|
|
|
!opt->help) {
|
2024-03-01 23:39:39 +08:00
|
|
|
vshError(ctl, "parameter '%s' of command '%s' has incorrect alias option",
|
2020-11-12 19:04:11 +08:00
|
|
|
opt->name, cmd->name);
|
2024-03-01 23:39:39 +08:00
|
|
|
return -1;
|
2019-03-12 10:17:33 +08:00
|
|
|
}
|
2021-08-11 00:56:18 +08:00
|
|
|
if ((p = strchr(opt->help, '=')))
|
|
|
|
name = g_strndup(opt->help, p - opt->help);
|
|
|
|
else
|
|
|
|
name = g_strdup(opt->help);
|
2015-06-16 00:53:58 +08:00
|
|
|
for (j = i + 1; cmd->opts[j].name; j++) {
|
|
|
|
if (STREQ(name, cmd->opts[j].name) &&
|
|
|
|
cmd->opts[j].type != VSH_OT_ALIAS)
|
|
|
|
break;
|
|
|
|
}
|
2021-08-11 00:56:18 +08:00
|
|
|
if (p) {
|
2015-06-16 00:53:58 +08:00
|
|
|
/* If alias comes with value, replacement must not be bool */
|
2019-03-12 10:17:33 +08:00
|
|
|
if (cmd->opts[j].type == VSH_OT_BOOL) {
|
2024-03-01 23:39:39 +08:00
|
|
|
vshError(ctl, "alias '%s' of command '%s' has mismatched alias type",
|
2020-11-12 19:04:11 +08:00
|
|
|
opt->name, cmd->name);
|
2015-06-16 00:53:58 +08:00
|
|
|
return -1;
|
2019-03-12 10:17:33 +08:00
|
|
|
}
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
2019-03-12 10:17:33 +08:00
|
|
|
if (!cmd->opts[j].name) {
|
2024-03-01 23:39:39 +08:00
|
|
|
vshError(ctl, "alias '%s' of command '%s' has missing alias option",
|
2020-11-12 19:04:11 +08:00
|
|
|
opt->name, cmd->name);
|
2024-03-01 23:39:39 +08:00
|
|
|
return -1;
|
2019-03-12 10:17:33 +08:00
|
|
|
}
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
2017-11-16 20:38:27 +08:00
|
|
|
break;
|
2024-03-05 22:07:47 +08:00
|
|
|
|
2017-11-16 20:38:27 +08:00
|
|
|
case VSH_OT_ARGV:
|
2019-03-12 10:17:33 +08:00
|
|
|
if (cmd->opts[i + 1].name) {
|
2024-03-01 23:39:39 +08:00
|
|
|
vshError(ctl, "parameter '%s' of command '%s' must be listed last",
|
2020-11-12 19:04:11 +08:00
|
|
|
opt->name, cmd->name);
|
2024-03-01 23:39:39 +08:00
|
|
|
return -1;
|
2019-03-12 10:17:33 +08:00
|
|
|
}
|
2017-11-16 20:38:27 +08:00
|
|
|
break;
|
|
|
|
|
2024-03-05 23:07:36 +08:00
|
|
|
case VSH_OT_INT:
|
|
|
|
case VSH_OT_STRING:
|
|
|
|
if (opt->positional && seenOptionalOption) {
|
2024-03-01 23:39:39 +08:00
|
|
|
vshError(ctl, "parameter '%s' of command '%s' must be listed before optional parameters",
|
2020-11-12 20:30:29 +08:00
|
|
|
opt->name, cmd->name);
|
2024-03-01 23:39:39 +08:00
|
|
|
return -1;
|
2020-11-12 20:30:29 +08:00
|
|
|
}
|
|
|
|
|
2024-03-05 23:07:36 +08:00
|
|
|
seenOptionalOption = !opt->required;
|
2017-11-16 20:38:27 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-07-08 20:56:02 +08:00
|
|
|
}
|
2021-03-04 22:47:28 +08:00
|
|
|
|
|
|
|
virBufferTrim(&complbuf, ", ");
|
2024-03-14 04:13:44 +08:00
|
|
|
virBufferTrim(&posbuf, ", ");
|
2021-03-04 22:47:28 +08:00
|
|
|
|
|
|
|
if (missingCompleters && virBufferUse(&complbuf) > 0)
|
|
|
|
vshPrintExtra(ctl, "%s: %s\n", cmd->name, virBufferCurrentContent(&complbuf));
|
|
|
|
|
2024-03-14 04:13:44 +08:00
|
|
|
if (virBufferUse(&posbuf)) {
|
|
|
|
vshPrintExtra(ctl, "%s: %s\n", cmd->name, virBufferCurrentContent(&posbuf));
|
|
|
|
}
|
|
|
|
|
2016-07-08 20:56:02 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-16 16:17:46 +08:00
|
|
|
/* Parse the options associated with @cmd, i.e. test whether options are
|
2020-11-12 20:49:21 +08:00
|
|
|
* required or need an argument and fill the appropriate caller-provided bitmaps
|
2016-09-16 16:17:46 +08:00
|
|
|
*/
|
2020-11-12 20:49:21 +08:00
|
|
|
static void
|
|
|
|
vshCmddefOptParse(const vshCmdDef *cmd,
|
|
|
|
uint64_t *opts_need_arg,
|
2016-09-16 16:17:46 +08:00
|
|
|
uint64_t *opts_required)
|
2016-07-08 20:56:02 +08:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
*opts_need_arg = 0;
|
|
|
|
*opts_required = 0;
|
|
|
|
|
|
|
|
if (!cmd->opts)
|
2020-11-12 20:49:21 +08:00
|
|
|
return;
|
2016-07-08 20:56:02 +08:00
|
|
|
|
|
|
|
for (i = 0; cmd->opts[i].name; i++) {
|
|
|
|
const vshCmdOptDef *opt = &cmd->opts[i];
|
|
|
|
|
2020-11-12 20:34:51 +08:00
|
|
|
if (opt->type == VSH_OT_BOOL)
|
2016-07-08 20:56:02 +08:00
|
|
|
continue;
|
2015-06-16 00:53:58 +08:00
|
|
|
|
2016-07-08 20:56:02 +08:00
|
|
|
if (opt->type == VSH_OT_ALIAS)
|
|
|
|
continue; /* skip the alias option */
|
|
|
|
|
2020-11-12 20:34:51 +08:00
|
|
|
if (!(opt->flags & VSH_OFLAG_REQ_OPT))
|
|
|
|
*opts_need_arg |= 1ULL << i;
|
|
|
|
|
2024-03-06 00:21:05 +08:00
|
|
|
if (opt->required)
|
2016-04-20 21:51:43 +08:00
|
|
|
*opts_required |= 1ULL << i;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
2016-07-08 20:56:02 +08:00
|
|
|
}
|
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
static vshCmdOptDef helpopt = {
|
|
|
|
.name = "help",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("print help for this function")
|
|
|
|
};
|
2024-03-05 19:35:51 +08:00
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
static const vshCmdOptDef *
|
2024-03-05 19:35:51 +08:00
|
|
|
vshCmddefGetOption(vshControl *ctl,
|
|
|
|
const vshCmdDef *cmd,
|
|
|
|
const char *name,
|
|
|
|
uint64_t *opts_seen,
|
|
|
|
size_t *opt_index,
|
|
|
|
char **optstr,
|
2016-07-08 20:56:04 +08:00
|
|
|
bool report)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
|
|
|
size_t i;
|
2021-08-10 23:09:07 +08:00
|
|
|
g_autofree char *alias = NULL;
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
if (STREQ(name, helpopt.name))
|
|
|
|
return &helpopt;
|
|
|
|
|
|
|
|
for (i = 0; cmd->opts && cmd->opts[i].name; i++) {
|
|
|
|
const vshCmdOptDef *opt = &cmd->opts[i];
|
|
|
|
|
2024-03-05 19:35:51 +08:00
|
|
|
if (STRNEQ(opt->name, name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (opt->type == VSH_OT_ALIAS) {
|
|
|
|
char *value;
|
|
|
|
|
|
|
|
/* Two types of replacements:
|
|
|
|
opt->help = "string": straight replacement of name
|
|
|
|
opt->help = "string=value": treat boolean flag as
|
|
|
|
alias of option and its default value */
|
|
|
|
alias = g_strdup(opt->help);
|
|
|
|
name = alias;
|
|
|
|
if ((value = strchr(name, '='))) {
|
|
|
|
*value = '\0';
|
|
|
|
if (*optstr) {
|
|
|
|
if (report)
|
|
|
|
vshError(ctl, _("invalid '=' after option --%1$s"),
|
|
|
|
opt->name);
|
|
|
|
return NULL;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
2024-03-05 19:35:51 +08:00
|
|
|
*optstr = g_strdup(value + 1);
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
2024-03-05 19:35:51 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((*opts_seen & (1ULL << i)) && opt->type != VSH_OT_ARGV) {
|
|
|
|
if (report)
|
|
|
|
vshError(ctl, _("option --%1$s already seen"), name);
|
|
|
|
return NULL;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
2024-03-05 19:35:51 +08:00
|
|
|
|
|
|
|
*opts_seen |= 1ULL << i;
|
|
|
|
*opt_index = i;
|
|
|
|
return opt;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
2024-03-05 19:35:51 +08:00
|
|
|
/* The 'help' command ignores extra options */
|
2016-07-08 20:56:04 +08:00
|
|
|
if (STRNEQ(cmd->name, "help") && report) {
|
2023-03-09 22:54:58 +08:00
|
|
|
vshError(ctl, _("command '%1$s' doesn't support option --%2$s"),
|
2015-06-16 00:53:58 +08:00
|
|
|
cmd->name, name);
|
|
|
|
}
|
2021-08-12 15:59:20 +08:00
|
|
|
return NULL;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const vshCmdOptDef *
|
2016-03-31 20:41:52 +08:00
|
|
|
vshCmddefGetData(const vshCmdDef *cmd, uint64_t *opts_need_arg,
|
|
|
|
uint64_t *opts_seen)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
const vshCmdOptDef *opt;
|
|
|
|
|
|
|
|
if (!*opts_need_arg)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Grab least-significant set bit */
|
2019-10-03 22:51:30 +08:00
|
|
|
i = __builtin_ffsl(*opts_need_arg) - 1;
|
2015-06-16 00:53:58 +08:00
|
|
|
opt = &cmd->opts[i];
|
|
|
|
if (opt->type != VSH_OT_ARGV)
|
2016-04-20 21:51:43 +08:00
|
|
|
*opts_need_arg &= ~(1ULL << i);
|
|
|
|
*opts_seen |= 1ULL << i;
|
2015-06-16 00:53:58 +08:00
|
|
|
return opt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checks for required options
|
|
|
|
*/
|
|
|
|
static int
|
2016-03-31 20:41:52 +08:00
|
|
|
vshCommandCheckOpts(vshControl *ctl, const vshCmd *cmd, uint64_t opts_required,
|
|
|
|
uint64_t opts_seen)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
|
|
|
const vshCmdDef *def = cmd->def;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
opts_required &= ~opts_seen;
|
|
|
|
if (!opts_required)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; def->opts[i].name; i++) {
|
2016-04-20 21:51:43 +08:00
|
|
|
if (opts_required & (1ULL << i)) {
|
2015-06-16 00:53:58 +08:00
|
|
|
const vshCmdOptDef *opt = &def->opts[i];
|
|
|
|
|
|
|
|
vshError(ctl,
|
2024-03-05 23:07:36 +08:00
|
|
|
opt->positional ?
|
2023-03-09 22:54:58 +08:00
|
|
|
_("command '%1$s' requires <%2$s> option") :
|
|
|
|
_("command '%1$s' requires --%2$s option"),
|
2015-06-16 00:53:58 +08:00
|
|
|
def->name, opt->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-03-04 22:51:28 +08:00
|
|
|
const vshCmdDef *
|
|
|
|
vshCmddefSearch(const char *cmdname)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
|
|
|
const vshCmdGrp *g;
|
|
|
|
const vshCmdDef *c;
|
|
|
|
|
|
|
|
for (g = cmdGroups; g->name; g++) {
|
|
|
|
for (c = g->commands; c->name; c++) {
|
|
|
|
if (STREQ(c->name, cmdname))
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const vshCmdGrp *
|
|
|
|
vshCmdGrpSearch(const char *grpname)
|
|
|
|
{
|
|
|
|
const vshCmdGrp *g;
|
|
|
|
|
|
|
|
for (g = cmdGroups; g->name; g++) {
|
|
|
|
if (STREQ(g->name, grpname) || STREQ(g->keyword, grpname))
|
|
|
|
return g;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-01-25 23:08:46 +08:00
|
|
|
vshCmdGrpHelp(vshControl *ctl, const vshCmdGrp *grp)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
|
|
|
const vshCmdDef *cmd = NULL;
|
|
|
|
|
2023-03-09 22:54:58 +08:00
|
|
|
vshPrint(ctl, _(" %1$s (help keyword '%2$s'):\n"), grp->name,
|
2018-01-25 23:08:46 +08:00
|
|
|
grp->keyword);
|
2015-06-16 00:53:58 +08:00
|
|
|
|
2018-01-25 23:08:46 +08:00
|
|
|
for (cmd = grp->commands; cmd->name; cmd++) {
|
2024-03-02 06:14:51 +08:00
|
|
|
if (cmd->alias ||
|
2021-09-16 21:57:28 +08:00
|
|
|
cmd->flags & VSH_CMD_FLAG_HIDDEN)
|
2018-01-25 23:08:46 +08:00
|
|
|
continue;
|
2024-03-04 20:21:50 +08:00
|
|
|
vshPrint(ctl, " %-30s %s\n", cmd->name, _(cmd->info->help));
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-12 20:38:23 +08:00
|
|
|
static bool
|
2020-11-12 20:42:45 +08:00
|
|
|
vshCmddefHelp(const vshCmdDef *def)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
2016-09-15 23:52:11 +08:00
|
|
|
fputs(_(" NAME\n"), stdout);
|
2024-03-04 20:21:50 +08:00
|
|
|
fprintf(stdout, " %s - %s\n", def->name, _(def->info->help));
|
2016-09-15 23:52:11 +08:00
|
|
|
|
|
|
|
fputs(_("\n SYNOPSIS\n"), stdout);
|
|
|
|
fprintf(stdout, " %s", def->name);
|
|
|
|
if (def->opts) {
|
|
|
|
const vshCmdOptDef *opt;
|
|
|
|
for (opt = def->opts; opt->name; opt++) {
|
2024-03-01 21:51:46 +08:00
|
|
|
|
2016-09-15 23:52:11 +08:00
|
|
|
switch (opt->type) {
|
|
|
|
case VSH_OT_BOOL:
|
2024-03-01 21:51:46 +08:00
|
|
|
fprintf(stdout, " [--%s]", opt->name);
|
2016-09-15 23:52:11 +08:00
|
|
|
break;
|
2024-03-01 21:51:46 +08:00
|
|
|
|
2024-03-05 23:53:55 +08:00
|
|
|
case VSH_OT_STRING:
|
2016-09-15 23:52:11 +08:00
|
|
|
case VSH_OT_INT:
|
2024-03-05 23:53:55 +08:00
|
|
|
if (opt->required) {
|
|
|
|
fprintf(stdout, " ");
|
2024-03-01 21:51:46 +08:00
|
|
|
} else {
|
2024-03-05 23:53:55 +08:00
|
|
|
fprintf(stdout, " [");
|
2024-03-01 21:51:46 +08:00
|
|
|
}
|
|
|
|
|
2024-03-05 23:53:55 +08:00
|
|
|
if (opt->positional) {
|
|
|
|
fprintf(stdout, "<%s>", opt->name);
|
2024-03-01 21:51:46 +08:00
|
|
|
} else {
|
2024-03-05 23:53:55 +08:00
|
|
|
if (opt->type == VSH_OT_INT) {
|
|
|
|
fprintf(stdout, _("--%1$s <number>"), opt->name);
|
|
|
|
} else {
|
|
|
|
fprintf(stdout, _("--%1$s <string>"), opt->name);
|
|
|
|
}
|
2024-03-01 21:51:46 +08:00
|
|
|
}
|
2024-03-05 23:53:55 +08:00
|
|
|
|
|
|
|
if (!opt->required)
|
|
|
|
fprintf(stdout, "]");
|
2016-09-15 23:52:11 +08:00
|
|
|
break;
|
2024-03-01 21:51:46 +08:00
|
|
|
|
2016-09-15 23:52:11 +08:00
|
|
|
case VSH_OT_ARGV:
|
2024-03-05 23:32:40 +08:00
|
|
|
if (opt->positional) {
|
|
|
|
if (opt->required) {
|
|
|
|
fprintf(stdout, " <%s>...", opt->name);
|
2024-03-01 21:51:46 +08:00
|
|
|
} else {
|
2024-03-05 23:32:40 +08:00
|
|
|
fprintf(stdout, " [<%s>]...", opt->name);
|
2024-03-01 21:51:46 +08:00
|
|
|
}
|
2016-09-15 23:52:11 +08:00
|
|
|
} else {
|
2024-03-05 23:32:40 +08:00
|
|
|
if (opt->required) {
|
2024-03-13 23:58:52 +08:00
|
|
|
fprintf(stdout, _(" --%1$s <string>..."), opt->name);
|
2024-03-01 21:51:46 +08:00
|
|
|
} else {
|
2024-03-13 23:58:52 +08:00
|
|
|
fprintf(stdout, _(" [--%1$s <string>]..."), opt->name);
|
2024-03-01 21:51:46 +08:00
|
|
|
}
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
2016-09-15 23:52:11 +08:00
|
|
|
break;
|
2024-03-01 21:51:46 +08:00
|
|
|
|
2016-09-15 23:52:11 +08:00
|
|
|
case VSH_OT_ALIAS:
|
2024-03-04 22:40:40 +08:00
|
|
|
case VSH_OT_NONE:
|
2016-09-15 23:52:11 +08:00
|
|
|
/* aliases are intentionally undocumented */
|
|
|
|
continue;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
}
|
2016-09-15 23:52:11 +08:00
|
|
|
}
|
|
|
|
fputc('\n', stdout);
|
2015-06-16 00:53:58 +08:00
|
|
|
|
2024-03-04 20:21:50 +08:00
|
|
|
if (def->info->desc && *def->info->desc) {
|
2016-09-15 23:52:11 +08:00
|
|
|
/* Print the description only if it's not empty. */
|
|
|
|
fputs(_("\n DESCRIPTION\n"), stdout);
|
2024-03-04 20:21:50 +08:00
|
|
|
fprintf(stdout, " %s\n", _(def->info->desc));
|
2016-09-15 23:52:11 +08:00
|
|
|
}
|
2015-06-16 00:53:58 +08:00
|
|
|
|
2016-09-15 23:52:11 +08:00
|
|
|
if (def->opts && def->opts->name) {
|
|
|
|
const vshCmdOptDef *opt;
|
|
|
|
fputs(_("\n OPTIONS\n"), stdout);
|
|
|
|
for (opt = def->opts; opt->name; opt++) {
|
2024-03-01 21:51:46 +08:00
|
|
|
g_autofree char *optstr = NULL;
|
|
|
|
|
2016-09-15 23:52:11 +08:00
|
|
|
switch (opt->type) {
|
|
|
|
case VSH_OT_BOOL:
|
2024-03-01 21:51:46 +08:00
|
|
|
optstr = g_strdup_printf("--%s", opt->name);
|
2016-09-15 23:52:11 +08:00
|
|
|
break;
|
2024-03-01 21:51:46 +08:00
|
|
|
|
2016-09-15 23:52:11 +08:00
|
|
|
case VSH_OT_INT:
|
2024-03-05 23:53:55 +08:00
|
|
|
if (opt->positional) {
|
2024-03-01 21:51:46 +08:00
|
|
|
optstr = g_strdup_printf(_("[--%1$s] <number>"), opt->name);
|
|
|
|
} else {
|
|
|
|
optstr = g_strdup_printf(_("--%1$s <number>"), opt->name);
|
|
|
|
}
|
2016-09-15 23:52:11 +08:00
|
|
|
break;
|
2024-03-01 21:51:46 +08:00
|
|
|
|
2016-09-15 23:52:11 +08:00
|
|
|
case VSH_OT_STRING:
|
2024-03-05 23:53:55 +08:00
|
|
|
if (opt->positional) {
|
|
|
|
optstr = g_strdup_printf(_("[--%1$s] <string>"), opt->name);
|
|
|
|
} else {
|
|
|
|
optstr = g_strdup_printf(_("--%1$s <string>"), opt->name);
|
|
|
|
}
|
2016-09-15 23:52:11 +08:00
|
|
|
break;
|
2024-03-01 21:51:46 +08:00
|
|
|
|
2016-09-15 23:52:11 +08:00
|
|
|
case VSH_OT_ARGV:
|
2024-03-05 23:32:40 +08:00
|
|
|
if (opt->positional) {
|
2024-03-13 23:58:52 +08:00
|
|
|
optstr = g_strdup_printf(_("[--%1$s] <string>..."), opt->name);
|
2024-03-05 23:32:40 +08:00
|
|
|
} else {
|
2024-03-13 23:58:52 +08:00
|
|
|
optstr = g_strdup_printf(_("--%1$s <string>..."), opt->name);
|
2024-03-01 21:51:46 +08:00
|
|
|
}
|
2016-09-15 23:52:11 +08:00
|
|
|
break;
|
2024-03-01 21:51:46 +08:00
|
|
|
|
2016-09-15 23:52:11 +08:00
|
|
|
case VSH_OT_ALIAS:
|
2024-03-04 22:40:40 +08:00
|
|
|
case VSH_OT_NONE:
|
2016-09-15 23:52:11 +08:00
|
|
|
continue;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
2016-09-15 23:52:11 +08:00
|
|
|
|
2024-03-01 21:51:46 +08:00
|
|
|
fprintf(stdout, " %-15s %s\n", optstr, _(opt->help));
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
}
|
2016-09-15 23:52:11 +08:00
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------
|
|
|
|
* Utils for work with runtime commands data
|
|
|
|
* ---------------
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
vshCommandOptFree(vshCmdOpt * arg)
|
|
|
|
{
|
|
|
|
vshCmdOpt *a = arg;
|
|
|
|
|
|
|
|
while (a) {
|
|
|
|
vshCmdOpt *tmp = a;
|
|
|
|
|
|
|
|
a = a->next;
|
|
|
|
|
2021-02-04 03:32:55 +08:00
|
|
|
g_free(tmp->data);
|
|
|
|
g_free(tmp);
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vshCommandFree(vshCmd *cmd)
|
|
|
|
{
|
|
|
|
vshCmd *c = cmd;
|
|
|
|
|
|
|
|
while (c) {
|
|
|
|
vshCmd *tmp = c;
|
|
|
|
|
|
|
|
c = c->next;
|
|
|
|
|
2017-12-29 19:19:47 +08:00
|
|
|
vshCommandOptFree(tmp->opts);
|
2021-02-04 03:32:55 +08:00
|
|
|
g_free(tmp);
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 17:23:32 +08:00
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC(vshCmd, vshCommandFree);
|
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
/**
|
|
|
|
* vshCommandOpt:
|
|
|
|
* @cmd: parsed command line to search
|
|
|
|
* @name: option name to search for
|
|
|
|
* @opt: result of the search
|
|
|
|
* @needData: true if option must be non-boolean
|
|
|
|
*
|
|
|
|
* Look up an option passed to CMD by NAME. Returns 1 with *OPT set
|
|
|
|
* to the option if found, 0 with *OPT set to NULL if the name is
|
|
|
|
* valid and the option is not required, -1 with *OPT set to NULL if
|
|
|
|
* the option is required but not present, and assert if NAME is not
|
2017-11-10 01:06:11 +08:00
|
|
|
* valid (which indicates a programming error) unless cmd->skipChecks
|
|
|
|
* is set. No error messages are issued if a value is returned.
|
2015-06-16 00:53:58 +08:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
vshCommandOpt(const vshCmd *cmd, const char *name, vshCmdOpt **opt,
|
|
|
|
bool needData)
|
|
|
|
{
|
|
|
|
vshCmdOpt *candidate = cmd->opts;
|
|
|
|
const vshCmdOptDef *valid = cmd->def->opts;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* See if option is valid and/or required. */
|
|
|
|
*opt = NULL;
|
2017-11-10 01:06:11 +08:00
|
|
|
|
2018-02-28 17:09:56 +08:00
|
|
|
while (valid && valid->name) {
|
|
|
|
if (STREQ(name, valid->name))
|
|
|
|
break;
|
|
|
|
valid++;
|
|
|
|
}
|
2017-11-10 01:06:11 +08:00
|
|
|
|
2018-02-28 17:09:56 +08:00
|
|
|
if (!cmd->skipChecks)
|
2017-11-10 01:06:11 +08:00
|
|
|
assert(valid && (!needData || valid->type != VSH_OT_BOOL));
|
|
|
|
|
2024-03-06 00:21:05 +08:00
|
|
|
if (valid && valid->required)
|
2018-02-28 17:09:56 +08:00
|
|
|
ret = -1;
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
/* See if option is present on command line. */
|
|
|
|
while (candidate) {
|
|
|
|
if (STREQ(candidate->def->name, name)) {
|
|
|
|
*opt = candidate;
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
candidate = candidate->next;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vshCommandOptInt:
|
|
|
|
* @ctl virtshell control structure
|
|
|
|
* @cmd command reference
|
|
|
|
* @name option name
|
|
|
|
* @value result
|
|
|
|
*
|
|
|
|
* Convert option to int.
|
|
|
|
* On error, a message is displayed.
|
|
|
|
*
|
|
|
|
* Return value:
|
|
|
|
* >0 if option found and valid (@value updated)
|
|
|
|
* 0 if option not found and not required (@value untouched)
|
|
|
|
* <0 in all other cases (@value untouched)
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vshCommandOptInt(vshControl *ctl, const vshCmd *cmd,
|
|
|
|
const char *name, int *value)
|
|
|
|
{
|
|
|
|
vshCmdOpt *arg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = vshCommandOpt(cmd, name, &arg, true)) <= 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if ((ret = virStrToLong_i(arg->data, NULL, 10, value)) < 0)
|
|
|
|
vshError(ctl,
|
2023-03-09 22:54:58 +08:00
|
|
|
_("Numeric value '%1$s' for <%2$s> option is malformed or out of range"),
|
2015-06-16 00:53:58 +08:00
|
|
|
arg->data, name);
|
|
|
|
else
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vshCommandOptUIntInternal(vshControl *ctl,
|
|
|
|
const vshCmd *cmd,
|
|
|
|
const char *name,
|
|
|
|
unsigned int *value,
|
|
|
|
bool wrap)
|
|
|
|
{
|
|
|
|
vshCmdOpt *arg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = vshCommandOpt(cmd, name, &arg, true)) <= 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (wrap)
|
|
|
|
ret = virStrToLong_ui(arg->data, NULL, 10, value);
|
|
|
|
else
|
|
|
|
ret = virStrToLong_uip(arg->data, NULL, 10, value);
|
|
|
|
if (ret < 0)
|
|
|
|
vshError(ctl,
|
2023-03-09 22:54:58 +08:00
|
|
|
_("Numeric value '%1$s' for <%2$s> option is malformed or out of range"),
|
2015-06-16 00:53:58 +08:00
|
|
|
arg->data, name);
|
|
|
|
else
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vshCommandOptUInt:
|
|
|
|
* @ctl virtshell control structure
|
|
|
|
* @cmd command reference
|
|
|
|
* @name option name
|
|
|
|
* @value result
|
|
|
|
*
|
|
|
|
* Convert option to unsigned int, reject negative numbers
|
|
|
|
* See vshCommandOptInt()
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vshCommandOptUInt(vshControl *ctl, const vshCmd *cmd,
|
|
|
|
const char *name, unsigned int *value)
|
|
|
|
{
|
|
|
|
return vshCommandOptUIntInternal(ctl, cmd, name, value, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vshCommandOptUIntWrap:
|
|
|
|
* @ctl virtshell control structure
|
|
|
|
* @cmd command reference
|
|
|
|
* @name option name
|
|
|
|
* @value result
|
|
|
|
*
|
|
|
|
* Convert option to unsigned int, wraps negative numbers to positive
|
|
|
|
* See vshCommandOptInt()
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vshCommandOptUIntWrap(vshControl *ctl, const vshCmd *cmd,
|
|
|
|
const char *name, unsigned int *value)
|
|
|
|
{
|
|
|
|
return vshCommandOptUIntInternal(ctl, cmd, name, value, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vshCommandOptULInternal(vshControl *ctl,
|
|
|
|
const vshCmd *cmd,
|
|
|
|
const char *name,
|
|
|
|
unsigned long *value,
|
|
|
|
bool wrap)
|
|
|
|
{
|
|
|
|
vshCmdOpt *arg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = vshCommandOpt(cmd, name, &arg, true)) <= 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (wrap)
|
|
|
|
ret = virStrToLong_ul(arg->data, NULL, 10, value);
|
|
|
|
else
|
|
|
|
ret = virStrToLong_ulp(arg->data, NULL, 10, value);
|
|
|
|
if (ret < 0)
|
|
|
|
vshError(ctl,
|
2023-03-09 22:54:58 +08:00
|
|
|
_("Numeric value '%1$s' for <%2$s> option is malformed or out of range"),
|
2015-06-16 00:53:58 +08:00
|
|
|
arg->data, name);
|
|
|
|
else
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* vshCommandOptUL:
|
|
|
|
* @ctl virtshell control structure
|
|
|
|
* @cmd command reference
|
|
|
|
* @name option name
|
|
|
|
* @value result
|
|
|
|
*
|
|
|
|
* Convert option to unsigned long
|
|
|
|
* See vshCommandOptInt()
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vshCommandOptUL(vshControl *ctl, const vshCmd *cmd,
|
|
|
|
const char *name, unsigned long *value)
|
|
|
|
{
|
|
|
|
return vshCommandOptULInternal(ctl, cmd, name, value, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vshCommandOptULWrap:
|
|
|
|
* @ctl virtshell control structure
|
|
|
|
* @cmd command reference
|
|
|
|
* @name option name
|
|
|
|
* @value result
|
|
|
|
*
|
|
|
|
* Convert option to unsigned long, wraps negative numbers to positive
|
|
|
|
* See vshCommandOptInt()
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vshCommandOptULWrap(vshControl *ctl, const vshCmd *cmd,
|
|
|
|
const char *name, unsigned long *value)
|
|
|
|
{
|
|
|
|
return vshCommandOptULInternal(ctl, cmd, name, value, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-03 20:47:56 +08:00
|
|
|
* vshCommandOptStringQuiet:
|
2015-06-16 00:53:58 +08:00
|
|
|
* @ctl virtshell control structure
|
|
|
|
* @cmd command reference
|
|
|
|
* @name option name
|
|
|
|
* @value result
|
|
|
|
*
|
2015-12-03 20:47:56 +08:00
|
|
|
* Returns option as STRING. On error -1 is returned but no error is set.
|
2015-06-16 00:53:58 +08:00
|
|
|
* Return value:
|
|
|
|
* >0 if option found and valid (@value updated)
|
|
|
|
* 0 if option not found and not required (@value untouched)
|
|
|
|
* <0 in all other cases (@value untouched)
|
|
|
|
*/
|
|
|
|
int
|
2019-10-14 20:44:29 +08:00
|
|
|
vshCommandOptStringQuiet(vshControl *ctl G_GNUC_UNUSED, const vshCmd *cmd,
|
2015-12-03 20:47:56 +08:00
|
|
|
const char *name, const char **value)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
|
|
|
vshCmdOpt *arg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = vshCommandOpt(cmd, name, &arg, true)) <= 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (!*arg->data && !(arg->def->flags & VSH_OFLAG_EMPTY_OK))
|
|
|
|
return -1;
|
|
|
|
*value = arg->data;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vshCommandOptStringReq:
|
|
|
|
* @ctl virtshell control structure
|
|
|
|
* @cmd command structure
|
|
|
|
* @name option name
|
|
|
|
* @value result (updated to NULL or the option argument)
|
|
|
|
*
|
|
|
|
* Gets a option argument as string.
|
|
|
|
*
|
|
|
|
* Returns 0 on success or when the option is not present and not
|
|
|
|
* required, *value is set to the option argument. On error -1 is
|
|
|
|
* returned and error message printed.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vshCommandOptStringReq(vshControl *ctl,
|
|
|
|
const vshCmd *cmd,
|
|
|
|
const char *name,
|
|
|
|
const char **value)
|
|
|
|
{
|
|
|
|
vshCmdOpt *arg;
|
|
|
|
int ret;
|
|
|
|
const char *error = NULL;
|
|
|
|
|
|
|
|
/* clear out the value */
|
|
|
|
*value = NULL;
|
|
|
|
|
|
|
|
ret = vshCommandOpt(cmd, name, &arg, true);
|
|
|
|
/* option is not required and not present */
|
|
|
|
if (ret == 0)
|
|
|
|
return 0;
|
|
|
|
/* this should not be propagated here, just to be sure */
|
|
|
|
if (ret == -1)
|
|
|
|
error = N_("Mandatory option not present");
|
2020-08-13 22:03:45 +08:00
|
|
|
else if (arg && !*arg->data && !(arg->def->flags & VSH_OFLAG_EMPTY_OK))
|
2015-06-16 00:53:58 +08:00
|
|
|
error = N_("Option argument is empty");
|
|
|
|
|
|
|
|
if (error) {
|
2018-02-28 17:09:56 +08:00
|
|
|
if (!cmd->skipChecks)
|
2023-03-09 22:54:58 +08:00
|
|
|
vshError(ctl, _("Failed to get option '%1$s': %2$s"), name, _(error));
|
2015-06-16 00:53:58 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*value = arg->data;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vshCommandOptLongLong:
|
|
|
|
* @ctl virtshell control structure
|
|
|
|
* @cmd command reference
|
|
|
|
* @name option name
|
|
|
|
* @value result
|
|
|
|
*
|
|
|
|
* Returns option as long long
|
|
|
|
* See vshCommandOptInt()
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vshCommandOptLongLong(vshControl *ctl, const vshCmd *cmd,
|
|
|
|
const char *name, long long *value)
|
|
|
|
{
|
|
|
|
vshCmdOpt *arg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = vshCommandOpt(cmd, name, &arg, true)) <= 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if ((ret = virStrToLong_ll(arg->data, NULL, 10, value)) < 0)
|
|
|
|
vshError(ctl,
|
2023-03-09 22:54:58 +08:00
|
|
|
_("Numeric value '%1$s' for <%2$s> option is malformed or out of range"),
|
2015-06-16 00:53:58 +08:00
|
|
|
arg->data, name);
|
|
|
|
else
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vshCommandOptULongLongInternal(vshControl *ctl,
|
|
|
|
const vshCmd *cmd,
|
|
|
|
const char *name,
|
|
|
|
unsigned long long *value,
|
|
|
|
bool wrap)
|
|
|
|
{
|
|
|
|
vshCmdOpt *arg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = vshCommandOpt(cmd, name, &arg, true)) <= 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (wrap)
|
|
|
|
ret = virStrToLong_ull(arg->data, NULL, 10, value);
|
|
|
|
else
|
|
|
|
ret = virStrToLong_ullp(arg->data, NULL, 10, value);
|
|
|
|
if (ret < 0)
|
|
|
|
vshError(ctl,
|
2023-03-09 22:54:58 +08:00
|
|
|
_("Numeric value '%1$s' for <%2$s> option is malformed or out of range"),
|
2015-06-16 00:53:58 +08:00
|
|
|
arg->data, name);
|
|
|
|
else
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vshCommandOptULongLong:
|
|
|
|
* @ctl virtshell control structure
|
|
|
|
* @cmd command reference
|
|
|
|
* @name option name
|
|
|
|
* @value result
|
|
|
|
*
|
|
|
|
* Returns option as long long, rejects negative numbers
|
|
|
|
* See vshCommandOptInt()
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vshCommandOptULongLong(vshControl *ctl, const vshCmd *cmd,
|
|
|
|
const char *name, unsigned long long *value)
|
|
|
|
{
|
|
|
|
return vshCommandOptULongLongInternal(ctl, cmd, name, value, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vshCommandOptULongLongWrap:
|
|
|
|
* @ctl virtshell control structure
|
|
|
|
* @cmd command reference
|
|
|
|
* @name option name
|
|
|
|
* @value result
|
|
|
|
*
|
|
|
|
* Returns option as long long, wraps negative numbers to positive
|
|
|
|
* See vshCommandOptInt()
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vshCommandOptULongLongWrap(vshControl *ctl, const vshCmd *cmd,
|
|
|
|
const char *name, unsigned long long *value)
|
|
|
|
{
|
|
|
|
return vshCommandOptULongLongInternal(ctl, cmd, name, value, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vshCommandOptScaledInt:
|
|
|
|
* @ctl virtshell control structure
|
|
|
|
* @cmd command reference
|
|
|
|
* @name option name
|
|
|
|
* @value result
|
|
|
|
* @scale default of 1 or 1024, if no suffix is present
|
|
|
|
* @max maximum value permitted
|
|
|
|
*
|
|
|
|
* Returns option as long long, scaled according to suffix
|
|
|
|
* See vshCommandOptInt()
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vshCommandOptScaledInt(vshControl *ctl, const vshCmd *cmd,
|
|
|
|
const char *name, unsigned long long *value,
|
|
|
|
int scale, unsigned long long max)
|
|
|
|
{
|
|
|
|
vshCmdOpt *arg;
|
|
|
|
char *end;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = vshCommandOpt(cmd, name, &arg, true)) <= 0)
|
|
|
|
return ret;
|
2016-03-17 22:45:09 +08:00
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
if (virStrToLong_ullp(arg->data, &end, 10, value) < 0 ||
|
2016-03-17 22:45:09 +08:00
|
|
|
virScaleInteger(value, end, scale, max) < 0) {
|
2015-06-16 00:53:58 +08:00
|
|
|
vshError(ctl,
|
2023-03-09 22:54:58 +08:00
|
|
|
_("Scaled numeric value '%1$s' for <%2$s> option is malformed or out of range"),
|
|
|
|
arg->data, name);
|
2016-03-17 22:45:09 +08:00
|
|
|
return -1;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
2016-03-17 22:45:09 +08:00
|
|
|
return 1;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vshCommandOptBool:
|
|
|
|
* @cmd command reference
|
|
|
|
* @name option name
|
|
|
|
*
|
|
|
|
* Returns true/false if the option exists. Note that this does NOT
|
|
|
|
* validate whether the option is actually boolean, or even whether
|
|
|
|
* name is legal; so that this can be used to probe whether a data
|
|
|
|
* option is present without actually using that data.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
vshCommandOptBool(const vshCmd *cmd, const char *name)
|
|
|
|
{
|
|
|
|
vshCmdOpt *dummy;
|
|
|
|
|
|
|
|
return vshCommandOpt(cmd, name, &dummy, false) == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vshCommandOptArgv:
|
|
|
|
* @ctl virtshell control structure
|
|
|
|
* @cmd command reference
|
|
|
|
* @opt starting point for the search
|
|
|
|
*
|
|
|
|
* Returns the next argv argument after OPT (or the first one if OPT
|
|
|
|
* is NULL), or NULL if no more are present.
|
|
|
|
*
|
|
|
|
* Requires that a VSH_OT_ARGV option be last in the
|
|
|
|
* list of supported options in CMD->def->opts.
|
|
|
|
*/
|
|
|
|
const vshCmdOpt *
|
2019-10-14 20:44:29 +08:00
|
|
|
vshCommandOptArgv(vshControl *ctl G_GNUC_UNUSED, const vshCmd *cmd,
|
2015-06-16 00:53:58 +08:00
|
|
|
const vshCmdOpt *opt)
|
|
|
|
{
|
|
|
|
opt = opt ? opt->next : cmd->opts;
|
|
|
|
|
|
|
|
while (opt) {
|
|
|
|
if (opt->def->type == VSH_OT_ARGV)
|
|
|
|
return opt;
|
|
|
|
opt = opt->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-29 21:47:40 +08:00
|
|
|
/**
|
|
|
|
* vshBlockJobOptionBandwidth:
|
|
|
|
* @ctl: virsh control data
|
|
|
|
* @cmd: virsh command description
|
|
|
|
* @bytes: return bandwidth in bytes/s instead of MiB/s
|
|
|
|
* @bandwidth: return value
|
|
|
|
*
|
|
|
|
* Extracts the value of --bandwidth either as a wrap-able number without scale
|
|
|
|
* or as a scaled integer. The returned value is checked to fit into a unsigned
|
|
|
|
* long data type. This is a legacy compatibility function and it should not
|
|
|
|
* be used for things other the block job APIs.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vshBlockJobOptionBandwidth(vshControl *ctl,
|
|
|
|
const vshCmd *cmd,
|
|
|
|
bool bytes,
|
|
|
|
unsigned long *bandwidth)
|
|
|
|
{
|
|
|
|
vshCmdOpt *arg;
|
|
|
|
char *end;
|
|
|
|
unsigned long long bw;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = vshCommandOpt(cmd, "bandwidth", &arg, true)) <= 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* due to historical reasons we declare to parse negative numbers and wrap
|
|
|
|
* them to the unsigned data type. */
|
|
|
|
if (virStrToLong_ul(arg->data, NULL, 10, bandwidth) < 0) {
|
|
|
|
/* try to parse the number as scaled size in this case we don't accept
|
|
|
|
* wrapping since it would be ridiculous. In case of a 32 bit host,
|
|
|
|
* limit the value to ULONG_MAX */
|
|
|
|
if (virStrToLong_ullp(arg->data, &end, 10, &bw) < 0 ||
|
|
|
|
virScaleInteger(&bw, end, 1, ULONG_MAX) < 0) {
|
|
|
|
vshError(ctl,
|
2023-03-09 22:54:58 +08:00
|
|
|
_("Scaled numeric value '%1$s' for <--bandwidth> option is malformed or out of range"),
|
|
|
|
arg->data);
|
2016-03-29 21:47:40 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bytes)
|
|
|
|
bw >>= 20;
|
|
|
|
|
|
|
|
*bandwidth = bw;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
/*
|
|
|
|
* Executes command(s) and returns return code from last command
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
vshCommandRun(vshControl *ctl, const vshCmd *cmd)
|
|
|
|
{
|
|
|
|
const vshClientHooks *hooks = ctl->hooks;
|
|
|
|
bool ret = true;
|
|
|
|
|
|
|
|
while (cmd) {
|
2019-12-21 00:19:30 +08:00
|
|
|
gint64 before, after;
|
2015-06-16 00:53:58 +08:00
|
|
|
bool enable_timing = ctl->timing;
|
|
|
|
|
2019-12-21 00:19:30 +08:00
|
|
|
before = g_get_real_time();
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
if ((cmd->def->flags & VSH_CMD_FLAG_NOCONNECT) ||
|
|
|
|
(hooks && hooks->connHandler && hooks->connHandler(ctl))) {
|
|
|
|
ret = cmd->def->handler(ctl, cmd);
|
|
|
|
} else {
|
|
|
|
/* connection is not usable, return error */
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
|
2019-12-21 00:19:30 +08:00
|
|
|
after = g_get_real_time();
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
/* try to automatically catch disconnections */
|
|
|
|
if (!ret &&
|
|
|
|
((last_error != NULL) &&
|
|
|
|
(((last_error->code == VIR_ERR_SYSTEM_ERROR) &&
|
|
|
|
(last_error->domain == VIR_FROM_REMOTE)) ||
|
|
|
|
(last_error->code == VIR_ERR_RPC) ||
|
|
|
|
(last_error->code == VIR_ERR_NO_CONNECT) ||
|
|
|
|
(last_error->code == VIR_ERR_INVALID_CONN))))
|
|
|
|
disconnected++;
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
vshReportError(ctl);
|
|
|
|
|
|
|
|
if (STREQ(cmd->def->name, "quit") ||
|
|
|
|
STREQ(cmd->def->name, "exit")) /* hack ... */
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (enable_timing) {
|
2019-12-21 00:19:30 +08:00
|
|
|
double diff_ms = (after - before) / 1000.0;
|
2015-06-16 00:53:58 +08:00
|
|
|
|
2023-03-09 22:54:58 +08:00
|
|
|
vshPrint(ctl, _("\n(Time: %1$.3f ms)\n\n"), diff_ms);
|
2015-06-16 00:53:58 +08:00
|
|
|
} else {
|
|
|
|
vshPrintExtra(ctl, "\n");
|
|
|
|
}
|
|
|
|
cmd = cmd->next;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------
|
|
|
|
* Command parsing
|
|
|
|
* ---------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
VSH_TK_ERROR, /* Failed to parse a token */
|
|
|
|
VSH_TK_ARG, /* Arbitrary argument, might be option or empty */
|
|
|
|
VSH_TK_SUBCMD_END, /* Separation between commands */
|
|
|
|
VSH_TK_END /* No more commands */
|
|
|
|
} vshCommandToken;
|
|
|
|
|
|
|
|
typedef struct _vshCommandParser vshCommandParser;
|
|
|
|
struct _vshCommandParser {
|
|
|
|
vshCommandToken(*getNextArg)(vshControl *, vshCommandParser *,
|
2016-07-08 20:56:04 +08:00
|
|
|
char **, bool);
|
2015-06-16 00:53:58 +08:00
|
|
|
/* vshCommandStringGetArg() */
|
|
|
|
char *pos;
|
2021-01-26 16:51:27 +08:00
|
|
|
const char *originalLine;
|
|
|
|
size_t point;
|
2015-06-16 00:53:58 +08:00
|
|
|
/* vshCommandArgvGetArg() */
|
|
|
|
char **arg_pos;
|
|
|
|
char **arg_end;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
2017-11-06 22:46:50 +08:00
|
|
|
vshCommandParse(vshControl *ctl, vshCommandParser *parser, vshCmd **partial)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
|
|
|
char *tkdata = NULL;
|
|
|
|
vshCmd *clast = NULL;
|
|
|
|
vshCmdOpt *first = NULL;
|
2017-11-06 22:46:50 +08:00
|
|
|
const vshCmdDef *cmd = NULL;
|
2015-06-16 00:53:58 +08:00
|
|
|
|
2017-11-06 22:46:50 +08:00
|
|
|
if (!partial) {
|
2022-01-29 01:42:45 +08:00
|
|
|
g_clear_pointer(&ctl->cmd, vshCommandFree);
|
2017-11-06 22:46:50 +08:00
|
|
|
}
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
vshCmdOpt *last = NULL;
|
|
|
|
vshCommandToken tk;
|
|
|
|
bool data_only = false;
|
2016-03-31 20:41:52 +08:00
|
|
|
uint64_t opts_need_arg = 0;
|
|
|
|
uint64_t opts_required = 0;
|
|
|
|
uint64_t opts_seen = 0;
|
2015-06-16 00:53:58 +08:00
|
|
|
|
2017-11-06 22:46:50 +08:00
|
|
|
cmd = NULL;
|
2015-06-16 00:53:58 +08:00
|
|
|
first = NULL;
|
|
|
|
|
2017-11-06 22:46:50 +08:00
|
|
|
if (partial) {
|
2022-01-29 01:42:45 +08:00
|
|
|
g_clear_pointer(partial, vshCommandFree);
|
2017-11-06 22:46:50 +08:00
|
|
|
}
|
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
while (1) {
|
|
|
|
const vshCmdOptDef *opt = NULL;
|
|
|
|
|
|
|
|
tkdata = NULL;
|
2016-07-08 20:56:04 +08:00
|
|
|
tk = parser->getNextArg(ctl, parser, &tkdata, true);
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
if (tk == VSH_TK_ERROR)
|
|
|
|
goto syntaxError;
|
|
|
|
if (tk != VSH_TK_ARG) {
|
|
|
|
VIR_FREE(tkdata);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd == NULL) {
|
2019-03-23 03:02:39 +08:00
|
|
|
/* first token must be command name or comment */
|
|
|
|
if (*tkdata == '#') {
|
|
|
|
do {
|
|
|
|
VIR_FREE(tkdata);
|
|
|
|
tk = parser->getNextArg(ctl, parser, &tkdata, false);
|
|
|
|
} while (tk == VSH_TK_ARG);
|
|
|
|
VIR_FREE(tkdata);
|
|
|
|
break;
|
|
|
|
} else if (!(cmd = vshCmddefSearch(tkdata))) {
|
2017-11-06 22:46:50 +08:00
|
|
|
if (!partial)
|
2023-03-09 22:54:58 +08:00
|
|
|
vshError(ctl, _("unknown command: '%1$s'"), tkdata);
|
2015-06-16 00:53:58 +08:00
|
|
|
goto syntaxError; /* ... or ignore this command only? */
|
|
|
|
}
|
2016-09-07 23:30:48 +08:00
|
|
|
|
|
|
|
/* aliases need to be resolved to the actual commands */
|
2024-03-02 06:14:51 +08:00
|
|
|
if (cmd->alias) {
|
2016-09-07 23:30:48 +08:00
|
|
|
VIR_FREE(tkdata);
|
2019-10-18 23:24:02 +08:00
|
|
|
tkdata = g_strdup(cmd->alias);
|
2024-03-04 22:51:28 +08:00
|
|
|
if (!(cmd = vshCmddefSearch(tkdata))) {
|
|
|
|
/* self-test ensures that the alias exists */
|
|
|
|
vshError(ctl, _("unknown command: '%1$s'"), tkdata);
|
|
|
|
goto syntaxError;
|
|
|
|
}
|
2016-09-07 23:30:48 +08:00
|
|
|
}
|
2020-11-12 20:49:21 +08:00
|
|
|
|
|
|
|
vshCmddefOptParse(cmd, &opts_need_arg, &opts_required);
|
2015-06-16 00:53:58 +08:00
|
|
|
VIR_FREE(tkdata);
|
|
|
|
} else if (data_only) {
|
|
|
|
goto get_data;
|
|
|
|
} else if (tkdata[0] == '-' && tkdata[1] == '-' &&
|
2019-11-18 22:14:06 +08:00
|
|
|
g_ascii_isalnum(tkdata[2])) {
|
2015-06-16 00:53:58 +08:00
|
|
|
char *optstr = strchr(tkdata + 2, '=');
|
2016-07-27 23:28:40 +08:00
|
|
|
size_t opt_index = 0;
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
if (optstr) {
|
|
|
|
*optstr = '\0'; /* convert the '=' to '\0' */
|
2019-10-18 23:24:02 +08:00
|
|
|
optstr = g_strdup(optstr + 1);
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
/* Special case 'help' to ignore all spurious options */
|
|
|
|
if (!(opt = vshCmddefGetOption(ctl, cmd, tkdata + 2,
|
|
|
|
&opts_seen, &opt_index,
|
2017-11-06 22:46:50 +08:00
|
|
|
&optstr, partial == NULL))) {
|
2015-06-16 00:53:58 +08:00
|
|
|
VIR_FREE(optstr);
|
|
|
|
if (STREQ(cmd->name, "help"))
|
|
|
|
continue;
|
|
|
|
goto syntaxError;
|
|
|
|
}
|
|
|
|
VIR_FREE(tkdata);
|
|
|
|
|
|
|
|
if (opt->type != VSH_OT_BOOL) {
|
|
|
|
/* option data */
|
|
|
|
if (optstr)
|
|
|
|
tkdata = optstr;
|
|
|
|
else
|
2021-01-08 01:09:11 +08:00
|
|
|
tk = parser->getNextArg(ctl, parser, &tkdata, partial == NULL);
|
2015-06-16 00:53:58 +08:00
|
|
|
if (tk == VSH_TK_ERROR)
|
|
|
|
goto syntaxError;
|
|
|
|
if (tk != VSH_TK_ARG) {
|
2017-11-06 22:46:50 +08:00
|
|
|
if (partial) {
|
2020-10-06 00:50:09 +08:00
|
|
|
vshCmdOpt *arg = g_new0(vshCmdOpt, 1);
|
2017-11-06 22:46:50 +08:00
|
|
|
arg->def = opt;
|
2021-03-24 17:32:58 +08:00
|
|
|
arg->data = g_steal_pointer(&tkdata);
|
2017-11-06 22:46:50 +08:00
|
|
|
arg->next = NULL;
|
2021-01-26 16:51:27 +08:00
|
|
|
|
|
|
|
if (parser->pos - parser->originalLine == parser->point - 1)
|
|
|
|
arg->completeThis = true;
|
|
|
|
|
2017-11-06 22:46:50 +08:00
|
|
|
if (!first)
|
|
|
|
first = arg;
|
|
|
|
if (last)
|
|
|
|
last->next = arg;
|
|
|
|
last = arg;
|
|
|
|
} else {
|
|
|
|
vshError(ctl,
|
2023-03-09 22:54:58 +08:00
|
|
|
_("expected syntax: --%1$s <%2$s>"),
|
2017-11-06 22:46:50 +08:00
|
|
|
opt->name,
|
|
|
|
opt->type ==
|
|
|
|
VSH_OT_INT ? _("number") : _("string"));
|
|
|
|
}
|
2015-06-16 00:53:58 +08:00
|
|
|
goto syntaxError;
|
|
|
|
}
|
|
|
|
if (opt->type != VSH_OT_ARGV)
|
2016-04-20 21:51:43 +08:00
|
|
|
opts_need_arg &= ~(1ULL << opt_index);
|
2015-06-16 00:53:58 +08:00
|
|
|
} else {
|
|
|
|
tkdata = NULL;
|
|
|
|
if (optstr) {
|
2017-11-06 22:46:50 +08:00
|
|
|
if (!partial)
|
2023-03-09 22:54:58 +08:00
|
|
|
vshError(ctl, _("invalid '=' after option --%1$s"),
|
2017-11-06 22:46:50 +08:00
|
|
|
opt->name);
|
2015-06-16 00:53:58 +08:00
|
|
|
VIR_FREE(optstr);
|
|
|
|
goto syntaxError;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (tkdata[0] == '-' && tkdata[1] == '-' &&
|
|
|
|
tkdata[2] == '\0') {
|
2017-11-13 21:46:36 +08:00
|
|
|
VIR_FREE(tkdata);
|
2015-06-16 00:53:58 +08:00
|
|
|
data_only = true;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
get_data:
|
|
|
|
/* Special case 'help' to ignore spurious data */
|
|
|
|
if (!(opt = vshCmddefGetData(cmd, &opts_need_arg,
|
|
|
|
&opts_seen)) &&
|
|
|
|
STRNEQ(cmd->name, "help")) {
|
2017-11-06 22:46:50 +08:00
|
|
|
if (!partial)
|
2023-03-09 22:54:58 +08:00
|
|
|
vshError(ctl, _("unexpected data '%1$s'"), tkdata);
|
2015-06-16 00:53:58 +08:00
|
|
|
goto syntaxError;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (opt) {
|
|
|
|
/* save option */
|
2020-10-06 00:50:09 +08:00
|
|
|
vshCmdOpt *arg = g_new0(vshCmdOpt, 1);
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
arg->def = opt;
|
2021-03-24 17:32:58 +08:00
|
|
|
arg->data = g_steal_pointer(&tkdata);
|
2015-06-16 00:53:58 +08:00
|
|
|
arg->next = NULL;
|
|
|
|
|
2021-01-26 16:51:27 +08:00
|
|
|
if (parser->pos - parser->originalLine == parser->point)
|
|
|
|
arg->completeThis = true;
|
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
if (!first)
|
|
|
|
first = arg;
|
|
|
|
if (last)
|
|
|
|
last->next = arg;
|
|
|
|
last = arg;
|
|
|
|
|
2017-11-06 22:46:50 +08:00
|
|
|
if (!partial)
|
|
|
|
vshDebug(ctl, VSH_ERR_INFO, "%s: %s(%s): %s\n",
|
|
|
|
cmd->name,
|
|
|
|
opt->name,
|
|
|
|
opt->type != VSH_OT_BOOL ? _("optdata") : _("bool"),
|
|
|
|
opt->type != VSH_OT_BOOL ? arg->data : _("(none)"));
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* command parsed -- allocate new struct for the command */
|
|
|
|
if (cmd) {
|
2020-10-06 00:50:09 +08:00
|
|
|
vshCmd *c = g_new0(vshCmd, 1);
|
2015-06-16 00:53:58 +08:00
|
|
|
vshCmdOpt *tmpopt = first;
|
|
|
|
|
|
|
|
/* if we encountered --help, replace parsed command with
|
|
|
|
* 'help <cmdname>' */
|
|
|
|
for (tmpopt = first; tmpopt; tmpopt = tmpopt->next) {
|
2016-10-10 21:01:18 +08:00
|
|
|
const vshCmdDef *help;
|
2015-06-16 00:53:58 +08:00
|
|
|
if (STRNEQ(tmpopt->def->name, "help"))
|
|
|
|
continue;
|
|
|
|
|
2024-03-04 22:51:28 +08:00
|
|
|
/* the self-test code ensures that help exists */
|
|
|
|
if (!(help = vshCmddefSearch("help")))
|
|
|
|
break;
|
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
vshCommandOptFree(first);
|
2020-10-06 00:50:09 +08:00
|
|
|
first = g_new0(vshCmdOpt, 1);
|
2015-06-16 00:53:58 +08:00
|
|
|
first->def = help->opts;
|
2019-10-18 23:24:02 +08:00
|
|
|
first->data = g_strdup(cmd->name);
|
2015-06-16 00:53:58 +08:00
|
|
|
first->next = NULL;
|
|
|
|
|
|
|
|
cmd = help;
|
|
|
|
opts_required = 0;
|
|
|
|
opts_seen = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-03-24 17:32:58 +08:00
|
|
|
c->opts = g_steal_pointer(&first);
|
2015-06-16 00:53:58 +08:00
|
|
|
c->def = cmd;
|
|
|
|
c->next = NULL;
|
|
|
|
|
2017-11-06 22:46:50 +08:00
|
|
|
if (!partial &&
|
|
|
|
vshCommandCheckOpts(ctl, c, opts_required, opts_seen) < 0) {
|
2021-04-06 20:23:31 +08:00
|
|
|
vshCommandFree(c);
|
2015-06-16 00:53:58 +08:00
|
|
|
goto syntaxError;
|
|
|
|
}
|
|
|
|
|
2017-11-06 22:46:50 +08:00
|
|
|
if (partial) {
|
|
|
|
vshCommandFree(*partial);
|
|
|
|
*partial = c;
|
|
|
|
} else {
|
|
|
|
if (!ctl->cmd)
|
|
|
|
ctl->cmd = c;
|
|
|
|
if (clast)
|
|
|
|
clast->next = c;
|
|
|
|
clast = c;
|
|
|
|
}
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tk == VSH_TK_END)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
syntaxError:
|
2017-11-06 22:46:50 +08:00
|
|
|
if (partial) {
|
|
|
|
vshCmd *tmp;
|
|
|
|
|
2020-10-06 00:50:09 +08:00
|
|
|
tmp = g_new0(vshCmd, 1);
|
2017-11-06 22:46:50 +08:00
|
|
|
tmp->opts = first;
|
|
|
|
tmp->def = cmd;
|
|
|
|
|
|
|
|
*partial = tmp;
|
|
|
|
} else {
|
2022-01-29 01:42:45 +08:00
|
|
|
g_clear_pointer(&ctl->cmd, vshCommandFree);
|
2017-11-06 22:46:50 +08:00
|
|
|
vshCommandOptFree(first);
|
|
|
|
}
|
2015-06-16 00:53:58 +08:00
|
|
|
VIR_FREE(tkdata);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------
|
|
|
|
* Command argv parsing
|
|
|
|
* --------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
static vshCommandToken ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
|
2019-10-19 06:12:53 +08:00
|
|
|
vshCommandArgvGetArg(vshControl *ctl G_GNUC_UNUSED,
|
2019-10-19 06:12:53 +08:00
|
|
|
vshCommandParser *parser,
|
|
|
|
char **res,
|
2019-10-14 20:44:29 +08:00
|
|
|
bool report G_GNUC_UNUSED)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
|
|
|
if (parser->arg_pos == parser->arg_end) {
|
|
|
|
*res = NULL;
|
|
|
|
return VSH_TK_END;
|
|
|
|
}
|
|
|
|
|
2019-10-19 06:12:53 +08:00
|
|
|
*res = g_strdup(*parser->arg_pos);
|
2015-06-16 00:53:58 +08:00
|
|
|
parser->arg_pos++;
|
|
|
|
return VSH_TK_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
vshCommandArgvParse(vshControl *ctl, int nargs, char **argv)
|
|
|
|
{
|
2021-01-26 16:51:27 +08:00
|
|
|
vshCommandParser parser = { 0 };
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
if (nargs <= 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
parser.arg_pos = argv;
|
|
|
|
parser.arg_end = argv + nargs;
|
|
|
|
parser.getNextArg = vshCommandArgvGetArg;
|
2017-11-06 22:46:50 +08:00
|
|
|
return vshCommandParse(ctl, &parser, NULL);
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------
|
|
|
|
* Command string parsing
|
|
|
|
* ----------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
static vshCommandToken ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
|
2016-07-08 20:56:04 +08:00
|
|
|
vshCommandStringGetArg(vshControl *ctl, vshCommandParser *parser, char **res,
|
|
|
|
bool report)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
|
|
|
bool single_quote = false;
|
|
|
|
bool double_quote = false;
|
|
|
|
char *p = parser->pos;
|
2019-10-18 23:24:02 +08:00
|
|
|
char *q = g_strdup(p);
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
*res = q;
|
|
|
|
|
2019-02-27 02:49:25 +08:00
|
|
|
while (*p == ' ' || *p == '\t' || (*p == '\\' && p[1] == '\n'))
|
|
|
|
p += 1 + (*p == '\\');
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
if (*p == '\0')
|
|
|
|
return VSH_TK_END;
|
2019-02-22 02:36:32 +08:00
|
|
|
if (*p == ';' || *p == '\n') {
|
2015-06-16 00:53:58 +08:00
|
|
|
parser->pos = ++p; /* = \0 or begin of next command */
|
|
|
|
return VSH_TK_SUBCMD_END;
|
|
|
|
}
|
2019-03-23 03:02:39 +08:00
|
|
|
if (*p == '#') { /* Argument starting with # is comment to end of line */
|
|
|
|
while (*p && *p != '\n')
|
|
|
|
p++;
|
|
|
|
parser->pos = p + !!*p;
|
|
|
|
return VSH_TK_SUBCMD_END;
|
|
|
|
}
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
while (*p) {
|
|
|
|
/* end of token is blank space or ';' */
|
|
|
|
if (!double_quote && !single_quote &&
|
2019-02-22 02:36:32 +08:00
|
|
|
(*p == ' ' || *p == '\t' || *p == ';' || *p == '\n'))
|
2015-06-16 00:53:58 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (!double_quote && *p == '\'') { /* single quote */
|
|
|
|
single_quote = !single_quote;
|
|
|
|
p++;
|
|
|
|
continue;
|
|
|
|
} else if (!single_quote && *p == '\\') { /* escape */
|
|
|
|
/*
|
2019-02-27 02:49:25 +08:00
|
|
|
* The same as in shell, a \ in "" is an escaper,
|
2015-06-16 00:53:58 +08:00
|
|
|
* but a \ in '' is not an escaper.
|
|
|
|
*/
|
|
|
|
p++;
|
|
|
|
if (*p == '\0') {
|
2016-07-08 20:56:04 +08:00
|
|
|
if (report)
|
|
|
|
vshError(ctl, "%s", _("dangling \\"));
|
2015-06-16 00:53:58 +08:00
|
|
|
return VSH_TK_ERROR;
|
2019-02-27 02:49:25 +08:00
|
|
|
} else if (*p == '\n') {
|
|
|
|
/* Elide backslash-newline entirely */
|
|
|
|
p++;
|
|
|
|
continue;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
} else if (!single_quote && *p == '"') { /* double quote */
|
|
|
|
double_quote = !double_quote;
|
|
|
|
p++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
*q++ = *p++;
|
|
|
|
}
|
2021-01-08 01:09:11 +08:00
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
if (double_quote) {
|
2021-01-08 01:09:11 +08:00
|
|
|
/* We have seen a double quote, but not it's companion
|
|
|
|
* ending. It's valid though, in case when we're called
|
|
|
|
* from completer (report = false), but it's not valid
|
|
|
|
* when parsing real command (report= true). */
|
|
|
|
if (report) {
|
2016-07-08 20:56:04 +08:00
|
|
|
vshError(ctl, "%s", _("missing \""));
|
2021-01-08 01:09:11 +08:00
|
|
|
return VSH_TK_ERROR;
|
|
|
|
}
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*q = '\0';
|
|
|
|
parser->pos = p;
|
|
|
|
return VSH_TK_ARG;
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:51:27 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* vshCommandStringParse:
|
|
|
|
* @ctl virsh control structure
|
|
|
|
* @cmdstr: string to parse
|
|
|
|
* @partial: store partially parsed command here
|
|
|
|
* @point: position of cursor (rl_point)
|
|
|
|
*
|
|
|
|
* Parse given string @cmdstr as a command and store it under
|
|
|
|
* @ctl->cmd. For readline completion, if @partial is not NULL on
|
|
|
|
* the input then errors in parsing are ignored (because user is
|
|
|
|
* still in progress of writing the command string) and partially
|
|
|
|
* parsed command is stored at *@partial (caller has to free it
|
|
|
|
* afterwards). Among with @partial, caller must set @point which
|
|
|
|
* is the position of cursor in @cmdstr (offset, numbered from 1).
|
|
|
|
* Parser will then set @completeThis attribute to true for the
|
|
|
|
* vshCmdOpt that appeared under the cursor.
|
|
|
|
*/
|
2015-06-16 00:53:58 +08:00
|
|
|
bool
|
2021-01-26 16:51:27 +08:00
|
|
|
vshCommandStringParse(vshControl *ctl,
|
|
|
|
char *cmdstr,
|
|
|
|
vshCmd **partial,
|
|
|
|
size_t point)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
2021-01-26 16:51:27 +08:00
|
|
|
vshCommandParser parser = { 0 };
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
if (cmdstr == NULL || *cmdstr == '\0')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
parser.pos = cmdstr;
|
2021-01-26 16:51:27 +08:00
|
|
|
parser.originalLine = cmdstr;
|
|
|
|
parser.point = point;
|
2015-06-16 00:53:58 +08:00
|
|
|
parser.getNextArg = vshCommandStringGetArg;
|
2017-11-06 22:46:50 +08:00
|
|
|
return vshCommandParse(ctl, &parser, partial);
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virshCommandOptTimeoutToMs:
|
|
|
|
* @ctl virsh control structure
|
|
|
|
* @cmd command reference
|
|
|
|
* @timeout result
|
|
|
|
*
|
|
|
|
* Parse an optional --timeout parameter in seconds, but store the
|
|
|
|
* value of the timeout in milliseconds.
|
|
|
|
* See vshCommandOptInt()
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vshCommandOptTimeoutToMs(vshControl *ctl, const vshCmd *cmd, int *timeout)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned int utimeout;
|
|
|
|
|
|
|
|
if ((ret = vshCommandOptUInt(ctl, cmd, "timeout", &utimeout)) <= 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Ensure that the timeout is not zero and that we can convert
|
|
|
|
* it from seconds to milliseconds without overflowing. */
|
|
|
|
if (utimeout == 0 || utimeout > INT_MAX / 1000) {
|
|
|
|
vshError(ctl,
|
2023-03-09 22:54:58 +08:00
|
|
|
_("Numeric value '%1$u' for <%2$s> option is malformed or out of range"),
|
2015-06-16 00:53:58 +08:00
|
|
|
utimeout,
|
|
|
|
"timeout");
|
|
|
|
ret = -1;
|
|
|
|
} else {
|
|
|
|
*timeout = ((int) utimeout) * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------------
|
|
|
|
* Misc utils
|
|
|
|
* ---------------
|
|
|
|
*/
|
|
|
|
|
2021-09-24 17:15:10 +08:00
|
|
|
/* Return a non-NULL string representation of a typed parameter; exit on
|
|
|
|
* unknown type. */
|
2015-06-16 00:53:58 +08:00
|
|
|
char *
|
|
|
|
vshGetTypedParamValue(vshControl *ctl, virTypedParameterPtr item)
|
|
|
|
{
|
|
|
|
switch (item->type) {
|
|
|
|
case VIR_TYPED_PARAM_INT:
|
2021-09-24 17:15:10 +08:00
|
|
|
return g_strdup_printf("%d", item->value.i);
|
2015-06-16 00:53:58 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VIR_TYPED_PARAM_UINT:
|
2021-09-24 17:15:10 +08:00
|
|
|
return g_strdup_printf("%u", item->value.ui);
|
2015-06-16 00:53:58 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VIR_TYPED_PARAM_LLONG:
|
2021-09-24 17:15:10 +08:00
|
|
|
return g_strdup_printf("%lld", item->value.l);
|
2015-06-16 00:53:58 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VIR_TYPED_PARAM_ULLONG:
|
2021-09-24 17:15:10 +08:00
|
|
|
return g_strdup_printf("%llu", item->value.ul);
|
2015-06-16 00:53:58 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VIR_TYPED_PARAM_DOUBLE:
|
2021-09-24 17:15:10 +08:00
|
|
|
return g_strdup_printf("%f", item->value.d);
|
2015-06-16 00:53:58 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VIR_TYPED_PARAM_BOOLEAN:
|
2021-09-24 17:15:10 +08:00
|
|
|
return g_strdup(item->value.b ? _("yes") : _("no"));
|
2015-06-16 00:53:58 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VIR_TYPED_PARAM_STRING:
|
2021-09-24 17:15:10 +08:00
|
|
|
return g_strdup(item->value.s);
|
2015-06-16 00:53:58 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-03-09 22:54:58 +08:00
|
|
|
vshError(ctl, _("unimplemented parameter type %1$d"), item->type);
|
2015-06-16 00:53:58 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
vshDebug(vshControl *ctl, int level, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2021-08-10 23:09:07 +08:00
|
|
|
g_autofree char *str = NULL;
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
/* Aligning log levels to that of libvirt.
|
|
|
|
* Traces with levels >= user-specified-level
|
|
|
|
* gets logged into file
|
|
|
|
*/
|
|
|
|
if (level < ctl->debug)
|
|
|
|
return;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
vshOutputLogFile(ctl, level, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
va_start(ap, format);
|
2019-10-22 20:11:15 +08:00
|
|
|
str = g_strdup_vprintf(format, ap);
|
2015-06-16 00:53:58 +08:00
|
|
|
va_end(ap);
|
|
|
|
fputs(str, stdout);
|
2022-03-09 21:01:49 +08:00
|
|
|
fflush(stdout);
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
2023-04-13 15:19:27 +08:00
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
void
|
2023-04-13 15:19:27 +08:00
|
|
|
vshPrintVa(vshControl *ctl G_GNUC_UNUSED,
|
|
|
|
const char *format,
|
|
|
|
va_list ap)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
2021-08-10 23:09:07 +08:00
|
|
|
g_autofree char *str = NULL;
|
2015-06-16 00:53:58 +08:00
|
|
|
|
2023-04-13 15:19:27 +08:00
|
|
|
str = g_strdup_vprintf(format, ap);
|
|
|
|
fputs(str, stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
vshPrintExtra(vshControl *ctl,
|
|
|
|
const char *format,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
if (ctl && ctl->quiet)
|
|
|
|
return;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
2023-04-13 15:19:27 +08:00
|
|
|
vshPrintVa(ctl, format, ap);
|
2015-06-16 00:53:58 +08:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-12 21:09:02 +08:00
|
|
|
void
|
2023-04-13 15:19:27 +08:00
|
|
|
vshPrint(vshControl *ctl,
|
|
|
|
const char *format,
|
|
|
|
...)
|
2016-02-12 21:09:02 +08:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
2023-04-13 15:19:27 +08:00
|
|
|
vshPrintVa(ctl, format, ap);
|
2016-02-12 21:09:02 +08:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
bool
|
2019-10-14 20:44:29 +08:00
|
|
|
vshTTYIsInterruptCharacter(vshControl *ctl G_GNUC_UNUSED,
|
|
|
|
const char chr G_GNUC_UNUSED)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
|
|
|
#ifndef WIN32
|
|
|
|
if (ctl->istty &&
|
|
|
|
ctl->termattr.c_cc[VINTR] == chr)
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
vshTTYAvailable(vshControl *ctl)
|
|
|
|
{
|
|
|
|
return ctl->istty;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2019-10-14 20:44:29 +08:00
|
|
|
vshTTYDisableInterrupt(vshControl *ctl G_GNUC_UNUSED)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
|
|
|
#ifndef WIN32
|
|
|
|
struct termios termset = ctl->termattr;
|
|
|
|
|
|
|
|
if (!ctl->istty)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* check if we need to set the terminal */
|
|
|
|
if (termset.c_cc[VINTR] == _POSIX_VDISABLE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
termset.c_cc[VINTR] = _POSIX_VDISABLE;
|
|
|
|
termset.c_lflag &= ~ICANON;
|
|
|
|
|
|
|
|
if (tcsetattr(STDIN_FILENO, TCSANOW, &termset) < 0)
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2019-10-14 20:44:29 +08:00
|
|
|
vshTTYRestore(vshControl *ctl G_GNUC_UNUSED)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
|
|
|
#ifndef WIN32
|
|
|
|
if (!ctl->istty)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &ctl->termattr) < 0)
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2019-10-14 20:44:29 +08:00
|
|
|
vshTTYMakeRaw(vshControl *ctl G_GNUC_UNUSED,
|
|
|
|
bool report_errors G_GNUC_UNUSED)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
|
|
|
#ifndef WIN32
|
|
|
|
struct termios rawattr = ctl->termattr;
|
2020-02-27 01:57:34 +08:00
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
if (!ctl->istty) {
|
|
|
|
if (report_errors) {
|
|
|
|
vshError(ctl, "%s",
|
|
|
|
_("unable to make terminal raw: console isn't a tty"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cfmakeraw(&rawattr);
|
|
|
|
|
|
|
|
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &rawattr) < 0) {
|
|
|
|
if (report_errors)
|
2023-03-09 22:54:58 +08:00
|
|
|
vshError(ctl, _("unable to set tty attributes: %1$s"),
|
2020-02-27 01:57:34 +08:00
|
|
|
g_strerror(errno));
|
2015-06-16 00:53:58 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
vshError(vshControl *ctl, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2021-08-10 23:09:07 +08:00
|
|
|
g_autofree char *str = NULL;
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
if (ctl != NULL) {
|
|
|
|
va_start(ap, format);
|
|
|
|
vshOutputLogFile(ctl, VSH_ERR_ERROR, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Most output is to stdout, but if someone ran virsh 2>&1, then
|
|
|
|
* printing to stderr will not interleave correctly with stdout
|
|
|
|
* unless we flush between every transition between streams. */
|
|
|
|
fflush(stdout);
|
|
|
|
fputs(_("error: "), stderr);
|
|
|
|
|
|
|
|
va_start(ap, format);
|
2019-10-22 20:11:15 +08:00
|
|
|
str = g_strdup_vprintf(format, ap);
|
2015-06-16 00:53:58 +08:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
fprintf(stderr, "%s\n", NULLSTR(str));
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
vshEventLoop(void *opaque)
|
|
|
|
{
|
|
|
|
vshControl *ctl = opaque;
|
|
|
|
|
|
|
|
while (1) {
|
2022-02-08 21:59:30 +08:00
|
|
|
bool quit = false;
|
|
|
|
VIR_WITH_MUTEX_LOCK_GUARD(&ctl->lock) {
|
|
|
|
quit = ctl->quit;
|
|
|
|
}
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
if (quit)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (virEventRunDefaultImpl() < 0)
|
|
|
|
vshReportError(ctl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helpers for waiting for a libvirt event.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* We want to use SIGINT to cancel a wait; but as signal handlers
|
|
|
|
* don't have an opaque argument, we have to use static storage. */
|
2020-01-17 19:16:48 +08:00
|
|
|
#ifndef WIN32
|
2015-06-16 00:53:58 +08:00
|
|
|
static int vshEventFd = -1;
|
|
|
|
static struct sigaction vshEventOldAction;
|
|
|
|
|
|
|
|
|
|
|
|
/* Signal handler installed in vshEventStart, removed in vshEventCleanup. */
|
|
|
|
static void
|
2019-10-14 20:44:29 +08:00
|
|
|
vshEventInt(int sig G_GNUC_UNUSED,
|
|
|
|
siginfo_t *siginfo G_GNUC_UNUSED,
|
|
|
|
void *context G_GNUC_UNUSED)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
|
|
|
char reason = VSH_EVENT_INTERRUPT;
|
|
|
|
if (vshEventFd >= 0)
|
|
|
|
ignore_value(safewrite(vshEventFd, &reason, 1));
|
|
|
|
}
|
2020-01-17 19:16:48 +08:00
|
|
|
#endif /* !WIN32 */
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* Event loop handler used to limit length of waiting for any other event. */
|
|
|
|
void
|
2019-10-14 20:44:29 +08:00
|
|
|
vshEventTimeout(int timer G_GNUC_UNUSED,
|
2015-06-16 00:53:58 +08:00
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
vshControl *ctl = opaque;
|
|
|
|
char reason = VSH_EVENT_TIMEOUT;
|
|
|
|
|
|
|
|
if (ctl->eventPipe[1] >= 0)
|
|
|
|
ignore_value(safewrite(ctl->eventPipe[1], &reason, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vshEventStart:
|
|
|
|
* @ctl vsh command struct
|
|
|
|
* @timeout_ms max wait time in milliseconds, or 0 for indefinite
|
|
|
|
*
|
|
|
|
* Set up a wait for a libvirt event. The wait can be canceled by
|
|
|
|
* SIGINT or by calling vshEventDone() in your event handler. If
|
|
|
|
* @timeout_ms is positive, the wait will also end if the timeout
|
|
|
|
* expires. Call vshEventWait() to block the main thread (the event
|
|
|
|
* handler runs in the event loop thread). When done (including if
|
|
|
|
* there was an error registering for an event), use vshEventCleanup()
|
|
|
|
* to quit waiting. Returns 0 on success, -1 on failure. */
|
|
|
|
int
|
|
|
|
vshEventStart(vshControl *ctl, int timeout_ms)
|
|
|
|
{
|
2020-01-17 19:16:48 +08:00
|
|
|
#ifndef WIN32
|
2015-06-16 00:53:58 +08:00
|
|
|
struct sigaction action;
|
2020-01-17 19:16:48 +08:00
|
|
|
assert(vshEventFd == -1);
|
|
|
|
#endif /* !WIN32 */
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
assert(ctl->eventPipe[0] == -1 && ctl->eventPipe[1] == -1 &&
|
2020-01-17 19:16:48 +08:00
|
|
|
ctl->eventTimerId >= 0);
|
2020-01-24 23:22:12 +08:00
|
|
|
if (virPipe(ctl->eventPipe) < 0) {
|
|
|
|
vshSaveLibvirtError();
|
|
|
|
vshReportError(ctl);
|
2015-06-16 00:53:58 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2020-01-17 19:16:48 +08:00
|
|
|
|
|
|
|
#ifndef WIN32
|
2015-06-16 00:53:58 +08:00
|
|
|
vshEventFd = ctl->eventPipe[1];
|
|
|
|
|
|
|
|
action.sa_sigaction = vshEventInt;
|
|
|
|
action.sa_flags = SA_SIGINFO;
|
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
sigaction(SIGINT, &action, &vshEventOldAction);
|
2020-01-17 19:16:48 +08:00
|
|
|
#endif /* !WIN32 */
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
if (timeout_ms)
|
|
|
|
virEventUpdateTimeout(ctl->eventTimerId, timeout_ms);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vshEventDone:
|
|
|
|
* @ctl vsh command struct
|
|
|
|
*
|
|
|
|
* Call this from an event callback to let the main thread quit
|
|
|
|
* blocking on further events.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vshEventDone(vshControl *ctl)
|
|
|
|
{
|
|
|
|
char reason = VSH_EVENT_DONE;
|
|
|
|
|
|
|
|
if (ctl->eventPipe[1] >= 0)
|
|
|
|
ignore_value(safewrite(ctl->eventPipe[1], &reason, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vshEventWait:
|
|
|
|
* @ctl vsh command struct
|
|
|
|
*
|
|
|
|
* Call this in the main thread after calling vshEventStart() then
|
|
|
|
* registering for one or more events. This call will block until
|
|
|
|
* SIGINT, the timeout registered at the start, or until one of your
|
|
|
|
* event handlers calls vshEventDone(). Returns an enum VSH_EVENT_*
|
|
|
|
* stating how the wait concluded, or -1 on error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vshEventWait(vshControl *ctl)
|
|
|
|
{
|
|
|
|
char buf;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
assert(ctl->eventPipe[0] >= 0);
|
|
|
|
while ((rv = read(ctl->eventPipe[0], &buf, 1)) < 0 && errno == EINTR);
|
|
|
|
if (rv != 1) {
|
|
|
|
if (!rv)
|
|
|
|
errno = EPIPE;
|
2023-03-09 22:54:58 +08:00
|
|
|
vshError(ctl, _("failed to determine loop exit status: %1$s"),
|
2020-02-27 01:57:34 +08:00
|
|
|
g_strerror(errno));
|
2015-06-16 00:53:58 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vshEventCleanup:
|
|
|
|
* @ctl vsh control struct
|
|
|
|
*
|
|
|
|
* Call at the end of any function that has used vshEventStart(), to
|
|
|
|
* tear down any remaining SIGINT or timeout handlers.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vshEventCleanup(vshControl *ctl)
|
|
|
|
{
|
2020-01-17 19:16:48 +08:00
|
|
|
#ifndef WIN32
|
2015-06-16 00:53:58 +08:00
|
|
|
if (vshEventFd >= 0) {
|
|
|
|
sigaction(SIGINT, &vshEventOldAction, NULL);
|
|
|
|
vshEventFd = -1;
|
|
|
|
}
|
2020-01-17 19:16:48 +08:00
|
|
|
#endif /* !WIN32 */
|
2015-06-16 00:53:58 +08:00
|
|
|
VIR_FORCE_CLOSE(ctl->eventPipe[0]);
|
|
|
|
VIR_FORCE_CLOSE(ctl->eventPipe[1]);
|
|
|
|
virEventUpdateTimeout(ctl->eventTimerId, -1);
|
|
|
|
}
|
|
|
|
|
2020-01-24 23:22:41 +08:00
|
|
|
#ifdef O_SYNC
|
|
|
|
# define LOGFILE_FLAGS (O_WRONLY | O_APPEND | O_CREAT | O_SYNC)
|
|
|
|
#else
|
|
|
|
# define LOGFILE_FLAGS (O_WRONLY | O_APPEND | O_CREAT)
|
|
|
|
#endif
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* vshOpenLogFile:
|
|
|
|
*
|
|
|
|
* Open log file.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vshOpenLogFile(vshControl *ctl)
|
|
|
|
{
|
|
|
|
if (ctl->logfile == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ((ctl->log_fd = open(ctl->logfile, LOGFILE_FLAGS, FILE_MODE)) < 0) {
|
|
|
|
vshError(ctl, "%s",
|
|
|
|
_("failed to open the log file. check the log file path"));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vshOutputLogFile:
|
|
|
|
*
|
|
|
|
* Outputting an error to log file.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vshOutputLogFile(vshControl *ctl, int log_level, const char *msg_format,
|
|
|
|
va_list ap)
|
|
|
|
{
|
2020-07-03 07:40:16 +08:00
|
|
|
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
|
2021-08-10 23:09:07 +08:00
|
|
|
g_autofree char *str = NULL;
|
2015-06-16 00:53:58 +08:00
|
|
|
size_t len;
|
|
|
|
const char *lvl = "";
|
2020-01-09 22:07:15 +08:00
|
|
|
g_autoptr(GDateTime) now = g_date_time_new_now_local();
|
|
|
|
g_autofree gchar *nowstr = NULL;
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
if (ctl->log_fd == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create log format
|
|
|
|
*
|
|
|
|
* [YYYY.MM.DD HH:MM:SS SIGNATURE PID] LOG_LEVEL message
|
|
|
|
*/
|
2020-01-09 22:07:15 +08:00
|
|
|
nowstr = g_date_time_format(now, "%Y.%m.%d %H:%M:%S");
|
|
|
|
virBufferAsprintf(&buf, "[%s %s %d] ",
|
|
|
|
nowstr,
|
2015-06-16 00:53:58 +08:00
|
|
|
ctl->progname,
|
|
|
|
(int) getpid());
|
|
|
|
switch (log_level) {
|
|
|
|
case VSH_ERR_DEBUG:
|
|
|
|
lvl = LVL_DEBUG;
|
|
|
|
break;
|
|
|
|
case VSH_ERR_INFO:
|
|
|
|
lvl = LVL_INFO;
|
|
|
|
break;
|
|
|
|
case VSH_ERR_NOTICE:
|
|
|
|
lvl = LVL_INFO;
|
|
|
|
break;
|
|
|
|
case VSH_ERR_WARNING:
|
|
|
|
lvl = LVL_WARNING;
|
|
|
|
break;
|
|
|
|
case VSH_ERR_ERROR:
|
|
|
|
lvl = LVL_ERROR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
lvl = LVL_DEBUG;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
virBufferAsprintf(&buf, "%s ", lvl);
|
|
|
|
virBufferVasprintf(&buf, msg_format, ap);
|
2020-02-03 03:26:38 +08:00
|
|
|
virBufferTrim(&buf, "\n");
|
2015-06-16 00:53:58 +08:00
|
|
|
virBufferAddChar(&buf, '\n');
|
|
|
|
|
|
|
|
str = virBufferContentAndReset(&buf);
|
|
|
|
len = strlen(str);
|
|
|
|
|
|
|
|
/* write log */
|
|
|
|
if (safewrite(ctl->log_fd, str, len) < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
vshCloseLogFile(ctl);
|
|
|
|
vshError(ctl, "%s", _("failed to write the log file"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vshCloseLogFile:
|
|
|
|
*
|
|
|
|
* Close log file.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vshCloseLogFile(vshControl *ctl)
|
|
|
|
{
|
|
|
|
/* log file close */
|
|
|
|
if (VIR_CLOSE(ctl->log_fd) < 0) {
|
2023-03-09 22:54:58 +08:00
|
|
|
vshError(ctl, _("%1$s: failed to write log file: %2$s"),
|
2015-06-16 00:53:58 +08:00
|
|
|
ctl->logfile ? ctl->logfile : "?",
|
2020-02-27 01:57:34 +08:00
|
|
|
g_strerror(errno));
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
2021-08-10 23:09:30 +08:00
|
|
|
g_clear_pointer(&ctl->logfile, g_free);
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef WIN32
|
|
|
|
static void
|
|
|
|
vshPrintRaw(vshControl *ctl, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *key;
|
|
|
|
|
|
|
|
va_start(ap, ctl);
|
|
|
|
while ((key = va_arg(ap, char *)) != NULL)
|
|
|
|
vshPrint(ctl, "%s\r\n", key);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vshAskReedit:
|
|
|
|
* @msg: Question to ask user
|
|
|
|
*
|
|
|
|
* Ask user if he wants to return to previously
|
|
|
|
* edited file.
|
|
|
|
*
|
|
|
|
* Returns 'y' if he wants to
|
|
|
|
* 'n' if he doesn't want to
|
|
|
|
* 'i' if he wants to try defining it again while ignoring validation
|
|
|
|
* 'f' if he forcibly wants to
|
|
|
|
* -1 on error
|
|
|
|
* 0 otherwise
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vshAskReedit(vshControl *ctl, const char *msg, bool relax_avail)
|
|
|
|
{
|
|
|
|
int c = -1;
|
|
|
|
|
|
|
|
if (!isatty(STDIN_FILENO))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
vshReportError(ctl);
|
|
|
|
|
|
|
|
if (vshTTYMakeRaw(ctl, false) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
vshPrint(ctl, "\r%s %s %s: ", msg, _("Try again?"),
|
|
|
|
relax_avail ? "[y,n,i,f,?]" : "[y,n,f,?]");
|
2019-11-18 22:15:31 +08:00
|
|
|
c = g_ascii_tolower(getchar());
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
if (c == '?') {
|
|
|
|
vshPrintRaw(ctl,
|
|
|
|
"",
|
|
|
|
_("y - yes, start editor again"),
|
|
|
|
_("n - no, throw away my changes"),
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (relax_avail) {
|
|
|
|
vshPrintRaw(ctl,
|
2023-08-24 23:04:34 +08:00
|
|
|
_("i - turn off validation and try to redefine again"),
|
2015-06-16 00:53:58 +08:00
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
vshPrintRaw(ctl,
|
|
|
|
_("f - force, try to redefine again"),
|
|
|
|
_("? - print this help"),
|
|
|
|
NULL);
|
|
|
|
continue;
|
|
|
|
} else if (c == 'y' || c == 'n' || c == 'f' ||
|
|
|
|
(relax_avail && c == 'i')) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vshTTYRestore(ctl);
|
|
|
|
|
|
|
|
vshPrint(ctl, "\r\n");
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
#else /* WIN32 */
|
|
|
|
int
|
|
|
|
vshAskReedit(vshControl *ctl,
|
2019-10-14 20:44:29 +08:00
|
|
|
const char *msg G_GNUC_UNUSED,
|
|
|
|
bool relax_avail G_GNUC_UNUSED)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
2023-08-24 23:04:34 +08:00
|
|
|
vshDebug(ctl, VSH_ERR_WARNING, "%s", _("This function is not supported on WIN32 platform"));
|
2015-06-16 00:53:58 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* WIN32 */
|
|
|
|
|
|
|
|
|
2022-03-02 00:00:56 +08:00
|
|
|
void
|
|
|
|
vshEditUnlinkTempfile(char *file)
|
|
|
|
{
|
|
|
|
if (!file)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ignore_value(unlink(file));
|
|
|
|
g_free(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
/* Common code for the edit / net-edit / pool-edit functions which follow. */
|
|
|
|
char *
|
|
|
|
vshEditWriteToTempFile(vshControl *ctl, const char *doc)
|
|
|
|
{
|
2022-03-02 00:00:56 +08:00
|
|
|
g_autofree char *filename = NULL;
|
|
|
|
g_autoptr(vshTempFile) ret = NULL;
|
2015-06-16 00:53:58 +08:00
|
|
|
const char *tmpdir;
|
2021-09-26 18:54:10 +08:00
|
|
|
VIR_AUTOCLOSE fd = -1;
|
2015-06-16 00:53:58 +08:00
|
|
|
|
2019-08-01 20:35:56 +08:00
|
|
|
tmpdir = getenv("TMPDIR");
|
2022-03-02 00:00:56 +08:00
|
|
|
if (!tmpdir)
|
|
|
|
tmpdir = "/tmp";
|
|
|
|
filename = g_strdup_printf("%s/virshXXXXXX.xml", tmpdir);
|
|
|
|
fd = g_mkstemp_full(filename, O_RDWR | O_CLOEXEC, S_IRUSR | S_IWUSR);
|
2015-06-16 00:53:58 +08:00
|
|
|
if (fd == -1) {
|
2023-03-09 22:54:58 +08:00
|
|
|
vshError(ctl, _("g_mkstemp_full: failed to create temporary file: %1$s"),
|
2020-02-27 01:57:34 +08:00
|
|
|
g_strerror(errno));
|
2015-06-16 00:53:58 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-03-02 00:00:56 +08:00
|
|
|
ret = g_steal_pointer(&filename);
|
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
if (safewrite(fd, doc, strlen(doc)) == -1) {
|
2023-03-09 22:54:58 +08:00
|
|
|
vshError(ctl, _("write: %1$s: failed to write to temporary file: %2$s"),
|
2020-02-27 01:57:34 +08:00
|
|
|
ret, g_strerror(errno));
|
2015-06-16 00:53:58 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (VIR_CLOSE(fd) < 0) {
|
2023-03-09 22:54:58 +08:00
|
|
|
vshError(ctl, _("close: %1$s: failed to write or close temporary file: %2$s"),
|
2020-02-27 01:57:34 +08:00
|
|
|
ret, g_strerror(errno));
|
2015-06-16 00:53:58 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Temporary filename: caller frees. */
|
2021-08-12 17:45:03 +08:00
|
|
|
return g_steal_pointer(&ret);
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Characters permitted in $EDITOR environment variable and temp filename. */
|
|
|
|
#define ACCEPTED_CHARS \
|
|
|
|
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-/_.:@"
|
|
|
|
|
2020-07-24 22:41:06 +08:00
|
|
|
/* Hard-code default editor used as a fallback if not configured by
|
|
|
|
* VISUAL or EDITOR environment variables. */
|
|
|
|
#define DEFAULT_EDITOR "vi"
|
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
int
|
|
|
|
vshEditFile(vshControl *ctl, const char *filename)
|
|
|
|
{
|
|
|
|
const char *editor;
|
2021-08-10 23:09:07 +08:00
|
|
|
g_autoptr(virCommand) cmd = NULL;
|
2015-06-16 00:53:58 +08:00
|
|
|
int outfd = STDOUT_FILENO;
|
|
|
|
int errfd = STDERR_FILENO;
|
|
|
|
|
2019-08-01 20:35:56 +08:00
|
|
|
editor = getenv("VISUAL");
|
2015-06-16 00:53:58 +08:00
|
|
|
if (!editor)
|
2019-08-01 20:35:56 +08:00
|
|
|
editor = getenv("EDITOR");
|
2015-06-16 00:53:58 +08:00
|
|
|
if (!editor)
|
|
|
|
editor = DEFAULT_EDITOR;
|
|
|
|
|
|
|
|
/* Check that filename doesn't contain shell meta-characters, and
|
|
|
|
* if it does, refuse to run. Follow the Unix conventions for
|
|
|
|
* EDITOR: the user can intentionally specify command options, so
|
|
|
|
* we don't protect any shell metacharacters there. Lots more
|
|
|
|
* than virsh will misbehave if EDITOR has bogus contents (which
|
|
|
|
* is why sudo scrubs it by default). Conversely, if the editor
|
|
|
|
* is safe, we can run it directly rather than wasting a shell.
|
|
|
|
*/
|
|
|
|
if (strspn(editor, ACCEPTED_CHARS) != strlen(editor)) {
|
|
|
|
if (strspn(filename, ACCEPTED_CHARS) != strlen(filename)) {
|
|
|
|
vshError(ctl,
|
2023-03-09 22:54:58 +08:00
|
|
|
_("%1$s: temporary filename contains shell meta or other unacceptable characters (is $TMPDIR wrong?)"),
|
2015-06-16 00:53:58 +08:00
|
|
|
filename);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cmd = virCommandNewArgList("sh", "-c", NULL);
|
|
|
|
virCommandAddArgFormat(cmd, "%s %s", editor, filename);
|
|
|
|
} else {
|
|
|
|
cmd = virCommandNewArgList(editor, filename, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
virCommandSetInputFD(cmd, STDIN_FILENO);
|
|
|
|
virCommandSetOutputFD(cmd, &outfd);
|
|
|
|
virCommandSetErrorFD(cmd, &errfd);
|
|
|
|
if (virCommandRunAsync(cmd, NULL) < 0 ||
|
|
|
|
virCommandWait(cmd, NULL) < 0) {
|
|
|
|
vshReportError(ctl);
|
2021-08-10 23:12:02 +08:00
|
|
|
return -1;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
2021-08-10 23:12:02 +08:00
|
|
|
return 0;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
vshEditReadBackFile(vshControl *ctl, const char *filename)
|
|
|
|
{
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
if (virFileReadAll(filename, VSH_MAX_XML_FILE, &ret) == -1) {
|
|
|
|
vshError(ctl,
|
2023-03-09 22:54:58 +08:00
|
|
|
_("%1$s: failed to read temporary file: %2$s"),
|
2020-02-27 01:57:34 +08:00
|
|
|
filename, g_strerror(errno));
|
2015-06-16 00:53:58 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-02-28 23:31:35 +08:00
|
|
|
int
|
|
|
|
vshEditString(vshControl *ctl,
|
|
|
|
char **output,
|
|
|
|
const char *string)
|
|
|
|
{
|
|
|
|
g_autoptr(vshTempFile) tmp = NULL;
|
|
|
|
char *tmpstr;
|
|
|
|
|
|
|
|
/* Create and open the temporary file. */
|
|
|
|
if (!(tmp = vshEditWriteToTempFile(ctl, string)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Start the editor. */
|
|
|
|
if (vshEditFile(ctl, tmp) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Read back the edited file. */
|
|
|
|
if (!(*output = vshEditReadBackFile(ctl, tmp)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* strip a possible newline at the end of file; some
|
|
|
|
* editors enforce a newline, this makes editing
|
|
|
|
* more convenient */
|
|
|
|
if ((tmpstr = strrchr(*output, '\n')) &&
|
|
|
|
*(tmpstr+1) == '\0')
|
|
|
|
*tmpstr = '\0';
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
/* Tree listing helpers. */
|
|
|
|
|
|
|
|
static int
|
|
|
|
vshTreePrintInternal(vshControl *ctl,
|
|
|
|
vshTreeLookup lookup,
|
|
|
|
void *opaque,
|
|
|
|
int num_devices,
|
|
|
|
int devid,
|
|
|
|
int lastdev,
|
|
|
|
bool root,
|
2021-03-11 15:16:13 +08:00
|
|
|
virBuffer *indent)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
int nextlastdev = -1;
|
|
|
|
const char *dev = (lookup)(devid, false, opaque);
|
|
|
|
|
|
|
|
/* Print this device, with indent if not at root */
|
|
|
|
vshPrint(ctl, "%s%s%s\n", virBufferCurrentContent(indent),
|
|
|
|
root ? "" : "+- ", dev);
|
|
|
|
|
|
|
|
/* Update indent to show '|' or ' ' for child devices */
|
|
|
|
if (!root) {
|
|
|
|
virBufferAddChar(indent, devid == lastdev ? ' ' : '|');
|
|
|
|
virBufferAddChar(indent, ' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine the index of the last child device */
|
|
|
|
for (i = 0; i < num_devices; i++) {
|
|
|
|
const char *parent = (lookup)(i, true, opaque);
|
|
|
|
|
|
|
|
if (parent && STREQ(parent, dev))
|
|
|
|
nextlastdev = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If there is a child device, then print another blank line */
|
|
|
|
if (nextlastdev != -1)
|
|
|
|
vshPrint(ctl, "%s |\n", virBufferCurrentContent(indent));
|
|
|
|
|
|
|
|
/* Finally print all children */
|
|
|
|
virBufferAddLit(indent, " ");
|
|
|
|
for (i = 0; i < num_devices; i++) {
|
|
|
|
const char *parent = (lookup)(i, true, opaque);
|
|
|
|
|
|
|
|
if (parent && STREQ(parent, dev) &&
|
|
|
|
vshTreePrintInternal(ctl, lookup, opaque,
|
|
|
|
num_devices, i, nextlastdev,
|
|
|
|
false, indent) < 0)
|
2019-10-22 02:19:09 +08:00
|
|
|
return -1;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
2020-02-03 03:26:38 +08:00
|
|
|
virBufferTrim(indent, " ");
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
/* If there was no child device, and we're the last in
|
|
|
|
* a list of devices, then print another blank line */
|
|
|
|
if (nextlastdev == -1 && devid == lastdev)
|
|
|
|
vshPrint(ctl, "%s\n", virBufferCurrentContent(indent));
|
|
|
|
|
|
|
|
if (!root)
|
2020-02-03 03:17:20 +08:00
|
|
|
virBufferTrimLen(indent, 2);
|
2019-10-22 02:19:09 +08:00
|
|
|
|
|
|
|
return 0;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
vshTreePrint(vshControl *ctl, vshTreeLookup lookup, void *opaque,
|
|
|
|
int num_devices, int devid)
|
|
|
|
{
|
|
|
|
int ret;
|
2020-07-03 07:40:16 +08:00
|
|
|
g_auto(virBuffer) indent = VIR_BUFFER_INITIALIZER;
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
ret = vshTreePrintInternal(ctl, lookup, opaque, num_devices,
|
|
|
|
devid, devid, true, &indent);
|
|
|
|
if (ret < 0)
|
|
|
|
vshError(ctl, "%s", _("Failed to complete tree listing"));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-22 00:23:11 +08:00
|
|
|
/**
|
|
|
|
* vshReadlineCommandGenerator:
|
|
|
|
*
|
2021-09-17 16:58:28 +08:00
|
|
|
* Generator function for command completion. Used also for completing the
|
|
|
|
* '--command' option of the 'help' command.
|
2017-11-22 00:23:11 +08:00
|
|
|
*
|
2021-02-03 14:08:19 +08:00
|
|
|
* Returns a string list of all commands, or NULL on failure.
|
2015-06-16 00:53:58 +08:00
|
|
|
*/
|
2017-11-22 00:23:11 +08:00
|
|
|
static char **
|
2021-02-04 20:46:12 +08:00
|
|
|
vshReadlineCommandGenerator(void)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
2021-01-27 00:22:00 +08:00
|
|
|
size_t grp_list_index = 0;
|
2015-06-16 00:53:58 +08:00
|
|
|
const vshCmdGrp *grp;
|
2017-11-22 00:23:11 +08:00
|
|
|
size_t ret_size = 0;
|
2021-02-02 18:09:01 +08:00
|
|
|
g_auto(GStrv) ret = NULL;
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
grp = cmdGroups;
|
|
|
|
|
2021-01-27 00:22:00 +08:00
|
|
|
for (grp_list_index = 0; grp[grp_list_index].name; grp_list_index++) {
|
|
|
|
const vshCmdDef *cmds = grp[grp_list_index].commands;
|
|
|
|
size_t cmd_list_index;
|
2015-06-16 00:53:58 +08:00
|
|
|
|
2021-01-27 00:22:00 +08:00
|
|
|
for (cmd_list_index = 0; cmds[cmd_list_index].name; cmd_list_index++) {
|
|
|
|
const char *name = cmds[cmd_list_index].name;
|
|
|
|
|
2024-03-02 06:14:51 +08:00
|
|
|
if (cmds[cmd_list_index].alias ||
|
2021-09-16 21:57:28 +08:00
|
|
|
cmds[cmd_list_index].flags & VSH_CMD_FLAG_HIDDEN)
|
2021-01-27 00:22:00 +08:00
|
|
|
continue;
|
2015-06-16 00:53:58 +08:00
|
|
|
|
2021-03-20 07:37:05 +08:00
|
|
|
VIR_REALLOC_N(ret, ret_size + 2);
|
2021-02-02 18:09:01 +08:00
|
|
|
|
2021-01-27 00:22:00 +08:00
|
|
|
ret[ret_size] = g_strdup(name);
|
|
|
|
ret_size++;
|
|
|
|
/* Terminate the string list properly. */
|
|
|
|
ret[ret_size] = NULL;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:09:01 +08:00
|
|
|
return g_steal_pointer(&ret);
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
2021-09-17 16:58:28 +08:00
|
|
|
|
|
|
|
#if WITH_READLINE
|
|
|
|
|
|
|
|
/* -----------------
|
|
|
|
* Readline stuff
|
|
|
|
* -----------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2017-11-22 00:23:11 +08:00
|
|
|
static char **
|
2021-02-04 20:46:12 +08:00
|
|
|
vshReadlineOptionsGenerator(const vshCmdDef *cmd,
|
2018-01-13 00:05:33 +08:00
|
|
|
vshCmd *last)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
2017-11-22 00:23:11 +08:00
|
|
|
size_t list_index = 0;
|
|
|
|
size_t ret_size = 0;
|
2021-02-02 18:09:01 +08:00
|
|
|
g_auto(GStrv) ret = NULL;
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
if (!cmd)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!cmd->opts)
|
|
|
|
return NULL;
|
|
|
|
|
2021-02-02 21:55:28 +08:00
|
|
|
for (list_index = 0; cmd->opts[list_index].name; list_index++) {
|
|
|
|
const char *name = cmd->opts[list_index].name;
|
2018-01-13 00:05:33 +08:00
|
|
|
bool exists = false;
|
|
|
|
vshCmdOpt *opt = last->opts;
|
2015-06-16 00:53:58 +08:00
|
|
|
|
2021-02-02 21:55:28 +08:00
|
|
|
/* Skip aliases, we do not report them in help output either. */
|
|
|
|
if (cmd->opts[list_index].type == VSH_OT_ALIAS)
|
|
|
|
continue;
|
2015-06-16 00:53:58 +08:00
|
|
|
|
2018-01-13 00:05:33 +08:00
|
|
|
while (opt) {
|
2018-05-08 22:20:37 +08:00
|
|
|
if (STREQ(opt->def->name, name) && opt->def->type != VSH_OT_ARGV) {
|
2018-01-13 00:05:33 +08:00
|
|
|
exists = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
opt = opt->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exists)
|
|
|
|
continue;
|
|
|
|
|
2021-03-20 07:37:05 +08:00
|
|
|
VIR_REALLOC_N(ret, ret_size + 2);
|
2017-11-22 00:23:11 +08:00
|
|
|
|
2021-02-02 18:05:47 +08:00
|
|
|
ret[ret_size] = g_strdup_printf("--%s", name);
|
2017-11-22 00:23:11 +08:00
|
|
|
ret_size++;
|
|
|
|
/* Terminate the string list properly. */
|
|
|
|
ret[ret_size] = NULL;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
2021-02-02 18:09:01 +08:00
|
|
|
return g_steal_pointer(&ret);
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
2017-11-22 00:45:50 +08:00
|
|
|
|
|
|
|
static const vshCmdOptDef *
|
2021-01-26 16:51:27 +08:00
|
|
|
vshReadlineCommandFindOpt(const vshCmd *partial)
|
2017-11-22 00:45:50 +08:00
|
|
|
{
|
|
|
|
const vshCmd *tmp = partial;
|
|
|
|
|
2021-01-26 16:51:27 +08:00
|
|
|
while (tmp) {
|
2017-11-22 00:45:50 +08:00
|
|
|
const vshCmdOpt *opt = tmp->opts;
|
|
|
|
|
|
|
|
while (opt) {
|
2021-01-26 16:51:27 +08:00
|
|
|
if (opt->completeThis)
|
2017-11-22 00:45:50 +08:00
|
|
|
return opt->def;
|
|
|
|
|
|
|
|
opt = opt->next;
|
|
|
|
}
|
2021-01-26 16:51:27 +08:00
|
|
|
tmp = tmp->next;
|
2017-11-22 00:45:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-28 19:26:41 +08:00
|
|
|
static int
|
|
|
|
vshCompleterFilter(char ***list,
|
|
|
|
const char *text)
|
|
|
|
{
|
|
|
|
char **newList = NULL;
|
|
|
|
size_t newList_len = 0;
|
|
|
|
size_t list_len;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!list || !*list)
|
2021-02-02 17:39:19 +08:00
|
|
|
return 0;
|
2017-12-28 19:26:41 +08:00
|
|
|
|
2021-02-06 01:03:26 +08:00
|
|
|
list_len = g_strv_length(*list);
|
2020-09-11 19:42:16 +08:00
|
|
|
newList = g_new0(char *, list_len + 1);
|
2017-12-28 19:26:41 +08:00
|
|
|
|
|
|
|
for (i = 0; i < list_len; i++) {
|
|
|
|
if (!STRPREFIX((*list)[i], text)) {
|
2020-09-11 19:42:16 +08:00
|
|
|
g_clear_pointer(&(*list)[i], g_free);
|
2017-12-28 19:26:41 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-10-16 19:43:36 +08:00
|
|
|
newList[newList_len] = g_steal_pointer(&(*list)[i]);
|
2017-12-28 19:26:41 +08:00
|
|
|
newList_len++;
|
|
|
|
}
|
|
|
|
|
2020-09-11 19:42:16 +08:00
|
|
|
newList = g_renew(char *, newList, newList_len + 1);
|
|
|
|
g_free(*list);
|
2017-12-28 19:26:41 +08:00
|
|
|
*list = newList;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-08 20:56:05 +08:00
|
|
|
static char *
|
|
|
|
vshReadlineParse(const char *text, int state)
|
|
|
|
{
|
2017-11-22 00:23:11 +08:00
|
|
|
static char **list;
|
|
|
|
static size_t list_index;
|
|
|
|
char *ret = NULL;
|
2016-07-08 20:56:05 +08:00
|
|
|
|
2021-01-26 16:18:21 +08:00
|
|
|
/* Readline calls this function until NULL is returned. On
|
|
|
|
* the very first call @state is zero which means we should
|
|
|
|
* initialize those static variables above. On subsequent
|
|
|
|
* calls @state is non zero. */
|
2016-07-08 20:56:05 +08:00
|
|
|
if (!state) {
|
2021-01-26 17:23:32 +08:00
|
|
|
g_autoptr(vshCmd) partial = NULL;
|
2021-01-26 16:18:21 +08:00
|
|
|
const vshCmdDef *cmd = NULL;
|
|
|
|
const vshCmdOptDef *opt = NULL;
|
2021-01-26 16:23:10 +08:00
|
|
|
g_autofree char *line = g_strdup(rl_line_buffer);
|
2016-07-08 20:56:05 +08:00
|
|
|
|
2022-01-29 01:42:45 +08:00
|
|
|
g_clear_pointer(&list, g_strfreev);
|
2017-11-22 00:23:11 +08:00
|
|
|
list_index = 0;
|
2016-09-05 17:45:09 +08:00
|
|
|
|
2021-01-26 16:23:10 +08:00
|
|
|
*(line + rl_point) = '\0';
|
2016-07-08 20:56:05 +08:00
|
|
|
|
2021-01-26 16:51:27 +08:00
|
|
|
vshCommandStringParse(NULL, line, &partial, rl_point);
|
2016-07-08 20:56:05 +08:00
|
|
|
|
2017-11-22 00:45:50 +08:00
|
|
|
if (partial) {
|
2017-11-21 22:07:34 +08:00
|
|
|
cmd = partial->def;
|
2017-11-22 00:45:50 +08:00
|
|
|
partial->skipChecks = true;
|
|
|
|
}
|
2016-07-08 20:56:05 +08:00
|
|
|
|
2017-11-21 22:07:34 +08:00
|
|
|
if (cmd && STREQ(cmd->name, text)) {
|
|
|
|
/* Corner case - some commands share prefix (e.g.
|
|
|
|
* dump and dumpxml). If user typed 'dump<TAB><TAB>',
|
|
|
|
* then @text = "dump" and we want to offer command
|
|
|
|
* completion. If they typed 'dump <TAB><TAB>' then
|
|
|
|
* @text = "" (the space after the command) and we
|
|
|
|
* want to offer options completion for dump command.
|
|
|
|
*/
|
|
|
|
cmd = NULL;
|
2016-09-05 17:45:09 +08:00
|
|
|
}
|
2017-11-22 00:45:50 +08:00
|
|
|
|
2021-01-26 16:51:27 +08:00
|
|
|
opt = vshReadlineCommandFindOpt(partial);
|
2016-07-08 20:56:05 +08:00
|
|
|
|
2017-11-22 00:23:11 +08:00
|
|
|
if (!cmd) {
|
2021-02-04 20:46:12 +08:00
|
|
|
list = vshReadlineCommandGenerator();
|
2021-02-02 18:52:27 +08:00
|
|
|
} else if (!opt || opt->type == VSH_OT_BOOL) {
|
2021-02-04 20:46:12 +08:00
|
|
|
list = vshReadlineOptionsGenerator(cmd, partial);
|
2021-02-02 18:52:27 +08:00
|
|
|
} else if (opt && opt->completer) {
|
|
|
|
list = opt->completer(autoCompleteOpaque,
|
|
|
|
partial,
|
|
|
|
opt->completer_flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Escape completions, if needed (i.e. argument
|
|
|
|
* we are completing wasn't started with a quote
|
|
|
|
* character). This also enables filtering done
|
|
|
|
* below to work properly. */
|
|
|
|
if (list &&
|
|
|
|
!rl_completion_quote_character) {
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; list[i]; i++) {
|
|
|
|
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
|
2021-01-26 16:23:24 +08:00
|
|
|
|
2021-02-02 18:52:27 +08:00
|
|
|
virBufferEscape(&buf, '\\', " ", "%s", list[i]);
|
|
|
|
VIR_FREE(list[i]);
|
|
|
|
list[i] = virBufferContentAndReset(&buf);
|
2017-11-22 00:45:50 +08:00
|
|
|
}
|
2017-11-22 00:23:11 +08:00
|
|
|
}
|
2021-02-03 14:08:19 +08:00
|
|
|
|
|
|
|
/* For string list returned by completers we have to do
|
|
|
|
* filtering based on @text because completers returns all
|
|
|
|
* possible strings. */
|
|
|
|
if (vshCompleterFilter(&list, text) < 0)
|
|
|
|
goto cleanup;
|
2017-11-22 00:23:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (list) {
|
2019-10-18 23:24:02 +08:00
|
|
|
ret = g_strdup(list[list_index]);
|
2017-11-22 00:23:11 +08:00
|
|
|
list_index++;
|
2017-11-21 22:07:34 +08:00
|
|
|
}
|
|
|
|
|
2017-11-22 00:45:50 +08:00
|
|
|
cleanup:
|
2017-11-22 00:23:11 +08:00
|
|
|
if (!ret) {
|
2022-01-29 01:42:45 +08:00
|
|
|
g_clear_pointer(&list, g_strfreev);
|
2017-11-22 00:23:11 +08:00
|
|
|
list_index = 0;
|
2016-07-08 20:56:05 +08:00
|
|
|
}
|
|
|
|
|
2017-11-22 00:23:11 +08:00
|
|
|
return ret;
|
2016-07-08 20:56:05 +08:00
|
|
|
}
|
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
static char **
|
2016-12-09 17:52:29 +08:00
|
|
|
vshReadlineCompletion(const char *text,
|
2019-10-14 20:44:29 +08:00
|
|
|
int start G_GNUC_UNUSED,
|
|
|
|
int end G_GNUC_UNUSED)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
2019-10-17 16:10:10 +08:00
|
|
|
return rl_completion_matches(text, vshReadlineParse);
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
2017-11-13 20:34:54 +08:00
|
|
|
|
|
|
|
static int
|
|
|
|
vshReadlineCharIsQuoted(char *line, int idx)
|
|
|
|
{
|
|
|
|
return idx > 0 &&
|
|
|
|
line[idx - 1] == '\\' &&
|
|
|
|
!vshReadlineCharIsQuoted(line, idx - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
# define HISTSIZE_MAX 500000
|
|
|
|
|
|
|
|
static int
|
|
|
|
vshReadlineInit(vshControl *ctl)
|
|
|
|
{
|
2021-08-10 23:09:07 +08:00
|
|
|
g_autofree char *userdir = NULL;
|
2015-06-16 00:53:58 +08:00
|
|
|
int max_history = 500;
|
2021-08-10 23:09:07 +08:00
|
|
|
g_autofree char *histsize_env = NULL;
|
2015-06-16 00:53:58 +08:00
|
|
|
const char *histsize_str = NULL;
|
2021-01-08 00:58:41 +08:00
|
|
|
const char *break_characters = " \t\n`@$><=;|&{(";
|
2017-11-13 20:34:54 +08:00
|
|
|
const char *quote_characters = "\"'";
|
2015-06-16 00:53:58 +08:00
|
|
|
|
2016-09-06 20:04:37 +08:00
|
|
|
/* Opaque data for autocomplete callbacks. */
|
|
|
|
autoCompleteOpaque = ctl;
|
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
rl_readline_name = ctl->name;
|
|
|
|
|
|
|
|
/* Tell the completer that we want a crack first. */
|
|
|
|
rl_attempted_completion_function = vshReadlineCompletion;
|
|
|
|
|
2017-11-24 01:00:33 +08:00
|
|
|
rl_basic_word_break_characters = break_characters;
|
2016-07-08 20:56:05 +08:00
|
|
|
|
2017-11-13 20:34:54 +08:00
|
|
|
rl_completer_quote_characters = quote_characters;
|
|
|
|
rl_char_is_quoted_p = vshReadlineCharIsQuoted;
|
|
|
|
|
2019-10-22 21:26:14 +08:00
|
|
|
histsize_env = g_strdup_printf("%s_HISTSIZE", ctl->env_prefix);
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
/* Limit the total size of the history buffer */
|
2019-08-01 20:35:56 +08:00
|
|
|
if ((histsize_str = getenv(histsize_env))) {
|
2015-06-16 00:53:58 +08:00
|
|
|
if (virStrToLong_i(histsize_str, NULL, 10, &max_history) < 0) {
|
2023-03-09 22:54:58 +08:00
|
|
|
vshError(ctl, _("Bad $%1$s value."), histsize_env);
|
2021-08-10 23:12:02 +08:00
|
|
|
return -1;
|
2015-06-16 00:53:58 +08:00
|
|
|
} else if (max_history > HISTSIZE_MAX || max_history < 0) {
|
2023-03-09 22:54:58 +08:00
|
|
|
vshError(ctl, _("$%1$s value should be between 0 and %2$d"),
|
2015-06-16 00:53:58 +08:00
|
|
|
histsize_env, HISTSIZE_MAX);
|
2021-08-10 23:12:02 +08:00
|
|
|
return -1;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
stifle_history(max_history);
|
|
|
|
|
|
|
|
/* Prepare to read/write history from/to the
|
|
|
|
* $XDG_CACHE_HOME/virtshell/history file
|
|
|
|
*/
|
|
|
|
userdir = virGetUserCacheDirectory();
|
|
|
|
|
2019-10-22 21:26:14 +08:00
|
|
|
ctl->historydir = g_strdup_printf("%s/%s", userdir, ctl->name);
|
2015-06-16 00:53:58 +08:00
|
|
|
|
2019-10-22 21:26:14 +08:00
|
|
|
ctl->historyfile = g_strdup_printf("%s/history", ctl->historydir);
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
read_history(ctl->historyfile);
|
2021-08-10 23:12:02 +08:00
|
|
|
return 0;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vshReadlineDeinit(vshControl *ctl)
|
|
|
|
{
|
|
|
|
if (ctl->historyfile != NULL) {
|
2021-02-26 16:17:30 +08:00
|
|
|
if (g_mkdir_with_parents(ctl->historydir, 0755) < 0 &&
|
2015-06-16 00:53:58 +08:00
|
|
|
errno != EEXIST) {
|
2023-03-09 22:54:58 +08:00
|
|
|
vshError(ctl, _("Failed to create '%1$s': %2$s"),
|
2020-02-27 01:57:34 +08:00
|
|
|
ctl->historydir, g_strerror(errno));
|
2015-06-16 00:53:58 +08:00
|
|
|
} else {
|
|
|
|
write_history(ctl->historyfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 23:09:30 +08:00
|
|
|
g_clear_pointer(&ctl->historydir, g_free);
|
|
|
|
g_clear_pointer(&ctl->historyfile, g_free);
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2019-10-14 20:44:29 +08:00
|
|
|
vshReadline(vshControl *ctl G_GNUC_UNUSED, const char *prompt)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
|
|
|
return readline(prompt);
|
|
|
|
}
|
|
|
|
|
2020-09-03 16:02:52 +08:00
|
|
|
void
|
|
|
|
vshReadlineHistoryAdd(const char *cmd)
|
|
|
|
{
|
|
|
|
return add_history(cmd);
|
|
|
|
}
|
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
#else /* !WITH_READLINE */
|
|
|
|
|
|
|
|
static int
|
2019-10-14 20:44:29 +08:00
|
|
|
vshReadlineInit(vshControl *ctl G_GNUC_UNUSED)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
|
|
|
/* empty */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-10-14 20:44:29 +08:00
|
|
|
vshReadlineDeinit(vshControl *ctl G_GNUC_UNUSED)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
|
|
|
/* empty */
|
|
|
|
}
|
|
|
|
|
2015-08-15 15:59:41 +08:00
|
|
|
char *
|
2019-10-21 17:11:04 +08:00
|
|
|
vshReadline(vshControl *ctl G_GNUC_UNUSED,
|
|
|
|
const char *prompt)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
|
|
|
char line[1024];
|
|
|
|
char *r;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
fputs(prompt, stdout);
|
2022-03-09 21:01:49 +08:00
|
|
|
fflush(stdout);
|
2015-06-16 00:53:58 +08:00
|
|
|
r = fgets(line, sizeof(line), stdin);
|
|
|
|
if (r == NULL) return NULL; /* EOF */
|
|
|
|
|
|
|
|
/* Chomp trailing \n */
|
|
|
|
len = strlen(r);
|
|
|
|
if (len > 0 && r[len-1] == '\n')
|
|
|
|
r[len-1] = '\0';
|
|
|
|
|
2019-10-18 23:24:02 +08:00
|
|
|
return g_strdup(r);
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
2020-09-03 16:02:52 +08:00
|
|
|
void
|
2020-09-04 17:17:30 +08:00
|
|
|
vshReadlineHistoryAdd(const char *cmd G_GNUC_UNUSED)
|
2020-09-03 16:02:52 +08:00
|
|
|
{
|
|
|
|
/* empty */
|
|
|
|
}
|
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
#endif /* !WITH_READLINE */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize debug settings.
|
|
|
|
*/
|
2016-07-28 18:33:21 +08:00
|
|
|
static int
|
2015-06-16 00:53:58 +08:00
|
|
|
vshInitDebug(vshControl *ctl)
|
|
|
|
{
|
|
|
|
const char *debugEnv;
|
|
|
|
|
|
|
|
if (ctl->debug == VSH_DEBUG_DEFAULT) {
|
2021-08-10 23:09:07 +08:00
|
|
|
g_autofree char *env = g_strdup_printf("%s_DEBUG", ctl->env_prefix);
|
2016-07-28 18:54:16 +08:00
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
/* log level not set from commandline, check env variable */
|
2019-08-01 20:35:56 +08:00
|
|
|
debugEnv = getenv(env);
|
2015-06-16 00:53:58 +08:00
|
|
|
if (debugEnv) {
|
|
|
|
int debug;
|
|
|
|
if (virStrToLong_i(debugEnv, NULL, 10, &debug) < 0 ||
|
|
|
|
debug < VSH_ERR_DEBUG || debug > VSH_ERR_ERROR) {
|
2023-03-09 22:54:58 +08:00
|
|
|
vshError(ctl, _("%1$s_DEBUG not set with a valid numeric value"),
|
2016-11-11 20:39:45 +08:00
|
|
|
ctl->env_prefix);
|
2015-06-16 00:53:58 +08:00
|
|
|
} else {
|
|
|
|
ctl->debug = debug;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctl->logfile == NULL) {
|
2021-08-10 23:09:07 +08:00
|
|
|
g_autofree char *env = g_strdup_printf("%s_LOG_FILE", ctl->env_prefix);
|
2016-07-28 18:54:16 +08:00
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
/* log file not set from cmdline */
|
2019-08-01 20:35:56 +08:00
|
|
|
debugEnv = getenv(env);
|
2015-06-16 00:53:58 +08:00
|
|
|
if (debugEnv && *debugEnv) {
|
2019-10-18 23:24:02 +08:00
|
|
|
ctl->logfile = g_strdup(debugEnv);
|
2015-06-16 00:53:58 +08:00
|
|
|
vshOpenLogFile(ctl);
|
|
|
|
}
|
|
|
|
}
|
2016-07-28 18:33:21 +08:00
|
|
|
|
|
|
|
return 0;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize global data
|
|
|
|
*/
|
2015-09-03 22:52:44 +08:00
|
|
|
bool
|
2024-03-04 22:51:28 +08:00
|
|
|
vshInit(vshControl *ctl, const vshCmdGrp *groups)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
|
|
|
if (!ctl->hooks) {
|
|
|
|
vshError(ctl, "%s", _("client hooks cannot be NULL"));
|
2015-09-03 22:52:44 +08:00
|
|
|
return false;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
2024-03-04 22:51:28 +08:00
|
|
|
if (!groups) {
|
|
|
|
vshError(ctl, "%s", _("command groups must be non-NULL"));
|
2015-09-03 22:52:44 +08:00
|
|
|
return false;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cmdGroups = groups;
|
|
|
|
|
2016-07-28 18:33:21 +08:00
|
|
|
if (vshInitDebug(ctl) < 0 ||
|
|
|
|
(ctl->imode && vshReadlineInit(ctl) < 0))
|
2015-09-03 22:52:44 +08:00
|
|
|
return false;
|
2015-06-16 00:53:58 +08:00
|
|
|
|
2015-09-03 22:52:44 +08:00
|
|
|
return true;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
2015-09-03 22:59:01 +08:00
|
|
|
bool
|
|
|
|
vshInitReload(vshControl *ctl)
|
|
|
|
{
|
2024-03-04 22:51:28 +08:00
|
|
|
if (!cmdGroups) {
|
|
|
|
vshError(ctl, "%s", _("command groups is NULL run vshInit before reloading"));
|
2015-09-03 22:59:01 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-28 18:33:21 +08:00
|
|
|
if (vshInitDebug(ctl) < 0)
|
|
|
|
return false;
|
2015-09-03 22:59:01 +08:00
|
|
|
|
|
|
|
if (ctl->imode)
|
|
|
|
vshReadlineDeinit(ctl);
|
|
|
|
if (ctl->imode && vshReadlineInit(ctl) < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
void
|
|
|
|
vshDeinit(vshControl *ctl)
|
|
|
|
{
|
2016-09-29 03:01:55 +08:00
|
|
|
/* NB: Don't make calling of vshReadlineDeinit conditional on active
|
|
|
|
* interactive mode. */
|
|
|
|
vshReadlineDeinit(ctl);
|
2015-06-16 00:53:58 +08:00
|
|
|
vshCloseLogFile(ctl);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -----------------------------------------------
|
|
|
|
* Generic commands available to use by any client
|
|
|
|
* -----------------------------------------------
|
|
|
|
*/
|
2021-09-16 22:07:19 +08:00
|
|
|
|
|
|
|
static char **
|
|
|
|
vshCompleteHelpCommand(vshControl *ctl G_GNUC_UNUSED,
|
|
|
|
const vshCmd *cmd G_GNUC_UNUSED,
|
|
|
|
unsigned int completerflags G_GNUC_UNUSED)
|
|
|
|
{
|
|
|
|
return vshReadlineCommandGenerator();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
const vshCmdOptDef opts_help[] = {
|
|
|
|
{.name = "command",
|
|
|
|
.type = VSH_OT_STRING,
|
2024-03-14 23:58:40 +08:00
|
|
|
.positional = true,
|
2021-09-16 22:07:19 +08:00
|
|
|
.completer = vshCompleteHelpCommand,
|
2015-06-16 00:53:58 +08:00
|
|
|
.help = N_("Prints global help, command specific help, or help for a group of related commands")
|
|
|
|
},
|
|
|
|
{.name = NULL}
|
|
|
|
};
|
|
|
|
|
2024-03-04 20:21:50 +08:00
|
|
|
const vshCmdInfo info_help = {
|
|
|
|
.help = N_("print help"),
|
|
|
|
.desc = N_("Prints global help, command specific help, or help for a\n"
|
|
|
|
" group of related commands"),
|
2015-06-16 00:53:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
cmdHelp(vshControl *ctl, const vshCmd *cmd)
|
2016-06-18 01:27:08 +08:00
|
|
|
{
|
2018-01-25 23:08:46 +08:00
|
|
|
const vshCmdDef *def = NULL;
|
|
|
|
const vshCmdGrp *grp = NULL;
|
2015-06-16 00:53:58 +08:00
|
|
|
const char *name = NULL;
|
|
|
|
|
2015-12-03 20:47:56 +08:00
|
|
|
if (vshCommandOptStringQuiet(ctl, cmd, "command", &name) <= 0) {
|
2015-06-16 00:53:58 +08:00
|
|
|
vshPrint(ctl, "%s", _("Grouped commands:\n\n"));
|
|
|
|
|
|
|
|
for (grp = cmdGroups; grp->name; grp++) {
|
2023-03-09 22:54:58 +08:00
|
|
|
vshPrint(ctl, _(" %1$s (help keyword '%2$s'):\n"), grp->name,
|
2015-06-16 00:53:58 +08:00
|
|
|
grp->keyword);
|
|
|
|
|
|
|
|
for (def = grp->commands; def->name; def++) {
|
2024-03-02 06:14:51 +08:00
|
|
|
if (def->alias ||
|
2021-09-16 21:57:28 +08:00
|
|
|
def->flags & VSH_CMD_FLAG_HIDDEN)
|
2015-06-16 00:53:58 +08:00
|
|
|
continue;
|
2024-03-04 20:21:50 +08:00
|
|
|
vshPrint(ctl, " %-30s %s\n", def->name, _(def->info->help));
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
vshPrint(ctl, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-25 23:08:46 +08:00
|
|
|
if ((def = vshCmddefSearch(name))) {
|
2024-03-02 06:14:51 +08:00
|
|
|
if (def->alias)
|
2018-01-25 23:13:37 +08:00
|
|
|
def = vshCmddefSearch(def->alias);
|
2024-03-04 22:51:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (def) {
|
2020-11-12 20:42:45 +08:00
|
|
|
return vshCmddefHelp(def);
|
2018-01-25 23:08:46 +08:00
|
|
|
} else if ((grp = vshCmdGrpSearch(name))) {
|
|
|
|
return vshCmdGrpHelp(ctl, grp);
|
2015-06-16 00:53:58 +08:00
|
|
|
} else {
|
2023-03-09 22:54:58 +08:00
|
|
|
vshError(ctl, _("command or command group '%1$s' doesn't exist"), name);
|
2015-06-16 00:53:58 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const vshCmdOptDef opts_cd[] = {
|
|
|
|
{.name = "dir",
|
|
|
|
.type = VSH_OT_STRING,
|
2024-03-14 23:58:40 +08:00
|
|
|
.positional = true,
|
2015-06-16 00:53:58 +08:00
|
|
|
.help = N_("directory to switch to (default: home or else root)")
|
|
|
|
},
|
|
|
|
{.name = NULL}
|
|
|
|
};
|
|
|
|
|
2024-03-04 20:21:50 +08:00
|
|
|
const vshCmdInfo info_cd = {
|
|
|
|
.help = N_("change the current directory"),
|
|
|
|
.desc = N_("Change the current directory."),
|
2015-06-16 00:53:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
cmdCd(vshControl *ctl, const vshCmd *cmd)
|
|
|
|
{
|
|
|
|
const char *dir = NULL;
|
2019-12-19 16:09:02 +08:00
|
|
|
g_autofree char *dir_malloced = NULL;
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
if (!ctl->imode) {
|
|
|
|
vshError(ctl, "%s", _("cd: command valid only in interactive mode"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-03 20:47:56 +08:00
|
|
|
if (vshCommandOptStringQuiet(ctl, cmd, "dir", &dir) <= 0)
|
2015-06-16 00:53:58 +08:00
|
|
|
dir = dir_malloced = virGetUserDirectory();
|
|
|
|
if (!dir)
|
|
|
|
dir = "/";
|
|
|
|
|
|
|
|
if (chdir(dir) == -1) {
|
2023-03-09 22:54:58 +08:00
|
|
|
vshError(ctl, _("cd: %1$s: %2$s"),
|
2020-02-27 01:57:34 +08:00
|
|
|
g_strerror(errno), dir);
|
2019-12-19 16:09:02 +08:00
|
|
|
return false;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
2019-12-19 16:09:02 +08:00
|
|
|
return true;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const vshCmdOptDef opts_echo[] = {
|
|
|
|
{.name = "shell",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("escape for shell use")
|
|
|
|
},
|
|
|
|
{.name = "xml",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("escape for XML use")
|
|
|
|
},
|
2021-08-11 21:22:59 +08:00
|
|
|
{.name = "split",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("split each argument on ','; ',,' is an escape sequence")
|
|
|
|
},
|
2019-03-24 08:02:13 +08:00
|
|
|
{.name = "err",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("output to stderr"),
|
|
|
|
},
|
2015-06-16 00:53:58 +08:00
|
|
|
{.name = "str",
|
|
|
|
.type = VSH_OT_ALIAS,
|
|
|
|
.help = "string"
|
|
|
|
},
|
|
|
|
{.name = "hi",
|
|
|
|
.type = VSH_OT_ALIAS,
|
|
|
|
.help = "string=hello"
|
|
|
|
},
|
|
|
|
{.name = "string",
|
|
|
|
.type = VSH_OT_ARGV,
|
2024-03-05 22:07:47 +08:00
|
|
|
.positional = true,
|
2015-06-16 00:53:58 +08:00
|
|
|
.help = N_("arguments to echo")
|
|
|
|
},
|
|
|
|
{.name = NULL}
|
|
|
|
};
|
|
|
|
|
2024-03-04 20:21:50 +08:00
|
|
|
const vshCmdInfo info_echo = {
|
|
|
|
.help = N_("echo arguments. Used for internal testing."),
|
|
|
|
.desc = N_("Echo back arguments, possibly with quoting. Used for internal testing."),
|
2015-06-16 00:53:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Exists mainly for debugging virsh, but also handy for adding back
|
|
|
|
* quotes for later evaluation.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
cmdEcho(vshControl *ctl, const vshCmd *cmd)
|
|
|
|
{
|
2021-08-11 16:36:04 +08:00
|
|
|
bool shell = vshCommandOptBool(cmd, "shell");
|
|
|
|
bool xml = vshCommandOptBool(cmd, "xml");
|
|
|
|
bool err = vshCommandOptBool(cmd, "err");
|
2021-08-11 21:22:59 +08:00
|
|
|
bool split = vshCommandOptBool(cmd, "split");
|
2015-06-16 00:53:58 +08:00
|
|
|
const vshCmdOpt *opt = NULL;
|
2021-08-10 23:09:07 +08:00
|
|
|
g_autofree char *arg = NULL;
|
2020-07-03 07:40:16 +08:00
|
|
|
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
|
2015-06-16 00:53:58 +08:00
|
|
|
|
2021-08-11 16:36:04 +08:00
|
|
|
VSH_EXCLUSIVE_OPTIONS_VAR(shell, xml);
|
2021-08-11 21:22:59 +08:00
|
|
|
VSH_EXCLUSIVE_OPTIONS_VAR(shell, split);
|
|
|
|
VSH_EXCLUSIVE_OPTIONS_VAR(xml, split);
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
while ((opt = vshCommandOptArgv(ctl, cmd, opt))) {
|
2021-08-10 23:10:34 +08:00
|
|
|
const char *curr = opt->data;
|
2015-06-16 00:53:58 +08:00
|
|
|
|
|
|
|
if (xml) {
|
2021-08-11 17:10:12 +08:00
|
|
|
virBufferEscapeString(&buf, "%s", curr);
|
|
|
|
} else if (shell) {
|
|
|
|
virBufferEscapeShell(&buf, curr);
|
2021-08-11 21:22:59 +08:00
|
|
|
} else if (split) {
|
|
|
|
g_auto(GStrv) spl = NULL;
|
|
|
|
GStrv n;
|
|
|
|
|
|
|
|
vshStringToArray(curr, &spl);
|
|
|
|
|
|
|
|
for (n = spl; *n; n++)
|
|
|
|
virBufferAsprintf(&buf, "%s\n", *n);
|
2015-06-16 00:53:58 +08:00
|
|
|
} else {
|
2021-08-11 17:10:12 +08:00
|
|
|
virBufferAdd(&buf, curr, -1);
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
2021-08-11 17:10:12 +08:00
|
|
|
virBufferAddChar(&buf, ' ');
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
2021-08-11 17:10:12 +08:00
|
|
|
virBufferTrim(&buf, " ");
|
|
|
|
|
2015-06-16 00:53:58 +08:00
|
|
|
arg = virBufferContentAndReset(&buf);
|
2019-03-24 08:02:13 +08:00
|
|
|
if (arg) {
|
|
|
|
if (err)
|
|
|
|
vshError(ctl, "%s", arg);
|
|
|
|
else
|
|
|
|
vshPrint(ctl, "%s", arg);
|
|
|
|
}
|
2015-06-16 00:53:58 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-03-04 20:21:50 +08:00
|
|
|
const vshCmdInfo info_pwd = {
|
|
|
|
.help = N_("print the current directory"),
|
|
|
|
.desc = N_("Print the current directory."),
|
2015-06-16 00:53:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
2019-10-14 20:44:29 +08:00
|
|
|
cmdPwd(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
2019-12-21 00:51:16 +08:00
|
|
|
g_autofree char *cwd = g_get_current_dir();
|
2015-06-16 00:53:58 +08:00
|
|
|
|
2023-03-09 22:54:58 +08:00
|
|
|
vshPrint(ctl, _("%1$s\n"), cwd);
|
2015-06-16 00:53:58 +08:00
|
|
|
|
2019-12-21 00:51:16 +08:00
|
|
|
return true;
|
2015-06-16 00:53:58 +08:00
|
|
|
}
|
|
|
|
|
2024-03-04 20:21:50 +08:00
|
|
|
const vshCmdInfo info_quit = {
|
|
|
|
.help = N_("quit this interactive terminal"),
|
|
|
|
.desc = "",
|
2015-06-16 00:53:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
2019-10-14 20:44:29 +08:00
|
|
|
cmdQuit(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
|
2015-06-16 00:53:58 +08:00
|
|
|
{
|
|
|
|
ctl->imode = false;
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-14 15:50:22 +08:00
|
|
|
|
|
|
|
/* -----------------
|
|
|
|
* Command self-test
|
|
|
|
* ----------------- */
|
|
|
|
|
2021-03-04 22:47:28 +08:00
|
|
|
const vshCmdOptDef opts_selftest[] = {
|
|
|
|
{.name = "completers-missing",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("output the list of options which are missing completers")
|
|
|
|
},
|
2024-03-02 06:01:54 +08:00
|
|
|
{.name = "dump-help",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("output help for each command")
|
|
|
|
},
|
2024-03-14 04:13:44 +08:00
|
|
|
{.name = "broken-positionals",
|
|
|
|
.type = VSH_OT_INT,
|
|
|
|
.flags = VSH_OFLAG_REQ_OPT,
|
|
|
|
.help = N_("debug positional args")
|
|
|
|
},
|
2021-03-04 22:47:28 +08:00
|
|
|
{.name = NULL}
|
|
|
|
};
|
2024-03-04 20:21:50 +08:00
|
|
|
const vshCmdInfo info_selftest = {
|
|
|
|
.help = N_("internal command for testing virt shells"),
|
|
|
|
.desc = N_("internal use only"),
|
2016-09-14 15:50:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
2021-03-04 22:47:28 +08:00
|
|
|
cmdSelfTest(vshControl *ctl, const vshCmd *cmd)
|
2016-09-14 15:50:22 +08:00
|
|
|
{
|
|
|
|
const vshCmdGrp *grp;
|
|
|
|
const vshCmdDef *def;
|
2021-03-04 22:47:28 +08:00
|
|
|
bool completers = vshCommandOptBool(cmd, "completers-missing");
|
2024-03-02 06:01:54 +08:00
|
|
|
bool dumphelp = vshCommandOptBool(cmd, "dump-help");
|
2024-03-14 04:13:44 +08:00
|
|
|
int brokenPositionals = -1;
|
|
|
|
|
|
|
|
ignore_value(vshCommandOptInt(ctl, cmd, "broken-positionals", &brokenPositionals));
|
2016-09-14 15:50:22 +08:00
|
|
|
|
|
|
|
for (grp = cmdGroups; grp->name; grp++) {
|
|
|
|
for (def = grp->commands; def->name; def++) {
|
2024-03-02 06:01:54 +08:00
|
|
|
|
|
|
|
if (dumphelp && !def->alias)
|
|
|
|
vshCmddefHelp(def);
|
|
|
|
|
2024-03-14 04:13:44 +08:00
|
|
|
if (vshCmddefCheckInternals(ctl, def, completers, brokenPositionals) < 0)
|
2016-09-14 15:50:22 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-11-01 22:34:14 +08:00
|
|
|
|
|
|
|
/* ----------------------
|
|
|
|
* Autocompletion command
|
|
|
|
* ---------------------- */
|
|
|
|
|
|
|
|
const vshCmdOptDef opts_complete[] = {
|
|
|
|
{.name = "string",
|
|
|
|
.type = VSH_OT_ARGV,
|
2024-03-05 22:07:47 +08:00
|
|
|
.positional = true,
|
2017-11-01 22:34:14 +08:00
|
|
|
.flags = VSH_OFLAG_EMPTY_OK,
|
|
|
|
.help = N_("partial string to autocomplete")
|
|
|
|
},
|
|
|
|
{.name = NULL}
|
|
|
|
};
|
|
|
|
|
2024-03-04 20:21:50 +08:00
|
|
|
const vshCmdInfo info_complete = {
|
|
|
|
.help = N_("internal command for autocompletion"),
|
|
|
|
.desc = N_("internal use only"),
|
2017-11-01 22:34:14 +08:00
|
|
|
};
|
|
|
|
|
2018-01-14 18:43:51 +08:00
|
|
|
|
|
|
|
#ifdef WITH_READLINE
|
2017-11-01 22:34:14 +08:00
|
|
|
bool
|
|
|
|
cmdComplete(vshControl *ctl, const vshCmd *cmd)
|
|
|
|
{
|
|
|
|
const vshClientHooks *hooks = ctl->hooks;
|
|
|
|
int stdin_fileno = STDIN_FILENO;
|
|
|
|
const char *arg = "";
|
|
|
|
const vshCmdOpt *opt = NULL;
|
2021-11-01 17:34:10 +08:00
|
|
|
g_auto(GStrv) matches = NULL;
|
|
|
|
char **iter;
|
2020-07-03 07:40:16 +08:00
|
|
|
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
|
2017-11-01 22:34:14 +08:00
|
|
|
|
|
|
|
if (vshCommandOptStringQuiet(ctl, cmd, "string", &arg) <= 0)
|
2022-01-14 21:05:52 +08:00
|
|
|
return false;
|
2017-11-01 22:34:14 +08:00
|
|
|
|
|
|
|
/* This command is flagged VSH_CMD_FLAG_NOCONNECT because we
|
|
|
|
* need to prevent auth hooks reading any input. Therefore, we
|
|
|
|
* have to close stdin and then connect ourselves. */
|
|
|
|
VIR_FORCE_CLOSE(stdin_fileno);
|
|
|
|
|
|
|
|
if (!(hooks && hooks->connHandler && hooks->connHandler(ctl)))
|
2022-01-14 21:05:52 +08:00
|
|
|
return false;
|
2017-11-01 22:34:14 +08:00
|
|
|
|
|
|
|
while ((opt = vshCommandOptArgv(ctl, cmd, opt))) {
|
|
|
|
if (virBufferUse(&buf) != 0)
|
|
|
|
virBufferAddChar(&buf, ' ');
|
|
|
|
virBufferAddStr(&buf, opt->data);
|
|
|
|
arg = opt->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
vshReadlineInit(ctl);
|
|
|
|
|
2019-10-20 19:49:46 +08:00
|
|
|
if (!(rl_line_buffer = virBufferContentAndReset(&buf)))
|
|
|
|
rl_line_buffer = g_strdup("");
|
2017-11-01 22:34:14 +08:00
|
|
|
|
|
|
|
/* rl_point is current cursor position in rl_line_buffer.
|
|
|
|
* In our case it's at the end of the whole line. */
|
|
|
|
rl_point = strlen(rl_line_buffer);
|
|
|
|
|
|
|
|
if (!(matches = vshReadlineCompletion(arg, 0, 0)))
|
2022-01-14 21:05:52 +08:00
|
|
|
return false;
|
2017-11-01 22:34:14 +08:00
|
|
|
|
2018-05-11 17:13:10 +08:00
|
|
|
for (iter = matches; *iter; iter++) {
|
|
|
|
if (iter == matches && matches[1])
|
|
|
|
continue;
|
2017-11-01 22:34:14 +08:00
|
|
|
printf("%s\n", *iter);
|
2018-05-11 17:13:10 +08:00
|
|
|
}
|
2017-11-01 22:34:14 +08:00
|
|
|
|
2022-01-14 21:05:52 +08:00
|
|
|
return true;
|
2017-11-01 22:34:14 +08:00
|
|
|
}
|
2018-01-14 18:43:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
#else /* !WITH_READLINE */
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
2019-10-14 20:44:29 +08:00
|
|
|
cmdComplete(vshControl *ctl G_GNUC_UNUSED,
|
|
|
|
const vshCmd *cmd G_GNUC_UNUSED)
|
2018-01-14 18:43:51 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif /* !WITH_READLINE */
|