Skip to content

Commit d198cf9

Browse files
committed
protocol: Generate map functions from NBD protocol flags to printable strings.
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.
1 parent 521f17d commit d198cf9

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Makefile.in
4747
/podwrapper.pl
4848
/src/nbdkit
4949
/src/nbdkit.pc
50+
/src/protostrings.c
5051
/src/synopsis.c
5152
/src/test-utils
5253
/stamp-h1

src/Makefile.am

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ nbdkit_SOURCES = \
4949
options.h \
5050
plugins.c \
5151
protocol.h \
52+
protostrings.c \
5253
sockets.c \
5354
threadlocal.c \
5455
utils.c \
@@ -77,11 +78,23 @@ nbdkit_LDFLAGS = \
7778
$(PTHREAD_LIBS) \
7879
$(DL_LDFLAGS)
7980

81+
# protostrings.c is generated from the protocol.h header file where it
82+
# is used to map NBD protocol flags to strings.
83+
84+
BUILT_SOURCES = protostrings.c
85+
EXTRA_DIST = protostrings.c
86+
CLEANFILES += protostrings.c
87+
protostrings.c: protocol.h protostrings.sed Makefile
88+
rm -f $@ $@-t
89+
$(SED) -n -f protostrings.sed < $< > $@-t
90+
mv $@-t $@
91+
chmod 0444 $@
92+
8093
# synopsis.c is generated from docs/synopsis.txt where it is also
8194
# used to generate the man page. It is included in main.c.
8295

83-
BUILT_SOURCES = synopsis.c
84-
EXTRA_DIST = synopsis.c
96+
BUILT_SOURCES += synopsis.c
97+
EXTRA_DIST += synopsis.c
8598
nbdkit_DEPENDENCIES = synopsis.c
8699
CLEANFILES += synopsis.c
87100
main.c: synopsis.c

src/protocol.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,12 @@ struct fixed_new_option_reply {
8181
#define NBD_REP_MAGIC UINT64_C(0x3e889045565a9)
8282

8383
/* Global flags. */
84+
extern const char *name_of_nbd_global_flag (int);
8485
#define NBD_FLAG_FIXED_NEWSTYLE 1
8586
#define NBD_FLAG_NO_ZEROES 2
8687

8788
/* Per-export flags. */
89+
extern const char *name_of_nbd_flag (int);
8890
#define NBD_FLAG_HAS_FLAGS (1 << 0)
8991
#define NBD_FLAG_READ_ONLY (1 << 1)
9092
#define NBD_FLAG_SEND_FLUSH (1 << 2)
@@ -94,13 +96,15 @@ struct fixed_new_option_reply {
9496
#define NBD_FLAG_SEND_WRITE_ZEROES (1 << 6)
9597

9698
/* NBD options (new style handshake only). */
99+
extern const char *name_of_nbd_opt (int);
97100
#define NBD_OPT_EXPORT_NAME 1
98101
#define NBD_OPT_ABORT 2
99102
#define NBD_OPT_LIST 3
100103
#define NBD_OPT_STARTTLS 5
101104
#define NBD_OPT_INFO 6
102105
#define NBD_OPT_GO 7
103106

107+
extern const char *name_of_nbd_rep (int);
104108
#define NBD_REP_ACK 1
105109
#define NBD_REP_SERVER 2
106110
#define NBD_REP_INFO 3
@@ -110,6 +114,7 @@ struct fixed_new_option_reply {
110114
#define NBD_REP_ERR_PLATFORM 0x80000004
111115
#define NBD_REP_ERR_TLS_REQD 0x80000005
112116

117+
extern const char *name_of_nbd_info (int);
113118
#define NBD_INFO_EXPORT 0
114119

115120
/* NBD_INFO_EXPORT reply (follows fixed_new_option_reply). */
@@ -148,19 +153,23 @@ struct reply {
148153
#define NBD_REQUEST_MAGIC 0x25609513
149154
#define NBD_REPLY_MAGIC 0x67446698
150155

156+
/* NBD commands. */
157+
extern const char *name_of_nbd_cmd (int);
151158
#define NBD_CMD_READ 0
152159
#define NBD_CMD_WRITE 1
153160
#define NBD_CMD_DISC 2 /* Disconnect. */
154161
#define NBD_CMD_FLUSH 3
155162
#define NBD_CMD_TRIM 4
156163
#define NBD_CMD_WRITE_ZEROES 6
157164

165+
extern const char *name_of_nbd_cmd_flag (int);
158166
#define NBD_CMD_FLAG_FUA (1<<0)
159167
#define NBD_CMD_FLAG_NO_HOLE (1<<1)
160168

161169
/* Error codes (previously errno).
162170
* See http://git.qemu.org/?p=qemu.git;a=commitdiff;h=ca4414804114fd0095b317785bc0b51862e62ebb
163171
*/
172+
extern const char *name_of_nbd_error (int);
164173
#define NBD_SUCCESS 0
165174
#define NBD_EPERM 1
166175
#define NBD_EIO 5

src/protostrings.sed

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# nbdkit
2+
# Copyright (C) 2018 Red Hat Inc.
3+
# All rights reserved.
4+
#
5+
# Redistribution and use in source and binary forms, with or without
6+
# modification, are permitted provided that the following conditions are
7+
# met:
8+
#
9+
# * Redistributions of source code must retain the above copyright
10+
# notice, this list of conditions and the following disclaimer.
11+
#
12+
# * Redistributions in binary form must reproduce the above copyright
13+
# notice, this list of conditions and the following disclaimer in the
14+
# documentation and/or other materials provided with the distribution.
15+
#
16+
# * Neither the name of Red Hat nor the names of its contributors may be
17+
# used to endorse or promote products derived from this software without
18+
# specific prior written permission.
19+
#
20+
# THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
21+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23+
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
24+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27+
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30+
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31+
# SUCH DAMAGE.
32+
33+
# Generate the protostrings.c file from protocol.h.
34+
35+
# Prologue.
36+
1i\
37+
/* Generated from protocol.h by protostrings.sed. */\
38+
\#include "protocol.h"\
39+
40+
41+
# Match the precise sections of the source file.
42+
/^extern const char \*name_of_/,/^$/ {
43+
44+
# Convert extern function prototype into a definition.
45+
s/extern \(const char \*name_of_.*\) (int);/\1 (int fl) {\
46+
switch (fl) {/;
47+
48+
# Convert #define lines into cases.
49+
s/^#define \([_A-Z]*\).*/ case \1: return "\1\";/;
50+
51+
# Append closing brace.
52+
s/^$/ default: return "unknown";\
53+
}\
54+
}/;
55+
56+
# Print pattern buffer.
57+
p;
58+
59+
}

0 commit comments

Comments
 (0)