[libvirt] [PATCH 9/4] virsh: declare more common functions

Eric Blake eblake at redhat.com
Mon Aug 20 20:54:35 UTC 2012


In preparation for splitting virsh-interface.c, I found these
functions need to be declared in virsh.h, as well as one that
belongs more properly in virsh-domain.h.

* tools/virsh.h	(vshNameSorter, vshCmdHasOption): Declare.
(vshCommandOptDomainBy): Move...
* tools/virsh-domain.h): ...here.
* tools/virsh.c: (vshNameSorter): Export.
(cmd_has_option): Rename...
(vshCmdHasOption): ...and export.
(vshCommandOptDomainBy): Move...
* tools/virsh-domain.c (vshCommandOptDomainBy): ...here.
* tools/virsh-network.c (vshCommandOptNetworkBy): Update callers.
* tools/virsh-nwfilter.c (vshCommandOptNWFilterBy): Likewise.
* tools/virsh-secret.c (vshCommandOptSecret): Likewise.
* tools/virsh-domain-monitor.c (includes): Likewise.
* tools/virsh-host.c (includes): Likewise.
---
 tools/virsh-domain-monitor.c |  1 +
 tools/virsh-domain.c         | 48 +++++++++++++++++++++++++++++++++++++++
 tools/virsh-domain.h         | 12 ++++++++++
 tools/virsh-host.c           |  1 +
 tools/virsh-interface.c      |  2 +-
 tools/virsh-network.c        |  2 +-
 tools/virsh-nwfilter.c       |  2 +-
 tools/virsh-secret.c         |  2 +-
 tools/virsh.c                | 54 +++-----------------------------------------
 tools/virsh.h                | 14 ++----------
 10 files changed, 71 insertions(+), 67 deletions(-)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 4f00e65..44f65ad 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -36,6 +36,7 @@
 #include "intprops.h"
 #include "memory.h"
 #include "virmacaddr.h"
+#include "virsh-domain.h"
 #include "xml.h"

 static const char *
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index e949dcf..31f15dc 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -58,6 +58,54 @@
 # define SA_SIGINFO 0
 #endif

+virDomainPtr
+vshCommandOptDomainBy(vshControl *ctl, const vshCmd *cmd,
+                      const char **name, int flag)
+{
+    virDomainPtr dom = NULL;
+    const char *n = NULL;
+    int id;
+    const char *optname = "domain";
+    if (!vshCmdHasOption(ctl, cmd, optname))
+        return NULL;
+
+    if (vshCommandOptString(cmd, optname, &n) <= 0)
+        return NULL;
+
+    vshDebug(ctl, VSH_ERR_INFO, "%s: found option <%s>: %s\n",
+             cmd->def->name, optname, n);
+
+    if (name)
+        *name = n;
+
+    /* try it by ID */
+    if (flag & VSH_BYID) {
+        if (virStrToLong_i(n, NULL, 10, &id) == 0 && id >= 0) {
+            vshDebug(ctl, VSH_ERR_DEBUG,
+                     "%s: <%s> seems like domain ID\n",
+                     cmd->def->name, optname);
+            dom = virDomainLookupByID(ctl->conn, id);
+        }
+    }
+    /* try it by UUID */
+    if (dom==NULL && (flag & VSH_BYUUID) && strlen(n)==VIR_UUID_STRING_BUFLEN-1) {
+        vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as domain UUID\n",
+                 cmd->def->name, optname);
+        dom = virDomainLookupByUUIDString(ctl->conn, n);
+    }
+    /* try it by NAME */
+    if (dom==NULL && (flag & VSH_BYNAME)) {
+        vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as domain NAME\n",
+                 cmd->def->name, optname);
+        dom = virDomainLookupByName(ctl->conn, n);
+    }
+
+    if (!dom)
+        vshError(ctl, _("failed to get domain '%s'"), n);
+
+    return dom;
+}
+
 static const char *
 vshDomainVcpuStateToString(int state)
 {
diff --git a/tools/virsh-domain.h b/tools/virsh-domain.h
index b1b7930..21672e7 100644
--- a/tools/virsh-domain.h
+++ b/tools/virsh-domain.h
@@ -28,6 +28,18 @@

 # include "virsh.h"

+# define VSH_BYID     (1 << 1)
+# define VSH_BYUUID   (1 << 2)
+# define VSH_BYNAME   (1 << 3)
+# define VSH_BYMAC    (1 << 4)
+
+virDomainPtr vshCommandOptDomainBy(vshControl *ctl, const vshCmd *cmd,
+                                   const char **name, int flag);
+
+/* default is lookup by Id, Name and UUID */
+# define vshCommandOptDomain(_ctl, _cmd, _name)                      \
+    vshCommandOptDomainBy(_ctl, _cmd, _name, VSH_BYID|VSH_BYUUID|VSH_BYNAME)
+
 extern const vshCmdDef domManagementCmds[];

 #endif /* VIRSH_DOMAIN_H */
diff --git a/tools/virsh-host.c b/tools/virsh-host.c
index e3cff8e..3c44969 100644
--- a/tools/virsh-host.c
+++ b/tools/virsh-host.c
@@ -35,6 +35,7 @@
 #include "buf.h"
 #include "memory.h"
 #include "util.h"
+#include "virsh-domain.h"
 #include "xml.h"

 /*
diff --git a/tools/virsh-interface.c b/tools/virsh-interface.c
index ad080a1..e43aa91 100644
--- a/tools/virsh-interface.c
+++ b/tools/virsh-interface.c
@@ -38,7 +38,7 @@ vshCommandOptInterfaceBy(vshControl *ctl, const vshCmd *cmd,

     if (!optname)
        optname = "interface";
-    if (!cmd_has_option(ctl, cmd, optname))
+    if (!vshCmdHasOption(ctl, cmd, optname))
         return NULL;

     if (vshCommandOptString(cmd, optname, &n) <= 0)
diff --git a/tools/virsh-network.c b/tools/virsh-network.c
index b33e2d6..b891c91 100644
--- a/tools/virsh-network.c
+++ b/tools/virsh-network.c
@@ -35,7 +35,7 @@ vshCommandOptNetworkBy(vshControl *ctl, const vshCmd *cmd,
     virNetworkPtr network = NULL;
     const char *n = NULL;
     const char *optname = "network";
-    if (!cmd_has_option(ctl, cmd, optname))
+    if (!vshCmdHasOption(ctl, cmd, optname))
         return NULL;

     if (vshCommandOptString(cmd, optname, &n) <= 0)
diff --git a/tools/virsh-nwfilter.c b/tools/virsh-nwfilter.c
index 501e20d..a6ef233 100644
--- a/tools/virsh-nwfilter.c
+++ b/tools/virsh-nwfilter.c
@@ -35,7 +35,7 @@ vshCommandOptNWFilterBy(vshControl *ctl, const vshCmd *cmd,
     virNWFilterPtr nwfilter = NULL;
     const char *n = NULL;
     const char *optname = "nwfilter";
-    if (!cmd_has_option(ctl, cmd, optname))
+    if (!vshCmdHasOption(ctl, cmd, optname))
         return NULL;

     if (vshCommandOptString(cmd, optname, &n) <= 0)
diff --git a/tools/virsh-secret.c b/tools/virsh-secret.c
index 049ead5..6f971da 100644
--- a/tools/virsh-secret.c
+++ b/tools/virsh-secret.c
@@ -30,7 +30,7 @@ vshCommandOptSecret(vshControl *ctl, const vshCmd *cmd, const char **name)
     const char *n = NULL;
     const char *optname = "secret";

-    if (!cmd_has_option(ctl, cmd, optname))
+    if (!vshCmdHasOption(ctl, cmd, optname))
         return NULL;

     if (vshCommandOptString(cmd, optname, &n) <= 0)
diff --git a/tools/virsh.c b/tools/virsh.c
index 793a357..fe79b7c 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -128,7 +128,7 @@ _vshStrdup(vshControl *ctl, const char *s, const char *filename, int line)
 /* Poison the raw allocating identifiers in favor of our vsh variants.  */
 #define strdup use_vshStrdup_instead_of_strdup

-static int
+int
 vshNameSorter(const void *a, const void *b)
 {
     const char **sa = (const char**)a;
@@ -1441,8 +1441,8 @@ vshCommandOptArgv(const vshCmd *cmd, const vshCmdOpt *opt)
 /* Determine whether CMD->opts includes an option with name OPTNAME.
    If not, give a diagnostic and return false.
    If so, return true.  */
-static bool
-cmd_has_option(vshControl *ctl, const vshCmd *cmd, const char *optname)
+bool
+vshCmdHasOption(vshControl *ctl, const vshCmd *cmd, const char *optname)
 {
     /* Iterate through cmd->opts, to ensure that there is an entry
        with name OPTNAME and type VSH_OT_DATA. */
@@ -1461,54 +1461,6 @@ cmd_has_option(vshControl *ctl, const vshCmd *cmd, const char *optname)
     return found;
 }

-virDomainPtr
-vshCommandOptDomainBy(vshControl *ctl, const vshCmd *cmd,
-                      const char **name, int flag)
-{
-    virDomainPtr dom = NULL;
-    const char *n = NULL;
-    int id;
-    const char *optname = "domain";
-    if (!cmd_has_option(ctl, cmd, optname))
-        return NULL;
-
-    if (vshCommandOptString(cmd, optname, &n) <= 0)
-        return NULL;
-
-    vshDebug(ctl, VSH_ERR_INFO, "%s: found option <%s>: %s\n",
-             cmd->def->name, optname, n);
-
-    if (name)
-        *name = n;
-
-    /* try it by ID */
-    if (flag & VSH_BYID) {
-        if (virStrToLong_i(n, NULL, 10, &id) == 0 && id >= 0) {
-            vshDebug(ctl, VSH_ERR_DEBUG,
-                     "%s: <%s> seems like domain ID\n",
-                     cmd->def->name, optname);
-            dom = virDomainLookupByID(ctl->conn, id);
-        }
-    }
-    /* try it by UUID */
-    if (dom==NULL && (flag & VSH_BYUUID) && strlen(n)==VIR_UUID_STRING_BUFLEN-1) {
-        vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as domain UUID\n",
-                 cmd->def->name, optname);
-        dom = virDomainLookupByUUIDString(ctl->conn, n);
-    }
-    /* try it by NAME */
-    if (dom==NULL && (flag & VSH_BYNAME)) {
-        vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as domain NAME\n",
-                 cmd->def->name, optname);
-        dom = virDomainLookupByName(ctl->conn, n);
-    }
-
-    if (!dom)
-        vshError(ctl, _("failed to get domain '%s'"), n);
-
-    return dom;
-}
-
 /*
  * Executes command(s) and returns return code from last command
  */
diff --git a/tools/virsh.h b/tools/virsh.h
index 24d2020..58a2dcd 100644
--- a/tools/virsh.h
+++ b/tools/virsh.h
@@ -286,18 +286,7 @@ int vshCommandOptScaledInt(const vshCmd *cmd, const char *name,
 bool vshCommandOptBool(const vshCmd *cmd, const char *name);
 const vshCmdOpt *vshCommandOptArgv(const vshCmd *cmd,
                                    const vshCmdOpt *opt);
-
-# define VSH_BYID     (1 << 1)
-# define VSH_BYUUID   (1 << 2)
-# define VSH_BYNAME   (1 << 3)
-# define VSH_BYMAC    (1 << 4)
-
-virDomainPtr vshCommandOptDomainBy(vshControl *ctl, const vshCmd *cmd,
-                                   const char **name, int flag);
-
-/* default is lookup by Id, Name and UUID */
-# define vshCommandOptDomain(_ctl, _cmd, _name)                      \
-    vshCommandOptDomainBy(_ctl, _cmd, _name, VSH_BYID|VSH_BYUUID|VSH_BYNAME)
+bool vshCmdHasOption(vshControl *ctl, const vshCmd *cmd, const char *optname);

 void vshPrintExtra(vshControl *ctl, const char *format, ...)
     ATTRIBUTE_FMT_PRINTF(2, 3);
@@ -309,6 +298,7 @@ void vshDebug(vshControl *ctl, int level, const char *format, ...)

 /* User visible sort, so we want locale-specific case comparison.  */
 # define vshStrcasecmp(S1, S2) strcasecmp(S1, S2)
+int vshNameSorter(const void *a, const void *b);

 int vshDomainState(vshControl *ctl, virDomainPtr dom, int *reason);
 bool vshConnectionUsability(vshControl *ctl, virConnectPtr conn);
-- 
1.7.11.4




More information about the libvir-list mailing list