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