From: Lin Ma Date: Tue, 10 Nov 2020 09:50:55 +0000 (+0800) Subject: virsh-interface: Add a static helper virshInterfaceStringHelper X-Git-Tag: v6.10.0-rc1~267 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9b751469ea30b3890ae95160731bebc25b9db16e;p=thirdparty%2Flibvirt.git virsh-interface: Add a static helper virshInterfaceStringHelper It will be helpful to get the desired string of interface name/mac in a consistent way. Signed-off-by: Lin Ma Signed-off-by: Michal Privoznik Reviewed-by: Michal Privoznik --- diff --git a/tools/virsh-completer-interface.c b/tools/virsh-completer-interface.c index 8028db8746..1aea7e03c5 100644 --- a/tools/virsh-completer-interface.c +++ b/tools/virsh-completer-interface.c @@ -25,16 +25,19 @@ #include "virsh.h" #include "virstring.h" -char ** -virshInterfaceNameCompleter(vshControl *ctl, - const vshCmd *cmd G_GNUC_UNUSED, - unsigned int flags) +typedef const char * +(*virInterfaceStringCallback)(virInterfacePtr iface); + +static char ** +virshInterfaceStringHelper(vshControl *ctl, + const vshCmd *cmd G_GNUC_UNUSED, + unsigned int flags, + virInterfaceStringCallback cb) { virshControlPtr priv = ctl->privData; virInterfacePtr *ifaces = NULL; int nifaces = 0; size_t i = 0; - char **ret = NULL; VIR_AUTOSTRINGLIST tmp = NULL; virCheckFlags(VIR_CONNECT_LIST_INTERFACES_ACTIVE | @@ -50,15 +53,23 @@ virshInterfaceNameCompleter(vshControl *ctl, tmp = g_new0(char *, nifaces + 1); for (i = 0; i < nifaces; i++) { - const char *name = virInterfaceGetName(ifaces[i]); + const char *name = (cb)(ifaces[i]); tmp[i] = g_strdup(name); } - ret = g_steal_pointer(&tmp); - for (i = 0; i < nifaces; i++) virInterfaceFree(ifaces[i]); g_free(ifaces); - return ret; + + return g_steal_pointer(&tmp); +} + + +char ** +virshInterfaceNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags) +{ + return virshInterfaceStringHelper(ctl, cmd, flags, virInterfaceGetName); }