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