(scm_mem2symbol): Call `scm_must_strndup' instead of
[bpt/guile.git] / libguile / symbols.c
1 /* Copyright (C) 1995,1996,1997,1998, 2000, 2001 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
41
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
45 \f
46
47 #include "libguile/_scm.h"
48 #include "libguile/chars.h"
49 #include "libguile/eval.h"
50 #include "libguile/hash.h"
51 #include "libguile/smob.h"
52 #include "libguile/variable.h"
53 #include "libguile/alist.h"
54 #include "libguile/fluids.h"
55 #include "libguile/strings.h"
56 #include "libguile/vectors.h"
57 #include "libguile/hashtab.h"
58 #include "libguile/weaks.h"
59 #include "libguile/modules.h"
60
61 #include "libguile/validate.h"
62 #include "libguile/symbols.h"
63
64 #ifdef HAVE_STRING_H
65 #include <string.h>
66 #endif
67
68 \f
69
70 static SCM symbols;
71
72 #ifdef GUILE_DEBUG
73 SCM_DEFINE (scm_sys_symbols, "%symbols", 0, 0, 0,
74 (),
75 "Return the system symbol obarray.")
76 #define FUNC_NAME s_scm_sys_symbols
77 {
78 return symbols;
79 }
80 #undef FUNC_NAME
81 #endif
82
83 \f
84
85 /* {Symbols}
86 */
87
88
89 SCM
90 scm_mem2symbol (const char *name, scm_sizet len)
91 {
92 scm_sizet raw_hash = scm_string_hash ((const unsigned char *) name, len);
93 scm_sizet hash = raw_hash % SCM_VECTOR_LENGTH (symbols);
94
95 {
96 /* Try to find the symbol in the symbols table */
97
98 SCM l;
99
100 for (l = SCM_VELTS (symbols) [hash]; !SCM_NULLP (l); l = SCM_CDR (l))
101 {
102 SCM sym = SCM_CAR (l);
103 if (SCM_SYMBOL_HASH (sym) == raw_hash
104 && SCM_SYMBOL_LENGTH (sym) == len)
105 {
106 char *chrs = SCM_SYMBOL_CHARS (sym);
107 scm_sizet i = len;
108
109 while (i != 0)
110 {
111 --i;
112 if (name[i] != chrs[i])
113 goto next_symbol;
114 }
115
116 return sym;
117 }
118 next_symbol:
119 ;
120 }
121 }
122
123 {
124 /* The symbol was not found - create it. */
125
126 SCM symbol;
127 SCM slot;
128
129 SCM_NEWCELL2 (symbol);
130 SCM_SET_SYMBOL_CHARS (symbol, scm_must_strndup (name, len));
131 SCM_SET_SYMBOL_HASH (symbol, raw_hash);
132 SCM_SET_PROP_SLOTS (symbol, scm_cons (SCM_BOOL_F, SCM_EOL));
133 SCM_SET_SYMBOL_LENGTH (symbol, (long) len);
134
135 slot = SCM_VELTS (symbols) [hash];
136 SCM_VELTS (symbols) [hash] = scm_cons (symbol, slot);
137
138 return symbol;
139 }
140 }
141
142
143 SCM
144 scm_str2symbol (const char *str)
145 {
146 return scm_mem2symbol (str, strlen (str));
147 }
148
149 SCM_DEFINE (scm_symbol_p, "symbol?", 1, 0, 0,
150 (SCM obj),
151 "Return @code{#t} if @var{obj} is a symbol, otherwise return\n"
152 "@code{#f}.")
153 #define FUNC_NAME s_scm_symbol_p
154 {
155 return SCM_BOOL (SCM_SYMBOLP (obj));
156 }
157 #undef FUNC_NAME
158
159 SCM_DEFINE (scm_symbol_to_string, "symbol->string", 1, 0, 0,
160 (SCM s),
161 "Return the name of @var{symbol} as a string. If the symbol was\n"
162 "part of an object returned as the value of a literal expression\n"
163 "(section @pxref{Literal expressions,,,r5rs, The Revised^5\n"
164 "Report on Scheme}) or by a call to the @code{read} procedure,\n"
165 "and its name contains alphabetic characters, then the string\n"
166 "returned will contain characters in the implementation's\n"
167 "preferred standard case---some implementations will prefer\n"
168 "upper case, others lower case. If the symbol was returned by\n"
169 "@code{string->symbol}, the case of characters in the string\n"
170 "returned will be the same as the case in the string that was\n"
171 "passed to @code{string->symbol}. It is an error to apply\n"
172 "mutation procedures like @code{string-set!} to strings returned\n"
173 "by this procedure.\n"
174 "\n"
175 "The following examples assume that the implementation's\n"
176 "standard case is lower case:\n"
177 "\n"
178 "@lisp\n"
179 "(symbol->string 'flying-fish) @result{} \"flying-fish\"\n"
180 "(symbol->string 'Martin) @result{} \"martin\"\n"
181 "(symbol->string\n"
182 " (string->symbol \"Malvina\")) @result{} \"Malvina\"\n"
183 "@end lisp")
184 #define FUNC_NAME s_scm_symbol_to_string
185 {
186 SCM_VALIDATE_SYMBOL (1, s);
187 return scm_makfromstr (SCM_SYMBOL_CHARS (s), SCM_SYMBOL_LENGTH (s), 0);
188 }
189 #undef FUNC_NAME
190
191
192 SCM_DEFINE (scm_string_to_symbol, "string->symbol", 1, 0, 0,
193 (SCM string),
194 "Return the symbol whose name is @var{string}. This procedure\n"
195 "can create symbols with names containing special characters or\n"
196 "letters in the non-standard case, but it is usually a bad idea\n"
197 "to create such symbols because in some implementations of\n"
198 "Scheme they cannot be read as themselves. See\n"
199 "@code{symbol->string}.\n"
200 "\n"
201 "The following examples assume that the implementation's\n"
202 "standard case is lower case:\n"
203 "\n"
204 "@lisp\n"
205 "(eq? 'mISSISSIppi 'mississippi) @result{} #t\n"
206 "(string->symbol \"mISSISSIppi\") @result{} @r{the symbol with name \"mISSISSIppi\"}\n"
207 "(eq? 'bitBlt (string->symbol \"bitBlt\")) @result{} #f\n"
208 "(eq? 'JollyWog\n"
209 " (string->symbol (symbol->string 'JollyWog))) @result{} #t\n"
210 "(string=? \"K. Harper, M.D.\"\n"
211 " (symbol->string\n"
212 " (string->symbol \"K. Harper, M.D.\"))) @result{}#t\n"
213 "@end lisp")
214 #define FUNC_NAME s_scm_string_to_symbol
215 {
216 SCM_VALIDATE_STRING (1, string);
217 return scm_mem2symbol (SCM_STRING_CHARS (string),
218 SCM_STRING_LENGTH (string));
219 }
220 #undef FUNC_NAME
221
222 #define MAX_PREFIX_LENGTH 30
223
224 static int gensym_counter;
225
226 SCM_DEFINE (scm_gensym, "gensym", 0, 1, 0,
227 (SCM prefix),
228 "Create a new symbol with a name constructed from a prefix and\n"
229 "a counter value. The string @var{prefix} can be specified as\n"
230 "an optional argument. Default prefix is @code{g}. The counter\n"
231 "is increased by 1 at each call. There is no provision for\n"
232 "resetting the counter.")
233 #define FUNC_NAME s_scm_gensym
234 {
235 char buf[MAX_PREFIX_LENGTH + SCM_INTBUFLEN];
236 char *name = buf;
237 int len;
238 if (SCM_UNBNDP (prefix))
239 {
240 name[0] = 'g';
241 len = 1;
242 }
243 else
244 {
245 SCM_VALIDATE_STRING (1, prefix);
246 len = SCM_STRING_LENGTH (prefix);
247 if (len > MAX_PREFIX_LENGTH)
248 name = SCM_MUST_MALLOC (MAX_PREFIX_LENGTH + SCM_INTBUFLEN);
249 strncpy (name, SCM_STRING_CHARS (prefix), len);
250 }
251 {
252 int n_digits = scm_iint2str (gensym_counter++, 10, &name[len]);
253 SCM res = scm_mem2symbol (name, len + n_digits);
254 if (name != buf)
255 scm_must_free (name);
256 return res;
257 }
258 }
259 #undef FUNC_NAME
260
261 SCM_DEFINE (scm_symbol_hash, "symbol-hash", 1, 0, 0,
262 (SCM symbol),
263 "Return a hash value for @var{symbol}.")
264 #define FUNC_NAME s_scm_symbol_hash
265 {
266 SCM_VALIDATE_SYMBOL (1, symbol);
267 return SCM_MAKINUM (SCM_SYMBOL_HASH (symbol));
268 }
269 #undef FUNC_NAME
270
271 SCM_DEFINE (scm_symbol_fref, "symbol-fref", 1, 0, 0,
272 (SCM s),
273 "Return the contents of @var{symbol}'s @dfn{function slot}.")
274 #define FUNC_NAME s_scm_symbol_fref
275 {
276 SCM_VALIDATE_SYMBOL (1,s);
277 return SCM_SYMBOL_FUNC (s);
278 }
279 #undef FUNC_NAME
280
281
282 SCM_DEFINE (scm_symbol_pref, "symbol-pref", 1, 0, 0,
283 (SCM s),
284 "Return the @dfn{property list} currently associated with @var{symbol}.")
285 #define FUNC_NAME s_scm_symbol_pref
286 {
287 SCM_VALIDATE_SYMBOL (1,s);
288 return SCM_SYMBOL_PROPS (s);
289 }
290 #undef FUNC_NAME
291
292
293 SCM_DEFINE (scm_symbol_fset_x, "symbol-fset!", 2, 0, 0,
294 (SCM s, SCM val),
295 "Change the binding of @var{symbol}'s function slot.")
296 #define FUNC_NAME s_scm_symbol_fset_x
297 {
298 SCM_VALIDATE_SYMBOL (1,s);
299 SCM_SET_SYMBOL_FUNC (s, val);
300 return SCM_UNSPECIFIED;
301 }
302 #undef FUNC_NAME
303
304
305 SCM_DEFINE (scm_symbol_pset_x, "symbol-pset!", 2, 0, 0,
306 (SCM s, SCM val),
307 "Change the binding of @var{symbol}'s property slot.")
308 #define FUNC_NAME s_scm_symbol_pset_x
309 {
310 SCM_VALIDATE_SYMBOL (1,s);
311 SCM_DEFER_INTS;
312 SCM_SET_SYMBOL_PROPS (s, val);
313 SCM_ALLOW_INTS;
314 return SCM_UNSPECIFIED;
315 }
316 #undef FUNC_NAME
317
318 void
319 scm_symbols_prehistory ()
320 {
321 symbols = scm_make_weak_key_hash_table (SCM_MAKINUM (1009));
322 scm_permanent_object (symbols);
323 }
324
325
326 void
327 scm_init_symbols ()
328 {
329 gensym_counter = 0;
330 #ifndef SCM_MAGIC_SNARFER
331 #include "libguile/symbols.x"
332 #endif
333 #if SCM_ENABLE_VCELLS
334 scm_init_symbols_deprecated ();
335 #endif
336 }
337
338 /*
339 Local Variables:
340 c-file-style: "gnu"
341 End:
342 */