1 /* string-util.c - Extra or enhanced routines for null terminated strings.
3 * Copyright (c) 2012 Jani Nikula
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see http://www.gnu.org/licenses/ .
18 * Author: Jani Nikula <jani@nikula.org>
22 #include "string-util.h"
29 strtok_len (char *s, const char *delim, size_t *len)
31 /* skip initial delims */
32 s += strspn (s, delim);
35 *len = strcspn (s, delim);
37 return *len ? s : NULL;
41 strtok_len_c (const char *s, const char *delim, size_t *len)
43 /* strtok_len is already const-safe, but we can't express both
44 * versions in the C type system. */
45 return strtok_len ((char*)s, delim, len);
49 sanitize_string (const void *ctx, const char *str)
56 out = talloc_strdup (ctx, str);
60 for (loop = out; *loop; loop++) {
61 if (*loop == '\t' || *loop == '\n')
63 else if ((unsigned char)(*loop) < 32)
71 is_unquoted_terminator (unsigned char c)
73 return c == 0 || c <= ' ' || c == ')';
77 make_boolean_term (void *ctx, const char *prefix, const char *term,
78 char **buf, size_t *len)
85 /* Do we need quoting? To be paranoid, we quote anything
86 * containing a quote or '(', even though these only matter at the
87 * beginning, and anything containing non-ASCII text. */
90 for (in = term; *in && !need_quoting; in++)
91 if (is_unquoted_terminator (*in) || *in == '"' || *in == '('
92 || (unsigned char)*in > 127)
96 for (in = term; *in; in++)
97 needed += (*in == '"') ? 2 : 1;
99 needed = strlen (term) + 1;
101 /* Reserve space for the prefix */
103 needed += strlen (prefix) + 1;
105 if ((*buf == NULL) || (needed > *len)) {
107 *buf = talloc_realloc (ctx, *buf, char, *len);
117 /* Copy in the prefix */
119 strcpy (out, prefix);
120 out += strlen (prefix);
124 if (! need_quoting) {
129 /* Quote term by enclosing it in double quotes and doubling any
130 * internal double quotes. */
145 skip_space (const char *str)
147 while (*str && isspace ((unsigned char) *str))
153 parse_boolean_term (void *ctx, const char *str,
154 char **prefix_out, char **term_out)
157 *prefix_out = *term_out = NULL;
160 str = skip_space (str);
161 const char *pos = strchr (str, ':');
162 if (! pos || pos == str)
164 *prefix_out = talloc_strndup (ctx, str, pos - str);
171 /* Implement de-quoting compatible with make_boolean_term. */
173 char *out = talloc_array (ctx, char, strlen (pos));
180 /* Skip the opening quote, find the closing quote, and
181 * un-double doubled internal quotes. */
182 for (++pos; *pos; ) {
186 /* Found the closing quote. */
188 pos = skip_space (pos);
194 /* Did the term terminate without a closing quote or is there
195 * trailing text after the closing quote? */
200 const char *start = pos;
201 /* Check for text after the boolean term. */
202 while (! is_unquoted_terminator (*pos))
204 if (*skip_space (pos)) {
208 /* No trailing text; dup the string so the caller can free
210 *term_out = talloc_strndup (ctx, start, pos - start);
219 talloc_free (*prefix_out);
220 talloc_free (*term_out);