[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Libguestfs] [PATCH nbdkit 2/4] common/protocol: Remove protostrings.sed, use bash+sed instead.



Use a simple bash script to generate the protostrings.c functions.

Remove the extern decls from the nbd-protocol.h file which were used
previously in the generation of this file.  They have been moved to a
new internal header called "protostrings.h".
---
 common/protocol/Makefile.am                   |  8 ++-
 ...tostrings.sed => generate-protostrings.sh} | 56 +++++++++++--------
 common/protocol/nbd-protocol.h                | 10 ----
 common/protocol/protostrings.h                | 51 +++++++++++++++++
 plugins/nbd/nbd-standalone.c                  |  1 +
 server/protocol-handshake-newstyle.c          |  1 +
 server/protocol.c                             |  1 +
 7 files changed, 91 insertions(+), 37 deletions(-)

diff --git a/common/protocol/Makefile.am b/common/protocol/Makefile.am
index 996be26..74c288a 100644
--- a/common/protocol/Makefile.am
+++ b/common/protocol/Makefile.am
@@ -34,13 +34,14 @@ include $(top_srcdir)/common-rules.mk
 EXTRA_DIST = \
 	nbd-protocol.h \
 	protostrings.c \
-	protostrings.sed \
+	generate-protostrings.sh \
 	$(NULL)
 
 noinst_LTLIBRARIES = libprotocol.la
 
 libprotocol_la_SOURCES = \
         protostrings.c \
+	protostrings.h \
 	nbd-protocol.h \
 	$(NULL)
 libprotocol_la_CFLAGS = $(WARNINGS_CFLAGS)
@@ -50,8 +51,9 @@ libprotocol_la_CFLAGS = $(WARNINGS_CFLAGS)
 
 BUILT_SOURCES = protostrings.c
 CLEANFILES += protostrings.c
-protostrings.c: nbd-protocol.h protostrings.sed Makefile
+
+protostrings.c: nbd-protocol.h generate-protostrings.sh Makefile
 	rm -f $@ $ -t
-	$(SED) -n -f $(srcdir)/protostrings.sed < $< > $ -t
+	SED=$(SED) $(srcdir)/generate-protostrings.sh > $ -t
 	mv $ -t $@
 	chmod 0444 $@
diff --git a/common/protocol/protostrings.sed b/common/protocol/generate-protostrings.sh
old mode 100644
new mode 100755
similarity index 61%
rename from common/protocol/protostrings.sed
rename to common/protocol/generate-protostrings.sh
index cb1a76e..d3f5d18
--- a/common/protocol/protostrings.sed
+++ b/common/protocol/generate-protostrings.sh
@@ -1,5 +1,6 @@
+#!/usr/bin/env bash
 # nbdkit
-# Copyright (C) 2018 Red Hat Inc.
+# Copyright (C) 2018-2019 Red Hat Inc.
 #
 # Redistribution and use in source and binary forms, with or without
 # modification, are permitted provided that the following conditions are
@@ -29,30 +30,37 @@
 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 # SUCH DAMAGE.
 
-# Generate the protostrings.c file from nbd-protocol.h.
+# The header.
+cat <<'EOF'
+/* Generated from nbd-protocol.h by generate-protostrings.sh. */
 
-# Prologue.
-1i\
-/* Generated from nbd-protocol.h by protostrings.sed. */\
-\#include "nbd-protocol.h"\
+#include "nbd-protocol.h"
 
+EOF
 
-# Match the precise sections of the source file.
-/^extern const char \*name_of_/,/^$/ {
+declare -A functions=(
+    [global_flag]=NBD_FLAG_FIXED_NEWSTYLE
+    [flag]=NBD_FLAG_HAS_FLAGS
+    [opt]=NBD_OPT_EXPORT_NAME
+    [rep]=NBD_REP_ACK
+    [info]=NBD_INFO_EXPORT
+    [reply]=NBD_REPLY_FLAG_DONE
+    [reply_type]=NBD_REPLY_TYPE_NONE
+    [cmd]=NBD_CMD_READ
+    [cmd_flag]=NBD_CMD_FLAG_FUA
+    [error]=NBD_SUCCESS
+)
 
-  # Convert extern function prototype into a definition.
-  s/extern \(const char \*name_of_.*\) (int);/\1 (int fl) {\
-  switch (fl) {/;
-
-  # Convert #define lines into cases.
-  s/^#define \([_A-Z]*\).*/  case \1: return "\1\";/;
-
-  # Append closing brace.
-  s/^$/  default: return "unknown";\
-  }\
-}/;
-
-  # Print pattern buffer.
-  p;
-
-}
+# Generate each 'const char *name_of_nbd_<fn>'
+for fn in "${!functions[ ]}"; do
+    echo 'extern const char *'
+    echo "name_of_nbd_$fn (int fl)"
+    echo '{'
+    echo '  switch (fl) {'
+    $SED -n "/^#define ${functions[$fn]}/,/^$/p" nbd-protocol.h |
+        $SED 's/^#define \([_A-Z]*\).*/  case \1:\n    return "\1\";/'
+    echo '  default: return "unknown";'
+    echo '  }'
+    echo '}'
+    echo
+done
diff --git a/common/protocol/nbd-protocol.h b/common/protocol/nbd-protocol.h
index da2e0d0..60d35d0 100644
--- a/common/protocol/nbd-protocol.h
+++ b/common/protocol/nbd-protocol.h
@@ -80,12 +80,10 @@ struct fixed_new_option_reply {
 #define NBD_REP_MAGIC UINT64_C(0x3e889045565a9)
 
 /* Global flags. */
-extern const char *name_of_nbd_global_flag (int);
 #define NBD_FLAG_FIXED_NEWSTYLE 1
 #define NBD_FLAG_NO_ZEROES      2
 
 /* Per-export flags. */
-extern const char *name_of_nbd_flag (int);
 #define NBD_FLAG_HAS_FLAGS         (1 << 0)
 #define NBD_FLAG_READ_ONLY         (1 << 1)
 #define NBD_FLAG_SEND_FLUSH        (1 << 2)
@@ -99,7 +97,6 @@ extern const char *name_of_nbd_flag (int);
 #define NBD_FLAG_SEND_FAST_ZERO    (1 << 11)
 
 /* NBD options (new style handshake only). */
-extern const char *name_of_nbd_opt (int);
 #define NBD_OPT_EXPORT_NAME        1
 #define NBD_OPT_ABORT              2
 #define NBD_OPT_LIST               3
@@ -113,7 +110,6 @@ extern const char *name_of_nbd_opt (int);
 #define NBD_REP_ERR(val) (0x80000000 | (val))
 #define NBD_REP_IS_ERR(val) (!!((val) & 0x80000000))
 
-extern const char *name_of_nbd_rep (int);
 #define NBD_REP_ACK          1
 #define NBD_REP_SERVER       2
 #define NBD_REP_INFO         3
@@ -124,7 +120,6 @@ extern const char *name_of_nbd_rep (int);
 #define NBD_REP_ERR_PLATFORM NBD_REP_ERR (4)
 #define NBD_REP_ERR_TLS_REQD NBD_REP_ERR (5)
 
-extern const char *name_of_nbd_info (int);
 #define NBD_INFO_EXPORT      0
 
 /* NBD_INFO_EXPORT reply (follows fixed_new_option_reply). */
@@ -197,14 +192,12 @@ struct structured_reply_error {
 #define NBD_STRUCTURED_REPLY_MAGIC  0x668e33ef
 
 /* Structured reply flags. */
-extern const char *name_of_nbd_reply_flag (int);
 #define NBD_REPLY_FLAG_DONE         (1<<0)
 
 #define NBD_REPLY_TYPE_ERR(val) ((1<<15) | (val))
 #define NBD_REPLY_TYPE_IS_ERR(val) (!!((val) & (1<<15)))
 
 /* Structured reply types. */
-extern const char *name_of_nbd_reply_type (int);
 #define NBD_REPLY_TYPE_NONE         0
 #define NBD_REPLY_TYPE_OFFSET_DATA  1
 #define NBD_REPLY_TYPE_OFFSET_HOLE  2
@@ -213,7 +206,6 @@ extern const char *name_of_nbd_reply_type (int);
 #define NBD_REPLY_TYPE_ERROR_OFFSET NBD_REPLY_TYPE_ERR (2)
 
 /* NBD commands. */
-extern const char *name_of_nbd_cmd (int);
 #define NBD_CMD_READ              0
 #define NBD_CMD_WRITE             1
 #define NBD_CMD_DISC              2 /* Disconnect. */
@@ -223,7 +215,6 @@ extern const char *name_of_nbd_cmd (int);
 #define NBD_CMD_WRITE_ZEROES      6
 #define NBD_CMD_BLOCK_STATUS      7
 
-extern const char *name_of_nbd_cmd_flag (int);
 #define NBD_CMD_FLAG_FUA       (1<<0)
 #define NBD_CMD_FLAG_NO_HOLE   (1<<1)
 #define NBD_CMD_FLAG_DF        (1<<2)
@@ -233,7 +224,6 @@ extern const char *name_of_nbd_cmd_flag (int);
 /* Error codes (previously errno).
  * See http://git.qemu.org/?p=qemu.git;a=commitdiff;h=ca4414804114fd0095b317785bc0b51862e62ebb
  */
-extern const char *name_of_nbd_error (int);
 #define NBD_SUCCESS     0
 #define NBD_EPERM       1
 #define NBD_EIO         5
diff --git a/common/protocol/protostrings.h b/common/protocol/protostrings.h
new file mode 100644
index 0000000..8029652
--- /dev/null
+++ b/common/protocol/protostrings.h
@@ -0,0 +1,51 @@
+/* nbdkit
+ * Copyright (C) 2018-2019 Red Hat Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef NBDKIT_PROTOSTRINGS_H
+#define NBDKIT_PROTOSTRINGS_H
+
+/* The corresponding functions are generated by
+ * generate-protostrings.sh from the nbd-protocol.h header file.
+ */
+
+extern const char *name_of_nbd_global_flag (int);
+extern const char *name_of_nbd_flag (int);
+extern const char *name_of_nbd_opt (int);
+extern const char *name_of_nbd_rep (int);
+extern const char *name_of_nbd_info (int);
+extern const char *name_of_nbd_reply_flag (int);
+extern const char *name_of_nbd_reply_type (int);
+extern const char *name_of_nbd_cmd (int);
+extern const char *name_of_nbd_cmd_flag (int);
+extern const char *name_of_nbd_error (int);
+
+#endif /* NBDKIT_PROTOSTRINGS_H */
diff --git a/plugins/nbd/nbd-standalone.c b/plugins/nbd/nbd-standalone.c
index a6779a4..1789e39 100644
--- a/plugins/nbd/nbd-standalone.c
+++ b/plugins/nbd/nbd-standalone.c
@@ -54,6 +54,7 @@
 
 #include <nbdkit-plugin.h>
 #include "nbd-protocol.h"
+#include "protostrings.h"
 #include "byte-swapping.h"
 #include "cleanup.h"
 
diff --git a/server/protocol-handshake-newstyle.c b/server/protocol-handshake-newstyle.c
index 866360b..104796a 100644
--- a/server/protocol-handshake-newstyle.c
+++ b/server/protocol-handshake-newstyle.c
@@ -42,6 +42,7 @@
 #include "internal.h"
 #include "byte-swapping.h"
 #include "nbd-protocol.h"
+#include "protostrings.h"
 
 /* Maximum number of client options we allow before giving up. */
 #define MAX_NR_OPTIONS 32
diff --git a/server/protocol.c b/server/protocol.c
index 498db1d..8df5ed5 100644
--- a/server/protocol.c
+++ b/server/protocol.c
@@ -46,6 +46,7 @@
 #include "byte-swapping.h"
 #include "minmax.h"
 #include "nbd-protocol.h"
+#include "protostrings.h"
 
 static bool
 validate_request (struct connection *conn,
-- 
2.23.0


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]