xref: /netbsd-src/usr.bin/make/str.c (revision d9158b13b5dfe46201430699a3f7a235ecf28df3)
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.8 1994/06/16 18:50:18 jtc 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     free(argv[0]);
72     free((Address) argv);
73     if (buffer)
74 	free(buffer);
75 }
76 
77 /*-
78  * str_concat --
79  *	concatenate the two strings, inserting a space or slash between them,
80  *	freeing them if requested.
81  *
82  * returns --
83  *	the resulting string in allocated space.
84  */
85 char *
86 str_concat(s1, s2, flags)
87 	char *s1, *s2;
88 	int flags;
89 {
90 	register int len1, len2;
91 	register char *result;
92 
93 	/* get the length of both strings */
94 	len1 = strlen(s1);
95 	len2 = strlen(s2);
96 
97 	/* allocate length plus separator plus EOS */
98 	result = emalloc((u_int)(len1 + len2 + 2));
99 
100 	/* copy first string into place */
101 	memcpy(result, s1, len1);
102 
103 	/* add separator character */
104 	if (flags & STR_ADDSPACE) {
105 		result[len1] = ' ';
106 		++len1;
107 	} else if (flags & STR_ADDSLASH) {
108 		result[len1] = '/';
109 		++len1;
110 	}
111 
112 	/* copy second string plus EOS into place */
113 	memcpy(result + len1, s2, len2 + 1);
114 
115 	/* free original strings */
116 	if (flags & STR_DOFREE) {
117 		(void)free(s1);
118 		(void)free(s2);
119 	}
120 	return(result);
121 }
122 
123 /*-
124  * brk_string --
125  *	Fracture a string into an array of words (as delineated by tabs or
126  *	spaces) taking quotation marks into account.  Leading tabs/spaces
127  *	are ignored.
128  *
129  * returns --
130  *	Pointer to the array of pointers to the words.  To make life easier,
131  *	the first word is always the value of the .MAKE variable.
132  */
133 char **
134 brk_string(str, store_argc, expand)
135 	register char *str;
136 	int *store_argc;
137 	Boolean expand;
138 {
139 	register int argc, ch;
140 	register char inquote, *p, *start, *t;
141 	int len;
142 
143 	/* skip leading space chars. */
144 	for (; *str == ' ' || *str == '\t'; ++str)
145 		continue;
146 
147 	/* allocate room for a copy of the string */
148 	if ((len = strlen(str) + 1) > curlen) {
149 		if (buffer)
150 		    free(buffer);
151 		buffer = emalloc(curlen = len);
152 	}
153 
154 	/*
155 	 * copy the string; at the same time, parse backslashes,
156 	 * quotes and build the argument list.
157 	 */
158 	argc = 1;
159 	inquote = '\0';
160 	for (p = str, start = t = buffer;; ++p) {
161 		switch(ch = *p) {
162 		case '"':
163 		case '\'':
164 			if (inquote)
165 				if (inquote == ch)
166 					inquote = '\0';
167 				else
168 					break;
169 			else {
170 				inquote = (char) ch;
171 				/* Don't miss "" or '' */
172 				if (start == NULL && p[1] == inquote) {
173 					start = t + 1;
174 					break;
175 				}
176 			}
177 			if (!expand) {
178 				if (!start)
179 					start = t;
180 				*t++ = ch;
181 			}
182 			continue;
183 		case ' ':
184 		case '\t':
185 		case '\n':
186 			if (inquote)
187 				break;
188 			if (!start)
189 				continue;
190 			/* FALLTHROUGH */
191 		case '\0':
192 			/*
193 			 * end of a token -- make sure there's enough argv
194 			 * space and save off a pointer.
195 			 */
196 			if (!start)
197 			    goto done;
198 
199 			*t++ = '\0';
200 			if (argc == argmax) {
201 				argmax *= 2;		/* ramp up fast */
202 				if (!(argv = (char **)realloc(argv,
203 				    argmax * sizeof(char *))))
204 				enomem();
205 			}
206 			argv[argc++] = start;
207 			start = (char *)NULL;
208 			if (ch == '\n' || ch == '\0')
209 				goto done;
210 			continue;
211 		case '\\':
212 			if (!expand) {
213 				if (!start)
214 					start = t;
215 				*t++ = '\\';
216 				ch = *++p;
217 				break;
218 			}
219 
220 			switch (ch = *++p) {
221 			case '\0':
222 			case '\n':
223 				/* hmmm; fix it up as best we can */
224 				ch = '\\';
225 				--p;
226 				break;
227 			case 'b':
228 				ch = '\b';
229 				break;
230 			case 'f':
231 				ch = '\f';
232 				break;
233 			case 'n':
234 				ch = '\n';
235 				break;
236 			case 'r':
237 				ch = '\r';
238 				break;
239 			case 't':
240 				ch = '\t';
241 				break;
242 			}
243 			break;
244 		}
245 		if (!start)
246 			start = t;
247 		*t++ = (char) ch;
248 	}
249 done:	argv[argc] = (char *)NULL;
250 	*store_argc = argc;
251 	return(argv);
252 }
253 
254 /*
255  * Str_FindSubstring -- See if a string contains a particular substring.
256  *
257  * Results: If string contains substring, the return value is the location of
258  * the first matching instance of substring in string.  If string doesn't
259  * contain substring, the return value is NULL.  Matching is done on an exact
260  * character-for-character basis with no wildcards or special characters.
261  *
262  * Side effects: None.
263  */
264 char *
265 Str_FindSubstring(string, substring)
266 	register char *string;		/* String to search. */
267 	char *substring;		/* Substring to find in string */
268 {
269 	register 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(string);
284 			if (*a++ != *b++)
285 				break;
286 		}
287 		b = substring;
288 	}
289 	return((char *) 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(string, pattern)
305 	register char *string;		/* String */
306 	register char *pattern;		/* Pattern */
307 {
308 	char c2;
309 
310 	for (;;) {
311 		/*
312 		 * See if we're at the end of both the pattern and the
313 		 * string. If, we succeeded.  If we're at the end of the
314 		 * pattern but not at the end of the string, we failed.
315 		 */
316 		if (*pattern == 0)
317 			return(!*string);
318 		if (*string == 0 && *pattern != '*')
319 			return(0);
320 		/*
321 		 * Check for a "*" as the next pattern character.  It matches
322 		 * any substring.  We handle this by calling ourselves
323 		 * recursively for each postfix of string, until either we
324 		 * match or we reach the end of the string.
325 		 */
326 		if (*pattern == '*') {
327 			pattern += 1;
328 			if (*pattern == 0)
329 				return(1);
330 			while (*string != 0) {
331 				if (Str_Match(string, pattern))
332 					return(1);
333 				++string;
334 			}
335 			return(0);
336 		}
337 		/*
338 		 * Check for a "?" as the next pattern character.  It matches
339 		 * any single character.
340 		 */
341 		if (*pattern == '?')
342 			goto thisCharOK;
343 		/*
344 		 * Check for a "[" as the next pattern character.  It is
345 		 * followed by a list of characters that are acceptable, or
346 		 * by a range (two characters separated by "-").
347 		 */
348 		if (*pattern == '[') {
349 			++pattern;
350 			for (;;) {
351 				if ((*pattern == ']') || (*pattern == 0))
352 					return(0);
353 				if (*pattern == *string)
354 					break;
355 				if (pattern[1] == '-') {
356 					c2 = pattern[2];
357 					if (c2 == 0)
358 						return(0);
359 					if ((*pattern <= *string) &&
360 					    (c2 >= *string))
361 						break;
362 					if ((*pattern >= *string) &&
363 					    (c2 <= *string))
364 						break;
365 					pattern += 2;
366 				}
367 				++pattern;
368 			}
369 			while ((*pattern != ']') && (*pattern != 0))
370 				++pattern;
371 			goto thisCharOK;
372 		}
373 		/*
374 		 * If the next pattern character is '/', just strip off the
375 		 * '/' so we do exact matching on the character that follows.
376 		 */
377 		if (*pattern == '\\') {
378 			++pattern;
379 			if (*pattern == 0)
380 				return(0);
381 		}
382 		/*
383 		 * There's no special character.  Just make sure that the
384 		 * next characters of each string match.
385 		 */
386 		if (*pattern != *string)
387 			return(0);
388 thisCharOK:	++pattern;
389 		++string;
390 	}
391 }
392 
393 
394 /*-
395  *-----------------------------------------------------------------------
396  * Str_SYSVMatch --
397  *	Check word against pattern for a match (% is wild),
398  *
399  * Results:
400  *	Returns the beginning position of a match or null. The number
401  *	of characters matched is returned in len.
402  *
403  * Side Effects:
404  *	None
405  *
406  *-----------------------------------------------------------------------
407  */
408 char *
409 Str_SYSVMatch(word, pattern, len)
410     char	*word;		/* Word to examine */
411     char	*pattern;	/* Pattern to examine against */
412     int		*len;		/* Number of characters to substitute */
413 {
414     char *p = pattern;
415     char *w = word;
416     char *m;
417 
418     if (*p == '\0') {
419 	/* Null pattern is the whole string */
420 	*len = strlen(w);
421 	return 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 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 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(buf, pat, src, len)
470     Buffer buf;
471     char *pat;
472     char *src;
473     int   len;
474 {
475     char *m;
476 
477     if ((m = strchr(pat, '%')) != NULL) {
478 	/* Copy the prefix */
479 	Buf_AddBytes(buf, m - pat, (Byte *) pat);
480 	/* skip the % */
481 	pat = m + 1;
482     }
483 
484     /* Copy the pattern */
485     Buf_AddBytes(buf, len, (Byte *) src);
486 
487     /* append the rest */
488     Buf_AddBytes(buf, strlen(pat), (Byte *) pat);
489 }
490