Changeset 2bc6ad35


Ignore:
Timestamp:
Jan 2, 2011, 3:35:15 PM (13 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master, release-1.10, release-1.8, release-1.9
Children:
d222c44
Parents:
d275eb2
git-author:
David Benjamin <davidben@mit.edu> (12/14/10 23:52:42)
git-committer:
David Benjamin <davidben@mit.edu> (01/02/11 15:35:15)
Message:
Add owl_quote_arg and owl_string_append_quoted_arg

Also add unit tests. We don't appear to have an equivalent of
BarnOwl::quote in C that actually works.
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • tester.c

    re45d9f4 r2bc6ad35  
    175175  FAIL_UNLESS("skiptokens 5",
    176176              !strcmp("c d e", skiptokens("a \"'\" c d e", 2)));
     177
     178#define CHECK_QUOTING(desc, unquoted, quoted)           \
     179  do {                                                  \
     180      int __argc;                                       \
     181      char *__quoted = owl_arg_quote(unquoted);         \
     182      char **__argv;                                    \
     183      FAIL_UNLESS(desc, !strcmp(quoted, __quoted));     \
     184      __argv = owl_parseline(__quoted, &__argc);        \
     185      FAIL_UNLESS(desc " - arg count", __argc == 1);    \
     186      FAIL_UNLESS(desc " - parsed",                     \
     187                  !strcmp(__argv[0], unquoted));        \
     188      owl_parse_delete(__argv, __argc);                 \
     189      owl_free(__quoted);                               \
     190    } while (0)
     191
     192  CHECK_QUOTING("boring text", "mango", "mango");
     193  CHECK_QUOTING("spaces", "mangos are tasty", "'mangos are tasty'");
     194  CHECK_QUOTING("single quotes", "mango's", "\"mango's\"");
     195  CHECK_QUOTING("double quotes", "he said \"mangos are tasty\"",
     196                "'he said \"mangos are tasty\"'");
     197  CHECK_QUOTING("both quotes",
     198                "he said \"mango's are tasty even when you put in "
     199                "a random apostrophe\"",
     200                "\"he said \"'\"'\"mango's are tasty even when you put in "
     201                "a random apostrophe\"'\"'\"\"");
     202  CHECK_QUOTING("quote monster", "'\"\"'\"'''\"",
     203                "\""
     204                "'"
     205                "\"'\"'\""
     206                "\"'\"'\""
     207                "'"
     208                "\"'\"'\""
     209                "'"
     210                "'"
     211                "'"
     212                "\"'\"'\""
     213                "\"");
    177214
    178215  /* if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); */
  • util.c

    rd275eb2 r2bc6ad35  
    194194}
    195195
     196/* Appends a quoted version of arg suitable for placing in a
     197 * command-line to a GString. Does not append a space. */
     198void owl_string_append_quoted_arg(GString *buf, const char *arg)
     199{
     200  const char *argp;
     201  if (arg[0] == '\0') {
     202    /* Quote the empty string. */
     203    g_string_append(buf, "''");
     204  } else if (arg[strcspn(arg, "'\" \n\t")] == '\0') {
     205    /* If there are no nasty characters, return as-is. */
     206    g_string_append(buf, arg);
     207  } else if (!strchr(arg, '\'')) {
     208    /* Single-quote if possible. */
     209    g_string_append_c(buf, '\'');
     210    g_string_append(buf, arg);
     211    g_string_append_c(buf, '\'');
     212  } else {
     213    /* Nasty case: double-quote, but change all internal "s to "'"'"
     214     * so that they are single-quoted because we're too cool for
     215     * backslashes.
     216     */
     217    g_string_append_c(buf, '"');
     218    for (argp = arg; *argp; argp++) {
     219      if (*argp == '"')
     220        g_string_append(buf, "\"'\"'\"");
     221      else
     222        g_string_append_c(buf, *argp);
     223    }
     224    g_string_append_c(buf, '"');
     225  }
     226}
     227
     228/* Returns a quoted version of arg suitable for placing in a
     229 * command-line. Result should be freed with owl_free. */
     230char *owl_arg_quote(const char *arg)
     231{
     232  GString *buf = g_string_new("");;
     233  owl_string_append_quoted_arg(buf, arg);
     234  return g_string_free(buf, false);
     235}
     236
    196237/* caller must free the return */
    197238char *owl_util_minutes_to_timestr(int in)
Note: See TracChangeset for help on using the changeset viewer.