str.c revision 1.59 1 /* $NetBSD: str.c,v 1.59 2020/08/10 19:53:19 rillig Exp $ */
2
3 /*-
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 /*-
36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved.
38 *
39 * This code is derived from software contributed to Berkeley by
40 * Adam de Boor.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 */
70
71 #ifndef MAKE_NATIVE
72 static char rcsid[] = "$NetBSD: str.c,v 1.59 2020/08/10 19:53:19 rillig Exp $";
73 #else
74 #include <sys/cdefs.h>
75 #ifndef lint
76 #if 0
77 static char sccsid[] = "@(#)str.c 5.8 (Berkeley) 6/1/90";
78 #else
79 __RCSID("$NetBSD: str.c,v 1.59 2020/08/10 19:53:19 rillig Exp $");
80 #endif
81 #endif /* not lint */
82 #endif
83
84 #include "make.h"
85
86 /* Return the concatenation of s1 and s2, freshly allocated. */
87 char *
88 str_concat2(const char *s1, const char *s2)
89 {
90 size_t len1 = strlen(s1);
91 size_t len2 = strlen(s2);
92 char *result = bmake_malloc(len1 + len2 + 1);
93 memcpy(result, s1, len1);
94 memcpy(result + len1, s2, len2 + 1);
95 return result;
96 }
97
98 /* Return the concatenation of s1, s2 and s3, freshly allocated. */
99 char *
100 str_concat3(const char *s1, const char *s2, const char *s3)
101 {
102 size_t len1 = strlen(s1);
103 size_t len2 = strlen(s2);
104 size_t len3 = strlen(s3);
105 char *result = bmake_malloc(len1 + len2 + len3 + 1);
106 memcpy(result, s1, len1);
107 memcpy(result + len1, s2, len2);
108 memcpy(result + len1 + len2, s3, len3 + 1);
109 return result;
110 }
111
112 /*-
113 * brk_string --
114 * Fracture a string into an array of words (as delineated by tabs or
115 * spaces) taking quotation marks into account. Leading tabs/spaces
116 * are ignored.
117 *
118 * If expand is TRUE, quotes are removed and escape sequences
119 * such as \r, \t, etc... are expanded. In this case, the return value
120 * is NULL on parse errors.
121 *
122 * returns --
123 * Pointer to the array of pointers to the words.
124 * Memory containing the actual words in *out_words_buf.
125 * Both of these must be free'd by the caller.
126 * Number of words in *out_words_len.
127 */
128 char **
129 brk_string(const char *str, int *out_words_len, Boolean expand,
130 char **out_words_buf)
131 {
132 size_t str_len;
133 char *words_buf;
134 int words_cap;
135 char **words;
136 int words_len;
137 char inquote;
138 char *word_start;
139 char *word_end;
140 const char *str_p;
141
142 /* skip leading space chars. */
143 for (; *str == ' ' || *str == '\t'; ++str)
144 continue;
145
146 /* words_buf holds the words, separated by '\0'. */
147 str_len = strlen(str);
148 words_buf = bmake_malloc(strlen(str) + 1);
149
150 words_cap = MAX((str_len / 5), 50);
151 words = bmake_malloc((words_cap + 1) * sizeof(char *));
152
153 /*
154 * copy the string; at the same time, parse backslashes,
155 * quotes and build the word list.
156 */
157 words_len = 0;
158 inquote = '\0';
159 word_start = words_buf;
160 word_end = words_buf;
161 for (str_p = str;; ++str_p) {
162 char ch = *str_p;
163 switch (ch) {
164 case '"':
165 case '\'':
166 if (inquote) {
167 if (inquote == ch)
168 inquote = '\0';
169 else
170 break;
171 } else {
172 inquote = (char)ch;
173 /* Don't miss "" or '' */
174 if (word_start == NULL && str_p[1] == inquote) {
175 if (!expand) {
176 word_start = word_end;
177 *word_end++ = ch;
178 } else
179 word_start = word_end + 1;
180 str_p++;
181 inquote = '\0';
182 break;
183 }
184 }
185 if (!expand) {
186 if (word_start == NULL)
187 word_start = word_end;
188 *word_end++ = ch;
189 }
190 continue;
191 case ' ':
192 case '\t':
193 case '\n':
194 if (inquote)
195 break;
196 if (word_start == NULL)
197 continue;
198 /* FALLTHROUGH */
199 case '\0':
200 /*
201 * end of a token -- make sure there's enough words
202 * space and save off a pointer.
203 */
204 if (word_start == NULL)
205 goto done;
206
207 *word_end++ = '\0';
208 if (words_len == words_cap) {
209 size_t new_size;
210 words_cap *= 2; /* ramp up fast */
211 new_size = (words_cap + 1) * sizeof(char *);
212 words = bmake_realloc(words, new_size);
213 }
214 words[words_len++] = word_start;
215 word_start = NULL;
216 if (ch == '\n' || ch == '\0') {
217 if (expand && inquote) {
218 free(words);
219 free(words_buf);
220 *out_words_buf = NULL;
221 return NULL;
222 }
223 goto done;
224 }
225 continue;
226 case '\\':
227 if (!expand) {
228 if (word_start == NULL)
229 word_start = word_end;
230 *word_end++ = '\\';
231 /* catch '\' at end of line */
232 if (str_p[1] == '\0')
233 continue;
234 ch = *++str_p;
235 break;
236 }
237
238 switch (ch = *++str_p) {
239 case '\0':
240 case '\n':
241 /* hmmm; fix it up as best we can */
242 ch = '\\';
243 --str_p;
244 break;
245 case 'b':
246 ch = '\b';
247 break;
248 case 'f':
249 ch = '\f';
250 break;
251 case 'n':
252 ch = '\n';
253 break;
254 case 'r':
255 ch = '\r';
256 break;
257 case 't':
258 ch = '\t';
259 break;
260 }
261 break;
262 }
263 if (word_start == NULL)
264 word_start = word_end;
265 *word_end++ = ch;
266 }
267 done:
268 words[words_len] = NULL;
269 *out_words_len = words_len;
270 *out_words_buf = words_buf;
271 return words;
272 }
273
274 /*
275 * Str_FindSubstring -- See if a string contains a particular substring.
276 *
277 * Input:
278 * string String to search.
279 * substring Substring to find in string.
280 *
281 * Results: If string contains substring, the return value is the location of
282 * the first matching instance of substring in string. If string doesn't
283 * contain substring, the return value is NULL. Matching is done on an exact
284 * character-for-character basis with no wildcards or special characters.
285 *
286 * Side effects: None.
287 */
288 char *
289 Str_FindSubstring(const char *string, const char *substring)
290 {
291 const char *a, *b;
292
293 /*
294 * First scan quickly through the two strings looking for a single-
295 * character match. When it's found, then compare the rest of the
296 * substring.
297 */
298
299 for (b = substring; *string != 0; string++) {
300 if (*string != *b)
301 continue;
302 a = string;
303 for (;;) {
304 if (*b == 0)
305 return UNCONST(string);
306 if (*a++ != *b++)
307 break;
308 }
309 b = substring;
310 }
311 return NULL;
312 }
313
314 /*
315 * Str_Match -- Test if a string matches a pattern like "*.[ch]".
316 *
317 * XXX this function does not detect or report malformed patterns.
318 *
319 * Results:
320 * Non-zero is returned if string matches the pattern, 0 otherwise. The
321 * matching operation permits the following special characters in the
322 * pattern: *?\[] (as in fnmatch(3)).
323 *
324 * Side effects: None.
325 */
326 Boolean
327 Str_Match(const char *str, const char *pat)
328 {
329 for (;;) {
330 /*
331 * See if we're at the end of both the pattern and the
332 * string. If, we succeeded. If we're at the end of the
333 * pattern but not at the end of the string, we failed.
334 */
335 if (*pat == 0)
336 return *str == 0;
337 if (*str == 0 && *pat != '*')
338 return FALSE;
339
340 /*
341 * A '*' in the pattern matches any substring. We handle this
342 * by calling ourselves for each suffix of the string.
343 */
344 if (*pat == '*') {
345 pat++;
346 while (*pat == '*')
347 pat++;
348 if (*pat == 0)
349 return TRUE;
350 while (*str != 0) {
351 if (Str_Match(str, pat))
352 return TRUE;
353 str++;
354 }
355 return FALSE;
356 }
357
358 /* A '?' in the pattern matches any single character. */
359 if (*pat == '?')
360 goto thisCharOK;
361
362 /*
363 * A '[' in the pattern matches a character from a list.
364 * The '[' is followed by the list of acceptable characters,
365 * or by ranges (two characters separated by '-'). In these
366 * character lists, the backslash is an ordinary character.
367 */
368 if (*pat == '[') {
369 Boolean neg = pat[1] == '^';
370 pat += 1 + neg;
371
372 for (;;) {
373 if (*pat == ']' || *pat == 0) {
374 if (neg)
375 break;
376 return FALSE;
377 }
378 if (*pat == *str)
379 break;
380 if (pat[1] == '-') {
381 if (pat[2] == 0)
382 return neg;
383 if (*pat <= *str && pat[2] >= *str)
384 break;
385 if (*pat >= *str && pat[2] <= *str)
386 break;
387 pat += 2;
388 }
389 pat++;
390 }
391 if (neg && *pat != ']' && *pat != 0)
392 return FALSE;
393 while (*pat != ']' && *pat != 0)
394 pat++;
395 if (*pat == 0)
396 pat--;
397 goto thisCharOK;
398 }
399
400 /*
401 * A backslash in the pattern matches the character following
402 * it exactly.
403 */
404 if (*pat == '\\') {
405 pat++;
406 if (*pat == 0)
407 return FALSE;
408 }
409
410 if (*pat != *str)
411 return FALSE;
412
413 thisCharOK:
414 pat++;
415 str++;
416 }
417 }
418