git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
To: Steven Grimm <koreth@midwinter.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH v2] Allow commit (and tag) messages to be edited when $EDITOR has arguments
Date: Sun, 16 Dec 2007 15:36:44 +0000 (GMT)	[thread overview]
Message-ID: <Pine.LNX.4.64.0712161525090.27959@racer.site> (raw)
In-Reply-To: <20071216073408.GA5343@midwinter.com>

Hi,

On Sat, 15 Dec 2007, Steven Grimm wrote:

> @@ -238,7 +186,7 @@ struct cmd_struct {
>  	int option;
>  };
>  
> -static int run_command(struct cmd_struct *p, int argc, const char **argv)
> +static int run_git_command(struct cmd_struct *p, int argc, const char **argv)
>  {
>  	int status;
>  	struct stat st;

Funny ;-) I have a similar change in my (now-inactive until 1.5.4) tree, 
but I renamed it to execv_git_builtin (because it is not supposed to 
return in case of success).

> diff --git a/run-command.c b/run-command.c
> index 476d00c..3ae55ec 100644
> --- a/run-command.c
> +++ b/run-command.c
> @@ -237,3 +237,68 @@ int finish_async(struct async *async)
>  		ret = error("waitpid (async) failed");
>  	return ret;
>  }
> +
> +/*
> + * Parses a command line into an array of char* representing the tokens
> + * on the command line.  Pass in a count to reserve some number of additional
> + * slots in the allocated array, e.g., so the caller can add a filename
> + * argument without having to reallocate the array.

In the editor call, you said "2" for this, but I suspect that you counted 
the NULL extra.  However, in git.c you said "0", thus not counting the 
NULL.  IMHO it makes sense _not_ to count the NULL (and NULL-terminate 
the list _always_).

> +int split_cmdline(char *cmdline, const char ***argv, int extra_slots)
> +{
> +	int src, dst, count = 0, size = extra_slots + 16;
> +	char quoted = 0;
> +
> +	*argv = xmalloc(sizeof(char*) * size);
> +
> +	/* split alias_string */

s/alias_string/the command line/

> +	(*argv)[count++] = cmdline;
> +	for (src = dst = 0; cmdline[src];) {
> +		char c = cmdline[src];
> +		if (!quoted && isspace(c)) {
> +			cmdline[dst++] = 0;
> +			while (cmdline[++src]
> +					&& isspace(cmdline[src]))
> +				; /* skip */
> +			if (count >= size) {

s/count/count + extra_slots/

> +				size += 16;
> +				*argv = xrealloc(*argv, sizeof(char*) * size);

This seems a nice candidate for ALLOC_GROW() in any case:

			ALLOC_GROW(*argv, count + extra_slots + 1, size);

> +			}
> +			(*argv)[count++] = cmdline + dst;
> +		} else if(!quoted && (c == '\'' || c == '"')) {
> +			quoted = c;
> +			src++;
> +		} else if (c == quoted) {
> +			quoted = 0;
> +			src++;
> +		} else {
> +			if (c == '\\' && quoted != '\'') {
> +				src++;
> +				c = cmdline[src];
> +				if (!c) {
> +					free(*argv);
> +					*argv = NULL;
> +					return error("cmdline ends with \\");
> +				}
> +			}
> +			cmdline[dst++] = c;
> +			src++;
> +		}
> +	}
> +
> +	cmdline[dst] = 0;
> +
> +	if (quoted) {
> +		free(*argv);
> +		*argv = NULL;
> +		return error("unclosed quote");
> +	}

AFAICT the argv were not NULL terminated before.  But now that the 
function is public, it is safer to do so:

	(*argv)[count] = NULL;

> +
> +	return count;
> +}
> +

Thanks,
Dscho

  reply	other threads:[~2007-12-16 15:37 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-12-16  1:12 [PATCH] Allow commit (and tag) messages to be edited when $EDITOR has arguments Steven Grimm
2007-12-16  1:41 ` Johannes Schindelin
2007-12-16  7:34   ` [PATCH v2] " Steven Grimm
2007-12-16 15:36     ` Johannes Schindelin [this message]
2007-12-16  1:41 ` [PATCH] " Thomas Harning

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.64.0712161525090.27959@racer.site \
    --to=johannes.schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=koreth@midwinter.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).