[go: up one dir, main page]

File: id.c

package info (click to toggle)
cook 2.10-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 6,856 kB
  • ctags: 3,842
  • sloc: ansic: 47,714; sh: 12,150; makefile: 4,317; yacc: 3,104; awk: 154
file content (326 lines) | stat: -rw-r--r-- 6,501 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 *	cook - file construction tool
 *	Copyright (C) 1990, 1991, 1992, 1993, 1994, 1997, 1999 Peter Miller;
 *	All rights reserved.
 *
 *	This program 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 of the License, or
 *	(at your option) any later version.
 *
 *	This program 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 this program; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 * MANIFEST: functions to manipulate the symbol table
 */

#include <ac/stddef.h>
#include <ac/stdlib.h>

#include <id/private.h>
#include <id/variable.h>
#include <mem.h>
#include <option.h>
#include <progname.h>
#include <symtab.h>
#include <version-stmp.h>
#include <str_list.h>
#include <trace.h>

static	symtab_ty	*symtab;
	string_ty	*id_need;
	string_ty	*id_younger;
	string_ty	*id_target;
	string_ty	*id_targets;
	string_ty	*id_search_list;


static void reap _((void *));

static void
reap(p)
	void		*p;
{
	id_ty		*idp;

	idp = p;
	id_instance_delete(idp);
}


/*
 * NAME
 *	id_initialize - start up symbol table
 *
 * SYNOPSIS
 *	void id_initialize(void);
 *
 * DESCRIPTION
 *	The id_initialize function is used to create the hash table.
 *
 * RETURNS
 *	void
 *
 * CAVEAT
 *	Assumes the str_initialize function has been called already.
 */

void
id_initialize()
{
	trace(("init\n"));
	id_need = str_from_c("need");
	id_younger = str_from_c("younger");
	id_target = str_from_c("target");
	id_targets = str_from_c("targets");
	id_search_list = str_from_c("search_list");

	id_reset();
}


void
id_reset()
{
	string_list_ty	wl;
	string_ty	*s;

	if (symtab)
		symtab_free(symtab);
	symtab = symtab_alloc(100);
	symtab->reap = reap;

	/*
	 * set the "version" predefined variable
	 */
	string_list_constructor(&wl);
	s = str_from_c(version_stamp());
	string_list_append(&wl, s);
	str_free(s);
	s = str_from_c("version");
	id_assign(s, id_variable_new(&wl));
	str_free(s);
	string_list_destructor(&wl);

	/*
	 * set the "self" predefined variable
	 */
	s = str_from_c(progname_get());
	string_list_append(&wl, s);
	str_free(s);
	s = str_from_c("self");
	id_assign(s, id_variable_new(&wl));
	str_free(s);
	string_list_destructor(&wl);

#ifdef __STDC__
	/*
	 * This symbol is only defined if we have a conforming C
	 * compiler.  This is support for the C recipes.
	 */
	string_list_append(&wl, str_true);
	s = str_from_c("__STDC__");
	id_assign(s, id_variable_new(&wl));
	str_free(s);
	string_list_destructor(&wl);
#endif
	
	/*
	 * set the default search list
	 * in the "search_list" predefined variable
	 */
	s = str_from_c(".");
	string_list_append(&wl, s);
	str_free(s);
	id_assign(id_search_list, id_variable_new(&wl));
	string_list_destructor(&wl);
}


/*
 * NAME
 *	id_search - search for a variable
 *
 * SYNOPSIS
 *	int id_search(string_ty *name, string_list_ty *value);
 *
 * DESCRIPTION
 *	Id_search is used to reference a variable.
 *
 * RETURNS
 *	If the variable has been defined, the function returns a non-zero value
 *	and the value is returned through the 'value' pointer.
 *	If the variable has not been defined, it returns zero,
 *	and 'value' is unaltered.
 *
 * CAVEAT
 *	The value returned from this function, when returned, is allocated
 *	in dynamic memory (it is a copy of the value remembered by this module).
 *	It is the responsibility of the caller to free it when finished with,
 *	by a string_list_destructor() call.
 */

id_ty *
id_search(name)
	string_ty	*name;
{
	id_ty		*data;

	assert(symtab);
	data = symtab_query(symtab, name);
	return data;
}


id_ty *
id_search_fuzzy(name, name_used)
	string_ty	*name;
	string_ty	**name_used;
{
	id_ty		*data;

	assert(symtab);
	data = symtab_query_fuzzy(symtab, name, name_used);
	return data;
}


/*
 * NAME
 *	id_assign - assign a variable
 *
 * SYNOPSIS
 *	void id_assign(string_ty *name, id_ty *);
 *
 * DESCRIPTION
 *	Id_assign is used to assign a value to a given variable.
 *
 * CAVEAT
 *	The name and value are copied by id_assign, so the user may
 *	modify or free them at a later date without affecting the
 *	variable.
 */

void
id_assign(name, arg)
	string_ty	*name;
	id_ty		*arg;
{
	assert(symtab);
	symtab_assign(symtab, name, arg);
}


/*
 * NAME
 *	id_assign_push - assign a variable, remembering old value
 *
 * SYNOPSIS
 *	void id_assign_push(string_ty *name, id_ty *);
 *
 * DESCRIPTION
 *	Id_assign is used to assign a value to a given variable.
 *
 * CAVEAT
 *	The name and value are copied by id_assign, so the user may
 *	modify or free them at a later date without affecting the
 *	variable.
 */

void
id_assign_push(name, arg)
	string_ty	*name;
	id_ty		*arg;
{
	assert(symtab);
	symtab_assign_push(symtab, name, arg);
}


/*
 * NAME
 *	id_unassign - delete a variable
 *
 * SYNOPSIS
 *	void id_unassign(string_ty *name, id_class_ty class);
 *
 * DESCRIPTION
 *	Id_delete is used to delete variables.
 *
 * CAVEAT
 *	No complaint is made if the variable does not exist,
 *	since the user wanted it to not exist.
 */

void
id_unassign(name)
	string_ty	*name;
{
	assert(symtab);
	symtab_delete(symtab, name);
}


/*
 * NAME
 *	id_dump - dump id table
 *
 * SYNOPSIS
 *	void id_dump(char *title, int mask);
 *
 * DESCRIPTION
 *	The id_dump function is used to dump the contents of the id table.
 *	The title will be used to indicate why the table was dumped.  The mask
 *	may be used to selectively dump the table, 0 means everything, bits
 *	correspond directly with ID_CLASS defines.
 *
 * RETURNS
 *	void
 *
 * CAVEAT
 *	This function is only available when symbol DEBUG is defined.
 */

#ifdef DEBUG

void
id_dump(s)
	char		*s;
{
	assert(symtab);
	symtab_dump(symtab, s);
}

#endif


int
id_interpret(idp, ocp, pp)
	id_ty		*idp;
	struct opcode_context_ty *ocp;
	const struct expr_position_ty *pp;
{
	assert(idp);
	assert(idp->method);
	assert(idp->method->interpret);
	return idp->method->interpret(idp, ocp, pp);
}


int
id_interpret_script(idp, ocp, pp)
	id_ty		*idp;
	struct opcode_context_ty *ocp;
	const struct expr_position_ty *pp;
{
	assert(idp);
	assert(idp->method);
	assert(idp->method->script);
	return idp->method->script(idp, ocp, pp);
}