{"thread":{"id":"19239","subject":"[PATCH 0/5] grep parseopt conversion","startedAt":"2009-05-07T19:43:00Z","lastAt":"2009-05-20T22:05:22Z","messageCount":10,"participants":["René Scharfe","Jeff King","Stephen Boyd"],"isPatch":true,"patchVersion":1,"patchTotal":5},"messages":[{"id":"113252","messageId":"1241725380.4772.6.camel@ubuntu.ubuntu-domain","threadId":"19239","inReplyTo":null,"subject":"[PATCH 0/5] grep parseopt conversion","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2009-05-07T19:43:00Z","receivedAt":"2009-05-07T19:43:00Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"This series converts git-grep to parseopt.  The first three patches\nextend the parser to accept inverse bit options, numerical options\nand options without dashes:\n\n   [PATCH 1/5] parseopt: add OPT_NEGBIT\n   [PATCH 2/5] parseopt: add OPT_NUMBER_CALLBACK\n   [PATCH 3/5] parseopt: add PARSE_OPT_NODASH\n\nThe fourth patch is a cleanup:\n\n   [PATCH 4/5] grep: remove global variable builtin_grep\n\nThe last patch uses the added infrastructure in git-grep:\n\n   [PATCH 5/5] grep: use parseopt\n\n Documentation/technical/api-parse-options.txt |   12 +\n builtin-grep.c                                |  416 +++++++++++--------------\n grep.h                                        |   28 +-\n parse-options.c                               |   63 ++++-\n parse-options.h                               |   14 +-\n t/t0040-parse-options.sh                      |   56 ++++-\n test-parse-options.c                          |   11 +\n 7 files changed, 348 insertions(+), 252 deletions(-)\n"},{"id":"113253","messageId":"1241725457.4772.7.camel@ubuntu.ubuntu-domain","threadId":"19239","inReplyTo":"1241725380.4772.6.camel@ubuntu.ubuntu-domain","subject":"[PATCH 1/5] parseopt: add OPT_NEGBIT","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2009-05-07T19:44:17Z","receivedAt":"2009-05-07T19:44:17Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Add OPTION_NEGBIT and OPT_NEGBIT, mirroring OPTION_BIT and OPT_BIT.\nOPT_NEGBIT can be used together with OPT_BIT to define two options\nthat cancel each other out.\n\nNote: this patch removes the reminder from the test script because\nit adds a test for --no-or4 and there already was one for --or4.\n\nSigned-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>\n---\n Documentation/technical/api-parse-options.txt |    4 +++\n parse-options.c                               |    8 ++++++\n parse-options.h                               |    2 +\n t/t0040-parse-options.sh                      |   31 +++++++++++++++++++++++-\n test-parse-options.c                          |    1 +\n 5 files changed, 44 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/technical/api-parse-options.txt b/Documentation/technical/api-parse-options.txt\nindex e66ca9f..794194b 100644\n--- a/Documentation/technical/api-parse-options.txt\n+++ b/Documentation/technical/api-parse-options.txt\n@@ -137,6 +137,10 @@ There are some macros to easily define options:\n \tIntroduce a boolean option.\n \tIf used, `int_var` is bitwise-ored with `mask`.\n \n+`OPT_NEGBIT(short, long, &int_var, description, mask)`::\n+\tIntroduce a boolean option.\n+\tIf used, `int_var` is bitwise-anded with the inverted `mask`.\n+\n `OPT_SET_INT(short, long, &int_var, description, integer)`::\n \tIntroduce a boolean option.\n \tIf used, set `int_var` to `integer`.\ndiff --git a/parse-options.c b/parse-options.c\nindex cf71bcf..a8c05e3 100644\n--- a/parse-options.c\n+++ b/parse-options.c\n@@ -50,6 +50,7 @@ static int get_value(struct parse_opt_ctx_t *p,\n \t\t\t/* FALLTHROUGH */\n \t\tcase OPTION_BOOLEAN:\n \t\tcase OPTION_BIT:\n+\t\tcase OPTION_NEGBIT:\n \t\tcase OPTION_SET_INT:\n \t\tcase OPTION_SET_PTR:\n \t\t\treturn opterror(opt, \"takes no value\", flags);\n@@ -66,6 +67,13 @@ static int get_value(struct parse_opt_ctx_t *p,\n \t\t\t*(int *)opt->value |= opt->defval;\n \t\treturn 0;\n \n+\tcase OPTION_NEGBIT:\n+\t\tif (unset)\n+\t\t\t*(int *)opt->value |= opt->defval;\n+\t\telse\n+\t\t\t*(int *)opt->value &= ~opt->defval;\n+\t\treturn 0;\n+\n \tcase OPTION_BOOLEAN:\n \t\t*(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;\n \t\treturn 0;\ndiff --git a/parse-options.h b/parse-options.h\nindex b54eec1..f1e2452 100644\n--- a/parse-options.h\n+++ b/parse-options.h\n@@ -8,6 +8,7 @@ enum parse_opt_type {\n \tOPTION_GROUP,\n \t/* options with no arguments */\n \tOPTION_BIT,\n+\tOPTION_NEGBIT,\n \tOPTION_BOOLEAN, /* _INCR would have been a better name */\n \tOPTION_SET_INT,\n \tOPTION_SET_PTR,\n@@ -93,6 +94,7 @@ struct option {\n #define OPT_ARGUMENT(l, h)          { OPTION_ARGUMENT, 0, (l), NULL, NULL, (h) }\n #define OPT_GROUP(h)                { OPTION_GROUP, 0, NULL, NULL, NULL, (h) }\n #define OPT_BIT(s, l, v, h, b)      { OPTION_BIT, (s), (l), (v), NULL, (h), 0, NULL, (b) }\n+#define OPT_NEGBIT(s, l, v, h, b)   { OPTION_NEGBIT, (s), (l), (v), NULL, (h), 0, NULL, (b) }\n #define OPT_BOOLEAN(s, l, v, h)     { OPTION_BOOLEAN, (s), (l), (v), NULL, (h) }\n #define OPT_SET_INT(s, l, v, h, i)  { OPTION_SET_INT, (s), (l), (v), NULL, (h), 0, NULL, (i) }\n #define OPT_SET_PTR(s, l, v, h, p)  { OPTION_SET_PTR, (s), (l), (v), NULL, (h), 0, NULL, (p) }\ndiff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh\nindex e38241c..9054ed6 100755\n--- a/t/t0040-parse-options.sh\n+++ b/t/t0040-parse-options.sh\n@@ -12,6 +12,7 @@ usage: test-parse-options <options>\n \n     -b, --boolean         get a boolean\n     -4, --or4             bitwise-or boolean with ...0100\n+    --neg-or4             same as --no-or4\n \n     -i, --integer <n>     get a integer\n     -j <n>                get a integer, too\n@@ -245,7 +246,33 @@ test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '\n \ttest_cmp expect output\n '\n \n-# --or4\n-# --no-or4\n+test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '\n+\ttest-parse-options --set23 -bbbbb --neg-or4 > output 2> output.err &&\n+\ttest ! -s output.err &&\n+\ttest_cmp expect output\n+'\n+\n+cat > expect <<EOF\n+boolean: 6\n+integer: 0\n+timestamp: 0\n+string: (not set)\n+abbrev: 7\n+verbose: 0\n+quiet: no\n+dry run: no\n+EOF\n+\n+test_expect_success 'OPT_BIT() works' '\n+\ttest-parse-options -bb --or4 > output 2> output.err &&\n+\ttest ! -s output.err &&\n+\ttest_cmp expect output\n+'\n+\n+test_expect_success 'OPT_NEGBIT() works' '\n+\ttest-parse-options -bb --no-neg-or4 > output 2> output.err &&\n+\ttest ! -s output.err &&\n+\ttest_cmp expect output\n+'\n \n test_done\ndiff --git a/test-parse-options.c b/test-parse-options.c\nindex 61d2c39..eddc026 100644\n--- a/test-parse-options.c\n+++ b/test-parse-options.c\n@@ -29,6 +29,7 @@ int main(int argc, const char **argv)\n \t\tOPT_BOOLEAN('b', \"boolean\", &boolean, \"get a boolean\"),\n \t\tOPT_BIT('4', \"or4\", &boolean,\n \t\t\t\"bitwise-or boolean with ...0100\", 4),\n+\t\tOPT_NEGBIT(0, \"neg-or4\", &boolean, \"same as --no-or4\", 4),\n \t\tOPT_GROUP(\"\"),\n \t\tOPT_INTEGER('i', \"integer\", &integer, \"get a integer\"),\n \t\tOPT_INTEGER('j', NULL, &integer, \"get a integer, too\"),\n-- \n1.6.3\n"},{"id":"113254","messageId":"1241725508.4772.8.camel@ubuntu.ubuntu-domain","threadId":"19239","inReplyTo":"1241725380.4772.6.camel@ubuntu.ubuntu-domain","subject":"[PATCH 2/5] parseopt: add OPT_NUMBER_CALLBACK","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2009-05-07T19:45:08Z","receivedAt":"2009-05-07T19:45:08Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Add a way to recognize numerical options.  The number is passed to\na callback function as a string.\n\nSigned-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>\n---\n Documentation/technical/api-parse-options.txt |    8 +++++++\n parse-options.c                               |   26 ++++++++++++++++++++++++-\n parse-options.h                               |    4 +++\n t/t0040-parse-options.sh                      |   18 +++++++++++++++++\n test-parse-options.c                          |    8 +++++++\n 5 files changed, 63 insertions(+), 1 deletions(-)\n\ndiff --git a/Documentation/technical/api-parse-options.txt b/Documentation/technical/api-parse-options.txt\nindex 794194b..beca98d 100644\n--- a/Documentation/technical/api-parse-options.txt\n+++ b/Documentation/technical/api-parse-options.txt\n@@ -170,6 +170,14 @@ There are some macros to easily define options:\n `OPT_ARGUMENT(long, description)`::\n \tIntroduce a long-option argument that will be kept in `argv[]`.\n \n+`OPT_NUMBER_CALLBACK(&var, description, func_ptr)`::\n+\tRecognize numerical options like -123 and feed the integer as\n+\tif it was an argument to the function given by `func_ptr`.\n+\tThe result will be put into `var`.  There can be only one such\n+\toption definition.  It cannot be negated and it takes no\n+\targuments.  Short options that happen to be digits take\n+\tprecedence over it.\n+\n \n The last element of the array must be `OPT_END()`.\n \ndiff --git a/parse-options.c b/parse-options.c\nindex a8c05e3..aaa218d 100644\n--- a/parse-options.c\n+++ b/parse-options.c\n@@ -129,11 +129,33 @@ static int get_value(struct parse_opt_ctx_t *p,\n \n static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)\n {\n+\tconst struct option *numopt = NULL;\n+\n \tfor (; options->type != OPTION_END; options++) {\n \t\tif (options->short_name == *p->opt) {\n \t\t\tp->opt = p->opt[1] ? p->opt + 1 : NULL;\n \t\t\treturn get_value(p, options, OPT_SHORT);\n \t\t}\n+\n+\t\t/*\n+\t\t * Handle the numerical option later, explicit one-digit\n+\t\t * options take precedence over it.\n+\t\t */\n+\t\tif (options->type == OPTION_NUMBER)\n+\t\t\tnumopt = options;\n+\t}\n+\tif (numopt && isdigit(*p->opt)) {\n+\t\tsize_t len = 1;\n+\t\tchar *arg;\n+\t\tint rc;\n+\n+\t\twhile (isdigit(p->opt[len]))\n+\t\t\tlen++;\n+\t\targ = xmemdupz(p->opt, len);\n+\t\tp->opt = p->opt[len] ? p->opt + len : NULL;\n+\t\trc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;\n+\t\tfree(arg);\n+\t\treturn rc;\n \t}\n \treturn -2;\n }\n@@ -411,6 +433,8 @@ int usage_with_options_internal(const char * const *usagestr,\n \t\t\tpos += fprintf(stderr, \", \");\n \t\tif (opts->long_name)\n \t\t\tpos += fprintf(stderr, \"--%s\", opts->long_name);\n+\t\tif (opts->type == OPTION_NUMBER)\n+\t\t\tpos += fprintf(stderr, \"-NUM\");\n \n \t\tswitch (opts->type) {\n \t\tcase OPTION_ARGUMENT:\n@@ -447,7 +471,7 @@ int usage_with_options_internal(const char * const *usagestr,\n \t\t\t\t\tpos += fprintf(stderr, \" ...\");\n \t\t\t}\n \t\t\tbreak;\n-\t\tdefault: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */\n+\t\tdefault: /* OPTION_{BIT,BOOLEAN,NUMBER,SET_INT,SET_PTR} */\n \t\t\tbreak;\n \t\t}\n \ndiff --git a/parse-options.h b/parse-options.h\nindex f1e2452..77919a7 100644\n--- a/parse-options.h\n+++ b/parse-options.h\n@@ -6,6 +6,7 @@ enum parse_opt_type {\n \tOPTION_END,\n \tOPTION_ARGUMENT,\n \tOPTION_GROUP,\n+\tOPTION_NUMBER,\n \t/* options with no arguments */\n \tOPTION_BIT,\n \tOPTION_NEGBIT,\n@@ -105,6 +106,9 @@ struct option {\n \t  parse_opt_approxidate_cb }\n #define OPT_CALLBACK(s, l, v, a, h, f) \\\n \t{ OPTION_CALLBACK, (s), (l), (v), (a), (h), 0, (f) }\n+#define OPT_NUMBER_CALLBACK(v, h, f) \\\n+\t{ OPTION_NUMBER, 0, NULL, (v), NULL, (h), \\\n+\t  PARSE_OPT_NOARG | PARSE_OPT_NONEG, (f) }\n \n /* parse_options() will filter out the processed options and leave the\n  * non-option arguments in argv[].\ndiff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh\nindex 9054ed6..8ca62ef 100755\n--- a/t/t0040-parse-options.sh\n+++ b/t/t0040-parse-options.sh\n@@ -30,6 +30,7 @@ String options\n \n Magic arguments\n     --quux                means --quux\n+    -NUM                  set integer to NUM\n \n Standard options\n     --abbrev[=<n>]        use <n> digits to display SHA-1s\n@@ -275,4 +276,21 @@ test_expect_success 'OPT_NEGBIT() works' '\n \ttest_cmp expect output\n '\n \n+cat > expect <<EOF\n+boolean: 0\n+integer: 12345\n+timestamp: 0\n+string: (not set)\n+abbrev: 7\n+verbose: 0\n+quiet: no\n+dry run: no\n+EOF\n+\n+test_expect_success 'OPT_NUMBER_CALLBACK() works' '\n+\ttest-parse-options -12345 > output 2> output.err &&\n+\ttest ! -s output.err &&\n+\ttest_cmp expect output\n+'\n+\n test_done\ndiff --git a/test-parse-options.c b/test-parse-options.c\nindex eddc026..d46eac2 100644\n--- a/test-parse-options.c\n+++ b/test-parse-options.c\n@@ -19,6 +19,12 @@ int length_callback(const struct option *opt, const char *arg, int unset)\n \treturn 0;\n }\n \n+int number_callback(const struct option *opt, const char *arg, int unset)\n+{\n+\t*(int *)opt->value = strtol(arg, NULL, 10);\n+\treturn 0;\n+}\n+\n int main(int argc, const char **argv)\n {\n \tconst char *usage[] = {\n@@ -46,6 +52,8 @@ int main(int argc, const char **argv)\n \t\t\t\"set string to default\", (unsigned long)\"default\"),\n \t\tOPT_GROUP(\"Magic arguments\"),\n \t\tOPT_ARGUMENT(\"quux\", \"means --quux\"),\n+\t\tOPT_NUMBER_CALLBACK(&integer, \"set integer to NUM\",\n+\t\t\tnumber_callback),\n \t\tOPT_GROUP(\"Standard options\"),\n \t\tOPT__ABBREV(&abbrev),\n \t\tOPT__VERBOSE(&verbose),\n-- \n1.6.3\n"},{"id":"113255","messageId":"1241725542.4772.9.camel@ubuntu.ubuntu-domain","threadId":"19239","inReplyTo":"1241725380.4772.6.camel@ubuntu.ubuntu-domain","subject":"[PATCH 3/5] parseopt: add PARSE_OPT_NODASH","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2009-05-07T19:45:42Z","receivedAt":"2009-05-07T19:45:42Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Add support for options that don't start with a dash.  Initially, they\ndon't accept arguments and can only be short options, i.e. consist of a\nsingle character.\n\nSigned-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>\n---\n parse-options.c          |   29 +++++++++++++++++++++++++++--\n parse-options.h          |    8 ++++++--\n t/t0040-parse-options.sh |    7 +++++++\n test-parse-options.c     |    2 ++\n 4 files changed, 42 insertions(+), 4 deletions(-)\n\ndiff --git a/parse-options.c b/parse-options.c\nindex aaa218d..c52b8cc 100644\n--- a/parse-options.c\n+++ b/parse-options.c\n@@ -245,6 +245,25 @@ is_abbreviated:\n \treturn -2;\n }\n \n+static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,\n+\t\t\t    const struct option *options)\n+{\n+\tfor (; options->type != OPTION_END; options++) {\n+\t\tif (!(options->flags & PARSE_OPT_NODASH))\n+\t\t\tcontinue;\n+\t\tif ((options->flags & PARSE_OPT_OPTARG) ||\n+\t\t    !(options->flags & PARSE_OPT_NOARG))\n+\t\t\tdie(\"BUG: dashless options don't support arguments\");\n+\t\tif (!(options->flags & PARSE_OPT_NONEG))\n+\t\t\tdie(\"BUG: dashless options don't support negation\");\n+\t\tif (options->long_name)\n+\t\t\tdie(\"BUG: dashless options can't be long\");\n+\t\tif (options->short_name == arg[0] && arg[1] == '\\0')\n+\t\t\treturn get_value(p, options, OPT_SHORT);\n+\t}\n+\treturn -2;\n+}\n+\n static void check_typos(const char *arg, const struct option *options)\n {\n \tif (strlen(arg) < 3)\n@@ -295,6 +314,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,\n \t\tconst char *arg = ctx->argv[0];\n \n \t\tif (*arg != '-' || !arg[1]) {\n+\t\t\tif (parse_nodash_opt(ctx, arg, options) == 0)\n+\t\t\t\tcontinue;\n \t\t\tif (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)\n \t\t\t\tbreak;\n \t\t\tctx->out[ctx->cpidx++] = ctx->argv[0];\n@@ -427,8 +448,12 @@ int usage_with_options_internal(const char * const *usagestr,\n \t\t\tcontinue;\n \n \t\tpos = fprintf(stderr, \"    \");\n-\t\tif (opts->short_name)\n-\t\t\tpos += fprintf(stderr, \"-%c\", opts->short_name);\n+\t\tif (opts->short_name) {\n+\t\t\tif (opts->flags & PARSE_OPT_NODASH)\n+\t\t\t\tpos += fprintf(stderr, \"%c\", opts->short_name);\n+\t\t\telse\n+\t\t\t\tpos += fprintf(stderr, \"-%c\", opts->short_name);\n+\t\t}\n \t\tif (opts->long_name && opts->short_name)\n \t\t\tpos += fprintf(stderr, \", \");\n \t\tif (opts->long_name)\ndiff --git a/parse-options.h b/parse-options.h\nindex 77919a7..919b9b4 100644\n--- a/parse-options.h\n+++ b/parse-options.h\n@@ -33,6 +33,7 @@ enum parse_opt_option_flags {\n \tPARSE_OPT_NONEG   = 4,\n \tPARSE_OPT_HIDDEN  = 8,\n \tPARSE_OPT_LASTARG_DEFAULT = 16,\n+\tPARSE_OPT_NODASH = 32,\n };\n \n struct option;\n@@ -66,8 +67,11 @@ typedef int parse_opt_cb(const struct option *, const char *arg, int unset);\n  *   PARSE_OPT_OPTARG: says that the argument is optional (not for BOOLEANs)\n  *   PARSE_OPT_NOARG: says that this option takes no argument, for CALLBACKs\n  *   PARSE_OPT_NONEG: says that this option cannot be negated\n- *   PARSE_OPT_HIDDEN this option is skipped in the default usage, showed in\n- *                    the long one.\n+ *   PARSE_OPT_HIDDEN: this option is skipped in the default usage, and\n+ *                     shown only in the full usage.\n+ *   PARSE_OPT_LASTARG_DEFAULT: if no argument is given, the default value\n+ *                              is used.\n+ *   PARSE_OPT_NODASH: this option doesn't start with a dash.\n  *\n  * `callback`::\n  *   pointer to the callback to use for OPTION_CALLBACK.\ndiff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh\nindex 8ca62ef..a40c123 100755\n--- a/t/t0040-parse-options.sh\n+++ b/t/t0040-parse-options.sh\n@@ -31,6 +31,7 @@ String options\n Magic arguments\n     --quux                means --quux\n     -NUM                  set integer to NUM\n+    +                     same as -b\n \n Standard options\n     --abbrev[=<n>]        use <n> digits to display SHA-1s\n@@ -276,6 +277,12 @@ test_expect_success 'OPT_NEGBIT() works' '\n \ttest_cmp expect output\n '\n \n+test_expect_success 'OPT_BOOLEAN() with PARSE_OPT_NODASH works' '\n+\ttest-parse-options + + + + + + > output 2> output.err &&\n+\ttest ! -s output.err &&\n+\ttest_cmp expect output\n+'\n+\n cat > expect <<EOF\n boolean: 0\n integer: 12345\ndiff --git a/test-parse-options.c b/test-parse-options.c\nindex d46eac2..e0669dc 100644\n--- a/test-parse-options.c\n+++ b/test-parse-options.c\n@@ -54,6 +54,8 @@ int main(int argc, const char **argv)\n \t\tOPT_ARGUMENT(\"quux\", \"means --quux\"),\n \t\tOPT_NUMBER_CALLBACK(&integer, \"set integer to NUM\",\n \t\t\tnumber_callback),\n+\t\t{ OPTION_BOOLEAN, '+', NULL, &boolean, NULL, \"same as -b\",\n+\t\t  PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },\n \t\tOPT_GROUP(\"Standard options\"),\n \t\tOPT__ABBREV(&abbrev),\n \t\tOPT__VERBOSE(&verbose),\n-- \n1.6.3\n"},{"id":"113256","messageId":"1241725577.4772.10.camel@ubuntu.ubuntu-domain","threadId":"19239","inReplyTo":"1241725380.4772.6.camel@ubuntu.ubuntu-domain","subject":"[PATCH 4/5] grep: remove global variable builtin_grep","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2009-05-07T19:46:17Z","receivedAt":"2009-05-07T19:46:17Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Replace the only global variable in builtin-grep.c, builtin_grep, by a\nlocal one and a function parameter with reversed meaning.\n\nSigned-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>\n---\n builtin-grep.c |   14 +++++++-------\n 1 files changed, 7 insertions(+), 7 deletions(-)\n\ndiff --git a/builtin-grep.c b/builtin-grep.c\nindex f88a912..620399f 100644\n--- a/builtin-grep.c\n+++ b/builtin-grep.c\n@@ -20,8 +20,6 @@\n #endif\n #endif\n \n-static int builtin_grep;\n-\n static int grep_config(const char *var, const char *value, void *cb)\n {\n \tstruct grep_opt *opt = cb;\n@@ -432,7 +430,8 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)\n }\n #endif\n \n-static int grep_cache(struct grep_opt *opt, const char **paths, int cached)\n+static int grep_cache(struct grep_opt *opt, const char **paths, int cached,\n+\t\t      int external_grep_allowed)\n {\n \tint hit = 0;\n \tint nr;\n@@ -444,7 +443,7 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached)\n \t * we grep through the checked-out files. It tends to\n \t * be a lot more optimized\n \t */\n-\tif (!cached && !builtin_grep) {\n+\tif (!cached && external_grep_allowed) {\n \t\thit = external_grep(opt, paths, cached);\n \t\tif (hit >= 0)\n \t\t\treturn hit;\n@@ -574,6 +573,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)\n {\n \tint hit = 0;\n \tint cached = 0;\n+\tint external_grep_allowed = 1;\n \tint seen_dashdash = 0;\n \tstruct grep_opt opt;\n \tstruct object_array list = { 0, 0, NULL };\n@@ -612,7 +612,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)\n \t\t\tcontinue;\n \t\t}\n \t\tif (!strcmp(\"--no-ext-grep\", arg)) {\n-\t\t\tbuiltin_grep = 1;\n+\t\t\texternal_grep_allowed = 0;\n \t\t\tcontinue;\n \t\t}\n \t\tif (!strcmp(\"-a\", arg) ||\n@@ -823,7 +823,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)\n \t}\n \n \tif (opt.color && !opt.color_external)\n-\t\tbuiltin_grep = 1;\n+\t\texternal_grep_allowed = 0;\n \tif (!opt.pattern_list)\n \t\tdie(\"no pattern given.\");\n \tif ((opt.regflags != REG_NEWLINE) && opt.fixed)\n@@ -874,7 +874,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)\n \tif (!list.nr) {\n \t\tif (!cached)\n \t\t\tsetup_work_tree();\n-\t\treturn !grep_cache(&opt, paths, cached);\n+\t\treturn !grep_cache(&opt, paths, cached, external_grep_allowed);\n \t}\n \n \tif (cached)\n-- \n1.6.3\n"},{"id":"113257","messageId":"1241725608.4772.11.camel@ubuntu.ubuntu-domain","threadId":"19239","inReplyTo":"1241725380.4772.6.camel@ubuntu.ubuntu-domain","subject":"[PATCH 5/5] grep: use parseopt","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2009-05-07T19:46:48Z","receivedAt":"2009-05-07T19:46:48Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Convert git-grep to parseopt.\n\nThe bitfields in struct grep_opt are converted to full ints,\nincreasing its size.  This shouldn't be a problem as there is only a\nsingle instance in memory.\n\nSigned-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>\n---\n builtin-grep.c |  406 +++++++++++++++++++++++++-------------------------------\n grep.h         |   28 ++--\n 2 files changed, 194 insertions(+), 240 deletions(-)\n\ndiff --git a/builtin-grep.c b/builtin-grep.c\nindex 620399f..169a91c 100644\n--- a/builtin-grep.c\n+++ b/builtin-grep.c\n@@ -10,6 +10,7 @@\n #include \"tag.h\"\n #include \"tree-walk.h\"\n #include \"builtin.h\"\n+#include \"parse-options.h\"\n #include \"grep.h\"\n \n #ifndef NO_EXTERNAL_GREP\n@@ -20,6 +21,11 @@\n #endif\n #endif\n \n+static char const * const grep_usage[] = {\n+\t\"git grep [options] [-e] <pattern> [<rev>...] [[--] path...]\",\n+\tNULL\n+};\n+\n static int grep_config(const char *var, const char *value, void *cb)\n {\n \tstruct grep_opt *opt = cb;\n@@ -559,15 +565,86 @@ static int grep_object(struct grep_opt *opt, const char **paths,\n \tdie(\"unable to grep from object of type %s\", typename(obj->type));\n }\n \n-static const char builtin_grep_usage[] =\n-\"git grep <option>* [-e] <pattern> <rev>* [[--] <path>...]\";\n+int context_callback(const struct option *opt, const char *arg, int unset)\n+{\n+\tstruct grep_opt *grep_opt = opt->value;\n+\tint value;\n+\tconst char *endp;\n+\n+\tif (unset) {\n+\t\tgrep_opt->pre_context = grep_opt->post_context = 0;\n+\t\treturn 0;\n+\t}\n+\tvalue = strtol(arg, (char **)&endp, 10);\n+\tif (*endp) {\n+\t\treturn error(\"switch `%c' expects a numerical value\",\n+\t\t\t     opt->short_name);\n+\t}\n+\tgrep_opt->pre_context = grep_opt->post_context = value;\n+\treturn 0;\n+}\n+\n+int file_callback(const struct option *opt, const char *arg, int unset)\n+{\n+\tstruct grep_opt *grep_opt = opt->value;\n+\tFILE *patterns;\n+\tint lno = 0;\n+\tstruct strbuf sb;\n+\n+\tpatterns = fopen(arg, \"r\");\n+\tif (!patterns)\n+\t\tdie(\"'%s': %s\", arg, strerror(errno));\n+\twhile (strbuf_getline(&sb, patterns, '\\n') == 0) {\n+\t\t/* ignore empty line like grep does */\n+\t\tif (sb.len == 0)\n+\t\t\tcontinue;\n+\t\tappend_grep_pattern(grep_opt, strbuf_detach(&sb, NULL), arg,\n+\t\t\t\t    ++lno, GREP_PATTERN);\n+\t}\n+\tfclose(patterns);\n+\tstrbuf_release(&sb);\n+\treturn 0;\n+}\n+\n+int not_callback(const struct option *opt, const char *arg, int unset)\n+{\n+\tstruct grep_opt *grep_opt = opt->value;\n+\tappend_grep_pattern(grep_opt, \"--not\", \"command line\", 0, GREP_NOT);\n+\treturn 0;\n+}\n+\n+int and_callback(const struct option *opt, const char *arg, int unset)\n+{\n+\tstruct grep_opt *grep_opt = opt->value;\n+\tappend_grep_pattern(grep_opt, \"--and\", \"command line\", 0, GREP_AND);\n+\treturn 0;\n+}\n+\n+int open_callback(const struct option *opt, const char *arg, int unset)\n+{\n+\tstruct grep_opt *grep_opt = opt->value;\n+\tappend_grep_pattern(grep_opt, \"(\", \"command line\", 0, GREP_OPEN_PAREN);\n+\treturn 0;\n+}\n+\n+int close_callback(const struct option *opt, const char *arg, int unset)\n+{\n+\tstruct grep_opt *grep_opt = opt->value;\n+\tappend_grep_pattern(grep_opt, \")\", \"command line\", 0, GREP_CLOSE_PAREN);\n+\treturn 0;\n+}\n+\n+int pattern_callback(const struct option *opt, const char *arg, int unset)\n+{\n+\tstruct grep_opt *grep_opt = opt->value;\n+\tappend_grep_pattern(grep_opt, arg, \"-e option\", 0, GREP_PATTERN);\n+\treturn 0;\n+}\n \n-static const char emsg_invalid_context_len[] =\n-\"%s: invalid context length argument\";\n-static const char emsg_missing_context_len[] =\n-\"missing context length argument\";\n-static const char emsg_missing_argument[] =\n-\"option requires an argument -%s\";\n+int help_callback(const struct option *opt, const char *arg, int unset)\n+{\n+\treturn -1;\n+}\n \n int cmd_grep(int argc, const char **argv, const char *prefix)\n {\n@@ -579,6 +656,89 @@ int cmd_grep(int argc, const char **argv, const char *prefix)\n \tstruct object_array list = { 0, 0, NULL };\n \tconst char **paths = NULL;\n \tint i;\n+\tint dummy;\n+\tstruct option options[] = {\n+\t\tOPT_BOOLEAN(0, \"cached\", &cached,\n+\t\t\t\"search in index instead of in the work tree\"),\n+\t\tOPT_GROUP(\"\"),\n+\t\tOPT_BOOLEAN('v', \"invert-match\", &opt.invert,\n+\t\t\t\"show non-matching lines\"),\n+\t\tOPT_BIT('i', \"ignore-case\", &opt.regflags,\n+\t\t\t\"case insensitive matching\", REG_ICASE),\n+\t\tOPT_BOOLEAN('w', \"word-regexp\", &opt.word_regexp,\n+\t\t\t\"match patterns only at word boundaries\"),\n+\t\tOPT_SET_INT('a', \"text\", &opt.binary,\n+\t\t\t\"process binary files as text\", GREP_BINARY_TEXT),\n+\t\tOPT_SET_INT('I', NULL, &opt.binary,\n+\t\t\t\"don't match patterns in binary files\",\n+\t\t\tGREP_BINARY_NOMATCH),\n+\t\tOPT_GROUP(\"\"),\n+\t\tOPT_BIT('E', \"extended-regexp\", &opt.regflags,\n+\t\t\t\"use extended POSIX regular expressions\", REG_EXTENDED),\n+\t\tOPT_NEGBIT('G', \"basic-regexp\", &opt.regflags,\n+\t\t\t\"use basic POSIX regular expressions (default)\",\n+\t\t\tREG_EXTENDED),\n+\t\tOPT_BOOLEAN('F', \"fixed-strings\", &opt.fixed,\n+\t\t\t\"interpret patterns as fixed strings\"),\n+\t\tOPT_GROUP(\"\"),\n+\t\tOPT_BOOLEAN('n', NULL, &opt.linenum, \"show line numbers\"),\n+\t\tOPT_NEGBIT('h', NULL, &opt.pathname, \"don't show filenames\", 1),\n+\t\tOPT_BIT('H', NULL, &opt.pathname, \"show filenames\", 1),\n+\t\tOPT_NEGBIT(0, \"full-name\", &opt.relative,\n+\t\t\t\"show filenames relative to top directory\", 1),\n+\t\tOPT_BOOLEAN('l', \"files-with-matches\", &opt.name_only,\n+\t\t\t\"show only filenames instead of matching lines\"),\n+\t\tOPT_BOOLEAN(0, \"name-only\", &opt.name_only,\n+\t\t\t\"synonym for --files-with-matches\"),\n+\t\tOPT_BOOLEAN('L', \"files-without-match\",\n+\t\t\t&opt.unmatch_name_only,\n+\t\t\t\"show only the names of files without match\"),\n+\t\tOPT_BOOLEAN('z', \"null\", &opt.null_following_name,\n+\t\t\t\"print NUL after filenames\"),\n+\t\tOPT_BOOLEAN('c', \"count\", &opt.count,\n+\t\t\t\"show the number of matches instead of matching lines\"),\n+\t\tOPT_SET_INT(0, \"color\", &opt.color, \"highlight matches\", 1),\n+\t\tOPT_GROUP(\"\"),\n+\t\tOPT_CALLBACK('C', NULL, &opt, \"n\",\n+\t\t\t\"show <n> context lines before and after matches\",\n+\t\t\tcontext_callback),\n+\t\tOPT_INTEGER('B', NULL, &opt.pre_context,\n+\t\t\t\"show <n> context lines before matches\"),\n+\t\tOPT_INTEGER('A', NULL, &opt.post_context,\n+\t\t\t\"show <n> context lines after matches\"),\n+\t\tOPT_NUMBER_CALLBACK(&opt, \"shortcut for -C NUM\",\n+\t\t\tcontext_callback),\n+\t\tOPT_GROUP(\"\"),\n+\t\tOPT_CALLBACK('f', NULL, &opt, \"file\",\n+\t\t\t\"read patterns from file\", file_callback),\n+\t\t{ OPTION_CALLBACK, 'e', NULL, &opt, \"pattern\",\n+\t\t\t\"match <pattern>\", PARSE_OPT_NONEG, pattern_callback },\n+\t\t{ OPTION_CALLBACK, 0, \"and\", &opt, NULL,\n+\t\t  \"combine patterns specified with -e\",\n+\t\t  PARSE_OPT_NOARG | PARSE_OPT_NONEG, and_callback },\n+\t\tOPT_BOOLEAN(0, \"or\", &dummy, \"\"),\n+\t\t{ OPTION_CALLBACK, 0, \"not\", &opt, NULL, \"\",\n+\t\t  PARSE_OPT_NOARG | PARSE_OPT_NONEG, not_callback },\n+\t\t{ OPTION_CALLBACK, '(', NULL, &opt, NULL, \"\",\n+\t\t  PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH,\n+\t\t  open_callback },\n+\t\t{ OPTION_CALLBACK, ')', NULL, &opt, NULL, \"\",\n+\t\t  PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH,\n+\t\t  close_callback },\n+\t\tOPT_BOOLEAN(0, \"all-match\", &opt.all_match,\n+\t\t\t\"show only matches from files that match all patterns\"),\n+\t\tOPT_GROUP(\"\"),\n+#if NO_EXTERNAL_GREP\n+\t\tOPT_BOOLEAN(0, \"ext-grep\", &external_grep_allowed,\n+\t\t\t\"allow calling of grep(1) (ignored by this build)\"),\n+#else\n+\t\tOPT_BOOLEAN(0, \"ext-grep\", &external_grep_allowed,\n+\t\t\t\"allow calling of grep(1) (default)\"),\n+#endif\n+\t\t{ OPTION_CALLBACK, 0, \"help-all\", &options, NULL, \"show usage\",\n+\t\t  PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback },\n+\t\tOPT_END()\n+\t};\n \n \tmemset(&opt, 0, sizeof(opt));\n \topt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;\n@@ -603,223 +763,17 @@ int cmd_grep(int argc, const char **argv, const char *prefix)\n \t * unrecognized non option is the beginning of the refs list\n \t * that continues up to the -- (if exists), and then paths.\n \t */\n-\n-\twhile (1 < argc) {\n-\t\tconst char *arg = argv[1];\n-\t\targc--; argv++;\n-\t\tif (!strcmp(\"--cached\", arg)) {\n-\t\t\tcached = 1;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"--no-ext-grep\", arg)) {\n-\t\t\texternal_grep_allowed = 0;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"-a\", arg) ||\n-\t\t    !strcmp(\"--text\", arg)) {\n-\t\t\topt.binary = GREP_BINARY_TEXT;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"-i\", arg) ||\n-\t\t    !strcmp(\"--ignore-case\", arg)) {\n-\t\t\topt.regflags |= REG_ICASE;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"-I\", arg)) {\n-\t\t\topt.binary = GREP_BINARY_NOMATCH;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"-v\", arg) ||\n-\t\t    !strcmp(\"--invert-match\", arg)) {\n-\t\t\topt.invert = 1;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"-E\", arg) ||\n-\t\t    !strcmp(\"--extended-regexp\", arg)) {\n-\t\t\topt.regflags |= REG_EXTENDED;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"-F\", arg) ||\n-\t\t    !strcmp(\"--fixed-strings\", arg)) {\n-\t\t\topt.fixed = 1;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"-G\", arg) ||\n-\t\t    !strcmp(\"--basic-regexp\", arg)) {\n-\t\t\topt.regflags &= ~REG_EXTENDED;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"-n\", arg)) {\n-\t\t\topt.linenum = 1;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"-h\", arg)) {\n-\t\t\topt.pathname = 0;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"-H\", arg)) {\n-\t\t\topt.pathname = 1;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"-l\", arg) ||\n-\t\t    !strcmp(\"--name-only\", arg) ||\n-\t\t    !strcmp(\"--files-with-matches\", arg)) {\n-\t\t\topt.name_only = 1;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"-L\", arg) ||\n-\t\t    !strcmp(\"--files-without-match\", arg)) {\n-\t\t\topt.unmatch_name_only = 1;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"-z\", arg) ||\n-\t\t    !strcmp(\"--null\", arg)) {\n-\t\t\topt.null_following_name = 1;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"-c\", arg) ||\n-\t\t    !strcmp(\"--count\", arg)) {\n-\t\t\topt.count = 1;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"-w\", arg) ||\n-\t\t    !strcmp(\"--word-regexp\", arg)) {\n-\t\t\topt.word_regexp = 1;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!prefixcmp(arg, \"-A\") ||\n-\t\t    !prefixcmp(arg, \"-B\") ||\n-\t\t    !prefixcmp(arg, \"-C\") ||\n-\t\t    (arg[0] == '-' && '1' <= arg[1] && arg[1] <= '9')) {\n-\t\t\tunsigned num;\n-\t\t\tconst char *scan;\n-\t\t\tswitch (arg[1]) {\n-\t\t\tcase 'A': case 'B': case 'C':\n-\t\t\t\tif (!arg[2]) {\n-\t\t\t\t\tif (argc <= 1)\n-\t\t\t\t\t\tdie(emsg_missing_context_len);\n-\t\t\t\t\tscan = *++argv;\n-\t\t\t\t\targc--;\n-\t\t\t\t}\n-\t\t\t\telse\n-\t\t\t\t\tscan = arg + 2;\n-\t\t\t\tbreak;\n-\t\t\tdefault:\n-\t\t\t\tscan = arg + 1;\n-\t\t\t\tbreak;\n-\t\t\t}\n-\t\t\tif (strtoul_ui(scan, 10, &num))\n-\t\t\t\tdie(emsg_invalid_context_len, scan);\n-\t\t\tswitch (arg[1]) {\n-\t\t\tcase 'A':\n-\t\t\t\topt.post_context = num;\n-\t\t\t\tbreak;\n-\t\t\tdefault:\n-\t\t\tcase 'C':\n-\t\t\t\topt.post_context = num;\n-\t\t\tcase 'B':\n-\t\t\t\topt.pre_context = num;\n-\t\t\t\tbreak;\n-\t\t\t}\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"-f\", arg)) {\n-\t\t\tFILE *patterns;\n-\t\t\tint lno = 0;\n-\t\t\tchar buf[1024];\n-\t\t\tif (argc <= 1)\n-\t\t\t\tdie(emsg_missing_argument, arg);\n-\t\t\tpatterns = fopen(argv[1], \"r\");\n-\t\t\tif (!patterns)\n-\t\t\t\tdie(\"'%s': %s\", argv[1], strerror(errno));\n-\t\t\twhile (fgets(buf, sizeof(buf), patterns)) {\n-\t\t\t\tint len = strlen(buf);\n-\t\t\t\tif (len && buf[len-1] == '\\n')\n-\t\t\t\t\tbuf[len-1] = 0;\n-\t\t\t\t/* ignore empty line like grep does */\n-\t\t\t\tif (!buf[0])\n-\t\t\t\t\tcontinue;\n-\t\t\t\tappend_grep_pattern(&opt, xstrdup(buf),\n-\t\t\t\t\t\t    argv[1], ++lno,\n-\t\t\t\t\t\t    GREP_PATTERN);\n-\t\t\t}\n-\t\t\tfclose(patterns);\n-\t\t\targv++;\n-\t\t\targc--;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"--not\", arg)) {\n-\t\t\tappend_grep_pattern(&opt, arg, \"command line\", 0,\n-\t\t\t\t\t    GREP_NOT);\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"--and\", arg)) {\n-\t\t\tappend_grep_pattern(&opt, arg, \"command line\", 0,\n-\t\t\t\t\t    GREP_AND);\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"--or\", arg))\n-\t\t\tcontinue; /* no-op */\n-\t\tif (!strcmp(\"(\", arg)) {\n-\t\t\tappend_grep_pattern(&opt, arg, \"command line\", 0,\n-\t\t\t\t\t    GREP_OPEN_PAREN);\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\")\", arg)) {\n-\t\t\tappend_grep_pattern(&opt, arg, \"command line\", 0,\n-\t\t\t\t\t    GREP_CLOSE_PAREN);\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"--all-match\", arg)) {\n-\t\t\topt.all_match = 1;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"-e\", arg)) {\n-\t\t\tif (1 < argc) {\n-\t\t\t\tappend_grep_pattern(&opt, argv[1],\n-\t\t\t\t\t\t    \"-e option\", 0,\n-\t\t\t\t\t\t    GREP_PATTERN);\n-\t\t\t\targv++;\n-\t\t\t\targc--;\n-\t\t\t\tcontinue;\n-\t\t\t}\n-\t\t\tdie(emsg_missing_argument, arg);\n-\t\t}\n-\t\tif (!strcmp(\"--full-name\", arg)) {\n-\t\t\topt.relative = 0;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"--color\", arg)) {\n-\t\t\topt.color = 1;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"--no-color\", arg)) {\n-\t\t\topt.color = 0;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(\"--\", arg)) {\n-\t\t\t/* later processing wants to have this at argv[1] */\n-\t\t\targv--;\n-\t\t\targc++;\n-\t\t\tbreak;\n-\t\t}\n-\t\tif (*arg == '-')\n-\t\t\tusage(builtin_grep_usage);\n-\n-\t\t/* First unrecognized non-option token */\n-\t\tif (!opt.pattern_list) {\n-\t\t\tappend_grep_pattern(&opt, arg, \"command line\", 0,\n-\t\t\t\t\t    GREP_PATTERN);\n-\t\t\tbreak;\n-\t\t}\n-\t\telse {\n-\t\t\t/* We are looking at the first path or rev;\n-\t\t\t * it is found at argv[1] after leaving the\n-\t\t\t * loop.\n-\t\t\t */\n-\t\t\targc++; argv--;\n-\t\t\tbreak;\n-\t\t}\n+\targc = parse_options(argc, argv, options, grep_usage,\n+\t\t\t     PARSE_OPT_KEEP_DASHDASH |\n+\t\t\t     PARSE_OPT_STOP_AT_NON_OPTION |\n+\t\t\t     PARSE_OPT_NO_INTERNAL_HELP);\n+\n+\t/* First unrecognized non-option token */\n+\tif (argc > 0 && !opt.pattern_list) {\n+\t\tappend_grep_pattern(&opt, argv[0], \"command line\", 0,\n+\t\t\t\t    GREP_PATTERN);\n+\t\targv++;\n+\t\targc--;\n \t}\n \n \tif (opt.color && !opt.color_external)\n@@ -831,7 +785,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)\n \tcompile_grep_patterns(&opt);\n \n \t/* Check revs and then paths */\n-\tfor (i = 1; i < argc; i++) {\n+\tfor (i = 0; i < argc; i++) {\n \t\tconst char *arg = argv[i];\n \t\tunsigned char sha1[20];\n \t\t/* Is it a rev? */\ndiff --git a/grep.h b/grep.h\nindex a67005d..464e272 100644\n--- a/grep.h\n+++ b/grep.h\n@@ -61,23 +61,23 @@ struct grep_opt {\n \tstruct grep_expr *pattern_expression;\n \tint prefix_length;\n \tregex_t regexp;\n-\tunsigned linenum:1;\n-\tunsigned invert:1;\n-\tunsigned status_only:1;\n-\tunsigned name_only:1;\n-\tunsigned unmatch_name_only:1;\n-\tunsigned count:1;\n-\tunsigned word_regexp:1;\n-\tunsigned fixed:1;\n-\tunsigned all_match:1;\n+\tint linenum;\n+\tint invert;\n+\tint status_only;\n+\tint name_only;\n+\tint unmatch_name_only;\n+\tint count;\n+\tint word_regexp;\n+\tint fixed;\n+\tint all_match;\n #define GREP_BINARY_DEFAULT\t0\n #define GREP_BINARY_NOMATCH\t1\n #define GREP_BINARY_TEXT\t2\n-\tunsigned binary:2;\n-\tunsigned extended:1;\n-\tunsigned relative:1;\n-\tunsigned pathname:1;\n-\tunsigned null_following_name:1;\n+\tint binary;\n+\tint extended;\n+\tint relative;\n+\tint pathname;\n+\tint null_following_name;\n \tint color;\n \tchar color_match[COLOR_MAXLEN];\n \tconst char *color_external;\n-- \n1.6.3\n"},{"id":"113303","messageId":"20090508050117.GB8909@coredump.intra.peff.net","threadId":"19239","inReplyTo":"1241725457.4772.7.camel@ubuntu.ubuntu-domain","subject":"Re: [PATCH 1/5] parseopt: add OPT_NEGBIT","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2009-05-08T05:01:17Z","receivedAt":"2009-05-08T05:01:17Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, May 07, 2009 at 09:44:17PM +0200, René Scharfe wrote:\n\n> Add OPTION_NEGBIT and OPT_NEGBIT, mirroring OPTION_BIT and OPT_BIT.\n> OPT_NEGBIT can be used together with OPT_BIT to define two options\n> that cancel each other out.\n\nNice, this neatly fixes the problem with --no-no-empty-directory in\n\"ls-files\" discussed here:\n\n  http://article.gmane.org/gmane.comp.version-control.git/112607\n\nWe just need this patch on top of René's 1/5:\n\n-- >8 --\nSubject: [PATCH] ls-files: make --no-empty-directory properly negatable\n\nThis option was specified to parseopt as an OPT_BIT; however, we\nactually want to _set_ the bit on --no-empty-directory. Thus the\nexisting implementation used --no-empty-directory, and required\n--no-no-empty-directory to negate it.\n\nNow that OPT_NEGBIT exists, we can properly support it as\n--empty-directory and --no-empty-directory (but of course\nstill defaulting to showing empty directories).\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n builtin-ls-files.c |    2 +-\n 1 files changed, 1 insertions(+), 1 deletions(-)\n\ndiff --git a/builtin-ls-files.c b/builtin-ls-files.c\nindex da2daf4..3d59b0e 100644\n--- a/builtin-ls-files.c\n+++ b/builtin-ls-files.c\n@@ -454,7 +454,7 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)\n \t\tOPT_BIT(0, \"directory\", &dir.flags,\n \t\t\t\"show 'other' directories' name only\",\n \t\t\tDIR_SHOW_OTHER_DIRECTORIES),\n-\t\tOPT_BIT(0, \"no-empty-directory\", &dir.flags,\n+\t\tOPT_NEGBIT(0, \"empty-directory\", &dir.flags,\n \t\t\t\"don't show empty directories\",\n \t\t\tDIR_HIDE_EMPTY_DIRECTORIES),\n \t\tOPT_BOOLEAN('u', \"unmerged\", &show_unmerged,\n-- \n1.6.3.201.g5d2a4.dirty\n"},{"id":"114087","messageId":"4A0E8AEF.3000704@gmail.com","threadId":"19239","inReplyTo":"1241725608.4772.11.camel@ubuntu.ubuntu-domain","subject":"Re: [PATCH 5/5] grep: use parseopt","fromName":"Stephen Boyd","fromEmail":"bebarino@gmail.com","sentAt":"2009-05-16T09:44:15Z","receivedAt":"2009-05-16T09:44:15Z","isPatch":true,"sender":{"key":"bebarino@gmail.com","avatar":"https://avatars.githubusercontent.com/u/38832?v=4"},"body":"René Scharfe wrote:\n> +int context_callback(const struct option *opt, const char *arg, int unset)\n> +{\n\nShouldn't these callbacks be static?\n"},{"id":"114144","messageId":"4A10694C.5010109@lsrfire.ath.cx","threadId":"19239","inReplyTo":"4A0E8AEF.3000704@gmail.com","subject":"Re: [PATCH 5/5] grep: use parseopt","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2009-05-17T19:45:16Z","receivedAt":"2009-05-17T19:45:16Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Stephen Boyd schrieb:\n> René Scharfe wrote:\n>> +int context_callback(const struct option *opt, const char *arg, int unset)\n>> +{\n> \n> Shouldn't these callbacks be static?\n\nOh, yes, they should.\n\nRené\n"},{"id":"114372","messageId":"4A147EA2.7050203@lsrfire.ath.cx","threadId":"19239","inReplyTo":"4A0E8AEF.3000704@gmail.com","subject":"[PATCH] grep: make callback functions static","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2009-05-20T22:05:22Z","receivedAt":"2009-05-20T22:05:22Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Suggested by Stephen Boyd: make the callback functions used for option\nparsing static.\n\nSigned-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>\n---\nPatch applies to next.\n\n builtin-grep.c |   18 ++++++++++--------\n 1 files changed, 10 insertions(+), 8 deletions(-)\n\ndiff --git a/builtin-grep.c b/builtin-grep.c\nindex 169a91c..5308b34 100644\n--- a/builtin-grep.c\n+++ b/builtin-grep.c\n@@ -565,7 +565,8 @@ static int grep_object(struct grep_opt *opt, const char **paths,\n \tdie(\"unable to grep from object of type %s\", typename(obj->type));\n }\n \n-int context_callback(const struct option *opt, const char *arg, int unset)\n+static int context_callback(const struct option *opt, const char *arg,\n+\t\t\t    int unset)\n {\n \tstruct grep_opt *grep_opt = opt->value;\n \tint value;\n@@ -584,7 +585,7 @@ int context_callback(const struct option *opt, const char *arg, int unset)\n \treturn 0;\n }\n \n-int file_callback(const struct option *opt, const char *arg, int unset)\n+static int file_callback(const struct option *opt, const char *arg, int unset)\n {\n \tstruct grep_opt *grep_opt = opt->value;\n \tFILE *patterns;\n@@ -606,42 +607,43 @@ int file_callback(const struct option *opt, const char *arg, int unset)\n \treturn 0;\n }\n \n-int not_callback(const struct option *opt, const char *arg, int unset)\n+static int not_callback(const struct option *opt, const char *arg, int unset)\n {\n \tstruct grep_opt *grep_opt = opt->value;\n \tappend_grep_pattern(grep_opt, \"--not\", \"command line\", 0, GREP_NOT);\n \treturn 0;\n }\n \n-int and_callback(const struct option *opt, const char *arg, int unset)\n+static int and_callback(const struct option *opt, const char *arg, int unset)\n {\n \tstruct grep_opt *grep_opt = opt->value;\n \tappend_grep_pattern(grep_opt, \"--and\", \"command line\", 0, GREP_AND);\n \treturn 0;\n }\n \n-int open_callback(const struct option *opt, const char *arg, int unset)\n+static int open_callback(const struct option *opt, const char *arg, int unset)\n {\n \tstruct grep_opt *grep_opt = opt->value;\n \tappend_grep_pattern(grep_opt, \"(\", \"command line\", 0, GREP_OPEN_PAREN);\n \treturn 0;\n }\n \n-int close_callback(const struct option *opt, const char *arg, int unset)\n+static int close_callback(const struct option *opt, const char *arg, int unset)\n {\n \tstruct grep_opt *grep_opt = opt->value;\n \tappend_grep_pattern(grep_opt, \")\", \"command line\", 0, GREP_CLOSE_PAREN);\n \treturn 0;\n }\n \n-int pattern_callback(const struct option *opt, const char *arg, int unset)\n+static int pattern_callback(const struct option *opt, const char *arg,\n+\t\t\t    int unset)\n {\n \tstruct grep_opt *grep_opt = opt->value;\n \tappend_grep_pattern(grep_opt, arg, \"-e option\", 0, GREP_PATTERN);\n \treturn 0;\n }\n \n-int help_callback(const struct option *opt, const char *arg, int unset)\n+static int help_callback(const struct option *opt, const char *arg, int unset)\n {\n \treturn -1;\n }\n-- \n1.6.3.1\n"}]}