* strings.h: don't use SCM_P. don't include <string.h>.
[bpt/guile.git] / ice-9 / boot-9.scm
1 ;;; installed-scm-file
2
3 ;;;; Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
4 ;;;;
5 ;;;; This program is free software; you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation; either version 2, or (at your option)
8 ;;;; any later version.
9 ;;;;
10 ;;;; This program is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;;;; GNU General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with this software; see the file COPYING. If not, write to
17 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 ;;;; Boston, MA 02111-1307 USA
19 ;;;;
20 \f
21
22 ;;; This file is the first thing loaded into Guile. It adds many mundane
23 ;;; definitions and a few that are interesting.
24 ;;;
25 ;;; The module system (hence the hierarchical namespace) are defined in this
26 ;;; file.
27 ;;;
28
29 \f
30 ;;; {Features}
31 ;;
32
33 (define (provide sym)
34 (if (not (memq sym *features*))
35 (set! *features* (cons sym *features*))))
36
37 ;;; Return #t iff FEATURE is available to this Guile interpreter.
38 ;;; In SLIB, provided? also checks to see if the module is available.
39 ;;; We should do that too, but don't.
40 (define (provided? feature)
41 (and (memq feature *features*) #t))
42
43 ;;; presumably deprecated.
44 (define feature? provided?)
45
46 ;;; let format alias simple-format until the more complete version is loaded
47 (define format simple-format)
48
49 \f
50 ;;; {R4RS compliance}
51
52 (primitive-load-path "ice-9/r4rs.scm")
53
54 \f
55 ;;; {Simple Debugging Tools}
56 ;;
57
58
59 ;; peek takes any number of arguments, writes them to the
60 ;; current ouput port, and returns the last argument.
61 ;; It is handy to wrap around an expression to look at
62 ;; a value each time is evaluated, e.g.:
63 ;;
64 ;; (+ 10 (troublesome-fn))
65 ;; => (+ 10 (pk 'troublesome-fn-returned (troublesome-fn)))
66 ;;
67
68 (define (peek . stuff)
69 (newline)
70 (display ";;; ")
71 (write stuff)
72 (newline)
73 (car (last-pair stuff)))
74
75 (define pk peek)
76
77 (define (warn . stuff)
78 (with-output-to-port (current-error-port)
79 (lambda ()
80 (newline)
81 (display ";;; WARNING ")
82 (display stuff)
83 (newline)
84 (car (last-pair stuff)))))
85
86 \f
87 ;;; {Trivial Functions}
88 ;;;
89
90 (define (id x) x)
91 (define (1+ n) (+ n 1))
92 (define (-1+ n) (+ n -1))
93 (define 1- -1+)
94 (define return-it noop)
95 (define (and=> value procedure) (and value (procedure value)))
96 (define (make-hash-table k) (make-vector k '()))
97
98 ;;; apply-to-args is functionally redunant with apply and, worse,
99 ;;; is less general than apply since it only takes two arguments.
100 ;;;
101 ;;; On the other hand, apply-to-args is a syntacticly convenient way to
102 ;;; perform binding in many circumstances when the "let" family of
103 ;;; of forms don't cut it. E.g.:
104 ;;;
105 ;;; (apply-to-args (return-3d-mouse-coords)
106 ;;; (lambda (x y z)
107 ;;; ...))
108 ;;;
109
110 (define (apply-to-args args fn) (apply fn args))
111
112 \f
113 ;;; {Integer Math}
114 ;;;
115
116 (define (ipow-by-squaring x k acc proc)
117 (cond ((zero? k) acc)
118 ((= 1 k) (proc acc x))
119 (else (ipow-by-squaring (proc x x)
120 (quotient k 2)
121 (if (even? k) acc (proc acc x))
122 proc))))
123
124 (define string-character-length string-length)
125
126
127
128 ;; A convenience function for combining flag bits. Like logior, but
129 ;; handles the cases of 0 and 1 arguments.
130 ;;
131 (define (flags . args)
132 (cond
133 ((null? args) 0)
134 ((null? (cdr args)) (car args))
135 (else (apply logior args))))
136
137 \f
138 ;;; {Symbol Properties}
139 ;;;
140
141 (define (symbol-property sym prop)
142 (let ((pair (assoc prop (symbol-pref sym))))
143 (and pair (cdr pair))))
144
145 (define (set-symbol-property! sym prop val)
146 (let ((pair (assoc prop (symbol-pref sym))))
147 (if pair
148 (set-cdr! pair val)
149 (symbol-pset! sym (acons prop val (symbol-pref sym))))))
150
151 (define (symbol-property-remove! sym prop)
152 (let ((pair (assoc prop (symbol-pref sym))))
153 (if pair
154 (symbol-pset! sym (delq! pair (symbol-pref sym))))))
155
156 \f
157
158 ;;; {Line and Delimited I/O}
159
160 ;;; corresponds to SCM_LINE_INCREMENTORS in libguile.
161 (define scm-line-incrementors "\n")
162
163 (define (read-line! string . maybe-port)
164 (let* ((port (if (pair? maybe-port)
165 (car maybe-port)
166 (current-input-port))))
167 (let* ((rv (%read-delimited! scm-line-incrementors
168 string
169 #t
170 port))
171 (terminator (car rv))
172 (nchars (cdr rv)))
173 (cond ((and (= nchars 0)
174 (eof-object? terminator))
175 terminator)
176 ((not terminator) #f)
177 (else nchars)))))
178
179 (define (read-delimited! delims buf . args)
180 (let* ((num-args (length args))
181 (port (if (> num-args 0)
182 (car args)
183 (current-input-port)))
184 (handle-delim (if (> num-args 1)
185 (cadr args)
186 'trim))
187 (start (if (> num-args 2)
188 (caddr args)
189 0))
190 (end (if (> num-args 3)
191 (cadddr args)
192 (string-length buf))))
193 (let* ((rv (%read-delimited! delims
194 buf
195 (not (eq? handle-delim 'peek))
196 port
197 start
198 end))
199 (terminator (car rv))
200 (nchars (cdr rv)))
201 (cond ((or (not terminator) ; buffer filled
202 (eof-object? terminator))
203 (if (zero? nchars)
204 (if (eq? handle-delim 'split)
205 (cons terminator terminator)
206 terminator)
207 (if (eq? handle-delim 'split)
208 (cons nchars terminator)
209 nchars)))
210 (else
211 (case handle-delim
212 ((trim peek) nchars)
213 ((concat) (string-set! buf (+ nchars start) terminator)
214 (+ nchars 1))
215 ((split) (cons nchars terminator))
216 (else (error "unexpected handle-delim value: "
217 handle-delim))))))))
218
219 (define (read-delimited delims . args)
220 (let* ((port (if (pair? args)
221 (let ((pt (car args)))
222 (set! args (cdr args))
223 pt)
224 (current-input-port)))
225 (handle-delim (if (pair? args)
226 (car args)
227 'trim)))
228 (let loop ((substrings ())
229 (total-chars 0)
230 (buf-size 100)) ; doubled each time through.
231 (let* ((buf (make-string buf-size))
232 (rv (%read-delimited! delims
233 buf
234 (not (eq? handle-delim 'peek))
235 port))
236 (terminator (car rv))
237 (nchars (cdr rv))
238 (join-substrings
239 (lambda ()
240 (apply string-append
241 (reverse
242 (cons (if (and (eq? handle-delim 'concat)
243 (not (eof-object? terminator)))
244 (string terminator)
245 "")
246 (cons (make-shared-substring buf 0 nchars)
247 substrings))))))
248 (new-total (+ total-chars nchars)))
249 (cond ((not terminator)
250 ;; buffer filled.
251 (loop (cons (substring buf 0 nchars) substrings)
252 new-total
253 (* buf-size 2)))
254 ((eof-object? terminator)
255 (if (zero? new-total)
256 (if (eq? handle-delim 'split)
257 (cons terminator terminator)
258 terminator)
259 (if (eq? handle-delim 'split)
260 (cons (join-substrings) terminator)
261 (join-substrings))))
262 (else
263 (case handle-delim
264 ((trim peek concat) (join-substrings))
265 ((split) (cons (join-substrings) terminator))
266
267
268 (else (error "unexpected handle-delim value: "
269 handle-delim)))))))))
270
271 ;;; read-line [PORT [HANDLE-DELIM]] reads a newline-terminated string
272 ;;; from PORT. The return value depends on the value of HANDLE-DELIM,
273 ;;; which may be one of the symbols `trim', `concat', `peek' and
274 ;;; `split'. If it is `trim' (the default), the trailing newline is
275 ;;; removed and the string is returned. If `concat', the string is
276 ;;; returned with the trailing newline intact. If `peek', the newline
277 ;;; is left in the input port buffer and the string is returned. If
278 ;;; `split', the newline is split from the string and read-line
279 ;;; returns a pair consisting of the truncated string and the newline.
280
281 (define (read-line . args)
282 (let* ((port (if (null? args)
283 (current-input-port)
284 (car args)))
285 (handle-delim (if (> (length args) 1)
286 (cadr args)
287 'trim))
288 (line/delim (%read-line port))
289 (line (car line/delim))
290 (delim (cdr line/delim)))
291 (case handle-delim
292 ((trim) line)
293 ((split) line/delim)
294 ((concat) (if (and (string? line) (char? delim))
295 (string-append line (string delim))
296 line))
297 ((peek) (if (char? delim)
298 (unread-char delim port))
299 line)
300 (else
301 (error "unexpected handle-delim value: " handle-delim)))))
302
303 \f
304 ;;; {Arrays}
305 ;;;
306
307 (if (provided? 'array)
308 (primitive-load-path "ice-9/arrays.scm"))
309
310 \f
311 ;;; {Keywords}
312 ;;;
313
314 (define (symbol->keyword symbol)
315 (make-keyword-from-dash-symbol (symbol-append '- symbol)))
316
317 (define (keyword->symbol kw)
318 (let ((sym (keyword-dash-symbol kw)))
319 (string->symbol (substring sym 1 (string-length sym)))))
320
321 (define (kw-arg-ref args kw)
322 (let ((rem (member kw args)))
323 (and rem (pair? (cdr rem)) (cadr rem))))
324
325 \f
326
327 ;;; {Structs}
328
329 (define (struct-layout s)
330 (struct-ref (struct-vtable s) vtable-index-layout))
331
332 \f
333 ;;; {Records}
334 ;;;
335
336 ;; Printing records: by default, records are printed as
337 ;;
338 ;; #<type-name field1: val1 field2: val2 ...>
339 ;;
340 ;; You can change that by giving a custom printing function to
341 ;; MAKE-RECORD-TYPE (after the list of field symbols). This function
342 ;; will be called like
343 ;;
344 ;; (<printer> object port)
345 ;;
346 ;; It should print OBJECT to PORT.
347
348 (define (inherit-print-state old-port new-port)
349 (if (get-print-state old-port)
350 (port-with-print-state new-port (get-print-state old-port))
351 new-port))
352
353 ;; 0: type-name, 1: fields
354 (define record-type-vtable
355 (make-vtable-vtable "prpr" 0
356 (lambda (s p)
357 (cond ((eq? s record-type-vtable)
358 (display "#<record-type-vtable>" p))
359 (else
360 (display "#<record-type " p)
361 (display (record-type-name s) p)
362 (display ">" p))))))
363
364 (define (record-type? obj)
365 (and (struct? obj) (eq? record-type-vtable (struct-vtable obj))))
366
367 (define (make-record-type type-name fields . opt)
368 (let ((printer-fn (and (pair? opt) (car opt))))
369 (let ((struct (make-struct record-type-vtable 0
370 (make-struct-layout
371 (apply symbol-append
372 (map (lambda (f) "pw") fields)))
373 (or printer-fn
374 (lambda (s p)
375 (display "#<" p)
376 (display type-name p)
377 (let loop ((fields fields)
378 (off 0))
379 (cond
380 ((not (null? fields))
381 (display " " p)
382 (display (car fields) p)
383 (display ": " p)
384 (display (struct-ref s off) p)
385 (loop (cdr fields) (+ 1 off)))))
386 (display ">" p)))
387 type-name
388 (copy-tree fields))))
389 ;; Temporary solution: Associate a name to the record type descriptor
390 ;; so that the object system can create a wrapper class for it.
391 (set-struct-vtable-name! struct (if (symbol? type-name)
392 type-name
393 (string->symbol type-name)))
394 struct)))
395
396 (define (record-type-name obj)
397 (if (record-type? obj)
398 (struct-ref obj vtable-offset-user)
399 (error 'not-a-record-type obj)))
400
401 (define (record-type-fields obj)
402 (if (record-type? obj)
403 (struct-ref obj (+ 1 vtable-offset-user))
404 (error 'not-a-record-type obj)))
405
406 (define (record-constructor rtd . opt)
407 (let ((field-names (if (pair? opt) (car opt) (record-type-fields rtd))))
408 (eval `(lambda ,field-names
409 (make-struct ',rtd 0 ,@(map (lambda (f)
410 (if (memq f field-names)
411 f
412 #f))
413 (record-type-fields rtd)))))))
414
415 (define (record-predicate rtd)
416 (lambda (obj) (and (struct? obj) (eq? rtd (struct-vtable obj)))))
417
418 (define (record-accessor rtd field-name)
419 (let* ((pos (list-index (record-type-fields rtd) field-name)))
420 (if (not pos)
421 (error 'no-such-field field-name))
422 (eval `(lambda (obj)
423 (and (eq? ',rtd (record-type-descriptor obj))
424 (struct-ref obj ,pos))))))
425
426 (define (record-modifier rtd field-name)
427 (let* ((pos (list-index (record-type-fields rtd) field-name)))
428 (if (not pos)
429 (error 'no-such-field field-name))
430 (eval `(lambda (obj val)
431 (and (eq? ',rtd (record-type-descriptor obj))
432 (struct-set! obj ,pos val))))))
433
434
435 (define (record? obj)
436 (and (struct? obj) (record-type? (struct-vtable obj))))
437
438 (define (record-type-descriptor obj)
439 (if (struct? obj)
440 (struct-vtable obj)
441 (error 'not-a-record obj)))
442
443 (provide 'record)
444
445 \f
446 ;;; {Booleans}
447 ;;;
448
449 (define (->bool x) (not (not x)))
450
451 \f
452 ;;; {Symbols}
453 ;;;
454
455 (define (symbol-append . args)
456 (string->symbol (apply string-append args)))
457
458 (define (list->symbol . args)
459 (string->symbol (apply list->string args)))
460
461 (define (symbol . args)
462 (string->symbol (apply string args)))
463
464 (define (obarray-symbol-append ob . args)
465 (string->obarray-symbol (apply string-append ob args)))
466
467 (define (obarray-gensym obarray . opt)
468 (if (null? opt)
469 (gensym "%%gensym" obarray)
470 (gensym (car opt) obarray)))
471
472 \f
473 ;;; {Lists}
474 ;;;
475
476 (define (list-index l k)
477 (let loop ((n 0)
478 (l l))
479 (and (not (null? l))
480 (if (eq? (car l) k)
481 n
482 (loop (+ n 1) (cdr l))))))
483
484 (define (make-list n . init)
485 (if (pair? init) (set! init (car init)))
486 (let loop ((answer '())
487 (n n))
488 (if (<= n 0)
489 answer
490 (loop (cons init answer) (- n 1)))))
491
492
493 \f
494 ;;; {Multiple return values}
495
496 (define *values-rtd*
497 (make-record-type "values"
498 '(values)))
499
500 (define values
501 (let ((make-values (record-constructor *values-rtd*)))
502 (lambda x
503 (if (and (not (null? x))
504 (null? (cdr x)))
505 (car x)
506 (make-values x)))))
507
508 (define call-with-values
509 (let ((access-values (record-accessor *values-rtd* 'values))
510 (values-predicate? (record-predicate *values-rtd*)))
511 (lambda (producer consumer)
512 (let ((result (producer)))
513 (if (values-predicate? result)
514 (apply consumer (access-values result))
515 (consumer result))))))
516
517 (provide 'values)
518
519 \f
520 ;;; {and-map and or-map}
521 ;;;
522 ;;; (and-map fn lst) is like (and (fn (car lst)) (fn (cadr lst)) (fn...) ...)
523 ;;; (or-map fn lst) is like (or (fn (car lst)) (fn (cadr lst)) (fn...) ...)
524 ;;; (map-in-order fn lst) is like (map fn lst) but definately in order of lst.
525 ;;;
526
527 ;; and-map f l
528 ;;
529 ;; Apply f to successive elements of l until exhaustion or f returns #f.
530 ;; If returning early, return #f. Otherwise, return the last value returned
531 ;; by f. If f has never been called because l is empty, return #t.
532 ;;
533 (define (and-map f lst)
534 (let loop ((result #t)
535 (l lst))
536 (and result
537 (or (and (null? l)
538 result)
539 (loop (f (car l)) (cdr l))))))
540
541 ;; or-map f l
542 ;;
543 ;; Apply f to successive elements of l until exhaustion or while f returns #f.
544 ;; If returning early, return the return value of f.
545 ;;
546 (define (or-map f lst)
547 (let loop ((result #f)
548 (l lst))
549 (or result
550 (and (not (null? l))
551 (loop (f (car l)) (cdr l))))))
552
553 \f
554
555 (if (provided? 'posix)
556 (primitive-load-path "ice-9/posix.scm"))
557
558 (if (provided? 'socket)
559 (primitive-load-path "ice-9/networking.scm"))
560
561 (define file-exists?
562 (if (provided? 'posix)
563 (lambda (str)
564 (access? str F_OK))
565 (lambda (str)
566 (let ((port (catch 'system-error (lambda () (open-file str OPEN_READ))
567 (lambda args #f))))
568 (if port (begin (close-port port) #t)
569 #f)))))
570
571 (define file-is-directory?
572 (if (provided? 'posix)
573 (lambda (str)
574 (eq? (stat:type (stat str)) 'directory))
575 (lambda (str)
576 (let ((port (catch 'system-error
577 (lambda () (open-file (string-append str "/.")
578 OPEN_READ))
579 (lambda args #f))))
580 (if port (begin (close-port port) #t)
581 #f)))))
582
583 (define (has-suffix? str suffix)
584 (let ((sufl (string-length suffix))
585 (sl (string-length str)))
586 (and (> sl sufl)
587 (string=? (substring str (- sl sufl) sl) suffix))))
588
589 \f
590 ;;; {Error Handling}
591 ;;;
592
593 (define (error . args)
594 (save-stack)
595 (if (null? args)
596 (scm-error 'misc-error #f "?" #f #f)
597 (let loop ((msg "~A")
598 (rest (cdr args)))
599 (if (not (null? rest))
600 (loop (string-append msg " ~S")
601 (cdr rest))
602 (scm-error 'misc-error #f msg args #f)))))
603
604 ;; bad-throw is the hook that is called upon a throw to a an unhandled
605 ;; key (unless the throw has four arguments, in which case
606 ;; it's usually interpreted as an error throw.)
607 ;; If the key has a default handler (a throw-handler-default property),
608 ;; it is applied to the throw.
609 ;;
610 (define (bad-throw key . args)
611 (let ((default (symbol-property key 'throw-handler-default)))
612 (or (and default (apply default key args))
613 (apply error "unhandled-exception:" key args))))
614
615 \f
616
617 (define (tm:sec obj) (vector-ref obj 0))
618 (define (tm:min obj) (vector-ref obj 1))
619 (define (tm:hour obj) (vector-ref obj 2))
620 (define (tm:mday obj) (vector-ref obj 3))
621 (define (tm:mon obj) (vector-ref obj 4))
622 (define (tm:year obj) (vector-ref obj 5))
623 (define (tm:wday obj) (vector-ref obj 6))
624 (define (tm:yday obj) (vector-ref obj 7))
625 (define (tm:isdst obj) (vector-ref obj 8))
626 (define (tm:gmtoff obj) (vector-ref obj 9))
627 (define (tm:zone obj) (vector-ref obj 10))
628
629 (define (set-tm:sec obj val) (vector-set! obj 0 val))
630 (define (set-tm:min obj val) (vector-set! obj 1 val))
631 (define (set-tm:hour obj val) (vector-set! obj 2 val))
632 (define (set-tm:mday obj val) (vector-set! obj 3 val))
633 (define (set-tm:mon obj val) (vector-set! obj 4 val))
634 (define (set-tm:year obj val) (vector-set! obj 5 val))
635 (define (set-tm:wday obj val) (vector-set! obj 6 val))
636 (define (set-tm:yday obj val) (vector-set! obj 7 val))
637 (define (set-tm:isdst obj val) (vector-set! obj 8 val))
638 (define (set-tm:gmtoff obj val) (vector-set! obj 9 val))
639 (define (set-tm:zone obj val) (vector-set! obj 10 val))
640
641 (define (tms:clock obj) (vector-ref obj 0))
642 (define (tms:utime obj) (vector-ref obj 1))
643 (define (tms:stime obj) (vector-ref obj 2))
644 (define (tms:cutime obj) (vector-ref obj 3))
645 (define (tms:cstime obj) (vector-ref obj 4))
646
647 (define (file-position . args) (apply ftell args))
648 (define (file-set-position . args) (apply fseek args))
649
650 (define (move->fdes fd/port fd)
651 (cond ((integer? fd/port)
652 (dup->fdes fd/port fd)
653 (close fd/port)
654 fd)
655 (else
656 (primitive-move->fdes fd/port fd)
657 (set-port-revealed! fd/port 1)
658 fd/port)))
659
660 (define (release-port-handle port)
661 (let ((revealed (port-revealed port)))
662 (if (> revealed 0)
663 (set-port-revealed! port (- revealed 1)))))
664
665 (define (dup->port port/fd mode . maybe-fd)
666 (let ((port (fdopen (apply dup->fdes port/fd maybe-fd)
667 mode)))
668 (if (pair? maybe-fd)
669 (set-port-revealed! port 1))
670 port))
671
672 (define (dup->inport port/fd . maybe-fd)
673 (apply dup->port port/fd "r" maybe-fd))
674
675 (define (dup->outport port/fd . maybe-fd)
676 (apply dup->port port/fd "w" maybe-fd))
677
678 (define (dup port/fd . maybe-fd)
679 (if (integer? port/fd)
680 (apply dup->fdes port/fd maybe-fd)
681 (apply dup->port port/fd (port-mode port/fd) maybe-fd)))
682
683 (define (duplicate-port port modes)
684 (dup->port port modes))
685
686 (define (fdes->inport fdes)
687 (let loop ((rest-ports (fdes->ports fdes)))
688 (cond ((null? rest-ports)
689 (let ((result (fdopen fdes "r")))
690 (set-port-revealed! result 1)
691 result))
692 ((input-port? (car rest-ports))
693 (set-port-revealed! (car rest-ports)
694 (+ (port-revealed (car rest-ports)) 1))
695 (car rest-ports))
696 (else
697 (loop (cdr rest-ports))))))
698
699 (define (fdes->outport fdes)
700 (let loop ((rest-ports (fdes->ports fdes)))
701 (cond ((null? rest-ports)
702 (let ((result (fdopen fdes "w")))
703 (set-port-revealed! result 1)
704 result))
705 ((output-port? (car rest-ports))
706 (set-port-revealed! (car rest-ports)
707 (+ (port-revealed (car rest-ports)) 1))
708 (car rest-ports))
709 (else
710 (loop (cdr rest-ports))))))
711
712 (define (port->fdes port)
713 (set-port-revealed! port (+ (port-revealed port) 1))
714 (fileno port))
715
716 (define (setenv name value)
717 (if value
718 (putenv (string-append name "=" value))
719 (putenv name)))
720
721 \f
722 ;;; {Load Paths}
723 ;;;
724
725 ;;; Here for backward compatability
726 ;;
727 (define scheme-file-suffix (lambda () ".scm"))
728
729 (define (in-vicinity vicinity file)
730 (let ((tail (let ((len (string-length vicinity)))
731 (if (zero? len)
732 #f
733 (string-ref vicinity (- len 1))))))
734 (string-append vicinity
735 (if (or (not tail)
736 (eq? tail #\/))
737 ""
738 "/")
739 file)))
740
741 \f
742 ;;; {Help for scm_shell}
743 ;;; The argument-processing code used by Guile-based shells generates
744 ;;; Scheme code based on the argument list. This page contains help
745 ;;; functions for the code it generates.
746
747 (define (command-line) (program-arguments))
748
749 ;; This is mostly for the internal use of the code generated by
750 ;; scm_compile_shell_switches.
751 (define (load-user-init)
752 (define (existing-file dir)
753 (let ((path (in-vicinity dir ".guile")))
754 (if (and (file-exists? path)
755 (not (file-is-directory? path)))
756 path
757 #f)))
758 (let ((path (or (existing-file (or (getenv "HOME") "/"))
759 (and (provided? 'posix)
760 (existing-file (passwd:dir (getpw (getuid))))))))
761 (if path (primitive-load path))))
762
763 \f
764 ;;; {Loading by paths}
765
766 ;;; Load a Scheme source file named NAME, searching for it in the
767 ;;; directories listed in %load-path, and applying each of the file
768 ;;; name extensions listed in %load-extensions.
769 (define (load-from-path name)
770 (start-stack 'load-stack
771 (primitive-load-path name)))
772
773
774 \f
775 ;;; {Transcendental Functions}
776 ;;;
777 ;;; Derived from "Transcen.scm", Complex trancendental functions for SCM.
778 ;;; Written by Jerry D. Hedden, (C) FSF.
779 ;;; See the file `COPYING' for terms applying to this program.
780 ;;;
781
782 (define (exp z)
783 (if (real? z) ($exp z)
784 (make-polar ($exp (real-part z)) (imag-part z))))
785
786 (define (log z)
787 (if (and (real? z) (>= z 0))
788 ($log z)
789 (make-rectangular ($log (magnitude z)) (angle z))))
790
791 (define (sqrt z)
792 (if (real? z)
793 (if (negative? z) (make-rectangular 0 ($sqrt (- z)))
794 ($sqrt z))
795 (make-polar ($sqrt (magnitude z)) (/ (angle z) 2))))
796
797 (define expt
798 (let ((integer-expt integer-expt))
799 (lambda (z1 z2)
800 (cond ((exact? z2)
801 (integer-expt z1 z2))
802 ((and (real? z2) (real? z1) (>= z1 0))
803 ($expt z1 z2))
804 (else
805 (exp (* z2 (log z1))))))))
806
807 (define (sinh z)
808 (if (real? z) ($sinh z)
809 (let ((x (real-part z)) (y (imag-part z)))
810 (make-rectangular (* ($sinh x) ($cos y))
811 (* ($cosh x) ($sin y))))))
812 (define (cosh z)
813 (if (real? z) ($cosh z)
814 (let ((x (real-part z)) (y (imag-part z)))
815 (make-rectangular (* ($cosh x) ($cos y))
816 (* ($sinh x) ($sin y))))))
817 (define (tanh z)
818 (if (real? z) ($tanh z)
819 (let* ((x (* 2 (real-part z)))
820 (y (* 2 (imag-part z)))
821 (w (+ ($cosh x) ($cos y))))
822 (make-rectangular (/ ($sinh x) w) (/ ($sin y) w)))))
823
824 (define (asinh z)
825 (if (real? z) ($asinh z)
826 (log (+ z (sqrt (+ (* z z) 1))))))
827
828 (define (acosh z)
829 (if (and (real? z) (>= z 1))
830 ($acosh z)
831 (log (+ z (sqrt (- (* z z) 1))))))
832
833 (define (atanh z)
834 (if (and (real? z) (> z -1) (< z 1))
835 ($atanh z)
836 (/ (log (/ (+ 1 z) (- 1 z))) 2)))
837
838 (define (sin z)
839 (if (real? z) ($sin z)
840 (let ((x (real-part z)) (y (imag-part z)))
841 (make-rectangular (* ($sin x) ($cosh y))
842 (* ($cos x) ($sinh y))))))
843 (define (cos z)
844 (if (real? z) ($cos z)
845 (let ((x (real-part z)) (y (imag-part z)))
846 (make-rectangular (* ($cos x) ($cosh y))
847 (- (* ($sin x) ($sinh y)))))))
848 (define (tan z)
849 (if (real? z) ($tan z)
850 (let* ((x (* 2 (real-part z)))
851 (y (* 2 (imag-part z)))
852 (w (+ ($cos x) ($cosh y))))
853 (make-rectangular (/ ($sin x) w) (/ ($sinh y) w)))))
854
855 (define (asin z)
856 (if (and (real? z) (>= z -1) (<= z 1))
857 ($asin z)
858 (* -i (asinh (* +i z)))))
859
860 (define (acos z)
861 (if (and (real? z) (>= z -1) (<= z 1))
862 ($acos z)
863 (+ (/ (angle -1) 2) (* +i (asinh (* +i z))))))
864
865 (define (atan z . y)
866 (if (null? y)
867 (if (real? z) ($atan z)
868 (/ (log (/ (- +i z) (+ +i z))) +2i))
869 ($atan2 z (car y))))
870
871 (set! abs magnitude)
872
873 (define (log10 arg)
874 (/ (log arg) (log 10)))
875
876 \f
877
878 ;;; {Reader Extensions}
879 ;;;
880
881 ;;; Reader code for various "#c" forms.
882 ;;;
883
884 (read-hash-extend #\' (lambda (c port)
885 (read port)))
886 (read-hash-extend #\. (lambda (c port)
887 (eval (read port))))
888
889 \f
890 ;;; {Command Line Options}
891 ;;;
892
893 (define (get-option argv kw-opts kw-args return)
894 (cond
895 ((null? argv)
896 (return #f #f argv))
897
898 ((or (not (eq? #\- (string-ref (car argv) 0)))
899 (eq? (string-length (car argv)) 1))
900 (return 'normal-arg (car argv) (cdr argv)))
901
902 ((eq? #\- (string-ref (car argv) 1))
903 (let* ((kw-arg-pos (or (string-index (car argv) #\=)
904 (string-length (car argv))))
905 (kw (symbol->keyword (substring (car argv) 2 kw-arg-pos)))
906 (kw-opt? (member kw kw-opts))
907 (kw-arg? (member kw kw-args))
908 (arg (or (and (not (eq? kw-arg-pos (string-length (car argv))))
909 (substring (car argv)
910 (+ kw-arg-pos 1)
911 (string-length (car argv))))
912 (and kw-arg?
913 (begin (set! argv (cdr argv)) (car argv))))))
914 (if (or kw-opt? kw-arg?)
915 (return kw arg (cdr argv))
916 (return 'usage-error kw (cdr argv)))))
917
918 (else
919 (let* ((char (substring (car argv) 1 2))
920 (kw (symbol->keyword char)))
921 (cond
922
923 ((member kw kw-opts)
924 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
925 (new-argv (if (= 0 (string-length rest-car))
926 (cdr argv)
927 (cons (string-append "-" rest-car) (cdr argv)))))
928 (return kw #f new-argv)))
929
930 ((member kw kw-args)
931 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
932 (arg (if (= 0 (string-length rest-car))
933 (cadr argv)
934 rest-car))
935 (new-argv (if (= 0 (string-length rest-car))
936 (cddr argv)
937 (cdr argv))))
938 (return kw arg new-argv)))
939
940 (else (return 'usage-error kw argv)))))))
941
942 (define (for-next-option proc argv kw-opts kw-args)
943 (let loop ((argv argv))
944 (get-option argv kw-opts kw-args
945 (lambda (opt opt-arg argv)
946 (and opt (proc opt opt-arg argv loop))))))
947
948 (define (display-usage-report kw-desc)
949 (for-each
950 (lambda (kw)
951 (or (eq? (car kw) #t)
952 (eq? (car kw) 'else)
953 (let* ((opt-desc kw)
954 (help (cadr opt-desc))
955 (opts (car opt-desc))
956 (opts-proper (if (string? (car opts)) (cdr opts) opts))
957 (arg-name (if (string? (car opts))
958 (string-append "<" (car opts) ">")
959 ""))
960 (left-part (string-append
961 (with-output-to-string
962 (lambda ()
963 (map (lambda (x) (display (keyword-symbol x)) (display " "))
964 opts-proper)))
965 arg-name))
966 (middle-part (if (and (< (string-length left-part) 30)
967 (< (string-length help) 40))
968 (make-string (- 30 (string-length left-part)) #\ )
969 "\n\t")))
970 (display left-part)
971 (display middle-part)
972 (display help)
973 (newline))))
974 kw-desc))
975
976
977
978 (define (transform-usage-lambda cases)
979 (let* ((raw-usage (delq! 'else (map car cases)))
980 (usage-sans-specials (map (lambda (x)
981 (or (and (not (list? x)) x)
982 (and (symbol? (car x)) #t)
983 (and (boolean? (car x)) #t)
984 x))
985 raw-usage))
986 (usage-desc (delq! #t usage-sans-specials))
987 (kw-desc (map car usage-desc))
988 (kw-opts (apply append (map (lambda (x) (and (not (string? (car x))) x)) kw-desc)))
989 (kw-args (apply append (map (lambda (x) (and (string? (car x)) (cdr x))) kw-desc)))
990 (transmogrified-cases (map (lambda (case)
991 (cons (let ((opts (car case)))
992 (if (or (boolean? opts) (eq? 'else opts))
993 opts
994 (cond
995 ((symbol? (car opts)) opts)
996 ((boolean? (car opts)) opts)
997 ((string? (caar opts)) (cdar opts))
998 (else (car opts)))))
999 (cdr case)))
1000 cases)))
1001 `(let ((%display-usage (lambda () (display-usage-report ',usage-desc))))
1002 (lambda (%argv)
1003 (let %next-arg ((%argv %argv))
1004 (get-option %argv
1005 ',kw-opts
1006 ',kw-args
1007 (lambda (%opt %arg %new-argv)
1008 (case %opt
1009 ,@ transmogrified-cases))))))))
1010
1011
1012 \f
1013
1014 ;;; {Low Level Modules}
1015 ;;;
1016 ;;; These are the low level data structures for modules.
1017 ;;;
1018 ;;; !!! warning: The interface to lazy binder procedures is going
1019 ;;; to be changed in an incompatible way to permit all the basic
1020 ;;; module ops to be virtualized.
1021 ;;;
1022 ;;; (make-module size use-list lazy-binding-proc) => module
1023 ;;; module-{obarray,uses,binder}[|-set!]
1024 ;;; (module? obj) => [#t|#f]
1025 ;;; (module-locally-bound? module symbol) => [#t|#f]
1026 ;;; (module-bound? module symbol) => [#t|#f]
1027 ;;; (module-symbol-locally-interned? module symbol) => [#t|#f]
1028 ;;; (module-symbol-interned? module symbol) => [#t|#f]
1029 ;;; (module-local-variable module symbol) => [#<variable ...> | #f]
1030 ;;; (module-variable module symbol) => [#<variable ...> | #f]
1031 ;;; (module-symbol-binding module symbol opt-value)
1032 ;;; => [ <obj> | opt-value | an error occurs ]
1033 ;;; (module-make-local-var! module symbol) => #<variable...>
1034 ;;; (module-add! module symbol var) => unspecified
1035 ;;; (module-remove! module symbol) => unspecified
1036 ;;; (module-for-each proc module) => unspecified
1037 ;;; (make-scm-module) => module ; a lazy copy of the symhash module
1038 ;;; (set-current-module module) => unspecified
1039 ;;; (current-module) => #<module...>
1040 ;;;
1041 ;;;
1042
1043 \f
1044 ;;; {Printing Modules}
1045 ;; This is how modules are printed. You can re-define it.
1046 ;; (Redefining is actually more complicated than simply redefining
1047 ;; %print-module because that would only change the binding and not
1048 ;; the value stored in the vtable that determines how record are
1049 ;; printed. Sigh.)
1050
1051 (define (%print-module mod port) ; unused args: depth length style table)
1052 (display "#<" port)
1053 (display (or (module-kind mod) "module") port)
1054 (let ((name (module-name mod)))
1055 (if name
1056 (begin
1057 (display " " port)
1058 (display name port))))
1059 (display " " port)
1060 (display (number->string (object-address mod) 16) port)
1061 (display ">" port))
1062
1063 ;; module-type
1064 ;;
1065 ;; A module is characterized by an obarray in which local symbols
1066 ;; are interned, a list of modules, "uses", from which non-local
1067 ;; bindings can be inherited, and an optional lazy-binder which
1068 ;; is a (CLOSURE module symbol) which, as a last resort, can provide
1069 ;; bindings that would otherwise not be found locally in the module.
1070 ;;
1071 (define module-type
1072 (make-record-type 'module
1073 '(obarray uses binder eval-closure transformer name kind
1074 observers weak-observers observer-id)
1075 %print-module))
1076
1077 ;; make-module &opt size uses binder
1078 ;;
1079 ;; Create a new module, perhaps with a particular size of obarray,
1080 ;; initial uses list, or binding procedure.
1081 ;;
1082 (define make-module
1083 (lambda args
1084
1085 (define (parse-arg index default)
1086 (if (> (length args) index)
1087 (list-ref args index)
1088 default))
1089
1090 (if (> (length args) 3)
1091 (error "Too many args to make-module." args))
1092
1093 (let ((size (parse-arg 0 1021))
1094 (uses (parse-arg 1 '()))
1095 (binder (parse-arg 2 #f)))
1096
1097 (if (not (integer? size))
1098 (error "Illegal size to make-module." size))
1099 (if (not (and (list? uses)
1100 (and-map module? uses)))
1101 (error "Incorrect use list." uses))
1102 (if (and binder (not (procedure? binder)))
1103 (error
1104 "Lazy-binder expected to be a procedure or #f." binder))
1105
1106 (let ((module (module-constructor (make-vector size '())
1107 uses binder #f #f #f #f
1108 '()
1109 (make-weak-value-hash-table 31)
1110 0)))
1111
1112 ;; We can't pass this as an argument to module-constructor,
1113 ;; because we need it to close over a pointer to the module
1114 ;; itself.
1115 (set-module-eval-closure! module
1116 (lambda (symbol define?)
1117 (if define?
1118 (module-make-local-var! module symbol)
1119 (module-variable module symbol))))
1120
1121 module))))
1122
1123 (define module-constructor (record-constructor module-type))
1124 (define module-obarray (record-accessor module-type 'obarray))
1125 (define set-module-obarray! (record-modifier module-type 'obarray))
1126 (define module-uses (record-accessor module-type 'uses))
1127 (define set-module-uses! (record-modifier module-type 'uses))
1128 (define module-binder (record-accessor module-type 'binder))
1129 (define set-module-binder! (record-modifier module-type 'binder))
1130
1131 ;; NOTE: This binding is used in libguile/modules.c.
1132 (define module-eval-closure (record-accessor module-type 'eval-closure))
1133
1134 (define module-transformer (record-accessor module-type 'transformer))
1135 (define set-module-transformer! (record-modifier module-type 'transformer))
1136 (define module-name (record-accessor module-type 'name))
1137 (define set-module-name! (record-modifier module-type 'name))
1138 (define module-kind (record-accessor module-type 'kind))
1139 (define set-module-kind! (record-modifier module-type 'kind))
1140 (define module-observers (record-accessor module-type 'observers))
1141 (define set-module-observers! (record-modifier module-type 'observers))
1142 (define module-weak-observers (record-accessor module-type 'weak-observers))
1143 (define module-observer-id (record-accessor module-type 'observer-id))
1144 (define set-module-observer-id! (record-modifier module-type 'observer-id))
1145 (define module? (record-predicate module-type))
1146
1147 (define set-module-eval-closure!
1148 (let ((setter (record-modifier module-type 'eval-closure)))
1149 (lambda (module closure)
1150 (setter module closure)
1151 ;; Make it possible to lookup the module from the environment.
1152 ;; This implementation is correct since an eval closure can belong
1153 ;; to maximally one module.
1154 (set-procedure-property! closure 'module module))))
1155
1156 (define (eval-in-module exp module)
1157 (eval2 exp (module-eval-closure module)))
1158
1159 \f
1160 ;;; {Observer protocol}
1161 ;;;
1162
1163 (define (module-observe module proc)
1164 (set-module-observers! module (cons proc (module-observers module)))
1165 (cons module proc))
1166
1167 (define (module-observe-weak module proc)
1168 (let ((id (module-observer-id module)))
1169 (hash-set! (module-weak-observers module) id proc)
1170 (set-module-observer-id! module (+ 1 id))
1171 (cons module id)))
1172
1173 (define (module-unobserve token)
1174 (let ((module (car token))
1175 (id (cdr token)))
1176 (if (integer? id)
1177 (hash-remove! (module-weak-observers module) id)
1178 (set-module-observers! module (delq1! id (module-observers module)))))
1179 *unspecified*)
1180
1181 (define (module-modified m)
1182 (for-each (lambda (proc) (proc m)) (module-observers m))
1183 (hash-fold (lambda (id proc res) (proc m)) #f (module-weak-observers m)))
1184
1185 \f
1186 ;;; {Module Searching in General}
1187 ;;;
1188 ;;; We sometimes want to look for properties of a symbol
1189 ;;; just within the obarray of one module. If the property
1190 ;;; holds, then it is said to hold ``locally'' as in, ``The symbol
1191 ;;; DISPLAY is locally rebound in the module `safe-guile'.''
1192 ;;;
1193 ;;;
1194 ;;; Other times, we want to test for a symbol property in the obarray
1195 ;;; of M and, if it is not found there, try each of the modules in the
1196 ;;; uses list of M. This is the normal way of testing for some
1197 ;;; property, so we state these properties without qualification as
1198 ;;; in: ``The symbol 'fnord is interned in module M because it is
1199 ;;; interned locally in module M2 which is a member of the uses list
1200 ;;; of M.''
1201 ;;;
1202
1203 ;; module-search fn m
1204 ;;
1205 ;; return the first non-#f result of FN applied to M and then to
1206 ;; the modules in the uses of m, and so on recursively. If all applications
1207 ;; return #f, then so does this function.
1208 ;;
1209 (define (module-search fn m v)
1210 (define (loop pos)
1211 (and (pair? pos)
1212 (or (module-search fn (car pos) v)
1213 (loop (cdr pos)))))
1214 (or (fn m v)
1215 (loop (module-uses m))))
1216
1217
1218 ;;; {Is a symbol bound in a module?}
1219 ;;;
1220 ;;; Symbol S in Module M is bound if S is interned in M and if the binding
1221 ;;; of S in M has been set to some well-defined value.
1222 ;;;
1223
1224 ;; module-locally-bound? module symbol
1225 ;;
1226 ;; Is a symbol bound (interned and defined) locally in a given module?
1227 ;;
1228 (define (module-locally-bound? m v)
1229 (let ((var (module-local-variable m v)))
1230 (and var
1231 (variable-bound? var))))
1232
1233 ;; module-bound? module symbol
1234 ;;
1235 ;; Is a symbol bound (interned and defined) anywhere in a given module
1236 ;; or its uses?
1237 ;;
1238 (define (module-bound? m v)
1239 (module-search module-locally-bound? m v))
1240
1241 ;;; {Is a symbol interned in a module?}
1242 ;;;
1243 ;;; Symbol S in Module M is interned if S occurs in
1244 ;;; of S in M has been set to some well-defined value.
1245 ;;;
1246 ;;; It is possible to intern a symbol in a module without providing
1247 ;;; an initial binding for the corresponding variable. This is done
1248 ;;; with:
1249 ;;; (module-add! module symbol (make-undefined-variable))
1250 ;;;
1251 ;;; In that case, the symbol is interned in the module, but not
1252 ;;; bound there. The unbound symbol shadows any binding for that
1253 ;;; symbol that might otherwise be inherited from a member of the uses list.
1254 ;;;
1255
1256 (define (module-obarray-get-handle ob key)
1257 ((if (symbol? key) hashq-get-handle hash-get-handle) ob key))
1258
1259 (define (module-obarray-ref ob key)
1260 ((if (symbol? key) hashq-ref hash-ref) ob key))
1261
1262 (define (module-obarray-set! ob key val)
1263 ((if (symbol? key) hashq-set! hash-set!) ob key val))
1264
1265 (define (module-obarray-remove! ob key)
1266 ((if (symbol? key) hashq-remove! hash-remove!) ob key))
1267
1268 ;; module-symbol-locally-interned? module symbol
1269 ;;
1270 ;; is a symbol interned (not neccessarily defined) locally in a given module
1271 ;; or its uses? Interned symbols shadow inherited bindings even if
1272 ;; they are not themselves bound to a defined value.
1273 ;;
1274 (define (module-symbol-locally-interned? m v)
1275 (not (not (module-obarray-get-handle (module-obarray m) v))))
1276
1277 ;; module-symbol-interned? module symbol
1278 ;;
1279 ;; is a symbol interned (not neccessarily defined) anywhere in a given module
1280 ;; or its uses? Interned symbols shadow inherited bindings even if
1281 ;; they are not themselves bound to a defined value.
1282 ;;
1283 (define (module-symbol-interned? m v)
1284 (module-search module-symbol-locally-interned? m v))
1285
1286
1287 ;;; {Mapping modules x symbols --> variables}
1288 ;;;
1289
1290 ;; module-local-variable module symbol
1291 ;; return the local variable associated with a MODULE and SYMBOL.
1292 ;;
1293 ;;; This function is very important. It is the only function that can
1294 ;;; return a variable from a module other than the mutators that store
1295 ;;; new variables in modules. Therefore, this function is the location
1296 ;;; of the "lazy binder" hack.
1297 ;;;
1298 ;;; If symbol is defined in MODULE, and if the definition binds symbol
1299 ;;; to a variable, return that variable object.
1300 ;;;
1301 ;;; If the symbols is not found at first, but the module has a lazy binder,
1302 ;;; then try the binder.
1303 ;;;
1304 ;;; If the symbol is not found at all, return #f.
1305 ;;;
1306 (define (module-local-variable m v)
1307 ; (caddr
1308 ; (list m v
1309 (let ((b (module-obarray-ref (module-obarray m) v)))
1310 (or (and (variable? b) b)
1311 (and (module-binder m)
1312 ((module-binder m) m v #f)))))
1313 ;))
1314
1315 ;; module-variable module symbol
1316 ;;
1317 ;; like module-local-variable, except search the uses in the
1318 ;; case V is not found in M.
1319 ;;
1320 (define (module-variable m v)
1321 (module-search module-local-variable m v))
1322
1323
1324 ;;; {Mapping modules x symbols --> bindings}
1325 ;;;
1326 ;;; These are similar to the mapping to variables, except that the
1327 ;;; variable is dereferenced.
1328 ;;;
1329
1330 ;; module-symbol-binding module symbol opt-value
1331 ;;
1332 ;; return the binding of a variable specified by name within
1333 ;; a given module, signalling an error if the variable is unbound.
1334 ;; If the OPT-VALUE is passed, then instead of signalling an error,
1335 ;; return OPT-VALUE.
1336 ;;
1337 (define (module-symbol-local-binding m v . opt-val)
1338 (let ((var (module-local-variable m v)))
1339 (if var
1340 (variable-ref var)
1341 (if (not (null? opt-val))
1342 (car opt-val)
1343 (error "Locally unbound variable." v)))))
1344
1345 ;; module-symbol-binding module symbol opt-value
1346 ;;
1347 ;; return the binding of a variable specified by name within
1348 ;; a given module, signalling an error if the variable is unbound.
1349 ;; If the OPT-VALUE is passed, then instead of signalling an error,
1350 ;; return OPT-VALUE.
1351 ;;
1352 (define (module-symbol-binding m v . opt-val)
1353 (let ((var (module-variable m v)))
1354 (if var
1355 (variable-ref var)
1356 (if (not (null? opt-val))
1357 (car opt-val)
1358 (error "Unbound variable." v)))))
1359
1360
1361 \f
1362 ;;; {Adding Variables to Modules}
1363 ;;;
1364 ;;;
1365
1366
1367 ;; module-make-local-var! module symbol
1368 ;;
1369 ;; ensure a variable for V in the local namespace of M.
1370 ;; If no variable was already there, then create a new and uninitialzied
1371 ;; variable.
1372 ;;
1373 (define (module-make-local-var! m v)
1374 (or (let ((b (module-obarray-ref (module-obarray m) v)))
1375 (and (variable? b)
1376 (begin
1377 (module-modified m)
1378 b)))
1379 (and (module-binder m)
1380 ((module-binder m) m v #t))
1381 (begin
1382 (let ((answer (make-undefined-variable v)))
1383 (module-obarray-set! (module-obarray m) v answer)
1384 (module-modified m)
1385 answer))))
1386
1387 ;; module-add! module symbol var
1388 ;;
1389 ;; ensure a particular variable for V in the local namespace of M.
1390 ;;
1391 (define (module-add! m v var)
1392 (if (not (variable? var))
1393 (error "Bad variable to module-add!" var))
1394 (module-obarray-set! (module-obarray m) v var)
1395 (module-modified m))
1396
1397 ;; module-remove!
1398 ;;
1399 ;; make sure that a symbol is undefined in the local namespace of M.
1400 ;;
1401 (define (module-remove! m v)
1402 (module-obarray-remove! (module-obarray m) v)
1403 (module-modified m))
1404
1405 (define (module-clear! m)
1406 (vector-fill! (module-obarray m) '())
1407 (module-modified m))
1408
1409 ;; MODULE-FOR-EACH -- exported
1410 ;;
1411 ;; Call PROC on each symbol in MODULE, with arguments of (SYMBOL VARIABLE).
1412 ;;
1413 (define (module-for-each proc module)
1414 (let ((obarray (module-obarray module)))
1415 (do ((index 0 (+ index 1))
1416 (end (vector-length obarray)))
1417 ((= index end))
1418 (for-each
1419 (lambda (bucket)
1420 (proc (car bucket) (cdr bucket)))
1421 (vector-ref obarray index)))))
1422
1423
1424 (define (module-map proc module)
1425 (let* ((obarray (module-obarray module))
1426 (end (vector-length obarray)))
1427
1428 (let loop ((i 0)
1429 (answer '()))
1430 (if (= i end)
1431 answer
1432 (loop (+ 1 i)
1433 (append!
1434 (map (lambda (bucket)
1435 (proc (car bucket) (cdr bucket)))
1436 (vector-ref obarray i))
1437 answer))))))
1438 \f
1439
1440 ;;; {Low Level Bootstrapping}
1441 ;;;
1442
1443 ;; make-root-module
1444
1445 ;; A root module uses the symhash table (the system's privileged
1446 ;; obarray). Being inside a root module is like using SCM without
1447 ;; any module system.
1448 ;;
1449
1450
1451 (define (root-module-closure m s define?)
1452 (let ((bi (and (symbol-interned? #f s)
1453 (builtin-variable s))))
1454 (and bi
1455 (or define? (variable-bound? bi))
1456 (begin
1457 (module-add! m s bi)
1458 bi))))
1459
1460 (define (make-root-module)
1461 (make-module 1019 '() root-module-closure))
1462
1463
1464 ;; make-scm-module
1465
1466 ;; An scm module is a module into which the lazy binder copies
1467 ;; variable bindings from the system symhash table. The mapping is
1468 ;; one way only; newly introduced bindings in an scm module are not
1469 ;; copied back into the system symhash table (and can be used to override
1470 ;; bindings from the symhash table).
1471 ;;
1472
1473 (define (make-scm-module)
1474 (make-module 1019 '()
1475 (lambda (m s define?)
1476 (let ((bi (and (symbol-interned? #f s)
1477 (builtin-variable s))))
1478 (and bi
1479 (variable-bound? bi)
1480 (begin
1481 (module-add! m s bi)
1482 bi))))))
1483
1484
1485
1486
1487 ;; the-module
1488 ;;
1489 ;; NOTE: This binding is used in libguile/modules.c.
1490 ;;
1491 (define the-module #f)
1492
1493 ;; scm:eval-transformer
1494 ;;
1495 (define scm:eval-transformer #f)
1496
1497 ;; set-current-module module
1498 ;;
1499 ;; set the current module as viewed by the normalizer.
1500 ;;
1501 ;; NOTE: This binding is used in libguile/modules.c.
1502 ;;
1503 (define (set-current-module m)
1504 (set! the-module m)
1505 (if m
1506 (begin
1507 (set! *top-level-lookup-closure* (module-eval-closure the-module))
1508 (set! scm:eval-transformer (module-transformer the-module)))
1509 (set! *top-level-lookup-closure* #f)))
1510
1511
1512 ;; current-module
1513 ;;
1514 ;; return the current module as viewed by the normalizer.
1515 ;;
1516 (define (current-module) the-module)
1517 \f
1518 ;;; {Module-based Loading}
1519 ;;;
1520
1521 (define (save-module-excursion thunk)
1522 (let ((inner-module (current-module))
1523 (outer-module #f))
1524 (dynamic-wind (lambda ()
1525 (set! outer-module (current-module))
1526 (set-current-module inner-module)
1527 (set! inner-module #f))
1528 thunk
1529 (lambda ()
1530 (set! inner-module (current-module))
1531 (set-current-module outer-module)
1532 (set! outer-module #f)))))
1533
1534 (define basic-load load)
1535
1536 (define (load-module filename)
1537 (save-module-excursion
1538 (lambda ()
1539 (let ((oldname (and (current-load-port)
1540 (port-filename (current-load-port)))))
1541 (basic-load (if (and oldname
1542 (> (string-length filename) 0)
1543 (not (char=? (string-ref filename 0) #\/))
1544 (not (string=? (dirname oldname) ".")))
1545 (string-append (dirname oldname) "/" filename)
1546 filename))))))
1547
1548
1549 \f
1550 ;;; {MODULE-REF -- exported}
1551 ;;
1552 ;; Returns the value of a variable called NAME in MODULE or any of its
1553 ;; used modules. If there is no such variable, then if the optional third
1554 ;; argument DEFAULT is present, it is returned; otherwise an error is signaled.
1555 ;;
1556 (define (module-ref module name . rest)
1557 (let ((variable (module-variable module name)))
1558 (if (and variable (variable-bound? variable))
1559 (variable-ref variable)
1560 (if (null? rest)
1561 (error "No variable named" name 'in module)
1562 (car rest) ; default value
1563 ))))
1564
1565 ;; MODULE-SET! -- exported
1566 ;;
1567 ;; Sets the variable called NAME in MODULE (or in a module that MODULE uses)
1568 ;; to VALUE; if there is no such variable, an error is signaled.
1569 ;;
1570 (define (module-set! module name value)
1571 (let ((variable (module-variable module name)))
1572 (if variable
1573 (variable-set! variable value)
1574 (error "No variable named" name 'in module))))
1575
1576 ;; MODULE-DEFINE! -- exported
1577 ;;
1578 ;; Sets the variable called NAME in MODULE to VALUE; if there is no such
1579 ;; variable, it is added first.
1580 ;;
1581 (define (module-define! module name value)
1582 (let ((variable (module-local-variable module name)))
1583 (if variable
1584 (begin
1585 (variable-set! variable value)
1586 (module-modified module))
1587 (module-add! module name (make-variable value name)))))
1588
1589 ;; MODULE-DEFINED? -- exported
1590 ;;
1591 ;; Return #t iff NAME is defined in MODULE (or in a module that MODULE
1592 ;; uses)
1593 ;;
1594 (define (module-defined? module name)
1595 (let ((variable (module-variable module name)))
1596 (and variable (variable-bound? variable))))
1597
1598 ;; MODULE-USE! module interface
1599 ;;
1600 ;; Add INTERFACE to the list of interfaces used by MODULE.
1601 ;;
1602 (define (module-use! module interface)
1603 (set-module-uses! module
1604 (cons interface (delq! interface (module-uses module))))
1605 (module-modified module))
1606
1607 \f
1608 ;;; {Recursive Namespaces}
1609 ;;;
1610 ;;;
1611 ;;; A hierarchical namespace emerges if we consider some module to be
1612 ;;; root, and variables bound to modules as nested namespaces.
1613 ;;;
1614 ;;; The routines in this file manage variable names in hierarchical namespace.
1615 ;;; Each variable name is a list of elements, looked up in successively nested
1616 ;;; modules.
1617 ;;;
1618 ;;; (nested-ref some-root-module '(foo bar baz))
1619 ;;; => <value of a variable named baz in the module bound to bar in
1620 ;;; the module bound to foo in some-root-module>
1621 ;;;
1622 ;;;
1623 ;;; There are:
1624 ;;;
1625 ;;; ;; a-root is a module
1626 ;;; ;; name is a list of symbols
1627 ;;;
1628 ;;; nested-ref a-root name
1629 ;;; nested-set! a-root name val
1630 ;;; nested-define! a-root name val
1631 ;;; nested-remove! a-root name
1632 ;;;
1633 ;;;
1634 ;;; (current-module) is a natural choice for a-root so for convenience there are
1635 ;;; also:
1636 ;;;
1637 ;;; local-ref name == nested-ref (current-module) name
1638 ;;; local-set! name val == nested-set! (current-module) name val
1639 ;;; local-define! name val == nested-define! (current-module) name val
1640 ;;; local-remove! name == nested-remove! (current-module) name
1641 ;;;
1642
1643
1644 (define (nested-ref root names)
1645 (let loop ((cur root)
1646 (elts names))
1647 (cond
1648 ((null? elts) cur)
1649 ((not (module? cur)) #f)
1650 (else (loop (module-ref cur (car elts) #f) (cdr elts))))))
1651
1652 (define (nested-set! root names val)
1653 (let loop ((cur root)
1654 (elts names))
1655 (if (null? (cdr elts))
1656 (module-set! cur (car elts) val)
1657 (loop (module-ref cur (car elts)) (cdr elts)))))
1658
1659 (define (nested-define! root names val)
1660 (let loop ((cur root)
1661 (elts names))
1662 (if (null? (cdr elts))
1663 (module-define! cur (car elts) val)
1664 (loop (module-ref cur (car elts)) (cdr elts)))))
1665
1666 (define (nested-remove! root names)
1667 (let loop ((cur root)
1668 (elts names))
1669 (if (null? (cdr elts))
1670 (module-remove! cur (car elts))
1671 (loop (module-ref cur (car elts)) (cdr elts)))))
1672
1673 (define (local-ref names) (nested-ref (current-module) names))
1674 (define (local-set! names val) (nested-set! (current-module) names val))
1675 (define (local-define names val) (nested-define! (current-module) names val))
1676 (define (local-remove names) (nested-remove! (current-module) names))
1677
1678
1679 \f
1680 ;;; {The (app) module}
1681 ;;;
1682 ;;; The root of conventionally named objects not directly in the top level.
1683 ;;;
1684 ;;; (app modules)
1685 ;;; (app modules guile)
1686 ;;;
1687 ;;; The directory of all modules and the standard root module.
1688 ;;;
1689
1690 (define (module-public-interface m)
1691 (module-ref m '%module-public-interface #f))
1692 (define (set-module-public-interface! m i)
1693 (module-define! m '%module-public-interface i))
1694 (define (set-system-module! m s)
1695 (set-procedure-property! (module-eval-closure m) 'system-module s))
1696 (define the-root-module (make-root-module))
1697 (define the-scm-module (make-scm-module))
1698 (set-module-public-interface! the-root-module the-scm-module)
1699 (set-module-name! the-root-module 'the-root-module)
1700 (set-module-name! the-scm-module 'the-scm-module)
1701 (for-each set-system-module! (list the-root-module the-scm-module) '(#t #t))
1702
1703 (set-current-module the-root-module)
1704
1705 (define app (make-module 31))
1706 (local-define '(app modules) (make-module 31))
1707 (local-define '(app modules guile) the-root-module)
1708
1709 ;; (define-special-value '(app modules new-ws) (lambda () (make-scm-module)))
1710
1711 (define (try-load-module name)
1712 (or (try-module-linked name)
1713 (try-module-autoload name)
1714 (try-module-dynamic-link name)))
1715
1716 ;; NOTE: This binding is used in libguile/modules.c.
1717 ;;
1718 (define (resolve-module name . maybe-autoload)
1719 (let ((full-name (append '(app modules) name)))
1720 (let ((already (local-ref full-name)))
1721 (if already
1722 ;; The module already exists...
1723 (if (and (or (null? maybe-autoload) (car maybe-autoload))
1724 (not (module-ref already '%module-public-interface #f)))
1725 ;; ...but we are told to load and it doesn't contain source, so
1726 (begin
1727 (try-load-module name)
1728 already)
1729 ;; simply return it.
1730 already)
1731 (begin
1732 ;; Try to autoload it if we are told so
1733 (if (or (null? maybe-autoload) (car maybe-autoload))
1734 (try-load-module name))
1735 ;; Get/create it.
1736 (make-modules-in (current-module) full-name))))))
1737
1738 (define (beautify-user-module! module)
1739 (let ((interface (module-public-interface module)))
1740 (if (or (not interface)
1741 (eq? interface module))
1742 (let ((interface (make-module 31)))
1743 (set-module-name! interface (module-name module))
1744 (set-module-kind! interface 'interface)
1745 (set-module-public-interface! module interface))))
1746 (if (and (not (memq the-scm-module (module-uses module)))
1747 (not (eq? module the-root-module)))
1748 (set-module-uses! module (append (module-uses module) (list the-scm-module)))))
1749
1750 ;; NOTE: This binding is used in libguile/modules.c.
1751 ;;
1752 (define (make-modules-in module name)
1753 (if (null? name)
1754 module
1755 (cond
1756 ((module-ref module (car name) #f)
1757 => (lambda (m) (make-modules-in m (cdr name))))
1758 (else (let ((m (make-module 31)))
1759 (set-module-kind! m 'directory)
1760 (set-module-name! m (car name))
1761 (module-define! module (car name) m)
1762 (make-modules-in m (cdr name)))))))
1763
1764 (define (resolve-interface name)
1765 (let ((module (resolve-module name)))
1766 (and module (module-public-interface module))))
1767
1768
1769 (define %autoloader-developer-mode #t)
1770
1771 (define (process-define-module args)
1772 (let* ((module-id (car args))
1773 (module (resolve-module module-id #f))
1774 (kws (cdr args)))
1775 (beautify-user-module! module)
1776 (let loop ((kws kws)
1777 (reversed-interfaces '()))
1778 (if (null? kws)
1779 (for-each (lambda (interface)
1780 (module-use! module interface))
1781 reversed-interfaces)
1782 (let ((keyword (cond ((keyword? (car kws))
1783 (keyword->symbol (car kws)))
1784 ((and (symbol? (car kws))
1785 (eq? (string-ref (car kws) 0) #\:))
1786 (string->symbol (substring (car kws) 1)))
1787 (else #f))))
1788 (case keyword
1789 ((use-module use-syntax)
1790 (if (not (pair? (cdr kws)))
1791 (error "unrecognized defmodule argument" kws))
1792 (let* ((used-name (cadr kws))
1793 (used-module (resolve-module used-name)))
1794 (if (not (module-ref used-module
1795 '%module-public-interface
1796 #f))
1797 (begin
1798 ((if %autoloader-developer-mode warn error)
1799 "no code for module" (module-name used-module))
1800 (beautify-user-module! used-module)))
1801 (let ((interface (module-public-interface used-module)))
1802 (if (not interface)
1803 (error "missing interface for use-module"
1804 used-module))
1805 (if (eq? keyword 'use-syntax)
1806 (set-module-transformer!
1807 module
1808 (module-ref interface (car (last-pair used-name))
1809 #f)))
1810 (loop (cddr kws)
1811 (cons interface reversed-interfaces)))))
1812 ((autoload)
1813 (if (not (and (pair? (cdr kws)) (pair? (cddr kws))))
1814 (error "unrecognized defmodule argument" kws))
1815 (loop (cdddr kws)
1816 (cons (make-autoload-interface module
1817 (cadr kws)
1818 (caddr kws))
1819 reversed-interfaces)))
1820 ((no-backtrace)
1821 (set-system-module! module #t)
1822 (loop (cdr kws) reversed-interfaces))
1823 (else
1824 (error "unrecognized defmodule argument" kws))))))
1825 module))
1826
1827 ;;; {Autoload}
1828
1829 (define (make-autoload-interface module name bindings)
1830 (let ((b (lambda (a sym definep)
1831 (and (memq sym bindings)
1832 (let ((i (module-public-interface (resolve-module name))))
1833 (if (not i)
1834 (error "missing interface for module" name))
1835 ;; Replace autoload-interface with interface
1836 (set-car! (memq a (module-uses module)) i)
1837 (module-local-variable i sym))))))
1838 (module-constructor #() #f b #f #f name 'autoload
1839 '() (make-weak-value-hash-table 31) 0)))
1840
1841 \f
1842 ;;; {Autoloading modules}
1843
1844 (define autoloads-in-progress '())
1845
1846 (define (try-module-autoload module-name)
1847
1848 (define (sfx name) (string-append name (scheme-file-suffix)))
1849 (let* ((reverse-name (reverse module-name))
1850 (name (car reverse-name))
1851 (dir-hint-module-name (reverse (cdr reverse-name)))
1852 (dir-hint (apply symbol-append (map (lambda (elt) (symbol-append elt "/")) dir-hint-module-name))))
1853 (resolve-module dir-hint-module-name #f)
1854 (and (not (autoload-done-or-in-progress? dir-hint name))
1855 (let ((didit #f))
1856 (dynamic-wind
1857 (lambda () (autoload-in-progress! dir-hint name))
1858 (lambda ()
1859 (let ((full (%search-load-path (in-vicinity dir-hint name))))
1860 (if full
1861 (begin
1862 (save-module-excursion (lambda () (primitive-load full)))
1863 (set! didit #t)))))
1864 (lambda () (set-autoloaded! dir-hint name didit)))
1865 didit))))
1866
1867 \f
1868 ;;; Dynamic linking of modules
1869
1870 ;; Initializing a module that is written in C is a two step process.
1871 ;; First the module's `module init' function is called. This function
1872 ;; is expected to call `scm_register_module_xxx' to register the `real
1873 ;; init' function. Later, when the module is referenced for the first
1874 ;; time, this real init function is called in the right context. See
1875 ;; gtcltk-lib/gtcltk-module.c for an example.
1876 ;;
1877 ;; The code for the module can be in a regular shared library (so that
1878 ;; the `module init' function will be called when libguile is
1879 ;; initialized). Or it can be dynamically linked.
1880 ;;
1881 ;; You can safely call `scm_register_module_xxx' before libguile
1882 ;; itself is initialized. You could call it from an C++ constructor
1883 ;; of a static object, for example.
1884 ;;
1885 ;; To make your Guile extension into a dynamic linkable module, follow
1886 ;; these easy steps:
1887 ;;
1888 ;; - Find a name for your module, like (ice-9 gtcltk)
1889 ;; - Write a function with a name like
1890 ;;
1891 ;; scm_init_ice_9_gtcltk_module
1892 ;;
1893 ;; This is your `module init' function. It should call
1894 ;;
1895 ;; scm_register_module_xxx ("ice-9 gtcltk", scm_init_gtcltk);
1896 ;;
1897 ;; "ice-9 gtcltk" is the C version of the module name. Slashes are
1898 ;; replaced by spaces, the rest is untouched. `scm_init_gtcltk' is
1899 ;; the real init function that executes the usual initializations
1900 ;; like making new smobs, etc.
1901 ;;
1902 ;; - Make a shared library with your code and a name like
1903 ;;
1904 ;; ice-9/libgtcltk.so
1905 ;;
1906 ;; and put it somewhere in %load-path.
1907 ;;
1908 ;; - Then you can simply write `:use-module (ice-9 gtcltk)' and it
1909 ;; will be linked automatically.
1910 ;;
1911 ;; This is all very experimental.
1912
1913 (define (split-c-module-name str)
1914 (let loop ((rev '())
1915 (start 0)
1916 (pos 0)
1917 (end (string-length str)))
1918 (cond
1919 ((= pos end)
1920 (reverse (cons (string->symbol (substring str start pos)) rev)))
1921 ((eq? (string-ref str pos) #\space)
1922 (loop (cons (string->symbol (substring str start pos)) rev)
1923 (+ pos 1)
1924 (+ pos 1)
1925 end))
1926 (else
1927 (loop rev start (+ pos 1) end)))))
1928
1929 (define (convert-c-registered-modules dynobj)
1930 (let ((res (map (lambda (c)
1931 (list (split-c-module-name (car c)) (cdr c) dynobj))
1932 (c-registered-modules))))
1933 (c-clear-registered-modules)
1934 res))
1935
1936 (define registered-modules '())
1937
1938 (define (register-modules dynobj)
1939 (set! registered-modules
1940 (append! (convert-c-registered-modules dynobj)
1941 registered-modules)))
1942
1943 (define (init-dynamic-module modname)
1944 ;; Register any linked modules which has been registered on the C level
1945 (register-modules #f)
1946 (or-map (lambda (modinfo)
1947 (if (equal? (car modinfo) modname)
1948 (begin
1949 (set! registered-modules (delq! modinfo registered-modules))
1950 (let ((mod (resolve-module modname #f)))
1951 (save-module-excursion
1952 (lambda ()
1953 (set-current-module mod)
1954 (set-module-public-interface! mod mod)
1955 (dynamic-call (cadr modinfo) (caddr modinfo))
1956 ))
1957 #t))
1958 #f))
1959 registered-modules))
1960
1961 (define (dynamic-maybe-call name dynobj)
1962 (catch #t ; could use false-if-exception here
1963 (lambda ()
1964 (dynamic-call name dynobj))
1965 (lambda args
1966 #f)))
1967
1968 (define (dynamic-maybe-link filename)
1969 (catch #t ; could use false-if-exception here
1970 (lambda ()
1971 (dynamic-link filename))
1972 (lambda args
1973 #f)))
1974
1975 (define (find-and-link-dynamic-module module-name)
1976 (define (make-init-name mod-name)
1977 (string-append "scm_init"
1978 (list->string (map (lambda (c)
1979 (if (or (char-alphabetic? c)
1980 (char-numeric? c))
1981 c
1982 #\_))
1983 (string->list mod-name)))
1984 "_module"))
1985
1986 ;; Put the subdirectory for this module in the car of SUBDIR-AND-LIBNAME,
1987 ;; and the `libname' (the name of the module prepended by `lib') in the cdr
1988 ;; field. For example, if MODULE-NAME is the list (inet tcp-ip udp), then
1989 ;; SUBDIR-AND-LIBNAME will be the pair ("inet/tcp-ip" . "libudp").
1990 (let ((subdir-and-libname
1991 (let loop ((dirs "")
1992 (syms module-name))
1993 (if (null? (cdr syms))
1994 (cons dirs (string-append "lib" (car syms)))
1995 (loop (string-append dirs (car syms) "/") (cdr syms)))))
1996 (init (make-init-name (apply string-append
1997 (map (lambda (s)
1998 (string-append "_" s))
1999 module-name)))))
2000 (let ((subdir (car subdir-and-libname))
2001 (libname (cdr subdir-and-libname)))
2002
2003 ;; Now look in each dir in %LOAD-PATH for `subdir/libfoo.la'. If that
2004 ;; file exists, fetch the dlname from that file and attempt to link
2005 ;; against it. If `subdir/libfoo.la' does not exist, or does not seem
2006 ;; to name any shared library, look for `subdir/libfoo.so' instead and
2007 ;; link against that.
2008 (let check-dirs ((dir-list %load-path))
2009 (if (null? dir-list)
2010 #f
2011 (let* ((dir (in-vicinity (car dir-list) subdir))
2012 (sharlib-full
2013 (or (try-using-libtool-name dir libname)
2014 (try-using-sharlib-name dir libname))))
2015 (if (and sharlib-full (file-exists? sharlib-full))
2016 (link-dynamic-module sharlib-full init)
2017 (check-dirs (cdr dir-list)))))))))
2018
2019 (define (try-using-libtool-name libdir libname)
2020 (let ((libtool-filename (in-vicinity libdir
2021 (string-append libname ".la"))))
2022 (and (file-exists? libtool-filename)
2023 libtool-filename)))
2024
2025 (define (try-using-sharlib-name libdir libname)
2026 (in-vicinity libdir (string-append libname ".so")))
2027
2028 (define (link-dynamic-module filename initname)
2029 ;; Register any linked modules which has been registered on the C level
2030 (register-modules #f)
2031 (let ((dynobj (dynamic-link filename)))
2032 (dynamic-call initname dynobj)
2033 (register-modules dynobj)))
2034
2035 (define (try-module-linked module-name)
2036 (init-dynamic-module module-name))
2037
2038 (define (try-module-dynamic-link module-name)
2039 (and (find-and-link-dynamic-module module-name)
2040 (init-dynamic-module module-name)))
2041
2042
2043
2044 (define autoloads-done '((guile . guile)))
2045
2046 (define (autoload-done-or-in-progress? p m)
2047 (let ((n (cons p m)))
2048 (->bool (or (member n autoloads-done)
2049 (member n autoloads-in-progress)))))
2050
2051 (define (autoload-done! p m)
2052 (let ((n (cons p m)))
2053 (set! autoloads-in-progress
2054 (delete! n autoloads-in-progress))
2055 (or (member n autoloads-done)
2056 (set! autoloads-done (cons n autoloads-done)))))
2057
2058 (define (autoload-in-progress! p m)
2059 (let ((n (cons p m)))
2060 (set! autoloads-done
2061 (delete! n autoloads-done))
2062 (set! autoloads-in-progress (cons n autoloads-in-progress))))
2063
2064 (define (set-autoloaded! p m done?)
2065 (if done?
2066 (autoload-done! p m)
2067 (let ((n (cons p m)))
2068 (set! autoloads-done (delete! n autoloads-done))
2069 (set! autoloads-in-progress (delete! n autoloads-in-progress)))))
2070
2071
2072
2073
2074 \f
2075 ;;; {Macros}
2076 ;;;
2077
2078 (define (primitive-macro? m)
2079 (and (macro? m)
2080 (not (macro-transformer m))))
2081
2082 ;;; {Defmacros}
2083 ;;;
2084 (define macro-table (make-weak-key-hash-table 523))
2085 (define xformer-table (make-weak-key-hash-table 523))
2086
2087 (define (defmacro? m) (hashq-ref macro-table m))
2088 (define (assert-defmacro?! m) (hashq-set! macro-table m #t))
2089 (define (defmacro-transformer m) (hashq-ref xformer-table m))
2090 (define (set-defmacro-transformer! m t) (hashq-set! xformer-table m t))
2091
2092 (define defmacro:transformer
2093 (lambda (f)
2094 (let* ((xform (lambda (exp env)
2095 (copy-tree (apply f (cdr exp)))))
2096 (a (procedure->memoizing-macro xform)))
2097 (assert-defmacro?! a)
2098 (set-defmacro-transformer! a f)
2099 a)))
2100
2101
2102 (define defmacro
2103 (let ((defmacro-transformer
2104 (lambda (name parms . body)
2105 (let ((transformer `(lambda ,parms ,@body)))
2106 `(define ,name
2107 (,(lambda (transformer)
2108 (defmacro:transformer transformer))
2109 ,transformer))))))
2110 (defmacro:transformer defmacro-transformer)))
2111
2112 (define defmacro:syntax-transformer
2113 (lambda (f)
2114 (procedure->syntax
2115 (lambda (exp env)
2116 (copy-tree (apply f (cdr exp)))))))
2117
2118
2119 ;; XXX - should the definition of the car really be looked up in the
2120 ;; current module?
2121
2122 (define (macroexpand-1 e)
2123 (cond
2124 ((pair? e) (let* ((a (car e))
2125 (val (and (symbol? a) (local-ref (list a)))))
2126 (if (defmacro? val)
2127 (apply (defmacro-transformer val) (cdr e))
2128 e)))
2129 (#t e)))
2130
2131 (define (macroexpand e)
2132 (cond
2133 ((pair? e) (let* ((a (car e))
2134 (val (and (symbol? a) (local-ref (list a)))))
2135 (if (defmacro? val)
2136 (macroexpand (apply (defmacro-transformer val) (cdr e)))
2137 e)))
2138 (#t e)))
2139
2140 (define (gentemp)
2141 (gensym "scm:G"))
2142
2143 (provide 'defmacro)
2144
2145 \f
2146
2147 ;;; {Run-time options}
2148
2149 ((let* ((names '((eval-options-interface
2150 (eval-options eval-enable eval-disable)
2151 (eval-set!))
2152
2153 (debug-options-interface
2154 (debug-options debug-enable debug-disable)
2155 (debug-set!))
2156
2157 (evaluator-traps-interface
2158 (traps trap-enable trap-disable)
2159 (trap-set!))
2160
2161 (read-options-interface
2162 (read-options read-enable read-disable)
2163 (read-set!))
2164
2165 (print-options-interface
2166 (print-options print-enable print-disable)
2167 (print-set!))
2168
2169 (readline-options-interface
2170 (readline-options readline-enable readline-disable)
2171 (readline-set!))
2172 ))
2173 (option-name car)
2174 (option-value cadr)
2175 (option-documentation caddr)
2176
2177 (print-option (lambda (option)
2178 (display (option-name option))
2179 (if (< (string-length
2180 (symbol->string (option-name option)))
2181 8)
2182 (display #\tab))
2183 (display #\tab)
2184 (display (option-value option))
2185 (display #\tab)
2186 (display (option-documentation option))
2187 (newline)))
2188
2189 ;; Below follows the macros defining the run-time option interfaces.
2190
2191 (make-options (lambda (interface)
2192 `(lambda args
2193 (cond ((null? args) (,interface))
2194 ((list? (car args))
2195 (,interface (car args)) (,interface))
2196 (else (for-each ,print-option
2197 (,interface #t)))))))
2198
2199 (make-enable (lambda (interface)
2200 `(lambda flags
2201 (,interface (append flags (,interface)))
2202 (,interface))))
2203
2204 (make-disable (lambda (interface)
2205 `(lambda flags
2206 (let ((options (,interface)))
2207 (for-each (lambda (flag)
2208 (set! options (delq! flag options)))
2209 flags)
2210 (,interface options)
2211 (,interface)))))
2212
2213 (make-set! (lambda (interface)
2214 `((name exp)
2215 (,'quasiquote
2216 (begin (,interface (append (,interface)
2217 (list '(,'unquote name)
2218 (,'unquote exp))))
2219 (,interface))))))
2220 )
2221 (procedure->macro
2222 (lambda (exp env)
2223 (cons 'begin
2224 (apply append
2225 (map (lambda (group)
2226 (let ((interface (car group)))
2227 (append (map (lambda (name constructor)
2228 `(define ,name
2229 ,(constructor interface)))
2230 (cadr group)
2231 (list make-options
2232 make-enable
2233 make-disable))
2234 (map (lambda (name constructor)
2235 `(defmacro ,name
2236 ,@(constructor interface)))
2237 (caddr group)
2238 (list make-set!)))))
2239 names)))))))
2240
2241 \f
2242
2243 ;;; {Running Repls}
2244 ;;;
2245
2246 (define (repl read evaler print)
2247 (let loop ((source (read (current-input-port))))
2248 (print (evaler source))
2249 (loop (read (current-input-port)))))
2250
2251 ;; A provisional repl that acts like the SCM repl:
2252 ;;
2253 (define scm-repl-silent #f)
2254 (define (assert-repl-silence v) (set! scm-repl-silent v))
2255
2256 (define *unspecified* (if #f #f))
2257 (define (unspecified? v) (eq? v *unspecified*))
2258
2259 (define scm-repl-print-unspecified #f)
2260 (define (assert-repl-print-unspecified v) (set! scm-repl-print-unspecified v))
2261
2262 (define scm-repl-verbose #f)
2263 (define (assert-repl-verbosity v) (set! scm-repl-verbose v))
2264
2265 (define scm-repl-prompt "guile> ")
2266
2267 (define (set-repl-prompt! v) (set! scm-repl-prompt v))
2268
2269 (define (default-lazy-handler key . args)
2270 (save-stack lazy-handler-dispatch)
2271 (apply throw key args))
2272
2273 (define enter-frame-handler default-lazy-handler)
2274 (define apply-frame-handler default-lazy-handler)
2275 (define exit-frame-handler default-lazy-handler)
2276
2277 (define (lazy-handler-dispatch key . args)
2278 (case key
2279 ((apply-frame)
2280 (apply apply-frame-handler key args))
2281 ((exit-frame)
2282 (apply exit-frame-handler key args))
2283 ((enter-frame)
2284 (apply enter-frame-handler key args))
2285 (else
2286 (apply default-lazy-handler key args))))
2287
2288 (define abort-hook (make-hook))
2289
2290 ;; these definitions are used if running a script.
2291 ;; otherwise redefined in error-catching-loop.
2292 (define (set-batch-mode?! arg) #t)
2293 (define (batch-mode?) #t)
2294
2295 (define (error-catching-loop thunk)
2296 (let ((status #f)
2297 (interactive #t))
2298 (define (loop first)
2299 (let ((next
2300 (catch #t
2301
2302 (lambda ()
2303 (lazy-catch #t
2304 (lambda ()
2305 (dynamic-wind
2306 (lambda () (unmask-signals))
2307 (lambda ()
2308 (with-traps
2309 (lambda ()
2310 (first)
2311
2312 ;; This line is needed because mark
2313 ;; doesn't do closures quite right.
2314 ;; Unreferenced locals should be
2315 ;; collected.
2316 ;;
2317 (set! first #f)
2318 (let loop ((v (thunk)))
2319 (loop (thunk)))
2320 #f)))
2321 (lambda () (mask-signals))))
2322
2323 lazy-handler-dispatch))
2324
2325 (lambda (key . args)
2326 (case key
2327 ((quit)
2328 (set! status args)
2329 #f)
2330
2331 ((switch-repl)
2332 (apply throw 'switch-repl args))
2333
2334 ((abort)
2335 ;; This is one of the closures that require
2336 ;; (set! first #f) above
2337 ;;
2338 (lambda ()
2339 (run-hook abort-hook)
2340 (force-output (current-output-port))
2341 (display "ABORT: " (current-error-port))
2342 (write args (current-error-port))
2343 (newline (current-error-port))
2344 (if interactive
2345 (begin
2346 (if (and
2347 (not has-shown-debugger-hint?)
2348 (not (memq 'backtrace
2349 (debug-options-interface)))
2350 (stack? (fluid-ref the-last-stack)))
2351 (begin
2352 (newline (current-error-port))
2353 (display
2354 "Type \"(backtrace)\" to get more information.\n"
2355 (current-error-port))
2356 (set! has-shown-debugger-hint? #t)))
2357 (force-output (current-error-port)))
2358 (begin
2359 (primitive-exit 1)))
2360 (set! stack-saved? #f)))
2361
2362 (else
2363 ;; This is the other cons-leak closure...
2364 (lambda ()
2365 (cond ((= (length args) 4)
2366 (apply handle-system-error key args))
2367 (else
2368 (apply bad-throw key args))))))))))
2369 (if next (loop next) status)))
2370 (set! set-batch-mode?! (lambda (arg)
2371 (cond (arg
2372 (set! interactive #f)
2373 (restore-signals))
2374 (#t
2375 (error "sorry, not implemented")))))
2376 (set! batch-mode? (lambda () (not interactive)))
2377 (loop (lambda () #t))))
2378
2379 ;;(define the-last-stack (make-fluid)) Defined by scm_init_backtrace ()
2380 (define before-signal-stack (make-fluid))
2381 (define stack-saved? #f)
2382
2383 (define (save-stack . narrowing)
2384 (or stack-saved?
2385 (cond ((not (memq 'debug (debug-options-interface)))
2386 (fluid-set! the-last-stack #f)
2387 (set! stack-saved? #t))
2388 (else
2389 (fluid-set!
2390 the-last-stack
2391 (case (stack-id #t)
2392 ((repl-stack)
2393 (apply make-stack #t save-stack eval #t 0 narrowing))
2394 ((load-stack)
2395 (apply make-stack #t save-stack 0 #t 0 narrowing))
2396 ((tk-stack)
2397 (apply make-stack #t save-stack tk-stack-mark #t 0 narrowing))
2398 ((#t)
2399 (apply make-stack #t save-stack 0 1 narrowing))
2400 (else
2401 (let ((id (stack-id #t)))
2402 (and (procedure? id)
2403 (apply make-stack #t save-stack id #t 0 narrowing))))))
2404 (set! stack-saved? #t)))))
2405
2406 (define before-error-hook (make-hook))
2407 (define after-error-hook (make-hook))
2408 (define before-backtrace-hook (make-hook))
2409 (define after-backtrace-hook (make-hook))
2410
2411 (define has-shown-debugger-hint? #f)
2412
2413 (define (handle-system-error key . args)
2414 (let ((cep (current-error-port)))
2415 (cond ((not (stack? (fluid-ref the-last-stack))))
2416 ((memq 'backtrace (debug-options-interface))
2417 (run-hook before-backtrace-hook)
2418 (newline cep)
2419 (display "Backtrace:\n")
2420 (display-backtrace (fluid-ref the-last-stack) cep)
2421 (newline cep)
2422 (run-hook after-backtrace-hook)))
2423 (run-hook before-error-hook)
2424 (apply display-error (fluid-ref the-last-stack) cep args)
2425 (run-hook after-error-hook)
2426 (force-output cep)
2427 (throw 'abort key)))
2428
2429 (define (quit . args)
2430 (apply throw 'quit args))
2431
2432 (define exit quit)
2433
2434 ;;(define has-shown-backtrace-hint? #f) Defined by scm_init_backtrace ()
2435
2436 ;; Replaced by C code:
2437 ;;(define (backtrace)
2438 ;; (if (fluid-ref the-last-stack)
2439 ;; (begin
2440 ;; (newline)
2441 ;; (display-backtrace (fluid-ref the-last-stack) (current-output-port))
2442 ;; (newline)
2443 ;; (if (and (not has-shown-backtrace-hint?)
2444 ;; (not (memq 'backtrace (debug-options-interface))))
2445 ;; (begin
2446 ;; (display
2447 ;;"Type \"(debug-enable 'backtrace)\" if you would like a backtrace
2448 ;;automatically if an error occurs in the future.\n")
2449 ;; (set! has-shown-backtrace-hint? #t))))
2450 ;; (display "No backtrace available.\n")))
2451
2452 (define (error-catching-repl r e p)
2453 (error-catching-loop (lambda () (p (e (r))))))
2454
2455 (define (gc-run-time)
2456 (cdr (assq 'gc-time-taken (gc-stats))))
2457
2458 (define before-read-hook (make-hook))
2459 (define after-read-hook (make-hook))
2460
2461 ;;; The default repl-reader function. We may override this if we've
2462 ;;; the readline library.
2463 (define repl-reader
2464 (lambda (prompt)
2465 (display prompt)
2466 (force-output)
2467 (run-hook before-read-hook)
2468 (read (current-input-port))))
2469
2470 (define (scm-style-repl)
2471 (letrec (
2472 (start-gc-rt #f)
2473 (start-rt #f)
2474 (repl-report-start-timing (lambda ()
2475 (set! start-gc-rt (gc-run-time))
2476 (set! start-rt (get-internal-run-time))))
2477 (repl-report (lambda ()
2478 (display ";;; ")
2479 (display (inexact->exact
2480 (* 1000 (/ (- (get-internal-run-time) start-rt)
2481 internal-time-units-per-second))))
2482 (display " msec (")
2483 (display (inexact->exact
2484 (* 1000 (/ (- (gc-run-time) start-gc-rt)
2485 internal-time-units-per-second))))
2486 (display " msec in gc)\n")))
2487
2488 (consume-trailing-whitespace
2489 (lambda ()
2490 (let ((ch (peek-char)))
2491 (cond
2492 ((eof-object? ch))
2493 ((or (char=? ch #\space) (char=? ch #\tab))
2494 (read-char)
2495 (consume-trailing-whitespace))
2496 ((char=? ch #\newline)
2497 (read-char))))))
2498 (-read (lambda ()
2499 (let ((val
2500 (let ((prompt (cond ((string? scm-repl-prompt)
2501 scm-repl-prompt)
2502 ((thunk? scm-repl-prompt)
2503 (scm-repl-prompt))
2504 (scm-repl-prompt "> ")
2505 (else ""))))
2506 (repl-reader prompt))))
2507
2508 ;; As described in R4RS, the READ procedure updates the
2509 ;; port to point to the first character past the end of
2510 ;; the external representation of the object. This
2511 ;; means that it doesn't consume the newline typically
2512 ;; found after an expression. This means that, when
2513 ;; debugging Guile with GDB, GDB gets the newline, which
2514 ;; it often interprets as a "continue" command, making
2515 ;; breakpoints kind of useless. So, consume any
2516 ;; trailing newline here, as well as any whitespace
2517 ;; before it.
2518 ;; But not if EOF, for control-D.
2519 (if (not (eof-object? val))
2520 (consume-trailing-whitespace))
2521 (run-hook after-read-hook)
2522 (if (eof-object? val)
2523 (begin
2524 (repl-report-start-timing)
2525 (if scm-repl-verbose
2526 (begin
2527 (newline)
2528 (display ";;; EOF -- quitting")
2529 (newline)))
2530 (quit 0)))
2531 val)))
2532
2533 (-eval (lambda (sourc)
2534 (repl-report-start-timing)
2535 (start-stack 'repl-stack (eval sourc))))
2536
2537 (-print (lambda (result)
2538 (if (not scm-repl-silent)
2539 (begin
2540 (if (or scm-repl-print-unspecified
2541 (not (unspecified? result)))
2542 (begin
2543 (write result)
2544 (newline)))
2545 (if scm-repl-verbose
2546 (repl-report))
2547 (force-output)))))
2548
2549 (-quit (lambda (args)
2550 (if scm-repl-verbose
2551 (begin
2552 (display ";;; QUIT executed, repl exitting")
2553 (newline)
2554 (repl-report)))
2555 args))
2556
2557 (-abort (lambda ()
2558 (if scm-repl-verbose
2559 (begin
2560 (display ";;; ABORT executed.")
2561 (newline)
2562 (repl-report)))
2563 (repl -read -eval -print))))
2564
2565 (let ((status (error-catching-repl -read
2566 -eval
2567 -print)))
2568 (-quit status))))
2569
2570
2571 \f
2572 ;;; {IOTA functions: generating lists of numbers}
2573
2574 (define (iota n)
2575 (let loop ((count (1- n)) (result '()))
2576 (if (< count 0) result
2577 (loop (1- count) (cons count result)))))
2578
2579 \f
2580 ;;; {While}
2581 ;;;
2582 ;;; with `continue' and `break'.
2583 ;;;
2584
2585 (defmacro while (cond . body)
2586 `(letrec ((continue (lambda () (or (not ,cond) (begin (begin ,@ body) (continue)))))
2587 (break (lambda val (apply throw 'break val))))
2588 (catch 'break
2589 (lambda () (continue))
2590 (lambda v (cadr v)))))
2591
2592 ;;; {collect}
2593 ;;;
2594 ;;; Similar to `begin' but returns a list of the results of all constituent
2595 ;;; forms instead of the result of the last form.
2596 ;;; (The definition relies on the current left-to-right
2597 ;;; order of evaluation of operands in applications.)
2598
2599 (defmacro collect forms
2600 (cons 'list forms))
2601
2602 ;;; {with-fluids}
2603
2604 ;; with-fluids is a convenience wrapper for the builtin procedure
2605 ;; `with-fluids*'. The syntax is just like `let':
2606 ;;
2607 ;; (with-fluids ((fluid val)
2608 ;; ...)
2609 ;; body)
2610
2611 (defmacro with-fluids (bindings . body)
2612 `(with-fluids* (list ,@(map car bindings)) (list ,@(map cadr bindings))
2613 (lambda () ,@body)))
2614
2615 ;;; Environments
2616
2617 (define the-environment
2618 (procedure->syntax
2619 (lambda (x e)
2620 e)))
2621
2622 (define (environment-module env)
2623 (let ((closure (and (pair? env) (car (last-pair env)))))
2624 (and closure (procedure-property closure 'module))))
2625
2626 \f
2627
2628 ;;; {Macros}
2629 ;;;
2630
2631 ;; actually....hobbit might be able to hack these with a little
2632 ;; coaxing
2633 ;;
2634
2635 (defmacro define-macro (first . rest)
2636 (let ((name (if (symbol? first) first (car first)))
2637 (transformer
2638 (if (symbol? first)
2639 (car rest)
2640 `(lambda ,(cdr first) ,@rest))))
2641 `(define ,name (defmacro:transformer ,transformer))))
2642
2643
2644 (defmacro define-syntax-macro (first . rest)
2645 (let ((name (if (symbol? first) first (car first)))
2646 (transformer
2647 (if (symbol? first)
2648 (car rest)
2649 `(lambda ,(cdr first) ,@rest))))
2650 `(define ,name (defmacro:syntax-transformer ,transformer))))
2651 \f
2652 ;;; {Module System Macros}
2653 ;;;
2654
2655 (defmacro define-module args
2656 `(let* ((process-define-module process-define-module)
2657 (set-current-module set-current-module)
2658 (module (process-define-module ',args)))
2659 (set-current-module module)
2660 module))
2661
2662 ;; the guts of the use-modules macro. add the interfaces of the named
2663 ;; modules to the use-list of the current module, in order
2664 (define (process-use-modules module-names)
2665 (for-each (lambda (module-name)
2666 (let ((mod-iface (resolve-interface module-name)))
2667 (or mod-iface
2668 (error "no such module" module-name))
2669 (module-use! (current-module) mod-iface)))
2670 (reverse module-names)))
2671
2672 (defmacro use-modules modules
2673 `(process-use-modules ',modules))
2674
2675 (defmacro use-syntax (spec)
2676 `(begin
2677 ,@(if (pair? spec)
2678 `((process-use-modules ',(list spec))
2679 (set-module-transformer! (current-module)
2680 ,(car (last-pair spec))))
2681 `((set-module-transformer! (current-module) ,spec)))
2682 (set! scm:eval-transformer (module-transformer (current-module)))))
2683
2684 (define define-private define)
2685
2686 (defmacro define-public args
2687 (define (syntax)
2688 (error "bad syntax" (list 'define-public args)))
2689 (define (defined-name n)
2690 (cond
2691 ((symbol? n) n)
2692 ((pair? n) (defined-name (car n)))
2693 (else (syntax))))
2694 (cond
2695 ((null? args) (syntax))
2696
2697 (#t (let ((name (defined-name (car args))))
2698 `(begin
2699 (let ((public-i (module-public-interface (current-module))))
2700 ;; Make sure there is a local variable:
2701 ;;
2702 (module-define! (current-module)
2703 ',name
2704 (module-ref (current-module) ',name #f))
2705
2706 ;; Make sure that local is exported:
2707 ;;
2708 (module-add! public-i ',name
2709 (module-variable (current-module) ',name)))
2710
2711 ;; Now (re)define the var normally. Bernard URBAN
2712 ;; suggests we use eval here to accomodate Hobbit; it lets
2713 ;; the interpreter handle the define-private form, which
2714 ;; Hobbit can't digest.
2715 (eval '(define-private ,@ args)))))))
2716
2717
2718
2719 (defmacro defmacro-public args
2720 (define (syntax)
2721 (error "bad syntax" (list 'defmacro-public args)))
2722 (define (defined-name n)
2723 (cond
2724 ((symbol? n) n)
2725 (else (syntax))))
2726 (cond
2727 ((null? args) (syntax))
2728
2729 (#t (let ((name (defined-name (car args))))
2730 `(begin
2731 (let ((public-i (module-public-interface (current-module))))
2732 ;; Make sure there is a local variable:
2733 ;;
2734 (module-define! (current-module)
2735 ',name
2736 (module-ref (current-module) ',name #f))
2737
2738 ;; Make sure that local is exported:
2739 ;;
2740 (module-add! public-i ',name (module-variable (current-module) ',name)))
2741
2742 ;; Now (re)define the var normally.
2743 ;;
2744 (defmacro ,@ args))))))
2745
2746
2747 (defmacro export names
2748 `(let* ((m (current-module))
2749 (public-i (module-public-interface m)))
2750 (for-each (lambda (name)
2751 ;; Make sure there is a local variable:
2752 (module-define! m name (module-ref m name #f))
2753 ;; Make sure that local is exported:
2754 (module-add! public-i name (module-variable m name)))
2755 ',names)))
2756
2757 (define export-syntax export)
2758
2759
2760
2761
2762 (define load load-module)
2763
2764
2765 \f
2766 ;;; {Load emacs interface support if emacs option is given.}
2767
2768 (define (load-emacs-interface)
2769 (if (memq 'debug-extensions *features*)
2770 (debug-enable 'backtrace))
2771 (define-module (guile-user) :use-module (ice-9 emacs)))
2772
2773 \f
2774
2775 (define using-readline?
2776 (let ((using-readline? (make-fluid)))
2777 (make-procedure-with-setter
2778 (lambda () (fluid-ref using-readline?))
2779 (lambda (v) (fluid-set! using-readline? v)))))
2780
2781 ;; this is just (scm-style-repl) with a wrapper to install and remove
2782 ;; signal handlers.
2783 (define (top-repl)
2784
2785 ;; Load emacs interface support if emacs option is given.
2786 (if (and (module-defined? the-root-module 'use-emacs-interface)
2787 use-emacs-interface)
2788 (load-emacs-interface))
2789
2790 ;; Place the user in the guile-user module.
2791 (define-module (guile-user)
2792 :use-module (guile) ;so that bindings will be checked here first
2793 :use-module (ice-9 session)
2794 :use-module (ice-9 debug)
2795 :autoload (ice-9 debugger) (debug)) ;load debugger on demand
2796 (if (memq 'threads *features*)
2797 (define-module (guile-user) :use-module (ice-9 threads)))
2798 (if (memq 'regex *features*)
2799 (define-module (guile-user) :use-module (ice-9 regex)))
2800
2801 (let ((old-handlers #f)
2802 (signals (if (provided? 'posix)
2803 `((,SIGINT . "User interrupt")
2804 (,SIGFPE . "Arithmetic error")
2805 (,SIGBUS . "Bad memory access (bus error)")
2806 (,SIGSEGV .
2807 "Bad memory access (Segmentation violation)"))
2808 '())))
2809
2810 (dynamic-wind
2811
2812 ;; call at entry
2813 (lambda ()
2814 (let ((make-handler (lambda (msg)
2815 (lambda (sig)
2816 ;; Make a backup copy of the stack
2817 (fluid-set! before-signal-stack
2818 (fluid-ref the-last-stack))
2819 (save-stack %deliver-signals)
2820 (scm-error 'signal
2821 #f
2822 msg
2823 #f
2824 (list sig))))))
2825 (set! old-handlers
2826 (map (lambda (sig-msg)
2827 (sigaction (car sig-msg)
2828 (make-handler (cdr sig-msg))))
2829 signals))))
2830
2831 ;; the protected thunk.
2832 (lambda ()
2833 (let ((status (scm-style-repl)))
2834 (run-hook exit-hook)
2835 status))
2836
2837 ;; call at exit.
2838 (lambda ()
2839 (map (lambda (sig-msg old-handler)
2840 (if (not (car old-handler))
2841 ;; restore original C handler.
2842 (sigaction (car sig-msg) #f)
2843 ;; restore Scheme handler, SIG_IGN or SIG_DFL.
2844 (sigaction (car sig-msg)
2845 (car old-handler)
2846 (cdr old-handler))))
2847 signals old-handlers)))))
2848
2849 (defmacro false-if-exception (expr)
2850 `(catch #t (lambda () ,expr)
2851 (lambda args #f)))
2852
2853 ;;; This hook is run at the very end of an interactive session.
2854 ;;;
2855 (define exit-hook (make-hook))
2856
2857 \f
2858 (define-module (guile))
2859
2860 (append! %load-path (cons "." ()))