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