Changeset 24 for branches/FREEBSD/src/kmk/var.c
- Timestamp:
- Nov 26, 2002, 10:24:54 PM (23 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/FREEBSD/src/kmk/var.c
r10 r24 35 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 36 * SUCH DAMAGE. 37 * 38 * @(#)var.c 8.3 (Berkeley) 3/19/94 39 */ 40 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD: src/usr.bin/make/var.c,v 1.35 2002/10/09 03:42:10 jmallett Exp $"); 37 */ 38 39 #ifndef lint 40 #if 0 41 static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94"; 42 #else 43 static const char rcsid[] = 44 "$FreeBSD: src/usr.bin/make/var.c,v 1.16.2.3 2002/02/27 14:18:57 cjc Exp $"; 45 #endif 46 #endif /* not lint */ 43 47 44 48 /*- … … 126 130 static Lst allVars; /* List of all variables */ 127 131 128 #define 129 #define 130 #define 132 #define FIND_CMD 0x1 /* look in VAR_CMD when searching */ 133 #define FIND_GLOBAL 0x2 /* look in VAR_GLOBAL as well */ 134 #define FIND_ENV 0x4 /* look in the environment also */ 131 135 132 136 typedef struct Var { … … 134 138 Buffer val; /* its value */ 135 139 int flags; /* miscellaneous status flags */ 136 #define 140 #define VAR_IN_USE 1 /* Variable's value currently being used. 137 141 * Used to avoid recursion */ 138 #define 139 #define 142 #define VAR_FROM_ENV 2 /* Variable comes from the environment */ 143 #define VAR_JUNK 4 /* Variable is a junk variable that 140 144 * should be destroyed when done with 141 145 * it. Used by Var_Parse for undefined, … … 144 148 145 149 /* Var*Pattern flags */ 146 #define 147 #define 148 #define 149 #define 150 #define 151 #define 150 #define VAR_SUB_GLOBAL 0x01 /* Apply substitution globally */ 151 #define VAR_SUB_ONE 0x02 /* Apply substitution to one word */ 152 #define VAR_SUB_MATCHED 0x04 /* There was a match */ 153 #define VAR_MATCH_START 0x08 /* Match at start of word */ 154 #define VAR_MATCH_END 0x10 /* Match at end of word */ 155 #define VAR_NOSUBST 0x20 /* don't expand vars in VarGetPattern */ 152 156 153 157 typedef struct { … … 167 171 } VarREPattern; 168 172 169 static int VarCmp(void *, void *); 170 static void VarPossiblyExpand(char **, GNode *); 171 static Var *VarFind(char *, GNode *, int); 172 static void VarAdd(char *, char *, GNode *); 173 static void VarDelete(void *); 174 static Boolean VarHead(char *, Boolean, Buffer, void *); 175 static Boolean VarTail(char *, Boolean, Buffer, void *); 176 static Boolean VarSuffix(char *, Boolean, Buffer, void *); 177 static Boolean VarRoot(char *, Boolean, Buffer, void *); 178 static Boolean VarMatch(char *, Boolean, Buffer, void *); 173 static int VarCmp __P((ClientData, ClientData)); 174 static Var *VarFind __P((char *, GNode *, int)); 175 static void VarAdd __P((char *, char *, GNode *)); 176 static void VarDelete __P((ClientData)); 177 static Boolean VarHead __P((char *, Boolean, Buffer, ClientData)); 178 static Boolean VarTail __P((char *, Boolean, Buffer, ClientData)); 179 static Boolean VarSuffix __P((char *, Boolean, Buffer, ClientData)); 180 static Boolean VarRoot __P((char *, Boolean, Buffer, ClientData)); 181 static Boolean VarMatch __P((char *, Boolean, Buffer, ClientData)); 179 182 #ifdef SYSVVARSUB 180 static Boolean VarSYSVMatch (char *, Boolean, Buffer, void *);183 static Boolean VarSYSVMatch __P((char *, Boolean, Buffer, ClientData)); 181 184 #endif 182 static Boolean VarNoMatch(char *, Boolean, Buffer, void *); 183 static void VarREError(int, regex_t *, const char *); 184 static Boolean VarRESubstitute(char *, Boolean, Buffer, void *); 185 static Boolean VarSubstitute(char *, Boolean, Buffer, void *); 186 static char *VarGetPattern(GNode *, int, char **, int, int *, int *, 187 VarPattern *); 188 static char *VarQuote(char *); 189 static char *VarModify(char *, Boolean (*)(char *, Boolean, Buffer, void *), 190 void *); 191 static int VarPrintVar(void *, void *); 185 static Boolean VarNoMatch __P((char *, Boolean, Buffer, ClientData)); 186 static void VarREError __P((int, regex_t *, const char *)); 187 static Boolean VarRESubstitute __P((char *, Boolean, Buffer, ClientData)); 188 static Boolean VarSubstitute __P((char *, Boolean, Buffer, ClientData)); 189 static char *VarGetPattern __P((GNode *, int, char **, int, int *, int *, 190 VarPattern *)); 191 static char *VarQuote __P((char *)); 192 static char *VarModify __P((char *, Boolean (*)(char *, Boolean, Buffer, 193 ClientData), 194 ClientData)); 195 static int VarPrintVar __P((ClientData, ClientData)); 192 196 193 197 /*- … … 205 209 */ 206 210 static int 207 VarCmp (void *v, void *name) 211 VarCmp (v, name) 212 ClientData v; /* VAR structure to compare */ 213 ClientData name; /* name to look for */ 208 214 { 209 215 return (strcmp ((char *) name, ((Var *) v)->name)); 210 }211 212 /*-213 *-----------------------------------------------------------------------214 * VarPossiblyExpand --215 * Expand a variable name's embedded variables in the given context.216 *217 * Results:218 * The contents of name, possibly expanded.219 *220 * Side Effects:221 * The caller must free the new contents or old contents of name.222 *-----------------------------------------------------------------------223 */224 static void225 VarPossiblyExpand(char **name, GNode *ctxt)226 {227 if (strchr(*name, '$') != NULL)228 *name = Var_Subst(NULL, *name, ctxt, 0);229 else230 *name = estrdup(*name);231 216 } 232 217 … … 237 222 * indicated. 238 223 * 239 * Flags:240 * FIND_GLOBAL set means look in the VAR_GLOBAL context too241 * FIND_CMD set means to look in the VAR_CMD context too242 * FIND_ENV set means to look in the environment243 *244 224 * Results: 245 225 * A pointer to the structure describing the desired variable or 246 * N ULL if the variable does not exist.226 * NIL if the variable does not exist. 247 227 * 248 228 * Side Effects: … … 251 231 */ 252 232 static Var * 253 VarFind (char *name, GNode *ctxt, int flags) 233 VarFind (name, ctxt, flags) 234 char *name; /* name to find */ 235 GNode *ctxt; /* context in which to find it */ 236 int flags; /* FIND_GLOBAL set means to look in the 237 * VAR_GLOBAL context as well. 238 * FIND_CMD set means to look in the VAR_CMD 239 * context also. 240 * FIND_ENV set means to look in the 241 * environment */ 254 242 { 255 243 Boolean localCheckEnvFirst; … … 297 285 * the -E flag to use environment-variable-override for. 298 286 */ 299 if (Lst_Find (envFirstVars, ( void *)name,300 (int (*)( void *, void *)) strcmp) != NULL)287 if (Lst_Find (envFirstVars, (ClientData)name, 288 (int (*)(ClientData, ClientData)) strcmp) != NILLNODE) 301 289 { 302 290 localCheckEnvFirst = TRUE; … … 310 298 * depending on the FIND_* flags in 'flags' 311 299 */ 312 var = Lst_Find (ctxt->context, ( void *)name, VarCmp);313 314 if ((var == N ULL) && (flags & FIND_CMD) && (ctxt != VAR_CMD)) {315 var = Lst_Find (VAR_CMD->context, ( void *)name, VarCmp);316 } 317 if ((var == N ULL) && (flags & FIND_GLOBAL) && (ctxt != VAR_GLOBAL) &&300 var = Lst_Find (ctxt->context, (ClientData)name, VarCmp); 301 302 if ((var == NILLNODE) && (flags & FIND_CMD) && (ctxt != VAR_CMD)) { 303 var = Lst_Find (VAR_CMD->context, (ClientData)name, VarCmp); 304 } 305 if ((var == NILLNODE) && (flags & FIND_GLOBAL) && (ctxt != VAR_GLOBAL) && 318 306 !checkEnvFirst && !localCheckEnvFirst) 319 307 { 320 var = Lst_Find (VAR_GLOBAL->context, ( void *)name, VarCmp);321 } 322 if ((var == N ULL) && (flags & FIND_ENV)) {308 var = Lst_Find (VAR_GLOBAL->context, (ClientData)name, VarCmp); 309 } 310 if ((var == NILLNODE) && (flags & FIND_ENV)) { 323 311 char *env; 324 312 … … 339 327 (flags & FIND_GLOBAL) && (ctxt != VAR_GLOBAL)) 340 328 { 341 var = Lst_Find (VAR_GLOBAL->context, ( void *)name, VarCmp);342 if (var == N ULL) {343 return ((Var *) N ULL);329 var = Lst_Find (VAR_GLOBAL->context, (ClientData)name, VarCmp); 330 if (var == NILLNODE) { 331 return ((Var *) NIL); 344 332 } else { 345 333 return ((Var *)Lst_Datum(var)); 346 334 } 347 335 } else { 348 return((Var *)N ULL);336 return((Var *)NIL); 349 337 } 350 } else if (var == N ULL) {351 return ((Var *) N ULL);338 } else if (var == NILLNODE) { 339 return ((Var *) NIL); 352 340 } else { 353 341 return ((Var *) Lst_Datum (var)); … … 358 346 *----------------------------------------------------------------------- 359 347 * VarAdd -- 360 * Add a new variable of name name and value val to the given context .348 * Add a new variable of name name and value val to the given context 361 349 * 362 350 * Results: … … 370 358 */ 371 359 static void 372 VarAdd (char *name, char *val, GNode *ctxt) 373 { 374 Var *v; 360 VarAdd (name, val, ctxt) 361 char *name; /* name of variable to add */ 362 char *val; /* value to set it to */ 363 GNode *ctxt; /* context in which to set it */ 364 { 365 register Var *v; 375 366 int len; 376 367 … … 385 376 v->flags = 0; 386 377 387 (void) Lst_AtFront (ctxt->context, (void *)v); 388 (void) Lst_AtEnd (allVars, (void *) v); 389 DEBUGF(VAR, ("%s:%s = %s\n", ctxt->name, name, val)); 378 (void) Lst_AtFront (ctxt->context, (ClientData)v); 379 (void) Lst_AtEnd (allVars, (ClientData) v); 380 if (DEBUG(VAR)) { 381 printf("%s:%s = %s\n", ctxt->name, name, val); 382 } 390 383 } 391 384 … … 404 397 */ 405 398 static void 406 VarDelete(void *vp) 399 VarDelete(vp) 400 ClientData vp; 407 401 { 408 402 Var *v = (Var *) vp; 409 403 free(v->name); 410 404 Buf_Destroy(v->val, TRUE); 411 free( v);405 free((Address) v); 412 406 } 413 407 … … 428 422 */ 429 423 void 430 Var_Delete(char *name, GNode *ctxt) 424 Var_Delete(name, ctxt) 425 char *name; 426 GNode *ctxt; 431 427 { 432 428 LstNode ln; 433 429 434 DEBUGF(VAR, ("%s:delete %s\n", ctxt->name, name)); 435 ln = Lst_Find(ctxt->context, (void *)name, VarCmp); 436 if (ln != NULL) { 437 Var *v; 430 if (DEBUG(VAR)) { 431 printf("%s:delete %s\n", ctxt->name, name); 432 } 433 ln = Lst_Find(ctxt->context, (ClientData)name, VarCmp); 434 if (ln != NILLNODE) { 435 register Var *v; 438 436 439 437 v = (Var *)Lst_Datum(ln); … … 441 439 ln = Lst_Member(allVars, v); 442 440 Lst_Remove(allVars, ln); 443 VarDelete(( void *) v);441 VarDelete((ClientData) v); 444 442 } 445 443 } … … 467 465 */ 468 466 void 469 Var_Set (char *name, char *val, GNode *ctxt) 470 { 471 Var *v; 467 Var_Set (name, val, ctxt) 468 char *name; /* name of variable to set */ 469 char *val; /* value to give to the variable */ 470 GNode *ctxt; /* context in which to set it */ 471 { 472 register Var *v; 472 473 473 474 /* … … 476 477 * point in searching them all just to save a bit of memory... 477 478 */ 478 VarPossiblyExpand(&name, ctxt);479 479 v = VarFind (name, ctxt, 0); 480 if (v == (Var *) N ULL) {480 if (v == (Var *) NIL) { 481 481 VarAdd (name, val, ctxt); 482 482 } else { … … 484 484 Buf_AddBytes(v->val, strlen(val), (Byte *)val); 485 485 486 DEBUGF(VAR, ("%s:%s = %s\n", ctxt->name, name, val)); 486 if (DEBUG(VAR)) { 487 printf("%s:%s = %s\n", ctxt->name, name, val); 488 } 487 489 } 488 490 /* … … 493 495 setenv(name, val, 1); 494 496 } 495 free(name);496 497 } 497 498 … … 519 520 */ 520 521 void 521 Var_Append (char *name, char *val, GNode *ctxt) 522 { 523 Var *v; 524 525 VarPossiblyExpand(&name, ctxt); 522 Var_Append (name, val, ctxt) 523 char *name; /* Name of variable to modify */ 524 char *val; /* String to append to it */ 525 GNode *ctxt; /* Context in which this should occur */ 526 { 527 register Var *v; 528 526 529 v = VarFind (name, ctxt, (ctxt == VAR_GLOBAL) ? FIND_ENV : 0); 527 530 528 if (v == (Var *) N ULL) {531 if (v == (Var *) NIL) { 529 532 VarAdd (name, val, ctxt); 530 533 } else { … … 532 535 Buf_AddBytes(v->val, strlen(val), (Byte *)val); 533 536 534 DEBUGF(VAR, ("%s:%s = %s\n", ctxt->name, name, 535 (char *) Buf_GetAll(v->val, (int *)NULL))); 537 if (DEBUG(VAR)) { 538 printf("%s:%s = %s\n", ctxt->name, name, 539 (char *) Buf_GetAll(v->val, (int *)NULL)); 540 } 536 541 537 542 if (v->flags & VAR_FROM_ENV) { … … 543 548 */ 544 549 v->flags &= ~VAR_FROM_ENV; 545 Lst_AtFront(ctxt->context, ( void *)v);550 Lst_AtFront(ctxt->context, (ClientData)v); 546 551 } 547 552 } 548 free(name);549 553 } 550 554 … … 563 567 */ 564 568 Boolean 565 Var_Exists(char *name, GNode *ctxt) 569 Var_Exists(name, ctxt) 570 char *name; /* Variable to find */ 571 GNode *ctxt; /* Context in which to start search */ 566 572 { 567 573 Var *v; 568 574 569 VarPossiblyExpand(&name, ctxt);570 575 v = VarFind(name, ctxt, FIND_CMD|FIND_GLOBAL|FIND_ENV); 571 free(name); 572 573 if (v == (Var *)NULL) { 576 577 if (v == (Var *)NIL) { 574 578 return(FALSE); 575 579 } else if (v->flags & VAR_FROM_ENV) { … … 594 598 */ 595 599 char * 596 Var_Value (char *name, GNode *ctxt, char **frp) 600 Var_Value (name, ctxt, frp) 601 char *name; /* name to find */ 602 GNode *ctxt; /* context in which to search for it */ 603 char **frp; 597 604 { 598 605 Var *v; 599 606 600 VarPossiblyExpand(&name, ctxt);601 607 v = VarFind (name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD); 602 free(name);603 608 *frp = NULL; 604 if (v != (Var *) N ULL) {609 if (v != (Var *) NIL) { 605 610 char *p = ((char *)Buf_GetAll(v->val, (int *)NULL)); 606 611 if (v->flags & VAR_FROM_ENV) { 607 612 Buf_Destroy(v->val, FALSE); 608 free( v);613 free((Address) v); 609 614 *frp = p; 610 615 } … … 631 636 */ 632 637 static Boolean 633 VarHead (char *word, Boolean addSpace, Buffer buf, void *dummy __unused) 634 { 635 char *slash; 638 VarHead (word, addSpace, buf, dummy) 639 char *word; /* Word to trim */ 640 Boolean addSpace; /* True if need to add a space to the buffer 641 * before sticking in the head */ 642 Buffer buf; /* Buffer in which to store it */ 643 ClientData dummy; 644 { 645 register char *slash; 636 646 637 647 slash = strrchr (word, '/'); … … 654 664 } 655 665 } 656 return (TRUE);666 return(dummy ? TRUE : TRUE); 657 667 } 658 668 … … 673 683 */ 674 684 static Boolean 675 VarTail (char *word, Boolean addSpace, Buffer buf, void *dummy __unused) 676 { 677 char *slash; 685 VarTail (word, addSpace, buf, dummy) 686 char *word; /* Word to trim */ 687 Boolean addSpace; /* TRUE if need to stick a space in the 688 * buffer before adding the tail */ 689 Buffer buf; /* Buffer in which to store it */ 690 ClientData dummy; 691 { 692 register char *slash; 678 693 679 694 if (addSpace) { … … 689 704 Buf_AddBytes (buf, strlen(word), (Byte *)word); 690 705 } 691 return ( TRUE);706 return (dummy ? TRUE : TRUE); 692 707 } 693 708 … … 707 722 */ 708 723 static Boolean 709 VarSuffix (char *word, Boolean addSpace, Buffer buf, void *dummy __unused) 710 { 711 char *dot; 724 VarSuffix (word, addSpace, buf, dummy) 725 char *word; /* Word to trim */ 726 Boolean addSpace; /* TRUE if need to add a space before placing 727 * the suffix in the buffer */ 728 Buffer buf; /* Buffer in which to store it */ 729 ClientData dummy; 730 { 731 register char *dot; 712 732 713 733 dot = strrchr (word, '.'); … … 721 741 addSpace = TRUE; 722 742 } 723 return ( addSpace);743 return (dummy ? addSpace : addSpace); 724 744 } 725 745 … … 740 760 */ 741 761 static Boolean 742 VarRoot (char *word, Boolean addSpace, Buffer buf, void *dummy __unused) 743 { 744 char *dot; 762 VarRoot (word, addSpace, buf, dummy) 763 char *word; /* Word to trim */ 764 Boolean addSpace; /* TRUE if need to add a space to the buffer 765 * before placing the root in it */ 766 Buffer buf; /* Buffer in which to store it */ 767 ClientData dummy; 768 { 769 register char *dot; 745 770 746 771 if (addSpace) { … … 756 781 Buf_AddBytes (buf, strlen(word), (Byte *)word); 757 782 } 758 return ( TRUE);783 return (dummy ? TRUE : TRUE); 759 784 } 760 785 … … 764 789 * Place the word in the buffer if it matches the given pattern. 765 790 * Callback function for VarModify to implement the :M modifier. 766 * A space will be added if requested. A pattern is supplied767 * which the word must match.768 791 * 769 792 * Results: … … 777 800 */ 778 801 static Boolean 779 VarMatch (char *word, Boolean addSpace, Buffer buf, void *pattern) 802 VarMatch (word, addSpace, buf, pattern) 803 char *word; /* Word to examine */ 804 Boolean addSpace; /* TRUE if need to add a space to the 805 * buffer before adding the word, if it 806 * matches */ 807 Buffer buf; /* Buffer in which to store it */ 808 ClientData pattern; /* Pattern the word must match */ 780 809 { 781 810 if (Str_Match(word, (char *) pattern)) { … … 795 824 * Place the word in the buffer if it matches the given pattern. 796 825 * Callback function for VarModify to implement the System V % 797 * modifiers. A space is added if requested.826 * modifiers. 798 827 * 799 828 * Results: … … 807 836 */ 808 837 static Boolean 809 VarSYSVMatch (char *word, Boolean addSpace, Buffer buf, void *patp) 838 VarSYSVMatch (word, addSpace, buf, patp) 839 char *word; /* Word to examine */ 840 Boolean addSpace; /* TRUE if need to add a space to the 841 * buffer before adding the word, if it 842 * matches */ 843 Buffer buf; /* Buffer in which to store it */ 844 ClientData patp; /* Pattern the word must match */ 810 845 { 811 846 int len; … … 832 867 * VarNoMatch -- 833 868 * Place the word in the buffer if it doesn't match the given pattern. 834 * Callback function for VarModify to implement the :N modifier. A 835 * space is added if requested. 869 * Callback function for VarModify to implement the :N modifier. 836 870 * 837 871 * Results: … … 845 879 */ 846 880 static Boolean 847 VarNoMatch (char *word, Boolean addSpace, Buffer buf, void *pattern) 881 VarNoMatch (word, addSpace, buf, pattern) 882 char *word; /* Word to examine */ 883 Boolean addSpace; /* TRUE if need to add a space to the 884 * buffer before adding the word, if it 885 * matches */ 886 Buffer buf; /* Buffer in which to store it */ 887 ClientData pattern; /* Pattern the word must match */ 848 888 { 849 889 if (!Str_Match(word, (char *) pattern)) { … … 862 902 * VarSubstitute -- 863 903 * Perform a string-substitution on the given word, placing the 864 * result in the passed buffer. A space is added if requested.904 * result in the passed buffer. 865 905 * 866 906 * Results: … … 873 913 */ 874 914 static Boolean 875 VarSubstitute (char *word, Boolean addSpace, Buffer buf, void *patternp) 876 { 877 int wordLen; /* Length of word */ 878 char *cp; /* General pointer */ 915 VarSubstitute (word, addSpace, buf, patternp) 916 char *word; /* Word to modify */ 917 Boolean addSpace; /* True if space should be added before 918 * other characters */ 919 Buffer buf; /* Buffer for result */ 920 ClientData patternp; /* Pattern for substitution */ 921 { 922 register int wordLen; /* Length of word */ 923 register char *cp; /* General pointer */ 879 924 VarPattern *pattern = (VarPattern *) patternp; 880 925 … … 971 1016 * buffer. 972 1017 */ 973 Boolean done;1018 register Boolean done; 974 1019 int origSize; 975 1020 … … 1035 1080 */ 1036 1081 static void 1037 VarREError(int err, regex_t *pat, const char *str) 1082 VarREError(err, pat, str) 1083 int err; 1084 regex_t *pat; 1085 const char *str; 1038 1086 { 1039 1087 char *errbuf; … … 1052 1100 * VarRESubstitute -- 1053 1101 * Perform a regex substitution on the given word, placing the 1054 * result in the passed buffer. A space is added if requested.1102 * result in the passed buffer. 1055 1103 * 1056 1104 * Results: … … 1063 1111 */ 1064 1112 static Boolean 1065 VarRESubstitute(char *word, Boolean addSpace, Buffer buf, void *patternp) 1113 VarRESubstitute(word, addSpace, buf, patternp) 1114 char *word; 1115 Boolean addSpace; 1116 Buffer buf; 1117 ClientData patternp; 1066 1118 { 1067 1119 VarREPattern *pat; … … 1072 1124 int flags = 0; 1073 1125 1074 #define 1126 #define MAYBE_ADD_SPACE() \ 1075 1127 if (addSpace && !added) \ 1076 1128 Buf_AddByte(buf, ' '); \ … … 1191 1243 */ 1192 1244 static char * 1193 VarModify (char *str, Boolean (*modProc)(char *, Boolean, Buffer, void *), 1194 void *datum) 1245 VarModify (str, modProc, datum) 1246 char *str; /* String whose words should be trimmed */ 1247 /* Function to use to modify them */ 1248 Boolean (*modProc) __P((char *, Boolean, Buffer, ClientData)); 1249 ClientData datum; /* Datum to pass it */ 1195 1250 { 1196 1251 Buffer buf; /* Buffer for the new string */ … … 1238 1293 */ 1239 1294 static char * 1240 VarGetPattern(GNode *ctxt, int err, char **tstr, int delim, int *flags, 1241 int *length, VarPattern *pattern) 1295 VarGetPattern(ctxt, err, tstr, delim, flags, length, pattern) 1296 GNode *ctxt; 1297 int err; 1298 char **tstr; 1299 int delim; 1300 int *flags; 1301 int *length; 1302 VarPattern *pattern; 1242 1303 { 1243 1304 char *cp; … … 1247 1308 length = &junk; 1248 1309 1249 #define 1310 #define IS_A_MATCH(cp, delim) \ 1250 1311 ((cp[0] == '\\') && ((cp[1] == delim) || \ 1251 1312 (cp[1] == '\\') || (cp[1] == '$') || (pattern && (cp[1] == '&')))) … … 1352 1413 */ 1353 1414 static char * 1354 VarQuote(char *str) 1415 VarQuote(str) 1416 char *str; 1355 1417 { 1356 1418 … … 1392 1454 */ 1393 1455 char * 1394 Var_Parse(char *str, GNode *ctxt, Boolean err, int *lengthPtr, Boolean *freePtr) 1395 { 1396 char *tstr; /* Pointer into str */ 1456 Var_Parse (str, ctxt, err, lengthPtr, freePtr) 1457 char *str; /* The string to parse */ 1458 GNode *ctxt; /* The context for the variable */ 1459 Boolean err; /* TRUE if undefined variables are an error */ 1460 int *lengthPtr; /* OUT: The length of the specification */ 1461 Boolean *freePtr; /* OUT: TRUE if caller should free result */ 1462 { 1463 register char *tstr; /* Pointer into str */ 1397 1464 Var *v; /* Variable in invocation */ 1398 1465 char *cp; /* Secondary pointer into str (place marker 1399 1466 * for tstr) */ 1400 1467 Boolean haveModifier;/* TRUE if have modifiers for the variable */ 1401 charendc; /* Ending character when variable in parens1468 register char endc; /* Ending character when variable in parens 1402 1469 * or braces */ 1403 charstartc=0; /* Starting character when variable in parens1470 register char startc=0; /* Starting character when variable in parens 1404 1471 * or braces */ 1405 1472 int cnt; /* Used to count brace pairs when variable in … … 1430 1497 1431 1498 v = VarFind (name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD); 1432 if (v == (Var *)N ULL) {1499 if (v == (Var *)NIL) { 1433 1500 *lengthPtr = 2; 1434 1501 … … 1453 1520 case '!': 1454 1521 return("$(.MEMBER)"); 1455 default:1456 break;1457 1522 } 1458 1523 } … … 1512 1577 1513 1578 v = VarFind (str, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD); 1514 if ((v == (Var *)N ULL) && (ctxt != VAR_CMD) && (ctxt != VAR_GLOBAL) &&1579 if ((v == (Var *)NIL) && (ctxt != VAR_CMD) && (ctxt != VAR_GLOBAL) && 1515 1580 (vlen == 2) && (str[1] == 'F' || str[1] == 'D')) 1516 1581 { … … 1537 1602 v = VarFind(vname, ctxt, 0); 1538 1603 1539 if (v != (Var *)N ULL && !haveModifier) {1604 if (v != (Var *)NIL && !haveModifier) { 1540 1605 /* 1541 1606 * No need for nested expansion or anything, as we're … … 1546 1611 1547 1612 if (str[1] == 'D') { 1548 val = VarModify(val, VarHead, ( void *)0);1613 val = VarModify(val, VarHead, (ClientData)0); 1549 1614 } else { 1550 val = VarModify(val, VarTail, ( void *)0);1615 val = VarModify(val, VarTail, (ClientData)0); 1551 1616 } 1552 1617 /* … … 1561 1626 } 1562 1627 break; 1563 default:1564 break;1565 1628 } 1566 1629 } 1567 1630 } 1568 1631 1569 if (v == (Var *)N ULL) {1632 if (v == (Var *)NIL) { 1570 1633 if (((vlen == 1) || 1571 1634 (((vlen == 2) && (str[1] == 'F' || … … 1588 1651 case '!': 1589 1652 dynamic = TRUE; 1590 break;1591 default:1592 1653 break; 1593 1654 } … … 1693 1754 char termc; /* Character which terminated scan */ 1694 1755 1695 DEBUGF(VAR, ("Applying :%c to \"%s\"\n", *tstr, str)); 1756 if (DEBUG(VAR)) { 1757 printf("Applying :%c to \"%s\"\n", *tstr, str); 1758 } 1696 1759 switch (*tstr) { 1697 1760 case 'U': … … 1770 1833 } 1771 1834 if (*tstr == 'M' || *tstr == 'm') { 1772 newStr = VarModify(str, VarMatch, ( void *)pattern);1835 newStr = VarModify(str, VarMatch, (ClientData)pattern); 1773 1836 } else { 1774 1837 newStr = VarModify(str, VarNoMatch, 1775 ( void *)pattern);1838 (ClientData)pattern); 1776 1839 } 1777 1840 if (copy) { … … 1783 1846 { 1784 1847 VarPattern pattern; 1785 char del;1848 register char delim; 1786 1849 Buffer buf; /* Buffer for patterns */ 1787 1850 1788 1851 pattern.flags = 0; 1789 del = tstr[1];1852 delim = tstr[1]; 1790 1853 tstr += 2; 1791 1854 … … 1808 1871 * The result is left in the Buffer buf. 1809 1872 */ 1810 for (cp = tstr; *cp != '\0' && *cp != del ; cp++) {1873 for (cp = tstr; *cp != '\0' && *cp != delim; cp++) { 1811 1874 if ((*cp == '\\') && 1812 ((cp[1] == del ) ||1875 ((cp[1] == delim) || 1813 1876 (cp[1] == '$') || 1814 1877 (cp[1] == '\\'))) … … 1817 1880 cp++; 1818 1881 } else if (*cp == '$') { 1819 if (cp[1] != del ) {1882 if (cp[1] != delim) { 1820 1883 /* 1821 1884 * If unescaped dollar sign not before the … … 1851 1914 * return NULL 1852 1915 */ 1853 if (*cp != del ) {1916 if (*cp != delim) { 1854 1917 *lengthPtr = cp - start + 1; 1855 1918 if (*freePtr) { … … 1858 1921 Buf_Destroy(buf, TRUE); 1859 1922 Error("Unclosed substitution for %s (%c missing)", 1860 v->name, del );1923 v->name, delim); 1861 1924 return (var_Error); 1862 1925 } … … 1883 1946 1884 1947 tstr = cp + 1; 1885 for (cp = tstr; *cp != '\0' && *cp != del ; cp++) {1948 for (cp = tstr; *cp != '\0' && *cp != delim; cp++) { 1886 1949 if ((*cp == '\\') && 1887 ((cp[1] == del ) ||1950 ((cp[1] == delim) || 1888 1951 (cp[1] == '&') || 1889 1952 (cp[1] == '\\') || … … 1892 1955 Buf_AddByte(buf, (Byte)cp[1]); 1893 1956 cp++; 1894 } else if ((*cp == '$') && (cp[1] != del )) {1957 } else if ((*cp == '$') && (cp[1] != delim)) { 1895 1958 char *cp2; 1896 1959 int len; … … 1916 1979 * If didn't end in delimiter character, complain 1917 1980 */ 1918 if (*cp != del ) {1981 if (*cp != delim) { 1919 1982 *lengthPtr = cp - start + 1; 1920 1983 if (*freePtr) { … … 1923 1986 Buf_Destroy(buf, TRUE); 1924 1987 Error("Unclosed substitution for %s (%c missing)", 1925 v->name, del );1988 v->name, delim); 1926 1989 return (var_Error); 1927 1990 } … … 1944 2007 termc = *cp; 1945 2008 newStr = VarModify(str, VarSubstitute, 1946 ( void *)&pattern);2009 (ClientData)&pattern); 1947 2010 /* 1948 2011 * Free the two strings. … … 1998 2061 pattern.flags |= VAR_SUB_ONE; 1999 2062 continue; 2000 default:2001 break;2002 2063 } 2003 2064 break; … … 2023 2084 sizeof(regmatch_t)); 2024 2085 newStr = VarModify(str, VarRESubstitute, 2025 ( void *) &pattern);2086 (ClientData) &pattern); 2026 2087 regfree(&pattern.re); 2027 2088 free(pattern.replace); … … 2039 2100 case 'T': 2040 2101 if (tstr[1] == endc || tstr[1] == ':') { 2041 newStr = VarModify (str, VarTail, ( void *)0);2102 newStr = VarModify (str, VarTail, (ClientData)0); 2042 2103 cp = tstr + 1; 2043 2104 termc = *cp; … … 2047 2108 case 'H': 2048 2109 if (tstr[1] == endc || tstr[1] == ':') { 2049 newStr = VarModify (str, VarHead, ( void *)0);2110 newStr = VarModify (str, VarHead, (ClientData)0); 2050 2111 cp = tstr + 1; 2051 2112 termc = *cp; … … 2055 2116 case 'E': 2056 2117 if (tstr[1] == endc || tstr[1] == ':') { 2057 newStr = VarModify (str, VarSuffix, ( void *)0);2118 newStr = VarModify (str, VarSuffix, (ClientData)0); 2058 2119 cp = tstr + 1; 2059 2120 termc = *cp; … … 2063 2124 case 'R': 2064 2125 if (tstr[1] == endc || tstr[1] == ':') { 2065 newStr = VarModify (str, VarRoot, ( void *)0);2126 newStr = VarModify (str, VarRoot, (ClientData)0); 2066 2127 cp = tstr + 1; 2067 2128 termc = *cp; … … 2072 2133 case 's': 2073 2134 if (tstr[1] == 'h' && (tstr[2] == endc || tstr[2] == ':')) { 2074 char *err or;2075 newStr = Cmd_Exec (str, &err or);2076 if (err or)2077 Error (err or, str);2135 char *err; 2136 newStr = Cmd_Exec (str, &err); 2137 if (err) 2138 Error (err, str); 2078 2139 cp = tstr + 2; 2079 2140 termc = *cp; … … 2143 2204 */ 2144 2205 newStr = VarModify(str, VarSYSVMatch, 2145 ( void *)&pattern);2206 (ClientData)&pattern); 2146 2207 2147 2208 /* … … 2164 2225 } 2165 2226 } 2166 DEBUGF(VAR, ("Result is \"%s\"\n", newStr)); 2227 if (DEBUG(VAR)) { 2228 printf("Result is \"%s\"\n", newStr); 2229 } 2167 2230 2168 2231 if (*freePtr) { … … 2203 2266 } 2204 2267 Buf_Destroy(v->val, destroy); 2205 free( v);2268 free((Address)v); 2206 2269 } else if (v->flags & VAR_JUNK) { 2207 2270 /* … … 2214 2277 *freePtr = FALSE; 2215 2278 Buf_Destroy(v->val, TRUE); 2216 free( v);2279 free((Address)v); 2217 2280 if (dynamic) { 2218 2281 str = emalloc(*lengthPtr + 1); … … 2242 2305 */ 2243 2306 char * 2244 Var_Subst (char *var, char *str, GNode *ctxt, Boolean undefErr) 2307 Var_Subst (var, str, ctxt, undefErr) 2308 char *var; /* Named variable || NULL for all */ 2309 char *str; /* the string in which to substitute */ 2310 GNode *ctxt; /* the context wherein to find variables */ 2311 Boolean undefErr; /* TRUE if undefineds are an error */ 2245 2312 { 2246 2313 Buffer buf; /* Buffer for forming things */ … … 2377 2444 Buf_AddBytes (buf, strlen (val), (Byte *)val); 2378 2445 if (doFree) { 2379 free ( val);2446 free ((Address)val); 2380 2447 } 2381 2448 } … … 2404 2471 */ 2405 2472 char * 2406 Var_GetTail(char *file) 2407 { 2408 return(VarModify(file, VarTail, (void *)0)); 2473 Var_GetTail(file) 2474 char *file; /* Filename to modify */ 2475 { 2476 return(VarModify(file, VarTail, (ClientData)0)); 2409 2477 } 2410 2478 … … 2425 2493 */ 2426 2494 char * 2427 Var_GetHead(char *file) 2428 { 2429 return(VarModify(file, VarHead, (void *)0)); 2495 Var_GetHead(file) 2496 char *file; /* Filename to manipulate */ 2497 { 2498 return(VarModify(file, VarHead, (ClientData)0)); 2430 2499 } 2431 2500 … … 2443 2512 */ 2444 2513 void 2445 Var_Init ( void)2514 Var_Init () 2446 2515 { 2447 2516 VAR_GLOBAL = Targ_NewGN ("Global"); … … 2453 2522 2454 2523 void 2455 Var_End ( void)2524 Var_End () 2456 2525 { 2457 2526 Lst_Destroy(allVars, VarDelete); … … 2461 2530 /****************** PRINT DEBUGGING INFO *****************/ 2462 2531 static int 2463 VarPrintVar (void *vp, void *dummy __unused) 2532 VarPrintVar (vp, dummy) 2533 ClientData vp; 2534 ClientData dummy; 2464 2535 { 2465 2536 Var *v = (Var *) vp; 2466 2537 printf ("%-16s = %s\n", v->name, (char *) Buf_GetAll(v->val, (int *)NULL)); 2467 return ( 0);2538 return (dummy ? 0 : 0); 2468 2539 } 2469 2540 … … 2475 2546 */ 2476 2547 void 2477 Var_Dump (GNode *ctxt) 2478 { 2479 Lst_ForEach (ctxt->context, VarPrintVar, (void *) 0); 2480 } 2548 Var_Dump (ctxt) 2549 GNode *ctxt; 2550 { 2551 Lst_ForEach (ctxt->context, VarPrintVar, (ClientData) 0); 2552 }
Note:
See TracChangeset
for help on using the changeset viewer.