This generates small functions which map from various integer NBD
protocol flags to the string equivalent.
eg:
name_of_nbd_cmd (NBD_CMD_READ)
---> "NBD_CMD_READ"
This commit uses some hairy sed scripting to ensure that we don't add
any more dependencies to nbdkit.
---
src/protocol.h | 9 +++++++
.gitignore | 1 +
src/Makefile.am | 17 +++++++++++--
src/protostrings.sed | 59 ++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 84 insertions(+), 2 deletions(-)
diff --git a/src/protocol.h b/src/protocol.h
index 0444641..6709ddc 100644
--- a/src/protocol.h
+++ b/src/protocol.h
@@ -81,10 +81,12 @@ 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)
@@ -94,6 +96,7 @@ struct fixed_new_option_reply {
#define NBD_FLAG_SEND_WRITE_ZEROES (1 << 6)
/* 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
@@ -101,6 +104,7 @@ struct fixed_new_option_reply {
#define NBD_OPT_INFO 6
#define NBD_OPT_GO 7
+extern const char *name_of_nbd_rep (int);
#define NBD_REP_ACK 1
#define NBD_REP_SERVER 2
#define NBD_REP_INFO 3
@@ -110,6 +114,7 @@ struct fixed_new_option_reply {
#define NBD_REP_ERR_PLATFORM 0x80000004
#define NBD_REP_ERR_TLS_REQD 0x80000005
+extern const char *name_of_nbd_info (int);
#define NBD_INFO_EXPORT 0
/* NBD_INFO_EXPORT reply (follows fixed_new_option_reply). */
@@ -148,6 +153,8 @@ struct reply {
#define NBD_REQUEST_MAGIC 0x25609513
#define NBD_REPLY_MAGIC 0x67446698
+/* 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. */
@@ -155,12 +162,14 @@ struct reply {
#define NBD_CMD_TRIM 4
#define NBD_CMD_WRITE_ZEROES 6
+extern const char *name_of_nbd_cmd_flag (int);
#define NBD_CMD_FLAG_FUA (1<<0)
#define NBD_CMD_FLAG_NO_HOLE (1<<1)
/* Error codes (previously errno).
* See
http://git.qemu.org/?p=qemu.git;a=commitdiff;h=ca4414804114fd0095b317785b...
*/
+extern const char *name_of_nbd_error (int);
#define NBD_SUCCESS 0
#define NBD_EPERM 1
#define NBD_EIO 5
diff --git a/.gitignore b/.gitignore
index 86ef6cb..8cfe734 100644
--- a/.gitignore
+++ b/.gitignore
@@ -47,6 +47,7 @@ Makefile.in
/podwrapper.pl
/src/nbdkit
/src/nbdkit.pc
+/src/protostrings.c
/src/synopsis.c
/src/test-utils
/stamp-h1
diff --git a/src/Makefile.am b/src/Makefile.am
index 65f9498..1563d74 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -49,6 +49,7 @@ nbdkit_SOURCES = \
options.h \
plugins.c \
protocol.h \
+ protostrings.c \
sockets.c \
threadlocal.c \
utils.c \
@@ -77,11 +78,23 @@ nbdkit_LDFLAGS = \
$(PTHREAD_LIBS) \
$(DL_LDFLAGS)
+# protostrings.c is generated from the protocol.h header file where it
+# is used to map NBD protocol flags to strings.
+
+BUILT_SOURCES = protostrings.c
+EXTRA_DIST = protostrings.c
+CLEANFILES += protostrings.c
+protostrings.c: protocol.h protostrings.sed Makefile
+ rm -f $@ $@-t
+ $(SED) -n -f protostrings.sed < $< > $@-t
+ mv $@-t $@
+ chmod 0444 $@
+
# synopsis.c is generated from docs/synopsis.txt where it is also
# used to generate the man page. It is included in main.c.
-BUILT_SOURCES = synopsis.c
-EXTRA_DIST = synopsis.c
+BUILT_SOURCES += synopsis.c
+EXTRA_DIST += synopsis.c
nbdkit_DEPENDENCIES = synopsis.c
CLEANFILES += synopsis.c
main.c: synopsis.c
diff --git a/src/protostrings.sed b/src/protostrings.sed
new file mode 100644
index 0000000..9a94f75
--- /dev/null
+++ b/src/protostrings.sed
@@ -0,0 +1,59 @@
+# nbdkit
+# Copyright (C) 2018 Red Hat Inc.
+# All rights reserved.
+#
+# 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.
+
+# Generate the protostrings.c file from protocol.h.
+
+# Prologue.
+1i\
+/* Generated from protocol.h by protostrings.sed. */\
+\#include "protocol.h"\
+
+
+# Match the precise sections of the source file.
+/^extern const char \*name_of_/,/^$/ {
+
+ # 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;
+
+}
--
2.19.0.rc0