str.c revision 1.9 1 /*-
2 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
3 * Copyright (c) 1988, 1989 by Adam de Boor
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 /* from: static char sccsid[] = "@(#)str.c 5.8 (Berkeley) 6/1/90"; */
41 static char *rcsid = "$Id: str.c,v 1.9 1995/02/04 23:44:41 christos Exp $";
42 #endif /* not lint */
43
44 #include "make.h"
45
46 static char **argv, *buffer;
47 static int argmax, curlen;
48
49 /*
50 * str_init --
51 * Initialize the strings package
52 *
53 */
54 void
55 str_init()
56 {
57 char *p1;
58 argv = (char **)emalloc((argmax = 50) * 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 */
68 void
69 str_end()
70 {
71 if (argv[0]) {
72 free(argv[0]);
73 free((Address) argv);
74 }
75 if (buffer)
76 free(buffer);
77 }
78
79 /*-
80 * str_concat --
81 * concatenate the two strings, inserting a space or slash between them,
82 * freeing them if requested.
83 *
84 * returns --
85 * the resulting string in allocated space.
86 */
87 char *
88 str_concat(s1, s2, flags)
89 char *s1, *s2;
90 int flags;
91 {
92 register int len1, len2;
93 register char *result;
94
95 /* get the length of both strings */
96 len1 = strlen(s1);
97 len2 = strlen(s2);
98
99 /* allocate length plus separator plus EOS */
100 result = emalloc((u_int)(len1 + len2 + 2));
101
102 /* copy first string into place */
103 memcpy(result, s1, len1);
104
105 /* add separator character */
106 if (flags & STR_ADDSPACE) {
107 result[len1] = ' ';
108 ++len1;
109 } else if (flags & STR_ADDSLASH) {
110 result[len1] = '/';
111 ++len1;
112 }
113
114 /* copy second string plus EOS into place */
115 memcpy(result + len1, s2, len2 + 1);
116
117 /* free original strings */
118 if (flags & STR_DOFREE) {
119 (void)free(s1);
120 (void)free(s2);
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. To make life easier,
133 * the first word is always the value of the .MAKE variable.
134 */
135 char **
136 brk_string(str, store_argc, expand)
137 register char *str;
138 int *store_argc;
139 Boolean expand;
140 {
141 register int argc, ch;
142 register char inquote, *p, *start, *t;
143 int len;
144
145 /* skip leading space chars. */
146 for (; *str == ' ' || *str == '\t'; ++str)
147 continue;
148
149 /* allocate room for a copy of the string */
150 if ((len = strlen(str) + 1) > curlen) {
151 if (buffer)
152 free(buffer);
153 buffer = emalloc(curlen = len);
154 }
155
156 /*
157 * copy the string; at the same time, parse backslashes,
158 * quotes and build the argument list.
159 */
160 argc = 1;
161 inquote = '\0';
162 for (p = str, start = t = buffer;; ++p) {
163 switch(ch = *p) {
164 case '"':
165 case '\'':
166 if (inquote)
167 if (inquote == ch)
168 inquote = '\0';
169 else
170 break;
171 else {
172 inquote = (char) ch;
173 /* Don't miss "" or '' */
174 if (start == NULL && p[1] == inquote) {
175 start = t + 1;
176 break;
177 }
178 }
179 if (!expand) {
180 if (!start)
181 start = t;
182 *t++ = ch;
183 }
184 continue;
185 case ' ':
186 case '\t':
187 case '\n':
188 if (inquote)
189 break;
190 if (!start)
191 continue;
192 /* FALLTHROUGH */
193 case '\0':
194 /*
195 * end of a token -- make sure there's enough argv
196 * space and save off a pointer.
197 */
198 if (!start)
199 goto done;
200
201 *t++ = '\0';
202 if (argc == argmax) {
203 argmax *= 2; /* ramp up fast */
204 if (!(argv = (char **)realloc(argv,
205 argmax * sizeof(char *))))
206 enomem();
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 }
251 done: 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 */
266 char *
267 Str_FindSubstring(string, substring)
268 register char *string; /* String to search. */
269 char *substring; /* Substring to find in string */
270 {
271 register 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 */
305 int
306 Str_Match(string, pattern)
307 register char *string; /* String */
308 register 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);
390 thisCharOK: ++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 */
410 char *
411 Str_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 (*p == '\0') {
421 /* Null pattern is the whole string */
422 *len = strlen(w);
423 return w;
424 }
425
426 if ((m = strchr(p, '%')) != NULL) {
427 /* check that the prefix matches */
428 for (; p != m && *w && *w == *p; w++, p++)
429 continue;
430
431 if (p != m)
432 return NULL; /* No match */
433
434 if (*++p == '\0') {
435 /* No more pattern, return the rest of the string */
436 *len = strlen(w);
437 return w;
438 }
439 }
440
441 m = w;
442
443 /* Find a matching tail */
444 do
445 if (strcmp(p, w) == 0) {
446 *len = w - m;
447 return m;
448 }
449 while (*w++ != '\0');
450
451 return NULL;
452 }
453
454
455 /*-
456 *-----------------------------------------------------------------------
457 * Str_SYSVSubst --
458 * Substitute '%' on the pattern with len characters from src.
459 * If the pattern does not contain a '%' prepend len characters
460 * from src.
461 *
462 * Results:
463 * None
464 *
465 * Side Effects:
466 * Places result on buf
467 *
468 *-----------------------------------------------------------------------
469 */
470 void
471 Str_SYSVSubst(buf, pat, src, len)
472 Buffer buf;
473 char *pat;
474 char *src;
475 int len;
476 {
477 char *m;
478
479 if ((m = strchr(pat, '%')) != NULL) {
480 /* Copy the prefix */
481 Buf_AddBytes(buf, m - pat, (Byte *) pat);
482 /* skip the % */
483 pat = m + 1;
484 }
485
486 /* Copy the pattern */
487 Buf_AddBytes(buf, len, (Byte *) src);
488
489 /* append the rest */
490 Buf_AddBytes(buf, strlen(pat), (Byte *) pat);
491 }
492