]> pd.if.org Git - zpackage/commitdiff
add zpm-quote option to shell quoting
authorNathan Wagner <nw@hydaspes.if.org>
Sun, 23 Sep 2018 08:01:32 +0000 (08:01 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Mon, 24 Sep 2018 10:40:18 +0000 (10:40 +0000)
Add -s to shell quote, and -q to add single quotes around the
quoted string.  Default behavior is to quote for sqlite and
not add quotes.

add sql identifier quoting

Makefile
zpm-quote.c

index c876ce1588e64ab178e0434922e949f346427952..e85060629124316ed57b01a975ce6a898049477c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -132,8 +132,8 @@ zpm-findpkg: zpm-findpkg.o libzpm.a
 zpm-parse: zpm-parse.o libzpm.a
        $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< -lzpm -lelf
 
-zpm-quote: zpm-quote.o libzpm.a
-       $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< -lzpm -lelf
+zpm-quote: zpm-quote.o
+       $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $<
 
 newdb.c: db.sql
        echo "char createdb[] = {" > $@
index ea306b4b00ab742a3e1b7791b96ade2680a9e83f..97e4fe641420cdca4859d2e2984ff851a308ebef 100644 (file)
@@ -1,28 +1,93 @@
-#include <stdlib.h>
+#define _POSIX_C_SOURCE 2
 
-#include "zpm.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
 
 /* for quoting against the shell replace ' with '\'' and surround with
  * single quotes
  */
-int main(int ac, char **av) {
-       size_t n = 0;
-       size_t bufsize = 0;
-       char *buffer = 0;
-       int i;
 
-       for (i=1;i<ac;i++) {
-               n = zpm_quote(av[i], 0, 0);
-               if (n+1 > bufsize) {
-                       buffer = realloc(buffer, n+1);
-                       if (buffer) {
-                               bufsize = n+1;
+char *quote(char *s, int chi, char *repl, int addquotes) {
+       size_t size = 0, rsize = 0;
+       char *d, *q, *r;
+       char ch = chi;
+
+       if (!s) return NULL;
+       if (!repl) return NULL;
+
+       rsize = strlen(repl);
+
+       for (d = s; *d; d++) {
+               if (*d == ch) {
+                       size += rsize-1;
+               }
+               size++;
+       }
+
+       if (addquotes) {
+               size += 2;
+       }
+
+       d = q = malloc(size+1);
+
+       if (q) {
+               if (addquotes) {
+                       *d++ = ch;
+               }
+
+               while (*s) {
+                       if (*s == ch) {
+                               r = repl;
+                               while (*r) {
+                                       *d++ = *r++;
+                               }
+                               s++;
                        } else {
-                               exit(EXIT_FAILURE);
+                               *d++ = *s++;
                        }
+                       *d = 0;
                }
-               zpm_quote(av[i], buffer, bufsize);
-               printf("%s\n", buffer);
+
+               if (addquotes) {
+                       *d++ = ch;
+               }
+               *d = 0;
+       }
+
+       return q;
+}
+
+int main(int ac, char **av) {
+       char *quoted;
+       int i;
+       int ch = '\'';
+       int opt, shellmode = 0, addquotes = 0, ident = 0;
+       char *repl = "''";
+
+       while ((opt = getopt(ac, av, "sqi")) != -1) {
+               switch (opt) {
+                       case 's': shellmode = 1; break;
+                       case 'q': addquotes = 1; break;
+                       case 'i': ident = 1; break;
+                       default:
+                                 exit(EXIT_FAILURE);
+                                 break;
+               }
+       }
+       int argn = optind;
+
+       if (shellmode) {
+               repl = "'\\''";
+       } else if (ident) {
+               ch = '"';
+               repl = "\"\"";
+       }
+
+       for (i=argn;i<ac;i++) {
+               quoted = quote(av[i], ch, repl, addquotes);
+               printf("%s\n", quoted);
        }
        return 0;
 }