1 /* $NetBSD: str.c,v 1.32 2009/01/17 13:29:37 dsl Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved.
39 * This code is derived from software contributed to Berkeley by
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 static char rcsid
[] = "$NetBSD: str.c,v 1.32 2009/01/17 13:29:37 dsl Exp $";
74 #include <sys/cdefs.h>
77 static char sccsid
[] = "@(#)str.c 5.8 (Berkeley) 6/1/90";
79 __RCSID("$NetBSD: str.c,v 1.32 2009/01/17 13:29:37 dsl Exp $");
88 * concatenate the two strings, inserting a space or slash between them,
89 * freeing them if requested.
92 * the resulting string in allocated space.
95 str_concat(const char *s1
, const char *s2
, int flags
)
100 /* get the length of both strings */
104 /* allocate length plus separator plus EOS */
105 result
= bmake_malloc((u_int
)(len1
+ len2
+ 2));
107 /* copy first string into place */
108 memcpy(result
, s1
, len1
);
110 /* add separator character */
111 if (flags
& STR_ADDSPACE
) {
114 } else if (flags
& STR_ADDSLASH
) {
119 /* copy second string plus EOS into place */
120 memcpy(result
+ len1
, s2
, len2
+ 1);
127 * Fracture a string into an array of words (as delineated by tabs or
128 * spaces) taking quotation marks into account. Leading tabs/spaces
131 * If expand is TRUE, quotes are removed and escape sequences
132 * such as \r, \t, etc... are expanded.
135 * Pointer to the array of pointers to the words.
136 * Memory containing the actual words in *buffer.
137 * Both of these must be free'd by the caller.
138 * Number of words in *store_argc.
141 brk_string(const char *str
, int *store_argc
, Boolean expand
, char **buffer
)
144 char inquote
, *start
, *t
;
147 int argmax
= 50, curlen
= 0;
148 char **argv
= bmake_malloc((argmax
+ 1) * sizeof(char *));
150 /* skip leading space chars. */
151 for (; *str
== ' ' || *str
== '\t'; ++str
)
154 /* allocate room for a copy of the string */
155 if ((len
= strlen(str
) + 1) > curlen
)
156 *buffer
= bmake_malloc(curlen
= len
);
159 * copy the string; at the same time, parse backslashes,
160 * quotes and build the argument list.
164 for (p
= str
, start
= t
= *buffer
;; ++p
) {
176 /* Don't miss "" or '' */
177 if (start
== NULL
&& p
[1] == inquote
) {
204 * end of a token -- make sure there's enough argv
205 * space and save off a pointer.
211 if (argc
== argmax
) {
212 argmax
*= 2; /* ramp up fast */
213 argv
= (char **)bmake_realloc(argv
,
214 (argmax
+ 1) * sizeof(char *));
216 argv
[argc
++] = start
;
218 if (ch
== '\n' || ch
== '\0') {
219 if (expand
&& inquote
) {
233 if (*(p
+1) == '\0') /* catch '\' at end of line */
242 /* hmmm; fix it up as best we can */
268 done
: argv
[argc
] = NULL
;
274 * Str_FindSubstring -- See if a string contains a particular substring.
277 * string String to search.
278 * substring Substring to find in string.
280 * Results: If string contains substring, the return value is the location of
281 * the first matching instance of substring in string. If string doesn't
282 * contain substring, the return value is NULL. Matching is done on an exact
283 * character-for-character basis with no wildcards or special characters.
285 * Side effects: None.
288 Str_FindSubstring(const char *string
, const char *substring
)
293 * First scan quickly through the two strings looking for a single-
294 * character match. When it's found, then compare the rest of the
298 for (b
= substring
; *string
!= 0; string
+= 1) {
304 return UNCONST(string
);
316 * See if a particular string matches a particular pattern.
318 * Results: Non-zero is returned if string matches pattern, 0 otherwise. The
319 * matching operation permits the following special characters in the
320 * pattern: *?\[] (see the man page for details on what these mean).
322 * Side effects: None.
325 Str_Match(const char *string
, const char *pattern
)
331 * See if we're at the end of both the pattern and the
332 * string. If, we succeeded. If we're at the end of the
333 * pattern but not at the end of the string, we failed.
337 if (*string
== 0 && *pattern
!= '*')
340 * Check for a "*" as the next pattern character. It matches
341 * any substring. We handle this by calling ourselves
342 * recursively for each postfix of string, until either we
343 * match or we reach the end of the string.
345 if (*pattern
== '*') {
349 while (*string
!= 0) {
350 if (Str_Match(string
, pattern
))
357 * Check for a "?" as the next pattern character. It matches
358 * any single character.
363 * Check for a "[" as the next pattern character. It is
364 * followed by a list of characters that are acceptable, or
365 * by a range (two characters separated by "-").
367 if (*pattern
== '[') {
370 if ((*pattern
== ']') || (*pattern
== 0))
372 if (*pattern
== *string
)
374 if (pattern
[1] == '-') {
378 if ((*pattern
<= *string
) &&
381 if ((*pattern
>= *string
) &&
388 while ((*pattern
!= ']') && (*pattern
!= 0))
393 * If the next pattern character is '/', just strip off the
394 * '/' so we do exact matching on the character that follows.
396 if (*pattern
== '\\') {
402 * There's no special character. Just make sure that the
403 * next characters of each string match.
405 if (*pattern
!= *string
)
407 thisCharOK
: ++pattern
;
414 *-----------------------------------------------------------------------
416 * Check word against pattern for a match (% is wild),
419 * word Word to examine
420 * pattern Pattern to examine against
421 * len Number of characters to substitute
424 * Returns the beginning position of a match or null. The number
425 * of characters matched is returned in len.
430 *-----------------------------------------------------------------------
433 Str_SYSVMatch(const char *word
, const char *pattern
, int *len
)
435 const char *p
= pattern
;
436 const char *w
= word
;
440 /* Null pattern is the whole string */
445 if ((m
= strchr(p
, '%')) != NULL
) {
446 /* check that the prefix matches */
447 for (; p
!= m
&& *w
&& *w
== *p
; w
++, p
++)
451 return NULL
; /* No match */
454 /* No more pattern, return the rest of the string */
462 /* Find a matching tail */
464 if (strcmp(p
, w
) == 0) {
468 while (*w
++ != '\0');
475 *-----------------------------------------------------------------------
477 * Substitute '%' on the pattern with len characters from src.
478 * If the pattern does not contain a '%' prepend len characters
485 * Places result on buf
487 *-----------------------------------------------------------------------
490 Str_SYSVSubst(Buffer
*buf
, char *pat
, char *src
, int len
)
494 if ((m
= strchr(pat
, '%')) != NULL
) {
495 /* Copy the prefix */
496 Buf_AddBytes(buf
, m
- pat
, pat
);
501 /* Copy the pattern */
502 Buf_AddBytes(buf
, len
, src
);
504 /* append the rest */
505 Buf_AddBytes(buf
, strlen(pat
), pat
);