From: fork0@t-online.de (Alex Riesen)
To: git@vger.kernel.org
Cc: Junio C Hamano <junkio@cox.net>
Subject: [PATCH 1/2] add git-quote: shell and C quoting tool
Date: Mon, 17 Jul 2006 00:27:57 +0200 [thread overview]
Message-ID: <20060716222757.GA10135@steel.home> (raw)
Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
---
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(-)
diff --git a/Makefile b/Makefile
index 01fb9cf..9fcbf3b 100644
--- a/Makefile
+++ b/Makefile
@@ -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
diff --git a/builtin-quote.c b/builtin-quote.c
new file mode 100644
index 0000000..cbd822d
--- /dev/null
+++ b/builtin-quote.c
@@ -0,0 +1,102 @@
+/*
+ * "git quote" builtin command
+ *
+ * DOES NOT QUOTE \0 (truncates lines at it)
+ */
+#include "cache.h"
+#include "builtin.h"
+#include "quote.h"
+
+enum {SHELL_QUOTE, C_QUOTE};
+static int style = SHELL_QUOTE,
+ use_stdin = 0;
+static const char *separator = NULL; /* default is space */
+static unsigned sep_len = 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;
+ case C_QUOTE:
+ quote_c_style(text, NULL, stdout, 0);
+ break;
+ }
+ fwrite(separator, 1, sep_len, stdout);
+}
+
+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")) {
+ style = C_QUOTE;
+ continue;
+ }
+ if (!strcmp(arg, "-z")) {
+ separator = "";
+ sep_len = 1;
+ continue;
+ }
+ if (!strncmp(arg, "--sep=", 6)) {
+ 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);
+ /* this will leak if multiple --sep= given */
+ continue;
+ }
+ die(builtin_quote_usage);
+ }
+ if (!separator) {
+ sep_len = 1;
+ separator = "\x20";
+ }
+ 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);
+ }
+ }
+ } else
+ for (; argv[i]; ++i)
+ print_quoted(argv[i]);
+ return 0;
+}
diff --git a/builtin.h b/builtin.h
index 5339d86..9bd522e 100644
--- a/builtin.h
+++ b/builtin.h
@@ -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
diff --git a/git.c b/git.c
index ee5a0e8..f94d25a 100644
--- a/git.c
+++ b/git.c
@@ -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;
--
1.4.1.gb944
next reply other threads:[~2006-07-16 22:29 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-07-16 22:27 Alex Riesen [this message]
2006-07-24 5:49 ` [PATCH 1/2] add git-quote: shell and C quoting tool Junio C Hamano
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=20060716222757.GA10135@steel.home \
--to=fork0@t-online.de \
--cc=git@vger.kernel.org \
--cc=junkio@cox.net \
/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