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