[libvirt] [PATCH 2/4] virsh: split out virsh.h

Eric Blake eblake at redhat.com
Sat Aug 18 04:38:22 UTC 2012


Having one .c file include another does not give any compilation
benefits; move towards modular .o files by first splitting out
reused declarations into a new virsh.h.  This patch doesn't try
very hard to see which functions are used or not, to make it
easier to review the file split.  Future patches can further trim
the header to be smaller.

* tools/Makefile.am (virsh_SOURCES): List new file, and prepare
for others.
* tools/virsh.c: Split declarations...
* tools/virsh.h: ...into new file, and make several functions
non-static.
* tools/virsh-domain-monitor.c (vshGetDomainDescription): Make
non-static.
---
 tools/Makefile.am            |  15 +-
 tools/virsh-domain-monitor.c |   2 +-
 tools/virsh.c                | 407 +++++--------------------------------------
 tools/virsh.h                | 370 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 431 insertions(+), 363 deletions(-)
 create mode 100644 tools/virsh.h

diff --git a/tools/Makefile.am b/tools/Makefile.am
index 7c048b2..52a8699 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -105,7 +105,20 @@ virt_host_validate_CFLAGS = \

 virsh_SOURCES =							\
 		console.c console.h				\
-		virsh.c
+		virsh.c virsh.h					\
+		$(NULL)
+#		virsh-domain.c virsh-domain.h			\
+#		virsh-domain-monitor.c virsh-domain-monitor.h	\
+#		virsh-host.c virsh-host.h			\
+#		virsh-interface.c virsh-interface.h		\
+#		virsh-network.c virsh-network.h			\
+#		virsh-nodedev.c virsh-nodedev.h			\
+#		virsh-nwfilter.c virsh-nwfilter.h		\
+#		virsh-pool.c virsh-pool.h			\
+#		virsh-secret.c virsh-secret.h			\
+#		virsh-snapshot.c virsh-snapshot.h		\
+#		virsh-volume.c virsh-volume.h			\
+#

 virsh_LDFLAGS = $(WARN_LDFLAGS) $(COVERAGE_LDFLAGS)
 virsh_LDADD =							\
diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 1272a05..52e44c9 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -43,7 +43,7 @@ vshDomainIOErrorToString(int error)
 }

 /* extract description or title from domain xml */
-static char *
+char *
 vshGetDomainDescription(vshControl *ctl, virDomainPtr dom, bool title,
                         unsigned int flags)
 {
diff --git a/tools/virsh.c b/tools/virsh.c
index 3be2f99..610236b 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -23,6 +23,7 @@
  */

 #include <config.h>
+#include "virsh.h"

 #include <stdio.h>
 #include <stdlib.h>
@@ -79,322 +80,12 @@

 static char *progname;

-#define VIRSH_MAX_XML_FILE 10*1024*1024
-
-#define VSH_PROMPT_RW    "virsh # "
-#define VSH_PROMPT_RO    "virsh > "
-
-#define VIR_FROM_THIS VIR_FROM_NONE
-
-#define GETTIMEOFDAY(T) gettimeofday(T, NULL)
-#define DIFF_MSEC(T, U) \
-        ((((int) ((T)->tv_sec - (U)->tv_sec)) * 1000000.0 + \
-          ((int) ((T)->tv_usec - (U)->tv_usec))) / 1000.0)
-
-/* Default escape char Ctrl-] as per telnet */
-#define CTRL_CLOSE_BRACKET "^]"
-
-/**
- * The log configuration
- */
-#define MSG_BUFFER    4096
-#define SIGN_NAME     "virsh"
-#define DIR_MODE      (S_IWUSR | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)  /* 0755 */
-#define FILE_MODE     (S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH)                                /* 0644 */
-#define LOCK_MODE     (S_IWUSR | S_IRUSR)                                                    /* 0600 */
-#define LVL_DEBUG     "DEBUG"
-#define LVL_INFO      "INFO"
-#define LVL_NOTICE    "NOTICE"
-#define LVL_WARNING   "WARNING"
-#define LVL_ERROR     "ERROR"
-
-/**
- * vshErrorLevel:
- *
- * Indicates the level of a log message
- */
-typedef enum {
-    VSH_ERR_DEBUG = 0,
-    VSH_ERR_INFO,
-    VSH_ERR_NOTICE,
-    VSH_ERR_WARNING,
-    VSH_ERR_ERROR
-} vshErrorLevel;
-
-#define VSH_DEBUG_DEFAULT VSH_ERR_ERROR
-
-/*
- * virsh command line grammar:
- *
- *    command_line    =     <command>\n | <command>; <command>; ...
- *
- *    command         =    <keyword> <option> [--] <data>
- *
- *    option          =     <bool_option> | <int_option> | <string_option>
- *    data            =     <string>
- *
- *    bool_option     =     --optionname
- *    int_option      =     --optionname <number> | --optionname=<number>
- *    string_option   =     --optionname <string> | --optionname=<string>
- *
- *    keyword         =     [a-zA-Z][a-zA-Z-]*
- *    number          =     [0-9]+
- *    string          =     ('[^']*'|"([^\\"]|\\.)*"|([^ \t\n\\'"]|\\.))+
- *
- */
-
-/*
- * vshCmdOptType - command option type
- */
-typedef enum {
-    VSH_OT_BOOL,     /* optional boolean option */
-    VSH_OT_STRING,   /* optional string option */
-    VSH_OT_INT,      /* optional or mandatory int option */
-    VSH_OT_DATA,     /* string data (as non-option) */
-    VSH_OT_ARGV,     /* remaining arguments */
-    VSH_OT_ALIAS,    /* alternate spelling for a later argument */
-} vshCmdOptType;
-
-/*
- * Command group types
- */
-#define VSH_CMD_GRP_DOM_MANAGEMENT   "Domain Management"
-#define VSH_CMD_GRP_DOM_MONITORING   "Domain Monitoring"
-#define VSH_CMD_GRP_STORAGE_POOL     "Storage Pool"
-#define VSH_CMD_GRP_STORAGE_VOL      "Storage Volume"
-#define VSH_CMD_GRP_NETWORK          "Networking"
-#define VSH_CMD_GRP_NODEDEV          "Node Device"
-#define VSH_CMD_GRP_IFACE            "Interface"
-#define VSH_CMD_GRP_NWFILTER         "Network Filter"
-#define VSH_CMD_GRP_SECRET           "Secret"
-#define VSH_CMD_GRP_SNAPSHOT         "Snapshot"
-#define VSH_CMD_GRP_HOST_AND_HV      "Host and Hypervisor"
-#define VSH_CMD_GRP_VIRSH            "Virsh itself"
-
-/*
- * Command Option Flags
- */
-enum {
-    VSH_OFLAG_NONE     = 0,        /* without flags */
-    VSH_OFLAG_REQ      = (1 << 0), /* option required */
-    VSH_OFLAG_EMPTY_OK = (1 << 1), /* empty string option allowed */
-    VSH_OFLAG_REQ_OPT  = (1 << 2), /* --optionname required */
-};
-
-/* dummy */
-typedef struct __vshControl vshControl;
-typedef struct __vshCmd vshCmd;
-
-/*
- * vshCmdInfo -- name/value pair for information about command
- *
- * Commands should have at least the following names:
- * "name" - command name
- * "desc" - description of command, or empty string
- */
-typedef struct {
-    const char *name;           /* name of information, or NULL for list end */
-    const char *data;           /* non-NULL information */
-} vshCmdInfo;
-
-/*
- * vshCmdOptDef - command option definition
- */
-typedef struct {
-    const char *name;           /* the name of option, or NULL for list end */
-    vshCmdOptType type;         /* option type */
-    unsigned int flags;         /* flags */
-    const char *help;           /* non-NULL help string; or for VSH_OT_ALIAS
-                                 * the name of a later public option */
-} vshCmdOptDef;
-
-/*
- * vshCmdOpt - command options
- *
- * After parsing a command, all arguments to the command have been
- * collected into a list of these objects.
- */
-typedef struct vshCmdOpt {
-    const vshCmdOptDef *def;    /* non-NULL pointer to option definition */
-    char *data;                 /* allocated data, or NULL for bool option */
-    struct vshCmdOpt *next;
-} vshCmdOpt;
-
-/*
- * Command Usage Flags
- */
-enum {
-    VSH_CMD_FLAG_NOCONNECT = (1 << 0),  /* no prior connection needed */
-    VSH_CMD_FLAG_ALIAS     = (1 << 1),  /* command is an alias */
-};
-
-/*
- * vshCmdDef - command definition
- */
-typedef struct {
-    const char *name;           /* name of command, or NULL for list end */
-    bool (*handler) (vshControl *, const vshCmd *);    /* command handler */
-    const vshCmdOptDef *opts;   /* definition of command options */
-    const vshCmdInfo *info;     /* details about command */
-    unsigned int flags;         /* bitwise OR of VSH_CMD_FLAG */
-} vshCmdDef;
-
-/*
- * vshCmd - parsed command
- */
-typedef struct __vshCmd {
-    const vshCmdDef *def;       /* command definition */
-    vshCmdOpt *opts;            /* list of command arguments */
-    struct __vshCmd *next;      /* next command */
-} __vshCmd;
-
-/*
- * vshControl
- */
-typedef struct __vshControl {
-    char *name;                 /* connection name */
-    virConnectPtr conn;         /* connection to hypervisor (MAY BE NULL) */
-    vshCmd *cmd;                /* the current command */
-    char *cmdstr;               /* string with command */
-    bool imode;                 /* interactive mode? */
-    bool quiet;                 /* quiet mode */
-    int debug;                  /* print debug messages? */
-    bool timing;                /* print timing info? */
-    bool readonly;              /* connect readonly (first time only, not
-                                 * during explicit connect command)
-                                 */
-    char *logfile;              /* log file name */
-    int log_fd;                 /* log file descriptor */
-    char *historydir;           /* readline history directory name */
-    char *historyfile;          /* readline history file name */
-    bool useGetInfo;            /* must use virDomainGetInfo, since
-                                   virDomainGetState is not supported */
-    bool useSnapshotOld;        /* cannot use virDomainSnapshotGetParent or
-                                   virDomainSnapshotNumChildren */
-    virThread eventLoop;
-    virMutex lock;
-    bool eventLoopStarted;
-    bool quit;
-
-    const char *escapeChar;     /* String representation of
-                                   console escape character */
-} __vshControl;
-
-typedef struct vshCmdGrp {
-    const char *name;    /* name of group, or NULL for list end */
-    const char *keyword; /* help keyword */
-    const vshCmdDef *commands;
-} vshCmdGrp;
-
 static const vshCmdGrp cmdGroups[];

-static void vshError(vshControl *ctl, const char *format, ...)
-    ATTRIBUTE_FMT_PRINTF(2, 3);
-static bool vshInit(vshControl *ctl);
-static bool vshDeinit(vshControl *ctl);
-static void vshUsage(void);
-static void vshOpenLogFile(vshControl *ctl);
-static void vshOutputLogFile(vshControl *ctl, int log_level, const char *format, va_list ap)
-    ATTRIBUTE_FMT_PRINTF(3, 0);
-static void vshCloseLogFile(vshControl *ctl);
-
-static bool vshParseArgv(vshControl *ctl, int argc, char **argv);
-
-static const char *vshCmddefGetInfo(const vshCmdDef *cmd, const char *info);
-static const vshCmdDef *vshCmddefSearch(const char *cmdname);
-static bool vshCmddefHelp(vshControl *ctl, const char *name);
-static const vshCmdGrp *vshCmdGrpSearch(const char *grpname);
-static bool vshCmdGrpHelp(vshControl *ctl, const char *name);
-
-static int vshCommandOpt(const vshCmd *cmd, const char *name, vshCmdOpt **opt)
-    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
-    ATTRIBUTE_RETURN_CHECK;
-static int vshCommandOptInt(const vshCmd *cmd, const char *name, int *value)
-    ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
-static int vshCommandOptUInt(const vshCmd *cmd, const char *name,
-                             unsigned int *value)
-    ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
-static int vshCommandOptUL(const vshCmd *cmd, const char *name,
-                           unsigned long *value)
-    ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
-static int vshCommandOptString(const vshCmd *cmd, const char *name,
-                               const char **value)
-    ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
-static int vshCommandOptLongLong(const vshCmd *cmd, const char *name,
-                                 long long *value)
-    ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
-static int vshCommandOptULongLong(const vshCmd *cmd, const char *name,
-                                  unsigned long long *value)
-    ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
-static int vshCommandOptScaledInt(const vshCmd *cmd, const char *name,
-                                  unsigned long long *value, int scale,
-                                  unsigned long long max)
-    ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
-static bool vshCommandOptBool(const vshCmd *cmd, const char *name);
-static const vshCmdOpt *vshCommandOptArgv(const vshCmd *cmd,
-                                          const vshCmdOpt *opt);
-static char *vshGetDomainDescription(vshControl *ctl, virDomainPtr dom,
-                                     bool title, unsigned int flags)
-    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
-
-#define VSH_BYID     (1 << 1)
-#define VSH_BYUUID   (1 << 2)
-#define VSH_BYNAME   (1 << 3)
-#define VSH_BYMAC    (1 << 4)
-
-static 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)
-
-static void vshPrintExtra(vshControl *ctl, const char *format, ...)
-    ATTRIBUTE_FMT_PRINTF(2, 3);
-static void vshDebug(vshControl *ctl, int level, const char *format, ...)
-    ATTRIBUTE_FMT_PRINTF(3, 4);
-
-/* XXX: add batch support */
-#define vshPrint(_ctl, ...)   vshPrintExtra(NULL, __VA_ARGS__)
-
-/* User visible sort, so we want locale-specific case comparison.  */
-#define vshStrcasecmp(S1, S2) strcasecmp(S1, S2)
-
-static int vshDomainState(vshControl *ctl, virDomainPtr dom, int *reason);
-static bool vshConnectionUsability(vshControl *ctl, virConnectPtr conn);
-static virTypedParameterPtr vshFindTypedParamByName(const char *name,
-                                                    virTypedParameterPtr list,
-                                                    int count);
-static char *vshGetTypedParamValue(vshControl *ctl, virTypedParameterPtr item)
-    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
-
-static char *editWriteToTempFile(vshControl *ctl, const char *doc);
-static int   editFile(vshControl *ctl, const char *filename);
-static char *editReadBackFile(vshControl *ctl, const char *filename);
-
-/* Typedefs, function prototypes for job progress reporting.
- * There are used by some long lingering commands like
- * migrate, dump, save, managedsave.
- */
-typedef struct __vshCtrlData {
-    vshControl *ctl;
-    const vshCmd *cmd;
-    int writefd;
-} vshCtrlData;
-
-typedef void (*jobWatchTimeoutFunc) (vshControl *ctl, virDomainPtr dom,
-                                     void *opaque);
-
-static void *_vshMalloc(vshControl *ctl, size_t sz, const char *filename, int line);
-#define vshMalloc(_ctl, _sz)    _vshMalloc(_ctl, _sz, __FILE__, __LINE__)
-
-static void *_vshCalloc(vshControl *ctl, size_t nmemb, size_t sz, const char *filename, int line);
-#define vshCalloc(_ctl, _nmemb, _sz)    _vshCalloc(_ctl, _nmemb, _sz, __FILE__, __LINE__)
-
-static char *_vshStrdup(vshControl *ctl, const char *s, const char *filename, int line);
-#define vshStrdup(_ctl, _s)    _vshStrdup(_ctl, _s, __FILE__, __LINE__)
+/* Bypass header poison */
+#undef strdup

-static void *
+void *
 _vshMalloc(vshControl *ctl, size_t size, const char *filename, int line)
 {
     char *x;
@@ -406,8 +97,9 @@ _vshMalloc(vshControl *ctl, size_t size, const char *filename, int line)
     exit(EXIT_FAILURE);
 }

-static void *
-_vshCalloc(vshControl *ctl, size_t nmemb, size_t size, const char *filename, int line)
+void *
+_vshCalloc(vshControl *ctl, size_t nmemb, size_t size, const char *filename,
+           int line)
 {
     char *x;

@@ -419,7 +111,7 @@ _vshCalloc(vshControl *ctl, size_t nmemb, size_t size, const char *filename, int
     exit(EXIT_FAILURE);
 }

-static char *
+char *
 _vshStrdup(vshControl *ctl, const char *s, const char *filename, int line)
 {
     char *x;
@@ -434,13 +126,6 @@ _vshStrdup(vshControl *ctl, const char *s, const char *filename, int line)
 }

 /* Poison the raw allocating identifiers in favor of our vsh variants.  */
-#undef malloc
-#undef calloc
-#undef realloc
-#undef strdup
-#define malloc use_vshMalloc_instead_of_malloc
-#define calloc use_vshCalloc_instead_of_calloc
-#define realloc use_vshRealloc_instead_of_realloc
 #define strdup use_vshStrdup_instead_of_strdup

 static int
@@ -474,7 +159,7 @@ prettyCapacity(unsigned long long val,
 }


-static virErrorPtr last_error;
+virErrorPtr last_error;

 /*
  * Quieten libvirt until we're done with the command.
@@ -830,7 +515,7 @@ vshTreePrint(vshControl *ctl, vshTreeLookup lookup, void *opaque,
 }

 /* Common code for the edit / net-edit / pool-edit functions which follow. */
-static char *
+char *
 editWriteToTempFile(vshControl *ctl, const char *doc)
 {
     char *ret;
@@ -875,7 +560,7 @@ editWriteToTempFile(vshControl *ctl, const char *doc)
 #define ACCEPTED_CHARS \
   "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-/_.:@"

-static int
+int
 editFile(vshControl *ctl, const char *filename)
 {
     const char *editor;
@@ -927,7 +612,7 @@ cleanup:
     return ret;
 }

-static char *
+char *
 editReadBackFile(vshControl *ctl, const char *filename)
 {
     char *ret;
@@ -1105,7 +790,7 @@ cmdQuit(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
  * Utils for work with command definition
  * ---------------
  */
-static const char *
+const char *
 vshCmddefGetInfo(const vshCmdDef * cmd, const char *name)
 {
     const vshCmdInfo *info;
@@ -1258,7 +943,7 @@ vshCommandCheckOpts(vshControl *ctl, const vshCmd *cmd, uint32_t opts_required,
     return -1;
 }

-static const vshCmdDef *
+const vshCmdDef *
 vshCmddefSearch(const char *cmdname)
 {
     const vshCmdGrp *g;
@@ -1274,7 +959,7 @@ vshCmddefSearch(const char *cmdname)
     return NULL;
 }

-static const vshCmdGrp *
+const vshCmdGrp *
 vshCmdGrpSearch(const char *grpname)
 {
     const vshCmdGrp *g;
@@ -1287,7 +972,7 @@ vshCmdGrpSearch(const char *grpname)
     return NULL;
 }

-static bool
+bool
 vshCmdGrpHelp(vshControl *ctl, const char *grpname)
 {
     const vshCmdGrp *grp = vshCmdGrpSearch(grpname);
@@ -1309,7 +994,7 @@ vshCmdGrpHelp(vshControl *ctl, const char *grpname)
     return true;
 }

-static bool
+bool
 vshCmddefHelp(vshControl *ctl, const char *cmdname)
 {
     const vshCmdDef *def = vshCmddefSearch(cmdname);
@@ -1479,7 +1164,7 @@ vshCommandFree(vshCmd *cmd)
  * the option is required but not present, and -2 if NAME is not valid
  * (-2 indicates a programming error).  No error messages are issued.
  */
-static int
+int
 vshCommandOpt(const vshCmd *cmd, const char *name, vshCmdOpt **opt)
 {
     vshCmdOpt *candidate = cmd->opts;
@@ -1519,7 +1204,7 @@ vshCommandOpt(const vshCmd *cmd, const char *name, vshCmdOpt **opt)
  * 0 if option not found and not required (@value untouched)
  * <0 in all other cases (@value untouched)
  */
-static int
+int
 vshCommandOptInt(const vshCmd *cmd, const char *name, int *value)
 {
     vshCmdOpt *arg;
@@ -1549,7 +1234,7 @@ vshCommandOptInt(const vshCmd *cmd, const char *name, int *value)
  * Convert option to unsigned int
  * See vshCommandOptInt()
  */
-static int
+int
 vshCommandOptUInt(const vshCmd *cmd, const char *name, unsigned int *value)
 {
     vshCmdOpt *arg;
@@ -1579,7 +1264,7 @@ vshCommandOptUInt(const vshCmd *cmd, const char *name, unsigned int *value)
  * Convert option to unsigned long
  * See vshCommandOptInt()
  */
-static int
+int
 vshCommandOptUL(const vshCmd *cmd, const char *name, unsigned long *value)
 {
     vshCmdOpt *arg;
@@ -1611,7 +1296,7 @@ vshCommandOptUL(const vshCmd *cmd, const char *name, unsigned long *value)
  * 0 if option not found and not required (@value untouched)
  * <0 in all other cases (@value untouched)
  */
-static int
+int
 vshCommandOptString(const vshCmd *cmd, const char *name, const char **value)
 {
     vshCmdOpt *arg;
@@ -1642,7 +1327,7 @@ vshCommandOptString(const vshCmd *cmd, const char *name, const char **value)
  * Returns option as long long
  * See vshCommandOptInt()
  */
-static int
+int
 vshCommandOptLongLong(const vshCmd *cmd, const char *name,
                       long long *value)
 {
@@ -1672,7 +1357,7 @@ vshCommandOptLongLong(const vshCmd *cmd, const char *name,
  * Returns option as long long
  * See vshCommandOptInt()
  */
-static int
+int
 vshCommandOptULongLong(const vshCmd *cmd, const char *name,
                        unsigned long long *value)
 {
@@ -1705,7 +1390,7 @@ vshCommandOptULongLong(const vshCmd *cmd, const char *name,
  * Returns option as long long, scaled according to suffix
  * See vshCommandOptInt()
  */
-static int
+int
 vshCommandOptScaledInt(const vshCmd *cmd, const char *name,
                        unsigned long long *value, int scale,
                        unsigned long long max)
@@ -1734,7 +1419,7 @@ vshCommandOptScaledInt(const vshCmd *cmd, const char *name,
  * name is legal; so that this can be used to probe whether a data
  * option is present without actually using that data.
  */
-static bool
+bool
 vshCommandOptBool(const vshCmd *cmd, const char *name)
 {
     vshCmdOpt *dummy;
@@ -1753,7 +1438,7 @@ vshCommandOptBool(const vshCmd *cmd, const char *name)
  * Requires that a VSH_OT_ARGV option be last in the
  * list of supported options in CMD->def->opts.
  */
-static const vshCmdOpt *
+const vshCmdOpt *
 vshCommandOptArgv(const vshCmd *cmd, const vshCmdOpt *opt)
 {
     opt = opt ? opt->next : cmd->opts;
@@ -1790,7 +1475,7 @@ cmd_has_option(vshControl *ctl, const vshCmd *cmd, const char *optname)
     return found;
 }

-static virDomainPtr
+virDomainPtr
 vshCommandOptDomainBy(vshControl *ctl, const vshCmd *cmd,
                       const char **name, int flag)
 {
@@ -2218,7 +1903,7 @@ vshCommandStringParse(vshControl *ctl, char *cmdstr)
  * Misc utils
  * ---------------
  */
-static int
+int
 vshDomainState(vshControl *ctl, virDomainPtr dom, int *reason)
 {
     virDomainInfo info;
@@ -2248,7 +1933,7 @@ vshDomainState(vshControl *ctl, virDomainPtr dom, int *reason)

 /* Return a non-NULL string representation of a typed parameter; exit
  * if we are out of memory.  */
-static char *
+char *
 vshGetTypedParamValue(vshControl *ctl, virTypedParameterPtr item)
 {
     int ret = 0;
@@ -2294,7 +1979,7 @@ vshGetTypedParamValue(vshControl *ctl, virTypedParameterPtr item)
     return str;
 }

-static virTypedParameterPtr
+virTypedParameterPtr
 vshFindTypedParamByName(const char *name, virTypedParameterPtr list, int count)
 {
     int i = count;
@@ -2314,7 +1999,7 @@ vshFindTypedParamByName(const char *name, virTypedParameterPtr list, int count)
     return NULL;
 }

-static bool
+bool
 vshConnectionUsability(vshControl *ctl, virConnectPtr conn)
 {
     /* TODO: use something like virConnectionState() to
@@ -2327,7 +2012,7 @@ vshConnectionUsability(vshControl *ctl, virConnectPtr conn)
     return true;
 }

-static void
+void
 vshDebug(vshControl *ctl, int level, const char *format, ...)
 {
     va_list ap;
@@ -2355,7 +2040,7 @@ vshDebug(vshControl *ctl, int level, const char *format, ...)
     VIR_FREE(str);
 }

-static void
+void
 vshPrintExtra(vshControl *ctl, const char *format, ...)
 {
     va_list ap;
@@ -2376,7 +2061,7 @@ vshPrintExtra(vshControl *ctl, const char *format, ...)
 }


-static void
+void
 vshError(vshControl *ctl, const char *format, ...)
 {
     va_list ap;
@@ -2429,7 +2114,7 @@ vshEventLoop(void *opaque)
 /*
  * Initialize connection.
  */
-static bool
+bool
 vshInit(vshControl *ctl)
 {
     char *debugEnv;
@@ -2500,7 +2185,7 @@ vshInit(vshControl *ctl)
  *
  * Open log file.
  */
-static void
+void
 vshOpenLogFile(vshControl *ctl)
 {
     struct stat st;
@@ -2538,7 +2223,7 @@ vshOpenLogFile(vshControl *ctl)
  *
  * Outputting an error to log file.
  */
-static void
+void
 vshOutputLogFile(vshControl *ctl, int log_level, const char *msg_format,
                  va_list ap)
 {
@@ -2620,7 +2305,7 @@ error:
  *
  * Close log file.
  */
-static void
+void
 vshCloseLogFile(vshControl *ctl)
 {
     /* log file close */
@@ -2865,7 +2550,7 @@ vshDeinitTimer(int timer ATTRIBUTE_UNUSED, void *opaque ATTRIBUTE_UNUSED)
 /*
  * Deinitialize virsh
  */
-static bool
+bool
 vshDeinit(vshControl *ctl)
 {
     vshReadlineDeinit(ctl);
@@ -2904,7 +2589,7 @@ vshDeinit(vshControl *ctl)
 /*
  * Print usage
  */
-static void
+void
 vshUsage(void)
 {
     const vshCmdGrp *grp;
@@ -3095,7 +2780,7 @@ vshAllowedEscapeChar(char c)
  * argv[]:  virsh [options] [command]
  *
  */
-static bool
+bool
 vshParseArgv(vshControl *ctl, int argc, char **argv)
 {
     int arg, len, debug;
@@ -3190,15 +2875,15 @@ vshParseArgv(vshControl *ctl, int argc, char **argv)

 #include "virsh-domain.c"
 #include "virsh-domain-monitor.c"
-#include "virsh-pool.c"
-#include "virsh-volume.c"
+#include "virsh-host.c"
+#include "virsh-interface.c"
 #include "virsh-network.c"
 #include "virsh-nodedev.c"
-#include "virsh-interface.c"
 #include "virsh-nwfilter.c"
+#include "virsh-pool.c"
 #include "virsh-secret.c"
 #include "virsh-snapshot.c"
-#include "virsh-host.c"
+#include "virsh-volume.c"

 static const vshCmdDef virshCmds[] = {
     {"cd", cmdCd, opts_cd, info_cd, VSH_CMD_FLAG_NOCONNECT},
diff --git a/tools/virsh.h b/tools/virsh.h
new file mode 100644
index 0000000..8680a0a
--- /dev/null
+++ b/tools/virsh.h
@@ -0,0 +1,370 @@
+/*
+ * virsh.h: a shell to exercise the libvirt API
+ *
+ * Copyright (C) 2005, 2007-2012 Red Hat, Inc.
+ *
+ * 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/>.
+ *
+ * Daniel Veillard <veillard at redhat.com>
+ * Karel Zak <kzak at redhat.com>
+ * Daniel P. Berrange <berrange at redhat.com>
+ */
+
+#ifndef VIRSH_H
+#define VIRSH_H
+
+# include <stdio.h>
+# include <stdlib.h>
+# include <string.h>
+# include <stdarg.h>
+# include <unistd.h>
+# include <sys/stat.h>
+# include <inttypes.h>
+
+# include "internal.h"
+# include "virterror_internal.h"
+# include "threads.h"
+# include "virnetdevbandwidth.h"
+
+# define VIRSH_MAX_XML_FILE 10*1024*1024
+
+# define VSH_PROMPT_RW    "virsh # "
+# define VSH_PROMPT_RO    "virsh > "
+
+# define VIR_FROM_THIS VIR_FROM_NONE
+
+# define GETTIMEOFDAY(T) gettimeofday(T, NULL)
+# define DIFF_MSEC(T, U) \
+        ((((int) ((T)->tv_sec - (U)->tv_sec)) * 1000000.0 + \
+          ((int) ((T)->tv_usec - (U)->tv_usec))) / 1000.0)
+
+/* Default escape char Ctrl-] as per telnet */
+# define CTRL_CLOSE_BRACKET "^]"
+
+/**
+ * The log configuration
+ */
+# define MSG_BUFFER    4096
+# define SIGN_NAME     "virsh"
+# define DIR_MODE      (S_IWUSR | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)  /* 0755 */
+# define FILE_MODE     (S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH)                                /* 0644 */
+# define LOCK_MODE     (S_IWUSR | S_IRUSR)                                                    /* 0600 */
+# define LVL_DEBUG     "DEBUG"
+# define LVL_INFO      "INFO"
+# define LVL_NOTICE    "NOTICE"
+# define LVL_WARNING   "WARNING"
+# define LVL_ERROR     "ERROR"
+
+/**
+ * vshErrorLevel:
+ *
+ * Indicates the level of a log message
+ */
+typedef enum {
+    VSH_ERR_DEBUG = 0,
+    VSH_ERR_INFO,
+    VSH_ERR_NOTICE,
+    VSH_ERR_WARNING,
+    VSH_ERR_ERROR
+} vshErrorLevel;
+
+# define VSH_DEBUG_DEFAULT VSH_ERR_ERROR
+
+/*
+ * virsh command line grammar:
+ *
+ *    command_line    =     <command>\n | <command>; <command>; ...
+ *
+ *    command         =    <keyword> <option> [--] <data>
+ *
+ *    option          =     <bool_option> | <int_option> | <string_option>
+ *    data            =     <string>
+ *
+ *    bool_option     =     --optionname
+ *    int_option      =     --optionname <number> | --optionname=<number>
+ *    string_option   =     --optionname <string> | --optionname=<string>
+ *
+ *    keyword         =     [a-zA-Z][a-zA-Z-]*
+ *    number          =     [0-9]+
+ *    string          =     ('[^']*'|"([^\\"]|\\.)*"|([^ \t\n\\'"]|\\.))+
+ *
+ */
+
+/*
+ * vshCmdOptType - command option type
+ */
+typedef enum {
+    VSH_OT_BOOL,     /* optional boolean option */
+    VSH_OT_STRING,   /* optional string option */
+    VSH_OT_INT,      /* optional or mandatory int option */
+    VSH_OT_DATA,     /* string data (as non-option) */
+    VSH_OT_ARGV,     /* remaining arguments */
+    VSH_OT_ALIAS,    /* alternate spelling for a later argument */
+} vshCmdOptType;
+
+/*
+ * Command group types
+ */
+# define VSH_CMD_GRP_DOM_MANAGEMENT   "Domain Management"
+# define VSH_CMD_GRP_DOM_MONITORING   "Domain Monitoring"
+# define VSH_CMD_GRP_STORAGE_POOL     "Storage Pool"
+# define VSH_CMD_GRP_STORAGE_VOL      "Storage Volume"
+# define VSH_CMD_GRP_NETWORK          "Networking"
+# define VSH_CMD_GRP_NODEDEV          "Node Device"
+# define VSH_CMD_GRP_IFACE            "Interface"
+# define VSH_CMD_GRP_NWFILTER         "Network Filter"
+# define VSH_CMD_GRP_SECRET           "Secret"
+# define VSH_CMD_GRP_SNAPSHOT         "Snapshot"
+# define VSH_CMD_GRP_HOST_AND_HV      "Host and Hypervisor"
+# define VSH_CMD_GRP_VIRSH            "Virsh itself"
+
+/*
+ * Command Option Flags
+ */
+enum {
+    VSH_OFLAG_NONE     = 0,        /* without flags */
+    VSH_OFLAG_REQ      = (1 << 0), /* option required */
+    VSH_OFLAG_EMPTY_OK = (1 << 1), /* empty string option allowed */
+    VSH_OFLAG_REQ_OPT  = (1 << 2), /* --optionname required */
+};
+
+/* dummy */
+typedef struct __vshControl vshControl;
+typedef struct __vshCmd vshCmd;
+
+/*
+ * vshCmdInfo -- name/value pair for information about command
+ *
+ * Commands should have at least the following names:
+ * "name" - command name
+ * "desc" - description of command, or empty string
+ */
+typedef struct {
+    const char *name;           /* name of information, or NULL for list end */
+    const char *data;           /* non-NULL information */
+} vshCmdInfo;
+
+/*
+ * vshCmdOptDef - command option definition
+ */
+typedef struct {
+    const char *name;           /* the name of option, or NULL for list end */
+    vshCmdOptType type;         /* option type */
+    unsigned int flags;         /* flags */
+    const char *help;           /* non-NULL help string; or for VSH_OT_ALIAS
+                                 * the name of a later public option */
+} vshCmdOptDef;
+
+/*
+ * vshCmdOpt - command options
+ *
+ * After parsing a command, all arguments to the command have been
+ * collected into a list of these objects.
+ */
+typedef struct vshCmdOpt {
+    const vshCmdOptDef *def;    /* non-NULL pointer to option definition */
+    char *data;                 /* allocated data, or NULL for bool option */
+    struct vshCmdOpt *next;
+} vshCmdOpt;
+
+/*
+ * Command Usage Flags
+ */
+enum {
+    VSH_CMD_FLAG_NOCONNECT = (1 << 0),  /* no prior connection needed */
+    VSH_CMD_FLAG_ALIAS     = (1 << 1),  /* command is an alias */
+};
+
+/*
+ * vshCmdDef - command definition
+ */
+typedef struct {
+    const char *name;           /* name of command, or NULL for list end */
+    bool (*handler) (vshControl *, const vshCmd *);    /* command handler */
+    const vshCmdOptDef *opts;   /* definition of command options */
+    const vshCmdInfo *info;     /* details about command */
+    unsigned int flags;         /* bitwise OR of VSH_CMD_FLAG */
+} vshCmdDef;
+
+/*
+ * vshCmd - parsed command
+ */
+typedef struct __vshCmd {
+    const vshCmdDef *def;       /* command definition */
+    vshCmdOpt *opts;            /* list of command arguments */
+    struct __vshCmd *next;      /* next command */
+} __vshCmd;
+
+/*
+ * vshControl
+ */
+typedef struct __vshControl {
+    char *name;                 /* connection name */
+    virConnectPtr conn;         /* connection to hypervisor (MAY BE NULL) */
+    vshCmd *cmd;                /* the current command */
+    char *cmdstr;               /* string with command */
+    bool imode;                 /* interactive mode? */
+    bool quiet;                 /* quiet mode */
+    int debug;                  /* print debug messages? */
+    bool timing;                /* print timing info? */
+    bool readonly;              /* connect readonly (first time only, not
+                                 * during explicit connect command)
+                                 */
+    char *logfile;              /* log file name */
+    int log_fd;                 /* log file descriptor */
+    char *historydir;           /* readline history directory name */
+    char *historyfile;          /* readline history file name */
+    bool useGetInfo;            /* must use virDomainGetInfo, since
+                                   virDomainGetState is not supported */
+    bool useSnapshotOld;        /* cannot use virDomainSnapshotGetParent or
+                                   virDomainSnapshotNumChildren */
+    virThread eventLoop;
+    virMutex lock;
+    bool eventLoopStarted;
+    bool quit;
+
+    const char *escapeChar;     /* String representation of
+                                   console escape character */
+} __vshControl;
+
+typedef struct vshCmdGrp {
+    const char *name;    /* name of group, or NULL for list end */
+    const char *keyword; /* help keyword */
+    const vshCmdDef *commands;
+} vshCmdGrp;
+
+void vshError(vshControl *ctl, const char *format, ...)
+    ATTRIBUTE_FMT_PRINTF(2, 3);
+bool vshInit(vshControl *ctl);
+bool vshDeinit(vshControl *ctl);
+void vshUsage(void);
+void vshOpenLogFile(vshControl *ctl);
+void vshOutputLogFile(vshControl *ctl, int log_level, const char *format,
+                      va_list ap)
+    ATTRIBUTE_FMT_PRINTF(3, 0);
+void vshCloseLogFile(vshControl *ctl);
+
+bool vshParseArgv(vshControl *ctl, int argc, char **argv);
+
+const char *vshCmddefGetInfo(const vshCmdDef *cmd, const char *info);
+const vshCmdDef *vshCmddefSearch(const char *cmdname);
+bool vshCmddefHelp(vshControl *ctl, const char *name);
+const vshCmdGrp *vshCmdGrpSearch(const char *grpname);
+bool vshCmdGrpHelp(vshControl *ctl, const char *name);
+
+int vshCommandOpt(const vshCmd *cmd, const char *name, vshCmdOpt **opt)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
+    ATTRIBUTE_RETURN_CHECK;
+int vshCommandOptInt(const vshCmd *cmd, const char *name, int *value)
+    ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
+int vshCommandOptUInt(const vshCmd *cmd, const char *name,
+                      unsigned int *value)
+    ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
+int vshCommandOptUL(const vshCmd *cmd, const char *name,
+                    unsigned long *value)
+    ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
+int vshCommandOptString(const vshCmd *cmd, const char *name,
+                        const char **value)
+    ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
+int vshCommandOptLongLong(const vshCmd *cmd, const char *name,
+                          long long *value)
+    ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
+int vshCommandOptULongLong(const vshCmd *cmd, const char *name,
+                           unsigned long long *value)
+    ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
+int vshCommandOptScaledInt(const vshCmd *cmd, const char *name,
+                           unsigned long long *value, int scale,
+                           unsigned long long max)
+    ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
+bool vshCommandOptBool(const vshCmd *cmd, const char *name);
+const vshCmdOpt *vshCommandOptArgv(const vshCmd *cmd,
+                                   const vshCmdOpt *opt);
+char *vshGetDomainDescription(vshControl *ctl, virDomainPtr dom,
+                              bool title, unsigned int flags)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
+
+# 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)
+
+void vshPrintExtra(vshControl *ctl, const char *format, ...)
+    ATTRIBUTE_FMT_PRINTF(2, 3);
+void vshDebug(vshControl *ctl, int level, const char *format, ...)
+    ATTRIBUTE_FMT_PRINTF(3, 4);
+
+/* XXX: add batch support */
+# define vshPrint(_ctl, ...)   vshPrintExtra(NULL, __VA_ARGS__)
+
+/* User visible sort, so we want locale-specific case comparison.  */
+# define vshStrcasecmp(S1, S2) strcasecmp(S1, S2)
+
+int vshDomainState(vshControl *ctl, virDomainPtr dom, int *reason);
+bool vshConnectionUsability(vshControl *ctl, virConnectPtr conn);
+virTypedParameterPtr vshFindTypedParamByName(const char *name,
+                                             virTypedParameterPtr list,
+                                             int count);
+char *vshGetTypedParamValue(vshControl *ctl, virTypedParameterPtr item)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+
+char *editWriteToTempFile(vshControl *ctl, const char *doc);
+int   editFile(vshControl *ctl, const char *filename);
+char *editReadBackFile(vshControl *ctl, const char *filename);
+
+/* Typedefs, function prototypes for job progress reporting.
+ * There are used by some long lingering commands like
+ * migrate, dump, save, managedsave.
+ */
+typedef struct __vshCtrlData {
+    vshControl *ctl;
+    const vshCmd *cmd;
+    int writefd;
+} vshCtrlData;
+
+typedef void (*jobWatchTimeoutFunc) (vshControl *ctl, virDomainPtr dom,
+                                     void *opaque);
+
+void *_vshMalloc(vshControl *ctl, size_t sz, const char *filename, int line);
+# define vshMalloc(_ctl, _sz)    _vshMalloc(_ctl, _sz, __FILE__, __LINE__)
+
+void *_vshCalloc(vshControl *ctl, size_t nmemb, size_t sz,
+                 const char *filename, int line);
+# define vshCalloc(_ctl, _nmemb, _sz) \
+    _vshCalloc(_ctl, _nmemb, _sz, __FILE__, __LINE__)
+
+char *_vshStrdup(vshControl *ctl, const char *s, const char *filename,
+                 int line);
+# define vshStrdup(_ctl, _s)    _vshStrdup(_ctl, _s, __FILE__, __LINE__)
+
+/* Poison the raw allocating identifiers in favor of our vsh variants.  */
+# undef malloc
+# undef calloc
+# undef realloc
+# undef strdup
+# define malloc use_vshMalloc_instead_of_malloc
+# define calloc use_vshCalloc_instead_of_calloc
+# define realloc use_vshRealloc_instead_of_realloc
+# define strdup use_vshStrdup_instead_of_strdup
+
+extern virErrorPtr last_error;
+
+#endif /* VIRSH_H */
-- 
1.7.11.2




More information about the libvir-list mailing list