From 0f1930c58754a821bd51087ca4676e6a69cd4d2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 25 Feb 2012 20:14:54 +0100 Subject: parse-options: allow positivation of options starting, with no- Long options can be negated by adding no- right after the leading two dashes. This is useful e.g. to override options set by aliases. For options that are defined to start with no- already, this looks a bit funny. Allow such options to also be negated by removing the prefix. The following thirteen options are affected: apply --no-add bisect--helper --no-checkout checkout-index --no-create clone --no-checkout --no-hardlinks commit --no-verify --no-post-rewrite format-patch --no-binary hash-object --no-filters read-tree --no-sparse-checkout revert --no-commit show-branch --no-name update-ref --no-deref The following five are NOT affected because they are defined with PARSE_OPT_NONEG or the non-negated version is defined as well: branch --no-merged format-patch --no-stat --no-numbered update-index --no-assume-unchanged --no-skip-worktree Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- parse-options.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'parse-options.c') diff --git a/parse-options.c b/parse-options.c index f0098eb8ea..8906841665 100644 --- a/parse-options.c +++ b/parse-options.c @@ -193,13 +193,14 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg, arg_end = arg + strlen(arg); for (; options->type != OPTION_END; options++) { - const char *rest; - int flags = 0; + const char *rest, *long_name = options->long_name; + int flags = 0, opt_flags = 0; - if (!options->long_name) + if (!long_name) continue; - rest = skip_prefix(arg, options->long_name); +again: + rest = skip_prefix(arg, long_name); if (options->type == OPTION_ARGUMENT) { if (!rest) continue; @@ -212,7 +213,7 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg, } if (!rest) { /* abbreviated? */ - if (!strncmp(options->long_name, arg, arg_end - arg)) { + if (!strncmp(long_name, arg, arg_end - arg)) { is_abbreviated: if (abbrev_option) { /* @@ -227,7 +228,7 @@ is_abbreviated: if (!(flags & OPT_UNSET) && *arg_end) p->opt = arg_end + 1; abbrev_option = options; - abbrev_flags = flags; + abbrev_flags = flags ^ opt_flags; continue; } /* negation allowed? */ @@ -239,12 +240,18 @@ is_abbreviated: goto is_abbreviated; } /* negated? */ - if (strncmp(arg, "no-", 3)) + if (prefixcmp(arg, "no-")) { + if (!prefixcmp(long_name, "no-")) { + long_name += 3; + opt_flags |= OPT_UNSET; + goto again; + } continue; + } flags |= OPT_UNSET; - rest = skip_prefix(arg + 3, options->long_name); + rest = skip_prefix(arg + 3, long_name); /* abbreviated and negated? */ - if (!rest && !prefixcmp(options->long_name, arg + 3)) + if (!rest && !prefixcmp(long_name, arg + 3)) goto is_abbreviated; if (!rest) continue; @@ -254,7 +261,7 @@ is_abbreviated: continue; p->opt = rest + 1; } - return get_value(p, options, flags); + return get_value(p, options, flags ^ opt_flags); } if (ambiguous_option) -- cgit v1.2.1 From cbb08c2e0b41ab162838aa1e83b959bac91151e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Tue, 28 Feb 2012 20:06:09 +0100 Subject: parse-options: remove PARSE_OPT_NEGHELP PARSE_OPT_NEGHELP is confusing because short options defined with that flag do the opposite of what the helptext says. It is also not needed anymore now that options starting with no- can be negated by removing that prefix. Convert its only two users to OPT_NEGBIT() and OPT_BOOL() and then remove support for PARSE_OPT_NEGHELP. Signed-off-by: Rene Scharfe Acked-by: Jeff King Signed-off-by: Junio C Hamano --- parse-options.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'parse-options.c') diff --git a/parse-options.c b/parse-options.c index 8906841665..190899611e 100644 --- a/parse-options.c +++ b/parse-options.c @@ -533,7 +533,7 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx, continue; pos = fprintf(outfile, " "); - if (opts->short_name && !(opts->flags & PARSE_OPT_NEGHELP)) { + if (opts->short_name) { if (opts->flags & PARSE_OPT_NODASH) pos += fprintf(outfile, "%c", opts->short_name); else @@ -542,9 +542,7 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx, if (opts->long_name && opts->short_name) pos += fprintf(outfile, ", "); if (opts->long_name) - pos += fprintf(outfile, "--%s%s", - (opts->flags & PARSE_OPT_NEGHELP) ? "no-" : "", - opts->long_name); + pos += fprintf(outfile, "--%s", opts->long_name); if (opts->type == OPTION_NUMBER) pos += fprintf(outfile, "-NUM"); -- cgit v1.2.1 From 38916c5b4740f6db09dc140a84bb470dfb582366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 3 Mar 2012 12:00:29 +0100 Subject: parse-options: typo check for unknown switches The user specifies a long option but forgets to type the second leading dash, we currently detect and report that fact if its first letter is a valid short option. This is done for safety, to avoid ambiguity between short options (and their arguments) and a long option with a missing dash. This diagnostic message is also helpful for long options whose first letter is not a valid short option, however. Print it in that case, too, as a courtesy. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- parse-options.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'parse-options.c') diff --git a/parse-options.c b/parse-options.c index 190899611e..850cfa78c9 100644 --- a/parse-options.c +++ b/parse-options.c @@ -393,6 +393,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx, case -1: return parse_options_usage(ctx, usagestr, options, 1); case -2: + if (ctx->opt) + check_typos(arg + 1, options); goto unknown; } if (ctx->opt) -- cgit v1.2.1 From 54e6dc7daf8e4160d5ec5d2742ae09d416126a1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sun, 6 May 2012 21:23:51 +0700 Subject: i18n: parseopt: lookup help and argument translations when showing usage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- parse-options.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'parse-options.c') diff --git a/parse-options.c b/parse-options.c index 850cfa78c9..ab70c29c49 100644 --- a/parse-options.c +++ b/parse-options.c @@ -490,7 +490,7 @@ static int usage_argh(const struct option *opts, FILE *outfile) s = literal ? "[%s]" : "[<%s>]"; else s = literal ? " %s" : " <%s>"; - return fprintf(outfile, s, opts->argh ? opts->argh : "..."); + return fprintf(outfile, s, opts->argh ? _(opts->argh) : _("...")); } #define USAGE_OPTS_WIDTH 24 @@ -508,13 +508,16 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx, if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL) fprintf(outfile, "cat <<\\EOF\n"); - fprintf(outfile, "usage: %s\n", *usagestr++); + fprintf_ln(outfile, _("usage: %s"), _(*usagestr++)); while (*usagestr && **usagestr) - fprintf(outfile, " or: %s\n", *usagestr++); + /* TRANSLATORS: the colon here should align with the + one in "usage: %s" translation */ + fprintf_ln(outfile, _(" or: %s"), _(*usagestr++)); while (*usagestr) { - fprintf(outfile, "%s%s\n", - **usagestr ? " " : "", - *usagestr); + if (**usagestr) + fprintf_ln(outfile, _(" %s"), _(*usagestr)); + else + putchar('\n'); usagestr++; } @@ -528,7 +531,7 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx, if (opts->type == OPTION_GROUP) { fputc('\n', outfile); if (*opts->help) - fprintf(outfile, "%s\n", opts->help); + fprintf(outfile, "%s\n", _(opts->help)); continue; } if (!full && (opts->flags & PARSE_OPT_HIDDEN)) @@ -558,7 +561,7 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx, fputc('\n', outfile); pad = USAGE_OPTS_WIDTH; } - fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", opts->help); + fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help)); } fputc('\n', outfile); -- cgit v1.2.1 From 76759c7dff53e8c84e975b88cb8245587c14c7ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20B=C3=B6gershausen?= Date: Sun, 8 Jul 2012 15:50:25 +0200 Subject: git on Mac OS and precomposed unicode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mac OS X mangles file names containing unicode on file systems HFS+, VFAT or SAMBA. When a file using unicode code points outside ASCII is created on a HFS+ drive, the file name is converted into decomposed unicode and written to disk. No conversion is done if the file name is already decomposed unicode. Calling open("\xc3\x84", ...) with a precomposed "Ä" yields the same result as open("\x41\xcc\x88",...) with a decomposed "Ä". As a consequence, readdir() returns the file names in decomposed unicode, even if the user expects precomposed unicode. Unlike on HFS+, Mac OS X stores files on a VFAT drive (e.g. an USB drive) in precomposed unicode, but readdir() still returns file names in decomposed unicode. When a git repository is stored on a network share using SAMBA, file names are send over the wire and written to disk on the remote system in precomposed unicode, but Mac OS X readdir() returns decomposed unicode to be compatible with its behaviour on HFS+ and VFAT. The unicode decomposition causes many problems: - The names "git add" and other commands get from the end user may often be precomposed form (the decomposed form is not easily input from the keyboard), but when the commands read from the filesystem to see what it is going to update the index with already is on the filesystem, readdir() will give decomposed form, which is different. - Similarly "git log", "git mv" and all other commands that need to compare pathnames found on the command line (often but not always precomposed form; a command line input resulting from globbing may be in decomposed) with pathnames found in the tree objects (should be precomposed form to be compatible with other systems and for consistency in general). - The same for names stored in the index, which should be precomposed, that may need to be compared with the names read from readdir(). NFS mounted from Linux is fully transparent and does not suffer from the above. As Mac OS X treats precomposed and decomposed file names as equal, we can - wrap readdir() on Mac OS X to return the precomposed form, and - normalize decomposed form given from the command line also to the precomposed form, to ensure that all pathnames used in Git are always in the precomposed form. This behaviour can be requested by setting "core.precomposedunicode" configuration variable to true. The code in compat/precomposed_utf8.c implements basically 4 new functions: precomposed_utf8_opendir(), precomposed_utf8_readdir(), precomposed_utf8_closedir() and precompose_argv(). The first three are to wrap opendir(3), readdir(3), and closedir(3) functions. The argv[] conversion allows to use the TAB filename completion done by the shell on command line. It tolerates other tools which use readdir() to feed decomposed file names into git. When creating a new git repository with "git init" or "git clone", "core.precomposedunicode" will be set "false". The user needs to activate this feature manually. She typically sets core.precomposedunicode to "true" on HFS and VFAT, or file systems mounted via SAMBA. Helped-by: Junio C Hamano Signed-off-by: Torsten Bögershausen Signed-off-by: Junio C Hamano --- parse-options.c | 1 + 1 file changed, 1 insertion(+) (limited to 'parse-options.c') diff --git a/parse-options.c b/parse-options.c index ab70c29c49..c1c66bd408 100644 --- a/parse-options.c +++ b/parse-options.c @@ -476,6 +476,7 @@ int parse_options(int argc, const char **argv, const char *prefix, usage_with_options(usagestr, options); } + precompose_argv(argc, argv); return parse_options_end(&ctx); } -- cgit v1.2.1 From a469a1019352b8efc4bd7003b0bd59eb60fc428c Mon Sep 17 00:00:00 2001 From: Jeff King Date: Sat, 15 Dec 2012 12:42:10 -0500 Subject: silence some -Wuninitialized false positives There are a few error functions that simply wrap error() and provide a standardized message text. Like error(), they always return -1; knowing that can help the compiler silence some false positive -Wuninitialized warnings. One strategy would be to just declare these as inline in the header file so that the compiler can see that they always return -1. However, gcc does not always inline them (e.g., it will not inline opterror, even with -O3), which renders our change pointless. Instead, let's follow the same route we did with error() in the last patch, and define a macro that makes the constant return value obvious to the compiler. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- parse-options.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'parse-options.c') diff --git a/parse-options.c b/parse-options.c index c1c66bd408..67e98a6323 100644 --- a/parse-options.c +++ b/parse-options.c @@ -18,15 +18,6 @@ int optbug(const struct option *opt, const char *reason) return error("BUG: switch '%c' %s", opt->short_name, reason); } -int opterror(const struct option *opt, const char *reason, int flags) -{ - if (flags & OPT_SHORT) - return error("switch `%c' %s", opt->short_name, reason); - if (flags & OPT_UNSET) - return error("option `no-%s' %s", opt->long_name, reason); - return error("option `%s' %s", opt->long_name, reason); -} - static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt, int flags, const char **arg) { @@ -594,3 +585,12 @@ static int parse_options_usage(struct parse_opt_ctx_t *ctx, return usage_with_options_internal(ctx, usagestr, opts, 0, err); } +#undef opterror +int opterror(const struct option *opt, const char *reason, int flags) +{ + if (flags & OPT_SHORT) + return error("switch `%c' %s", opt->short_name, reason); + if (flags & OPT_UNSET) + return error("option `no-%s' %s", opt->long_name, reason); + return error("option `%s' %s", opt->long_name, reason); +} -- cgit v1.2.1 From c082196575e13dd5960031f213b20e2df989ca18 Mon Sep 17 00:00:00 2001 From: Jiang Xin Date: Sat, 9 Feb 2013 14:31:09 +0800 Subject: Add utf8_fprintf helper that returns correct number of columns MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since command usages can be translated, they may include utf-8 encoded strings, and the output in console may not align well any more. This is because strlen() is different from strwidth() on utf-8 strings. A wrapper utf8_fprintf() can help to return the correct number of columns required. Signed-off-by: Jiang Xin Signed-off-by: Nguyễn Thái Ngọc Duy Reviewed-by: Torsten Bögershausen Signed-off-by: Junio C Hamano --- parse-options.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'parse-options.c') diff --git a/parse-options.c b/parse-options.c index c1c66bd408..670bf09b45 100644 --- a/parse-options.c +++ b/parse-options.c @@ -3,6 +3,7 @@ #include "cache.h" #include "commit.h" #include "color.h" +#include "utf8.h" static int parse_options_usage(struct parse_opt_ctx_t *ctx, const char * const *usagestr, @@ -491,7 +492,7 @@ static int usage_argh(const struct option *opts, FILE *outfile) s = literal ? "[%s]" : "[<%s>]"; else s = literal ? " %s" : " <%s>"; - return fprintf(outfile, s, opts->argh ? _(opts->argh) : _("...")); + return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("...")); } #define USAGE_OPTS_WIDTH 24 @@ -550,7 +551,7 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx, if (opts->long_name) pos += fprintf(outfile, "--%s", opts->long_name); if (opts->type == OPTION_NUMBER) - pos += fprintf(outfile, "-NUM"); + pos += utf8_fprintf(outfile, _("-NUM")); if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !(opts->flags & PARSE_OPT_NOARG)) -- cgit v1.2.1 From b141a47801d6fb2d68ec48adfe7597ec3ce49c0d Mon Sep 17 00:00:00 2001 From: Erik Faye-Lund Date: Tue, 12 Feb 2013 00:13:48 +0100 Subject: parse-options: report uncorrupted multi-byte options Because our command-line parser considers only one byte at the time for short-options, we incorrectly report only the first byte when multi-byte input was provided. This makes user-errors slightly awkward to diagnose for instance under UTF-8 locale and non-English keyboard layouts. Report the whole argument-string when a non-ASCII short-option is detected. Signed-off-by: Erik Faye-Lund Improved-by: Jeff King Signed-off-by: Junio C Hamano --- parse-options.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'parse-options.c') diff --git a/parse-options.c b/parse-options.c index c1c66bd408..052bf72bb8 100644 --- a/parse-options.c +++ b/parse-options.c @@ -470,8 +470,11 @@ int parse_options(int argc, const char **argv, const char *prefix, default: /* PARSE_OPT_UNKNOWN */ if (ctx.argv[0][1] == '-') { error("unknown option `%s'", ctx.argv[0] + 2); - } else { + } else if (isascii(*ctx.opt)) { error("unknown switch `%c'", *ctx.opt); + } else { + error("unknown non-ascii option in string: `%s'", + ctx.argv[0]); } usage_with_options(usagestr, options); } -- cgit v1.2.1