1 /* $NetBSD: str.c,v 1.25 2006/08/11 19:11:00 christos 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 #ifndef MAKE_NATIVE 72 static char rcsid[] = "$NetBSD: str.c,v 1.25 2006/08/11 19:11:00 christos 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.25 2006/08/11 19:11:00 christos 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. 133 */ 134 char ** 135 brk_string(const char *str, int *store_argc, Boolean expand, char **buffer) 136 { 137 int argc, ch; 138 char inquote, *start, *t; 139 const char *p; 140 int len; 141 int argmax = 50, curlen = 0; 142 char **argv = emalloc((argmax + 1) * sizeof(char *)); 143 144 /* skip leading space chars. */ 145 for (; *str == ' ' || *str == '\t'; ++str) 146 continue; 147 148 /* allocate room for a copy of the string */ 149 if ((len = strlen(str) + 1) > curlen) 150 *buffer = emalloc(curlen = len); 151 152 /* 153 * copy the string; at the same time, parse backslashes, 154 * quotes and build the argument list. 155 */ 156 argc = 0; 157 inquote = '\0'; 158 for (p = str, start = t = *buffer;; ++p) { 159 switch(ch = *p) { 160 case '"': 161 case '\'': 162 if (inquote) { 163 if (inquote == ch) 164 inquote = '\0'; 165 else 166 break; 167 } 168 else { 169 inquote = (char) ch; 170 /* Don't miss "" or '' */ 171 if (start == NULL && p[1] == inquote) { 172 start = t + 1; 173 p++; 174 inquote = '\0'; 175 break; 176 } 177 } 178 if (!expand) { 179 if (!start) 180 start = t; 181 *t++ = ch; 182 } 183 continue; 184 case ' ': 185 case '\t': 186 case '\n': 187 if (inquote) 188 break; 189 if (!start) 190 continue; 191 /* FALLTHROUGH */ 192 case '\0': 193 /* 194 * end of a token -- make sure there's enough argv 195 * space and save off a pointer. 196 */ 197 if (!start) 198 goto done; 199 200 *t++ = '\0'; 201 if (argc == argmax) { 202 argmax *= 2; /* ramp up fast */ 203 argv = (char **)erealloc(argv, 204 (argmax + 1) * sizeof(char *)); 205 } 206 argv[argc++] = start; 207 start = NULL; 208 if (ch == '\n' || ch == '\0') 209 goto done; 210 continue; 211 case '\\': 212 if (!expand) { 213 if (!start) 214 start = t; 215 *t++ = '\\'; 216 ch = *++p; 217 break; 218 } 219 220 switch (ch = *++p) { 221 case '\0': 222 case '\n': 223 /* hmmm; fix it up as best we can */ 224 ch = '\\'; 225 --p; 226 break; 227 case 'b': 228 ch = '\b'; 229 break; 230 case 'f': 231 ch = '\f'; 232 break; 233 case 'n': 234 ch = '\n'; 235 break; 236 case 'r': 237 ch = '\r'; 238 break; 239 case 't': 240 ch = '\t'; 241 break; 242 } 243 break; 244 } 245 if (!start) 246 start = t; 247 *t++ = (char) ch; 248 } 249 done: argv[argc] = NULL; 250 *store_argc = argc; 251 return(argv); 252 } 253 254 /* 255 * Str_FindSubstring -- See if a string contains a particular substring. 256 * 257 * Input: 258 * string String to search. 259 * substring Substring to find in string. 260 * 261 * Results: If string contains substring, the return value is the location of 262 * the first matching instance of substring in string. If string doesn't 263 * contain substring, the return value is NULL. Matching is done on an exact 264 * character-for-character basis with no wildcards or special characters. 265 * 266 * Side effects: None. 267 */ 268 char * 269 Str_FindSubstring(const char *string, const char *substring) 270 { 271 const char *a, *b; 272 273 /* 274 * First scan quickly through the two strings looking for a single- 275 * character match. When it's found, then compare the rest of the 276 * substring. 277 */ 278 279 for (b = substring; *string != 0; string += 1) { 280 if (*string != *b) 281 continue; 282 a = string; 283 for (;;) { 284 if (*b == 0) 285 return UNCONST(string); 286 if (*a++ != *b++) 287 break; 288 } 289 b = substring; 290 } 291 return NULL; 292 } 293 294 /* 295 * Str_Match -- 296 * 297 * See if a particular string matches a particular pattern. 298 * 299 * Results: Non-zero is returned if string matches pattern, 0 otherwise. The 300 * matching operation permits the following special characters in the 301 * pattern: *?\[] (see the man page for details on what these mean). 302 * 303 * Side effects: None. 304 */ 305 int 306 Str_Match(const char *string, const char *pattern) 307 { 308 char c2; 309 310 for (;;) { 311 /* 312 * See if we're at the end of both the pattern and the 313 * string. If, we succeeded. If we're at the end of the 314 * pattern but not at the end of the string, we failed. 315 */ 316 if (*pattern == 0) 317 return(!*string); 318 if (*string == 0 && *pattern != '*') 319 return(0); 320 /* 321 * Check for a "*" as the next pattern character. It matches 322 * any substring. We handle this by calling ourselves 323 * recursively for each postfix of string, until either we 324 * match or we reach the end of the string. 325 */ 326 if (*pattern == '*') { 327 pattern += 1; 328 if (*pattern == 0) 329 return(1); 330 while (*string != 0) { 331 if (Str_Match(string, pattern)) 332 return(1); 333 ++string; 334 } 335 return(0); 336 } 337 /* 338 * Check for a "?" as the next pattern character. It matches 339 * any single character. 340 */ 341 if (*pattern == '?') 342 goto thisCharOK; 343 /* 344 * Check for a "[" as the next pattern character. It is 345 * followed by a list of characters that are acceptable, or 346 * by a range (two characters separated by "-"). 347 */ 348 if (*pattern == '[') { 349 ++pattern; 350 for (;;) { 351 if ((*pattern == ']') || (*pattern == 0)) 352 return(0); 353 if (*pattern == *string) 354 break; 355 if (pattern[1] == '-') { 356 c2 = pattern[2]; 357 if (c2 == 0) 358 return(0); 359 if ((*pattern <= *string) && 360 (c2 >= *string)) 361 break; 362 if ((*pattern >= *string) && 363 (c2 <= *string)) 364 break; 365 pattern += 2; 366 } 367 ++pattern; 368 } 369 while ((*pattern != ']') && (*pattern != 0)) 370 ++pattern; 371 goto thisCharOK; 372 } 373 /* 374 * If the next pattern character is '/', just strip off the 375 * '/' so we do exact matching on the character that follows. 376 */ 377 if (*pattern == '\\') { 378 ++pattern; 379 if (*pattern == 0) 380 return(0); 381 } 382 /* 383 * There's no special character. Just make sure that the 384 * next characters of each string match. 385 */ 386 if (*pattern != *string) 387 return(0); 388 thisCharOK: ++pattern; 389 ++string; 390 } 391 } 392 393 394 /*- 395 *----------------------------------------------------------------------- 396 * Str_SYSVMatch -- 397 * Check word against pattern for a match (% is wild), 398 * 399 * Input: 400 * word Word to examine 401 * pattern Pattern to examine against 402 * len Number of characters to substitute 403 * 404 * Results: 405 * Returns the beginning position of a match or null. The number 406 * of characters matched is returned in len. 407 * 408 * Side Effects: 409 * None 410 * 411 *----------------------------------------------------------------------- 412 */ 413 char * 414 Str_SYSVMatch(const char *word, const char *pattern, int *len) 415 { 416 const char *p = pattern; 417 const char *w = word; 418 const char *m; 419 420 if (*p == '\0') { 421 /* Null pattern is the whole string */ 422 *len = strlen(w); 423 return UNCONST(w); 424 } 425 426 if ((m = strchr(p, '%')) != NULL) { 427 /* check that the prefix matches */ 428 for (; p != m && *w && *w == *p; w++, p++) 429 continue; 430 431 if (p != m) 432 return NULL; /* No match */ 433 434 if (*++p == '\0') { 435 /* No more pattern, return the rest of the string */ 436 *len = strlen(w); 437 return UNCONST(w); 438 } 439 } 440 441 m = w; 442 443 /* Find a matching tail */ 444 do 445 if (strcmp(p, w) == 0) { 446 *len = w - m; 447 return UNCONST(m); 448 } 449 while (*w++ != '\0'); 450 451 return NULL; 452 } 453 454 455 /*- 456 *----------------------------------------------------------------------- 457 * Str_SYSVSubst -- 458 * Substitute '%' on the pattern with len characters from src. 459 * If the pattern does not contain a '%' prepend len characters 460 * from src. 461 * 462 * Results: 463 * None 464 * 465 * Side Effects: 466 * Places result on buf 467 * 468 *----------------------------------------------------------------------- 469 */ 470 void 471 Str_SYSVSubst(Buffer buf, char *pat, char *src, int len) 472 { 473 char *m; 474 475 if ((m = strchr(pat, '%')) != NULL) { 476 /* Copy the prefix */ 477 Buf_AddBytes(buf, m - pat, (Byte *)pat); 478 /* skip the % */ 479 pat = m + 1; 480 } 481 482 /* Copy the pattern */ 483 Buf_AddBytes(buf, len, (Byte *)src); 484 485 /* append the rest */ 486 Buf_AddBytes(buf, strlen(pat), (Byte *)pat); 487 } 488