aboutsummaryrefslogtreecommitdiff
path: root/quote.c
diff options
context:
space:
mode:
authorShawn O. Pearce <spearce@spearce.org>2007-01-14 02:44:18 -0500
committerShawn O. Pearce <spearce@spearce.org>2007-01-14 02:44:18 -0500
commit1fcdd62adf81a172f45c7c6a58177212d500b9d9 (patch)
tree94acde078fd78c3d214fe09d45e85ed346a2f2d4 /quote.c
parent9938ffc53a15c755bbd3894c02492b940ea34c4c (diff)
parent696b1b507f8ff9e80a2edc4eced59ca8cdda920e (diff)
downloadgit-1fcdd62adf81a172f45c7c6a58177212d500b9d9.tar.gz
git-1fcdd62adf81a172f45c7c6a58177212d500b9d9.tar.xz
Merge branch 'master' into sp/fast-import
I'm bringing master in early so that the OBJ_OFS_DELTA implementation is available as part of the topic. This way git-fast-import can learn about this new slightly smaller and faster packfile format, and can generate them directly rather than needing to have them be repacked with git-pack-objects. Due to the API changes in master during the period of development of git-fast-import, a few minor tweaks to fast-import.c are needed to produce a working merge. I've done them here as part of the merge to ensure bisection always works. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Diffstat (limited to 'quote.c')
-rw-r--r--quote.c101
1 files changed, 100 insertions, 1 deletions
diff --git a/quote.c b/quote.c
index e220dcc28..a418a0f80 100644
--- a/quote.c
+++ b/quote.c
@@ -74,6 +74,67 @@ char *sq_quote(const char *src)
return buf;
}
+char *sq_quote_argv(const char** argv, int count)
+{
+ char *buf, *to;
+ int i;
+ size_t len = 0;
+
+ /* Count argv if needed. */
+ if (count < 0) {
+ for (count = 0; argv[count]; count++)
+ ; /* just counting */
+ }
+
+ /* Special case: no argv. */
+ if (!count)
+ return xcalloc(1,1);
+
+ /* Get destination buffer length. */
+ for (i = 0; i < count; i++)
+ len += sq_quote_buf(NULL, 0, argv[i]) + 1;
+
+ /* Alloc destination buffer. */
+ to = buf = xmalloc(len + 1);
+
+ /* Copy into destination buffer. */
+ for (i = 0; i < count; ++i) {
+ *to++ = ' ';
+ to += sq_quote_buf(to, len, argv[i]);
+ }
+
+ return buf;
+}
+
+/*
+ * Append a string to a string buffer, with or without shell quoting.
+ * Return true if the buffer overflowed.
+ */
+int add_to_string(char **ptrp, int *sizep, const char *str, int quote)
+{
+ char *p = *ptrp;
+ int size = *sizep;
+ int oc;
+ int err = 0;
+
+ if (quote)
+ oc = sq_quote_buf(p, size, str);
+ else {
+ oc = strlen(str);
+ memcpy(p, str, (size <= oc) ? size - 1 : oc);
+ }
+
+ if (size <= oc) {
+ err = 1;
+ oc = size - 1;
+ }
+
+ *ptrp += oc;
+ **ptrp = '\0';
+ *sizep -= oc;
+ return err;
+}
+
char *sq_dequote(char *arg)
{
char *dst = arg;
@@ -148,7 +209,7 @@ static int quote_c_style_counted(const char *name, int namelen,
if (!ch)
break;
if ((ch < ' ') || (ch == '"') || (ch == '\\') ||
- (ch == 0177)) {
+ (ch >= 0177)) {
needquote = 1;
switch (ch) {
case '\a': EMITQ(); ch = 'a'; break;
@@ -288,3 +349,41 @@ void write_name_quoted(const char *prefix, int prefix_len,
else
goto no_quote;
}
+
+/* quoting as a string literal for other languages */
+
+void perl_quote_print(FILE *stream, const char *src)
+{
+ const char sq = '\'';
+ const char bq = '\\';
+ char c;
+
+ fputc(sq, stream);
+ while ((c = *src++)) {
+ if (c == sq || c == bq)
+ fputc(bq, stream);
+ fputc(c, stream);
+ }
+ fputc(sq, stream);
+}
+
+void python_quote_print(FILE *stream, const char *src)
+{
+ const char sq = '\'';
+ const char bq = '\\';
+ const char nl = '\n';
+ char c;
+
+ fputc(sq, stream);
+ while ((c = *src++)) {
+ if (c == nl) {
+ fputc(bq, stream);
+ fputc('n', stream);
+ continue;
+ }
+ if (c == sq || c == bq)
+ fputc(bq, stream);
+ fputc(c, stream);
+ }
+ fputc(sq, stream);
+}