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
38 * @(#)str.c 5.8 (Berkeley) 6/1/90
39 * $FreeBSD: src/usr.bin/make/str.c,v 1.12.2.2 2004/02/23 12:10:57 ru Exp $
40 * $DragonFly: src/usr.bin/make/str.c,v 1.5 2004/11/12 22:02:51 dillon Exp $
45 static char **argv, *buffer;
46 static int argmax, curlen;
50 * Initialize the strings package
57 argv = (char **)emalloc(((argmax = 50) + 1) * sizeof(char *));
58 argv[0] = Var_Value(".MAKE", VAR_GLOBAL, &p1);
64 * Cleanup the strings package
81 * concatenate the two strings, inserting a space or slash between them,
82 * freeing them if requested.
85 * the resulting string in allocated space.
88 str_concat(s1, s2, flags)
95 /* get the length of both strings */
99 /* allocate length plus separator plus EOS */
100 result = emalloc((u_int)(len1 + len2 + 2));
102 /* copy first string into place */
103 memcpy(result, s1, len1);
105 /* add separator character */
106 if (flags & STR_ADDSPACE) {
109 } else if (flags & STR_ADDSLASH) {
114 /* copy second string plus EOS into place */
115 memcpy(result + len1, s2, len2 + 1);
117 /* free original strings */
118 if (flags & STR_DOFREE) {
119 free(__DECONST(void *, s1));
120 free(__DECONST(void *, s2));
127 * Fracture a string into an array of words (as delineated by tabs or
128 * spaces) taking quotation marks into account. Leading tabs/spaces
132 * Pointer to the array of pointers to the words. To make life easier,
133 * the first word is always the value of the .MAKE variable.
136 brk_string(str, store_argc, expand)
142 char inquote, *p, *start, *t;
145 /* skip leading space chars. */
146 for (; *str == ' ' || *str == '\t'; ++str)
149 /* allocate room for a copy of the string */
150 if ((len = strlen(str) + 1) > curlen) {
153 buffer = emalloc(curlen = len);
157 * copy the string; at the same time, parse backslashes,
158 * quotes and build the argument list.
162 for (p = str, start = t = buffer;; ++p) {
170 /* Don't miss "" or '' */
188 * end of a token -- make sure there's enough argv
189 * space and save off a pointer.
195 if (argc == argmax) {
196 argmax *= 2; /* ramp up fast */
197 argv = (char **)erealloc(argv,
198 (argmax + 1) * sizeof(char *));
200 argv[argc++] = start;
201 start = (char *)NULL;
202 if (ch == '\n' || ch == '\0')
217 /* hmmm; fix it up as best we can */
243 done: argv[argc] = (char *)NULL;
249 * Str_FindSubstring -- See if a string contains a particular substring.
251 * Results: If string contains substring, the return value is the location of
252 * the first matching instance of substring in string. If string doesn't
253 * contain substring, the return value is NULL. Matching is done on an exact
254 * character-for-character basis with no wildcards or special characters.
256 * Side effects: None.
259 Str_FindSubstring(string, substring)
260 char *string; /* String to search. */
261 char *substring; /* Substring to find in string */
266 * First scan quickly through the two strings looking for a single-
267 * character match. When it's found, then compare the rest of the
271 for (b = substring; *string != 0; string += 1) {
283 return((char *) NULL);
289 * See if a particular string matches a particular pattern.
291 * Results: Non-zero is returned if string matches pattern, 0 otherwise. The
292 * matching operation permits the following special characters in the
293 * pattern: *?\[] (see the man page for details on what these mean).
295 * Side effects: None.
298 Str_Match(string, pattern)
299 char *string; /* String */
300 char *pattern; /* Pattern */
306 * See if we're at the end of both the pattern and the
307 * string. If, we succeeded. If we're at the end of the
308 * pattern but not at the end of the string, we failed.
312 if (*string == 0 && *pattern != '*')
315 * Check for a "*" as the next pattern character. It matches
316 * any substring. We handle this by calling ourselves
317 * recursively for each postfix of string, until either we
318 * match or we reach the end of the string.
320 if (*pattern == '*') {
324 while (*string != 0) {
325 if (Str_Match(string, pattern))
332 * Check for a "?" as the next pattern character. It matches
333 * any single character.
338 * Check for a "[" as the next pattern character. It is
339 * followed by a list of characters that are acceptable, or
340 * by a range (two characters separated by "-").
342 if (*pattern == '[') {
345 if ((*pattern == ']') || (*pattern == 0))
347 if (*pattern == *string)
349 if (pattern[1] == '-') {
353 if ((*pattern <= *string) &&
356 if ((*pattern >= *string) &&
363 while ((*pattern != ']') && (*pattern != 0))
368 * If the next pattern character is '/', just strip off the
369 * '/' so we do exact matching on the character that follows.
371 if (*pattern == '\\') {
377 * There's no special character. Just make sure that the
378 * next characters of each string match.
380 if (*pattern != *string)
382 thisCharOK: ++pattern;
389 *-----------------------------------------------------------------------
391 * Check word against pattern for a match (% is wild),
394 * Returns the beginning position of a match or null. The number
395 * of characters matched is returned in len.
400 *-----------------------------------------------------------------------
403 Str_SYSVMatch(word, pattern, len)
404 char *word; /* Word to examine */
405 char *pattern; /* Pattern to examine against */
406 int *len; /* Number of characters to substitute */
413 /* Zero-length word cannot be matched against */
419 /* Null pattern is the whole string */
424 if ((m = strchr(p, '%')) != NULL) {
425 /* check that the prefix matches */
426 for (; p != m && *w && *w == *p; w++, p++)
430 return NULL; /* No match */
433 /* No more pattern, return the rest of the string */
441 /* Find a matching tail */
443 if (strcmp(p, w) == 0) {
447 while (*w++ != '\0');
454 *-----------------------------------------------------------------------
456 * Substitute '%' on the pattern with len characters from src.
457 * If the pattern does not contain a '%' prepend len characters
464 * Places result on buf
466 *-----------------------------------------------------------------------
469 Str_SYSVSubst(buf, pat, src, len)
477 if ((m = strchr(pat, '%')) != NULL) {
478 /* Copy the prefix */
479 Buf_AddBytes(buf, m - pat, (Byte *) pat);
484 /* Copy the pattern */
485 Buf_AddBytes(buf, len, (Byte *) src);
487 /* append the rest */
488 Buf_AddBytes(buf, strlen(pat), (Byte *) pat);