1 /******************************************************************************
2   Copyright (c) 1992, 1995, 1996 Xerox Corporation.  All rights reserved.
3   Portions of this code were written by Stephen White, aka ghond.
4   Use and copying of this software and preparation of derivative works based
5   upon this software are permitted.  Any distribution of this software or
6   derivative works must comply with all applicable United States export
7   control laws.  This software is made available AS IS, and Xerox Corporation
8   makes no warranty about the software, its performance or its conformity to
9   any specification.  Any person obtaining a copy of this software is requested
10   to send their name and post office or electronic mail address to:
11     Pavel Curtis
12     Xerox PARC
13     3333 Coyote Hill Rd.
14     Palo Alto, CA 94304
15     Pavel@Xerox.Com
16  *****************************************************************************/
17 
18 #include "my-stdio.h"
19 
20 #include "ast.h"
21 #include "config.h"
22 #include "exceptions.h"
23 #include "log.h"
24 #include "storage.h"
25 #include "structures.h"
26 #include "sym_table.h"
27 #include "utils.h"
28 #include "version.h"
29 
30 static Names *
new_names(unsigned max_size)31 new_names(unsigned max_size)
32 {
33     Names *names = mymalloc(sizeof(Names), M_NAMES);
34 
35     names->names = mymalloc(sizeof(char *) * max_size, M_NAMES);
36     names->max_size = max_size;
37     names->size = 0;
38 
39     return names;
40 }
41 
42 static Names *
copy_names(Names * old)43 copy_names(Names * old)
44 {
45     Names *new = new_names(old->size);
46     unsigned i;
47 
48     new->size = old->size;
49     for (i = 0; i < new->size; i++)
50 	new->names[i] = str_ref(old->names[i]);
51 
52     return new;
53 }
54 
55 int
first_user_slot(DB_Version version)56 first_user_slot(DB_Version version)
57 {
58     int count = 16;		/* DBV_Prehistory count */
59 
60     if (version >= DBV_Float)
61 	count += 2;
62 
63     return count;
64 }
65 
66 Names *
new_builtin_names(DB_Version version)67 new_builtin_names(DB_Version version)
68 {
69     static Names *builtins[Num_DB_Versions];
70 
71     if (builtins[version] == 0) {
72 	Names *bi = new_names(first_user_slot(version));
73 
74 	builtins[version] = bi;
75 	bi->size = bi->max_size;
76 
77 	bi->names[SLOT_NUM] = str_dup("NUM");
78 	bi->names[SLOT_OBJ] = str_dup("OBJ");
79 	bi->names[SLOT_STR] = str_dup("STR");
80 	bi->names[SLOT_LIST] = str_dup("LIST");
81 	bi->names[SLOT_ERR] = str_dup("ERR");
82 	bi->names[SLOT_PLAYER] = str_dup("player");
83 	bi->names[SLOT_THIS] = str_dup("this");
84 	bi->names[SLOT_CALLER] = str_dup("caller");
85 	bi->names[SLOT_VERB] = str_dup("verb");
86 	bi->names[SLOT_ARGS] = str_dup("args");
87 	bi->names[SLOT_ARGSTR] = str_dup("argstr");
88 	bi->names[SLOT_DOBJ] = str_dup("dobj");
89 	bi->names[SLOT_DOBJSTR] = str_dup("dobjstr");
90 	bi->names[SLOT_PREPSTR] = str_dup("prepstr");
91 	bi->names[SLOT_IOBJ] = str_dup("iobj");
92 	bi->names[SLOT_IOBJSTR] = str_dup("iobjstr");
93 
94 	if (version >= DBV_Float) {
95 	    bi->names[SLOT_INT] = str_dup("INT");
96 	    bi->names[SLOT_FLOAT] = str_dup("FLOAT");
97 	}
98     }
99     return copy_names(builtins[version]);
100 }
101 
102 int
find_name(Names * names,const char * str)103 find_name(Names * names, const char *str)
104 {
105     unsigned i;
106 
107     for (i = 0; i < names->size; i++)
108 	if (!mystrcasecmp(names->names[i], str))
109 	    return i;
110     return -1;
111 }
112 
113 unsigned
find_or_add_name(Names ** names,const char * str)114 find_or_add_name(Names ** names, const char *str)
115 {
116     unsigned i;
117 
118     for (i = 0; i < (*names)->size; i++)
119 	if (!mystrcasecmp((*names)->names[i], str)) {	/* old name */
120 	    return i;
121 	}
122     if ((*names)->size == (*names)->max_size) {
123 	unsigned old_max = (*names)->max_size;
124 	Names *new = new_names(old_max * 2);
125 	unsigned i;
126 
127 	for (i = 0; i < old_max; i++)
128 	    new->names[i] = (*names)->names[i];
129 	new->size = old_max;
130 	myfree((*names)->names, M_NAMES);
131 	myfree(*names, M_NAMES);
132 	*names = new;
133     }
134     (*names)->names[(*names)->size] = str_dup(str);
135     return (*names)->size++;
136 }
137 
138 void
free_names(Names * names)139 free_names(Names * names)
140 {
141     unsigned i;
142 
143     for (i = 0; i < names->size; i++)
144 	free_str(names->names[i]);
145     myfree(names->names, M_NAMES);
146     myfree(names, M_NAMES);
147 }
148 
149 char rcsid_sym_table[] = "$Id: sym_table.c,v 1.3 1998/12/14 13:19:05 nop Exp $";
150 
151 /*
152  * $Log: sym_table.c,v $
153  * Revision 1.3  1998/12/14 13:19:05  nop
154  * Merge UNSAFE_OPTS (ref fixups); fix Log tag placement to fit CVS whims
155  *
156  * Revision 1.2  1997/03/03 04:19:29  nop
157  * GNU Indent normalization
158  *
159  * Revision 1.1.1.1  1997/03/03 03:45:01  nop
160  * LambdaMOO 1.8.0p5
161  *
162  * Revision 2.2  1996/03/10  01:16:32  pavel
163  * Removed a bunch of obsolete unused functions.  Release 1.8.0.
164  *
165  * Revision 2.1  1996/02/08  06:49:24  pavel
166  * Made new_builtin_names() and first_user_slot() version-dependent.  Renamed
167  * TYPE_NUM to TYPE_INT.  Updated copyright notice for 1996.
168  * Release 1.8.0beta1.
169  *
170  * Revision 2.0  1995/11/30  04:31:53  pavel
171  * New baseline version, corresponding to release 1.8.0alpha1.
172  *
173  * Revision 1.10  1992/10/23  23:03:47  pavel
174  * Added copyright notice.
175  *
176  * Revision 1.9  1992/10/21  03:02:35  pavel
177  * Converted to use new automatic configuration system.
178  *
179  * Revision 1.8  1992/10/17  20:54:04  pavel
180  * Global rename of strdup->str_dup, strref->str_ref, vardup->var_dup, and
181  * varref->var_ref.
182  *
183  * Revision 1.7  1992/09/08  21:58:33  pjames
184  * Updated #includes.
185  *
186  * Revision 1.6  1992/08/31  22:24:18  pjames
187  * Changed some `char *'s to `const char *'
188  *
189  * Revision 1.5  1992/08/28  16:07:24  pjames
190  * Changed vardup to varref.
191  * Changed some strref's to strdup.
192  * Changed myfree(*, M_STRING) to free_str(*).
193  *
194  * Revision 1.4  1992/08/10  16:50:28  pjames
195  * Updated #includes.
196  *
197  * Revision 1.3  1992/07/27  18:23:48  pjames
198  * Changed M_CT_ENV to M_NAMES.  Freed some memory that wasn't being
199  * freed when growing or freeing a list of names.
200  *
201  * Revision 1.2  1992/07/21  00:07:06  pavel
202  * Added rcsid_<filename-root> declaration to hold the RCS ident. string.
203  *
204  * Revision 1.1  1992/07/20  23:23:12  pavel
205  * Initial RCS-controlled version.
206  */
207