str.c revision 1.58 1 /* $NetBSD: str.c,v 1.58 2020/08/10 19:30:30 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.58 2020/08/10 19:30:30 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.58 2020/08/10 19:30:30 rillig Exp $");
80 #endif
81 #endif /* not lint */
82 #endif
83
84 #include "make.h"
85
86 /*-
87 * str_concat --
88 * concatenate the two strings, inserting a space or slash between them.
89 *
90 * returns --
91 * the resulting string in allocated space.
92 */
93 char *
94 str_concat(const char *s1, const char *s2, StrConcatMode mode)
95 {
96 size_t len1 = strlen(s1);
97 size_t len2 = strlen(s2);
98 char *result = bmake_malloc(len1 + 1 + len2 + 1);
99
100 /* copy first string into place */
101 memcpy(result, s1, len1);
102
103 /* add separator character */
104 if (mode == STR_ADDSPACE) {
105 result[len1] = ' ';
106 ++len1;
107 } else if (mode == STR_ADDSLASH) {
108 result[len1] = '/';
109 ++len1;
110 }
111
112 /* copy second string plus EOS into place */
113 memcpy(result + len1, s2, len2 + 1);
114
115 return result;
116 }
117
118 /*-
119 * brk_string --
120 * Fracture a string into an array of words (as delineated by tabs or
121 * spaces) taking quotation marks into account. Leading tabs/spaces
122 * are ignored.
123 *
124 * If expand is TRUE, quotes are removed and escape sequences
125 * such as \r, \t, etc... are expanded. In this case, the return value
126 * is NULL on parse errors.
127 *
128 * returns --
129 * Pointer to the array of pointers to the words.
130 * Memory containing the actual words in *out_words_buf.
131 * Both of these must be free'd by the caller.
132 * Number of words in *out_words_len.
133 */
134 char **
135 brk_string(const char *str, int *out_words_len, Boolean expand,
136 char **out_words_buf)
137 {
138 size_t str_len;
139 char *words_buf;
140 int words_cap;
141 char **words;
142 int words_len;
143 char inquote;
144 char *word_start;
145 char *word_end;
146 const char *str_p;
147
148 /* skip leading space chars. */
149 for (; *str == ' ' || *str == '\t'; ++str)
150 continue;
151
152 /* words_buf holds the words, separated by '\0'. */
153 str_len = strlen(str);
154 words_buf = bmake_malloc(strlen(str) + 1);
155
156 words_cap = MAX((str_len / 5), 50);
157 words = bmake_malloc((words_cap + 1) * sizeof(char *));
158
159 /*
160 * copy the string; at the same time, parse backslashes,
161 * quotes and build the word list.
162 */
163 words_len = 0;
164 inquote = '\0';
165 word_start = words_buf;
166 word_end = words_buf;
167 for (str_p = str;; ++str_p) {
168 char ch = *str_p;
169 switch (ch) {
170 case '"':
171 case '\'':
172 if (inquote) {
173 if (inquote == ch)
174 inquote = '\0';
175 else
176 break;
177 } else {
178 inquote = (char)ch;
179 /* Don't miss "" or '' */
180 if (word_start == NULL && str_p[1] == inquote) {
181 if (!expand) {
182 word_start = word_end;
183 *word_end++ = ch;
184 } else
185 word_start = word_end + 1;
186 str_p++;
187 inquote = '\0';
188 break;
189 }
190 }
191 if (!expand) {
192 if (word_start == NULL)
193 word_start = word_end;
194 *word_end++ = ch;
195 }
196 continue;
197 case ' ':
198 case '\t':
199 case '\n':
200 if (inquote)
201 break;
202 if (word_start == NULL)
203 continue;
204 /* FALLTHROUGH */
205 case '\0':
206 /*
207 * end of a token -- make sure there's enough words
208 * space and save off a pointer.
209 */
210 if (word_start == NULL)
211 goto done;
212
213 *word_end++ = '\0';
214 if (words_len == words_cap) {
215 size_t new_size;
216 words_cap *= 2; /* ramp up fast */
217 new_size = (words_cap + 1) * sizeof(char *);
218 words = bmake_realloc(words, new_size);
219 }
220 words[words_len++] = word_start;
221 word_start = NULL;
222 if (ch == '\n' || ch == '\0') {
223 if (expand && inquote) {
224 free(words);
225 free(words_buf);
226 *out_words_buf = NULL;
227 return NULL;
228 }
229 goto done;
230 }
231 continue;
232 case '\\':
233 if (!expand) {
234 if (word_start == NULL)
235 word_start = word_end;
236 *word_end++ = '\\';
237 /* catch '\' at end of line */
238 if (str_p[1] == '\0')
239 continue;
240 ch = *++str_p;
241 break;
242 }
243
244 switch (ch = *++str_p) {
245 case '\0':
246 case '\n':
247 /* hmmm; fix it up as best we can */
248 ch = '\\';
249 --str_p;
250 break;
251 case 'b':
252 ch = '\b';
253 break;
254 case 'f':
255 ch = '\f';
256 break;
257 case 'n':
258 ch = '\n';
259 break;
260 case 'r':
261 ch = '\r';
262 break;
263 case 't':
264 ch = '\t';
265 break;
266 }
267 break;
268 }
269 if (word_start == NULL)
270 word_start = word_end;
271 *word_end++ = ch;
272 }
273 done:
274 words[words_len] = NULL;
275 *out_words_len = words_len;
276 *out_words_buf = words_buf;
277 return words;
278 }
279
280 /*
281 * Str_FindSubstring -- See if a string contains a particular substring.
282 *
283 * Input:
284 * string String to search.
285 * substring Substring to find in string.
286 *
287 * Results: If string contains substring, the return value is the location of
288 * the first matching instance of substring in string. If string doesn't
289 * contain substring, the return value is NULL. Matching is done on an exact
290 * character-for-character basis with no wildcards or special characters.
291 *
292 * Side effects: None.
293 */
294 char *
295 Str_FindSubstring(const char *string, const char *substring)
296 {
297 const char *a, *b;
298
299 /*
300 * First scan quickly through the two strings looking for a single-
301 * character match. When it's found, then compare the rest of the
302 * substring.
303 */
304
305 for (b = substring; *string != 0; string++) {
306 if (*string != *b)
307 continue;
308 a = string;
309 for (;;) {
310 if (*b == 0)
311 return UNCONST(string);
312 if (*a++ != *b++)
313 break;
314 }
315 b = substring;
316 }
317 return NULL;
318 }
319
320 /*
321 * Str_Match -- Test if a string matches a pattern like "*.[ch]".
322 *
323 * XXX this function does not detect or report malformed patterns.
324 *
325 * Results:
326 * Non-zero is returned if string matches the pattern, 0 otherwise. The
327 * matching operation permits the following special characters in the
328 * pattern: *?\[] (as in fnmatch(3)).
329 *
330 * Side effects: None.
331 */
332 Boolean
333 Str_Match(const char *str, const char *pat)
334 {
335 for (;;) {
336 /*
337 * See if we're at the end of both the pattern and the
338 * string. If, we succeeded. If we're at the end of the
339 * pattern but not at the end of the string, we failed.
340 */
341 if (*pat == 0)
342 return *str == 0;
343 if (*str == 0 && *pat != '*')
344 return FALSE;
345
346 /*
347 * A '*' in the pattern matches any substring. We handle this
348 * by calling ourselves for each suffix of the string.
349 */
350 if (*pat == '*') {
351 pat++;
352 while (*pat == '*')
353 pat++;
354 if (*pat == 0)
355 return TRUE;
356 while (*str != 0) {
357 if (Str_Match(str, pat))
358 return TRUE;
359 str++;
360 }
361 return FALSE;
362 }
363
364 /* A '?' in the pattern matches any single character. */
365 if (*pat == '?')
366 goto thisCharOK;
367
368 /*
369 * A '[' in the pattern matches a character from a list.
370 * The '[' is followed by the list of acceptable characters,
371 * or by ranges (two characters separated by '-'). In these
372 * character lists, the backslash is an ordinary character.
373 */
374 if (*pat == '[') {
375 Boolean neg = pat[1] == '^';
376 pat += 1 + neg;
377
378 for (;;) {
379 if (*pat == ']' || *pat == 0) {
380 if (neg)
381 break;
382 return FALSE;
383 }
384 if (*pat == *str)
385 break;
386 if (pat[1] == '-') {
387 if (pat[2] == 0)
388 return neg;
389 if (*pat <= *str && pat[2] >= *str)
390 break;
391 if (*pat >= *str && pat[2] <= *str)
392 break;
393 pat += 2;
394 }
395 pat++;
396 }
397 if (neg && *pat != ']' && *pat != 0)
398 return FALSE;
399 while (*pat != ']' && *pat != 0)
400 pat++;
401 if (*pat == 0)
402 pat--;
403 goto thisCharOK;
404 }
405
406 /*
407 * A backslash in the pattern matches the character following
408 * it exactly.
409 */
410 if (*pat == '\\') {
411 pat++;
412 if (*pat == 0)
413 return FALSE;
414 }
415
416 if (*pat != *str)
417 return FALSE;
418
419 thisCharOK:
420 pat++;
421 str++;
422 }
423 }
424