2 * Copyright (c) 1988, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1989 by Berkeley Softworks
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 static char sccsid[] = "@(#)str.c 5.8 (Berkeley) 6/1/90";
43 static const char rcsid[] =
44 "$FreeBSD: src/usr.bin/make/str.c,v 1.12.2.1 2002/06/17 04:30:48 jmallett Exp $";
50 static char **argv, *buffer;
51 static int argmax, curlen;
55 * Initialize the strings package
62 argv = (char **)emalloc(((argmax = 50) + 1) * sizeof(char *));
63 argv[0] = Var_Value(".MAKE", VAR_GLOBAL, &p1);
69 * Cleanup the strings package
86 * concatenate the two strings, inserting a space or slash between them,
87 * freeing them if requested.
90 * the resulting string in allocated space.
93 str_concat(s1, s2, flags)
97 register int len1, len2;
98 register char *result;
100 /* get the length of both strings */
104 /* allocate length plus separator plus EOS */
105 result = emalloc((u_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);
122 /* free original strings */
123 if (flags & STR_DOFREE) {
132 * Fracture a string into an array of words (as delineated by tabs or
133 * spaces) taking quotation marks into account. Leading tabs/spaces
137 * Pointer to the array of pointers to the words. To make life easier,
138 * the first word is always the value of the .MAKE variable.
141 brk_string(str, store_argc, expand)
146 register int argc, ch;
147 register char inquote, *p, *start, *t;
150 /* skip leading space chars. */
151 for (; *str == ' ' || *str == '\t'; ++str)
154 /* allocate room for a copy of the string */
155 if ((len = strlen(str) + 1) > curlen) {
158 buffer = emalloc(curlen = len);
162 * copy the string; at the same time, parse backslashes,
163 * quotes and build the argument list.
167 for (p = str, start = t = buffer;; ++p) {
178 /* Don't miss "" or '' */
179 if (start == NULL && p[1] == inquote) {
200 * end of a token -- make sure there's enough argv
201 * space and save off a pointer.
207 if (argc == argmax) {
208 argmax *= 2; /* ramp up fast */
209 argv = (char **)erealloc(argv,
210 (argmax + 1) * sizeof(char *));
212 argv[argc++] = start;
213 start = (char *)NULL;
214 if (ch == '\n' || ch == '\0')
229 /* hmmm; fix it up as best we can */
255 done: argv[argc] = (char *)NULL;
261 * Str_FindSubstring -- See if a string contains a particular substring.
263 * Results: If string contains substring, the return value is the location of
264 * the first matching instance of substring in string. If string doesn't
265 * contain substring, the return value is NULL. Matching is done on an exact
266 * character-for-character basis with no wildcards or special characters.
268 * Side effects: None.
271 Str_FindSubstring(string, substring)
272 register char *string; /* String to search. */
273 char *substring; /* Substring to find in string */
275 register char *a, *b;
278 * First scan quickly through the two strings looking for a single-
279 * character match. When it's found, then compare the rest of the
283 for (b = substring; *string != 0; string += 1) {
295 return((char *) NULL);
301 * See if a particular string matches a particular pattern.
303 * Results: Non-zero is returned if string matches pattern, 0 otherwise. The
304 * matching operation permits the following special characters in the
305 * pattern: *?\[] (see the man page for details on what these mean).
307 * Side effects: None.
310 Str_Match(string, pattern)
311 register char *string; /* String */
312 register char *pattern; /* Pattern */
318 * See if we're at the end of both the pattern and the
319 * string. If, we succeeded. If we're at the end of the
320 * pattern but not at the end of the string, we failed.
324 if (*string == 0 && *pattern != '*')
327 * Check for a "*" as the next pattern character. It matches
328 * any substring. We handle this by calling ourselves
329 * recursively for each postfix of string, until either we
330 * match or we reach the end of the string.
332 if (*pattern == '*') {
336 while (*string != 0) {
337 if (Str_Match(string, pattern))
344 * Check for a "?" as the next pattern character. It matches
345 * any single character.
350 * Check for a "[" as the next pattern character. It is
351 * followed by a list of characters that are acceptable, or
352 * by a range (two characters separated by "-").
354 if (*pattern == '[') {
357 if ((*pattern == ']') || (*pattern == 0))
359 if (*pattern == *string)
361 if (pattern[1] == '-') {
365 if ((*pattern <= *string) &&
368 if ((*pattern >= *string) &&
375 while ((*pattern != ']') && (*pattern != 0))
380 * If the next pattern character is '/', just strip off the
381 * '/' so we do exact matching on the character that follows.
383 if (*pattern == '\\') {
389 * There's no special character. Just make sure that the
390 * next characters of each string match.
392 if (*pattern != *string)
394 thisCharOK: ++pattern;
401 *-----------------------------------------------------------------------
403 * Check word against pattern for a match (% is wild),
406 * Returns the beginning position of a match or null. The number
407 * of characters matched is returned in len.
412 *-----------------------------------------------------------------------
415 Str_SYSVMatch(word, pattern, len)
416 char *word; /* Word to examine */
417 char *pattern; /* Pattern to examine against */
418 int *len; /* Number of characters to substitute */
425 /* Zero-length word cannot be matched against */
431 /* Null pattern is the whole string */
436 if ((m = strchr(p, '%')) != NULL) {
437 /* check that the prefix matches */
438 for (; p != m && *w && *w == *p; w++, p++)
442 return NULL; /* No match */
445 /* No more pattern, return the rest of the string */
453 /* Find a matching tail */
455 if (strcmp(p, w) == 0) {
459 while (*w++ != '\0');
466 *-----------------------------------------------------------------------
468 * Substitute '%' on the pattern with len characters from src.
469 * If the pattern does not contain a '%' prepend len characters
476 * Places result on buf
478 *-----------------------------------------------------------------------
481 Str_SYSVSubst(buf, pat, src, len)
489 if ((m = strchr(pat, '%')) != NULL) {
490 /* Copy the prefix */
491 Buf_AddBytes(buf, m - pat, (Byte *) pat);
496 /* Copy the pattern */
497 Buf_AddBytes(buf, len, (Byte *) src);
499 /* append the rest */
500 Buf_AddBytes(buf, strlen(pat), (Byte *) pat);