Commit 153859be1a0928d07ec2dc2b18847e32e180ff43
Committed by
Blue Swirl
1 parent
3a2eeac0
Use hxtool for qemu-img command list
Use hxtool to generate the 'command syntax' section of qemu-img's help message, and the corresponding section of the texinfo documentation. This has the side-effect of adding 'check' to this list of commands in the texinfo documentation. Signed-off-by: Stuart Brady <stuart.brady@gmail.com>
Showing
5 changed files
with
101 additions
and
38 deletions
.gitignore
Makefile
| ... | ... | @@ -238,6 +238,8 @@ libqemu_user.a: $(USER_OBJS) |
| 238 | 238 | |
| 239 | 239 | ###################################################################### |
| 240 | 240 | |
| 241 | +qemu-img.o: qemu-img-cmds.h | |
| 242 | + | |
| 241 | 243 | qemu-img$(EXESUF): qemu-img.o qemu-tool.o tool-osdep.o $(BLOCK_OBJS) |
| 242 | 244 | |
| 243 | 245 | qemu-nbd$(EXESUF): qemu-nbd.o qemu-tool.o tool-osdep.o $(BLOCK_OBJS) |
| ... | ... | @@ -246,6 +248,9 @@ qemu-io$(EXESUF): qemu-io.o qemu-tool.o tool-osdep.o cmd.o $(BLOCK_OBJS) |
| 246 | 248 | |
| 247 | 249 | qemu-img$(EXESUF) qemu-nbd$(EXESUF) qemu-io$(EXESUF): LIBS += -lz |
| 248 | 250 | |
| 251 | +qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx | |
| 252 | + $(call quiet-command,sh $(SRC_PATH)/hxtool -h < $< > $@," GEN $@") | |
| 253 | + | |
| 249 | 254 | clean: |
| 250 | 255 | # avoid old build problems by removing potentially incorrect old files |
| 251 | 256 | rm -f config.mak config.h op-i386.h opc-i386.h gen-op-i386.h op-arm.h opc-arm.h gen-op-arm.h |
| ... | ... | @@ -257,7 +262,7 @@ clean: |
| 257 | 262 | done |
| 258 | 263 | |
| 259 | 264 | distclean: clean |
| 260 | - rm -f config-host.mak config-host.h $(DOCS) qemu-options.texi | |
| 265 | + rm -f config-host.mak config-host.h $(DOCS) qemu-options.texi qemu-img-cmds.texi | |
| 261 | 266 | rm -f qemu-{doc,tech}.{info,aux,cp,dvi,fn,info,ky,log,pg,toc,tp,vr} |
| 262 | 267 | for d in $(TARGET_DIRS) libhw32 libhw64; do \ |
| 263 | 268 | rm -rf $$d || exit 1 ; \ |
| ... | ... | @@ -335,13 +340,16 @@ qemu-options.texi: $(SRC_PATH)/qemu-options.hx |
| 335 | 340 | qemu-monitor.texi: $(SRC_PATH)/qemu-monitor.hx |
| 336 | 341 | $(call quiet-command,sh $(SRC_PATH)/hxtool -t < $< > $@," GEN $@") |
| 337 | 342 | |
| 343 | +qemu-img-cmds.texi: $(SRC_PATH)/qemu-img-cmds.hx | |
| 344 | + $(call quiet-command,sh $(SRC_PATH)/hxtool -t < $< > $@," GEN $@") | |
| 345 | + | |
| 338 | 346 | qemu.1: qemu-doc.texi qemu-options.texi qemu-monitor.texi |
| 339 | 347 | $(call quiet-command, \ |
| 340 | 348 | perl -Ww -- $(SRC_PATH)/texi2pod.pl $< qemu.pod && \ |
| 341 | 349 | pod2man --section=1 --center=" " --release=" " qemu.pod > $@, \ |
| 342 | 350 | " GEN $@") |
| 343 | 351 | |
| 344 | -qemu-img.1: qemu-img.texi | |
| 352 | +qemu-img.1: qemu-img.texi qemu-img-cmds.texi | |
| 345 | 353 | $(call quiet-command, \ |
| 346 | 354 | perl -Ww -- $(SRC_PATH)/texi2pod.pl $< qemu-img.pod && \ |
| 347 | 355 | pod2man --section=1 --center=" " --release=" " qemu-img.pod > $@, \ |
| ... | ... | @@ -359,7 +367,7 @@ dvi: qemu-doc.dvi qemu-tech.dvi |
| 359 | 367 | |
| 360 | 368 | html: qemu-doc.html qemu-tech.html |
| 361 | 369 | |
| 362 | -qemu-doc.dvi qemu-doc.html qemu-doc.info: qemu-img.texi qemu-nbd.texi qemu-options.texi qemu-monitor.texi | |
| 370 | +qemu-doc.dvi qemu-doc.html qemu-doc.info: qemu-img.texi qemu-nbd.texi qemu-options.texi qemu-monitor.texi qemu-img-cmds.texi | |
| 363 | 371 | |
| 364 | 372 | VERSION ?= $(shell cat VERSION) |
| 365 | 373 | FILE = qemu-$(VERSION) | ... | ... |
qemu-img-cmds.hx
0 → 100644
| 1 | +HXCOMM Use DEFHEADING() to define headings in both help text and texi | |
| 2 | +HXCOMM Text between STEXI and ETEXI are copied to texi version and | |
| 3 | +HXCOMM discarded from C version | |
| 4 | +HXCOMM DEF(command, callback, arg_string) is used to construct | |
| 5 | +HXCOMM command structures and help message. | |
| 6 | +HXCOMM HXCOMM can be used for comments, discarded from both texi and C | |
| 7 | + | |
| 8 | +STEXI | |
| 9 | +@table @option | |
| 10 | +STEXI | |
| 11 | + | |
| 12 | +DEF("check", img_check, | |
| 13 | + "check [-f fmt] filename") | |
| 14 | +STEXI | |
| 15 | +@item check [-f @var{fmt}] @var{filename} | |
| 16 | +ETEXI | |
| 17 | + | |
| 18 | +DEF("create", img_create, | |
| 19 | + "create [-F fmt] [-b base_image] [-f fmt] [-o options] filename [size]") | |
| 20 | +STEXI | |
| 21 | +@item create [-F @var{base_fmt}] [-b @var{base_image}] [-f @var{fmt}] [-o @var{options}] @var{filename} [@var{size}] | |
| 22 | +ETEXI | |
| 23 | + | |
| 24 | +DEF("commit", img_commit, | |
| 25 | + "commit [-f fmt] filename") | |
| 26 | +STEXI | |
| 27 | +@item commit [-f @var{fmt}] @var{filename} | |
| 28 | +ETEXI | |
| 29 | + | |
| 30 | +DEF("convert", img_convert, | |
| 31 | + "convert [-c] [-f fmt] [-O output_fmt] [-o options] [-B output_base_image] filename [filename2 [...]] output_filename") | |
| 32 | +STEXI | |
| 33 | +@item convert [-c] [-f @var{fmt}] [-O @var{output_fmt}] [-o @var{options}] [-B @var{output_base_image}] @var{filename} [@var{filename2} [...]] @var{output_filename} | |
| 34 | +ETEXI | |
| 35 | + | |
| 36 | +DEF("info", img_info, | |
| 37 | + "info [-f fmt] filename") | |
| 38 | +STEXI | |
| 39 | +@item info [-f @var{fmt}] @var{filename} | |
| 40 | +ETEXI | |
| 41 | + | |
| 42 | +DEF("snapshot", img_snapshot, | |
| 43 | + "snapshot [-l | -a snapshot | -c snapshot | -d snapshot] filename") | |
| 44 | +STEXI | |
| 45 | +@item snapshot [-l | -a @var{snapshot} | -c @var{snapshot} | -d @var{snapshot}] @var{filename} | |
| 46 | +@end table | |
| 47 | +ETEXI | ... | ... |
qemu-img.c
| ... | ... | @@ -31,6 +31,11 @@ |
| 31 | 31 | #include <windows.h> |
| 32 | 32 | #endif |
| 33 | 33 | |
| 34 | +typedef struct img_cmd_t { | |
| 35 | + const char *name; | |
| 36 | + int (*handler)(int argc, char **argv); | |
| 37 | +} img_cmd_t; | |
| 38 | + | |
| 34 | 39 | /* Default to cache=writeback as data integrity is not important for qemu-tcg. */ |
| 35 | 40 | #define BRDV_O_FLAGS BDRV_O_CACHE_WB |
| 36 | 41 | |
| ... | ... | @@ -58,12 +63,11 @@ static void help(void) |
| 58 | 63 | "QEMU disk image utility\n" |
| 59 | 64 | "\n" |
| 60 | 65 | "Command syntax:\n" |
| 61 | - " check [-f fmt] filename\n" | |
| 62 | - " create [-F fmt] [-b base_image] [-f fmt] [-o options] filename [size]\n" | |
| 63 | - " commit [-f fmt] filename\n" | |
| 64 | - " convert [-c] [-f fmt] [-O output_fmt] [-o options] [-B output_base_image] filename [filename2 [...]] output_filename\n" | |
| 65 | - " info [-f fmt] filename\n" | |
| 66 | - " snapshot [-l | -a snapshot | -c snapshot | -d snapshot] filename\n" | |
| 66 | +#define DEF(option, callback, arg_string) \ | |
| 67 | + " " arg_string "\n" | |
| 68 | +#include "qemu-img-cmds.h" | |
| 69 | +#undef DEF | |
| 70 | +#undef GEN_DOCS | |
| 67 | 71 | "\n" |
| 68 | 72 | "Command parameters:\n" |
| 69 | 73 | " 'filename' is a disk image filename\n" |
| ... | ... | @@ -919,7 +923,7 @@ static int img_info(int argc, char **argv) |
| 919 | 923 | #define SNAPSHOT_APPLY 3 |
| 920 | 924 | #define SNAPSHOT_DELETE 4 |
| 921 | 925 | |
| 922 | -static void img_snapshot(int argc, char **argv) | |
| 926 | +static int img_snapshot(int argc, char **argv) | |
| 923 | 927 | { |
| 924 | 928 | BlockDriverState *bs; |
| 925 | 929 | QEMUSnapshotInfo sn; |
| ... | ... | @@ -936,18 +940,18 @@ static void img_snapshot(int argc, char **argv) |
| 936 | 940 | switch(c) { |
| 937 | 941 | case 'h': |
| 938 | 942 | help(); |
| 939 | - return; | |
| 943 | + return 0; | |
| 940 | 944 | case 'l': |
| 941 | 945 | if (action) { |
| 942 | 946 | help(); |
| 943 | - return; | |
| 947 | + return 0; | |
| 944 | 948 | } |
| 945 | 949 | action = SNAPSHOT_LIST; |
| 946 | 950 | break; |
| 947 | 951 | case 'a': |
| 948 | 952 | if (action) { |
| 949 | 953 | help(); |
| 950 | - return; | |
| 954 | + return 0; | |
| 951 | 955 | } |
| 952 | 956 | action = SNAPSHOT_APPLY; |
| 953 | 957 | snapshot_name = optarg; |
| ... | ... | @@ -955,7 +959,7 @@ static void img_snapshot(int argc, char **argv) |
| 955 | 959 | case 'c': |
| 956 | 960 | if (action) { |
| 957 | 961 | help(); |
| 958 | - return; | |
| 962 | + return 0; | |
| 959 | 963 | } |
| 960 | 964 | action = SNAPSHOT_CREATE; |
| 961 | 965 | snapshot_name = optarg; |
| ... | ... | @@ -963,7 +967,7 @@ static void img_snapshot(int argc, char **argv) |
| 963 | 967 | case 'd': |
| 964 | 968 | if (action) { |
| 965 | 969 | help(); |
| 966 | - return; | |
| 970 | + return 0; | |
| 967 | 971 | } |
| 968 | 972 | action = SNAPSHOT_DELETE; |
| 969 | 973 | snapshot_name = optarg; |
| ... | ... | @@ -1021,31 +1025,38 @@ static void img_snapshot(int argc, char **argv) |
| 1021 | 1025 | |
| 1022 | 1026 | /* Cleanup */ |
| 1023 | 1027 | bdrv_delete(bs); |
| 1028 | + | |
| 1029 | + return 0; | |
| 1024 | 1030 | } |
| 1025 | 1031 | |
| 1032 | +static const img_cmd_t img_cmds[] = { | |
| 1033 | +#define DEF(option, callback, arg_string) \ | |
| 1034 | + { option, callback }, | |
| 1035 | +#include "qemu-img-cmds.h" | |
| 1036 | +#undef DEF | |
| 1037 | +#undef GEN_DOCS | |
| 1038 | + { NULL, NULL, }, | |
| 1039 | +}; | |
| 1040 | + | |
| 1026 | 1041 | int main(int argc, char **argv) |
| 1027 | 1042 | { |
| 1028 | - const char *cmd; | |
| 1043 | + const img_cmd_t *cmd; | |
| 1044 | + const char *cmdname; | |
| 1029 | 1045 | |
| 1030 | 1046 | bdrv_init(); |
| 1031 | 1047 | if (argc < 2) |
| 1032 | 1048 | help(); |
| 1033 | - cmd = argv[1]; | |
| 1049 | + cmdname = argv[1]; | |
| 1034 | 1050 | argc--; argv++; |
| 1035 | - if (!strcmp(cmd, "create")) { | |
| 1036 | - img_create(argc, argv); | |
| 1037 | - } else if (!strcmp(cmd, "check")) { | |
| 1038 | - img_check(argc, argv); | |
| 1039 | - } else if (!strcmp(cmd, "commit")) { | |
| 1040 | - img_commit(argc, argv); | |
| 1041 | - } else if (!strcmp(cmd, "convert")) { | |
| 1042 | - img_convert(argc, argv); | |
| 1043 | - } else if (!strcmp(cmd, "info")) { | |
| 1044 | - img_info(argc, argv); | |
| 1045 | - } else if (!strcmp(cmd, "snapshot")) { | |
| 1046 | - img_snapshot(argc, argv); | |
| 1047 | - } else { | |
| 1048 | - help(); | |
| 1051 | + | |
| 1052 | + /* find the command */ | |
| 1053 | + for(cmd = img_cmds; cmd->name != NULL; cmd++) { | |
| 1054 | + if (!strcmp(cmdname, cmd->name)) { | |
| 1055 | + return cmd->handler(argc, argv); | |
| 1056 | + } | |
| 1049 | 1057 | } |
| 1058 | + | |
| 1059 | + /* not found */ | |
| 1060 | + help(); | |
| 1050 | 1061 | return 0; |
| 1051 | 1062 | } | ... | ... |
qemu-img.texi
| ... | ... | @@ -7,13 +7,8 @@ usage: qemu-img command [command options] |
| 7 | 7 | @c man begin OPTIONS |
| 8 | 8 | |
| 9 | 9 | The following commands are supported: |
| 10 | -@table @option | |
| 11 | -@item create [-F @var{base_fmt}] [-b @var{base_image}] [-f @var{fmt}] [-o @var{options}] @var{filename} [@var{size}] | |
| 12 | -@item commit [-f @var{fmt}] @var{filename} | |
| 13 | -@item convert [-c] [-f @var{fmt}] [-O @var{output_fmt}] [-o @var{options}] [-B @var{output_base_image}] @var{filename} [@var{filename2} [...]] @var{output_filename} | |
| 14 | -@item info [-f @var{fmt}] @var{filename} | |
| 15 | -@item snapshot [-l | -a @var{snapshot} | -c @var{snapshot} | -d @var{snapshot}] @var{filename} | |
| 16 | -@end table | |
| 10 | + | |
| 11 | +@include qemu-img-cmds.texi | |
| 17 | 12 | |
| 18 | 13 | Command parameters: |
| 19 | 14 | @table @var | ... | ... |