From a6cc8ea10ad84e4b7d1303ed048e644f0dd7ae04 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Thu, 14 Jan 2016 10:30:35 +0100 Subject: [PATCH] btrfs-progs: introduce helper for parsing args without options All commands should support the "--" option separator. This is transparently handled by getopt, but we don't use that everywhere. Introduce a helper for commands that take no options (just the path). The object file dependencies need to be adjusted a bit. Signed-off-by: David Sterba --- Makefile.in | 6 +++--- utils.c | 28 ++++++++++++++++++++++++++++ utils.h | 1 + 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Makefile.in b/Makefile.in index 112c3ba2..1bd497ac 100644 --- a/Makefile.in +++ b/Makefile.in @@ -70,7 +70,7 @@ objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \ extent-cache.o extent_io.o volumes.o utils.o repair.o \ qgroup.o raid6.o free-space-cache.o list_sort.o props.o \ ulist.o qgroup-verify.o backref.o string-table.o task-utils.o \ - inode.o file.o find-root.o free-space-tree.o + inode.o file.o find-root.o free-space-tree.o help.o cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \ cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \ cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \ @@ -260,9 +260,9 @@ btrfs-%: $(objects) $(libs_static) btrfs-%.o $(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o $(libs_static) \ $(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs)) -btrfs: $(objects) btrfs.o help.o $(cmds_objects) $(libs_static) +btrfs: $(objects) btrfs.o $(cmds_objects) $(libs_static) @echo " [LD] $@" - $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o help.o $(cmds_objects) \ + $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o $(cmds_objects) \ $(objects) $(libs_static) $(LDFLAGS) $(LIBS) btrfs.static: $(static_objects) btrfs.static.o help.static.o $(static_cmds_objects) $(static_libbtrfs_objects) diff --git a/utils.c b/utils.c index ad3ada08..3df8b42a 100644 --- a/utils.c +++ b/utils.c @@ -37,6 +37,7 @@ #include #include #include +#include #include "kerncompat.h" #include "radix-tree.h" @@ -47,6 +48,7 @@ #include "utils.h" #include "volumes.h" #include "ioctl.h" +#include "commands.h" #ifndef BLKDISCARD #define BLKDISCARD _IO(0x12,119) @@ -3119,3 +3121,29 @@ int string_is_numerical(const char *str) return 0; return 1; } + +/* + * Preprocess @argv with getopt_long to reorder options and consume the "--" + * option separator. + * Unknown short and long options are reported, optionally the @usage is printed + * before exit. + */ +void clean_args_no_options(int argc, char *argv[], const char * const *usagestr) +{ + static const struct option long_options[] = { + {NULL, 0, NULL, 0} + }; + + while (1) { + int c = getopt_long(argc, argv, "", long_options, NULL); + + if (c < 0) + break; + + switch (c) { + default: + if (usagestr) + usage(usagestr); + } + } +} diff --git a/utils.h b/utils.h index e522a8c3..d53357a2 100644 --- a/utils.h +++ b/utils.h @@ -274,6 +274,7 @@ const char *get_argv0_buf(void); "-t|--tbytes show sizes in TiB, or TB with --si" unsigned int get_unit_mode_from_arg(int *argc, char *argv[], int df_mode); +void clean_args_no_options(int argc, char *argv[], const char * const *usage); int string_is_numerical(const char *str); __attribute__ ((format (printf, 1, 2)))