Changeset 51 in kBuild for trunk/src/kmk/str.c
- Timestamp:
- Apr 7, 2003 1:30:32 AM (22 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk/str.c
r35 r51 1 1 /*- 2 2 * Copyright (c) 1988, 1989, 1990, 1993 3 * 3 * The Regents of the University of California. All rights reserved. 4 4 * Copyright (c) 1989 by Berkeley Softworks 5 5 * All rights reserved. … … 18 18 * 3. All advertising materials mentioning features or use of this software 19 19 * must display the following acknowledgement: 20 * 21 * 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 22 * 4. Neither the name of the University nor the names of its contributors 23 23 * may be used to endorse or promote products derived from this software … … 39 39 #ifndef lint 40 40 #if 0 41 static char sccsid[] = "@(#)str.c 41 static char sccsid[] = "@(#)str.c 5.8 (Berkeley) 6/1/90"; 42 42 #else 43 43 static const char rcsid[] = 44 44 "$FreeBSD: src/usr.bin/make/str.c,v 1.12.2.1 2002/06/17 04:30:48 jmallett Exp $"; 45 45 #endif 46 #define KLIBFILEDEF rcsid 46 47 #endif /* not lint */ 47 48 … … 53 54 /* 54 55 * str_init -- 55 * 56 * Initialize the strings package 56 57 * 57 58 */ … … 67 68 /* 68 69 * str_end -- 69 * 70 * Cleanup the strings package 70 71 * 71 72 */ … … 74 75 { 75 76 if (argv) { 76 77 78 77 if (argv[0]) 78 efree(argv[0]); 79 efree((Address) argv); 79 80 } 80 81 if (buffer) 81 82 efree(buffer); 82 83 } 83 84 84 85 /*- 85 86 * str_concat -- 86 * 87 * 87 * concatenate the two strings, inserting a space or slash between them, 88 * freeing them if requested. 88 89 * 89 90 * returns -- 90 * 91 * the resulting string in allocated space. 91 92 */ 92 93 char * 93 94 str_concat(s1, s2, flags) 94 95 96 { 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 95 char *s1, *s2; 96 int flags; 97 { 98 register int len1, len2; 99 register char *result; 100 101 /* get the length of both strings */ 102 len1 = strlen(s1); 103 len2 = strlen(s2); 104 105 /* allocate length plus separator plus EOS */ 106 result = emalloc((u_int)(len1 + len2 + 2)); 107 108 /* copy first string into place */ 109 memcpy(result, s1, len1); 110 111 /* add separator character */ 112 if (flags & STR_ADDSPACE) { 113 result[len1] = ' '; 114 ++len1; 115 } else if (flags & STR_ADDSLASH) { 116 result[len1] = '/'; 117 ++len1; 118 } 119 120 /* copy second string plus EOS into place */ 121 memcpy(result + len1, s2, len2 + 1); 122 123 /* efree original strings */ 124 if (flags & STR_DOFREE) { 125 (void)efree(s1); 126 (void)efree(s2); 127 } 128 return(result); 128 129 } 129 130 130 131 /*- 131 132 * brk_string -- 132 * 133 * 134 * 133 * Fracture a string into an array of words (as delineated by tabs or 134 * spaces) taking quotation marks into account. Leading tabs/spaces 135 * are ignored. 135 136 * 136 137 * returns -- 137 * 138 * 138 * Pointer to the array of pointers to the words. To make life easier, 139 * the first word is always the value of the .MAKE variable. 139 140 */ 140 141 char ** 141 142 brk_string(str, store_argc, expand) 142 143 144 145 { 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 argmax *= 2;/* ramp up fast */209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 done: 256 257 143 register char *str; 144 int *store_argc; 145 Boolean expand; 146 { 147 register int argc, ch; 148 register char inquote, *p, *start, *t; 149 int len; 150 151 /* skip leading space chars. */ 152 for (; *str == ' ' || *str == '\t'; ++str) 153 continue; 154 155 /* allocate room for a copy of the string */ 156 if ((len = strlen(str) + 1) > curlen) { 157 if (buffer) 158 efree(buffer); 159 buffer = emalloc(curlen = len); 160 } 161 162 /* 163 * copy the string; at the same time, parse backslashes, 164 * quotes and build the argument list. 165 */ 166 argc = 1; 167 inquote = '\0'; 168 for (p = str, start = t = buffer;; ++p) { 169 switch(ch = *p) { 170 case '"': 171 case '\'': 172 if (inquote) { 173 if (inquote == ch) 174 inquote = '\0'; 175 else 176 break; 177 } else { 178 inquote = (char) ch; 179 /* Don't miss "" or '' */ 180 if (start == NULL && p[1] == inquote) { 181 start = t + 1; 182 break; 183 } 184 } 185 if (!expand) { 186 if (!start) 187 start = t; 188 *t++ = ch; 189 } 190 continue; 191 case ' ': 192 case '\t': 193 case '\n': 194 if (inquote) 195 break; 196 if (!start) 197 continue; 198 /* FALLTHROUGH */ 199 case '\0': 200 /* 201 * end of a token -- make sure there's enough argv 202 * space and save off a pointer. 203 */ 204 if (!start) 205 goto done; 206 207 *t++ = '\0'; 208 if (argc == argmax) { 209 argmax *= 2; /* ramp up fast */ 210 argv = (char **)erealloc(argv, 211 (argmax + 1) * sizeof(char *)); 212 } 213 argv[argc++] = start; 214 start = (char *)NULL; 215 if (ch == '\n' || ch == '\0') 216 goto done; 217 continue; 218 case '\\': 219 if (!expand) { 220 if (!start) 221 start = t; 222 *t++ = '\\'; 223 ch = *++p; 224 break; 225 } 226 227 switch (ch = *++p) { 228 case '\0': 229 case '\n': 230 /* hmmm; fix it up as best we can */ 231 ch = '\\'; 232 --p; 233 break; 234 case 'b': 235 ch = '\b'; 236 break; 237 case 'f': 238 ch = '\f'; 239 break; 240 case 'n': 241 ch = '\n'; 242 break; 243 case 'r': 244 ch = '\r'; 245 break; 246 case 't': 247 ch = '\t'; 248 break; 249 } 250 break; 251 } 252 if (!start) 253 start = t; 254 *t++ = (char) ch; 255 } 256 done: argv[argc] = (char *)NULL; 257 *store_argc = argc; 258 return(argv); 258 259 } 259 260 … … 270 271 char * 271 272 Str_FindSubstring(string, substring) 272 register char *string;/* String to search. */273 char *substring;/* Substring to find in string */274 { 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 273 register char *string; /* String to search. */ 274 char *substring; /* Substring to find in string */ 275 { 276 register char *a, *b; 277 278 /* 279 * First scan quickly through the two strings looking for a single- 280 * character match. When it's found, then compare the rest of the 281 * substring. 282 */ 283 284 for (b = substring; *string != 0; string += 1) { 285 if (*string != *b) 286 continue; 287 a = string; 288 for (;;) { 289 if (*b == 0) 290 return(string); 291 if (*a++ != *b++) 292 break; 293 } 294 b = substring; 295 } 296 return((char *) NULL); 296 297 } 297 298 … … 309 310 int 310 311 Str_Match(string, pattern) 311 register char *string;/* String */312 register char *pattern;/* Pattern */313 { 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 thisCharOK: 395 396 312 register char *string; /* String */ 313 register char *pattern; /* Pattern */ 314 { 315 char c2; 316 317 for (;;) { 318 /* 319 * See if we're at the end of both the pattern and the 320 * string. If, we succeeded. If we're at the end of the 321 * pattern but not at the end of the string, we failed. 322 */ 323 if (*pattern == 0) 324 return(!*string); 325 if (*string == 0 && *pattern != '*') 326 return(0); 327 /* 328 * Check for a "*" as the next pattern character. It matches 329 * any substring. We handle this by calling ourselves 330 * recursively for each postfix of string, until either we 331 * match or we reach the end of the string. 332 */ 333 if (*pattern == '*') { 334 pattern += 1; 335 if (*pattern == 0) 336 return(1); 337 while (*string != 0) { 338 if (Str_Match(string, pattern)) 339 return(1); 340 ++string; 341 } 342 return(0); 343 } 344 /* 345 * Check for a "?" as the next pattern character. It matches 346 * any single character. 347 */ 348 if (*pattern == '?') 349 goto thisCharOK; 350 /* 351 * Check for a "[" as the next pattern character. It is 352 * followed by a list of characters that are acceptable, or 353 * by a range (two characters separated by "-"). 354 */ 355 if (*pattern == '[') { 356 ++pattern; 357 for (;;) { 358 if ((*pattern == ']') || (*pattern == 0)) 359 return(0); 360 if (*pattern == *string) 361 break; 362 if (pattern[1] == '-') { 363 c2 = pattern[2]; 364 if (c2 == 0) 365 return(0); 366 if ((*pattern <= *string) && 367 (c2 >= *string)) 368 break; 369 if ((*pattern >= *string) && 370 (c2 <= *string)) 371 break; 372 pattern += 2; 373 } 374 ++pattern; 375 } 376 while ((*pattern != ']') && (*pattern != 0)) 377 ++pattern; 378 goto thisCharOK; 379 } 380 /* 381 * If the next pattern character is '/', just strip off the 382 * '/' so we do exact matching on the character that follows. 383 */ 384 if (*pattern == '\\') { 385 ++pattern; 386 if (*pattern == 0) 387 return(0); 388 } 389 /* 390 * There's no special character. Just make sure that the 391 * next characters of each string match. 392 */ 393 if (*pattern != *string) 394 return(0); 395 thisCharOK: ++pattern; 396 ++string; 397 } 397 398 } 398 399 … … 401 402 *----------------------------------------------------------------------- 402 403 * Str_SYSVMatch -- 403 * 404 * Check word against pattern for a match (% is wild), 404 405 * 405 406 * Results: 406 * 407 * 407 * Returns the beginning position of a match or null. The number 408 * of characters matched is returned in len. 408 409 * 409 410 * Side Effects: 410 * 411 * None 411 412 * 412 413 *----------------------------------------------------------------------- … … 414 415 char * 415 416 Str_SYSVMatch(word, pattern, len) 416 char *word;/* Word to examine */417 char *pattern;/* Pattern to examine against */418 int *len;/* Number of characters to substitute */417 char *word; /* Word to examine */ 418 char *pattern; /* Pattern to examine against */ 419 int *len; /* Number of characters to substitute */ 419 420 { 420 421 char *p = pattern; … … 423 424 424 425 if (*w == '\0') { 425 426 427 426 /* Zero-length word cannot be matched against */ 427 *len = 0; 428 return NULL; 428 429 } 429 430 430 431 if (*p == '\0') { 431 432 433 432 /* Null pattern is the whole string */ 433 *len = strlen(w); 434 return w; 434 435 } 435 436 436 437 if ((m = strchr(p, '%')) != NULL) { 437 438 439 440 441 442 return NULL;/* No match */443 444 445 446 447 448 438 /* check that the prefix matches */ 439 for (; p != m && *w && *w == *p; w++, p++) 440 continue; 441 442 if (p != m) 443 return NULL; /* No match */ 444 445 if (*++p == '\0') { 446 /* No more pattern, return the rest of the string */ 447 *len = strlen(w); 448 return w; 449 } 449 450 } 450 451 … … 453 454 /* Find a matching tail */ 454 455 do 455 456 457 458 456 if (strcmp(p, w) == 0) { 457 *len = w - m; 458 return m; 459 } 459 460 while (*w++ != '\0'); 460 461 … … 466 467 *----------------------------------------------------------------------- 467 468 * Str_SYSVSubst -- 468 * 469 * 470 * 469 * Substitute '%' on the pattern with len characters from src. 470 * If the pattern does not contain a '%' prepend len characters 471 * from src. 471 472 * 472 473 * Results: 473 * 474 * None 474 475 * 475 476 * Side Effects: 476 * 477 * Places result on buf 477 478 * 478 479 *----------------------------------------------------------------------- … … 488 489 489 490 if ((m = strchr(pat, '%')) != NULL) { 490 491 492 493 491 /* Copy the prefix */ 492 Buf_AddBytes(buf, m - pat, (Byte *) pat); 493 /* skip the % */ 494 pat = m + 1; 494 495 } 495 496
Note:
See TracChangeset
for help on using the changeset viewer.