From bea0037dbdd979603fb0b5be8b43f5478c1f6fec Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Fri, 19 Sep 2014 02:53:32 +0000 Subject: initial commit --- src/skaembutils/s6-unquote.c | 70 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/skaembutils/s6-unquote.c (limited to 'src/skaembutils/s6-unquote.c') diff --git a/src/skaembutils/s6-unquote.c b/src/skaembutils/s6-unquote.c new file mode 100644 index 0000000..37c3dfb --- /dev/null +++ b/src/skaembutils/s6-unquote.c @@ -0,0 +1,70 @@ +/* ISC license. */ + +#include +#include +#include +#include +#include +#include + +#define USAGE "s6-unquote [ -n ] [ -d delim ] string" + +int main (int argc, char const *const *argv) +{ + char const *delim = "\"" ; + unsigned int len, delimlen ; + int nl = 1 ; + char const *string ; + PROG = "s6-unquote" ; + { + subgetopt_t l = SUBGETOPT_ZERO ; + for (;;) + { + register int opt = subgetopt_r(argc, argv, "nd:", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'n' : nl = 0 ; break ; + case 'd': delim = l.arg ; break ; + default : strerr_dieusage(100, USAGE) ; + } + } + argc -= l.ind ; argv += l.ind ; + } + if (!argc) strerr_dieusage(100, USAGE) ; + string = *argv ; + len = str_len(string) ; + delimlen = str_len(delim) ; + if (delimlen) + { + if (!len--) strerr_dief1x(100, "the empty string isn't a quoted string") ; + if (byte_chr(delim, delimlen, *string++) >= delimlen) + strerr_dief1x(100, "invalid starting quote character") ; + } + { + unsigned int r = 0, w = 0 ; + char buf[len+1] ; + if (!string_unquote_withdelim(buf, &w, string, len, &r, delim, delimlen)) + { + char fmt[UINT_FMT] ; + fmt[uint_fmt(fmt, r + !!delimlen)] = 0 ; + strerr_diefu2sys(100, "unquote at character ", fmt) ; + } + if (delimlen) + { + if (r == len) strerr_dief1x(100, "no ending quote character") ; + else if (r < len - 1) + { + char fmtnum[UINT_FMT] ; + char fmtden[UINT_FMT] ; + fmtnum[uint_fmt(fmtnum, r+1)] = 0 ; + fmtden[uint_fmt(fmtden, len)] = 0 ; + strerr_warnw5x("found ending quote character at position ", fmtnum, "/", fmtden, "; ignoring remainder") ; + } + } + if (nl) buf[w++] = '\n' ; + if (allwrite(1, buf, w) < w) + strerr_diefu1sys(111, "write to stdout") ; + } + return 0 ; +} -- cgit v1.2.3