* Deprecated scm_makfromstr and added scm_mem2string as a replacement.
[bpt/guile.git] / libguile / read.c
1 /* Copyright (C) 1995,1996,1997,1999,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 <stdio.h>
48 #include "libguile/_scm.h"
49 #include "libguile/chars.h"
50 #include "libguile/eval.h"
51 #include "libguile/unif.h"
52 #include "libguile/keywords.h"
53 #include "libguile/alist.h"
54 #include "libguile/srcprop.h"
55 #include "libguile/hashtab.h"
56 #include "libguile/hash.h"
57 #include "libguile/ports.h"
58 #include "libguile/root.h"
59 #include "libguile/strings.h"
60 #include "libguile/vectors.h"
61
62 #include "libguile/validate.h"
63 #include "libguile/read.h"
64
65 \f
66
67 SCM_SYMBOL (scm_keyword_prefix, "prefix");
68
69 scm_t_option scm_read_opts[] = {
70 { SCM_OPTION_BOOLEAN, "copy", 0,
71 "Copy source code expressions." },
72 { SCM_OPTION_BOOLEAN, "positions", 0,
73 "Record positions of source code expressions." },
74 { SCM_OPTION_BOOLEAN, "case-insensitive", 0,
75 "Convert symbols to lower case."},
76 { SCM_OPTION_SCM, "keywords", SCM_UNPACK (SCM_BOOL_F),
77 "Style of keyword recognition: #f or 'prefix"}
78 };
79
80 SCM_DEFINE (scm_read_options, "read-options-interface", 0, 1, 0,
81 (SCM setting),
82 "Option interface for the read options. Instead of using\n"
83 "this procedure directly, use the procedures @code{read-enable},\n"
84 "@code{read-disable}, @code{read-set!} and @var{read-options}.")
85 #define FUNC_NAME s_scm_read_options
86 {
87 SCM ans = scm_options (setting,
88 scm_read_opts,
89 SCM_N_READ_OPTIONS,
90 FUNC_NAME);
91 if (SCM_COPY_SOURCE_P)
92 SCM_RECORD_POSITIONS_P = 1;
93 return ans;
94 }
95 #undef FUNC_NAME
96
97 /* An association list mapping extra hash characters to procedures. */
98 static SCM *scm_read_hash_procedures;
99
100 SCM_DEFINE (scm_read, "read", 0, 1, 0,
101 (SCM port),
102 "Read an s-expression from the input port @var{port}, or from\n"
103 "the current input port if @var{port} is not specified.\n"
104 "Any whitespace before the next token is discarded.")
105 #define FUNC_NAME s_scm_read
106 {
107 int c;
108 SCM tok_buf, copy;
109
110 if (SCM_UNBNDP (port))
111 port = scm_cur_inp;
112 SCM_VALIDATE_OPINPORT (1,port);
113
114 c = scm_flush_ws (port, (char *) NULL);
115 if (EOF == c)
116 return SCM_EOF_VAL;
117 scm_ungetc (c, port);
118
119 tok_buf = scm_allocate_string (30);
120 return scm_lreadr (&tok_buf, port, &copy);
121 }
122 #undef FUNC_NAME
123
124
125
126 char *
127 scm_grow_tok_buf (SCM *tok_buf)
128 {
129 size_t oldlen = SCM_STRING_LENGTH (*tok_buf);
130 SCM newstr = scm_allocate_string (2 * oldlen);
131 size_t i;
132
133 for (i = 0; i != oldlen; ++i)
134 SCM_STRING_CHARS (newstr) [i] = SCM_STRING_CHARS (*tok_buf) [i];
135
136 *tok_buf = newstr;
137 return SCM_STRING_CHARS (newstr);
138 }
139
140
141
142 int
143 scm_flush_ws (SCM port, const char *eoferr)
144 {
145 register int c;
146 while (1)
147 switch (c = scm_getc (port))
148 {
149 case EOF:
150 goteof:
151 if (eoferr)
152 {
153 if (!SCM_FALSEP (SCM_FILENAME (port)))
154 scm_misc_error (eoferr,
155 "end of file in ~A",
156 SCM_LIST1 (SCM_FILENAME (port)));
157 else
158 scm_misc_error (eoferr, "end of file", SCM_EOL);
159 }
160 return c;
161 case ';':
162 lp:
163 switch (c = scm_getc (port))
164 {
165 case EOF:
166 goto goteof;
167 default:
168 goto lp;
169 case SCM_LINE_INCREMENTORS:
170 break;
171 }
172 break;
173 case SCM_LINE_INCREMENTORS:
174 case SCM_SINGLE_SPACES:
175 case '\t':
176 break;
177 default:
178 return c;
179 }
180 }
181
182
183
184 int
185 scm_casei_streq (char *s1, char *s2)
186 {
187 while (*s1 && *s2)
188 if (scm_downcase((int)*s1) != scm_downcase((int)*s2))
189 return 0;
190 else
191 {
192 ++s1;
193 ++s2;
194 }
195 return !(*s1 || *s2);
196 }
197
198
199 /* recsexpr is used when recording expressions
200 * constructed by read:sharp.
201 */
202 #ifndef DEBUG_EXTENSIONS
203 #define recsexpr(obj, line, column, filename) (obj)
204 #else
205 static SCM
206 recsexpr (SCM obj, long line, int column, SCM filename)
207 {
208 if (!SCM_CONSP(obj)) {
209 return obj;
210 } else {
211 SCM tmp = obj, copy;
212 /* If this sexpr is visible in the read:sharp source, we want to
213 keep that information, so only record non-constant cons cells
214 which haven't previously been read by the reader. */
215 if (SCM_FALSEP (scm_whash_lookup (scm_source_whash, obj)))
216 {
217 if (SCM_COPY_SOURCE_P)
218 {
219 copy = scm_cons (recsexpr (SCM_CAR (obj), line, column, filename),
220 SCM_UNDEFINED);
221 while ((tmp = SCM_CDR (tmp)) && SCM_CONSP (tmp))
222 {
223 SCM_SETCDR (copy, scm_cons (recsexpr (SCM_CAR (tmp),
224 line,
225 column,
226 filename),
227 SCM_UNDEFINED));
228 copy = SCM_CDR (copy);
229 }
230 SCM_SETCDR (copy, tmp);
231 }
232 else
233 {
234 recsexpr (SCM_CAR (obj), line, column, filename);
235 while ((tmp = SCM_CDR (tmp)) && SCM_CONSP (tmp))
236 recsexpr (SCM_CAR (tmp), line, column, filename);
237 copy = SCM_UNDEFINED;
238 }
239 scm_whash_insert (scm_source_whash,
240 obj,
241 scm_make_srcprops (line,
242 column,
243 filename,
244 copy,
245 SCM_EOL));
246 }
247 return obj;
248 }
249 }
250 #endif
251
252 /* Consume an SCSH-style block comment. Assume that we've already
253 read the initial `#!', and eat characters until we get a
254 newline/exclamation-point/sharp-sign/newline sequence. */
255
256 static void
257 skip_scsh_block_comment (SCM port)
258 #define FUNC_NAME "skip_scsh_block_comment"
259 {
260 /* Is this portable? Dear God, spare me from the non-eight-bit
261 characters. But is it tasteful? */
262 long history = 0;
263
264 for (;;)
265 {
266 int c = scm_getc (port);
267
268 if (c == EOF)
269 SCM_MISC_ERROR ("unterminated `#! ... !#' comment", SCM_EOL);
270 history = ((history << 8) | (c & 0xff)) & 0xffffffff;
271
272 /* Were the last four characters read "\n!#\n"? */
273 if (history == (('\n' << 24) | ('!' << 16) | ('#' << 8) | '\n'))
274 return;
275 }
276 }
277 #undef FUNC_NAME
278
279
280 static SCM scm_get_hash_procedure(int c);
281
282 static char s_list[]="list";
283
284 SCM
285 scm_lreadr (SCM *tok_buf,SCM port,SCM *copy)
286 #define FUNC_NAME "scm_lreadr"
287 {
288 int c;
289 size_t j;
290 SCM p;
291
292 tryagain:
293 c = scm_flush_ws (port, s_scm_read);
294 tryagain_no_flush_ws:
295 switch (c)
296 {
297 case EOF:
298 return SCM_EOF_VAL;
299
300 case '(':
301 return SCM_RECORD_POSITIONS_P
302 ? scm_lreadrecparen (tok_buf, port, s_list, copy)
303 : scm_lreadparen (tok_buf, port, s_list, copy);
304 case ')':
305 SCM_MISC_ERROR ("unexpected \")\"", SCM_EOL);
306 goto tryagain;
307
308 case '\'':
309 p = scm_sym_quote;
310 goto recquote;
311 case '`':
312 p = scm_sym_quasiquote;
313 goto recquote;
314 case ',':
315 c = scm_getc (port);
316 if ('@' == c)
317 p = scm_sym_uq_splicing;
318 else
319 {
320 scm_ungetc (c, port);
321 p = scm_sym_unquote;
322 }
323 recquote:
324 p = scm_cons2 (p,
325 scm_lreadr (tok_buf, port, copy),
326 SCM_EOL);
327 if (SCM_RECORD_POSITIONS_P)
328 scm_whash_insert (scm_source_whash,
329 p,
330 scm_make_srcprops (SCM_LINUM (port),
331 SCM_COL (port) - 1,
332 SCM_FILENAME (port),
333 SCM_COPY_SOURCE_P
334 ? (*copy = scm_cons2 (SCM_CAR (p),
335 SCM_CAR (SCM_CDR (p)),
336 SCM_EOL))
337 : SCM_UNDEFINED,
338 SCM_EOL));
339 return p;
340 case '#':
341 c = scm_getc (port);
342 switch (c)
343 {
344 case '(':
345 p = scm_lreadparen (tok_buf, port, "vector", copy);
346 return SCM_NULLP (p) ? scm_nullvect : scm_vector (p);
347
348 case 't':
349 case 'T':
350 return SCM_BOOL_T;
351 case 'f':
352 case 'F':
353 return SCM_BOOL_F;
354
355 case 'b':
356 case 'B':
357 case 'o':
358 case 'O':
359 case 'd':
360 case 'D':
361 case 'x':
362 case 'X':
363 case 'i':
364 case 'I':
365 case 'e':
366 case 'E':
367 scm_ungetc (c, port);
368 c = '#';
369 goto num;
370
371 case '!':
372 /* start of a shell script. Parse as a block comment,
373 terminated by !#, just like SCSH. */
374 skip_scsh_block_comment (port);
375 /* EOF is not an error here */
376 c = scm_flush_ws (port, (char *)NULL);
377 goto tryagain_no_flush_ws;
378
379 #ifdef HAVE_ARRAYS
380 case '*':
381 j = scm_read_token (c, tok_buf, port, 0);
382 p = scm_istr2bve (SCM_STRING_CHARS (*tok_buf) + 1, (long) (j - 1));
383 if (!SCM_FALSEP (p))
384 return p;
385 else
386 goto unkshrp;
387 #endif
388
389 case '{':
390 j = scm_read_token (c, tok_buf, port, 1);
391 return scm_mem2symbol (SCM_STRING_CHARS (*tok_buf), j);
392
393 case '\\':
394 c = scm_getc (port);
395 j = scm_read_token (c, tok_buf, port, 0);
396 if (j == 1)
397 return SCM_MAKE_CHAR (c);
398 if (c >= '0' && c < '8')
399 {
400 p = scm_istr2int (SCM_STRING_CHARS (*tok_buf), (long) j, 8);
401 if (!SCM_FALSEP (p))
402 return SCM_MAKE_CHAR (SCM_INUM (p));
403 }
404 for (c = 0; c < scm_n_charnames; c++)
405 if (scm_charnames[c]
406 && (scm_casei_streq (scm_charnames[c], SCM_STRING_CHARS (*tok_buf))))
407 return SCM_MAKE_CHAR (scm_charnums[c]);
408 SCM_MISC_ERROR ("unknown # object", SCM_EOL);
409
410 /* #:SYMBOL is a syntax for keywords supported in all contexts. */
411 case ':':
412 j = scm_read_token ('-', tok_buf, port, 0);
413 p = scm_mem2symbol (SCM_STRING_CHARS (*tok_buf), j);
414 return scm_make_keyword_from_dash_symbol (p);
415
416 default:
417 callshrp:
418 {
419 SCM sharp = scm_get_hash_procedure (c);
420
421 if (!SCM_FALSEP (sharp))
422 {
423 int line = SCM_LINUM (port);
424 int column = SCM_COL (port) - 2;
425 SCM got;
426
427 got = scm_apply (sharp,
428 SCM_MAKE_CHAR (c),
429 scm_acons (port, SCM_EOL, SCM_EOL));
430 if (SCM_EQ_P (got, SCM_UNSPECIFIED))
431 goto unkshrp;
432 if (SCM_RECORD_POSITIONS_P)
433 return *copy = recsexpr (got, line, column,
434 SCM_FILENAME (port));
435 else
436 return got;
437 }
438 }
439 unkshrp:
440 scm_misc_error (s_scm_read, "Unknown # object: ~S",
441 SCM_LIST1 (SCM_MAKE_CHAR (c)));
442 }
443
444 case '"':
445 j = 0;
446 while ('"' != (c = scm_getc (port)))
447 {
448 if (c == EOF)
449 SCM_MISC_ERROR ("end of file in string constant", SCM_EOL);
450
451 while (j + 2 >= SCM_STRING_LENGTH (*tok_buf))
452 scm_grow_tok_buf (tok_buf);
453
454 if (c == '\\')
455 switch (c = scm_getc (port))
456 {
457 case '\n':
458 continue;
459 case '0':
460 c = '\0';
461 break;
462 case 'f':
463 c = '\f';
464 break;
465 case 'n':
466 c = '\n';
467 break;
468 case 'r':
469 c = '\r';
470 break;
471 case 't':
472 c = '\t';
473 break;
474 case 'a':
475 c = '\007';
476 break;
477 case 'v':
478 c = '\v';
479 break;
480 }
481 SCM_STRING_CHARS (*tok_buf)[j] = c;
482 ++j;
483 }
484 if (j == 0)
485 return scm_nullstr;
486 SCM_STRING_CHARS (*tok_buf)[j] = 0;
487 return scm_mem2string (SCM_STRING_CHARS (*tok_buf), j);
488
489 case'0':case '1':case '2':case '3':case '4':
490 case '5':case '6':case '7':case '8':case '9':
491 case '.':
492 case '-':
493 case '+':
494 num:
495 j = scm_read_token (c, tok_buf, port, 0);
496 p = scm_istring2number (SCM_STRING_CHARS (*tok_buf), (long) j, 10L);
497 if (!SCM_FALSEP (p))
498 return p;
499 if (c == '#')
500 {
501 if ((j == 2) && (scm_getc (port) == '('))
502 {
503 scm_ungetc ('(', port);
504 c = SCM_STRING_CHARS (*tok_buf)[1];
505 goto callshrp;
506 }
507 SCM_MISC_ERROR ("unknown # object", SCM_EOL);
508 }
509 goto tok;
510
511 case ':':
512 if (SCM_EQ_P (SCM_PACK (SCM_KEYWORD_STYLE), scm_keyword_prefix))
513 {
514 j = scm_read_token ('-', tok_buf, port, 0);
515 p = scm_mem2symbol (SCM_STRING_CHARS (*tok_buf), j);
516 return scm_make_keyword_from_dash_symbol (p);
517 }
518 /* fallthrough */
519 default:
520 j = scm_read_token (c, tok_buf, port, 0);
521 /* fallthrough */
522
523 tok:
524 return scm_mem2symbol (SCM_STRING_CHARS (*tok_buf), j);
525 }
526 }
527 #undef FUNC_NAME
528
529
530 #ifdef _UNICOS
531 _Pragma ("noopt"); /* # pragma _CRI noopt */
532 #endif
533
534 size_t
535 scm_read_token (int ic, SCM *tok_buf, SCM port, int weird)
536 {
537 register size_t j;
538 register int c;
539 register char *p;
540
541 c = (SCM_CASE_INSENSITIVE_P ? scm_downcase(ic) : ic);
542 p = SCM_STRING_CHARS (*tok_buf);
543
544 if (weird)
545 j = 0;
546 else
547 {
548 j = 0;
549 while (j + 2 >= SCM_STRING_LENGTH (*tok_buf))
550 p = scm_grow_tok_buf (tok_buf);
551 p[j] = c;
552 ++j;
553 }
554
555 while (1)
556 {
557 while (j + 2 >= SCM_STRING_LENGTH (*tok_buf))
558 p = scm_grow_tok_buf (tok_buf);
559 c = scm_getc (port);
560 switch (c)
561 {
562 case '(':
563 case ')':
564 case '"':
565 case ';':
566 case SCM_WHITE_SPACES:
567 case SCM_LINE_INCREMENTORS:
568 if (weird)
569 goto default_case;
570
571 scm_ungetc (c, port);
572 case EOF:
573 eof_case:
574 p[j] = 0;
575 return j;
576 case '\\':
577 if (!weird)
578 goto default_case;
579 else
580 {
581 c = scm_getc (port);
582 if (c == EOF)
583 goto eof_case;
584 else
585 goto default_case;
586 }
587 case '}':
588 if (!weird)
589 goto default_case;
590
591 c = scm_getc (port);
592 if (c == '#')
593 {
594 p[j] = 0;
595 return j;
596 }
597 else
598 {
599 scm_ungetc (c, port);
600 c = '}';
601 goto default_case;
602 }
603
604 default:
605 default_case:
606 {
607 c = (SCM_CASE_INSENSITIVE_P ? scm_downcase(c) : c);
608 p[j] = c;
609 ++j;
610 }
611
612 }
613 }
614 }
615
616 #ifdef _UNICOS
617 _Pragma ("opt"); /* # pragma _CRI opt */
618 #endif
619
620 SCM
621 scm_lreadparen (SCM *tok_buf, SCM port, char *name, SCM *copy)
622 #define FUNC_NAME "scm_lreadparen"
623 {
624 SCM tmp;
625 SCM tl;
626 SCM ans;
627 int c;
628
629 c = scm_flush_ws (port, name);
630 if (')' == c)
631 return SCM_EOL;
632 scm_ungetc (c, port);
633 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
634 {
635 ans = scm_lreadr (tok_buf, port, copy);
636 closeit:
637 if (')' != (c = scm_flush_ws (port, name)))
638 SCM_MISC_ERROR ("missing close paren", SCM_EOL);
639 return ans;
640 }
641 ans = tl = scm_cons (tmp, SCM_EOL);
642 while (')' != (c = scm_flush_ws (port, name)))
643 {
644 scm_ungetc (c, port);
645 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
646 {
647 SCM_SETCDR (tl, scm_lreadr (tok_buf, port, copy));
648 goto closeit;
649 }
650 SCM_SETCDR (tl, scm_cons (tmp, SCM_EOL));
651 tl = SCM_CDR (tl);
652 }
653 return ans;
654 }
655 #undef FUNC_NAME
656
657
658 SCM
659 scm_lreadrecparen (SCM *tok_buf, SCM port, char *name, SCM *copy)
660 #define FUNC_NAME "scm_lreadrecparen"
661 {
662 register int c;
663 register SCM tmp;
664 register SCM tl, tl2 = SCM_EOL;
665 SCM ans, ans2 = SCM_EOL;
666 /* Need to capture line and column numbers here. */
667 int line = SCM_LINUM (port);
668 int column = SCM_COL (port) - 1;
669
670 c = scm_flush_ws (port, name);
671 if (')' == c)
672 return SCM_EOL;
673 scm_ungetc (c, port);
674 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
675 {
676 ans = scm_lreadr (tok_buf, port, copy);
677 if (')' != (c = scm_flush_ws (port, name)))
678 SCM_MISC_ERROR ("missing close paren", SCM_EOL);
679 return ans;
680 }
681 /* Build the head of the list structure. */
682 ans = tl = scm_cons (tmp, SCM_EOL);
683 if (SCM_COPY_SOURCE_P)
684 ans2 = tl2 = scm_cons (SCM_CONSP (tmp)
685 ? *copy
686 : tmp,
687 SCM_EOL);
688 while (')' != (c = scm_flush_ws (port, name)))
689 {
690 SCM new_tail;
691
692 scm_ungetc (c, port);
693 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
694 {
695 SCM_SETCDR (tl, tmp = scm_lreadr (tok_buf, port, copy));
696 if (SCM_COPY_SOURCE_P)
697 SCM_SETCDR (tl2, scm_cons (SCM_CONSP (tmp)
698 ? *copy
699 : tmp,
700 SCM_EOL));
701 if (')' != (c = scm_flush_ws (port, name)))
702 SCM_MISC_ERROR ("missing close paren", SCM_EOL);
703 goto exit;
704 }
705
706 new_tail = scm_cons (tmp, SCM_EOL);
707 SCM_SETCDR (tl, new_tail);
708 tl = new_tail;
709
710 if (SCM_COPY_SOURCE_P)
711 {
712 SCM new_tail2 = scm_cons (SCM_CONSP (tmp) ? *copy : tmp, SCM_EOL);
713 SCM_SETCDR (tl2, new_tail2);
714 tl2 = new_tail2;
715 }
716 }
717 exit:
718 scm_whash_insert (scm_source_whash,
719 ans,
720 scm_make_srcprops (line,
721 column,
722 SCM_FILENAME (port),
723 SCM_COPY_SOURCE_P
724 ? *copy = ans2
725 : SCM_UNDEFINED,
726 SCM_EOL));
727 return ans;
728 }
729 #undef FUNC_NAME
730
731
732 \f
733
734 /* Manipulate the read-hash-procedures alist. This could be written in
735 Scheme, but maybe it will also be used by C code during initialisation. */
736 SCM_DEFINE (scm_read_hash_extend, "read-hash-extend", 2, 0, 0,
737 (SCM chr, SCM proc),
738 "Install the procedure @var{proc} for reading expressions\n"
739 "starting with the character sequence @code{#} and @var{chr}.\n"
740 "@var{proc} will be called with two arguments: the character\n"
741 "@var{chr} and the port to read further data from. The object\n"
742 "returned will be the return value of @code{read}.")
743 #define FUNC_NAME s_scm_read_hash_extend
744 {
745 SCM this;
746 SCM prev;
747
748 SCM_VALIDATE_CHAR (1, chr);
749 SCM_ASSERT (SCM_FALSEP (proc)
750 || SCM_EQ_P (scm_procedure_p (proc), SCM_BOOL_T),
751 proc, SCM_ARG2, FUNC_NAME);
752
753 /* Check if chr is already in the alist. */
754 this = *scm_read_hash_procedures;
755 prev = SCM_BOOL_F;
756 while (1)
757 {
758 if (SCM_NULLP (this))
759 {
760 /* not found, so add it to the beginning. */
761 if (!SCM_FALSEP (proc))
762 {
763 *scm_read_hash_procedures =
764 scm_cons (scm_cons (chr, proc), *scm_read_hash_procedures);
765 }
766 break;
767 }
768 if (SCM_EQ_P (chr, SCM_CAAR (this)))
769 {
770 /* already in the alist. */
771 if (SCM_FALSEP (proc))
772 {
773 /* remove it. */
774 if (SCM_FALSEP (prev))
775 {
776 *scm_read_hash_procedures =
777 SCM_CDR (*scm_read_hash_procedures);
778 }
779 else
780 scm_set_cdr_x (prev, SCM_CDR (this));
781 }
782 else
783 {
784 /* replace it. */
785 scm_set_cdr_x (SCM_CAR (this), proc);
786 }
787 break;
788 }
789 prev = this;
790 this = SCM_CDR (this);
791 }
792
793 return SCM_UNSPECIFIED;
794 }
795 #undef FUNC_NAME
796
797 /* Recover the read-hash procedure corresponding to char c. */
798 static SCM
799 scm_get_hash_procedure (int c)
800 {
801 SCM rest = *scm_read_hash_procedures;
802
803 while (1)
804 {
805 if (SCM_NULLP (rest))
806 return SCM_BOOL_F;
807
808 if (SCM_CHAR (SCM_CAAR (rest)) == c)
809 return SCM_CDAR (rest);
810
811 rest = SCM_CDR (rest);
812 }
813 }
814
815 void
816 scm_init_read ()
817 {
818 scm_read_hash_procedures =
819 SCM_VARIABLE_LOC (scm_c_define ("read-hash-procedures", SCM_EOL));
820
821 scm_init_opts (scm_read_options, scm_read_opts, SCM_N_READ_OPTIONS);
822 #ifndef SCM_MAGIC_SNARFER
823 #include "libguile/read.x"
824 #endif
825 }
826
827 /*
828 Local Variables:
829 c-file-style: "gnu"
830 End:
831 */