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$");
52 * Initialize the argument array object. The array is initially
53 * eight positions, and will be expanded as necessary. The first
54 * position is set to NULL since everything ignores it. We allocate
55 * (size + 1) since we need space for the terminating NULL. The
56 * buffer is set to NULL, since no common buffer is allocated yet.
59 ArgArray_Init(ArgArray *aa)
63 aa->argv = emalloc((aa->size + 1) * sizeof(char *));
65 aa->argv[aa->argc++] = NULL;
71 * Cleanup the memory allocated for in the argument array object.
74 ArgArray_Done(ArgArray *aa)
77 if (aa->buffer == NULL) {
79 /* args are individually allocated */
80 for (i = 0; i < aa->argc; ++i) {
87 /* args are part of a single allocation */
99 * concatenate the two strings, inserting a space or slash between them.
102 * the resulting string in allocated space.
105 str_concat(const char *s1, const char *s2, int flags)
110 /* get the length of both strings */
114 /* allocate length plus separator plus EOS */
115 result = emalloc(len1 + len2 + 2);
117 /* copy first string into place */
118 memcpy(result, s1, len1);
120 /* add separator character */
121 if (flags & STR_ADDSPACE) {
124 } else if (flags & STR_ADDSLASH) {
129 /* copy second string plus EOS into place */
130 memcpy(result + len1, s2, len2 + 1);
136 * Fracture a string into an array of words (as delineated by tabs or
137 * spaces) taking quotation marks into account. Leading tabs/spaces
141 brk_string(ArgArray *aa, const char str[], Boolean expand)
147 /* skip leading space chars. */
148 for (; *str == ' ' || *str == '\t'; ++str)
153 aa->buffer = estrdup(str);
160 * copy the string; at the same time, parse backslashes,
161 * quotes and build the argument list.
167 if (inquote == '\0') {
173 } else if (inquote == str[0]) {
175 /* Don't miss "" or '' */
181 /* other type of quote found */
201 * end of a token -- make sure there's enough argv
202 * space and save off a pointer.
204 if (aa->argc == aa->size) {
205 aa->size *= 2; /* ramp up fast */
206 aa->argv = erealloc(aa->argv,
207 (aa->size + 1) * sizeof(char *));
212 aa->argv[aa->argc] = start;
215 if (str[0] == '\n' || str[0] == '\0') {
216 aa->argv[aa->argc++] = start;
217 aa->argv[aa->argc] = NULL;
220 aa->argv[aa->argc++] = start;
231 /* hmmm; fix it up as best we can */
261 if (str[1] != '\0') {
278 * Quote a string for appending it to MAKEFLAGS. According to Posix the
279 * kind of quoting here is implementation-defined. This quoting must ensure
280 * that the parsing of MAKEFLAGS's contents in a sub-shell yields the same
281 * options, option arguments and macro definitions as in the calling make.
282 * We simply quote all blanks, which according to Posix are space and tab
283 * in the POSIX locale. Don't use isblank because in that case makes with
284 * different locale settings could not communicate. We must also quote
285 * backslashes obviously.
288 MAKEFLAGS_quote(const char *str)
293 /* assume worst case - everything has to be quoted */
294 ret = emalloc(strlen(str) * 2 + 1);
316 MAKEFLAGS_break(ArgArray *aa, const char str[])
323 aa->buffer = strdup(str);
340 if (aa->argc == aa->size) {
342 aa->argv = erealloc(aa->argv,
343 (aa->size + 1) * sizeof(char *));
348 aa->argv[aa->argc] = start;
351 if (str[0] == '\0') {
352 aa->argv[aa->argc++] = start;
353 aa->argv[aa->argc] = NULL;
356 aa->argv[aa->argc++] = start;
363 if (str[1] == ' ' || str[1] == '\t')
379 * See if a particular string matches a particular pattern.
381 * Results: Non-zero is returned if string matches pattern, 0 otherwise. The
382 * matching operation permits the following special characters in the
383 * pattern: *?\[] (see the man page for details on what these mean).
385 * Side effects: None.
388 Str_Match(const char *string, const char *pattern)
394 * See if we're at the end of both the pattern and the
395 * string. If, we succeeded. If we're at the end of the
396 * pattern but not at the end of the string, we failed.
400 if (*string == 0 && *pattern != '*')
403 * Check for a "*" as the next pattern character. It matches
404 * any substring. We handle this by calling ourselves
405 * recursively for each postfix of string, until either we
406 * match or we reach the end of the string.
408 if (*pattern == '*') {
412 while (*string != 0) {
413 if (Str_Match(string, pattern))
420 * Check for a "?" as the next pattern character. It matches
421 * any single character.
426 * Check for a "[" as the next pattern character. It is
427 * followed by a list of characters that are acceptable, or
428 * by a range (two characters separated by "-").
430 if (*pattern == '[') {
433 if ((*pattern == ']') || (*pattern == 0))
435 if (*pattern == *string)
437 if (pattern[1] == '-') {
441 if ((*pattern <= *string) &&
444 if ((*pattern >= *string) &&
451 while ((*pattern != ']') && (*pattern != 0))
456 * If the next pattern character is '/', just strip off the
457 * '/' so we do exact matching on the character that follows.
459 if (*pattern == '\\') {
465 * There's no special character. Just make sure that the
466 * next characters of each string match.
468 if (*pattern != *string)
470 thisCharOK: ++pattern;
478 * Check word against pattern for a match (% is wild),
481 * Returns the beginning position of a match or null. The number
482 * of characters matched is returned in len.
485 Str_SYSVMatch(const char *word, const char *pattern, int *len)
487 const char *m, *p, *w;
493 /* Zero-length word cannot be matched against */
499 /* Null pattern is the whole string */
504 if ((m = strchr(p, '%')) != NULL) {
505 /* check that the prefix matches */
506 for (; p != m && *w && *w == *p; w++, p++)
510 return (NULL); /* No match */
513 /* No more pattern, return the rest of the string */
521 /* Find a matching tail */
523 if (strcmp(p, w) == 0) {
527 while (*w++ != '\0');
535 * Substitute '%' on the pattern with len characters from src.
536 * If the pattern does not contain a '%' prepend len characters
540 * Places result on buf
543 Str_SYSVSubst(Buffer *buf, const char *pat, const char *src, int len)
547 if ((m = strchr(pat, '%')) != NULL) {
548 /* Copy the prefix */
549 Buf_AppendRange(buf, pat, m);
554 /* Copy the pattern */
555 Buf_AddBytes(buf, len, (const Byte *)src);
557 /* append the rest */
558 Buf_Append(buf, pat);