diff --git a/HISTORY b/HISTORY index 67a67e69..cb273554 100644 --- a/HISTORY +++ b/HISTORY @@ -19,7 +19,9 @@ SnapRAID HISTORY * In Linux added the missing support for Btrfs file-systems. Note that to have full support you need also the 'libblkid' library, otherwise you won't get the UUIDs. - * In informative messages does't print the disk directory in file path. + * In screen messages don't print the disk directory in file path. You + can control the format with the test option: + --test-fmt file|disk|path. * In Windows allows to use the escape char '^' to handle file patterns containing real characters matching the globbing '*?[]' ones. In Unix it was already possible to do the same escaping with '\'. diff --git a/Makefile.am b/Makefile.am index 76afc13e..e0d725bd 100644 --- a/Makefile.am +++ b/Makefile.am @@ -455,7 +455,9 @@ endif #### MISC COMMANDS #### $(MSG) Some commands with a not empty array $(TESTENV) ./snapraid$(EXEEXT) $(CHECKFLAGS_VERBOSE) -c $(PAR1) dup - $(TESTENV) ./snapraid$(EXEEXT) $(CHECKFLAGS_VERBOSE) -c $(PAR1) list > output.log + $(TESTENV) ./snapraid$(EXEEXT) $(CHECKFLAGS_VERBOSE) -c $(PAR1) list --test-fmt file > output.log + $(TESTENV) ./snapraid$(EXEEXT) $(CHECKFLAGS_VERBOSE) -c $(PAR1) list --test-fmt disk > output.log + $(TESTENV) ./snapraid$(EXEEXT) $(CHECKFLAGS_VERBOSE) -c $(PAR1) list --test-fmt path > output.log if HAVE_SYMLINK $(TESTENV) ./snapraid$(EXEEXT) $(CHECKFLAGS) -c $(PAR1) pool endif diff --git a/cmdline/elem.c b/cmdline/elem.c index 900e29c4..4add9246 100644 --- a/cmdline/elem.c +++ b/cmdline/elem.c @@ -1420,3 +1420,40 @@ int time_compare(const void* void_a, const void* void_b) return 0; } +/****************************************************************************/ +/* format */ + +int FMT_MODE = FMT_FILE; + +/** + * Format a file path for poll reference + */ +const char* fmt_poll(const struct snapraid_disk* disk, const char* str, char* buffer) +{ + (void)disk; + return esc_shell(str, buffer); +} + +/** + * Format a path name for terminal reference + */ +const char* fmt_term(const struct snapraid_disk* disk, const char* str, char* buffer) +{ + const char* out[3]; + + switch (FMT_MODE) { + case FMT_FILE : + default : + return esc_shell(str, buffer); + case FMT_DISK : + out[0] = disk->name; + out[1] = ":"; + out[2] = str; + return esc_shell_multi(out, 3, buffer); + case FMT_PATH : + out[0] = disk->dir; + out[1] = str; + return esc_shell_multi(out, 2, buffer); + } +} + diff --git a/cmdline/elem.h b/cmdline/elem.h index 00907046..ecd87d55 100644 --- a/cmdline/elem.h +++ b/cmdline/elem.h @@ -1210,23 +1210,24 @@ static inline snapraid_info info_get(tommy_arrayblkof* array, block_off_t pos) */ int time_compare(const void* void_a, const void* void_b); +/****************************************************************************/ +/* format */ + +#define FMT_FILE 0 /**< Print only the file. */ +#define FMT_DISK 1 /**< Print the disk name and the file. */ +#define FMT_PATH 2 /**< Print the full path. */ + +extern int FMT_MODE; + /** * Format a file path for poll reference */ -static inline const char* fmt_poll(const struct snapraid_disk* disk, const char* str, char* buffer) -{ - (void)disk; - return esc_shell(str, buffer); -} +const char* fmt_poll(const struct snapraid_disk* disk, const char* str, char* buffer); /** * Format a path name for terminal reference */ -static inline const char* fmt_term(const struct snapraid_disk* disk, const char* str, char* buffer) -{ - (void)disk; - return esc_shell(str, buffer); -} +const char* fmt_term(const struct snapraid_disk* disk, const char* str, char* buffer); #endif diff --git a/cmdline/snapraid.c b/cmdline/snapraid.c index 01eb1bbd..d410cb6a 100644 --- a/cmdline/snapraid.c +++ b/cmdline/snapraid.c @@ -293,6 +293,7 @@ void config(char* conf, size_t conf_size, const char* argv0) #define OPT_TEST_PARITY_LIMIT 301 #define OPT_TEST_SKIP_CONTENT_WRITE 302 #define OPT_TEST_SKIP_SPACE_HOLDER 303 +#define OPT_TEST_FORMAT 304 #if HAVE_GETOPT_LONG struct option long_options[] = { @@ -457,6 +458,9 @@ struct option long_options[] = { /* Skip space holder file in parity disks */ { "test-skip-space-holder", 0, 0, OPT_TEST_SKIP_SPACE_HOLDER }, + /* Set the ourput format */ + { "test-fmt", 1, 0, OPT_TEST_FORMAT }, + { 0, 0, 0, 0 } }; #endif @@ -886,6 +890,20 @@ int main(int argc, char* argv[]) case OPT_TEST_SKIP_SPACE_HOLDER : opt.skip_space_holder = 1; break; + case OPT_TEST_FORMAT : + if (strcmp(optarg, "file") == 0) + FMT_MODE = FMT_FILE; + else if (strcmp(optarg, "disk") == 0) + FMT_MODE = FMT_DISK; + else if (strcmp(optarg, "path") == 0) + FMT_MODE = FMT_PATH; + else { + /* LCOV_EXCL_START */ + log_fatal("Unknown format '%s'\n", optarg); + exit(EXIT_FAILURE); + /* LCOV_EXCL_STOP */ + } + break; default : /* LCOV_EXCL_START */ log_fatal("Unknown option '%c'\n", (char)c); diff --git a/cmdline/support.c b/cmdline/support.c index 51d79b4b..bcb6685e 100644 --- a/cmdline/support.c +++ b/cmdline/support.c @@ -430,14 +430,22 @@ bail: /* LCOV_EXCL_STOP */ } -const char* esc_shell(const char* str, char* buffer) +const char* esc_shell_multi(const char** str_map, unsigned str_max, char* buffer) { char* begin = buffer; char* end = begin + ESC_MAX; char* p = begin; + unsigned str_mac; + const char* str; #ifdef _WIN32 - int has_quote = strchr(str, ' ') != 0; + int has_quote = 0; + + for (str_mac = 0; str_mac < str_max; ++str_mac) { + str = str_map[str_mac]; + if (strchr(str, ' ') != 0) + has_quote = 1; + } if (has_quote) { if (p == end) @@ -447,9 +455,23 @@ const char* esc_shell(const char* str, char* buffer) #endif /* copy string with escaping */ - while (*str) { + str_mac = 0; + str = str_map[str_mac]; + while (1) { + /* get the next char */ char c = *str; + /* if one string is finished, go to the next */ + while (c == 0 && str_mac + 1 < str_max) { + ++str_mac; + str = str_map[str_mac]; + c = *str; + } + + /* if we read all the strings, stop */ + if (!c) + break; + switch (c) { #ifdef _WIN32 /* diff --git a/cmdline/support.h b/cmdline/support.h index 4c09c802..25c1ad52 100644 --- a/cmdline/support.h +++ b/cmdline/support.h @@ -186,7 +186,6 @@ void msg_verbose(const char* format, ...) __attribute__((format(attribute_printf */ void msg_flush(void); - /****************************************************************************/ /* print */ @@ -229,7 +228,11 @@ const char* esc_tag(const char* str, char* buffer); * * \param buffer Preallocated buffer of ESC_MAX size. */ -const char* esc_shell(const char* str, char* buffer); +const char* esc_shell_multi(const char** str_map, unsigned str_max, char* buffer); +static inline const char* esc_shell(const char* str, char* buffer) +{ + return esc_shell_multi(&str, 1, buffer); +} /** * Polish a string.