Home | History | Annotate | Line # | Download | only in make
str.c revision 1.1
      1  1.1  cgd /*-
      2  1.1  cgd  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
      3  1.1  cgd  * Copyright (c) 1988, 1989 by Adam de Boor
      4  1.1  cgd  * Copyright (c) 1989 by Berkeley Softworks
      5  1.1  cgd  * All rights reserved.
      6  1.1  cgd  *
      7  1.1  cgd  * This code is derived from software contributed to Berkeley by
      8  1.1  cgd  * Adam de Boor.
      9  1.1  cgd  *
     10  1.1  cgd  * Redistribution and use in source and binary forms, with or without
     11  1.1  cgd  * modification, are permitted provided that the following conditions
     12  1.1  cgd  * are met:
     13  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     14  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     15  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     18  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     19  1.1  cgd  *    must display the following acknowledgement:
     20  1.1  cgd  *	This product includes software developed by the University of
     21  1.1  cgd  *	California, Berkeley and its contributors.
     22  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     23  1.1  cgd  *    may be used to endorse or promote products derived from this software
     24  1.1  cgd  *    without specific prior written permission.
     25  1.1  cgd  *
     26  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1  cgd  * SUCH DAMAGE.
     37  1.1  cgd  */
     38  1.1  cgd 
     39  1.1  cgd #ifndef lint
     40  1.1  cgd static char     sccsid[] = "@(#)str.c	5.8 (Berkeley) 6/1/90";
     41  1.1  cgd #endif				/* not lint */
     42  1.1  cgd 
     43  1.1  cgd #include "make.h"
     44  1.1  cgd 
     45  1.1  cgd /*-
     46  1.1  cgd  * str_concat --
     47  1.1  cgd  *	concatenate the two strings, inserting a space or slash between them,
     48  1.1  cgd  *	freeing them if requested.
     49  1.1  cgd  *
     50  1.1  cgd  * returns --
     51  1.1  cgd  *	the resulting string in allocated space.
     52  1.1  cgd  */
     53  1.1  cgd char *
     54  1.1  cgd str_concat(s1, s2, flags)
     55  1.1  cgd 	char *s1, *s2;
     56  1.1  cgd 	int flags;
     57  1.1  cgd {
     58  1.1  cgd 	register int len1, len2;
     59  1.1  cgd 	register char *result;
     60  1.1  cgd 
     61  1.1  cgd 	/* get the length of both strings */
     62  1.1  cgd 	len1 = strlen(s1);
     63  1.1  cgd 	len2 = strlen(s2);
     64  1.1  cgd 
     65  1.1  cgd 	/* allocate length plus separator plus EOS */
     66  1.1  cgd 	result = emalloc((u_int)(len1 + len2 + 2));
     67  1.1  cgd 
     68  1.1  cgd 	/* copy first string into place */
     69  1.1  cgd 	bcopy(s1, result, len1);
     70  1.1  cgd 
     71  1.1  cgd 	/* add separator character */
     72  1.1  cgd 	if (flags & STR_ADDSPACE) {
     73  1.1  cgd 		result[len1] = ' ';
     74  1.1  cgd 		++len1;
     75  1.1  cgd 	} else if (flags & STR_ADDSLASH) {
     76  1.1  cgd 		result[len1] = '/';
     77  1.1  cgd 		++len1;
     78  1.1  cgd 	}
     79  1.1  cgd 
     80  1.1  cgd 	/* copy second string plus EOS into place */
     81  1.1  cgd 	bcopy(s2, result + len1, len2 + 1);
     82  1.1  cgd 
     83  1.1  cgd 	/* free original strings */
     84  1.1  cgd 	if (flags & STR_DOFREE) {
     85  1.1  cgd 		(void)free(s1);
     86  1.1  cgd 		(void)free(s2);
     87  1.1  cgd 	}
     88  1.1  cgd 	return(result);
     89  1.1  cgd }
     90  1.1  cgd 
     91  1.1  cgd /*-
     92  1.1  cgd  * brk_string --
     93  1.1  cgd  *	Fracture a string into an array of words (as delineated by tabs or
     94  1.1  cgd  *	spaces) taking quotation marks into account.  Leading tabs/spaces
     95  1.1  cgd  *	are ignored.
     96  1.1  cgd  *
     97  1.1  cgd  * returns --
     98  1.1  cgd  *	Pointer to the array of pointers to the words.  To make life easier,
     99  1.1  cgd  *	the first word is always the value of the .MAKE variable.
    100  1.1  cgd  */
    101  1.1  cgd char **
    102  1.1  cgd brk_string(str, store_argc)
    103  1.1  cgd 	register char *str;
    104  1.1  cgd 	int *store_argc;
    105  1.1  cgd {
    106  1.1  cgd 	static int argmax, curlen;
    107  1.1  cgd 	static char **argv, *buf;
    108  1.1  cgd 	register int argc, ch;
    109  1.1  cgd 	register char inquote, *p, *start, *t;
    110  1.1  cgd 	int len;
    111  1.1  cgd 
    112  1.1  cgd 	/* save off pmake variable */
    113  1.1  cgd 	if (!argv) {
    114  1.1  cgd 		argv = (char **)emalloc((argmax = 50) * sizeof(char *));
    115  1.1  cgd 		argv[0] = Var_Value(".MAKE", VAR_GLOBAL);
    116  1.1  cgd 	}
    117  1.1  cgd 
    118  1.1  cgd 	/* skip leading space chars.
    119  1.1  cgd 	for (; *str == ' ' || *str == '\t'; ++str);
    120  1.1  cgd 
    121  1.1  cgd 	/* allocate room for a copy of the string */
    122  1.1  cgd 	if ((len = strlen(str) + 1) > curlen)
    123  1.1  cgd 		buf = emalloc(curlen = len);
    124  1.1  cgd 
    125  1.1  cgd 	/*
    126  1.1  cgd 	 * copy the string; at the same time, parse backslashes,
    127  1.1  cgd 	 * quotes and build the argument list.
    128  1.1  cgd 	 */
    129  1.1  cgd 	argc = 1;
    130  1.1  cgd 	inquote = '\0';
    131  1.1  cgd 	for (p = str, start = t = buf;; ++p) {
    132  1.1  cgd 		switch(ch = *p) {
    133  1.1  cgd 		case '"':
    134  1.1  cgd 		case '\'':
    135  1.1  cgd 			if (inquote)
    136  1.1  cgd 				if (inquote == ch)
    137  1.1  cgd 					inquote = NULL;
    138  1.1  cgd 				else
    139  1.1  cgd 					break;
    140  1.1  cgd 			else
    141  1.1  cgd 				inquote = ch;
    142  1.1  cgd 			continue;
    143  1.1  cgd 		case ' ':
    144  1.1  cgd 		case '\t':
    145  1.1  cgd 			if (inquote)
    146  1.1  cgd 				break;
    147  1.1  cgd 			if (!start)
    148  1.1  cgd 				continue;
    149  1.1  cgd 			/* FALLTHROUGH */
    150  1.1  cgd 		case '\n':
    151  1.1  cgd 		case '\0':
    152  1.1  cgd 			/*
    153  1.1  cgd 			 * end of a token -- make sure there's enough argv
    154  1.1  cgd 			 * space and save off a pointer.
    155  1.1  cgd 			 */
    156  1.1  cgd 			*t++ = '\0';
    157  1.1  cgd 			if (argc == argmax) {
    158  1.1  cgd 				argmax *= 2;		/* ramp up fast */
    159  1.1  cgd 				if (!(argv = (char **)realloc(argv,
    160  1.1  cgd 				    argmax * sizeof(char *))))
    161  1.1  cgd 				enomem();
    162  1.1  cgd 			}
    163  1.1  cgd 			argv[argc++] = start;
    164  1.1  cgd 			start = (char *)NULL;
    165  1.1  cgd 			if (ch == '\n' || ch == '\0')
    166  1.1  cgd 				goto done;
    167  1.1  cgd 			continue;
    168  1.1  cgd 		case '\\':
    169  1.1  cgd 			switch (ch = *++p) {
    170  1.1  cgd 			case '\0':
    171  1.1  cgd 			case '\n':
    172  1.1  cgd 				/* hmmm; fix it up as best we can */
    173  1.1  cgd 				ch = '\\';
    174  1.1  cgd 				--p;
    175  1.1  cgd 				break;
    176  1.1  cgd 			case 'b':
    177  1.1  cgd 				ch = '\b';
    178  1.1  cgd 				break;
    179  1.1  cgd 			case 'f':
    180  1.1  cgd 				ch = '\f';
    181  1.1  cgd 				break;
    182  1.1  cgd 			case 'n':
    183  1.1  cgd 				ch = '\n';
    184  1.1  cgd 				break;
    185  1.1  cgd 			case 'r':
    186  1.1  cgd 				ch = '\r';
    187  1.1  cgd 				break;
    188  1.1  cgd 			case 't':
    189  1.1  cgd 				ch = '\t';
    190  1.1  cgd 				break;
    191  1.1  cgd 			}
    192  1.1  cgd 			break;
    193  1.1  cgd 		}
    194  1.1  cgd 		if (!start)
    195  1.1  cgd 			start = t;
    196  1.1  cgd 		*t++ = ch;
    197  1.1  cgd 	}
    198  1.1  cgd done:	argv[argc] = (char *)NULL;
    199  1.1  cgd 	*store_argc = argc;
    200  1.1  cgd 	return(argv);
    201  1.1  cgd }
    202  1.1  cgd 
    203  1.1  cgd /*
    204  1.1  cgd  * Str_FindSubstring -- See if a string contains a particular substring.
    205  1.1  cgd  *
    206  1.1  cgd  * Results: If string contains substring, the return value is the location of
    207  1.1  cgd  * the first matching instance of substring in string.  If string doesn't
    208  1.1  cgd  * contain substring, the return value is NULL.  Matching is done on an exact
    209  1.1  cgd  * character-for-character basis with no wildcards or special characters.
    210  1.1  cgd  *
    211  1.1  cgd  * Side effects: None.
    212  1.1  cgd  */
    213  1.1  cgd char *
    214  1.1  cgd Str_FindSubstring(string, substring)
    215  1.1  cgd 	register char *string;		/* String to search. */
    216  1.1  cgd 	char *substring;		/* Substring to find in string */
    217  1.1  cgd {
    218  1.1  cgd 	register char *a, *b;
    219  1.1  cgd 
    220  1.1  cgd 	/*
    221  1.1  cgd 	 * First scan quickly through the two strings looking for a single-
    222  1.1  cgd 	 * character match.  When it's found, then compare the rest of the
    223  1.1  cgd 	 * substring.
    224  1.1  cgd 	 */
    225  1.1  cgd 
    226  1.1  cgd 	for (b = substring; *string != 0; string += 1) {
    227  1.1  cgd 		if (*string != *b)
    228  1.1  cgd 			continue;
    229  1.1  cgd 		a = string;
    230  1.1  cgd 		for (;;) {
    231  1.1  cgd 			if (*b == 0)
    232  1.1  cgd 				return(string);
    233  1.1  cgd 			if (*a++ != *b++)
    234  1.1  cgd 				break;
    235  1.1  cgd 		}
    236  1.1  cgd 		b = substring;
    237  1.1  cgd 	}
    238  1.1  cgd 	return((char *) NULL);
    239  1.1  cgd }
    240  1.1  cgd 
    241  1.1  cgd /*
    242  1.1  cgd  * Str_Match --
    243  1.1  cgd  *
    244  1.1  cgd  * See if a particular string matches a particular pattern.
    245  1.1  cgd  *
    246  1.1  cgd  * Results: Non-zero is returned if string matches pattern, 0 otherwise. The
    247  1.1  cgd  * matching operation permits the following special characters in the
    248  1.1  cgd  * pattern: *?\[] (see the man page for details on what these mean).
    249  1.1  cgd  *
    250  1.1  cgd  * Side effects: None.
    251  1.1  cgd  */
    252  1.1  cgd Str_Match(string, pattern)
    253  1.1  cgd 	register char *string;		/* String */
    254  1.1  cgd 	register char *pattern;		/* Pattern */
    255  1.1  cgd {
    256  1.1  cgd 	char c2;
    257  1.1  cgd 
    258  1.1  cgd 	for (;;) {
    259  1.1  cgd 		/*
    260  1.1  cgd 		 * See if we're at the end of both the pattern and the
    261  1.1  cgd 		 * string. If, we succeeded.  If we're at the end of the
    262  1.1  cgd 		 * pattern but not at the end of the string, we failed.
    263  1.1  cgd 		 */
    264  1.1  cgd 		if (*pattern == 0)
    265  1.1  cgd 			return(!*string);
    266  1.1  cgd 		if (*string == 0 && *pattern != '*')
    267  1.1  cgd 			return(0);
    268  1.1  cgd 		/*
    269  1.1  cgd 		 * Check for a "*" as the next pattern character.  It matches
    270  1.1  cgd 		 * any substring.  We handle this by calling ourselves
    271  1.1  cgd 		 * recursively for each postfix of string, until either we
    272  1.1  cgd 		 * match or we reach the end of the string.
    273  1.1  cgd 		 */
    274  1.1  cgd 		if (*pattern == '*') {
    275  1.1  cgd 			pattern += 1;
    276  1.1  cgd 			if (*pattern == 0)
    277  1.1  cgd 				return(1);
    278  1.1  cgd 			while (*string != 0) {
    279  1.1  cgd 				if (Str_Match(string, pattern))
    280  1.1  cgd 					return(1);
    281  1.1  cgd 				++string;
    282  1.1  cgd 			}
    283  1.1  cgd 			return(0);
    284  1.1  cgd 		}
    285  1.1  cgd 		/*
    286  1.1  cgd 		 * Check for a "?" as the next pattern character.  It matches
    287  1.1  cgd 		 * any single character.
    288  1.1  cgd 		 */
    289  1.1  cgd 		if (*pattern == '?')
    290  1.1  cgd 			goto thisCharOK;
    291  1.1  cgd 		/*
    292  1.1  cgd 		 * Check for a "[" as the next pattern character.  It is
    293  1.1  cgd 		 * followed by a list of characters that are acceptable, or
    294  1.1  cgd 		 * by a range (two characters separated by "-").
    295  1.1  cgd 		 */
    296  1.1  cgd 		if (*pattern == '[') {
    297  1.1  cgd 			++pattern;
    298  1.1  cgd 			for (;;) {
    299  1.1  cgd 				if ((*pattern == ']') || (*pattern == 0))
    300  1.1  cgd 					return(0);
    301  1.1  cgd 				if (*pattern == *string)
    302  1.1  cgd 					break;
    303  1.1  cgd 				if (pattern[1] == '-') {
    304  1.1  cgd 					c2 = pattern[2];
    305  1.1  cgd 					if (c2 == 0)
    306  1.1  cgd 						return(0);
    307  1.1  cgd 					if ((*pattern <= *string) &&
    308  1.1  cgd 					    (c2 >= *string))
    309  1.1  cgd 						break;
    310  1.1  cgd 					if ((*pattern >= *string) &&
    311  1.1  cgd 					    (c2 <= *string))
    312  1.1  cgd 						break;
    313  1.1  cgd 					pattern += 2;
    314  1.1  cgd 				}
    315  1.1  cgd 				++pattern;
    316  1.1  cgd 			}
    317  1.1  cgd 			while ((*pattern != ']') && (*pattern != 0))
    318  1.1  cgd 				++pattern;
    319  1.1  cgd 			goto thisCharOK;
    320  1.1  cgd 		}
    321  1.1  cgd 		/*
    322  1.1  cgd 		 * If the next pattern character is '/', just strip off the
    323  1.1  cgd 		 * '/' so we do exact matching on the character that follows.
    324  1.1  cgd 		 */
    325  1.1  cgd 		if (*pattern == '\\') {
    326  1.1  cgd 			++pattern;
    327  1.1  cgd 			if (*pattern == 0)
    328  1.1  cgd 				return(0);
    329  1.1  cgd 		}
    330  1.1  cgd 		/*
    331  1.1  cgd 		 * There's no special character.  Just make sure that the
    332  1.1  cgd 		 * next characters of each string match.
    333  1.1  cgd 		 */
    334  1.1  cgd 		if (*pattern != *string)
    335  1.1  cgd 			return(0);
    336  1.1  cgd thisCharOK:	++pattern;
    337  1.1  cgd 		++string;
    338  1.1  cgd 	}
    339  1.1  cgd }
    340