Changeset 2596 for vendor/gnumake/current/variable.c
- Timestamp:
- Jun 20, 2012, 12:44:52 AM (13 years ago)
- Location:
- vendor/gnumake/current
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
vendor/gnumake/current
- Property svn:ignore deleted
-
vendor/gnumake/current/variable.c
r1989 r2596 1 1 /* Internals of variables for GNU Make. 2 2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software4 Foundation, Inc.3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 4 2010 Free Software Foundation, Inc. 5 5 This file is part of GNU Make. 6 6 … … 36 36 static struct pattern_var *pattern_vars; 37 37 38 /* Pointer to last struct in the chain, so we can add onto the end. */ 39 40 static struct pattern_var *last_pattern_var; 41 42 /* Create a new pattern-specific variable struct. */ 38 /* Pointer to the last struct in the pack of a specific size, from 1 to 255.*/ 39 40 static struct pattern_var *last_pattern_vars[256]; 41 42 /* Create a new pattern-specific variable struct. The new variable is 43 inserted into the PATTERN_VARS list in the shortest patterns first 44 order to support the shortest stem matching (the variables are 45 matched in the reverse order so the ones with the longest pattern 46 will be considered first). Variables with the same pattern length 47 are inserted in the definition order. */ 43 48 44 49 struct pattern_var * 45 50 create_pattern_var (const char *target, const char *suffix) 46 51 { 52 register unsigned int len = strlen (target); 47 53 register struct pattern_var *p = xmalloc (sizeof (struct pattern_var)); 48 54 49 if (last_pattern_var != 0) 50 last_pattern_var->next = p; 55 if (pattern_vars != 0) 56 { 57 if (len < 256 && last_pattern_vars[len] != 0) 58 { 59 p->next = last_pattern_vars[len]->next; 60 last_pattern_vars[len]->next = p; 61 } 62 else 63 { 64 /* Find the position where we can insert this variable. */ 65 register struct pattern_var **v; 66 67 for (v = &pattern_vars; ; v = &(*v)->next) 68 { 69 /* Insert at the end of the pack so that patterns with the 70 same length appear in the order they were defined .*/ 71 72 if (*v == 0 || (*v)->len > len) 73 { 74 p->next = *v; 75 *v = p; 76 break; 77 } 78 } 79 } 80 } 51 81 else 52 pattern_vars = p; 53 last_pattern_var = p; 54 p->next = 0; 82 { 83 pattern_vars = p; 84 p->next = 0; 85 } 55 86 56 87 p->target = target; 57 p->len = strlen (target);88 p->len = len; 58 89 p->suffix = suffix + 1; 90 91 if (len < 256) 92 last_pattern_vars[len] = p; 59 93 60 94 return p; … … 140 174 static struct variable_set global_variable_set; 141 175 static struct variable_set_list global_setlist 142 = { 0, &global_variable_set };176 = { 0, &global_variable_set, 0 }; 143 177 struct variable_set_list *current_variable_set_list = &global_setlist; 144 178 … … 209 243 210 244 v = xmalloc (sizeof (struct variable)); 211 v->name = savestring(name, length);245 v->name = xstrndup (name, length); 212 246 v->length = length; 213 247 hash_insert_at (&set->table, v, var_slot); … … 224 258 v->per_target = 0; 225 259 v->append = 0; 260 v->private_var = 0; 226 261 v->export = v_default; 227 262 … … 244 279 } 245 280 281 282 283 /* Undefine variable named NAME in SET. LENGTH is the length of NAME, which 284 does not need to be null-terminated. ORIGIN specifies the origin of the 285 variable (makefile, command line or environment). */ 286 287 static void 288 free_variable_name_and_value (const void *item); 289 290 void 291 undefine_variable_in_set (const char *name, unsigned int length, 292 enum variable_origin origin, 293 struct variable_set *set) 294 { 295 struct variable *v; 296 struct variable **var_slot; 297 struct variable var_key; 298 299 if (set == NULL) 300 set = &global_variable_set; 301 302 var_key.name = (char *) name; 303 var_key.length = length; 304 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key); 305 306 if (env_overrides && origin == o_env) 307 origin = o_env_override; 308 309 v = *var_slot; 310 if (! HASH_VACANT (v)) 311 { 312 if (env_overrides && v->origin == o_env) 313 /* V came from in the environment. Since it was defined 314 before the switches were parsed, it wasn't affected by -e. */ 315 v->origin = o_env_override; 316 317 /* If the definition is from a stronger source than this one, don't 318 undefine it. */ 319 if ((int) origin >= (int) v->origin) 320 { 321 hash_delete_at (&set->table, var_slot); 322 free_variable_name_and_value (v); 323 } 324 } 325 } 246 326 247 327 /* If the variable passed in is "special", handle its special nature. … … 345 425 const struct variable_set_list *setlist; 346 426 struct variable var_key; 427 int is_parent = 0; 347 428 348 429 var_key.name = (char *) name; … … 356 437 357 438 v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key); 358 if (v )439 if (v && (!is_parent || !v->private_var)) 359 440 return v->special ? lookup_special_var (v) : v; 441 442 is_parent |= setlist->next_is_parent; 360 443 } 361 444 … … 470 553 initialize_file_variables (file->double_colon, reading); 471 554 l->next = file->double_colon->variables; 555 l->next_is_parent = 0; 472 556 return; 473 557 } … … 480 564 l->next = file->parent->variables; 481 565 } 566 l->next_is_parent = 1; 482 567 483 568 /* If we're not reading makefiles and we haven't looked yet, see if … … 525 610 v->per_target = p->variable.per_target; 526 611 v->export = p->variable.export; 612 v->private_var = p->variable.private_var; 527 613 } 528 614 while ((p = lookup_pattern_var (p, file->name)) != 0); … … 538 624 { 539 625 file->pat_variables->next = l->next; 626 file->pat_variables->next_is_parent = l->next_is_parent; 540 627 l->next = file->pat_variables; 628 l->next_is_parent = 0; 541 629 } 542 630 } … … 560 648 setlist->set = set; 561 649 setlist->next = current_variable_set_list; 650 setlist->next_is_parent = 0; 562 651 563 652 return setlist; … … 631 720 global_setlist.set = setlist->set; 632 721 global_setlist.next = setlist->next; 722 global_setlist.next_is_parent = setlist->next_is_parent; 633 723 } 634 724 … … 719 809 720 810 sprintf (buf, "%u", makelevel); 721 (void) define_variable (MAKELEVEL_NAME, MAKELEVEL_LENGTH, buf, o_env, 0);811 define_variable_cname (MAKELEVEL_NAME, buf, o_env, 0); 722 812 723 813 sprintf (buf, "%s%s%s", … … 727 817 (remote_description == 0 || remote_description[0] == '\0') 728 818 ? "" : remote_description); 729 (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);819 define_variable_cname ("MAKE_VERSION", buf, o_default, 0); 730 820 731 821 #ifdef __MSDOS__ … … 738 828 struct variable *comp = lookup_variable ("COMSPEC", 7); 739 829 740 /* Make $MAKESHELL override $SHELLeven if -e is in effect. */830 /* $(MAKESHELL) overrides $(SHELL) even if -e is in effect. */ 741 831 if (mshp) 742 832 (void) define_variable (shell_str, shlen, … … 744 834 else if (comp) 745 835 { 746 /* $ COMSPEC shouldn't override $SHELL. */836 /* $(COMSPEC) shouldn't override $(SHELL). */ 747 837 struct variable *shp = lookup_variable (shell_str, shlen); 748 838 … … 803 893 /* This won't override any definition, but it will provide one if there 804 894 isn't one there. */ 805 v = define_variable ("SHELL", 5, default_shell, o_default, 0);895 v = define_variable_cname ("SHELL", default_shell, o_default, 0); 806 896 #ifdef __MSDOS__ 807 897 v->export = v_export; /* Export always SHELL. */ … … 823 913 824 914 /* Make sure MAKEFILES gets exported if it is set. */ 825 v = define_variable ("MAKEFILES", 9, "", o_default, 0);915 v = define_variable_cname ("MAKEFILES", "", o_default, 0); 826 916 v->export = v_ifset; 827 917 … … 830 920 831 921 #ifdef VMS 832 define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);833 define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);834 define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);835 define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);836 define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);837 define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);838 define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);922 define_variable_cname ("@D", "$(dir $@)", o_automatic, 1); 923 define_variable_cname ("%D", "$(dir $%)", o_automatic, 1); 924 define_variable_cname ("*D", "$(dir $*)", o_automatic, 1); 925 define_variable_cname ("<D", "$(dir $<)", o_automatic, 1); 926 define_variable_cname ("?D", "$(dir $?)", o_automatic, 1); 927 define_variable_cname ("^D", "$(dir $^)", o_automatic, 1); 928 define_variable_cname ("+D", "$(dir $+)", o_automatic, 1); 839 929 #else 840 define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);841 define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);842 define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);843 define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);844 define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);845 define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);846 define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);930 define_variable_cname ("@D", "$(patsubst %/,%,$(dir $@))", o_automatic, 1); 931 define_variable_cname ("%D", "$(patsubst %/,%,$(dir $%))", o_automatic, 1); 932 define_variable_cname ("*D", "$(patsubst %/,%,$(dir $*))", o_automatic, 1); 933 define_variable_cname ("<D", "$(patsubst %/,%,$(dir $<))", o_automatic, 1); 934 define_variable_cname ("?D", "$(patsubst %/,%,$(dir $?))", o_automatic, 1); 935 define_variable_cname ("^D", "$(patsubst %/,%,$(dir $^))", o_automatic, 1); 936 define_variable_cname ("+D", "$(patsubst %/,%,$(dir $+))", o_automatic, 1); 847 937 #endif 848 define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);849 define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);850 define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);851 define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);852 define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);853 define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);854 define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);938 define_variable_cname ("@F", "$(notdir $@)", o_automatic, 1); 939 define_variable_cname ("%F", "$(notdir $%)", o_automatic, 1); 940 define_variable_cname ("*F", "$(notdir $*)", o_automatic, 1); 941 define_variable_cname ("<F", "$(notdir $<)", o_automatic, 1); 942 define_variable_cname ("?F", "$(notdir $?)", o_automatic, 1); 943 define_variable_cname ("^F", "$(notdir $^)", o_automatic, 1); 944 define_variable_cname ("+F", "$(notdir $+)", o_automatic, 1); 855 945 } 856 946 … … 979 1069 convert_Path_to_windows32(value, ';'); 980 1070 #endif 981 *result++ = xstrdup (concat ( v->name, "=", value));1071 *result++ = xstrdup (concat (3, v->name, "=", value)); 982 1072 free (value); 983 1073 } … … 989 1079 convert_Path_to_windows32(v->value, ';'); 990 1080 #endif 991 *result++ = xstrdup (concat ( v->name, "=", v->value));1081 *result++ = xstrdup (concat (3, v->name, "=", v->value)); 992 1082 } 993 1083 } … … 1093 1183 unsigned int oldlen, vallen; 1094 1184 const char *val; 1095 char *tp ;1185 char *tp = NULL; 1096 1186 1097 1187 val = value; … … 1106 1196 memory for the expansion as we may still need the rest of the 1107 1197 buffer if we're looking at a target-specific variable. */ 1108 val = alloc_value= allocated_variable_expand (val);1198 val = tp = allocated_variable_expand (val); 1109 1199 1110 1200 oldlen = strlen (v->value); 1111 1201 vallen = strlen (val); 1112 tp = alloca (oldlen + 1 + vallen + 1); 1113 memcpy (tp, v->value, oldlen); 1114 tp[oldlen] = ' '; 1115 memcpy (&tp[oldlen + 1], val, vallen + 1); 1116 p = tp; 1202 p = alloc_value = xmalloc (oldlen + 1 + vallen + 1); 1203 memcpy (alloc_value, v->value, oldlen); 1204 alloc_value[oldlen] = ' '; 1205 memcpy (&alloc_value[oldlen + 1], val, vallen + 1); 1206 1207 if (tp) 1208 free (tp); 1117 1209 } 1118 1210 } … … 1222 1314 else 1223 1315 { 1224 if (alloc_value) 1225 free (alloc_value); 1316 char *tp = alloc_value; 1226 1317 1227 1318 alloc_value = allocated_variable_expand (p); 1319 1228 1320 if (find_and_set_default_shell (alloc_value)) 1229 1321 { … … 1238 1330 else 1239 1331 v = lookup_variable (varname, strlen (varname)); 1332 1333 if (tp) 1334 free (tp); 1240 1335 } 1241 1336 } … … 1264 1359 1265 1360 1266 /* Try to interpret LINE (a null-terminated string) as a variable definition. 1267 1268 ORIGIN may be o_file, o_override, o_env, o_env_override, 1269 or o_command specifying that the variable definition comes 1270 from a makefile, an override directive, the environment with 1271 or without the -e switch, or the command line. 1272 1273 See the comments for parse_variable_definition(). 1274 1275 If LINE was recognized as a variable definition, a pointer to its `struct 1276 variable' is returned. If LINE is not a variable definition, NULL is 1277 returned. */ 1278 1279 struct variable * 1280 parse_variable_definition (struct variable *v, char *line) 1281 { 1282 register int c; 1283 register char *p = line; 1284 register char *beg; 1285 register char *end; 1286 enum variable_flavor flavor = f_bogus; 1287 char *name; 1361 /* Parse P (a null-terminated string) as a variable definition. 1362 1363 If it is not a variable definition, return NULL. 1364 1365 If it is a variable definition, return a pointer to the char after the 1366 assignment token and set *FLAVOR to the type of variable assignment. */ 1367 1368 char * 1369 parse_variable_definition (const char *p, enum variable_flavor *flavor) 1370 { 1371 int wspace = 0; 1372 1373 p = next_token (p); 1288 1374 1289 1375 while (1) 1290 1376 { 1291 c = *p++; 1377 int c = *p++; 1378 1379 /* If we find a comment or EOS, it's not a variable definition. */ 1292 1380 if (c == '\0' || c == '#') 1293 return 0; 1294 if (c == '=') 1381 return NULL; 1382 1383 if (c == '$') 1295 1384 { 1296 end = p - 1; 1297 flavor = f_recursive; 1298 break; 1299 } 1300 else if (c == ':') 1301 if (*p == '=') 1302 { 1303 end = p++ - 1; 1304 flavor = f_simple; 1305 break; 1306 } 1307 else 1308 /* A colon other than := is a rule line, not a variable defn. */ 1309 return 0; 1310 else if (c == '+' && *p == '=') 1311 { 1312 end = p++ - 1; 1313 flavor = f_append; 1314 break; 1315 } 1316 else if (c == '?' && *p == '=') 1317 { 1318 end = p++ - 1; 1319 flavor = f_conditional; 1320 break; 1321 } 1322 else if (c == '$') 1323 { 1324 /* This might begin a variable expansion reference. Make sure we 1325 don't misrecognize chars inside the reference as =, := or +=. */ 1385 /* This begins a variable expansion reference. Make sure we don't 1386 treat chars inside the reference as assignment tokens. */ 1326 1387 char closeparen; 1327 1388 int count; … … 1332 1393 closeparen = '}'; 1333 1394 else 1334 continue; /* Nope. */ 1395 /* '$$' or '$X'. Either way, nothing special to do here. */ 1396 continue; 1335 1397 1336 1398 /* P now points past the opening paren or brace. … … 1347 1409 } 1348 1410 } 1411 continue; 1349 1412 } 1350 } 1351 v->flavor = flavor; 1413 1414 /* If we find whitespace skip it, and remember we found it. */ 1415 if (isblank ((unsigned char)c)) 1416 { 1417 wspace = 1; 1418 p = next_token (p); 1419 c = *p; 1420 if (c == '\0') 1421 return NULL; 1422 ++p; 1423 } 1424 1425 1426 if (c == '=') 1427 { 1428 *flavor = f_recursive; 1429 return (char *)p; 1430 } 1431 1432 /* Match assignment variants (:=, +=, ?=) */ 1433 if (*p == '=') 1434 { 1435 switch (c) 1436 { 1437 case ':': 1438 *flavor = f_simple; 1439 break; 1440 case '+': 1441 *flavor = f_append; 1442 break; 1443 case '?': 1444 *flavor = f_conditional; 1445 break; 1446 default: 1447 /* If we skipped whitespace, non-assignments means no var. */ 1448 if (wspace) 1449 return NULL; 1450 1451 /* Might be assignment, or might be $= or #=. Check. */ 1452 continue; 1453 } 1454 return (char *)++p; 1455 } 1456 else if (c == ':') 1457 /* A colon other than := is a rule line, not a variable defn. */ 1458 return NULL; 1459 1460 /* If we skipped whitespace, non-assignments means no var. */ 1461 if (wspace) 1462 return NULL; 1463 } 1464 1465 return (char *)p; 1466 } 1467 1468 1469 /* Try to interpret LINE (a null-terminated string) as a variable definition. 1470 1471 If LINE was recognized as a variable definition, a pointer to its `struct 1472 variable' is returned. If LINE is not a variable definition, NULL is 1473 returned. */ 1474 1475 struct variable * 1476 assign_variable_definition (struct variable *v, char *line) 1477 { 1478 char *beg; 1479 char *end; 1480 enum variable_flavor flavor; 1481 char *name; 1352 1482 1353 1483 beg = next_token (line); 1484 line = parse_variable_definition (beg, &flavor); 1485 if (!line) 1486 return NULL; 1487 1488 end = line - (flavor == f_recursive ? 1 : 2); 1354 1489 while (end > beg && isblank ((unsigned char)end[-1])) 1355 1490 --end; 1356 p = next_token (p); 1357 v->value = p; 1491 line = next_token (line); 1492 v->value = line; 1493 v->flavor = flavor; 1358 1494 1359 1495 /* Expand the name, so "$(foo)bar = baz" works. */ … … 1377 1513 or without the -e switch, or the command line. 1378 1514 1379 See the comments for parse_variable_definition().1515 See the comments for assign_variable_definition(). 1380 1516 1381 1517 If LINE was recognized as a variable definition, a pointer to its `struct … … 1395 1531 v.fileinfo.filenm = 0; 1396 1532 1397 if (! parse_variable_definition (&v, line))1533 if (!assign_variable_definition (&v, line)) 1398 1534 return 0; 1399 1535 … … 1445 1581 fputs ("# ", stdout); 1446 1582 fputs (origin, stdout); 1583 if (v->private_var) 1584 fputs (" private", stdout); 1447 1585 if (v->fileinfo.filenm) 1448 1586 printf (_(" (from `%s', line %lu)"), … … 1456 1594 else 1457 1595 { 1458 registerchar *p;1596 char *p; 1459 1597 1460 1598 printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":"); … … 1554 1692 */ 1555 1693 convert_Path_to_windows32 (path, ';'); 1556 environ_path = xstrdup (concat ( "PATH", "=", path));1694 environ_path = xstrdup (concat (3, "PATH", "=", path)); 1557 1695 putenv (environ_path); 1558 1696 free (path);
Note:
See TracChangeset
for help on using the changeset viewer.