add git-quote: shell and C quoting tool
Subsystems (2)
PATCH 1/2 add git-quote: shell and C quoting tool
---
As git-stripspace, it may ne useful for something. As an example, the next patch converts git-bisect.sh to use of this tool.
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.
Makefile | 3 +- builtin-quote.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ builtin.h | 2 + git.c | 1 + 4 files changed, 107 insertions(+), 1 deletions(-)
Makefile +2 -1
@@ -234,7 +234,8 @@ BUILTIN_OBJS = \ builtin-apply.o builtin-show-branch.o builtin-diff-files.o \ builtin-diff-index.o builtin-diff-stages.o builtin-diff-tree.o \ builtin-cat-file.o builtin-mailsplit.o builtin-stripspace.o \ - builtin-update-ref.o builtin-fmt-merge-msg.o builtin-prune.o + builtin-update-ref.o builtin-fmt-merge-msg.o builtin-prune.o \ + builtin-quote.o GITLIBS = $(LIB_FILE) $(XDIFF_LIB) LIBS = $(GITLIBS) -lz
builtin-quote.c new file +102 -0
102 lines not on this page · raw message
builtin.h +2 -0
@@ -64,4 +64,6 @@ extern int mailinfo(FILE *in, FILE *out,extern int cmd_stripspace (int argc,const char **argv,char **envp);extern void stripspace (FILE *in,FILE *out); + +extern int cmd_quote (int argc,const char **argv,char **envp);#endif
git.c +1 -0
@@ -202,6 +202,7 @@ static void handle_internal_command(int { "update-ref", cmd_update_ref }, { "fmt-merge-msg", cmd_fmt_merge_msg }, { "prune", cmd_prune }, + { "quote", cmd_quote }, };int i;
Signature · 1 line
1.4.1.gb944
Quoted 2 lines · [email protected] (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?
builtin-quote.c line 12
Quoted 3 lines
builtin-quote.c new file +3
-0+enum {SHELL_QUOTE ,C_QUOTE }; +static int style =SHELL_QUOTE , + use_stdin =0 ;
Shouldn't style be of type which is that unnamed enum?
builtin-quote.c line 14
Quoted 2 lines
builtin-quote.c new file +2
-0+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?
Quoted 4 lines
builtin-quote.c new file +4
-0+static const char builtin_quote_usage[] = +"git-quote [--c] [--sep=<c-quoted> | -z] ( [--stdin] | [--] ... )" ; + +static void print_quoted (const char *text)
builtin-quote.c line 29
Quoted 10 lines
builtin-quote.c new file +10
-0+{ +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?
Quoted 11 lines
builtin-quote.c new file +11
-0+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 ;
builtin-quote.c line 53
Quoted 10 lines
builtin-quote.c new file +10
-0+ } +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)?
builtin-quote.c line 61
Quoted 8 lines
builtin-quote.c new file +8
-0+ 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)?
builtin-quote.c line 62
Quoted 1 line
builtin-quote.c new file +1
-0+if (!strncmp (arg,"--sep=" ,6 )) {
Perhaps "--separator" (or "--output-separator") with whatever shorthand is handy?
builtin-quote.c line 74
Quoted 12 lines
builtin-quote.c new file +12
-0+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.
builtin-quote.c line 82
Quoted 8 lines
builtin-quote.c new file +8
-0+/* 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.
Quoted 5 lines
builtin-quote.c new file +5
-0+ } +if (use_stdin) { +size_t size =BUFSIZ ; +char *buf =xmalloc (size); +int ch, pos =0 ;
builtin-quote.c line 97
Quoted 10 lines
builtin-quote.c new file +10
-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...
Junio C Hamano, Mon, Jul 24, 2006 07:49:53 +0200:
Quoted 6 lines
Quoted 2 lines
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?
Not done yet. I actually never expected it to be used for unquoting, being naturally unquoted by bash. C-quoting was done just because it was so simple to do. I have no idea where could that be used.
Quoted 1 line
* doc?
Done. Sorry for missing that.
Quoted 5 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+enum {SHELL_QUOTE, C_QUOTE}; +static int style = SHELL_QUOTE, + use_stdin = 0;Shouldn't style be of type which is that unnamed enum?
It should. Done.
Quoted 7 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+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?
Right. It is out_separator now.
Quoted 10 lines
Quoted 10 lines
(quoted diff) +10
-0+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;
Quoted 8 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+ 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?
Not done yet. "--no-overquote"?
Quoted 4 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ if (!strcmp(arg, "--c")) {Perhaps "--c-style" with "-c" as a shorthand, (and "--shell-style" with "-s" to complement)?
Done. --sh-style.
Quoted 4 lines
Quoted 4 lines
(quoted diff) +4
-0+ style = C_QUOTE; + continue; + } + if (!strcmp(arg, "-z")) {
Quoted 8 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+ 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)?
Done. -z sets both separators to NUL, --in-separator=<c-style> defines by its first character the input separator. I.e.:
$ find | git quote -z --in-separator='\n' 'a'\0'b'\0''
which is not all that useful...
Quoted 4 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ if (!strncmp(arg, "--sep=", 6)) {Perhaps "--separator" (or "--output-separator") with whatever shorthand is handy?
--out-separator. No idea of a shortcut, though
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ separator = unquote_c_style(tmp, &end); + sep_len = strlen(separator);If the parameter is a malformed c-quoted string, you would dump core here.
Oops. Fixed.
Quoted 7 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ 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.
Visibility. I have dificulty seeing spaces and tabs. I changed it anyway, maybe that's just me.
Quoted 4 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ while (EOF != (ch = fgetc(stdin))) {Looks similar to
strbuf.c::read_line()loop...
Indeed. Replaced.
I attach the updated patch here, because it is obviously is not quite finished yet and I probably wont have enough time over the week. So maybe someone takes over. Or it'll be taken as is, that happens too.
---
Documentation/git-quote.txt new file +70 -0
@@ -0,0 +1,70 @@ +git-quote(1) +============ + +NAME +---- +git-quote - Quote given text according to Shell or C rules + + +SYNOPSIS +-------- +'git-quote' [-c] [-s] [-s] [--out-separator=<c-quoted>] + [--in-separator=<c-quoted>] [-z] [--] ... +'git-quote' ... --stdin < <stream> + +DESCRIPTION +----------- +Quote the lines according to the given rules, which can be Shell backslash +quoting (every line is enclosed in single ticks, backslash and single tick +quoted specially) or a C quoting (use double ticks and backslash-prefixed +symbols). + +The separator options are handled in the order they are given, so one +can save some typing by giving "-z" first, and overriding input or +output separator as needed. + +OPTIONS +------- +-c|--c-style:: + Select C-style quoting + +-s|--sh-style:: + Select Shell-style quoting. This is default. + +--out-separator=<c-quoted>:: + Specify the separator between the output elements. If the separator + have to contain special characters, they must be quoted according + to C quoting rules (double ticks are not essential). + The ASCII space is default output separator. + +--in-separator=<c-quoted>:: + Specify the separator between the input elements. Only the + first character of <c-quoted> is used. + Used only if --stdin is also given. The default is LF. + +-z:: + Separate the elements on output and expect elements separated + on input with NUL character. + Used only if --stdin is also given. + +--stdin:: + Specify that the elements have to be read from stdin. + This option ends command line parsing. + Also, if there were no output separator set by previous + --out-separator or -z, this option will set it to LF. + +<stream>:: + Byte stream to act on. + +Author +------ +Written by Alex Riesen <[email protected]> + +Documentation +-------------- +Documentation by Alex Riesen. + +GIT +--- +Part of the gitlink:git[7] suite +
Makefile +2 -1
@@ -246,7 +246,8 @@ BUILTIN_OBJS = \ builtin-apply.o builtin-show-branch.o builtin-diff-files.o \ builtin-diff-index.o builtin-diff-stages.o builtin-diff-tree.o \ builtin-cat-file.o builtin-mailsplit.o builtin-stripspace.o \ - builtin-update-ref.o builtin-fmt-merge-msg.o builtin-prune.o + builtin-update-ref.o builtin-fmt-merge-msg.o builtin-prune.o \ + builtin-quote.o GITLIBS = $(LIB_FILE) $(XDIFF_LIB) LIBS = $(GITLIBS) -lz
builtin-quote.c new file +123 -0
123 lines not on this page · raw message
builtin.h +2 -0
@@ -64,4 +64,6 @@ extern int mailinfo(FILE *in, FILE *out,extern int cmd_stripspace (int argc,const char **argv,char **envp);extern void stripspace (FILE *in,FILE *out); + +extern int cmd_quote (int argc,const char **argv,char **envp);#endif
git.c +1 -0
@@ -202,6 +202,7 @@ static void handle_internal_command(int { "update-ref", cmd_update_ref }, { "fmt-merge-msg", cmd_fmt_merge_msg }, { "prune", cmd_prune }, + { "quote", cmd_quote }, };int i;