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
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
46 static char **argv, *buffer;
47 static int argmax, curlen;
51 * Initialize the strings package
59 argv = emalloc(((argmax = 50) + 1) * sizeof(char *));
60 argv[0] = Var_Value(".MAKE", VAR_GLOBAL, &p1);
66 * Cleanup the strings package
83 * concatenate the two strings, inserting a space or slash between them,
84 * freeing them if requested.
87 * the resulting string in allocated space.
90 str_concat(char *s1, char *s2, int flags)
95 /* get the length of both strings */
99 /* allocate length plus separator plus EOS */
100 result = emalloc(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) {
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(char *str, int *store_argc, Boolean expand)
139 char inquote, *p, *start, *t;
142 /* skip leading space chars. */
143 for (; *str == ' ' || *str == '\t'; ++str)
146 /* allocate room for a copy of the string */
147 if ((len = strlen(str) + 1) > curlen) {
150 buffer = emalloc(curlen = len);
154 * copy the string; at the same time, parse backslashes,
155 * quotes and build the argument list.
159 for (p = str, start = t = buffer;; ++p) {
167 /* Don't miss "" or '' */
185 * end of a token -- make sure there's enough argv
186 * space and save off a pointer.
192 if (argc == argmax) {
193 argmax *= 2; /* ramp up fast */
194 argv = erealloc(argv,
195 (argmax + 1) * sizeof(char *));
197 argv[argc++] = start;
199 if (ch == '\n' || ch == '\0')
214 /* hmmm; fix it up as best we can */
244 done: argv[argc] = NULL;
252 * See if a particular string matches a particular pattern.
254 * Results: Non-zero is returned if string matches pattern, 0 otherwise. The
255 * matching operation permits the following special characters in the
256 * pattern: *?\[] (see the man page for details on what these mean).
258 * Side effects: None.
261 Str_Match(const char *string, const char *pattern)
267 * See if we're at the end of both the pattern and the
268 * string. If, we succeeded. If we're at the end of the
269 * pattern but not at the end of the string, we failed.
273 if (*string == 0 && *pattern != '*')
276 * Check for a "*" as the next pattern character. It matches
277 * any substring. We handle this by calling ourselves
278 * recursively for each postfix of string, until either we
279 * match or we reach the end of the string.
281 if (*pattern == '*') {
285 while (*string != 0) {
286 if (Str_Match(string, pattern))
293 * Check for a "?" as the next pattern character. It matches
294 * any single character.
299 * Check for a "[" as the next pattern character. It is
300 * followed by a list of characters that are acceptable, or
301 * by a range (two characters separated by "-").
303 if (*pattern == '[') {
306 if ((*pattern == ']') || (*pattern == 0))
308 if (*pattern == *string)
310 if (pattern[1] == '-') {
314 if ((*pattern <= *string) &&
317 if ((*pattern >= *string) &&
324 while ((*pattern != ']') && (*pattern != 0))
329 * If the next pattern character is '/', just strip off the
330 * '/' so we do exact matching on the character that follows.
332 if (*pattern == '\\') {
338 * There's no special character. Just make sure that the
339 * next characters of each string match.
341 if (*pattern != *string)
343 thisCharOK: ++pattern;
350 *-----------------------------------------------------------------------
352 * Check word against pattern for a match (% is wild),
355 * Returns the beginning position of a match or null. The number
356 * of characters matched is returned in len.
361 *-----------------------------------------------------------------------
364 Str_SYSVMatch(const char *word, const char *pattern, int *len)
366 const char *m, *p, *w;
372 /* Zero-length word cannot be matched against */
378 /* Null pattern is the whole string */
383 if ((m = strchr(p, '%')) != NULL) {
384 /* check that the prefix matches */
385 for (; p != m && *w && *w == *p; w++, p++)
389 return (NULL); /* No match */
392 /* No more pattern, return the rest of the string */
400 /* Find a matching tail */
402 if (strcmp(p, w) == 0) {
406 while (*w++ != '\0');
413 *-----------------------------------------------------------------------
415 * Substitute '%' on the pattern with len characters from src.
416 * If the pattern does not contain a '%' prepend len characters
423 * Places result on buf
425 *-----------------------------------------------------------------------
428 Str_SYSVSubst(Buffer buf, const char *pat, const char *src, int len)
432 if ((m = strchr(pat, '%')) != NULL) {
433 /* Copy the prefix */
434 Buf_AddBytes(buf, m - pat, (Byte *)pat);
439 /* Copy the pattern */
440 Buf_AddBytes(buf, len, (Byte *)src);
442 /* append the rest */
443 Buf_AddBytes(buf, strlen(pat), (Byte *)pat);