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