2014-07-02 21:24:54 +00:00
|
|
|
/* $NetBSD: str.c,v 1.35 2014/02/12 01:35:56 sjg Exp $ */
|
2012-06-08 21:57:36 +00:00
|
|
|
|
1996-10-06 15:57:15 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1988, 1989, 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Adam de Boor.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*-
|
1996-10-06 15:57:15 +00:00
|
|
|
* Copyright (c) 1989 by Berkeley Softworks
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Adam de Boor.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
#ifndef MAKE_NATIVE
|
2014-07-02 21:24:54 +00:00
|
|
|
static char rcsid[] = "$NetBSD: str.c,v 1.35 2014/02/12 01:35:56 sjg Exp $";
|
2012-06-08 21:57:36 +00:00
|
|
|
#else
|
|
|
|
#include <sys/cdefs.h>
|
1996-10-06 15:57:15 +00:00
|
|
|
#ifndef lint
|
2012-06-08 21:57:36 +00:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)str.c 5.8 (Berkeley) 6/1/90";
|
|
|
|
#else
|
2014-07-02 21:24:54 +00:00
|
|
|
__RCSID("$NetBSD: str.c,v 1.35 2014/02/12 01:35:56 sjg Exp $");
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif
|
|
|
|
#endif /* not lint */
|
|
|
|
#endif
|
1996-10-06 15:57:15 +00:00
|
|
|
|
|
|
|
#include "make.h"
|
|
|
|
|
|
|
|
/*-
|
|
|
|
* str_concat --
|
|
|
|
* concatenate the two strings, inserting a space or slash between them,
|
|
|
|
* freeing them if requested.
|
|
|
|
*
|
|
|
|
* returns --
|
|
|
|
* the resulting string in allocated space.
|
|
|
|
*/
|
|
|
|
char *
|
2012-06-08 21:57:36 +00:00
|
|
|
str_concat(const char *s1, const char *s2, int flags)
|
1996-10-06 15:57:15 +00:00
|
|
|
{
|
2012-06-08 21:57:36 +00:00
|
|
|
int len1, len2;
|
|
|
|
char *result;
|
1996-10-06 15:57:15 +00:00
|
|
|
|
|
|
|
/* get the length of both strings */
|
|
|
|
len1 = strlen(s1);
|
|
|
|
len2 = strlen(s2);
|
|
|
|
|
|
|
|
/* allocate length plus separator plus EOS */
|
2012-06-08 21:57:36 +00:00
|
|
|
result = bmake_malloc((u_int)(len1 + len2 + 2));
|
1996-10-06 15:57:15 +00:00
|
|
|
|
|
|
|
/* copy first string into place */
|
|
|
|
memcpy(result, s1, len1);
|
|
|
|
|
|
|
|
/* add separator character */
|
|
|
|
if (flags & STR_ADDSPACE) {
|
|
|
|
result[len1] = ' ';
|
|
|
|
++len1;
|
|
|
|
} else if (flags & STR_ADDSLASH) {
|
|
|
|
result[len1] = '/';
|
|
|
|
++len1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy second string plus EOS into place */
|
|
|
|
memcpy(result + len1, s2, len2 + 1);
|
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
* brk_string --
|
|
|
|
* Fracture a string into an array of words (as delineated by tabs or
|
|
|
|
* spaces) taking quotation marks into account. Leading tabs/spaces
|
|
|
|
* are ignored.
|
|
|
|
*
|
2012-06-08 21:57:36 +00:00
|
|
|
* If expand is TRUE, quotes are removed and escape sequences
|
|
|
|
* such as \r, \t, etc... are expanded.
|
|
|
|
*
|
1996-10-06 15:57:15 +00:00
|
|
|
* returns --
|
2012-06-08 21:57:36 +00:00
|
|
|
* Pointer to the array of pointers to the words.
|
|
|
|
* Memory containing the actual words in *buffer.
|
|
|
|
* Both of these must be free'd by the caller.
|
|
|
|
* Number of words in *store_argc.
|
1996-10-06 15:57:15 +00:00
|
|
|
*/
|
|
|
|
char **
|
2012-06-08 21:57:36 +00:00
|
|
|
brk_string(const char *str, int *store_argc, Boolean expand, char **buffer)
|
1996-10-06 15:57:15 +00:00
|
|
|
{
|
2012-06-08 21:57:36 +00:00
|
|
|
int argc, ch;
|
|
|
|
char inquote, *start, *t;
|
|
|
|
const char *p;
|
1996-10-06 15:57:15 +00:00
|
|
|
int len;
|
2012-06-08 21:57:36 +00:00
|
|
|
int argmax = 50, curlen = 0;
|
2014-07-02 21:24:54 +00:00
|
|
|
char **argv;
|
1996-10-06 15:57:15 +00:00
|
|
|
|
|
|
|
/* skip leading space chars. */
|
|
|
|
for (; *str == ' ' || *str == '\t'; ++str)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* allocate room for a copy of the string */
|
2012-06-08 21:57:36 +00:00
|
|
|
if ((len = strlen(str) + 1) > curlen)
|
|
|
|
*buffer = bmake_malloc(curlen = len);
|
1996-10-06 15:57:15 +00:00
|
|
|
|
2014-07-02 21:24:54 +00:00
|
|
|
/*
|
|
|
|
* initial argmax based on len
|
|
|
|
*/
|
|
|
|
argmax = MAX((len / 5), 50);
|
|
|
|
argv = bmake_malloc((argmax + 1) * sizeof(char *));
|
|
|
|
|
1996-10-06 15:57:15 +00:00
|
|
|
/*
|
|
|
|
* copy the string; at the same time, parse backslashes,
|
|
|
|
* quotes and build the argument list.
|
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
argc = 0;
|
1996-10-06 15:57:15 +00:00
|
|
|
inquote = '\0';
|
2012-06-08 21:57:36 +00:00
|
|
|
for (p = str, start = t = *buffer;; ++p) {
|
1996-10-06 15:57:15 +00:00
|
|
|
switch(ch = *p) {
|
|
|
|
case '"':
|
|
|
|
case '\'':
|
2012-06-08 21:57:36 +00:00
|
|
|
if (inquote) {
|
1996-10-06 15:57:15 +00:00
|
|
|
if (inquote == ch)
|
|
|
|
inquote = '\0';
|
|
|
|
else
|
|
|
|
break;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
1996-10-06 15:57:15 +00:00
|
|
|
else {
|
|
|
|
inquote = (char) ch;
|
|
|
|
/* Don't miss "" or '' */
|
|
|
|
if (start == NULL && p[1] == inquote) {
|
2012-06-08 21:57:36 +00:00
|
|
|
if (!expand) {
|
|
|
|
start = t;
|
|
|
|
*t++ = ch;
|
|
|
|
} else
|
|
|
|
start = t + 1;
|
|
|
|
p++;
|
|
|
|
inquote = '\0';
|
1996-10-06 15:57:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!expand) {
|
|
|
|
if (!start)
|
|
|
|
start = t;
|
|
|
|
*t++ = ch;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
if (inquote)
|
|
|
|
break;
|
|
|
|
if (!start)
|
|
|
|
continue;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case '\0':
|
|
|
|
/*
|
|
|
|
* end of a token -- make sure there's enough argv
|
|
|
|
* space and save off a pointer.
|
|
|
|
*/
|
|
|
|
if (!start)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
*t++ = '\0';
|
|
|
|
if (argc == argmax) {
|
|
|
|
argmax *= 2; /* ramp up fast */
|
2012-06-08 21:57:36 +00:00
|
|
|
argv = (char **)bmake_realloc(argv,
|
|
|
|
(argmax + 1) * sizeof(char *));
|
1996-10-06 15:57:15 +00:00
|
|
|
}
|
|
|
|
argv[argc++] = start;
|
2012-06-08 21:57:36 +00:00
|
|
|
start = NULL;
|
|
|
|
if (ch == '\n' || ch == '\0') {
|
|
|
|
if (expand && inquote) {
|
|
|
|
free(argv);
|
|
|
|
free(*buffer);
|
|
|
|
*buffer = NULL;
|
|
|
|
return NULL;
|
|
|
|
}
|
1996-10-06 15:57:15 +00:00
|
|
|
goto done;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
1996-10-06 15:57:15 +00:00
|
|
|
continue;
|
|
|
|
case '\\':
|
|
|
|
if (!expand) {
|
|
|
|
if (!start)
|
|
|
|
start = t;
|
|
|
|
*t++ = '\\';
|
2012-06-08 21:57:36 +00:00
|
|
|
if (*(p+1) == '\0') /* catch '\' at end of line */
|
|
|
|
continue;
|
1996-10-06 15:57:15 +00:00
|
|
|
ch = *++p;
|
|
|
|
break;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
1996-10-06 15:57:15 +00:00
|
|
|
switch (ch = *++p) {
|
|
|
|
case '\0':
|
|
|
|
case '\n':
|
|
|
|
/* hmmm; fix it up as best we can */
|
|
|
|
ch = '\\';
|
|
|
|
--p;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
ch = '\b';
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
ch = '\f';
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
ch = '\n';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
ch = '\r';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
ch = '\t';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!start)
|
|
|
|
start = t;
|
|
|
|
*t++ = (char) ch;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
done: argv[argc] = NULL;
|
1996-10-06 15:57:15 +00:00
|
|
|
*store_argc = argc;
|
|
|
|
return(argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Str_FindSubstring -- See if a string contains a particular substring.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
|
|
|
* Input:
|
|
|
|
* string String to search.
|
|
|
|
* substring Substring to find in string.
|
|
|
|
*
|
1996-10-06 15:57:15 +00:00
|
|
|
* Results: If string contains substring, the return value is the location of
|
|
|
|
* the first matching instance of substring in string. If string doesn't
|
|
|
|
* contain substring, the return value is NULL. Matching is done on an exact
|
|
|
|
* character-for-character basis with no wildcards or special characters.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
1996-10-06 15:57:15 +00:00
|
|
|
* Side effects: None.
|
|
|
|
*/
|
|
|
|
char *
|
2012-06-08 21:57:36 +00:00
|
|
|
Str_FindSubstring(const char *string, const char *substring)
|
1996-10-06 15:57:15 +00:00
|
|
|
{
|
2012-06-08 21:57:36 +00:00
|
|
|
const char *a, *b;
|
1996-10-06 15:57:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* First scan quickly through the two strings looking for a single-
|
|
|
|
* character match. When it's found, then compare the rest of the
|
|
|
|
* substring.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (b = substring; *string != 0; string += 1) {
|
|
|
|
if (*string != *b)
|
|
|
|
continue;
|
|
|
|
a = string;
|
|
|
|
for (;;) {
|
|
|
|
if (*b == 0)
|
2012-06-08 21:57:36 +00:00
|
|
|
return UNCONST(string);
|
1996-10-06 15:57:15 +00:00
|
|
|
if (*a++ != *b++)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
b = substring;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
return NULL;
|
1996-10-06 15:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Str_Match --
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
1996-10-06 15:57:15 +00:00
|
|
|
* See if a particular string matches a particular pattern.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
1996-10-06 15:57:15 +00:00
|
|
|
* Results: Non-zero is returned if string matches pattern, 0 otherwise. The
|
|
|
|
* matching operation permits the following special characters in the
|
|
|
|
* pattern: *?\[] (see the man page for details on what these mean).
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
|
|
|
* XXX this function does not detect or report malformed patterns.
|
|
|
|
*
|
1996-10-06 15:57:15 +00:00
|
|
|
* Side effects: None.
|
|
|
|
*/
|
|
|
|
int
|
2012-06-08 21:57:36 +00:00
|
|
|
Str_Match(const char *string, const char *pattern)
|
1996-10-06 15:57:15 +00:00
|
|
|
{
|
|
|
|
char c2;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
/*
|
|
|
|
* See if we're at the end of both the pattern and the
|
|
|
|
* string. If, we succeeded. If we're at the end of the
|
|
|
|
* pattern but not at the end of the string, we failed.
|
|
|
|
*/
|
|
|
|
if (*pattern == 0)
|
|
|
|
return(!*string);
|
|
|
|
if (*string == 0 && *pattern != '*')
|
|
|
|
return(0);
|
|
|
|
/*
|
|
|
|
* Check for a "*" as the next pattern character. It matches
|
|
|
|
* any substring. We handle this by calling ourselves
|
|
|
|
* recursively for each postfix of string, until either we
|
|
|
|
* match or we reach the end of the string.
|
|
|
|
*/
|
|
|
|
if (*pattern == '*') {
|
|
|
|
pattern += 1;
|
|
|
|
if (*pattern == 0)
|
|
|
|
return(1);
|
|
|
|
while (*string != 0) {
|
|
|
|
if (Str_Match(string, pattern))
|
|
|
|
return(1);
|
|
|
|
++string;
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Check for a "?" as the next pattern character. It matches
|
|
|
|
* any single character.
|
|
|
|
*/
|
|
|
|
if (*pattern == '?')
|
|
|
|
goto thisCharOK;
|
|
|
|
/*
|
|
|
|
* Check for a "[" as the next pattern character. It is
|
|
|
|
* followed by a list of characters that are acceptable, or
|
|
|
|
* by a range (two characters separated by "-").
|
|
|
|
*/
|
|
|
|
if (*pattern == '[') {
|
|
|
|
++pattern;
|
|
|
|
for (;;) {
|
|
|
|
if ((*pattern == ']') || (*pattern == 0))
|
|
|
|
return(0);
|
|
|
|
if (*pattern == *string)
|
|
|
|
break;
|
|
|
|
if (pattern[1] == '-') {
|
|
|
|
c2 = pattern[2];
|
|
|
|
if (c2 == 0)
|
|
|
|
return(0);
|
|
|
|
if ((*pattern <= *string) &&
|
|
|
|
(c2 >= *string))
|
|
|
|
break;
|
|
|
|
if ((*pattern >= *string) &&
|
|
|
|
(c2 <= *string))
|
|
|
|
break;
|
|
|
|
pattern += 2;
|
|
|
|
}
|
|
|
|
++pattern;
|
|
|
|
}
|
|
|
|
while ((*pattern != ']') && (*pattern != 0))
|
|
|
|
++pattern;
|
|
|
|
goto thisCharOK;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If the next pattern character is '/', just strip off the
|
|
|
|
* '/' so we do exact matching on the character that follows.
|
|
|
|
*/
|
|
|
|
if (*pattern == '\\') {
|
|
|
|
++pattern;
|
|
|
|
if (*pattern == 0)
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* There's no special character. Just make sure that the
|
|
|
|
* next characters of each string match.
|
|
|
|
*/
|
|
|
|
if (*pattern != *string)
|
|
|
|
return(0);
|
|
|
|
thisCharOK: ++pattern;
|
|
|
|
++string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Str_SYSVMatch --
|
2012-06-08 21:57:36 +00:00
|
|
|
* Check word against pattern for a match (% is wild),
|
|
|
|
*
|
|
|
|
* Input:
|
|
|
|
* word Word to examine
|
|
|
|
* pattern Pattern to examine against
|
|
|
|
* len Number of characters to substitute
|
|
|
|
*
|
1996-10-06 15:57:15 +00:00
|
|
|
* Results:
|
|
|
|
* Returns the beginning position of a match or null. The number
|
|
|
|
* of characters matched is returned in len.
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
char *
|
2012-06-08 21:57:36 +00:00
|
|
|
Str_SYSVMatch(const char *word, const char *pattern, int *len)
|
1996-10-06 15:57:15 +00:00
|
|
|
{
|
2012-06-08 21:57:36 +00:00
|
|
|
const char *p = pattern;
|
|
|
|
const char *w = word;
|
|
|
|
const char *m;
|
1996-10-06 15:57:15 +00:00
|
|
|
|
|
|
|
if (*p == '\0') {
|
|
|
|
/* Null pattern is the whole string */
|
|
|
|
*len = strlen(w);
|
2012-06-08 21:57:36 +00:00
|
|
|
return UNCONST(w);
|
1996-10-06 15:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((m = strchr(p, '%')) != NULL) {
|
|
|
|
/* check that the prefix matches */
|
|
|
|
for (; p != m && *w && *w == *p; w++, p++)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (p != m)
|
|
|
|
return NULL; /* No match */
|
|
|
|
|
|
|
|
if (*++p == '\0') {
|
|
|
|
/* No more pattern, return the rest of the string */
|
|
|
|
*len = strlen(w);
|
2012-06-08 21:57:36 +00:00
|
|
|
return UNCONST(w);
|
1996-10-06 15:57:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m = w;
|
|
|
|
|
|
|
|
/* Find a matching tail */
|
|
|
|
do
|
|
|
|
if (strcmp(p, w) == 0) {
|
|
|
|
*len = w - m;
|
2012-06-08 21:57:36 +00:00
|
|
|
return UNCONST(m);
|
1996-10-06 15:57:15 +00:00
|
|
|
}
|
|
|
|
while (*w++ != '\0');
|
2012-06-08 21:57:36 +00:00
|
|
|
|
1996-10-06 15:57:15 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Str_SYSVSubst --
|
|
|
|
* Substitute '%' on the pattern with len characters from src.
|
|
|
|
* If the pattern does not contain a '%' prepend len characters
|
|
|
|
* from src.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
1996-10-06 15:57:15 +00:00
|
|
|
* Results:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* Places result on buf
|
|
|
|
*
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2012-06-08 21:57:36 +00:00
|
|
|
Str_SYSVSubst(Buffer *buf, char *pat, char *src, int len)
|
1996-10-06 15:57:15 +00:00
|
|
|
{
|
|
|
|
char *m;
|
|
|
|
|
|
|
|
if ((m = strchr(pat, '%')) != NULL) {
|
|
|
|
/* Copy the prefix */
|
2012-06-08 21:57:36 +00:00
|
|
|
Buf_AddBytes(buf, m - pat, pat);
|
1996-10-06 15:57:15 +00:00
|
|
|
/* skip the % */
|
|
|
|
pat = m + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the pattern */
|
2012-06-08 21:57:36 +00:00
|
|
|
Buf_AddBytes(buf, len, src);
|
1996-10-06 15:57:15 +00:00
|
|
|
|
|
|
|
/* append the rest */
|
2012-06-08 21:57:36 +00:00
|
|
|
Buf_AddBytes(buf, strlen(pat), pat);
|
1996-10-06 15:57:15 +00:00
|
|
|
}
|