xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Wei Liu <wei.liu2@citrix.com>
To: Xen-devel <xen-devel@lists.xenproject.org>
Cc: Wei Liu <wei.liu2@citrix.com>, Ian Jackson <Ian.Jackson@eu.citrix.com>
Subject: [PATCH 07/29] xl: lift a bunch of macros to xl_utils.h
Date: Fri, 24 Feb 2017 16:12:52 +0000	[thread overview]
Message-ID: <20170224161314.22154-8-wei.liu2@citrix.com> (raw)
In-Reply-To: <20170224161314.22154-1-wei.liu2@citrix.com>

We're going to split xl_cmdimpl.c into multiple files. Lift the commonly
used macros to xl_utils.h.

Signed-off-by: Wei Liu <wei.liu2@citrix.com>
---
 tools/xl/xl_cmdimpl.c | 104 +---------------------------------------
 tools/xl/xl_utils.h   | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 130 insertions(+), 103 deletions(-)
 create mode 100644 tools/xl/xl_utils.h

diff --git a/tools/xl/xl_cmdimpl.c b/tools/xl/xl_cmdimpl.c
index 1d7cf8ffa8..d9d947827d 100644
--- a/tools/xl/xl_cmdimpl.c
+++ b/tools/xl/xl_cmdimpl.c
@@ -39,42 +39,7 @@
 #include <libxl_json.h>
 #include <libxlutil.h>
 #include "xl.h"
-
-/* For calls which return an errno on failure */
-#define CHK_ERRNOVAL( call ) ({                                         \
-        int chk_errnoval = (call);                                      \
-        if (chk_errnoval < 0)                                           \
-            abort();                                                    \
-        else if (chk_errnoval > 0) {                                    \
-            fprintf(stderr,"xl: fatal error: %s:%d: %s: %s\n",          \
-                    __FILE__,__LINE__, strerror(chk_errnoval), #call);  \
-            exit(EXIT_FAILURE);                                         \
-        }                                                               \
-    })
-
-/* For calls which return -1 and set errno on failure */
-#define CHK_SYSCALL( call ) ({                                          \
-        if ((call) == -1) {                                             \
-            fprintf(stderr,"xl: fatal error: %s:%d: %s: %s\n",          \
-                    __FILE__,__LINE__, strerror(errno), #call);         \
-            exit(EXIT_FAILURE);                                         \
-        }                                                               \
-    })
-
-#define MUST( call ) ({                                                 \
-        int must_rc = (call);                                           \
-        if (must_rc < 0) {                                                  \
-            fprintf(stderr,"xl: fatal error: %s:%d, rc=%d: %s\n",       \
-                    __FILE__,__LINE__, must_rc, #call);                 \
-            exit(EXIT_FAILURE);                                         \
-        }                                                               \
-    })
-
-#define STR_HAS_PREFIX( a, b )  \
-    ( strncmp(a, b, strlen(b)) == 0 )
-#define STR_SKIP_PREFIX( a, b ) \
-    ( STR_HAS_PREFIX(a, b) ? ((a) += strlen(b), 1) : 0 )
-
+#include "xl_utils.h"
 
 int logfile = 2;
 
@@ -83,7 +48,6 @@ libxl_ctx *ctx;
 
 xlchild children[child_max];
 
-#define INVALID_DOMID ~0
 static const char *common_domname;
 static int fd_lock = -1;
 
@@ -349,8 +313,6 @@ static char *xstrdup(const char *x)
 #define ARRAY_EXTEND_INIT_NODEVID(array,count,initfn) \
     ARRAY_EXTEND_INIT__CORE((array),(count),(initfn), /* nothing */ )
 
-#define LOG(_f, _a...)   dolog(__FILE__, __LINE__, __func__, _f "\n", ##_a)
-
 static void dolog(const char *file, int line, const char *func, char *fmt, ...)
      __attribute__((format(printf,4,5)));
 
@@ -3332,10 +3294,6 @@ static int64_t parse_mem_size_kb(const char *mem)
     return kbytes;
 }
 
-/* Must be last in list */
-#define COMMON_LONG_OPTS {"help", 0, 0, 'h'}, \
-                         {0, 0, 0, 0}
-
 /*
  * Callers should use SWITCH_FOREACH_OPT in preference to calling this
  * directly.
@@ -3378,66 +3336,6 @@ static int def_getopt(int argc, char * const argv[],
     return -1;
 }
 
-/*
- * Wraps def_getopt into a convenient loop+switch to process all
- * arguments. This macro is intended to be called from main_XXX().
- *
- *   SWITCH_FOREACH_OPT(int *opt, "OPTS",
- *                      const struct option *longopts,
- *                      const char *commandname,
- *                      int num_opts_req) { ...
- *
- * opt:               pointer to an int variable, holds the current option
- *                    during processing.
- * OPTS:              short options, as per getopt_long(3)'s optstring argument.
- *                    do not include "h"; will be provided automatically
- * longopts:          long options, as per getopt_long(3)'s longopts argument.
- *                    May be null.
- * commandname:       name of this command, for usage string.
- * num_required_opts: number of non-option command line parameters
- *                    which are required.
- *
- * In addition the calling context is expected to contain variables
- * "argc" and "argv" in the conventional C-style:
- *   main(int argc, char **argv)
- * manner.
- *
- * Callers should treat SWITCH_FOREACH_OPT as they would a switch
- * statement over the value of `opt`. Each option given in `opts` (or
- * `lopts`) should be handled by a case statement as if it were inside
- * a switch statement.
- *
- * In addition to the options provided in opts the macro will handle
- * the "help" option and enforce a minimum number of non-option
- * command line pearameters as follows:
- *  -- if the user passes a -h or --help option. help will be printed,
- *     and the macro will cause the process to exit with code 0.
- *  -- if the user does not provided `num_required_opts` non-option
- *     arguments, the macro will cause the process to exit with code 2.
- *
- * Example:
- *
- * int main_foo(int argc, char **argv) {
- *     int opt;
- *
- *     SWITCH_FOREACH_OPT(opt, "blah", NULL, "foo", 0) {
- *      case 'b':
- *          ... handle b option...
- *          break;
- *      case 'l':
- *          ... handle l option ...
- *          break;
- *      case etc etc...
- *      }
- *      ... do something useful with the options ...
- * }
- */
-#define SWITCH_FOREACH_OPT(opt, opts, longopts,                         \
-                           commandname, num_required_opts)              \
-    while (((opt) = def_getopt(argc, argv, "h" opts, (longopts),          \
-                                (commandname), (num_required_opts))) != -1) \
-        switch (opt)
-
 static int set_memory_max(uint32_t domid, const char *mem)
 {
     int64_t memorykb;
diff --git a/tools/xl/xl_utils.h b/tools/xl/xl_utils.h
new file mode 100644
index 0000000000..ebb9305b7c
--- /dev/null
+++ b/tools/xl/xl_utils.h
@@ -0,0 +1,129 @@
+/*
+ * Copyright 2009-2017 Citrix Ltd and other contributors
+ *
+ * This program 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; version 2.1 only. with the special
+ * exception on linking described in file LICENSE.
+ *
+ * This program 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.
+ */
+
+#ifndef XL_UTILS_H
+#define XL_UTILS_H
+
+/* For calls which return an errno on failure */
+#define CHK_ERRNOVAL( call ) ({                                         \
+        int chk_errnoval = (call);                                      \
+        if (chk_errnoval < 0)                                           \
+            abort();                                                    \
+        else if (chk_errnoval > 0) {                                    \
+            fprintf(stderr,"xl: fatal error: %s:%d: %s: %s\n",          \
+                    __FILE__,__LINE__, strerror(chk_errnoval), #call);  \
+            exit(EXIT_FAILURE);                                         \
+        }                                                               \
+    })
+
+/* For calls which return -1 and set errno on failure */
+#define CHK_SYSCALL( call ) ({                                          \
+        if ((call) == -1) {                                             \
+            fprintf(stderr,"xl: fatal error: %s:%d: %s: %s\n",          \
+                    __FILE__,__LINE__, strerror(errno), #call);         \
+            exit(EXIT_FAILURE);                                         \
+        }                                                               \
+    })
+
+#define MUST( call ) ({                                                 \
+        int must_rc = (call);                                           \
+        if (must_rc < 0) {                                              \
+            fprintf(stderr,"xl: fatal error: %s:%d, rc=%d: %s\n",       \
+                    __FILE__,__LINE__, must_rc, #call);                 \
+            exit(EXIT_FAILURE);                                         \
+        }                                                               \
+    })
+
+#define STR_HAS_PREFIX( a, b )  \
+    ( strncmp(a, b, strlen(b)) == 0 )
+#define STR_SKIP_PREFIX( a, b ) \
+    ( STR_HAS_PREFIX(a, b) ? ((a) += strlen(b), 1) : 0 )
+
+#define INVALID_DOMID ~0
+
+#define LOG(_f, _a...)   dolog(__FILE__, __LINE__, __func__, _f "\n", ##_a)
+
+/*
+ * Wraps def_getopt into a convenient loop+switch to process all
+ * arguments. This macro is intended to be called from main_XXX().
+ *
+ *   SWITCH_FOREACH_OPT(int *opt, "OPTS",
+ *                      const struct option *longopts,
+ *                      const char *commandname,
+ *                      int num_opts_req) { ...
+ *
+ * opt:               pointer to an int variable, holds the current option
+ *                    during processing.
+ * OPTS:              short options, as per getopt_long(3)'s optstring argument.
+ *                    do not include "h"; will be provided automatically
+ * longopts:          long options, as per getopt_long(3)'s longopts argument.
+ *                    May be null.
+ * commandname:       name of this command, for usage string.
+ * num_required_opts: number of non-option command line parameters
+ *                    which are required.
+ *
+ * In addition the calling context is expected to contain variables
+ * "argc" and "argv" in the conventional C-style:
+ *   main(int argc, char **argv)
+ * manner.
+ *
+ * Callers should treat SWITCH_FOREACH_OPT as they would a switch
+ * statement over the value of `opt`. Each option given in `opts` (or
+ * `lopts`) should be handled by a case statement as if it were inside
+ * a switch statement.
+ *
+ * In addition to the options provided in opts the macro will handle
+ * the "help" option and enforce a minimum number of non-option
+ * command line pearameters as follows:
+ *  -- if the user passes a -h or --help option. help will be printed,
+ *     and the macro will cause the process to exit with code 0.
+ *  -- if the user does not provided `num_required_opts` non-option
+ *     arguments, the macro will cause the process to exit with code 2.
+ *
+ * Example:
+ *
+ * int main_foo(int argc, char **argv) {
+ *     int opt;
+ *
+ *     SWITCH_FOREACH_OPT(opt, "blah", NULL, "foo", 0) {
+ *      case 'b':
+ *          ... handle b option...
+ *          break;
+ *      case 'l':
+ *          ... handle l option ...
+ *          break;
+ *      case etc etc...
+ *      }
+ *      ... do something useful with the options ...
+ * }
+ */
+#define SWITCH_FOREACH_OPT(opt, opts, longopts,                         \
+                           commandname, num_required_opts)              \
+    while (((opt) = def_getopt(argc, argv, "h" opts, (longopts),        \
+                               (commandname), (num_required_opts))) != -1) \
+        switch (opt)
+
+/* Must be last in list */
+#define COMMON_LONG_OPTS {"help", 0, 0, 'h'}, \
+                         {0, 0, 0, 0}
+
+#endif /* XL_UTILS_H */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
-- 
2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

  parent reply	other threads:[~2017-02-24 16:13 UTC|newest]

Thread overview: 76+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-24 16:12 [PATCH 00/29] Refactor xl code Wei Liu
2017-02-24 16:12 ` [PATCH 01/29] xl: remove accidentally committed hunk from Makefile Wei Liu
2017-02-27 15:53   ` Ian Jackson
2017-02-24 16:12 ` [PATCH 02/29] xl: update copyright information Wei Liu
2017-02-27 15:54   ` Ian Jackson
2017-02-24 16:12 ` [PATCH 03/29] xl: remove inclusion of libxl_osdeps.h Wei Liu
2017-02-27 15:53   ` Ian Jackson
2017-02-24 16:12 ` [PATCH 04/29] xl: use <> variant to include Xen tools library headers Wei Liu
2017-02-27 15:54   ` Ian Jackson
2017-02-24 16:12 ` [PATCH 05/29] xl: generate _paths.h Wei Liu
2017-02-27 15:54   ` Ian Jackson
2017-02-24 16:12 ` [PATCH 06/29] xl: remove trailing spaces in xl_cmdimpl.c Wei Liu
2017-02-27 15:54   ` Ian Jackson
2017-02-24 16:12 ` Wei Liu [this message]
2017-02-27 15:54   ` [PATCH 07/29] xl: lift a bunch of macros to xl_utils.h Ian Jackson
2017-02-24 16:12 ` [PATCH 08/29] xl: move some helper functions to xl_utils.c Wei Liu
2017-02-27 15:53   ` Ian Jackson
2017-02-27 17:44     ` [PATCH] " Wei Liu
2017-02-28 10:32       ` Ian Jackson
2017-02-24 16:12 ` [PATCH 09/29] xl: split out tmem related code to xl_tmem.c Wei Liu
2017-02-27 15:54   ` Ian Jackson
2017-02-24 16:12 ` [PATCH 10/29] xl: split out xl_parse.[ch] Wei Liu
2017-02-27 15:57   ` Ian Jackson
2017-02-27 17:47     ` [PATCH 1/3] xl: rename cpurange_parse to parse_cpurange Wei Liu
2017-02-27 17:47       ` [PATCH 2/3] xl: introduce a function to get shutdown action name Wei Liu
2017-02-27 18:11         ` Ian Jackson
2017-02-27 17:47       ` [PATCH 3/3] xl: split out xl_parse.[ch] Wei Liu
2017-02-27 18:11         ` Ian Jackson
2017-02-27 18:11       ` [PATCH 1/3] xl: rename cpurange_parse to parse_cpurange Ian Jackson
2017-02-24 16:12 ` [PATCH 11/29] xl: split out cpupool related code Wei Liu
2017-02-27 15:59   ` Ian Jackson
2017-02-24 16:12 ` [PATCH 12/29] xl: split out flask " Wei Liu
2017-02-27 16:01   ` Ian Jackson
2017-02-24 16:12 ` [PATCH 13/29] xl: split out vtpm " Wei Liu
2017-02-27 15:55   ` Ian Jackson
2017-02-24 16:12 ` [PATCH 14/29] xl: split out block " Wei Liu
2017-02-27 15:58   ` Ian Jackson
2017-02-24 16:13 ` [PATCH 15/29] xl: split out network " Wei Liu
2017-02-27 16:01   ` Ian Jackson
2017-02-24 16:13 ` [PATCH 16/29] xl: split out usb " Wei Liu
2017-02-27 16:00   ` Ian Jackson
2017-02-24 16:13 ` [PATCH 17/29] xl: split out scheduler " Wei Liu
2017-02-27 15:57   ` Ian Jackson
2017-02-24 16:13 ` [PATCH 18/29] xl: split out pci " Wei Liu
2017-02-27 16:01   ` Ian Jackson
2017-02-24 16:13 ` [PATCH 19/29] xl: split out vcpu " Wei Liu
2017-02-27 15:59   ` Ian Jackson
2017-02-24 16:13 ` [PATCH 20/29] xl: split out cd " Wei Liu
2017-02-27 15:56   ` Ian Jackson
2017-02-27 16:23     ` Wei Liu
2017-02-27 16:36       ` Ian Jackson
2017-02-27 16:38         ` Wei Liu
2017-02-27 16:44           ` Ian Jackson
2017-02-27 16:45             ` Wei Liu
2017-02-24 16:13 ` [PATCH 21/29] xl: split out memory " Wei Liu
2017-02-27 16:01   ` Ian Jackson
2017-02-24 16:13 ` [PATCH 22/29] xl: split out psr " Wei Liu
2017-02-27 15:55   ` Ian Jackson
2017-02-24 16:13 ` [PATCH 23/29] xl: split out functions to print out information Wei Liu
2017-02-27 16:00   ` Ian Jackson
2017-02-24 16:13 ` [PATCH 24/29] xl: split out vnc and console related code Wei Liu
2017-02-27 15:59   ` Ian Jackson
2017-02-27 17:48     ` [PATCH 1/2] xl: call libxl_vncviewer_exec in main_vncviewer Wei Liu
2017-02-27 17:48       ` [PATCH 2/2] xl: split out vnc and console related code Wei Liu
2017-02-27 18:13         ` Ian Jackson
2017-02-27 18:12       ` [PATCH 1/2] xl: call libxl_vncviewer_exec in main_vncviewer Ian Jackson
2017-02-24 16:13 ` [PATCH 25/29] xl: split out miscellaneous functions Wei Liu
2017-02-27 15:58   ` Ian Jackson
2017-02-24 16:13 ` [PATCH 26/29] xl: split out vm lifecycle control functions Wei Liu
2017-02-27 16:00   ` Ian Jackson
2017-02-24 16:13 ` [PATCH 27/29] xl: split out save/restore related code Wei Liu
2017-02-27 16:01   ` Ian Jackson
2017-02-24 16:13 ` [PATCH 28/29] xl: split out migration " Wei Liu
2017-02-27 15:59   ` Ian Jackson
2017-02-24 16:13 ` [PATCH 29/29] xl: merge xl_cmdimpl.c into xl.c Wei Liu
2017-02-27 15:58   ` Ian Jackson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170224161314.22154-8-wei.liu2@citrix.com \
    --to=wei.liu2@citrix.com \
    --cc=Ian.Jackson@eu.citrix.com \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).