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