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.20 2005/01/26 09:44:21 joerg Exp $
53 static char **argv, *buffer;
54 static int argmax, curlen;
58 * Initialize the strings package
66 argv = emalloc(((argmax = 50) + 1) * sizeof(char *));
67 argv[0] = Var_Value(".MAKE", VAR_GLOBAL, &p1);
72 * concatenate the two strings, inserting a space or slash between them.
75 * the resulting string in allocated space.
78 str_concat(const char *s1, const char *s2, int flags)
83 /* get the length of both strings */
87 /* allocate length plus separator plus EOS */
88 result = emalloc(len1 + len2 + 2);
90 /* copy first string into place */
91 memcpy(result, s1, len1);
93 /* add separator character */
94 if (flags & STR_ADDSPACE) {
97 } else if (flags & STR_ADDSLASH) {
102 /* copy second string plus EOS into place */
103 memcpy(result + len1, s2, len2 + 1);
110 * Fracture a string into an array of words (as delineated by tabs or
111 * spaces) taking quotation marks into account. Leading tabs/spaces
115 * Pointer to the array of pointers to the words. To make life easier,
116 * the first word is always the value of the .MAKE variable.
119 brk_string(char *str, int *store_argc, Boolean expand)
122 char inquote, *p, *start, *t;
125 /* skip leading space chars. */
126 for (; *str == ' ' || *str == '\t'; ++str)
129 /* allocate room for a copy of the string */
130 if ((len = strlen(str) + 1) > curlen) {
133 buffer = emalloc(curlen = len);
137 * copy the string; at the same time, parse backslashes,
138 * quotes and build the argument list.
142 for (p = str, start = t = buffer;; ++p) {
150 /* Don't miss "" or '' */
168 * end of a token -- make sure there's enough argv
169 * space and save off a pointer.
175 if (argc == argmax) {
176 argmax *= 2; /* ramp up fast */
177 argv = erealloc(argv,
178 (argmax + 1) * sizeof(char *));
180 argv[argc++] = start;
182 if (ch == '\n' || ch == '\0')
197 /* hmmm; fix it up as best we can */
227 done: argv[argc] = NULL;
233 * Quote a string for appending it to MAKEFLAGS. According to Posix the
234 * kind of quoting here is implementation-defined. This quoting must ensure
235 * that the parsing of MAKEFLAGS's contents in a sub-shell yields the same
236 * options, option arguments and macro definitions as in the calling make.
237 * We simply quote all blanks, which according to Posix are space and tab
238 * in the POSIX locale. Don't use isblank because in that case makes with
239 * different locale settings could not communicate. We must also quote
240 * backslashes obviously.
243 MAKEFLAGS_quote(const char *str)
248 /* assume worst case - everything has to be quoted */
249 ret = emalloc(strlen(str) * 2 + 1);
271 MAKEFLAGS_break(const char *str, int *pargc)
276 /* allocate room for a copy of the string */
277 if ((len = strlen(str) + 1) > curlen)
278 buffer = erealloc(buffer, curlen = len);
300 if (argmax == *pargc) {
302 argv = erealloc(argv,
303 sizeof(*argv) * (argmax + 1));
305 argv[(*pargc)++] = start;
313 if (str[1] == ' ' || str[1] == '\t')
322 /* start of new word */
327 argv[(*pargc)] = NULL;
334 * See if a particular string matches a particular pattern.
336 * Results: Non-zero is returned if string matches pattern, 0 otherwise. The
337 * matching operation permits the following special characters in the
338 * pattern: *?\[] (see the man page for details on what these mean).
340 * Side effects: None.
343 Str_Match(const char *string, const char *pattern)
349 * See if we're at the end of both the pattern and the
350 * string. If, we succeeded. If we're at the end of the
351 * pattern but not at the end of the string, we failed.
355 if (*string == 0 && *pattern != '*')
358 * Check for a "*" as the next pattern character. It matches
359 * any substring. We handle this by calling ourselves
360 * recursively for each postfix of string, until either we
361 * match or we reach the end of the string.
363 if (*pattern == '*') {
367 while (*string != 0) {
368 if (Str_Match(string, pattern))
375 * Check for a "?" as the next pattern character. It matches
376 * any single character.
381 * Check for a "[" as the next pattern character. It is
382 * followed by a list of characters that are acceptable, or
383 * by a range (two characters separated by "-").
385 if (*pattern == '[') {
388 if ((*pattern == ']') || (*pattern == 0))
390 if (*pattern == *string)
392 if (pattern[1] == '-') {
396 if ((*pattern <= *string) &&
399 if ((*pattern >= *string) &&
406 while ((*pattern != ']') && (*pattern != 0))
411 * If the next pattern character is '/', just strip off the
412 * '/' so we do exact matching on the character that follows.
414 if (*pattern == '\\') {
420 * There's no special character. Just make sure that the
421 * next characters of each string match.
423 if (*pattern != *string)
425 thisCharOK: ++pattern;
432 *-----------------------------------------------------------------------
434 * Check word against pattern for a match (% is wild),
437 * Returns the beginning position of a match or null. The number
438 * of characters matched is returned in len.
443 *-----------------------------------------------------------------------
446 Str_SYSVMatch(const char *word, const char *pattern, int *len)
448 const char *p = pattern;
449 const char *w = word;
453 /* Zero-length word cannot be matched against */
459 /* Null pattern is the whole string */
464 if ((m = strchr(p, '%')) != NULL) {
465 /* check that the prefix matches */
466 for (; p != m && *w && *w == *p; w++, p++)
470 return (NULL); /* No match */
473 /* No more pattern, return the rest of the string */
481 /* Find a matching tail */
483 if (strcmp(p, w) == 0) {
487 while (*w++ != '\0');
494 *-----------------------------------------------------------------------
496 * Substitute '%' on the pattern with len characters from src.
497 * If the pattern does not contain a '%' prepend len characters
504 * Places result on buf
506 *-----------------------------------------------------------------------
509 Str_SYSVSubst(Buffer *buf, const char *pat, const char *src, int len)
513 if ((m = strchr(pat, '%')) != NULL) {
514 /* Copy the prefix */
515 Buf_AddBytes(buf, m - pat, (const Byte *)pat);
520 /* Copy the pattern */
521 Buf_AddBytes(buf, len, (const Byte *)src);
523 /* append the rest */
524 Buf_AddBytes(buf, strlen(pat), (const Byte *)pat);