home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 5
/
FreshFish_July-August1994.bin
/
bbs
/
gnu
/
make-3.71-src.lha
/
src
/
amiga
/
make-3.71
/
variable.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-04-05
|
22KB
|
821 lines
/* Internals of variables for GNU Make.
Copyright (C) 1988, 89, 90, 91, 92, 93, 94 Free Software Foundation, Inc.
This file is part of GNU Make.
GNU Make is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Make is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Make; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "make.h"
#include "commands.h"
#include "variable.h"
#include "dep.h"
#include "file.h"
/* Hash table of all global variable definitions. */
#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 *variable_table[VARIABLE_BUCKETS];
static struct variable_set global_variable_set
= { variable_table, VARIABLE_BUCKETS };
static struct variable_set_list global_setlist
= { 0, &global_variable_set };
struct variable_set_list *current_variable_set_list = &global_setlist;
/* Implement variables. */
/* Define variable named NAME with value VALUE in SET. VALUE is copied.
LENGTH is the length of NAME, which does not need to be null-terminated.
ORIGIN specifies the origin of the variable (makefile, command line
or environment).
If RECURSIVE is nonzero a flag is set in the variable saying
that it should be recursively re-expanded. */
static struct variable *
define_variable_in_set (name, length, value, origin, recursive, set)
char *name;
unsigned int length;
char *value;
enum variable_origin origin;
int recursive;
struct variable_set *set;
{
register unsigned int i;
register unsigned int hashval;
register struct variable *v;
hashval = 0;
for (i = 0; i < length; ++i)
HASH (hashval, name[i]);
hashval %= set->buckets;
for (v = set->table[hashval]; v != 0; v = v->next)
if (*v->name == *name
&& !strncmp (v->name + 1, name + 1, length - 1)
&& v->name[length] == '\0')
break;
if (env_overrides && origin == o_env)
origin = o_env_override;
if (v != 0)
{
if (env_overrides && v->origin == o_env)
/* V came from in the environment. Since it was defined
before the switches were parsed, it wasn't affected by -e. */
v->origin = o_env_override;
/* A variable of this name is already defined.
If the old definition is from a stronger source
than this one, don't redefine it. */
if ((int) origin >= (int) v->origin)
{
if (v->value != 0)
free (v->value);
v->value = savestring (value, strlen (value));
v->origin = origin;
v->recursive = recursive;
}
return v;
}
/* Create a new variable definition and add it to the hash table. */
v = (struct variable *) xmalloc (sizeof (struct variable));
v->name = savestring (name, length);
v->value = savestring (value, strlen (value));
v->origin = origin;
v->recursive = recursive;
v->expanding = 0;
v->export = v_default;
v->next = set->table[hashval];
set->table[hashval] = v;
return v;
}
/* Define a variable in the current variable set. */
struct variable *
define_variable (name, length, value, origin, recursive)
char *name;
unsigned int length;
char *value;
enum variable_origin origin;
int recursive;
{
return define_variable_in_set (name, length, value, origin, recursive,
current_variable_set_list->set);
}
/* Define a variable in FILE's variable set. */
struct variable *
define_variable_for_file (name, length, value, origin, recursive, file)
char *name;
unsigned int length;
char *value;
enum variable_origin origin;
int recursive;
struct file *file;
{
return define_variable_in_set (name, length, value, origin, recursive,
file->variables->set);
}
/* Lookup a variable whose name is a string starting at NAME
and with LENGTH chars. NAME need not be null-terminated.
Returns address of the `struct variable' containing all info
on the variable, or nil if no such variable is defined. */
struct variable *
lookup_variable (name, length)
char *name;
unsigned int length;
{
register struct variable_set_list *setlist;
register unsigned int i;
register unsigned int rawhash = 0;
for (i = 0; i < length; ++i)
HASH (rawhash, name[i]);
for (setlist = current_variable_set_list;
setlist != 0; setlist = setlist->next)
{
register struct variable_set *set = setlist->set;
register unsigned int hashval = rawhash % set->buckets;
register struct variable *v;
for (v = set->table[hashval]; v != 0; v = v->next)
if (*v->name == *name
&& !strncmp (v->name + 1, name + 1, length - 1)
&& v->name[length] == 0)
return v;
}
return 0;
}
/* Initialize FILE's variable set list. If FILE already has a variable set
list, the topmost variable set is left intact, but the the rest of the
chain is replaced with FILE->parent's setlist. */
void
initialize_file_variables (file)
struct file *file;
{
register 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));
l->set->buckets = PERFILE_VARIABLE_BUCKETS;
l->set->table = (struct variable **)
xmalloc (l->set->buckets * sizeof (struct variable *));
bzero ((char *) l->set->table,
l->set->buckets * sizeof (struct variable *));
file->variables = l;
}
if (file->parent == 0)
l->next = &global_setlist;
else
{
if (file->parent->variables == 0)
initialize_file_variables (file->parent);
l->next = file->parent->variables;
}
}
/* Pop the top set off the current variable set list,
and free all its storage. */
void
pop_variable_scope ()
{
register struct variable_set_list *setlist = current_variable_set_list;
register struct variable_set *set = setlist->set;
register unsigned int i;
current_variable_set_list = setlist->next;
free ((char *) setlist);
for (i = 0; i < set->buckets; ++i)
{
register struct variable *next = set->table[i];
while (next != 0)
{
register struct variable *v = next;
next = v->next;
free (v->name);
free ((char *) v);
}
}
free ((char *) set->table);
free ((char *) set);
}
/* Create a new variable set and push it on the current setlist. */
void
push_new_variable_scope ()
{
register struct variable_set_list *setlist;
register struct variable_set *set;
set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
set->buckets = SMALL_SCOPE_VARIABLE_BUCKETS;
set->table = (struct variable **)
xmalloc (set->buckets * sizeof (struct variable *));
bzero ((char *) set->table, set->buckets * sizeof (struct variable *));
setlist = (struct variable_set_list *)
xmalloc (sizeof (struct variable_set_list));
setlist->set = set;
setlist->next = current_variable_set_list;
current_variable_set_list = setlist;
}
/* Merge SET1 into SET0, freeing unused storage in SET1. */
static void
merge_variable_sets (set0, set1)
struct variable_set *set0, *set1;
{
register unsigned int bucket1;
for (bucket1 = 0; bucket1 < set1->buckets; ++bucket1)
{
register struct variable *v1 = set1->table[bucket1];
while (v1 != 0)
{
struct variable *next = v1->next;
unsigned int bucket0;
register struct variable *v0;
if (set1->buckets >= set0->buckets)
bucket0 = bucket1;
else
{
register char *n;
bucket0 = 0;
for (n = v1->name; *n != '\0'; ++n)
HASH (bucket0, *n);
}
bucket0 %= set0->buckets;
for (v0 = set0->t