Changeset cb1759e


Ignore:
Timestamp:
Jan 5, 2011, 1:34:51 PM (11 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
release-1.7
Children:
b4f7576
Parents:
a359456
git-author:
David Benjamin <davidben@mit.edu> (12/14/10 23:52:42)
git-committer:
David Benjamin <davidben@mit.edu> (01/05/11 13:34:51)
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 rcb1759e  
    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

    ra359456 rcb1759e  
    234234}
    235235
     236/* Appends a quoted version of arg suitable for placing in a
     237 * command-line to a GString. Does not append a space. */
     238void owl_string_append_quoted_arg(GString *buf, const char *arg)
     239{
     240  const char *argp;
     241  if (arg[0] == '\0') {
     242    /* Quote the empty string. */
     243    g_string_append(buf, "''");
     244  } else if (arg[strcspn(arg, "'\" \n\t")] == '\0') {
     245    /* If there are no nasty characters, return as-is. */
     246    g_string_append(buf, arg);
     247  } else if (!strchr(arg, '\'')) {
     248    /* Single-quote if possible. */
     249    g_string_append_c(buf, '\'');
     250    g_string_append(buf, arg);
     251    g_string_append_c(buf, '\'');
     252  } else {
     253    /* Nasty case: double-quote, but change all internal "s to "'"'"
     254     * so that they are single-quoted because we're too cool for
     255     * backslashes.
     256     */
     257    g_string_append_c(buf, '"');
     258    for (argp = arg; *argp; argp++) {
     259      if (*argp == '"')
     260        g_string_append(buf, "\"'\"'\"");
     261      else
     262        g_string_append_c(buf, *argp);
     263    }
     264    g_string_append_c(buf, '"');
     265  }
     266}
     267
     268/* Returns a quoted version of arg suitable for placing in a
     269 * command-line. Result should be freed with owl_free. */
     270char *owl_arg_quote(const char *arg)
     271{
     272  GString *buf = g_string_new("");;
     273  owl_string_append_quoted_arg(buf, arg);
     274  return g_string_free(buf, false);
     275}
     276
    236277/* caller must free the return */
    237278char *owl_util_minutes_to_timestr(int in)
Note: See TracChangeset for help on using the changeset viewer.