[2837] in BarnOwl Developers

home help back first fref pref prev next nref lref last post

Re: [PATCH 1/2] Add owl_quote_arg and owl_string_append_quoted_arg

daemon@ATHENA.MIT.EDU (Nelson Elhage)
Tue Dec 28 18:37:22 2010

In-Reply-To: <1292389242-2503-1-git-send-email-davidben@mit.edu>
Date: Tue, 28 Dec 2010 18:37:20 -0500
From: Nelson Elhage <nelhage@nelhage.com>
To: David Benjamin <davidben@mit.edu>
Cc: barnowl-dev@mit.edu

lgtm on both patches, but let's s/TRUE/true/.

- Nelson

On Wed, Dec 15, 2010 at 12:00 AM, David Benjamin <davidben@mit.edu> wrote:
> Also add unit tests. We don't appear to have an equivalent of
> BarnOwl::quote in C that actually works.
> ---
> =C2=A0tester.c | =C2=A0 37 +++++++++++++++++++++++++++++++++++++
> =C2=A0util.c =C2=A0 | =C2=A0 41 +++++++++++++++++++++++++++++++++++++++++
> =C2=A02 files changed, 78 insertions(+), 0 deletions(-)
>
> diff --git a/tester.c b/tester.c
> index 41ba0d8..8678d74 100644
> --- a/tester.c
> +++ b/tester.c
> @@ -175,6 +175,43 @@ int owl_util_regtest(void)
> =C2=A0 FAIL_UNLESS("skiptokens 5",
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 !strcmp("c d e", skiptok=
ens("a \"'\" c d e", 2)));
>
> +#define CHECK_QUOTING(desc, unquoted, quoted) =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0\
> + =C2=A0do { =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 \
> + =C2=A0 =C2=A0 =C2=A0int __argc; =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0\
> + =C2=A0 =C2=A0 =C2=A0char *__quoted =3D owl_arg_quote(unquoted); =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0\
> + =C2=A0 =C2=A0 =C2=A0char **__argv; =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 \
> + =C2=A0 =C2=A0 =C2=A0FAIL_UNLESS(desc, !strcmp(quoted, __quoted)); =C2=
=A0 =C2=A0\
> + =C2=A0 =C2=A0 =C2=A0__argv =3D owl_parseline(__quoted, &__argc); =C2=A0=
 =C2=A0 =C2=A0 \
> + =C2=A0 =C2=A0 =C2=A0FAIL_UNLESS(desc " - arg count", __argc =3D=3D 1); =
=C2=A0 \
> + =C2=A0 =C2=A0 =C2=A0FAIL_UNLESS(desc " - parsed", =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0\
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 !strcmp(__argv[=
0], unquoted)); =C2=A0 =C2=A0 =C2=A0 =C2=A0\
> + =C2=A0 =C2=A0 =C2=A0owl_parse_delete(__argv, __argc); =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0\
> + =C2=A0 =C2=A0 =C2=A0owl_free(__quoted); =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0\
> + =C2=A0 =C2=A0} while (0)
> +
> + =C2=A0CHECK_QUOTING("boring text", "mango", "mango");
> + =C2=A0CHECK_QUOTING("spaces", "mangos are tasty", "'mangos are tasty'")=
;
> + =C2=A0CHECK_QUOTING("single quotes", "mango's", "\"mango's\"");
> + =C2=A0CHECK_QUOTING("double quotes", "he said \"mangos are tasty\"",
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "'he said \"mangos are=
 tasty\"'");
> + =C2=A0CHECK_QUOTING("both quotes",
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "he said \"mango's are=
 tasty even when you put in "
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "a random apostrophe\"=
",
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "\"he said \"'\"'\"man=
go's are tasty even when you put in "
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "a random apostrophe\"=
'\"'\"\"");
> + =C2=A0CHECK_QUOTING("quote monster", "'\"\"'\"'''\"",
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "\""
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "'"
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "\"'\"'\""
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "\"'\"'\""
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "'"
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "\"'\"'\""
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "'"
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "'"
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "'"
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "\"'\"'\""
> + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "\"");
> +
> =C2=A0 /* if (numfailed) printf("*** WARNING: failures encountered with o=
wl_util\n"); */
> =C2=A0 printf("# END testing owl_util (%d failures)\n", numfailed);
> =C2=A0 return(numfailed);
> diff --git a/util.c b/util.c
> index bd842df..3a233c9 100644
> --- a/util.c
> +++ b/util.c
> @@ -233,6 +233,47 @@ char **owl_parseline(const char *line, int *argc)
> =C2=A0 return(argv);
> =C2=A0}
>
> +/* Appends a quoted version of arg suitable for placing in a
> + * command-line to a GString. Does not append a space. */
> +void owl_string_append_quoted_arg(GString *buf, const char *arg)
> +{
> + =C2=A0const char *argp;
> + =C2=A0if (arg[0] =3D=3D '\0') {
> + =C2=A0 =C2=A0/* Quote the empty string. */
> + =C2=A0 =C2=A0g_string_append(buf, "''");
> + =C2=A0} else if (strcspn(arg, "'\" \n\t") =3D=3D strlen(arg)) {
> + =C2=A0 =C2=A0/* If there are no nasty characters, return as-is. */
> + =C2=A0 =C2=A0g_string_append(buf, arg);
> + =C2=A0} else if (!strchr(arg, '\'')) {
> + =C2=A0 =C2=A0/* Single-quote if possible. */
> + =C2=A0 =C2=A0g_string_append_c(buf, '\'');
> + =C2=A0 =C2=A0g_string_append(buf, arg);
> + =C2=A0 =C2=A0g_string_append_c(buf, '\'');
> + =C2=A0} else {
> + =C2=A0 =C2=A0/* Nasty case: double-quote, but change all internal "s to=
 "'"'"
> + =C2=A0 =C2=A0 * so that they are single-quoted because we're too cool f=
or
> + =C2=A0 =C2=A0 * backslashes.
> + =C2=A0 =C2=A0 */
> + =C2=A0 =C2=A0g_string_append_c(buf, '"');
> + =C2=A0 =C2=A0for (argp =3D arg; *argp; argp++) {
> + =C2=A0 =C2=A0 =C2=A0if (*argp =3D=3D '"')
> + =C2=A0 =C2=A0 =C2=A0 g_string_append(buf, "\"'\"'\"");
> + =C2=A0 =C2=A0 =C2=A0else
> + =C2=A0 =C2=A0 =C2=A0 g_string_append_c(buf, *argp);
> + =C2=A0 =C2=A0}
> + =C2=A0 =C2=A0g_string_append_c(buf, '"');
> + =C2=A0}
> +}
> +
> +/* Returns a quoted version of arg suitable for placing in a
> + * command-line. Result should be freed with owl_free. */
> +char *owl_arg_quote(const char *arg)
> +{
> + =C2=A0GString *buf =3D g_string_new("");;
> + =C2=A0owl_string_append_quoted_arg(buf, arg);
> + =C2=A0return g_string_free(buf, FALSE);
> +}
> +
> =C2=A0/* caller must free the return */
> =C2=A0char *owl_util_minutes_to_timestr(int in)
> =C2=A0{
> --
> 1.7.3.3.399.g06ddf
>
>

home help back first fref pref prev next nref lref last post