[PATCH 6/7] parse-opt: add PARSE_OPT_KEEP_ARGV0 parser option.

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Pierre Habouzit
Date: Tuesday, June 24, 2008 - 2:12 am

This way, argv[0] isn't clobbered, to the cost of maybe not having a
resulting NULL terminated argv array.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
 parse-options.c |    7 ++++---
 parse-options.h |    2 ++
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/parse-options.c b/parse-options.c
index ee82cf3..a6b5e04 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -246,7 +246,7 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
 	memset(ctx, 0, sizeof(*ctx));
 	ctx->argc = argc - 1;
 	ctx->argv = argv + 1;
-	ctx->out  = argv;
+	ctx->out  = argv + ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
 	ctx->flags = flags;
 	strbuf_init(&ctx->buf, 0);
 }
@@ -327,10 +327,11 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
 
 int parse_options_end(struct parse_opt_ctx_t *ctx)
 {
+	int res = ctx->cpidx + ctx->argc;
 	memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
-	ctx->out[ctx->cpidx + ctx->argc] = NULL;
+	ctx->out[res] = NULL;
 	strbuf_release(&ctx->buf);
-	return ctx->cpidx + ctx->argc;
+	return res + ((ctx->flags & PARSE_OPT_KEEP_ARGV0) != 0);
 }
 
 int parse_options(int argc, const char **argv, const struct option *options,
diff --git a/parse-options.h b/parse-options.h
index 14447d5..6745c7d 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -22,6 +22,8 @@ enum parse_opt_type {
 enum parse_opt_flags {
 	PARSE_OPT_KEEP_DASHDASH = 1,
 	PARSE_OPT_STOP_AT_NON_OPTION = 2,
+	/* using that option, the filtered argv may not be NULL terminated */
+	PARSE_OPT_KEEP_ARGV0 = 4,
 };
 
 enum parse_opt_option_flags {
-- 
1.5.6.110.g736c7.dirty

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Convert 'git blame' to parse_options(), Linus Torvalds, (Sun Jun 22, 10:15 pm)
Re: Convert 'git blame' to parse_options(), Junio C Hamano, (Sun Jun 22, 11:35 pm)
[RFC] Re: Convert 'git blame' to parse_options(), Pierre Habouzit, (Mon Jun 23, 1:22 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Johannes Schindelin, (Mon Jun 23, 5:26 am)
Re: Convert 'git blame' to parse_options(), Johannes Schindelin, (Mon Jun 23, 5:28 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Pierre Habouzit, (Mon Jun 23, 8:53 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Linus Torvalds, (Mon Jun 23, 9:11 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Linus Torvalds, (Mon Jun 23, 9:25 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Johannes Schindelin, (Mon Jun 23, 9:25 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Johannes Schindelin, (Mon Jun 23, 10:04 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Linus Torvalds, (Mon Jun 23, 10:06 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Jeff King, (Mon Jun 23, 10:15 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Linus Torvalds, (Mon Jun 23, 10:21 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Jeff King, (Mon Jun 23, 10:26 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Linus Torvalds, (Mon Jun 23, 10:32 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Jeff King, (Mon Jun 23, 11:15 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Linus Torvalds, (Mon Jun 23, 11:20 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Jeff King, (Mon Jun 23, 11:33 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Linus Torvalds, (Mon Jun 23, 11:36 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Johannes Schindelin, (Mon Jun 23, 11:39 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Johannes Schindelin, (Mon Jun 23, 11:41 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Linus Torvalds, (Mon Jun 23, 11:47 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Linus Torvalds, (Mon Jun 23, 12:16 pm)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Pierre Habouzit, (Mon Jun 23, 12:24 pm)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Jeff King, (Mon Jun 23, 12:53 pm)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Pierre Habouzit, (Mon Jun 23, 1:04 pm)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Linus Torvalds, (Mon Jun 23, 1:12 pm)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Pierre Habouzit, (Mon Jun 23, 2:09 pm)
[PATCH] parse-opt: have parse_options_{start,end}., Pierre Habouzit, (Mon Jun 23, 2:11 pm)
[PATCH] parse-opt: Export a non NORETURN usage dumper., Pierre Habouzit, (Mon Jun 23, 2:11 pm)
[PATCH] parse-opt: create parse_options_step., Pierre Habouzit, (Mon Jun 23, 2:11 pm)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Pierre Habouzit, (Mon Jun 23, 2:23 pm)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Junio C Hamano, (Mon Jun 23, 2:23 pm)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Linus Torvalds, (Mon Jun 23, 2:26 pm)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Pierre Habouzit, (Mon Jun 23, 2:28 pm)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Linus Torvalds, (Mon Jun 23, 2:41 pm)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Pierre Habouzit, (Mon Jun 23, 2:47 pm)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Junio C Hamano, (Mon Jun 23, 3:11 pm)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Pierre Habouzit, (Mon Jun 23, 3:24 pm)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Pierre Habouzit, (Mon Jun 23, 3:36 pm)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Junio C Hamano, (Mon Jun 23, 5:30 pm)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Jeff King, (Mon Jun 23, 10:35 pm)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Pierre Habouzit, (Tue Jun 24, 1:24 am)
Making parse-opt incremental, reworked series, Pierre Habouzit, (Tue Jun 24, 2:12 am)
[PATCH 1/7] parse-opt: have parse_options_{start,end}., Pierre Habouzit, (Tue Jun 24, 2:12 am)
[PATCH 2/7] parse-opt: Export a non NORETURN usage dumper., Pierre Habouzit, (Tue Jun 24, 2:12 am)
[PATCH 3/7] parse-opt: create parse_options_step., Pierre Habouzit, (Tue Jun 24, 2:12 am)
[PATCH 6/7] parse-opt: add PARSE_OPT_KEEP_ARGV0 parser option., Pierre Habouzit, (Tue Jun 24, 2:12 am)
[PATCH 7/7] Migrate git-blame to parse-option partially., Pierre Habouzit, (Tue Jun 24, 2:12 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Linus Torvalds, (Tue Jun 24, 9:59 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Linus Torvalds, (Tue Jun 24, 10:05 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Johannes Schindelin, (Tue Jun 24, 10:13 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Jeff King, (Tue Jun 24, 10:34 am)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Linus Torvalds, (Tue Jun 24, 10:44 am)
Re: [PATCH 5/7] parse-opt: fake short strings for callers ..., Pierre Habouzit, (Tue Jun 24, 12:26 pm)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Pierre Habouzit, (Tue Jun 24, 12:30 pm)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Pierre Habouzit, (Tue Jun 24, 12:43 pm)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Jeff King, (Tue Jun 24, 12:46 pm)
Re: [RFC] Re: Convert 'git blame' to parse_options(), Johannes Sixt, (Tue Jun 24, 11:09 pm)
Re: [PATCH 5/7] parse-opt: fake short strings for callers ..., Andreas Ericsson, (Wed Jun 25, 8:07 am)