#include "make.h"
#include <assert.h>
#include "dep.h"
#include "filedef.h"
#include "job.h"
#include "commands.h"
#include "variable.h"
#include "rule.h"
#ifdef WINDOWS32
#include "pathstuff.h"
#endif
#include "hash.h"
static struct pattern_var *pattern_vars;
static struct pattern_var *last_pattern_var;
struct pattern_var *
create_pattern_var (char *target, char *suffix)
{
register struct pattern_var *p
= (struct pattern_var *) xmalloc (sizeof (struct pattern_var));
if (last_pattern_var != 0)
last_pattern_var->next = p;
else
pattern_vars = p;
last_pattern_var = p;
p->next = 0;
p->target = target;
p->len = strlen (target);
p->suffix = suffix + 1;
return p;
}
static struct pattern_var *
lookup_pattern_var (struct pattern_var *start, char *target)
{
struct pattern_var *p;
unsigned int targlen = strlen(target);
for (p = start ? start->next : pattern_vars; p != 0; p = p->next)
{
char *stem;
unsigned int stemlen;
if (p->len > targlen)
continue;
stem = target + (p->suffix - p->target - 1);
stemlen = targlen - p->len + 1;
if (stem > target && !strneq (p->target, target, stem - target))
continue;
if (*p->suffix == stem[stemlen]
&& (*p->suffix == '\0' || streq (&p->suffix[1], &stem[stemlen+1])))
break;
}
return p;
}
static unsigned long
variable_hash_1 (const void *keyv)
{
struct variable const *key = (struct variable const *) keyv;
return_STRING_N_HASH_1 (key->name, key->length);
}
static unsigned long
variable_hash_2 (const void *keyv)
{
struct variable const *key = (struct variable const *) keyv;
return_STRING_N_HASH_2 (key->name, key->length);
}
static int
variable_hash_cmp (const void *xv, const void *yv)
{
struct variable const *x = (struct variable const *) xv;
struct variable const *y = (struct variable const *) yv;
int result = x->length - y->length;
if (result)
return result;
return_STRING_N_COMPARE (x->name, y->name, x->length);
}
#ifndef VARIABLE_BUCKETS
#define VARIABLE_BUCKETS 523
#endif
#ifndef PERFILE_VARIABLE_BUCKETS
#define PERFILE_VARIABLE_BUCKETS 23
#endif
#ifndef SMALL_SCOPE_VARIABLE_BUCKETS
#define SMALL_SCOPE_VARIABLE_BUCKETS 13
#endif
static struct variable_set global_variable_set;
static struct variable_set_list global_setlist
= { 0, &global_variable_set };
struct variable_set_list *current_variable_set_list = &global_setlist;
#if defined(__APPLE__) || defined(NeXT) || defined(NeXT_PDO)
static void check_apple_pb_support (name, length, value)
const char *name;
unsigned int length;
char *value;
{
char *p;
if (length == 20 && !strncmp (name, "USE_APPLE_PB_SUPPORT", length)) {
for (p = value; *p != '\0'; p++) {
if (isspace (*p)) {
continue;
}
if (!strncmp (p, "all", 3)) {
p += 3;
next_flag |= NEXT_ALL_FLAGS;
} else if (!strncmp (p, "vpath", 5)) {
p += 5;
next_flag |= NEXT_VPATH_FLAG;
} else if (!strncmp (p, "quiet", 5)) {
p += 5;
next_flag |= NEXT_QUIET_FLAG;
} else if (!strncmp (p, "makefiles", 9)) {
p += 9;
next_flag |= NEXT_MAKEFILES_FLAG;
} else if (!strncmp (p, "errexit", 7)) {
p += 7;
next_flag |= NEXT_ERREXIT_FLAG;
}
}
}
}
#endif
void
init_hash_global_variable_set (void)
{
hash_init (&global_variable_set.table, VARIABLE_BUCKETS,
variable_hash_1, variable_hash_2, variable_hash_cmp);
}
struct variable *
define_variable_in_set (const char *name, unsigned int length,
char *value, enum variable_origin origin,
int recursive, struct variable_set *set,
const struct floc *flocp)
{
struct variable *v;
struct variable **var_slot;
struct variable var_key;
#if defined(__APPLE__) || defined(NeXT) || defined(NeXT_PDO)
check_apple_pb_support (name, length, value);
#endif
if (set == NULL)
set = &global_variable_set;
var_key.name = (char *) name;
var_key.length = length;
var_slot = (struct variable **) hash_find_slot (&set->table, &var_key);
if (env_overrides && origin == o_env)
origin = o_env_override;
v = *var_slot;
if (! HASH_VACANT (v))
{
if (env_overrides && v->origin == o_env)
v->origin = o_env_override;
if ((int) origin >= (int) v->origin)
{
if (v->value != 0)
free (v->value);
v->value = xstrdup (value);
if (flocp != 0)
v->fileinfo = *flocp;
else
v->fileinfo.filenm = 0;
v->origin = origin;
v->recursive = recursive;
}
return v;
}
v = (struct variable *) xmalloc (sizeof (struct variable));
v->name = savestring (name, length);
v->length = length;
hash_insert_at (&set->table, v, var_slot);
v->value = xstrdup (value);
if (flocp != 0)
v->fileinfo = *flocp;
else
v->fileinfo.filenm = 0;
v->origin = origin;
v->recursive = recursive;
v->special = 0;
v->expanding = 0;
v->exp_count = 0;
v->per_target = 0;
v->append = 0;
v->export = v_default;
v->exportable = 1;
if (*name != '_' && (*name < 'A' || *name > 'Z')
&& (*name < 'a' || *name > 'z'))
v->exportable = 0;
else
{
for (++name; *name != '\0'; ++name)
if (*name != '_' && (*name < 'a' || *name > 'z')
&& (*name < 'A' || *name > 'Z') && !ISDIGIT(*name))
break;
if (*name != '\0')
v->exportable = 0;
}
return v;
}
#define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
static struct variable *
handle_special_var (struct variable *var)
{
static unsigned long last_var_count = 0;
if (streq (var->name, ".VARIABLES")
&& global_variable_set.table.ht_fill != last_var_count)
{
unsigned long max = EXPANSION_INCREMENT (strlen (var->value));
unsigned long len;
char *p;
struct variable **vp = (struct variable **) global_variable_set.table.ht_vec;
struct variable **end = &vp[global_variable_set.table.ht_size];
var->value = xrealloc (var->value, max);
p = var->value;
len = 0;
for (; vp < end; ++vp)
if (!HASH_VACANT (*vp))
{
struct variable *v = *vp;
int l = v->length;
len += l + 1;
if (len > max)
{
unsigned long off = p - var->value;
max += EXPANSION_INCREMENT (l + 1);
var->value = xrealloc (var->value, max);
p = &var->value[off];
}
bcopy (v->name, p, l);
p += l;
*(p++) = ' ';
}
*(p-1) = '\0';
last_var_count = global_variable_set.table.ht_fill;
}
return var;
}
struct variable *
lookup_variable (const char *name, unsigned int length)
{
const struct variable_set_list *setlist;
struct variable var_key;
var_key.name = (char *) name;
var_key.length = length;
for (setlist = current_variable_set_list;
setlist != 0; setlist = setlist->next)
{
const struct variable_set *set = setlist->set;
struct variable *v;
v = (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
if (v)
return v->special ? handle_special_var (v) : v;
}
#ifdef VMS
{
char *vname = alloca (length + 1);
char *value;
strncpy (vname, name, length);
vname[length] = 0;
value = getenv (vname);
if (value != 0)
{
char *sptr;
int scnt;
sptr = value;
scnt = 0;
while ((sptr = strchr (sptr, '$')))
{
scnt++;
sptr++;
}
if (scnt > 0)
{
char *nvalue;
char *nptr;
nvalue = alloca (strlen (value) + scnt + 1);
sptr = value;
nptr = nvalue;
while (*sptr)
{
if (*sptr == '$')
{
*nptr++ = '$';
*nptr++ = '$';
}
else
{
*nptr++ = *sptr;
}
sptr++;
}
*nptr = '\0';
return define_variable (vname, length, nvalue, o_env, 1);
}
return define_variable (vname, length, value, o_env, 1);
}
}
#endif
return 0;
}
struct variable *
lookup_variable_in_set (const char *name, unsigned int length,
const struct variable_set *set)
{
struct variable var_key;
var_key.name = (char *) name;
var_key.length = length;
return (struct variable *) hash_find_item ((struct hash_table *) &set->table, &var_key);
}
void
initialize_file_variables (struct file *file, int reading)
{
struct variable_set_list *l = file->variables;
if (l == 0)
{
l = (struct variable_set_list *)
xmalloc (sizeof (struct variable_set_list));
l->set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
hash_init (&l->set->table, PERFILE_VARIABLE_BUCKETS,
variable_hash_1, variable_hash_2, variable_hash_cmp);
file->variables = l;
}
if (file->double_colon && file->double_colon != file)
{
initialize_file_variables (file->double_colon, reading);
l->next = file->double_colon->variables;
return;
}
if (file->parent == 0)
l->next = &global_setlist;
else
{
initialize_file_variables (file->parent, reading);
l->next = file->parent->variables;
}
if (!reading && !file->pat_searched)
{
struct pattern_var *p;
p = lookup_pattern_var (0, file->name);
if (p != 0)
{
struct variable_set_list *global = current_variable_set_list;
file->pat_variables = create_new_variable_set ();
current_variable_set_list = file->pat_variables;
do
{
struct variable *v;
if (p->variable.flavor == f_simple)
{
v = define_variable_loc (
p->variable.name, strlen (p->variable.name),
p->variable.value, p->variable.origin,
0, &p->variable.fileinfo);
v->flavor = f_simple;
}
else
{
v = do_variable_definition (
&p->variable.fileinfo, p->variable.name,
p->variable.value, p->variable.origin,
p->variable.flavor, 1);
}
v->per_target = p->variable.per_target;
v->export = p->variable.export;
}
while ((p = lookup_pattern_var (p, file->name)) != 0);
current_variable_set_list = global;
}
file->pat_searched = 1;
}
if (file->pat_variables != 0)
{
file->pat_variables->next = l->next;
l->next = file->pat_variables;
}
}
struct variable_set_list *
create_new_variable_set (void)
{
register struct variable_set_list *setlist;
register struct variable_set *set;
set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
hash_init (&set->table, SMALL_SCOPE_VARIABLE_BUCKETS,
variable_hash_1, variable_hash_2, variable_hash_cmp);
setlist = (struct variable_set_list *)
xmalloc (sizeof (struct variable_set_list));
setlist->set = set;
setlist->next = current_variable_set_list;
return setlist;
}
static void
free_variable_name_and_value (const void *item)
{
struct variable *v = (struct variable *) item;
free (v->name);
free (v->value);
}
void
free_variable_set (struct variable_set_list *list)
{
hash_map (&list->set->table, free_variable_name_and_value);
hash_free (&list->set->table, 1);
free ((char *) list->set);
free ((char *) list);
}
struct variable_set_list *
push_new_variable_scope (void)
{
current_variable_set_list = create_new_variable_set();
if (current_variable_set_list->next == &global_setlist)
{
struct variable_set *set = current_variable_set_list->set;
current_variable_set_list->set = global_setlist.set;
global_setlist.set = set;
current_variable_set_list->next = global_setlist.next;
global_setlist.next = current_variable_set_list;
current_variable_set_list = &global_setlist;
}
return (current_variable_set_list);
}
void
pop_variable_scope (void)
{
struct variable_set_list *setlist;
struct variable_set *set;
assert(current_variable_set_list->next != NULL);
if (current_variable_set_list != &global_setlist)
{
setlist = current_variable_set_list;
set = setlist->set;
current_variable_set_list = setlist->next;
}
else
{
setlist = global_setlist.next;
set = global_setlist.set;
global_setlist.set = setlist->set;
global_setlist.next = setlist->next;
}
free ((char *) setlist);
hash_map (&set->table, free_variable_name_and_value);
hash_free (&set->table, 1);
free ((char *) set);
}
static void
merge_variable_sets (struct variable_set *to_set,
struct variable_set *from_set)
{
struct variable **from_var_slot = (struct variable **) from_set->table.ht_vec;
struct variable **from_var_end = from_var_slot + from_set->table.ht_size;
for ( ; from_var_slot < from_var_end; from_var_slot++)
if (! HASH_VACANT (*from_var_slot))
{
struct variable *from_var = *from_var_slot;
struct variable **to_var_slot
= (struct variable **) hash_find_slot (&to_set->table, *from_var_slot);
if (HASH_VACANT (*to_var_slot))
hash_insert_at (&to_set->table, from_var, to_var_slot);
else
{
free (from_var->value);
free (from_var);
}
}
}
void
merge_variable_set_lists (struct variable_set_list **setlist0,
struct variable_set_list *setlist1)
{
struct variable_set_list *to = *setlist0;
struct variable_set_list *last0 = 0;
if (!setlist1)
return;
if (to)
while (setlist1 != &global_setlist && to != &global_setlist)
{
struct variable_set_list *from = setlist1;
setlist1 = setlist1->next;
merge_variable_sets (to->set, from->set);
last0 = to;
to = to->next;
}
if (setlist1 != &global_setlist)
{
if (last0 == 0)
*setlist0 = setlist1;
else
last0->next = setlist1;
}
}
void
define_automatic_variables (void)
{
#if defined(WINDOWS32) || defined(__EMX__)
extern char* default_shell;
#else
extern char default_shell[];
#endif
register struct variable *v;
char buf[200];
sprintf (buf, "%u", makelevel);
(void) define_variable (MAKELEVEL_NAME, MAKELEVEL_LENGTH, buf, o_env, 0);
sprintf (buf, "%s%s%s",
version_string,
(remote_description == 0 || remote_description[0] == '\0')
? "" : "-",
(remote_description == 0 || remote_description[0] == '\0')
? "" : remote_description);
(void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
#ifdef __MSDOS__
{
static char shell_str[] = "SHELL";
const int shlen = sizeof (shell_str) - 1;
struct variable *mshp = lookup_variable ("MAKESHELL", 9);
struct variable *comp = lookup_variable ("COMSPEC", 7);
if (mshp)
(void) define_variable (shell_str, shlen,
mshp->value, o_env_override, 0);
else if (comp)
{
struct variable *shp = lookup_variable (shell_str, shlen);
if (!shp)
(void) define_variable (shell_str, shlen, comp->value, o_env, 0);
}
}
#elif defined(__EMX__)
{
static char shell_str[] = "SHELL";
const int shlen = sizeof (shell_str) - 1;
struct variable *shell = lookup_variable (shell_str, shlen);
struct variable *replace = lookup_variable ("MAKESHELL", 9);
if (replace && *replace->value && replace->origin == o_env)
replace->origin = o_env_override;
if (!replace || !*replace->value)
if (shell && *shell->value && (shell->origin == o_env
|| shell->origin == o_env_override))
{
free(shell->value);
shell->value = xstrdup (default_shell);
shell->origin = o_default;
}
# ifndef NO_CMD_DEFAULT
if (!replace || !*replace->value)
replace = lookup_variable ("COMSPEC", 7);
if (!replace || !*replace->value)
replace = lookup_variable ("OS2_SHELL", 9);
# else
# warning NO_CMD_DEFAULT: GNU make will not use CMD.EXE as default shell
# endif
if (replace && *replace->value)
(void) define_variable (shell_str, shlen, replace->value,
replace->origin, 0);
else
(void) define_variable (shell_str, shlen, default_shell,
o_default, 0);
}
#endif
v = define_variable ("SHELL", 5, default_shell, o_default, 0);
#if !defined(__MSDOS__) && !defined(__EMX__)
if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
{
free (v->value);
v->origin = o_file;
v->value = xstrdup (default_shell);
}
#endif
v = define_variable ("MAKEFILES", 9, "", o_default, 0);
v->export = v_ifset;
#ifdef VMS
define_variable ("@D", 2, "$(dir $@)", o_automatic, 1);
define_variable ("%D", 2, "$(dir $%)", o_automatic, 1);
define_variable ("*D", 2, "$(dir $*)", o_automatic, 1);
define_variable ("<D", 2, "$(dir $<)", o_automatic, 1);
define_variable ("?D", 2, "$(dir $?)", o_automatic, 1);
define_variable ("^D", 2, "$(dir $^)", o_automatic, 1);
define_variable ("+D", 2, "$(dir $+)", o_automatic, 1);
#else
define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
#endif
define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
}
int export_all_variables;
char **
target_environment (struct file *file)
{
struct variable_set_list *set_list;
register struct variable_set_list *s;
struct hash_table table;
struct variable **v_slot;
struct variable **v_end;
struct variable makelevel_key;
char **result_0;
char **result;
if (file == 0)
set_list = current_variable_set_list;
else
set_list = file->variables;
hash_init (&table, VARIABLE_BUCKETS,
variable_hash_1, variable_hash_2, variable_hash_cmp);
for (s = set_list; s != 0; s = s->next)
{
struct variable_set *set = s->set;
v_slot = (struct variable **) set->table.ht_vec;
v_end = v_slot + set->table.ht_size;
for ( ; v_slot < v_end; v_slot++)
if (! HASH_VACANT (*v_slot))
{
struct variable **new_slot;
struct variable *v = *v_slot;
if (v->per_target && v->export == v_default)
{
struct variable *gv;
gv = lookup_variable_in_set (v->name, strlen(v->name),
&global_variable_set);
if (gv)
v->export = gv->export;
}
switch (v->export)
{
case v_default:
if (v->origin == o_default || v->origin == o_automatic)
continue;
if (! v->exportable)
continue;
if (! export_all_variables
&& v->origin != o_command
&& v->origin != o_env && v->origin != o_env_override)
continue;
break;
case v_export:
break;
case v_noexport:
if (streq (v->name, "SHELL"))
{
extern struct variable shell_var;
v = &shell_var;
break;
}
continue;
case v_ifset:
if (v->origin == o_default)
continue;
break;
}
new_slot = (struct variable **) hash_find_slot (&table, v);
if (HASH_VACANT (*new_slot))
hash_insert_at (&table, v, new_slot);
}
}
makelevel_key.name = MAKELEVEL_NAME;
makelevel_key.length = MAKELEVEL_LENGTH;
hash_delete (&table, &makelevel_key);
result = result_0 = (char **) xmalloc ((table.ht_fill + 2) * sizeof (char *));
v_slot = (struct variable **) table.ht_vec;
v_end = v_slot + table.ht_size;
for ( ; v_slot < v_end; v_slot++)
if (! HASH_VACANT (*v_slot))
{
struct variable *v = *v_slot;
if (v->recursive
&& v->origin != o_env && v->origin != o_env_override)
{
char *value = recursively_expand_for_file (v, file);
#ifdef WINDOWS32
if (strcmp(v->name, "Path") == 0 ||
strcmp(v->name, "PATH") == 0)
convert_Path_to_windows32(value, ';');
#endif
*result++ = concat (v->name, "=", value);
free (value);
}
else
{
#ifdef WINDOWS32
if (strcmp(v->name, "Path") == 0 ||
strcmp(v->name, "PATH") == 0)
convert_Path_to_windows32(v->value, ';');
#endif
*result++ = concat (v->name, "=", v->value);
}
}
*result = (char *) xmalloc (100);
(void) sprintf (*result, "%s=%u", MAKELEVEL_NAME, makelevel + 1);
*++result = 0;
hash_free (&table, 0);
return result_0;
}
struct variable *
do_variable_definition (const struct floc *flocp, const char *varname,
char *value, enum variable_origin origin,
enum variable_flavor flavor, int target_var)
{
char *p, *alloc_value = NULL;
struct variable *v;
int append = 0;
int conditional = 0;
switch (flavor)
{
default:
case f_bogus:
abort ();
case f_simple:
p = alloc_value = allocated_variable_expand (value);
break;
case f_conditional:
v = lookup_variable (varname, strlen (varname));
if (v)
return v;
conditional = 1;
flavor = f_recursive;
case f_recursive:
p = value;
break;
case f_append:
{
if (target_var)
{
append = 1;
v = lookup_variable_in_set (varname, strlen (varname),
current_variable_set_list->set);
if (v && !v->append)
append = 0;
}
else
v = lookup_variable (varname, strlen (varname));
if (v == 0)
{
p = value;
flavor = f_recursive;
}
else
{
unsigned int oldlen, vallen;
char *val;
val = value;
if (v->recursive)
flavor = f_recursive;
else
val = alloc_value = allocated_variable_expand (val);
oldlen = strlen (v->value);
vallen = strlen (val);
p = (char *) alloca (oldlen + 1 + vallen + 1);
bcopy (v->value, p, oldlen);
p[oldlen] = ' ';
bcopy (val, &p[oldlen + 1], vallen + 1);
}
}
}
#ifdef __MSDOS__
if ((origin == o_file || origin == o_override)
&& strcmp (varname, "SHELL") == 0)
{
PATH_VAR (shellpath);
extern char * __dosexec_find_on_path (const char *, char *[], char *);
if (__dosexec_find_on_path (p, (char **)0, shellpath))
{
char *p;
for (p = shellpath; *p; p++)
{
if (*p == '\\')
*p = '/';
}
v = define_variable_loc (varname, strlen (varname),
shellpath, origin, flavor == f_recursive,
flocp);
}
else
{
char *shellbase, *bslash;
struct variable *pathv = lookup_variable ("PATH", 4);
char *path_string;
char *fake_env[2];
size_t pathlen = 0;
shellbase = strrchr (p, '/');
bslash = strrchr (p, '\\');
if (!shellbase || bslash > shellbase)
shellbase = bslash;
if (!shellbase && p[1] == ':')
shellbase = p + 1;
if (shellbase)
shellbase++;
else
shellbase = p;
if (pathv)
pathlen = strlen (pathv->value);
path_string = (char *)xmalloc (5 + pathlen + 2 + 1);
sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
fake_env[0] = path_string;
fake_env[1] = (char *)0;
if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
{
char *p;
for (p = shellpath; *p; p++)
{
if (*p == '\\')
*p = '/';
}
v = define_variable_loc (varname, strlen (varname),
shellpath, origin,
flavor == f_recursive, flocp);
}
else
v = lookup_variable (varname, strlen (varname));
free (path_string);
}
}
else
#endif
#ifdef WINDOWS32
if ((origin == o_file || origin == o_override || origin == o_command)
&& streq (varname, "SHELL"))
{
extern char *default_shell;
if (find_and_set_default_shell (p))
{
v = define_variable_in_set (varname, strlen (varname), default_shell,
origin, flavor == f_recursive,
(target_var
? current_variable_set_list->set
: NULL),
flocp);
no_default_sh_exe = 0;
}
else
v = lookup_variable (varname, strlen (varname));
}
else
#endif
v = define_variable_in_set (varname, strlen (varname), p,
origin, flavor == f_recursive,
(target_var
? current_variable_set_list->set : NULL),
flocp);
v->append = append;
v->conditional = conditional;
if (alloc_value)
free (alloc_value);
return v;
}
struct variable *
parse_variable_definition (struct variable *v, char *line)
{
register int c;
register char *p = line;
register char *beg;
register char *end;
enum variable_flavor flavor = f_bogus;
char *name;
while (1)
{
c = *p++;
if (c == '\0' || c == '#')
return 0;
if (c == '=')
{
end = p - 1;
flavor = f_recursive;
break;
}
else if (c == ':')
if (*p == '=')
{
end = p++ - 1;
flavor = f_simple;
break;
}
else
return 0;
else if (c == '+' && *p == '=')
{
end = p++ - 1;
flavor = f_append;
break;
}
else if (c == '?' && *p == '=')
{
end = p++ - 1;
flavor = f_conditional;
break;
}
else if (c == '$')
{
char closeparen;
int count;
c = *p++;
if (c == '(')
closeparen = ')';
else if (c == '{')
closeparen = '}';
else
continue;
count = 0;
for (; *p != '\0'; ++p)
{
if (*p == c)
++count;
else if (*p == closeparen && --count < 0)
{
++p;
break;
}
}
}
}
v->flavor = flavor;
beg = next_token (line);
while (end > beg && isblank ((unsigned char)end[-1]))
--end;
p = next_token (p);
v->value = p;
name = (char *) alloca (end - beg + 1);
bcopy (beg, name, end - beg);
name[end - beg] = '\0';
v->name = allocated_variable_expand (name);
if (v->name[0] == '\0')
fatal (&v->fileinfo, _("empty variable name"));
return v;
}
struct variable *
try_variable_definition (const struct floc *flocp, char *line,
enum variable_origin origin, int target_var)
{
struct variable v;
struct variable *vp;
if (flocp != 0)
v.fileinfo = *flocp;
else
v.fileinfo.filenm = 0;
if (!parse_variable_definition (&v, line))
return 0;
vp = do_variable_definition (flocp, v.name, v.value,
origin, v.flavor, target_var);
free (v.name);
return vp;
}
static void
print_variable (const void *item, void *arg)
{
const struct variable *v = (struct variable *) item;
const char *prefix = (char *) arg;
const char *origin;
switch (v->origin)
{
case o_default:
origin = _("default");
break;
case o_env:
origin = _("environment");
break;
case o_file:
origin = _("makefile");
break;
case o_env_override:
origin = _("environment under -e");
break;
case o_command:
origin = _("command line");
break;
case o_override:
origin = _("`override' directive");
break;
case o_automatic:
origin = _("automatic");
break;
case o_invalid:
default:
abort ();
}
fputs ("# ", stdout);
fputs (origin, stdout);
if (v->fileinfo.filenm)
printf (_(" (from `%s', line %lu)"),
v->fileinfo.filenm, v->fileinfo.lineno);
putchar ('\n');
fputs (prefix, stdout);
if (v->recursive && strchr (v->value, '\n') != 0)
printf ("define %s\n%s\nendef\n", v->name, v->value);
else
{
register char *p;
printf ("%s %s= ", v->name, v->recursive ? v->append ? "+" : "" : ":");
p = next_token (v->value);
if (p != v->value && *p == '\0')
printf ("$(subst ,,%s)", v->value);
else if (v->recursive)
fputs (v->value, stdout);
else
for (p = v->value; *p != '\0'; ++p)
{
if (*p == '$')
putchar ('$');
putchar (*p);
}
putchar ('\n');
}
}
void
print_variable_set (struct variable_set *set, char *prefix)
{
hash_map_arg (&set->table, print_variable, prefix);
fputs (_("# variable set hash-table stats:\n"), stdout);
fputs ("# ", stdout);
hash_print_stats (&set->table, stdout);
putc ('\n', stdout);
}
void
print_variable_data_base (void)
{
puts (_("\n# Variables\n"));
print_variable_set (&global_variable_set, "");
puts (_("\n# Pattern-specific Variable Values"));
{
struct pattern_var *p;
int rules = 0;
for (p = pattern_vars; p != 0; p = p->next)
{
++rules;
printf ("\n%s :\n", p->target);
print_variable (&p->variable, "# ");
}
if (rules == 0)
puts (_("\n# No pattern-specific variable values."));
else
printf (_("\n# %u pattern-specific variable values"), rules);
}
}
void
print_file_variables (struct file *file)
{
if (file->variables != 0)
print_variable_set (file->variables->set, "# ");
}
#ifdef WINDOWS32
void
sync_Path_environment (void)
{
char *path = allocated_variable_expand ("$(PATH)");
static char *environ_path = NULL;
if (!path)
return;
if (environ_path)
free (environ_path);
convert_Path_to_windows32 (path, ';');
environ_path = concat ("PATH", "=", path);
putenv (environ_path);
free (path);
}
#endif