Error messages are exempt from the 80 columns rule. Move them
onto one line.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
Add a version for functions which may already need to take a printf
format string.
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
We are not guaranteed that the string we are printing onto stdout
contains '\n' and thus that the stdout is flushed. In fact, I've
met this problem when virsh asked me whether I want to edit the
domain XML again (vshAskReedit()) but the prompt wasn't displayed
(as it does not contain a newline character) and virsh just sat
there waiting for my input, I sat there waiting for virsh's
output. Flush stdout after all fputs()-s which do not flush
stdout.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
The vsh helpers for user-editing of contents use temporary files.
Introduce 'vshTempFile' type which automatically removes the file.
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This change was generated using the following spatch:
@ rule1 @
expression a;
identifier f;
@@
<...
- f(*a);
... when != a;
- *a = NULL;
+ g_clear_pointer(a, f);
...>
@ rule2 @
expression a;
identifier f;
@@
<...
- f(a);
... when != a;
- a = NULL;
+ g_clear_pointer(&a, f);
...>
Then, I left some of the changes out, like tools/nss/ (which
doesn't link with glib) and put back a comment in
qemuBlockJobProcessEventCompletedActiveCommit() which coccinelle
decided to remove (I have no idea why).
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
There are few places where a cleanup label contains nothing but a
return statement. Drop such labels and return directly.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
There are a few cases where a string list is freed by an explicit
call of g_strfreev(), but the same result can be achieved by
g_atuo(GStrv).
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Tim Wiederhake <twiederh@redhat.com>
There are few places where we can replace explicit
VIR_FORCE_CLOSE() with VIR_AUTOCLOSE annotation.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Acked-by: Jonathon Jongsma <jjongsma@redhat.com>
Both function description and function itself mention check for
OOM which can't happen really. There was a bug in glib where
g_strdup_*() might have not aborted on OOM, but we have our own
implementation when dealing with broken glib (see
vir_g_strdup_printf()). Therefore, checking for OOM is redundant
and can never be true.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Let's check whether a boolean --option doesn't have completer or
completer_flags set. These options are just flags and don't
accept any value, thus they can't have any completer.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
If a command is an alias, then it can only have .name, .flags and
.alias set and .flags should contain just VSH_CMD_FLAG_ALIAS.
Check if that's the case in self-test.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
It's now also used in vshCompleteHelpCommand which is outside of the
conditionally compiled code.
Fixes: 80f70c74a7
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Wrap 'vshReadlineCommandGenerator' into a function with proper prototype
to provide a completer for the help command.
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
Make it simple to spot which options of which commands are missing
autocompletion functions by introducing this hidden option.
In the future when we'll have completers for everything this can be also
used as a hard fail so that completers are always added.
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
We don't need to validate the real command twice, but it's better to
check that the real command name exists and it's not an alias to prevent
loops.
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
Introduce a proper flag 'VSH_CMD_FLAG_HIDDEN' for hiding commands from
output so that we can validate that there aren't any loops or
misconfigured commands.
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
Use 'g_strsplit' to split the strings and then concatenate back when the
escape sequence (',,') is used.
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Add a '--split' switch for the 'virsh echo' command and add few test
cases to the virshtest.
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Remove the need for temporary strings by filling the output buffer
directly.
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Initialize the flags earlier and use VSH_EXCLUSIVE_OPTIONS_VAR to
declare the conflicting options as exclusive.
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Note that it's for internal testing use and remove the manpage entry.
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Remove cleanup sections that are no longer needed, as well
as unnecessary 'ret' variables.
Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
Do not use 'arg' which is later used for an allocated string.
Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
Instead of using the same variable to store either a const pointer
or an allocated string, always make a copy.
Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
None of them are currently needed to pass our upstream CI, most were
either for ancient clang versions or coverity for silencing false
positives.
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
Historically, we declared pointer type to our types:
typedef struct _virXXX virXXX;
typedef virXXX *virXXXPtr;
But usefulness of such declaration is questionable, at best.
Unfortunately, we can't drop every such declaration - we have to
carry some over, because they are part of public API (e.g.
virDomainPtr). But for internal types - we can do drop them and
use what every other C project uses 'virXXX *'.
This change was generated by a very ugly shell script that
generated sed script which was then called over each file in the
repository. For the shell script refer to the cover letter:
https://listman.redhat.com/archives/libvir-list/2021-March/msg00537.html
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
One of the error branches used a plain free where vshCommandFree
was required.
https://bugzilla.redhat.com/show_bug.cgi?id=1943415
Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
Generated by the following spatch:
@@
expression a, b;
@@
+ b = g_steal_pointer(&a);
- b = a;
... when != a
- a = NULL;
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
These functions are identical. Made using this spatch:
@@
expression path, mode;
@@
- virFileMakePathWithMode(path, mode)
+ g_mkdir_with_parents(path, mode)
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
The glib implementation doesn't tolerate NULL but in most cases we check
before anyways. The rest of the callers adds a NULL check.
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
After previous patches neither vshReadlineCommandGenerator() nor
vshReadlineOptionsGenerator() use prefix that user wants to
complete. The argument is marked as unused in both functions.
Drop it then.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>
Firstly, move variable declarations into the inner most block
they are used. Secondly, use for() loop instead of while so that
we don't have to advance loop counter explicitly on 'continue'.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>
The way we currently call completer callbacks is that if we've
found --option that user wants to complete value for and it has
callback set then the callback is called.
And just before that, if no --option to have the value completed
is found or is found and is of boolean type then a list of
--option is generated (for given command).
But these two conditions can never be true at the same time
because boolean type of --options do not accept values. Therefore
the calling of completer callback can be promoted onto the same
level as the --option list generation.
This means that merging of two lists can be dropped to and
completer callback can store its retval directly into @list (but
as shown earlier one of the string lists to merge is always
empty).
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>
Completer callbacks generate all possible outputs ignoring any partial
input (e.g. prefix of a domain name) and then use vshCompleterFilter() to
filter out those strings which don't fit the partial input (prefix).
In contrast, vshReadlineCommandGenerator() does some internal filtering and
only generates completions that match a given prefix. Rather than treating
these scenarios differently, simply generate all possible options and
filter them all at the end.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>
Completer callbacks generate all possible outputs ignoring any partial
input (e.g. prefix of a domain name) and then use vshCompleterFilter() to
filter out those strings which don't fit the partial input (prefix).
In contrast, vshReadlineOptionsGenerator() does some internal filtering and
only generates completions that match a given prefix. Rather than treating
these scenarios differently, simply generate all possible options and
filter them all at the end.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>
The vshReadlineParse() function is called whenever user hits
<TAB><TAB>. If there is no command (or a partially written one),
then a list of possible commands is printed to the user. But, if
there is a command then its --options are generated. But
obviously, we can not generate --options if there already is an
--option that's expecting a value. For instance, consider:
virsh # start --domain <TAB><TAB>
In this case we want to call completer for --domain option, but
that's a different story.
Anyway, the way that we currently check whether --options list
should be generated is checking the type of the last --option. If
it isn't DATA, STRING, INT, or ARGV (all these expect a value),
then we can generate --option list. Well, writing the condition
this way is needlessly verbose and also prone to errors (see
d9a320bf97 for example).
We know that boolean type does not require a value. This leaves
us with the only type that was not mentioned yet - VSH_OT_ALIAS.
This is a special type for backwards compatibility and it refers
to another --option which can be just any type.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>
There are two functions that are used to generate completion
lists: vshReadlineCommandGenerator() for command names and
vshReadlineOptionsGenerator() for --options for given command.
Both return a string list, but may also fail while constructing
it. For that case, they call g_strfreev() explicitly, which is
needless since we have g_auto(GStrv).
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>
The vshReadlineOptionsGenerator() function returns a string list
of all --options for given command. But the way that individual
items on the list are allocated can be written better.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>
The aim of vshCompleterFilter() is to take a string list and a
prefix and remove all strings from the list that don't have the
desired prefix. The function is used to filter out those strings
returned by a completer callback that don't correspond with
user's (partial) input. For instance, domain name completer
virshDomainNameCompleter() returns all domain names and then
vshCompleterFilter() refines the list so that only domains with
correct prefix of their name are offered to user. This was a
design choice - it allows us to have shorter completers as they
do not have to copy the list filtering over and over.
Having said all of that, it may happen that a completer does not
return anything (e.g. there is no domain in requested state,
virsh is not connected and thus completer exited early, etc.). In
that case, the string list is NULL and vshCompleterFilter() can
simply return early.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>
This saves us explicit call of g_strfreev() in error path.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>
We've invented VSH_OT_ALIAS type for --option so that we can
rewrite some --options (e.g. fix spelling). For instance
blkdeviotune command uses this feature heavily:
--options-with-dash are preferred over old
--options_with_underscore. Both versions are supported but only
the new ones (not aliased) are documented and reported in --help.
Except for options completer, which happily put also aliased
versions in front of user's eyes.
Note, there is a second (gross) way we use aliases: to rewrite
options from --oldoption to --newoption=value (for instance
--shareable option of attach-disk is an alias of
--mode=shareable). And just like with the previous group - don't
generate them into the list of possible options.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>