Home | History | Annotate | Line # | Download | only in make
str.c revision 1.3
      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 /*static char     sccsid[] = "from: @(#)str.c	5.8 (Berkeley) 6/1/90";*/
     41 static char rcsid[] = "$Id: str.c,v 1.3 1994/01/13 21:02:03 jtc Exp $";
     42 #endif				/* not lint */
     43 
     44 #include <stdlib.h>
     45 #include "make.h"
     46 
     47 /*-
     48  * str_concat --
     49  *	concatenate the two strings, inserting a space or slash between them,
     50  *	freeing them if requested.
     51  *
     52  * returns --
     53  *	the resulting string in allocated space.
     54  */
     55 char *
     56 str_concat(s1, s2, flags)
     57 	char *s1, *s2;
     58 	int flags;
     59 {
     60 	register int len1, len2;
     61 	register char *result;
     62 
     63 	/* get the length of both strings */
     64 	len1 = strlen(s1);
     65 	len2 = strlen(s2);
     66 
     67 	/* allocate length plus separator plus EOS */
     68 	result = emalloc((u_int)(len1 + len2 + 2));
     69 
     70 	/* copy first string into place */
     71 	bcopy(s1, result, len1);
     72 
     73 	/* add separator character */
     74 	if (flags & STR_ADDSPACE) {
     75 		result[len1] = ' ';
     76 		++len1;
     77 	} else if (flags & STR_ADDSLASH) {
     78 		result[len1] = '/';
     79 		++len1;
     80 	}
     81 
     82 	/* copy second string plus EOS into place */
     83 	bcopy(s2, result + len1, len2 + 1);
     84 
     85 	/* free original strings */
     86 	if (flags & STR_DOFREE) {
     87 		(void)free(s1);
     88 		(void)free(s2);
     89 	}
     90 	return(result);
     91 }
     92 
     93 /*-
     94  * brk_string --
     95  *	Fracture a string into an array of words (as delineated by tabs or
     96  *	spaces) taking quotation marks into account.  Leading tabs/spaces
     97  *	are ignored.
     98  *
     99  * returns --
    100  *	Pointer to the array of pointers to the words.  To make life easier,
    101  *	the first word is always the value of the .MAKE variable.
    102  */
    103 char **
    104 brk_string(str, store_argc)
    105 	register char *str;
    106 	int *store_argc;
    107 {
    108 	static int argmax, curlen;
    109 	static char **argv, *buf;
    110 	register int argc, ch;
    111 	register char inquote, *p, *start, *t;
    112 	int len;
    113 
    114 	/* save off pmake variable */
    115 	if (!argv) {
    116 		argv = (char **)emalloc((argmax = 50) * sizeof(char *));
    117 		argv[0] = Var_Value(".MAKE", VAR_GLOBAL);
    118 	}
    119 
    120 	/* skip leading space chars.
    121 	for (; *str == ' ' || *str == '\t'; ++str);
    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 = NULL;
    140 				else
    141 					break;
    142 			else
    143 				inquote = 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++ = 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 Str_Match(string, pattern)
    255 	register char *string;		/* String */
    256 	register char *pattern;		/* Pattern */
    257 {
    258 	char c2;
    259 
    260 	for (;;) {
    261 		/*
    262 		 * See if we're at the end of both the pattern and the
    263 		 * string. If, we succeeded.  If we're at the end of the
    264 		 * pattern but not at the end of the string, we failed.
    265 		 */
    266 		if (*pattern == 0)
    267 			return(!*string);
    268 		if (*string == 0 && *pattern != '*')
    269 			return(0);
    270 		/*
    271 		 * Check for a "*" as the next pattern character.  It matches
    272 		 * any substring.  We handle this by calling ourselves
    273 		 * recursively for each postfix of string, until either we
    274 		 * match or we reach the end of the string.
    275 		 */
    276 		if (*pattern == '*') {
    277 			pattern += 1;
    278 			if (*pattern == 0)
    279 				return(1);
    280 			while (*string != 0) {
    281 				if (Str_Match(string, pattern))
    282 					return(1);
    283 				++string;
    284 			}
    285 			return(0);
    286 		}
    287 		/*
    288 		 * Check for a "?" as the next pattern character.  It matches
    289 		 * any single character.
    290 		 */
    291 		if (*pattern == '?')
    292 			goto thisCharOK;
    293 		/*
    294 		 * Check for a "[" as the next pattern character.  It is
    295 		 * followed by a list of characters that are acceptable, or
    296 		 * by a range (two characters separated by "-").
    297 		 */
    298 		if (*pattern == '[') {
    299 			++pattern;
    300 			for (;;) {
    301 				if ((*pattern == ']') || (*pattern == 0))
    302 					return(0);
    303 				if (*pattern == *string)
    304 					break;
    305 				if (pattern[1] == '-') {
    306 					c2 = pattern[2];
    307 					if (c2 == 0)
    308 						return(0);
    309 					if ((*pattern <= *string) &&
    310 					    (c2 >= *string))
    311 						break;
    312 					if ((*pattern >= *string) &&
    313 					    (c2 <= *string))
    314 						break;
    315 					pattern += 2;
    316 				}
    317 				++pattern;
    318 			}
    319 			while ((*pattern != ']') && (*pattern != 0))
    320 				++pattern;
    321 			goto thisCharOK;
    322 		}
    323 		/*
    324 		 * If the next pattern character is '/', just strip off the
    325 		 * '/' so we do exact matching on the character that follows.
    326 		 */
    327 		if (*pattern == '\\') {
    328 			++pattern;
    329 			if (*pattern == 0)
    330 				return(0);
    331 		}
    332 		/*
    333 		 * There's no special character.  Just make sure that the
    334 		 * next characters of each string match.
    335 		 */
    336 		if (*pattern != *string)
    337 			return(0);
    338 thisCharOK:	++pattern;
    339 		++string;
    340 	}
    341 }
    342