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