Changeset 5ded5e8


Ignore:
Timestamp:
Jan 5, 2011, 1:35:52 PM (11 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
release-1.7
Children:
47efcf5
Parents:
3812784
git-author:
Nelson Elhage <nelhage@mit.edu> (01/03/11 21:18:17)
git-committer:
David Benjamin <davidben@mit.edu> (01/05/11 13:35:52)
Message:
Add owl_string_appendf_quoted for easy construction of command lines.
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • tester.c

    r3812784 r5ded5e8  
    215215                "\"");
    216216
     217  GString *g = g_string_new("");
     218  owl_string_appendf_quoted(g, "%q foo %q%q %s %", "hello", "world is", "can't");
     219  FAIL_UNLESS("owl_string_appendf",
     220              !strcmp(g_string_free(g, false),
     221                      "hello foo 'world is'\"can't\" %s %"));
     222
    217223  /* if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); */
    218224  printf("# END testing owl_util (%d failures)\n", numfailed);
  • util.c

    rcb1759e r5ded5e8  
    88#include <sys/types.h>
    99#include <assert.h>
     10#include <stdarg.h>
    1011#include <glib.h>
    1112#include <glib/gstdio.h>
     
    266267}
    267268
     269/*
     270 * Appends 'tmpl' to 'buf', replacing any instances of '%q' with arguments from
     271 * the varargs provided, quoting them to be safe for placing in a barnowl
     272 * command line.
     273 */
     274void owl_string_appendf_quoted(GString *buf, const char *tmpl, ...)
     275{
     276  va_list ap;
     277  va_start(ap, tmpl);
     278  owl_string_vappendf_quoted(buf, tmpl, ap);
     279  va_end(ap);
     280}
     281
     282void owl_string_vappendf_quoted(GString *buf, const char *tmpl, va_list ap)
     283{
     284  const char *p = tmpl, *last = tmpl;
     285  while (true) {
     286    p = strchr(p, '%');
     287    if (p == NULL) break;
     288    if (*(p+1) != 'q') {
     289      p++;
     290      if (*p) p++;
     291      continue;
     292    }
     293    g_string_append_len(buf, last, p - last);
     294    owl_string_append_quoted_arg(buf, va_arg(ap, char *));
     295    p += 2; last = p;
     296  }
     297
     298  g_string_append(buf, last);
     299}
     300
     301char *owl_string_build_quoted(const char *tmpl, ...)
     302{
     303  GString *buf = g_string_new("");
     304  va_list ap;
     305  va_start(ap, tmpl);
     306  owl_string_vappendf_quoted(buf, tmpl, ap);
     307  va_end(ap);
     308  return g_string_free(buf, false); 
     309}
     310
    268311/* Returns a quoted version of arg suitable for placing in a
    269312 * command-line. Result should be freed with owl_free. */
Note: See TracChangeset for help on using the changeset viewer.