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