str.c revision 1.24 1 /* $NetBSD: str.c,v 1.24 2005/08/08 16:42:54 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
21 *
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
32 * SUCH DAMAGE.
33 */
34
35 /*-
36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved.
38 *
39 * This code is derived from software contributed to Berkeley by
40 * Adam de Boor.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
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.
57 *
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
68 * SUCH DAMAGE.
69 */
70
71 #ifndef MAKE_NATIVE
72 static char rcsid[] = "$NetBSD: str.c,v 1.24 2005/08/08 16:42:54 christos Exp $";
73 #else
74 #include <sys/cdefs.h>
75 #ifndef lint
76 #if 0
77 static char sccsid[] = "@(#)str.c 5.8 (Berkeley) 6/1/90";
78 #else
79 __RCSID("$NetBSD: str.c,v 1.24 2005/08/08 16:42:54 christos Exp $");
80 #endif
81 #endif /* not lint */
82 #endif
83
84 #include "make.h"
85
86 /*-
87 * str_concat --
88 * concatenate the two strings, inserting a space or slash between them,
89 * freeing them if requested.
90 *
91 * returns --
92 * the resulting string in allocated space.
93 */
94 char *
95 str_concat(const char *s1, const char *s2, int flags)
96 {
97 int len1, len2;
98 char *result;
99
100 /* get the length of both strings */
101 len1 = strlen(s1);
102 len2 = strlen(s2);
103
104 /* allocate length plus separator plus EOS */
105 result = emalloc((u_int)(len1 + len2 + 2));
106
107 /* copy first string into place */
108 memcpy(result, s1, len1);
109
110 /* add separator character */
111 if (flags & STR_ADDSPACE) {
112 result[len1] = ' ';
113 ++len1;
114 } else if (flags & STR_ADDSLASH) {
115 result[len1] = '/';
116 ++len1;
117 }
118
119 /* copy second string plus EOS into place */
120 memcpy(result + len1, s2, len2 + 1);
121
122 return(result);
123 }
124
125 /*-
126 * brk_string --
127 * Fracture a string into an array of words (as delineated by tabs or
128 * spaces) taking quotation marks into account. Leading tabs/spaces
129 * are ignored.
130 *
131 * returns --
132 * Pointer to the array of pointers to the words.
133 */
134 char **
135 brk_string(const char *str, int *store_argc, Boolean expand, char **buffer)
136 {
137 int argc, ch;
138 char inquote, *start, *t;
139 const char *p;
140 int len;
141 int argmax = 50, curlen = 0;
142 char **argv = emalloc((argmax + 1) * sizeof(char *));
143
144 /* skip leading space chars. */
145 for (; *str == ' ' || *str == '\t'; ++str)
146 continue;
147
148 /* allocate room for a copy of the string */
149 if ((len = strlen(str) + 1) > curlen)
150 *buffer = emalloc(curlen = len);
151
152 /*
153 * copy the string; at the same time, parse backslashes,
154 * quotes and build the argument list.
155 */
156 argc = 0;
157 inquote = '\0';
158 for (p = str, start = t = *buffer;; ++p) {
159 switch(ch = *p) {
160 case '"':
161 case '\'':
162 if (inquote) {
163 if (inquote == ch)
164 inquote = '\0';
165 else
166 break;
167 }
168 else {
169 inquote = (char) ch;
170 /* Don't miss "" or '' */
171 if (start == NULL && p[1] == inquote) {
172 start = t + 1;
173 break;
174 }
175 }
176 if (!expand) {
177 if (!start)
178 start = t;
179 *t++ = ch;
180 }
181 continue;
182 case ' ':
183 case '\t':
184 case '\n':
185 if (inquote)
186 break;
187 if (!start)
188 continue;
189 /* FALLTHROUGH */
190 case '\0':
191 /*
192 * end of a token -- make sure there's enough argv
193 * space and save off a pointer.
194 */
195 if (!start)
196 goto done;
197
198 *t++ = '\0';
199 if (argc == argmax) {
200 argmax *= 2; /* ramp up fast */
201 argv = (char **)erealloc(argv,
202 (argmax + 1) * sizeof(char *));
203 }
204 argv[argc++] = start;
205 start = NULL;
206 if (ch == '\n' || ch == '\0')
207 goto done;
208 continue;
209 case '\\':
210 if (!expand) {
211 if (!start)
212 start = t;
213 *t++ = '\\';
214 ch = *++p;
215 break;
216 }
217
218 switch (ch = *++p) {
219 case '\0':
220 case '\n':
221 /* hmmm; fix it up as best we can */
222 ch = '\\';
223 --p;
224 break;
225 case 'b':
226 ch = '\b';
227 break;
228 case 'f':
229 ch = '\f';
230 break;
231 case 'n':
232 ch = '\n';
233 break;
234 case 'r':
235 ch = '\r';
236 break;
237 case 't':
238 ch = '\t';
239 break;
240 }
241 break;
242 }
243 if (!start)
244 start = t;
245 *t++ = (char) ch;
246 }
247 done: argv[argc] = NULL;
248 *store_argc = argc;
249 return(argv);
250 }
251
252 /*
253 * Str_FindSubstring -- See if a string contains a particular substring.
254 *
255 * Input:
256 * string String to search.
257 * substring Substring to find in string.
258 *
259 * Results: If string contains substring, the return value is the location of
260 * the first matching instance of substring in string. If string doesn't
261 * contain substring, the return value is NULL. Matching is done on an exact
262 * character-for-character basis with no wildcards or special characters.
263 *
264 * Side effects: None.
265 */
266 char *
267 Str_FindSubstring(const char *string, const char *substring)
268 {
269 const char *a, *b;
270
271 /*
272 * First scan quickly through the two strings looking for a single-
273 * character match. When it's found, then compare the rest of the
274 * substring.
275 */
276
277 for (b = substring; *string != 0; string += 1) {
278 if (*string != *b)
279 continue;
280 a = string;
281 for (;;) {
282 if (*b == 0)
283 return UNCONST(string);
284 if (*a++ != *b++)
285 break;
286 }
287 b = substring;
288 }
289 return NULL;
290 }
291
292 /*
293 * Str_Match --
294 *
295 * See if a particular string matches a particular pattern.
296 *
297 * Results: Non-zero is returned if string matches pattern, 0 otherwise. The
298 * matching operation permits the following special characters in the
299 * pattern: *?\[] (see the man page for details on what these mean).
300 *
301 * Side effects: None.
302 */
303 int
304 Str_Match(const char *string, const char *pattern)
305 {
306 char c2;
307
308 for (;;) {
309 /*
310 * See if we're at the end of both the pattern and the
311 * string. If, we succeeded. If we're at the end of the
312 * pattern but not at the end of the string, we failed.
313 */
314 if (*pattern == 0)
315 return(!*string);
316 if (*string == 0 && *pattern != '*')
317 return(0);
318 /*
319 * Check for a "*" as the next pattern character. It matches
320 * any substring. We handle this by calling ourselves
321 * recursively for each postfix of string, until either we
322 * match or we reach the end of the string.
323 */
324 if (*pattern == '*') {
325 pattern += 1;
326 if (*pattern == 0)
327 return(1);
328 while (*string != 0) {
329 if (Str_Match(string, pattern))
330 return(1);
331 ++string;
332 }
333 return(0);
334 }
335 /*
336 * Check for a "?" as the next pattern character. It matches
337 * any single character.
338 */
339 if (*pattern == '?')
340 goto thisCharOK;
341 /*
342 * Check for a "[" as the next pattern character. It is
343 * followed by a list of characters that are acceptable, or
344 * by a range (two characters separated by "-").
345 */
346 if (*pattern == '[') {
347 ++pattern;
348 for (;;) {
349 if ((*pattern == ']') || (*pattern == 0))
350 return(0);
351 if (*pattern == *string)
352 break;
353 if (pattern[1] == '-') {
354 c2 = pattern[2];
355 if (c2 == 0)
356 return(0);
357 if ((*pattern <= *string) &&
358 (c2 >= *string))
359 break;
360 if ((*pattern >= *string) &&
361 (c2 <= *string))
362 break;
363 pattern += 2;
364 }
365 ++pattern;
366 }
367 while ((*pattern != ']') && (*pattern != 0))
368 ++pattern;
369 goto thisCharOK;
370 }
371 /*
372 * If the next pattern character is '/', just strip off the
373 * '/' so we do exact matching on the character that follows.
374 */
375 if (*pattern == '\\') {
376 ++pattern;
377 if (*pattern == 0)
378 return(0);
379 }
380 /*
381 * There's no special character. Just make sure that the
382 * next characters of each string match.
383 */
384 if (*pattern != *string)
385 return(0);
386 thisCharOK: ++pattern;
387 ++string;
388 }
389 }
390
391
392 /*-
393 *-----------------------------------------------------------------------
394 * Str_SYSVMatch --
395 * Check word against pattern for a match (% is wild),
396 *
397 * Input:
398 * word Word to examine
399 * pattern Pattern to examine against
400 * len Number of characters to substitute
401 *
402 * Results:
403 * Returns the beginning position of a match or null. The number
404 * of characters matched is returned in len.
405 *
406 * Side Effects:
407 * None
408 *
409 *-----------------------------------------------------------------------
410 */
411 char *
412 Str_SYSVMatch(const char *word, const char *pattern, int *len)
413 {
414 const char *p = pattern;
415 const char *w = word;
416 const char *m;
417
418 if (*p == '\0') {
419 /* Null pattern is the whole string */
420 *len = strlen(w);
421 return UNCONST(w);
422 }
423
424 if ((m = strchr(p, '%')) != NULL) {
425 /* check that the prefix matches */
426 for (; p != m && *w && *w == *p; w++, p++)
427 continue;
428
429 if (p != m)
430 return NULL; /* No match */
431
432 if (*++p == '\0') {
433 /* No more pattern, return the rest of the string */
434 *len = strlen(w);
435 return UNCONST(w);
436 }
437 }
438
439 m = w;
440
441 /* Find a matching tail */
442 do
443 if (strcmp(p, w) == 0) {
444 *len = w - m;
445 return UNCONST(m);
446 }
447 while (*w++ != '\0');
448
449 return NULL;
450 }
451
452
453 /*-
454 *-----------------------------------------------------------------------
455 * Str_SYSVSubst --
456 * Substitute '%' on the pattern with len characters from src.
457 * If the pattern does not contain a '%' prepend len characters
458 * from src.
459 *
460 * Results:
461 * None
462 *
463 * Side Effects:
464 * Places result on buf
465 *
466 *-----------------------------------------------------------------------
467 */
468 void
469 Str_SYSVSubst(Buffer buf, char *pat, char *src, int len)
470 {
471 char *m;
472
473 if ((m = strchr(pat, '%')) != NULL) {
474 /* Copy the prefix */
475 Buf_AddBytes(buf, m - pat, (Byte *)pat);
476 /* skip the % */
477 pat = m + 1;
478 }
479
480 /* Copy the pattern */
481 Buf_AddBytes(buf, len, (Byte *)src);
482
483 /* append the rest */
484 Buf_AddBytes(buf, strlen(pat), (Byte *)pat);
485 }
486