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