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