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