]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virsh: declare more common functions
authorEric Blake <eblake@redhat.com>
Mon, 20 Aug 2012 20:29:27 +0000 (14:29 -0600)
committerEric Blake <eblake@redhat.com>
Tue, 21 Aug 2012 05:56:18 +0000 (23:56 -0600)
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.  Also, since we
use the VSH_BY* flags in more than one function, I improved
how they are used.

* tools/virsh.h (vshNameSorter, vshCmdHasOption): Declare.
(VSH_BYID): Turn into enum.
(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, adjust
signature, and check flags.
* 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
tools/virsh-domain.c
tools/virsh-domain.h
tools/virsh-host.c
tools/virsh-interface.c
tools/virsh-network.c
tools/virsh-nwfilter.c
tools/virsh-secret.c
tools/virsh.c
tools/virsh.h

index 4f00e658677ae528e910fc6abe88f9b54e63228d..44f65adc22d919e27b018528eaa5129fff8ddc21 100644 (file)
@@ -36,6 +36,7 @@
 #include "intprops.h"
 #include "memory.h"
 #include "virmacaddr.h"
+#include "virsh-domain.h"
 #include "xml.h"
 
 static const char *
index e949dcffabb77c07e6070a55c9cd35d0bbbedf0d..6a87d4996dd1ed93ae0f0fdb57592df0263fa93c 100644 (file)
 # define SA_SIGINFO 0
 #endif
 
+virDomainPtr
+vshCommandOptDomainBy(vshControl *ctl, const vshCmd *cmd,
+                      const char **name, unsigned int flags)
+{
+    virDomainPtr dom = NULL;
+    const char *n = NULL;
+    int id;
+    const char *optname = "domain";
+    virCheckFlags(VSH_BYID | VSH_BYUUID | VSH_BYNAME, NULL);
+
+    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 (flags & 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 && (flags & 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 && (flags & 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)
 {
index b1b79304ed1636a88fdd09effaba3ef1f90a59d4..cd51b573a41267e069c3992bdb9f06be41390653 100644 (file)
 
 # include "virsh.h"
 
+virDomainPtr vshCommandOptDomainBy(vshControl *ctl, const vshCmd *cmd,
+                                   const char **name, unsigned int flags);
+
+/* 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 */
index e3cff8ede88797ff997ae2b5e591b9a965689723..3c44969405245848ce0e4fdacc62ced6981d2a10 100644 (file)
@@ -35,6 +35,7 @@
 #include "buf.h"
 #include "memory.h"
 #include "util.h"
+#include "virsh-domain.h"
 #include "xml.h"
 
 /*
index ad080a18a257ae7fbecdd1b9d113c5ece2fed2ea..e43aa91ceb8cf60dd4d4e0dc0f236c7ff9748548 100644 (file)
@@ -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)
index b33e2d61ed55991672cfaf294898873bf1e29d38..b891c910689258137665d767a2e1ec6e8ad33f4c 100644 (file)
@@ -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)
index 501e20dc24deea8870b7ffeec9a5b63ead5787d2..a6ef233f551fcb1a9942c6c8b0c1e2526904f505 100644 (file)
@@ -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)
index 049ead595f5c0d1491ebe9052606f8fdffd7c2ff..6f971dac4dcada4f03bc0ca06181f6448a9cae07 100644 (file)
@@ -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)
index 793a357bd1c399da810d2a3591702c8422db35a7..fe79b7c98db0aac0896007739f74b407e909b496 100644 (file)
@@ -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
  */
index 24d2020f415e89c91c108b004b8513f4521f87d0..818d51546a2a4e9ba5323837a0ae3ac4970caecf 100644 (file)
@@ -286,18 +286,15 @@ 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);
+bool vshCmdHasOption(vshControl *ctl, const vshCmd *cmd, const char *optname);
 
-# 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)
+/* Filter flags for various vshCommandOpt*By() functions */
+typedef enum {
+    VSH_BYID   = (1 << 1),
+    VSH_BYUUID = (1 << 2),
+    VSH_BYNAME = (1 << 3),
+    VSH_BYMAC  = (1 << 4),
+} vshLookupByFlags;
 
 void vshPrintExtra(vshControl *ctl, const char *format, ...)
     ATTRIBUTE_FMT_PRINTF(2, 3);
@@ -309,6 +306,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);