1 /* $NetBSD: str.c,v 1.51 2020/07/03 07:40:13 rillig Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved.
39 * This code is derived from software contributed to Berkeley by
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
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.
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
72 static char rcsid[] = "$NetBSD: str.c,v 1.51 2020/07/03 07:40:13 rillig Exp $";
74 #include <sys/cdefs.h>
77 static char sccsid[] = "@(#)str.c 5.8 (Berkeley) 6/1/90";
79 __RCSID("$NetBSD: str.c,v 1.51 2020/07/03 07:40:13 rillig Exp $");
88 * concatenate the two strings, inserting a space or slash between them,
89 * freeing them if requested.
92 * the resulting string in allocated space.
95 str_concat(const char *s1, const char *s2, int flags)
100 /* get the length of both strings */
104 /* allocate length plus separator plus EOS */
105 result = bmake_malloc((unsigned int)(len1 + len2 + 2));
107 /* copy first string into place */
108 memcpy(result, s1, len1);
110 /* add separator character */
111 if (flags & STR_ADDSPACE) {
114 } else if (flags & STR_ADDSLASH) {
119 /* copy second string plus EOS into place */
120 memcpy(result + len1, s2, len2 + 1);
127 * Fracture a string into an array of words (as delineated by tabs or
128 * spaces) taking quotation marks into account. Leading tabs/spaces
131 * If expand is TRUE, quotes are removed and escape sequences
132 * such as \r, \t, etc... are expanded.
135 * Pointer to the array of pointers to the words.
136 * Memory containing the actual words in *store_words_buf.
137 * Both of these must be free'd by the caller.
138 * Number of words in *store_words_len.
141 brk_string(const char *str, int *store_words_len, Boolean expand,
142 char **store_words_buf)
150 char *words_buf, *word_start, *word_end;
152 /* skip leading space chars. */
153 for (; *str == ' ' || *str == '\t'; ++str)
156 /* words_buf holds the words, separated by '\0'. */
157 str_len = strlen(str);
158 words_buf = bmake_malloc(strlen(str) + 1);
160 words_cap = MAX((str_len / 5), 50);
161 words = bmake_malloc((words_cap + 1) * sizeof(char *));
164 * copy the string; at the same time, parse backslashes,
165 * quotes and build the word list.
169 word_start = word_end = words_buf;
170 for (str_p = str;; ++str_p) {
183 /* Don't miss "" or '' */
184 if (word_start == NULL && str_p[1] == inquote) {
186 word_start = word_end;
189 word_start = word_end + 1;
196 if (word_start == NULL)
197 word_start = word_end;
206 if (word_start == NULL)
211 * end of a token -- make sure there's enough words
212 * space and save off a pointer.
214 if (word_start == NULL)
218 if (words_len == words_cap) {
219 words_cap *= 2; /* ramp up fast */
220 words = (char **)bmake_realloc(words,
221 (words_cap + 1) * sizeof(char *));
223 words[words_len++] = word_start;
225 if (ch == '\n' || ch == '\0') {
226 if (expand && inquote) {
229 *store_words_buf = NULL;
237 if (word_start == NULL)
238 word_start = word_end;
240 /* catch '\' at end of line */
241 if (str_p[1] == '\0')
247 switch (ch = *++str_p) {
250 /* hmmm; fix it up as best we can */
272 if (word_start == NULL)
273 word_start = word_end;
276 done: words[words_len] = NULL;
277 *store_words_len = words_len;
278 *store_words_buf = words_buf;
283 * Str_FindSubstring -- See if a string contains a particular substring.
286 * string String to search.
287 * substring Substring to find in string.
289 * Results: If string contains substring, the return value is the location of
290 * the first matching instance of substring in string. If string doesn't
291 * contain substring, the return value is NULL. Matching is done on an exact
292 * character-for-character basis with no wildcards or special characters.
294 * Side effects: None.
297 Str_FindSubstring(const char *string, const char *substring)
302 * First scan quickly through the two strings looking for a single-
303 * character match. When it's found, then compare the rest of the
307 for (b = substring; *string != 0; string++) {
313 return UNCONST(string);
323 * Str_Match -- Test if a string matches a pattern like "*.[ch]".
325 * XXX this function does not detect or report malformed patterns.
328 * Non-zero is returned if string matches the pattern, 0 otherwise. The
329 * matching operation permits the following special characters in the
330 * pattern: *?\[] (as in fnmatch(3)).
332 * Side effects: None.
335 Str_Match(const char *str, const char *pat)
339 * See if we're at the end of both the pattern and the
340 * string. If, we succeeded. If we're at the end of the
341 * pattern but not at the end of the string, we failed.
345 if (*str == 0 && *pat != '*')
349 * A '*' in the pattern matches any substring. We handle this
350 * by calling ourselves for each suffix of the string.
359 if (Str_Match(str, pat))
366 /* A '?' in the pattern matches any single character. */
371 * A '[' in the pattern matches a character from a list.
372 * The '[' is followed by the list of acceptable characters,
373 * or by ranges (two characters separated by '-'). In these
374 * character lists, the backslash is an ordinary character.
377 Boolean neg = pat[1] == '^';
381 if (*pat == ']' || *pat == 0) {
391 if (*pat <= *str && pat[2] >= *str)
393 if (*pat >= *str && pat[2] <= *str)
399 if (neg && *pat != ']' && *pat != 0)
401 while (*pat != ']' && *pat != 0)
409 * A backslash in the pattern matches the character following
428 *-----------------------------------------------------------------------
430 * Check word against pattern for a match (% is wild),
433 * word Word to examine
434 * pattern Pattern to examine against
435 * len Number of characters to substitute
438 * Returns the beginning position of a match or null. The number
439 * of characters matched is returned in len.
444 *-----------------------------------------------------------------------
447 Str_SYSVMatch(const char *word, const char *pattern, size_t *len,
450 const char *p = pattern;
451 const char *w = word;
456 /* Null pattern is the whole string */
461 if ((m = strchr(p, '%')) != NULL) {
464 /* empty word does not match pattern */
467 /* check that the prefix matches */
468 for (; p != m && *w && *w == *p; w++, p++)
472 return NULL; /* No match */
475 /* No more pattern, return the rest of the string */
483 /* Find a matching tail */
485 if (strcmp(p, w) == 0) {
489 while (*w++ != '\0');
496 *-----------------------------------------------------------------------
498 * Substitute '%' on the pattern with len characters from src.
499 * If the pattern does not contain a '%' prepend len characters
506 * Places result on buf
508 *-----------------------------------------------------------------------
511 Str_SYSVSubst(Buffer *buf, char *pat, char *src, size_t len,
512 Boolean lhsHasPercent)
516 if ((m = strchr(pat, '%')) != NULL && lhsHasPercent) {
517 /* Copy the prefix */
518 Buf_AddBytes(buf, m - pat, pat);
522 if (m != NULL || !lhsHasPercent) {
523 /* Copy the pattern */
524 Buf_AddBytes(buf, len, src);
527 /* append the rest */
528 Buf_AddBytes(buf, strlen(pat), pat);