str: Various whitespace cleanups.
[mLib] / str.c
1 /* -*-c-*-
2 *
3 * $Id: str.c,v 1.6 2004/04/08 01:36:13 mdw Exp $
4 *
5 * Functions for hacking with strings
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the mLib utilities library.
13 *
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Header files ------------------------------------------------------*/
31
32 #include <ctype.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "str.h"
38
39 /*----- Main code ---------------------------------------------------------*/
40
41 /* --- @str_qword@ --- *
42 *
43 * Arguments: @char **pp@ = address of pointer into string
44 * @unsigned f@ = various flags
45 *
46 * Returns: Pointer to the next space-separated possibly-quoted word from
47 * the string, or null.
48 *
49 * Use: Fetches the next word from a string. If the flag
50 * @STRF_QUOTE@ is set, the `\' character acts as an escape, and
51 * single and double quotes protect whitespace.
52 */
53
54 char *str_qword(char **pp, unsigned f)
55 {
56 char *p = *pp, *q, *qq;
57 int st = 0, pst = 0;
58
59 /* --- Preliminaries --- */
60
61 if (!p)
62 return (0);
63 while (isspace((unsigned char)*p))
64 p++;
65 if (!*p) {
66 *pp = 0;
67 return (0);
68 }
69
70 /* --- Main work --- */
71
72 for (q = qq = p; *q; q++) {
73 switch (st) {
74 case '\\':
75 *qq++ = *q;
76 st = pst;
77 break;
78 case '\'':
79 case '\"':
80 if (*q == st)
81 st = pst = 0;
82 else if (*q == '\\')
83 st = '\\';
84 else
85 *qq++ = *q;
86 break;
87 default:
88 if (isspace((unsigned char)*q)) {
89 do q++; while (*q && isspace((unsigned char)*q));
90 goto done;
91 } else if (!(f & STRF_QUOTE))
92 goto stdchar;
93 switch (*q) {
94 case '\\':
95 st = '\\';
96 break;
97 case '\'':
98 case '\"':
99 st = pst = *q;
100 break;
101 default:
102 stdchar:
103 *qq++ = *q;
104 break;
105 }
106 }
107 }
108
109 /* --- Finished --- */
110
111 done:
112 *pp = *q ? q : 0;
113 *qq++ = 0;
114 return (p);
115 }
116
117 /* --- @str_qsplit@ --- *
118 *
119 * Arguments: @char *p@ = pointer to string
120 * @char *v[]@ = pointer to array to fill in
121 * @size_t c@ = count of strings to fill in
122 * @char **rest@ = where to store the remainder of the string
123 * @unsigned f@ = flags for @str_qword@
124 *
125 * Returns: Number of strings filled in.
126 *
127 * Use: Fills an array with pointers to the individual words of a
128 * string. The string is modified in place to contain zero
129 * bytes at the word boundaries, and the words have leading
130 * and trailing space stripped off. No more than @c@ words
131 * are read; the actual number is returned as the value of the
132 * function. Unused slots in the array are populated with
133 * null bytes. If there's any string left, the address of the
134 * remainder is stored in @rest@ (if it's non-null); otherwise
135 * @rest@ is set to a null pointer.
136 */
137
138 size_t str_qsplit(char *p, char *v[], size_t c, char **rest, unsigned f)
139 {
140 size_t n = 0;
141 char *q;
142
143 while (c && (q = str_qword(&p, f)) != 0) {
144 *v++ = q;
145 c--;
146 n++;
147 }
148 while (c) {
149 *v++ = 0;
150 c--;
151 }
152 if (rest)
153 *rest = p;
154 return (n);
155 }
156
157 /* --- @str_getword@ --- *
158 *
159 * Arguments: @char **pp@ = address of pointer into string
160 *
161 * Returns: Pointer to the next space-separated word from the string,
162 * or null.
163 *
164 * Use: Parses off space-separated words from a string. This is a
165 * compatibility veneer over @str_qword@.
166 */
167
168 char *str_getword(char **pp) { return (str_qword(pp, 0)); }
169
170 /* --- @str_split@ --- *
171 *
172 * Arguments: @char *p@ = pointer to string
173 * @char *v[]@ = pointer to array to fill in
174 * @size_t c@ = count of strings to fill in
175 * @char **rest@ = where to store the remainder of the string
176 *
177 * Returns: Number of strings filled in.
178 *
179 * Use: Fills an array with pointers to the individual words of a
180 * string. This is a compatibility veneer over @str_qsplit@.
181 */
182
183 size_t str_split(char *p, char *v[], size_t c, char **rest)
184 { return (str_qsplit(p, v, c, rest, 0)); }
185
186 /* --- @str_match@ --- *
187 *
188 * Arguments: @const char *p@ = pointer to pattern string
189 * @const char *s@ = string to compare with
190 *
191 * Returns: Nonzero if the pattern matches the string.
192 *
193 * Use: Does simple wildcard matching. This is quite nasty and more
194 * than a little slow. Supports metacharacters `*', `?' and
195 * '['.
196 */
197
198 int str_match(const char *p, const char *s)
199 {
200 for (;;) {
201 char pch = *p++, pche, sch;
202 int sense;
203
204 switch (pch) {
205 case '?':
206 if (!*s)
207 return (0);
208 s++;
209 break;
210 case '*':
211 if (!*p)
212 return (1);
213 while (*s) {
214 if (str_match(p, s))
215 return (1);
216 s++;
217 }
218 return (0);
219 case '[':
220 if (!*s)
221 return (0);
222 sch = *s++;
223 pch = *p++;
224 sense = 1;
225 if (pch == '^' || pch == '!') {
226 sense = !sense;
227 pch = *p++;
228 }
229 if (pch == ']') {
230 if (*p == '-' && p[1] && p[1] != ']') {
231 pche = p[1];
232 p += 2;
233 if (pch <= sch && sch <= pche)
234 goto class_match;
235 } else if (pch == sch)
236 goto class_match;
237 pch = *p++;
238 }
239 for (;; pch = *p++) {
240 if (!pch || pch == ']')
241 goto class_nomatch;
242 if (*p == '-' && p[1] && p[1] != ']') {
243 pche = p[1];
244 p += 2;
245 if (pch <= sch && sch <= pche)
246 goto class_match;
247 } else if (pch == sch)
248 goto class_match;
249 }
250 class_match:
251 if (!sense)
252 return (0);
253 for (;;) {
254 pch = *p++;
255 if (!pch)
256 return (0);
257 if (pch == ']')
258 break;
259 if (*p == '-' && p[1] && p[1] != ']')
260 p += 2;
261 }
262 break;
263 class_nomatch:
264 if (sense)
265 return (0);
266 break;
267 case '\\':
268 pch = *p++;
269 default:
270 if (pch != *s)
271 return (0);
272 if (!pch)
273 return (1);
274 s++;
275 break;
276 }
277 }
278 }
279
280 /* --- @str_sanitize@ --- *
281 *
282 * Arguments: @char *d@ = destination buffer
283 * @const char *p@ = pointer to source string
284 * @size_t sz@ = size of destination buffer
285 *
286 * Returns: ---
287 *
288 * Use: Writes a string into a buffer, being careful not to overflow
289 * the buffer, to null terminate the result, and to prevent
290 * nasty nonprintable characters ending up in the buffer.
291 */
292
293 void str_sanitize(char *d, const char *p, size_t sz)
294 {
295 if (!sz)
296 return;
297 sz--;
298 while (*p && sz) {
299 int ch = *p++;
300 if (!isgraph((unsigned char)ch))
301 ch = '_';
302 *d++ = ch;
303 sz--;
304 }
305 *d++ = 0;
306 }
307
308 /*----- That's all, folks -------------------------------------------------*/