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