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$");
54 * Initialize the argument array object. The array is initially
55 * eight positions, and will be expaned as neccessary. The first
56 * position is set to NULL since everything ignores it. We allocate
57 * (size + 1) since we need space for the terminating NULL. The
58 * buffer is set to NULL, since no common buffer is alloated yet.
61 ArgArray_Init(ArgArray *aa)
65 aa->argv = emalloc((aa->size + 1) * sizeof(char *));
67 aa->argv[aa->argc++] = NULL;
73 * Cleanup the memory allocated for in the argument array object.
76 ArgArray_Done(ArgArray *aa)
79 if (aa->buffer == NULL) {
81 /* args are individually allocated */
82 for (i = 0; i < aa->argc; ++i) {
89 /* args are part of a single allocation */
101 * concatenate the two strings, inserting a space or slash between them.
104 * the resulting string in allocated space.
107 str_concat(const char *s1, const char *s2, int flags)
112 /* get the length of both strings */
116 /* allocate length plus separator plus EOS */
117 result = emalloc(len1 + len2 + 2);
119 /* copy first string into place */
120 memcpy(result, s1, len1);
122 /* add separator character */
123 if (flags & STR_ADDSPACE) {
126 } else if (flags & STR_ADDSLASH) {
131 /* copy second string plus EOS into place */
132 memcpy(result + len1, s2, len2 + 1);
138 * Fracture a string into an array of words (as delineated by tabs or
139 * spaces) taking quotation marks into account. Leading tabs/spaces
143 brk_string(ArgArray *aa, const char str[], Boolean expand)
149 /* skip leading space chars. */
150 for (; *str == ' ' || *str == '\t'; ++str)
155 aa->buffer = estrdup(str);;
162 * copy the string; at the same time, parse backslashes,
163 * quotes and build the argument list.
169 if (inquote == '\0') {
175 } else if (inquote == str[0]) {
177 /* Don't miss "" or '' */
183 /* other type of quote found */
203 * end of a token -- make sure there's enough argv
204 * space and save off a pointer.
206 if (aa->argc == aa->size) {
207 aa->size *= 2; /* ramp up fast */
208 aa->argv = erealloc(aa->argv,
209 (aa->size + 1) * sizeof(char *));
214 aa->argv[aa->argc] = start;
217 if (str[0] == '\n' || str[0] == '\0') {
218 aa->argv[aa->argc++] = start;
219 aa->argv[aa->argc] = NULL;
222 aa->argv[aa->argc++] = start;
233 /* hmmm; fix it up as best we can */
263 if (str[1] != '\0') {
280 * Quote a string for appending it to MAKEFLAGS. According to Posix the
281 * kind of quoting here is implementation-defined. This quoting must ensure
282 * that the parsing of MAKEFLAGS's contents in a sub-shell yields the same
283 * options, option arguments and macro definitions as in the calling make.
284 * We simply quote all blanks, which according to Posix are space and tab
285 * in the POSIX locale. Don't use isblank because in that case makes with
286 * different locale settings could not communicate. We must also quote
287 * backslashes obviously.
290 MAKEFLAGS_quote(const char *str)
295 /* assume worst case - everything has to be quoted */
296 ret = emalloc(strlen(str) * 2 + 1);
318 MAKEFLAGS_break(ArgArray *aa, const char str[])
325 aa->buffer = strdup(str);
342 if (aa->argc == aa->size) {
344 aa->argv = erealloc(aa->argv,
345 (aa->size + 1) * sizeof(char *));
350 aa->argv[aa->argc] = start;
353 if (str[0] == '\0') {
354 aa->argv[aa->argc++] = start;
355 aa->argv[aa->argc] = NULL;
358 aa->argv[aa->argc++] = start;
365 if (str[1] == ' ' || str[1] == '\t')
381 * See if a particular string matches a particular pattern.
383 * Results: Non-zero is returned if string matches pattern, 0 otherwise. The
384 * matching operation permits the following special characters in the
385 * pattern: *?\[] (see the man page for details on what these mean).
387 * Side effects: None.
390 Str_Match(const char *string, const char *pattern)
396 * See if we're at the end of both the pattern and the
397 * string. If, we succeeded. If we're at the end of the
398 * pattern but not at the end of the string, we failed.
402 if (*string == 0 && *pattern != '*')
405 * Check for a "*" as the next pattern character. It matches
406 * any substring. We handle this by calling ourselves
407 * recursively for each postfix of string, until either we
408 * match or we reach the end of the string.
410 if (*pattern == '*') {
414 while (*string != 0) {
415 if (Str_Match(string, pattern))
422 * Check for a "?" as the next pattern character. It matches
423 * any single character.
428 * Check for a "[" as the next pattern character. It is
429 * followed by a list of characters that are acceptable, or
430 * by a range (two characters separated by "-").
432 if (*pattern == '[') {
435 if ((*pattern == ']') || (*pattern == 0))
437 if (*pattern == *string)
439 if (pattern[1] == '-') {
443 if ((*pattern <= *string) &&
446 if ((*pattern >= *string) &&
453 while ((*pattern != ']') && (*pattern != 0))
458 * If the next pattern character is '/', just strip off the
459 * '/' so we do exact matching on the character that follows.
461 if (*pattern == '\\') {
467 * There's no special character. Just make sure that the
468 * next characters of each string match.
470 if (*pattern != *string)
472 thisCharOK: ++pattern;
480 * Check word against pattern for a match (% is wild),
483 * Returns the beginning position of a match or null. The number
484 * of characters matched is returned in len.
487 Str_SYSVMatch(const char *word, const char *pattern, int *len)
489 const char *m, *p, *w;
495 /* Zero-length word cannot be matched against */
501 /* Null pattern is the whole string */
506 if ((m = strchr(p, '%')) != NULL) {
507 /* check that the prefix matches */
508 for (; p != m && *w && *w == *p; w++, p++)
512 return (NULL); /* No match */
515 /* No more pattern, return the rest of the string */
523 /* Find a matching tail */
525 if (strcmp(p, w) == 0) {
529 while (*w++ != '\0');
537 * Substitute '%' on the pattern with len characters from src.
538 * If the pattern does not contain a '%' prepend len characters
542 * Places result on buf
545 Str_SYSVSubst(Buffer *buf, const char *pat, const char *src, int len)
549 if ((m = strchr(pat, '%')) != NULL) {
550 /* Copy the prefix */
551 Buf_AppendRange(buf, pat, m);
556 /* Copy the pattern */
557 Buf_AddBytes(buf, len, (const Byte *)src);
559 /* append the rest */
560 Buf_Append(buf, pat);