From: Junio C Hamano <junkio@cox.net>
To: fork0@t-online.de (Alex Riesen)
Cc: git@vger.kernel.org
Subject: Re: [PATCH 1/2] add git-quote: shell and C quoting tool
Date: Sun, 23 Jul 2006 22:49:53 -0700 [thread overview]
Message-ID: <7vwta3h7r2.fsf@assigned-by-dhcp.cox.net> (raw)
In-Reply-To: <20060716222757.GA10135@steel.home> (Alex Riesen's message of "Mon, 17 Jul 2006 00:27:57 +0200")
fork0@t-online.de (Alex Riesen) writes:
> In case anyone asks why isn't it a standalone tool nor is it put into
> git-stripspace: I don't know. Maybe it should be.
This organization is fine by me.
* unquote?
* doc?
> +enum {SHELL_QUOTE, C_QUOTE};
> +static int style = SHELL_QUOTE,
> + use_stdin = 0;
Shouldn't style be of type which is that unnamed enum?
> +static const char *separator = NULL; /* default is space */
> +static unsigned sep_len = 0;
It is not clear if this is separator of input side or output
side (after reading the code it becomes somewhat obvious that
you are talking about output separator), and if the default is
space wouldn't it make more sense to set that here?
> +static const char builtin_quote_usage[] =
> +"git-quote [--c] [--sep=<c-quoted> | -z] ( [--stdin] | [--] ... )";
> +
> +static void print_quoted(const char *text)
> +{
> + switch (style)
> + {
> + case SHELL_QUOTE:
> + sq_quote_print(stdout, text);
> + break;
> + case C_QUOTE:
> + quote_c_style(text, NULL, stdout, 0);
> + break;
> + }
Not a big deal but aren't these going to write out things quoted
when they do not need to? To help scripts, it might be worth
adding "check if this needs quoting" interface perhaps?
> +int cmd_quote(int argc, const char **argv, char **envp)
> +{
> + int i;
> + for (i = 1; i < argc; i++) {
> + const char *arg = argv[i];
> +
> + if (arg[0] != '-')
> + break;
> + if (!strcmp(arg, "--")) {
> + i++;
> + break;
> + }
> + if (!strcmp(arg, "--stdin")) {
> + use_stdin = 1;
> + if ( !separator ) {
> + separator = "\n";
> + sep_len = 1;
> + }
> + break;
> + }
> + if (!strcmp(arg, "--c")) {
Perhaps "--c-style" with "-c" as a shorthand, (and
"--shell-style" with "-s" to complement)?
> + style = C_QUOTE;
> + continue;
> + }
> + if (!strcmp(arg, "-z")) {
> + separator = "";
> + sep_len = 1;
> + continue;
> + }
Is it plausible that somebody might want to feed you NUL
terminated sequence as input (iow you might want to give them
choice of separator on the input side)?
> + if (!strncmp(arg, "--sep=", 6)) {
Perhaps "--separator" (or "--output-separator") with whatever
shorthand is handy?
> + const char *end;
> + char *tmp;
> + arg += 6;
> + if ('"' == *arg)
> + tmp = strdup(arg);
> + else {
> + size_t l = strlen(arg);
> + tmp = malloc(l + 3);
> + sprintf(tmp, "\"%s\"", arg);
> + }
> + separator = unquote_c_style(tmp, &end);
> + sep_len = strlen(separator);
If the parameter is a malformed c-quoted string, you would dump
core here.
> + /* this will leak if multiple --sep= given */
> + continue;
> + }
> + die(builtin_quote_usage);
> + }
> + if (!separator) {
> + sep_len = 1;
> + separator = "\x20";
Any reason you needed to spell it in hex? Is this trying to
be portable to EBCDIC (or trying to prevent the code to be
ported there)? I dunno.
> + }
> + if (use_stdin) {
> + size_t size = BUFSIZ;
> + char *buf = xmalloc(size);
> + int ch, pos = 0;
> + while (EOF != (ch = fgetc(stdin))) {
> + if (pos == size)
> + buf = xrealloc(buf, size <<= 1);
> + buf[pos++] = ch;
> + if ('\n' == ch) {
> + buf[--pos] = '\0';
> + pos = 0;
> + print_quoted(buf);
> + }
> + }
Looks similar to strbuf.c::read_line() loop...
next prev parent reply other threads:[~2006-07-24 5:50 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-07-16 22:27 [PATCH 1/2] add git-quote: shell and C quoting tool Alex Riesen
2006-07-24 5:49 ` Junio C Hamano [this message]
2006-07-24 23:12 ` Alex Riesen
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=7vwta3h7r2.fsf@assigned-by-dhcp.cox.net \
--to=junkio@cox.net \
--cc=fork0@t-online.de \
--cc=git@vger.kernel.org \
/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