From a277c0ad3baac3e83bf439ee7a1feec6fe7b15b6 Mon Sep 17 00:00:00 2001 From: Kalle Olavi Niemitalo Date: Sun, 8 Mar 2009 13:28:13 +0200 Subject: [PATCH] Wrap option descriptions in --config-help, --long-help --- src/config/cmdline.c | 38 +++++++++++++++++++++++++------------- src/config/conf.c | 2 +- src/config/conf.h | 3 +++ 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/config/cmdline.c b/src/config/cmdline.c index 0f6f4172b..d521ce258 100644 --- a/src/config/cmdline.c +++ b/src/config/cmdline.c @@ -419,6 +419,28 @@ version_cmd(struct option *o, unsigned char ***argv, int *argc) #define gettext_nonempty(x) (*(x) ? gettext(x) : (x)) +static void print_option_desc(const unsigned char *desc) +{ + struct string wrapped; + static const struct string indent = INIT_STRING(" ", 12); + + if (init_string(&wrapped) + && wrap_option_desc(&wrapped, desc, &indent, 79 - indent.length)) { + /* struct string could in principle contain null + * characters, so don't use printf() or fputs(). */ + fwrite(wrapped.source, 1, wrapped.length, stdout); + } else { + /* Write the error to stderr so it appears on the + * screen even if stdout is being parsed by a script + * that reformats it to HTML or such. */ + fprintf(stderr, "%12s%s\n", "", + gettext("Out of memory formatting option documentation")); + } + + /* done_string() is safe even if init_string() failed. */ + done_string(&wrapped); +} + static void print_full_help_outer(struct option *tree, unsigned char *path); static void @@ -537,19 +559,9 @@ print_full_help_inner(struct option *tree, unsigned char *path, } } - printf("\n %8s", ""); - { - int l = strlen(desc); - int i; - - for (i = 0; i < l; i++) { - putchar(desc[i]); - - if (desc[i] == '\n') - printf(" %8s", ""); - } - } - printf("\n\n"); + putchar('\n'); + print_option_desc(desc); + putchar('\n'); if (option->type == OPT_TREE) { memcpy(savedpos, ".", 2); diff --git a/src/config/conf.c b/src/config/conf.c index f90f6fd3f..48fd13536 100644 --- a/src/config/conf.c +++ b/src/config/conf.c @@ -791,7 +791,7 @@ add_indent_to_string(struct string *string, int depth) add_xchar_to_string(string, ' ', depth * indentation); } -static struct string * +struct string * wrap_option_desc(struct string *out, const unsigned char *src, const struct string *indent, int maxwidth) { diff --git a/src/config/conf.h b/src/config/conf.h index 2a9a7ffee..1b3cc8fd9 100644 --- a/src/config/conf.h +++ b/src/config/conf.h @@ -26,4 +26,7 @@ unsigned char * create_config_string(unsigned char *prefix, unsigned char *name, struct option *options); +struct string *wrap_option_desc(struct string *out, const unsigned char *src, + const struct string *indent, int maxwidth); + #endif