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.40 2005/02/07 07:54:23 harti Exp $
40 * $DragonFly: src/usr.bin/make/str.c,v 1.39 2005/08/05 22:42:12 okumoto Exp $
53 * Initialize the argument array object. The array is initially
54 * eight positions, and will be expaned as necessary. The first
55 * position is set to NULL since everything ignores it. We allocate
56 * (size + 1) since we need space for the terminating NULL. The
57 * buffer is set to NULL, since no common buffer is allocated yet.
60 ArgArray_Init(ArgArray
*aa
)
64 aa
->argv
= emalloc((aa
->size
+ 1) * sizeof(char *));
66 aa
->argv
[aa
->argc
++] = NULL
;
72 * Cleanup the memory allocated for in the argument array object.
75 ArgArray_Done(ArgArray
*aa
)
78 if (aa
->buffer
== NULL
) {
80 /* args are individually allocated */
81 for (i
= 0; i
< aa
->argc
; ++i
) {
88 /* args are part of a single allocation */
99 * Concatenate the two strings, possibily inserting a character between them.
102 * the resulting string in allocated space.
105 str_concat(const char s1
[], char c
, const char s2
[])
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 */
126 /* copy second string plus EOS into place */
127 memcpy(result
+ len1
, s2
, len2
+ 1);
133 * Fracture a string into an array of words (as delineated by tabs or
134 * spaces) taking quotation marks into account. Leading tabs/spaces
138 brk_string(ArgArray
*aa
, const char str
[], bool expand
)
144 /* skip leading space chars. */
145 for (; *str
== ' ' || *str
== '\t'; ++str
)
149 aa
->buffer
= estrdup(str
);
156 * copy the string; at the same time, parse backslashes,
157 * quotes and build the argument list.
163 if (inquote
== '\0') {
169 } else if (inquote
== str
[0]) {
171 /* Don't miss "" or '' */
177 /* other type of quote found */
197 * end of a token -- make sure there's enough argv
198 * space and save off a pointer.
200 if (aa
->argc
== aa
->size
) {
201 aa
->size
*= 2; /* ramp up fast */
202 aa
->argv
= erealloc(aa
->argv
,
203 (aa
->size
+ 1) * sizeof(char *));
208 aa
->argv
[aa
->argc
] = start
;
211 if (str
[0] == '\n' || str
[0] == '\0') {
212 aa
->argv
[aa
->argc
++] = start
;
213 aa
->argv
[aa
->argc
] = NULL
;
216 aa
->argv
[aa
->argc
++] = start
;
227 /* hmmm; fix it up as best we can */
272 * Quote a string for appending it to MAKEFLAGS. According to Posix the
273 * kind of quoting here is implementation-defined. This quoting must ensure
274 * that the parsing of MAKEFLAGS's contents in a sub-shell yields the same
275 * options, option arguments and macro definitions as in the calling make.
276 * We simply quote all blanks, which according to Posix are space and tab
277 * in the POSIX locale. Don't use isblank because in that case makes with
278 * different locale settings could not communicate. We must also quote
279 * backslashes obviously.
282 MAKEFLAGS_quote(const char *str
)
287 /* assume worst case - everything has to be quoted */
288 ret
= emalloc(strlen(str
) * 2 + 1);
310 MAKEFLAGS_break(ArgArray
*aa
, const char str
[])
316 aa
->buffer
= strdup(str
);
333 if (aa
->argc
== aa
->size
) {
335 aa
->argv
= erealloc(aa
->argv
,
336 (aa
->size
+ 1) * sizeof(char *));
341 aa
->argv
[aa
->argc
] = start
;
344 if (str
[0] == '\0') {
345 aa
->argv
[aa
->argc
++] = start
;
346 aa
->argv
[aa
->argc
] = NULL
;
349 aa
->argv
[aa
->argc
++] = start
;
356 if (str
[1] == ' ' || str
[1] == '\t')
372 * See if a particular string matches a particular pattern.
374 * Results: Non-zero is returned if string matches pattern, 0 otherwise. The
375 * matching operation permits the following special characters in the
376 * pattern: *?\[] (see the man page for details on what these mean).
378 * Side effects: None.
381 Str_Match(const char *string
, const char *pattern
)
387 * See if we're at the end of both the pattern and the
388 * string. If, we succeeded. If we're at the end of the
389 * pattern but not at the end of the string, we failed.
393 if (*string
== 0 && *pattern
!= '*')
396 * Check for a "*" as the next pattern character. It matches
397 * any substring. We handle this by calling ourselves
398 * recursively for each postfix of string, until either we
399 * match or we reach the end of the string.
401 if (*pattern
== '*') {
405 while (*string
!= 0) {
406 if (Str_Match(string
, pattern
))
413 * Check for a "?" as the next pattern character. It matches
414 * any single character.
419 * Check for a "[" as the next pattern character. It is
420 * followed by a list of characters that are acceptable, or
421 * by a range (two characters separated by "-").
423 if (*pattern
== '[') {
426 if ((*pattern
== ']') || (*pattern
== 0))
428 if (*pattern
== *string
)
430 if (pattern
[1] == '-') {
434 if ((*pattern
<= *string
) &&
437 if ((*pattern
>= *string
) &&
444 while ((*pattern
!= ']') && (*pattern
!= 0))
449 * If the next pattern character is '/', just strip off the
450 * '/' so we do exact matching on the character that follows.
452 if (*pattern
== '\\') {
458 * There's no special character. Just make sure that the
459 * next characters of each string match.
461 if (*pattern
!= *string
)
463 thisCharOK
: ++pattern
;
471 * Check word against pattern for a match (% is wild),
474 * Returns the beginning position of a match or null. The number
475 * of characters matched is returned in len.
478 Str_SYSVMatch(const char *word
, const char *pattern
, int *len
)
480 const char *m
, *p
, *w
;
486 /* Zero-length word cannot be matched against */
492 /* Null pattern is the whole string */
497 if ((m
= strchr(p
, '%')) != NULL
) {
498 /* check that the prefix matches */
499 for (; p
!= m
&& *w
&& *w
== *p
; w
++, p
++)
503 return (NULL
); /* No match */
506 /* No more pattern, return the rest of the string */
514 /* Find a matching tail */
516 if (strcmp(p
, w
) == 0) {
520 while (*w
++ != '\0');
528 * Substitute '%' on the pattern with len characters from src.
529 * If the pattern does not contain a '%' prepend len characters
533 * Places result on buf
536 Str_SYSVSubst(Buffer
*buf
, const char *pat
, const char *src
, int len
)
540 if ((m
= strchr(pat
, '%')) != NULL
) {
541 /* Copy the prefix */
542 Buf_AppendRange(buf
, pat
, m
);
547 /* Copy the pattern */
548 Buf_AddBytes(buf
, len
, src
);
550 /* append the rest */
551 Buf_Append(buf
, pat
);