Commit | Line | Data |
---|---|---|
081e6815 | 1 | /* -*-c-*- |
2 | * | |
8656dc50 | 3 | * $Id: str.c,v 1.6 2004/04/08 01:36:13 mdw Exp $ |
081e6815 | 4 | * |
5 | * Functions for hacking with strings | |
6 | * | |
7 | * (c) 1999 Straylight/Edgeware | |
8 | */ | |
9 | ||
d4efbcd9 | 10 | /*----- Licensing notice --------------------------------------------------* |
081e6815 | 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. | |
d4efbcd9 | 18 | * |
081e6815 | 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. | |
d4efbcd9 | 23 | * |
081e6815 | 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 | ||
081e6815 | 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 | ||
efae42a6 | 41 | /* --- @str_qword@ --- * |
081e6815 | 42 | * |
43 | * Arguments: @char **pp@ = address of pointer into string | |
efae42a6 | 44 | * @unsigned f@ = various flags |
081e6815 | 45 | * |
efae42a6 | 46 | * Returns: Pointer to the next space-separated possibly-quoted word from |
47 | * the string, or null. | |
081e6815 | 48 | * |
efae42a6 | 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. | |
081e6815 | 52 | */ |
53 | ||
efae42a6 | 54 | char *str_qword(char **pp, unsigned f) |
081e6815 | 55 | { |
efae42a6 | 56 | char *p = *pp, *q, *qq; |
57 | int st = 0, pst = 0; | |
58 | ||
59 | /* --- Preliminaries --- */ | |
081e6815 | 60 | |
61 | if (!p) | |
62 | return (0); | |
081e6815 | 63 | while (isspace((unsigned char)*p)) |
64 | p++; | |
efae42a6 | 65 | if (!*p) { |
66 | *pp = 0; | |
67 | return (0); | |
68 | } | |
69 | ||
70 | /* --- Main work --- */ | |
081e6815 | 71 | |
efae42a6 | 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 | } | |
081e6815 | 106 | } |
107 | } | |
108 | ||
efae42a6 | 109 | /* --- Finished --- */ |
110 | ||
111 | done: | |
112 | *pp = *q ? q : 0; | |
113 | *qq++ = 0; | |
081e6815 | 114 | return (p); |
115 | } | |
116 | ||
efae42a6 | 117 | /* --- @str_qsplit@ --- * |
081e6815 | 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 | |
f3a542e8 | 122 | * @char **rest@ = where to store the remainder of the string |
efae42a6 | 123 | * @unsigned f@ = flags for @str_qword@ |
081e6815 | 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 | |
f3a542e8 | 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. | |
081e6815 | 136 | */ |
137 | ||
efae42a6 | 138 | size_t str_qsplit(char *p, char *v[], size_t c, char **rest, unsigned f) |
081e6815 | 139 | { |
140 | size_t n = 0; | |
141 | char *q; | |
142 | ||
efae42a6 | 143 | while (c && (q = str_qword(&p, f)) != 0) { |
081e6815 | 144 | *v++ = q; |
145 | c--; | |
146 | n++; | |
147 | } | |
081e6815 | 148 | while (c) { |
149 | *v++ = 0; | |
150 | c--; | |
151 | } | |
48d198f1 | 152 | if (rest) |
153 | *rest = p; | |
081e6815 | 154 | return (n); |
155 | } | |
156 | ||
efae42a6 | 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 | ||
d45be1af | 168 | char *str_getword(char **pp) { return (str_qword(pp, 0)); } |
efae42a6 | 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) | |
d45be1af | 184 | { return (str_qsplit(p, v, c, rest, 0)); } |
efae42a6 | 185 | |
26f325c0 | 186 | /* --- @str_matchx@ --- * |
efae42a6 | 187 | * |
188 | * Arguments: @const char *p@ = pointer to pattern string | |
189 | * @const char *s@ = string to compare with | |
26f325c0 | 190 | * @unsigned f@ = various flags |
efae42a6 | 191 | * |
192 | * Returns: Nonzero if the pattern matches the string. | |
193 | * | |
194 | * Use: Does simple wildcard matching. This is quite nasty and more | |
195 | * than a little slow. Supports metacharacters `*', `?' and | |
196 | * '['. | |
197 | */ | |
198 | ||
26f325c0 | 199 | int str_matchx(const char *p, const char *s, unsigned f) |
efae42a6 | 200 | { |
201 | for (;;) { | |
202 | char pch = *p++, pche, sch; | |
203 | int sense; | |
204 | ||
26f325c0 MW |
205 | if ((f & STRF_PREFIX) && !*s) |
206 | return (1); | |
efae42a6 | 207 | switch (pch) { |
208 | case '?': | |
209 | if (!*s) | |
210 | return (0); | |
211 | s++; | |
212 | break; | |
213 | case '*': | |
26f325c0 | 214 | if (!*p || (f & STRF_PREFIX)) |
efae42a6 | 215 | return (1); |
216 | while (*s) { | |
217 | if (str_match(p, s)) | |
218 | return (1); | |
219 | s++; | |
220 | } | |
221 | return (0); | |
222 | case '[': | |
223 | if (!*s) | |
224 | return (0); | |
225 | sch = *s++; | |
226 | pch = *p++; | |
227 | sense = 1; | |
228 | if (pch == '^' || pch == '!') { | |
229 | sense = !sense; | |
230 | pch = *p++; | |
231 | } | |
232 | if (pch == ']') { | |
233 | if (*p == '-' && p[1] && p[1] != ']') { | |
234 | pche = p[1]; | |
235 | p += 2; | |
236 | if (pch <= sch && sch <= pche) | |
237 | goto class_match; | |
238 | } else if (pch == sch) | |
239 | goto class_match; | |
240 | pch = *p++; | |
241 | } | |
242 | for (;; pch = *p++) { | |
243 | if (!pch || pch == ']') | |
244 | goto class_nomatch; | |
245 | if (*p == '-' && p[1] && p[1] != ']') { | |
246 | pche = p[1]; | |
247 | p += 2; | |
248 | if (pch <= sch && sch <= pche) | |
249 | goto class_match; | |
250 | } else if (pch == sch) | |
251 | goto class_match; | |
252 | } | |
253 | class_match: | |
254 | if (!sense) | |
255 | return (0); | |
256 | for (;;) { | |
257 | pch = *p++; | |
258 | if (!pch) | |
259 | return (0); | |
260 | if (pch == ']') | |
261 | break; | |
262 | if (*p == '-' && p[1] && p[1] != ']') | |
263 | p += 2; | |
264 | } | |
265 | break; | |
266 | class_nomatch: | |
267 | if (sense) | |
268 | return (0); | |
269 | break; | |
270 | case '\\': | |
271 | pch = *p++; | |
272 | default: | |
273 | if (pch != *s) | |
274 | return (0); | |
275 | if (!pch) | |
276 | return (1); | |
277 | s++; | |
278 | break; | |
279 | } | |
280 | } | |
281 | } | |
282 | ||
26f325c0 MW |
283 | /* --- @str_match@ --- * |
284 | * | |
285 | * Arguments: @const char *p@ = pointer to pattern string | |
286 | * @const char *s@ = string to compare with | |
287 | * | |
288 | * Returns: Nonzero if the pattern matches the string. | |
289 | * | |
290 | * Use: Does simple wildcard matching. Equivalent to @str_matchx@ | |
291 | * with zero flags word. | |
292 | */ | |
293 | ||
294 | int str_match(const char *p, const char *s) | |
295 | { return (str_matchx(p, s, 0)); } | |
296 | ||
081e6815 | 297 | /* --- @str_sanitize@ --- * |
298 | * | |
299 | * Arguments: @char *d@ = destination buffer | |
300 | * @const char *p@ = pointer to source string | |
301 | * @size_t sz@ = size of destination buffer | |
302 | * | |
303 | * Returns: --- | |
304 | * | |
305 | * Use: Writes a string into a buffer, being careful not to overflow | |
306 | * the buffer, to null terminate the result, and to prevent | |
307 | * nasty nonprintable characters ending up in the buffer. | |
308 | */ | |
309 | ||
310 | void str_sanitize(char *d, const char *p, size_t sz) | |
311 | { | |
312 | if (!sz) | |
313 | return; | |
314 | sz--; | |
315 | while (*p && sz) { | |
316 | int ch = *p++; | |
317 | if (!isgraph((unsigned char)ch)) | |
318 | ch = '_'; | |
319 | *d++ = ch; | |
320 | sz--; | |
321 | } | |
322 | *d++ = 0; | |
323 | } | |
324 | ||
325 | /*----- That's all, folks -------------------------------------------------*/ |