Changeset 3138 for vendor/gnumake/current/variable.c
- Timestamp:
- Mar 12, 2018, 8:32:29 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vendor/gnumake/current/variable.c
r2596 r3138 1 1 /* Internals of variables for GNU Make. 2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 4 2010 Free Software Foundation, Inc. 2 Copyright (C) 1988-2016 Free Software Foundation, Inc. 5 3 This file is part of GNU Make. 6 4 … … 17 15 this program. If not, see <http://www.gnu.org/licenses/>. */ 18 16 19 #include "make .h"17 #include "makeint.h" 20 18 21 19 #include <assert.h> 22 20 21 #include "filedef.h" 23 22 #include "dep.h" 24 #include "filedef.h"25 23 #include "job.h" 26 24 #include "commands.h" … … 31 29 #endif 32 30 #include "hash.h" 31 32 /* Incremented every time we add or remove a global variable. */ 33 static unsigned long variable_changenum; 33 34 34 35 /* Chain of all pattern-specific variables. */ … … 101 102 { 102 103 struct pattern_var *p; 103 unsigned int targlen = strlen (target);104 unsigned int targlen = strlen (target); 104 105 105 106 for (p = start ? start->next : pattern_vars; p != 0; p = p->next) … … 162 163 } 163 164 164 #ifndef 165 #define VARIABLE_BUCKETS 165 #ifndef VARIABLE_BUCKETS 166 #define VARIABLE_BUCKETS 523 166 167 #endif 167 #ifndef 168 #define PERFILE_VARIABLE_BUCKETS23168 #ifndef PERFILE_VARIABLE_BUCKETS 169 #define PERFILE_VARIABLE_BUCKETS 23 169 170 #endif 170 #ifndef 171 #define SMALL_SCOPE_VARIABLE_BUCKETS13171 #ifndef SMALL_SCOPE_VARIABLE_BUCKETS 172 #define SMALL_SCOPE_VARIABLE_BUCKETS 13 172 173 #endif 173 174 … … 184 185 { 185 186 hash_init (&global_variable_set.table, VARIABLE_BUCKETS, 186 187 variable_hash_1, variable_hash_2, variable_hash_cmp); 187 188 } 188 189 … … 198 199 const char *value, enum variable_origin origin, 199 200 int recursive, struct variable_set *set, 200 const structfloc *flocp)201 const floc *flocp) 201 202 { 202 203 struct variable *v; … … 210 211 var_key.length = length; 211 212 var_slot = (struct variable **) hash_find_slot (&set->table, &var_key); 213 v = *var_slot; 214 215 #ifdef VMS 216 /* VMS does not populate envp[] with DCL symbols and logical names which 217 historically are mapped to environent variables. 218 If the variable is not yet defined, then we need to check if getenv() 219 can find it. Do not do this for origin == o_env to avoid infinte 220 recursion */ 221 if (HASH_VACANT (v) && (origin != o_env)) 222 { 223 struct variable * vms_variable; 224 char * vname = alloca (length + 1); 225 char * vvalue; 226 227 strncpy (vname, name, length); 228 vvalue = getenv(vname); 229 230 /* Values starting with '$' are probably foreign commands. 231 We want to treat them as Shell aliases and not look them up here */ 232 if ((vvalue != NULL) && (vvalue[0] != '$')) 233 { 234 vms_variable = lookup_variable(name, length); 235 /* Refresh the slot */ 236 var_slot = (struct variable **) hash_find_slot (&set->table, 237 &var_key); 238 v = *var_slot; 239 } 240 } 241 #endif 212 242 213 243 if (env_overrides && origin == o_env) 214 244 origin = o_env_override; 215 245 216 v = *var_slot;217 246 if (! HASH_VACANT (v)) 218 247 { 219 248 if (env_overrides && v->origin == o_env) 220 221 222 249 /* V came from in the environment. Since it was defined 250 before the switches were parsed, it wasn't affected by -e. */ 251 v->origin = o_env_override; 223 252 224 253 /* A variable of this name is already defined. 225 226 254 If the old definition is from a stronger source 255 than this one, don't redefine it. */ 227 256 if ((int) origin >= (int) v->origin) 228 { 229 if (v->value != 0) 230 free (v->value); 231 v->value = xstrdup (value); 257 { 258 free (v->value); 259 v->value = xstrdup (value); 232 260 if (flocp != 0) 233 261 v->fileinfo = *flocp; 234 262 else 235 263 v->fileinfo.filenm = 0; 236 237 238 264 v->origin = origin; 265 v->recursive = recursive; 266 } 239 267 return v; 240 268 } … … 246 274 v->length = length; 247 275 hash_insert_at (&set->table, v, var_slot); 276 if (set == &global_variable_set) 277 ++variable_changenum; 278 248 279 v->value = xstrdup (value); 249 280 if (flocp != 0) … … 286 317 287 318 static void 288 free_variable_name_and_value (const void *item); 319 free_variable_name_and_value (const void *item) 320 { 321 struct variable *v = (struct variable *) item; 322 free (v->name); 323 free (v->value); 324 } 325 326 void 327 free_variable_set (struct variable_set_list *list) 328 { 329 hash_map (&list->set->table, free_variable_name_and_value); 330 hash_free (&list->set->table, 1); 331 free (list->set); 332 free (list); 333 } 289 334 290 335 void … … 311 356 { 312 357 if (env_overrides && v->origin == o_env) 313 314 315 316 317 /* If the definition is from a stronger source than this one, don't318 undefine it. */358 /* V came from in the environment. Since it was defined 359 before the switches were parsed, it wasn't affected by -e. */ 360 v->origin = o_env_override; 361 362 /* Undefine only if this undefinition is from an equal or stronger 363 source than the variable definition. */ 319 364 if ((int) origin >= (int) v->origin) 320 365 { 321 366 hash_delete_at (&set->table, var_slot); 322 367 free_variable_name_and_value (v); 323 } 368 free (v); 369 if (set == &global_variable_set) 370 ++variable_changenum; 371 } 324 372 } 325 373 } … … 338 386 lookup_special_var (struct variable *var) 339 387 { 340 static unsigned long last_ var_count= 0;388 static unsigned long last_changenum = 0; 341 389 342 390 … … 366 414 */ 367 415 368 if (streq (var->name, ".VARIABLES") 369 && global_variable_set.table.ht_fill != last_var_count) 416 if (variable_changenum != last_changenum && streq (var->name, ".VARIABLES")) 370 417 { 371 418 unsigned long max = EXPANSION_INCREMENT (strlen (var->value)); … … 403 450 *(p-1) = '\0'; 404 451 405 /* Remember how many variables are in our current count. Since we never 406 remove variables from the list, this is a reliable way to know whether 407 the list is up to date or needs to be recomputed. */ 408 409 last_var_count = global_variable_set.table.ht_fill; 452 /* Remember the current variable change number. */ 453 last_changenum = variable_changenum; 410 454 } 411 455 … … 417 461 /* Lookup a variable whose name is a string starting at NAME 418 462 and with LENGTH chars. NAME need not be null-terminated. 419 Returns address of the `struct variable' containing all info463 Returns address of the 'struct variable' containing all info 420 464 on the variable, or nil if no such variable is defined. */ 421 465 … … 438 482 v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key); 439 483 if (v && (!is_parent || !v->private_var)) 440 484 return v->special ? lookup_special_var (v) : v; 441 485 442 486 is_parent |= setlist->next_is_parent; … … 444 488 445 489 #ifdef VMS 446 /* since we don't read envp[] on startup, try to get the447 variable via getenv() here.*/490 /* VMS does not populate envp[] with DCL symbols and logical names which 491 historically are mapped to enviroment varables and returned by getenv() */ 448 492 { 449 493 char *vname = alloca (length + 1); … … 505 549 /* Lookup a variable whose name is a string starting at NAME 506 550 and with LENGTH chars in set SET. NAME need not be null-terminated. 507 Returns address of the `struct variable' containing all info551 Returns address of the 'struct variable' containing all info 508 552 on the variable, or nil if no such variable is defined. */ 509 553 … … 538 582 { 539 583 l = (struct variable_set_list *) 540 584 xmalloc (sizeof (struct variable_set_list)); 541 585 l->set = xmalloc (sizeof (struct variable_set)); 542 586 hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS, … … 642 686 set = xmalloc (sizeof (struct variable_set)); 643 687 hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS, 644 688 variable_hash_1, variable_hash_2, variable_hash_cmp); 645 689 646 690 setlist = (struct variable_set_list *) … … 653 697 } 654 698 655 static void656 free_variable_name_and_value (const void *item)657 {658 struct variable *v = (struct variable *) item;659 free (v->name);660 free (v->value);661 }662 663 void664 free_variable_set (struct variable_set_list *list)665 {666 hash_map (&list->set->table, free_variable_name_and_value);667 hash_free (&list->set->table, 1);668 free (list->set);669 free (list);670 }671 672 699 /* Create a new variable set and push it on the current setlist. 673 700 If we're pushing a global scope (that is, the current scope is the global … … 679 706 push_new_variable_scope (void) 680 707 { 681 current_variable_set_list = create_new_variable_set ();708 current_variable_set_list = create_new_variable_set (); 682 709 if (current_variable_set_list->next == &global_setlist) 683 710 { … … 702 729 703 730 /* Can't call this if there's no scope to pop! */ 704 assert (current_variable_set_list->next != NULL);731 assert (current_variable_set_list->next != NULL); 705 732 706 733 if (current_variable_set_list != &global_setlist) … … 740 767 struct variable **from_var_end = from_var_slot + from_set->table.ht_size; 741 768 769 int inc = to_set == &global_variable_set ? 1 : 0; 770 742 771 for ( ; from_var_slot < from_var_end; from_var_slot++) 743 772 if (! HASH_VACANT (*from_var_slot)) 744 773 { 745 struct variable *from_var = *from_var_slot; 746 struct variable **to_var_slot 747 = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot); 748 if (HASH_VACANT (*to_var_slot)) 749 hash_insert_at (&to_set->table, from_var, to_var_slot); 750 else 751 { 752 /* GKM FIXME: delete in from_set->table */ 753 free (from_var->value); 754 free (from_var); 755 } 774 struct variable *from_var = *from_var_slot; 775 struct variable **to_var_slot 776 = (struct variable **) hash_find_slot (&to_set->table, *from_var_slot); 777 if (HASH_VACANT (*to_var_slot)) 778 { 779 hash_insert_at (&to_set->table, from_var, to_var_slot); 780 variable_changenum += inc; 781 } 782 else 783 { 784 /* GKM FIXME: delete in from_set->table */ 785 free (from_var->value); 786 free (from_var); 787 } 756 788 } 757 789 } … … 787 819 { 788 820 if (last0 == 0) 789 821 *setlist0 = setlist1; 790 822 else 791 823 last0->next = setlist1; 792 824 } 793 825 } … … 800 832 define_automatic_variables (void) 801 833 { 802 #if defined(WINDOWS32) || defined(__EMX__) 803 extern char* default_shell; 804 #else 805 extern char default_shell[]; 806 #endif 807 register struct variable *v; 834 struct variable *v; 808 835 char buf[200]; 809 836 … … 812 839 813 840 sprintf (buf, "%s%s%s", 814 815 816 817 818 841 version_string, 842 (remote_description == 0 || remote_description[0] == '\0') 843 ? "" : "-", 844 (remote_description == 0 || remote_description[0] == '\0') 845 ? "" : remote_description); 819 846 define_variable_cname ("MAKE_VERSION", buf, o_default, 0); 847 define_variable_cname ("MAKE_HOST", make_host, o_default, 0); 820 848 821 849 #ifdef __MSDOS__ … … 831 859 if (mshp) 832 860 (void) define_variable (shell_str, shlen, 833 861 mshp->value, o_env_override, 0); 834 862 else if (comp) 835 863 { 836 837 838 839 840 864 /* $(COMSPEC) shouldn't override $(SHELL). */ 865 struct variable *shp = lookup_variable (shell_str, shlen); 866 867 if (!shp) 868 (void) define_variable (shell_str, shlen, comp->value, o_env, 0); 841 869 } 842 870 } … … 856 884 if (!replace || !*replace->value) 857 885 if (shell && *shell->value && (shell->origin == o_env 858 859 860 861 free(shell->value);862 863 864 886 || shell->origin == o_env_override)) 887 { 888 /* overwrite whatever we got from the environment */ 889 free (shell->value); 890 shell->value = xstrdup (default_shell); 891 shell->origin = o_default; 892 } 865 893 866 894 /* Some people do not like cmd to be used as the default … … 882 910 /* overwrite $SHELL */ 883 911 (void) define_variable (shell_str, shlen, replace->value, 884 912 replace->origin, 0); 885 913 else 886 914 /* provide a definition if there is none */ 887 915 (void) define_variable (shell_str, shlen, default_shell, 888 916 o_default, 0); 889 917 } 890 918 … … 919 947 the automatic variables they are variations of. */ 920 948 921 #ifdef VMS 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); 929 #else 949 #if defined(__MSDOS__) || defined(WINDOWS32) 950 /* For consistency, remove the trailing backslash as well as slash. */ 951 define_variable_cname ("@D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $@)))", 952 o_automatic, 1); 953 define_variable_cname ("%D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $%)))", 954 o_automatic, 1); 955 define_variable_cname ("*D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $*)))", 956 o_automatic, 1); 957 define_variable_cname ("<D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $<)))", 958 o_automatic, 1); 959 define_variable_cname ("?D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $?)))", 960 o_automatic, 1); 961 define_variable_cname ("^D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $^)))", 962 o_automatic, 1); 963 define_variable_cname ("+D", "$(patsubst %/,%,$(patsubst %\\,%,$(dir $+)))", 964 o_automatic, 1); 965 #else /* not __MSDOS__, not WINDOWS32 */ 930 966 define_variable_cname ("@D", "$(patsubst %/,%,$(dir $@))", o_automatic, 1); 931 967 define_variable_cname ("%D", "$(patsubst %/,%,$(dir $%))", o_automatic, 1); … … 949 985 950 986 /* Create a new environment for FILE's commands. 951 If FILE is nil, this is for the `shell' function.987 If FILE is nil, this is for the 'shell' function. 952 988 The child's MAKELEVEL variable is incremented. */ 953 989 … … 970 1006 971 1007 hash_init (&table, VARIABLE_BUCKETS, 972 1008 variable_hash_1, variable_hash_2, variable_hash_cmp); 973 1009 974 1010 /* Run through all the variable sets in the list, … … 980 1016 v_end = v_slot + set->table.ht_size; 981 1017 for ( ; v_slot < v_end; v_slot++) 982 983 984 985 986 987 988 989 990 991 992 993 994 gv = lookup_variable_in_set (v->name, strlen(v->name),1018 if (! HASH_VACANT (*v_slot)) 1019 { 1020 struct variable **new_slot; 1021 struct variable *v = *v_slot; 1022 1023 /* If this is a per-target variable and it hasn't been touched 1024 already then look up the global version and take its export 1025 value. */ 1026 if (v->per_target && v->export == v_default) 1027 { 1028 struct variable *gv; 1029 1030 gv = lookup_variable_in_set (v->name, strlen (v->name), 995 1031 &global_variable_set); 996 997 998 999 1000 1001 1002 1003 1004 1005 1032 if (gv) 1033 v->export = gv->export; 1034 } 1035 1036 switch (v->export) 1037 { 1038 case v_default: 1039 if (v->origin == o_default || v->origin == o_automatic) 1040 /* Only export default variables by explicit request. */ 1041 continue; 1006 1042 1007 1043 /* The variable doesn't have a name that can be exported. */ … … 1009 1045 continue; 1010 1046 1011 if (! export_all_variables 1012 && v->origin != o_command 1013 && v->origin != o_env && v->origin != o_env_override) 1014 continue; 1015 break; 1016 1017 case v_export: 1018 break; 1019 1020 case v_noexport: 1021 { 1022 /* If this is the SHELL variable and it's not exported, 1023 then add the value from our original environment, if 1024 the original environment defined a value for SHELL. */ 1025 extern struct variable shell_var; 1026 if (streq (v->name, "SHELL") && shell_var.value) 1027 { 1028 v = &shell_var; 1029 break; 1030 } 1031 continue; 1032 } 1033 1034 case v_ifset: 1035 if (v->origin == o_default) 1036 continue; 1037 break; 1038 } 1039 1040 new_slot = (struct variable **) hash_find_slot (&table, v); 1041 if (HASH_VACANT (*new_slot)) 1042 hash_insert_at (&table, v, new_slot); 1043 } 1044 } 1045 1046 makelevel_key.name = MAKELEVEL_NAME; 1047 if (! export_all_variables 1048 && v->origin != o_command 1049 && v->origin != o_env && v->origin != o_env_override) 1050 continue; 1051 break; 1052 1053 case v_export: 1054 break; 1055 1056 case v_noexport: 1057 { 1058 /* If this is the SHELL variable and it's not exported, 1059 then add the value from our original environment, if 1060 the original environment defined a value for SHELL. */ 1061 if (streq (v->name, "SHELL") && shell_var.value) 1062 { 1063 v = &shell_var; 1064 break; 1065 } 1066 continue; 1067 } 1068 1069 case v_ifset: 1070 if (v->origin == o_default) 1071 continue; 1072 break; 1073 } 1074 1075 new_slot = (struct variable **) hash_find_slot (&table, v); 1076 if (HASH_VACANT (*new_slot)) 1077 hash_insert_at (&table, v, new_slot); 1078 } 1079 } 1080 1081 makelevel_key.name = (char *)MAKELEVEL_NAME; 1047 1082 makelevel_key.length = MAKELEVEL_LENGTH; 1048 1083 hash_delete (&table, &makelevel_key); … … 1055 1090 if (! HASH_VACANT (*v_slot)) 1056 1091 { 1057 1058 1059 1060 1061 1062 1063 1064 1065 1092 struct variable *v = *v_slot; 1093 1094 /* If V is recursively expanded and didn't come from the environment, 1095 expand its value. If it came from the environment, it should 1096 go back into the environment unchanged. */ 1097 if (v->recursive 1098 && v->origin != o_env && v->origin != o_env_override) 1099 { 1100 char *value = recursively_expand_for_file (v, file); 1066 1101 #ifdef WINDOWS32 1067 if (strcmp(v->name, "Path") == 0 ||1068 strcmp(v->name, "PATH") == 0)1069 convert_Path_to_windows32(value, ';');1102 if (strcmp (v->name, "Path") == 0 || 1103 strcmp (v->name, "PATH") == 0) 1104 convert_Path_to_windows32 (value, ';'); 1070 1105 #endif 1071 1072 1073 1074 1075 1106 *result++ = xstrdup (concat (3, v->name, "=", value)); 1107 free (value); 1108 } 1109 else 1110 { 1076 1111 #ifdef WINDOWS32 1077 if (strcmp (v->name, "Path") == 0 ||1078 strcmp (v->name, "PATH") == 0)1079 convert_Path_to_windows32 (v->value, ';');1112 if (strcmp (v->name, "Path") == 0 || 1113 strcmp (v->name, "PATH") == 0) 1114 convert_Path_to_windows32 (v->value, ';'); 1080 1115 #endif 1081 1082 1116 *result++ = xstrdup (concat (3, v->name, "=", v->value)); 1117 } 1083 1118 } 1084 1119 … … 1108 1143 1109 1144 1145 /* Given a string, shell-execute it and return a malloc'ed string of the 1146 * result. This removes only ONE newline (if any) at the end, for maximum 1147 * compatibility with the *BSD makes. If it fails, returns NULL. */ 1148 1149 static char * 1150 shell_result (const char *p) 1151 { 1152 char *buf; 1153 unsigned int len; 1154 char *args[2]; 1155 char *result; 1156 1157 install_variable_buffer (&buf, &len); 1158 1159 args[0] = (char *) p; 1160 args[1] = NULL; 1161 variable_buffer_output (func_shell_base (variable_buffer, args, 0), "\0", 1); 1162 result = strdup (variable_buffer); 1163 1164 restore_variable_buffer (buf, len); 1165 return result; 1166 } 1167 1168 1110 1169 /* Given a variable, a value, and a flavor, define the variable. 1111 1170 See the try_variable_definition() function for details on the parameters. */ 1112 1171 1113 1172 struct variable * 1114 do_variable_definition (const structfloc *flocp, const char *varname,1173 do_variable_definition (const floc *flocp, const char *varname, 1115 1174 const char *value, enum variable_origin origin, 1116 1175 enum variable_flavor flavor, int target_var) … … 1133 1192 /* A simple variable definition "var := value". Expand the value. 1134 1193 We have to allocate memory since otherwise it'll clobber the 1135 1194 variable buffer, and we may still need that if we're looking at a 1136 1195 target-specific variable. */ 1137 1196 p = alloc_value = allocated_variable_expand (value); 1138 1197 break; 1198 case f_shell: 1199 { 1200 /* A shell definition "var != value". Expand value, pass it to 1201 the shell, and store the result in recursively-expanded var. */ 1202 char *q = allocated_variable_expand (value); 1203 p = alloc_value = shell_result (q); 1204 free (q); 1205 flavor = f_recursive; 1206 break; 1207 } 1139 1208 case f_conditional: 1140 1209 /* A conditional variable definition "var ?= value". … … 1149 1218 case f_recursive: 1150 1219 /* A recursive variable definition "var = value". 1151 1220 The value is used verbatim. */ 1152 1221 p = value; 1153 1222 break; … … 1205 1274 memcpy (&alloc_value[oldlen + 1], val, vallen + 1); 1206 1275 1207 if (tp) 1208 free (tp); 1276 free (tp); 1209 1277 } 1210 1278 } … … 1214 1282 /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but 1215 1283 non-Unix systems don't conform to this default configuration (in 1216 fact, most of them don't even have `/bin'). On the other hand,1284 fact, most of them don't even have '/bin'). On the other hand, 1217 1285 $SHELL in the environment, if set, points to the real pathname of 1218 1286 the shell. … … 1233 1301 /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc. */ 1234 1302 if (__dosexec_find_on_path (p, NULL, shellpath)) 1235 1236 1237 1238 1303 { 1304 char *tp; 1305 1306 for (tp = shellpath; *tp; tp++) 1239 1307 if (*tp == '\\') 1240 1308 *tp = '/'; 1241 1309 1242 1310 v = define_variable_loc (varname, strlen (varname), 1243 1311 shellpath, origin, flavor == f_recursive, 1244 1312 flocp); 1245 1313 } 1246 1314 else 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1315 { 1316 const char *shellbase, *bslash; 1317 struct variable *pathv = lookup_variable ("PATH", 4); 1318 char *path_string; 1319 char *fake_env[2]; 1320 size_t pathlen = 0; 1321 1322 shellbase = strrchr (p, '/'); 1323 bslash = strrchr (p, '\\'); 1324 if (!shellbase || bslash > shellbase) 1325 shellbase = bslash; 1326 if (!shellbase && p[1] == ':') 1327 shellbase = p + 1; 1328 if (shellbase) 1329 shellbase++; 1330 else 1331 shellbase = p; 1332 1333 /* Search for the basename of the shell (with standard 1334 executable extensions) along the $PATH. */ 1335 if (pathv) 1336 pathlen = strlen (pathv->value); 1337 path_string = xmalloc (5 + pathlen + 2 + 1); 1338 /* On MSDOS, current directory is considered as part of $PATH. */ 1339 sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : ""); 1340 fake_env[0] = path_string; 1341 fake_env[1] = 0; 1342 if (__dosexec_find_on_path (shellbase, fake_env, shellpath)) 1343 { 1344 char *tp; 1345 1346 for (tp = shellpath; *tp; tp++) 1279 1347 if (*tp == '\\') 1280 1348 *tp = '/'; 1281 1349 1282 1350 v = define_variable_loc (varname, strlen (varname), 1283 1351 shellpath, origin, 1284 1352 flavor == f_recursive, flocp); 1285 1286 1287 1288 1289 1290 1353 } 1354 else 1355 v = lookup_variable (varname, strlen (varname)); 1356 1357 free (path_string); 1358 } 1291 1359 } 1292 1360 else … … 1296 1364 && streq (varname, "SHELL")) 1297 1365 { 1298 extern c har *default_shell;1366 extern const char *default_shell; 1299 1367 1300 1368 /* Call shell locator function. If it returns TRUE, then 1301 1369 set no_default_sh_exe to indicate sh was found and 1302 1370 set new value for SHELL variable. */ 1303 1371 … … 1331 1399 v = lookup_variable (varname, strlen (varname)); 1332 1400 1333 if (tp) 1334 free (tp); 1401 free (tp); 1335 1402 } 1336 1403 } … … 1352 1419 v->conditional = conditional; 1353 1420 1354 if (alloc_value) 1355 free (alloc_value); 1421 free (alloc_value); 1356 1422 1357 1423 return v->special ? set_special_var (v) : v; … … 1361 1427 /* Parse P (a null-terminated string) as a variable definition. 1362 1428 1363 If it is not a variable definition, return NULL. 1429 If it is not a variable definition, return NULL and the contents of *VAR 1430 are undefined, except NAME is set to the first non-space character or NIL. 1364 1431 1365 1432 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. */ 1433 assignment token and set the following fields (only) of *VAR: 1434 name : name of the variable (ALWAYS SET) (NOT NUL-TERMINATED!) 1435 length : length of the variable name 1436 value : value of the variable (nul-terminated) 1437 flavor : flavor of the variable 1438 Other values in *VAR are unchanged. 1439 */ 1367 1440 1368 1441 char * 1369 parse_variable_definition (const char *p, enum variable_flavor *flavor)1442 parse_variable_definition (const char *p, struct variable *var) 1370 1443 { 1371 1444 int wspace = 0; 1372 1373 p = next_token (p); 1445 const char *e = NULL; 1446 1447 NEXT_TOKEN (p); 1448 var->name = (char *)p; 1449 var->length = 0; 1374 1450 1375 1451 while (1) … … 1378 1454 1379 1455 /* If we find a comment or EOS, it's not a variable definition. */ 1380 if ( c == '\0' || c == '#')1381 1456 if (STOP_SET (c, MAP_COMMENT|MAP_NUL)) 1457 return NULL; 1382 1458 1383 1459 if (c == '$') 1384 { 1385 /* This begins a variable expansion reference. Make sure we don't 1386 treat chars inside the reference as assignment tokens. */ 1387 char closeparen; 1388 int count; 1389 c = *p++; 1390 if (c == '(') 1391 closeparen = ')'; 1392 else if (c == '{') 1393 closeparen = '}'; 1394 else 1460 { 1461 /* This begins a variable expansion reference. Make sure we don't 1462 treat chars inside the reference as assignment tokens. */ 1463 char closeparen; 1464 unsigned int count; 1465 1466 c = *p++; 1467 if (c == '(') 1468 closeparen = ')'; 1469 else if (c == '{') 1470 closeparen = '}'; 1471 else if (c == '\0') 1472 return NULL; 1473 else 1395 1474 /* '$$' or '$X'. Either way, nothing special to do here. */ 1396 continue; 1397 1398 /* P now points past the opening paren or brace. 1399 Count parens or braces until it is matched. */ 1400 count = 0; 1401 for (; *p != '\0'; ++p) 1402 { 1403 if (*p == c) 1404 ++count; 1405 else if (*p == closeparen && --count < 0) 1406 { 1407 ++p; 1408 break; 1409 } 1410 } 1475 continue; 1476 1477 /* P now points past the opening paren or brace. 1478 Count parens or braces until it is matched. */ 1479 for (count = 1; *p != '\0'; ++p) 1480 { 1481 if (*p == closeparen && --count == 0) 1482 { 1483 ++p; 1484 break; 1485 } 1486 if (*p == c) 1487 ++count; 1488 } 1411 1489 continue; 1412 1490 } 1413 1491 1414 1492 /* If we find whitespace skip it, and remember we found it. */ 1415 if ( isblank ((unsigned char)c))1493 if (ISBLANK (c)) 1416 1494 { 1417 1495 wspace = 1; 1418 p = next_token (p); 1496 e = p - 1; 1497 NEXT_TOKEN (p); 1419 1498 c = *p; 1420 1499 if (c == '\0') … … 1425 1504 1426 1505 if (c == '=') 1427 { 1428 *flavor = f_recursive; 1429 return (char *)p; 1430 } 1431 1432 /* Match assignment variants (:=, +=, ?=) */ 1506 { 1507 var->flavor = f_recursive; 1508 if (! e) 1509 e = p - 1; 1510 break; 1511 } 1512 1513 /* Match assignment variants (:=, +=, ?=, !=) */ 1433 1514 if (*p == '=') 1434 1515 { … … 1436 1517 { 1437 1518 case ':': 1438 *flavor = f_simple;1519 var->flavor = f_simple; 1439 1520 break; 1440 1521 case '+': 1441 *flavor = f_append;1522 var->flavor = f_append; 1442 1523 break; 1443 1524 case '?': 1444 *flavor = f_conditional; 1525 var->flavor = f_conditional; 1526 break; 1527 case '!': 1528 var->flavor = f_shell; 1445 1529 break; 1446 1530 default: … … 1452 1536 continue; 1453 1537 } 1454 return (char *)++p; 1538 if (! e) 1539 e = p - 1; 1540 ++p; 1541 break; 1455 1542 } 1456 else if (c == ':') 1457 /* A colon other than := is a rule line, not a variable defn. */ 1458 return NULL; 1543 1544 /* Check for POSIX ::= syntax */ 1545 if (c == ':') 1546 { 1547 /* A colon other than :=/::= is not a variable defn. */ 1548 if (*p != ':' || p[1] != '=') 1549 return NULL; 1550 1551 /* POSIX allows ::= to be the same as GNU make's := */ 1552 var->flavor = f_simple; 1553 if (! e) 1554 e = p - 1; 1555 p += 2; 1556 break; 1557 } 1459 1558 1460 1559 /* If we skipped whitespace, non-assignments means no var. */ … … 1463 1562 } 1464 1563 1564 var->length = e - var->name; 1565 var->value = next_token (p); 1465 1566 return (char *)p; 1466 1567 } … … 1469 1570 /* Try to interpret LINE (a null-terminated string) as a variable definition. 1470 1571 1471 If LINE was recognized as a variable definition, a pointer to its `struct1572 If LINE was recognized as a variable definition, a pointer to its 'struct 1472 1573 variable' is returned. If LINE is not a variable definition, NULL is 1473 1574 returned. */ 1474 1575 1475 1576 struct variable * 1476 assign_variable_definition (struct variable *v, char *line) 1477 { 1478 char *beg; 1479 char *end; 1480 enum variable_flavor flavor; 1577 assign_variable_definition (struct variable *v, const char *line) 1578 { 1481 1579 char *name; 1482 1580 1483 beg = next_token (line); 1484 line = parse_variable_definition (beg, &flavor); 1485 if (!line) 1581 if (!parse_variable_definition (line, v)) 1486 1582 return NULL; 1487 1583 1488 end = line - (flavor == f_recursive ? 1 : 2);1489 while (end > beg && isblank ((unsigned char)end[-1]))1490 --end;1491 line = next_token (line);1492 v->value = line;1493 v->flavor = flavor;1494 1495 1584 /* Expand the name, so "$(foo)bar = baz" works. */ 1496 name = alloca ( end - beg+ 1);1497 memcpy (name, beg, end - beg);1498 name[ end - beg] = '\0';1585 name = alloca (v->length + 1); 1586 memcpy (name, v->name, v->length); 1587 name[v->length] = '\0'; 1499 1588 v->name = allocated_variable_expand (name); 1500 1589 1501 1590 if (v->name[0] == '\0') 1502 fatal (&v->fileinfo, _("empty variable name"));1591 O (fatal, &v->fileinfo, _("empty variable name")); 1503 1592 1504 1593 return v; … … 1515 1604 See the comments for assign_variable_definition(). 1516 1605 1517 If LINE was recognized as a variable definition, a pointer to its `struct1606 If LINE was recognized as a variable definition, a pointer to its 'struct 1518 1607 variable' is returned. If LINE is not a variable definition, NULL is 1519 1608 returned. */ 1520 1609 1521 1610 struct variable * 1522 try_variable_definition (const struct floc *flocp,char *line,1611 try_variable_definition (const floc *flocp, const char *line, 1523 1612 enum variable_origin origin, int target_var) 1524 1613 { … … 1554 1643 switch (v->origin) 1555 1644 { 1645 case o_automatic: 1646 origin = _("automatic"); 1647 break; 1556 1648 case o_default: 1557 1649 origin = _("default"); … … 1570 1662 break; 1571 1663 case o_override: 1572 origin = _("`override' directive"); 1573 break; 1574 case o_automatic: 1575 origin = _("automatic"); 1664 origin = _("'override' directive"); 1576 1665 break; 1577 1666 case o_invalid: … … 1584 1673 fputs (" private", stdout); 1585 1674 if (v->fileinfo.filenm) 1586 printf (_(" (from `%s', line %lu)"),1587 v->fileinfo.filenm, v->fileinfo.lineno );1675 printf (_(" (from '%s', line %lu)"), 1676 v->fileinfo.filenm, v->fileinfo.lineno + v->fileinfo.offset); 1588 1677 putchar ('\n'); 1589 1678 fputs (prefix, stdout); 1590 1679 1591 /* Is this a `define'? */1680 /* Is this a 'define'? */ 1592 1681 if (v->recursive && strchr (v->value, '\n') != 0) 1593 1682 printf ("define %s\n%s\nendef\n", v->name, v->value); … … 1601 1690 p = next_token (v->value); 1602 1691 if (p != v->value && *p == '\0') 1603 1604 1692 /* All whitespace. */ 1693 printf ("$(subst ,,%s)", v->value); 1605 1694 else if (v->recursive) 1606 1695 fputs (v->value, stdout); 1607 1696 else 1608 1609 1610 1611 1612 1613 1614 1697 /* Double up dollar signs. */ 1698 for (p = v->value; *p != '\0'; ++p) 1699 { 1700 if (*p == '$') 1701 putchar ('$'); 1702 putchar (*p); 1703 } 1615 1704 putchar ('\n'); 1616 1705 } 1706 } 1707 1708 1709 static void 1710 print_auto_variable (const void *item, void *arg) 1711 { 1712 const struct variable *v = item; 1713 1714 if (v->origin == o_automatic) 1715 print_variable (item, arg); 1716 } 1717 1718 1719 static void 1720 print_noauto_variable (const void *item, void *arg) 1721 { 1722 const struct variable *v = item; 1723 1724 if (v->origin != o_automatic) 1725 print_variable (item, arg); 1617 1726 } 1618 1727 … … 1621 1730 the actual variable definitions (everything else is comments). */ 1622 1731 1623 void 1624 print_variable_set (struct variable_set *set, char *prefix) 1625 { 1626 hash_map_arg (&set->table, print_variable, prefix); 1732 static void 1733 print_variable_set (struct variable_set *set, const char *prefix, int pauto) 1734 { 1735 hash_map_arg (&set->table, (pauto ? print_auto_variable : print_variable), 1736 (void *)prefix); 1627 1737 1628 1738 fputs (_("# variable set hash-table stats:\n"), stdout); … … 1639 1749 puts (_("\n# Variables\n")); 1640 1750 1641 print_variable_set (&global_variable_set, "" );1751 print_variable_set (&global_variable_set, "", 0); 1642 1752 1643 1753 puts (_("\n# Pattern-specific Variable Values")); … … 1645 1755 { 1646 1756 struct pattern_var *p; 1647 int rules = 0;1757 unsigned int rules = 0; 1648 1758 1649 1759 for (p = pattern_vars; p != 0; p = p->next) … … 1651 1761 ++rules; 1652 1762 printf ("\n%s :\n", p->target); 1653 print_variable (&p->variable, "# ");1763 print_variable (&p->variable, (void *)"# "); 1654 1764 } 1655 1765 … … 1668 1778 { 1669 1779 if (file->variables != 0) 1670 print_variable_set (file->variables->set, "# "); 1780 print_variable_set (file->variables->set, "# ", 1); 1781 } 1782 1783 void 1784 print_target_variables (const struct file *file) 1785 { 1786 if (file->variables != 0) 1787 { 1788 int l = strlen (file->name); 1789 char *t = alloca (l + 3); 1790 1791 strcpy (t, file->name); 1792 t[l] = ':'; 1793 t[l+1] = ' '; 1794 t[l+2] = '\0'; 1795 1796 hash_map_arg (&file->variables->set->table, print_noauto_variable, t); 1797 } 1671 1798 } 1672 1799 … … 1681 1808 return; 1682 1809 1683 /* 1684 * If done this before, don't leak memory unnecessarily. 1685 * Free the previous entry before allocating new one. 1686 */ 1687 if (environ_path) 1688 free (environ_path); 1689 1690 /* 1691 * Create something WINDOWS32 world can grok 1692 */ 1810 /* If done this before, free the previous entry before allocating new one. */ 1811 free (environ_path); 1812 1813 /* Create something WINDOWS32 world can grok. */ 1693 1814 convert_Path_to_windows32 (path, ';'); 1694 1815 environ_path = xstrdup (concat (3, "PATH", "=", path));
Note:
See TracChangeset
for help on using the changeset viewer.