* boot-9.scm (purify-module!, module-export!): New procedures.
[bpt/guile.git] / ice-9 / boot-9.scm
1 ;;; installed-scm-file
2
3 ;;;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000 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 (let* ((home (or (getenv "HOME")
753 (false-if-exception (passwd:dir (getpwuid (getuid))))
754 "/")) ;; fallback for cygwin etc.
755 (init-file (in-vicinity home ".guile")))
756 (if (file-exists? init-file)
757 (primitive-load init-file))))
758
759 \f
760 ;;; {Loading by paths}
761
762 ;;; Load a Scheme source file named NAME, searching for it in the
763 ;;; directories listed in %load-path, and applying each of the file
764 ;;; name extensions listed in %load-extensions.
765 (define (load-from-path name)
766 (start-stack 'load-stack
767 (primitive-load-path name)))
768
769
770 \f
771 ;;; {Transcendental Functions}
772 ;;;
773 ;;; Derived from "Transcen.scm", Complex trancendental functions for SCM.
774 ;;; Written by Jerry D. Hedden, (C) FSF.
775 ;;; See the file `COPYING' for terms applying to this program.
776 ;;;
777
778 (define (exp z)
779 (if (real? z) ($exp z)
780 (make-polar ($exp (real-part z)) (imag-part z))))
781
782 (define (log z)
783 (if (and (real? z) (>= z 0))
784 ($log z)
785 (make-rectangular ($log (magnitude z)) (angle z))))
786
787 (define (sqrt z)
788 (if (real? z)
789 (if (negative? z) (make-rectangular 0 ($sqrt (- z)))
790 ($sqrt z))
791 (make-polar ($sqrt (magnitude z)) (/ (angle z) 2))))
792
793 (define expt
794 (let ((integer-expt integer-expt))
795 (lambda (z1 z2)
796 (cond ((exact? z2)
797 (integer-expt z1 z2))
798 ((and (real? z2) (real? z1) (>= z1 0))
799 ($expt z1 z2))
800 (else
801 (exp (* z2 (log z1))))))))
802
803 (define (sinh z)
804 (if (real? z) ($sinh z)
805 (let ((x (real-part z)) (y (imag-part z)))
806 (make-rectangular (* ($sinh x) ($cos y))
807 (* ($cosh x) ($sin y))))))
808 (define (cosh z)
809 (if (real? z) ($cosh z)
810 (let ((x (real-part z)) (y (imag-part z)))
811 (make-rectangular (* ($cosh x) ($cos y))
812 (* ($sinh x) ($sin y))))))
813 (define (tanh z)
814 (if (real? z) ($tanh z)
815 (let* ((x (* 2 (real-part z)))
816 (y (* 2 (imag-part z)))
817 (w (+ ($cosh x) ($cos y))))
818 (make-rectangular (/ ($sinh x) w) (/ ($sin y) w)))))
819
820 (define (asinh z)
821 (if (real? z) ($asinh z)
822 (log (+ z (sqrt (+ (* z z) 1))))))
823
824 (define (acosh z)
825 (if (and (real? z) (>= z 1))
826 ($acosh z)
827 (log (+ z (sqrt (- (* z z) 1))))))
828
829 (define (atanh z)
830 (if (and (real? z) (> z -1) (< z 1))
831 ($atanh z)
832 (/ (log (/ (+ 1 z) (- 1 z))) 2)))
833
834 (define (sin z)
835 (if (real? z) ($sin z)
836 (let ((x (real-part z)) (y (imag-part z)))
837 (make-rectangular (* ($sin x) ($cosh y))
838 (* ($cos x) ($sinh y))))))
839 (define (cos z)
840 (if (real? z) ($cos z)
841 (let ((x (real-part z)) (y (imag-part z)))
842 (make-rectangular (* ($cos x) ($cosh y))
843 (- (* ($sin x) ($sinh y)))))))
844 (define (tan z)
845 (if (real? z) ($tan z)
846 (let* ((x (* 2 (real-part z)))
847 (y (* 2 (imag-part z)))
848 (w (+ ($cos x) ($cosh y))))
849 (make-rectangular (/ ($sin x) w) (/ ($sinh y) w)))))
850
851 (define (asin z)
852 (if (and (real? z) (>= z -1) (<= z 1))
853 ($asin z)
854 (* -i (asinh (* +i z)))))
855
856 (define (acos z)
857 (if (and (real? z) (>= z -1) (<= z 1))
858 ($acos z)
859 (+ (/ (angle -1) 2) (* +i (asinh (* +i z))))))
860
861 (define (atan z . y)
862 (if (null? y)
863 (if (real? z) ($atan z)
864 (/ (log (/ (- +i z) (+ +i z))) +2i))
865 ($atan2 z (car y))))
866
867 (define (log10 arg)
868 (/ (log arg) (log 10)))
869
870 \f
871
872 ;;; {Reader Extensions}
873 ;;;
874
875 ;;; Reader code for various "#c" forms.
876 ;;;
877
878 (read-hash-extend #\' (lambda (c port)
879 (read port)))
880 (read-hash-extend #\. (lambda (c port)
881 (eval (read port))))
882
883 \f
884 ;;; {Command Line Options}
885 ;;;
886
887 (define (get-option argv kw-opts kw-args return)
888 (cond
889 ((null? argv)
890 (return #f #f argv))
891
892 ((or (not (eq? #\- (string-ref (car argv) 0)))
893 (eq? (string-length (car argv)) 1))
894 (return 'normal-arg (car argv) (cdr argv)))
895
896 ((eq? #\- (string-ref (car argv) 1))
897 (let* ((kw-arg-pos (or (string-index (car argv) #\=)
898 (string-length (car argv))))
899 (kw (symbol->keyword (substring (car argv) 2 kw-arg-pos)))
900 (kw-opt? (member kw kw-opts))
901 (kw-arg? (member kw kw-args))
902 (arg (or (and (not (eq? kw-arg-pos (string-length (car argv))))
903 (substring (car argv)
904 (+ kw-arg-pos 1)
905 (string-length (car argv))))
906 (and kw-arg?
907 (begin (set! argv (cdr argv)) (car argv))))))
908 (if (or kw-opt? kw-arg?)
909 (return kw arg (cdr argv))
910 (return 'usage-error kw (cdr argv)))))
911
912 (else
913 (let* ((char (substring (car argv) 1 2))
914 (kw (symbol->keyword char)))
915 (cond
916
917 ((member kw kw-opts)
918 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
919 (new-argv (if (= 0 (string-length rest-car))
920 (cdr argv)
921 (cons (string-append "-" rest-car) (cdr argv)))))
922 (return kw #f new-argv)))
923
924 ((member kw kw-args)
925 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
926 (arg (if (= 0 (string-length rest-car))
927 (cadr argv)
928 rest-car))
929 (new-argv (if (= 0 (string-length rest-car))
930 (cddr argv)
931 (cdr argv))))
932 (return kw arg new-argv)))
933
934 (else (return 'usage-error kw argv)))))))
935
936 (define (for-next-option proc argv kw-opts kw-args)
937 (let loop ((argv argv))
938 (get-option argv kw-opts kw-args
939 (lambda (opt opt-arg argv)
940 (and opt (proc opt opt-arg argv loop))))))
941
942 (define (display-usage-report kw-desc)
943 (for-each
944 (lambda (kw)
945 (or (eq? (car kw) #t)
946 (eq? (car kw) 'else)
947 (let* ((opt-desc kw)
948 (help (cadr opt-desc))
949 (opts (car opt-desc))
950 (opts-proper (if (string? (car opts)) (cdr opts) opts))
951 (arg-name (if (string? (car opts))
952 (string-append "<" (car opts) ">")
953 ""))
954 (left-part (string-append
955 (with-output-to-string
956 (lambda ()
957 (map (lambda (x) (display (keyword-symbol x)) (display " "))
958 opts-proper)))
959 arg-name))
960 (middle-part (if (and (< (string-length left-part) 30)
961 (< (string-length help) 40))
962 (make-string (- 30 (string-length left-part)) #\ )
963 "\n\t")))
964 (display left-part)
965 (display middle-part)
966 (display help)
967 (newline))))
968 kw-desc))
969
970
971
972 (define (transform-usage-lambda cases)
973 (let* ((raw-usage (delq! 'else (map car cases)))
974 (usage-sans-specials (map (lambda (x)
975 (or (and (not (list? x)) x)
976 (and (symbol? (car x)) #t)
977 (and (boolean? (car x)) #t)
978 x))
979 raw-usage))
980 (usage-desc (delq! #t usage-sans-specials))
981 (kw-desc (map car usage-desc))
982 (kw-opts (apply append (map (lambda (x) (and (not (string? (car x))) x)) kw-desc)))
983 (kw-args (apply append (map (lambda (x) (and (string? (car x)) (cdr x))) kw-desc)))
984 (transmogrified-cases (map (lambda (case)
985 (cons (let ((opts (car case)))
986 (if (or (boolean? opts) (eq? 'else opts))
987 opts
988 (cond
989 ((symbol? (car opts)) opts)
990 ((boolean? (car opts)) opts)
991 ((string? (caar opts)) (cdar opts))
992 (else (car opts)))))
993 (cdr case)))
994 cases)))
995 `(let ((%display-usage (lambda () (display-usage-report ',usage-desc))))
996 (lambda (%argv)
997 (let %next-arg ((%argv %argv))
998 (get-option %argv
999 ',kw-opts
1000 ',kw-args
1001 (lambda (%opt %arg %new-argv)
1002 (case %opt
1003 ,@ transmogrified-cases))))))))
1004
1005
1006 \f
1007
1008 ;;; {Low Level Modules}
1009 ;;;
1010 ;;; These are the low level data structures for modules.
1011 ;;;
1012 ;;; !!! warning: The interface to lazy binder procedures is going
1013 ;;; to be changed in an incompatible way to permit all the basic
1014 ;;; module ops to be virtualized.
1015 ;;;
1016 ;;; (make-module size use-list lazy-binding-proc) => module
1017 ;;; module-{obarray,uses,binder}[|-set!]
1018 ;;; (module? obj) => [#t|#f]
1019 ;;; (module-locally-bound? module symbol) => [#t|#f]
1020 ;;; (module-bound? module symbol) => [#t|#f]
1021 ;;; (module-symbol-locally-interned? module symbol) => [#t|#f]
1022 ;;; (module-symbol-interned? module symbol) => [#t|#f]
1023 ;;; (module-local-variable module symbol) => [#<variable ...> | #f]
1024 ;;; (module-variable module symbol) => [#<variable ...> | #f]
1025 ;;; (module-symbol-binding module symbol opt-value)
1026 ;;; => [ <obj> | opt-value | an error occurs ]
1027 ;;; (module-make-local-var! module symbol) => #<variable...>
1028 ;;; (module-add! module symbol var) => unspecified
1029 ;;; (module-remove! module symbol) => unspecified
1030 ;;; (module-for-each proc module) => unspecified
1031 ;;; (make-scm-module) => module ; a lazy copy of the symhash module
1032 ;;; (set-current-module module) => unspecified
1033 ;;; (current-module) => #<module...>
1034 ;;;
1035 ;;;
1036
1037 \f
1038 ;;; {Printing Modules}
1039 ;; This is how modules are printed. You can re-define it.
1040 ;; (Redefining is actually more complicated than simply redefining
1041 ;; %print-module because that would only change the binding and not
1042 ;; the value stored in the vtable that determines how record are
1043 ;; printed. Sigh.)
1044
1045 (define (%print-module mod port) ; unused args: depth length style table)
1046 (display "#<" port)
1047 (display (or (module-kind mod) "module") port)
1048 (let ((name (module-name mod)))
1049 (if name
1050 (begin
1051 (display " " port)
1052 (display name port))))
1053 (display " " port)
1054 (display (number->string (object-address mod) 16) port)
1055 (display ">" port))
1056
1057 ;; module-type
1058 ;;
1059 ;; A module is characterized by an obarray in which local symbols
1060 ;; are interned, a list of modules, "uses", from which non-local
1061 ;; bindings can be inherited, and an optional lazy-binder which
1062 ;; is a (CLOSURE module symbol) which, as a last resort, can provide
1063 ;; bindings that would otherwise not be found locally in the module.
1064 ;;
1065 (define module-type
1066 (make-record-type 'module
1067 '(obarray uses binder eval-closure transformer name kind
1068 observers weak-observers observer-id)
1069 %print-module))
1070
1071 ;; make-module &opt size uses binder
1072 ;;
1073 ;; Create a new module, perhaps with a particular size of obarray,
1074 ;; initial uses list, or binding procedure.
1075 ;;
1076 (define make-module
1077 (lambda args
1078
1079 (define (parse-arg index default)
1080 (if (> (length args) index)
1081 (list-ref args index)
1082 default))
1083
1084 (if (> (length args) 3)
1085 (error "Too many args to make-module." args))
1086
1087 (let ((size (parse-arg 0 1021))
1088 (uses (parse-arg 1 '()))
1089 (binder (parse-arg 2 #f)))
1090
1091 (if (not (integer? size))
1092 (error "Illegal size to make-module." size))
1093 (if (not (and (list? uses)
1094 (and-map module? uses)))
1095 (error "Incorrect use list." uses))
1096 (if (and binder (not (procedure? binder)))
1097 (error
1098 "Lazy-binder expected to be a procedure or #f." binder))
1099
1100 (let ((module (module-constructor (make-vector size '())
1101 uses binder #f #f #f #f
1102 '()
1103 (make-weak-value-hash-table 31)
1104 0)))
1105
1106 ;; We can't pass this as an argument to module-constructor,
1107 ;; because we need it to close over a pointer to the module
1108 ;; itself.
1109 (set-module-eval-closure! module (standard-eval-closure module))
1110
1111 module))))
1112
1113 (define module-constructor (record-constructor module-type))
1114 (define module-obarray (record-accessor module-type 'obarray))
1115 (define set-module-obarray! (record-modifier module-type 'obarray))
1116 (define module-uses (record-accessor module-type 'uses))
1117 (define set-module-uses! (record-modifier module-type 'uses))
1118 (define module-binder (record-accessor module-type 'binder))
1119 (define set-module-binder! (record-modifier module-type 'binder))
1120
1121 ;; NOTE: This binding is used in libguile/modules.c.
1122 (define module-eval-closure (record-accessor module-type 'eval-closure))
1123
1124 (define module-transformer (record-accessor module-type 'transformer))
1125 (define set-module-transformer! (record-modifier module-type 'transformer))
1126 (define module-name (record-accessor module-type 'name))
1127 (define set-module-name! (record-modifier module-type 'name))
1128 (define module-kind (record-accessor module-type 'kind))
1129 (define set-module-kind! (record-modifier module-type 'kind))
1130 (define module-observers (record-accessor module-type 'observers))
1131 (define set-module-observers! (record-modifier module-type 'observers))
1132 (define module-weak-observers (record-accessor module-type 'weak-observers))
1133 (define module-observer-id (record-accessor module-type 'observer-id))
1134 (define set-module-observer-id! (record-modifier module-type 'observer-id))
1135 (define module? (record-predicate module-type))
1136
1137 (define set-module-eval-closure!
1138 (let ((setter (record-modifier module-type 'eval-closure)))
1139 (lambda (module closure)
1140 (setter module closure)
1141 ;; Make it possible to lookup the module from the environment.
1142 ;; This implementation is correct since an eval closure can belong
1143 ;; to maximally one module.
1144 (set-procedure-property! closure 'module module))))
1145
1146 (define (eval-in-module exp module)
1147 (eval2 exp (module-eval-closure module)))
1148
1149 \f
1150 ;;; {Observer protocol}
1151 ;;;
1152
1153 (define (module-observe module proc)
1154 (set-module-observers! module (cons proc (module-observers module)))
1155 (cons module proc))
1156
1157 (define (module-observe-weak module proc)
1158 (let ((id (module-observer-id module)))
1159 (hash-set! (module-weak-observers module) id proc)
1160 (set-module-observer-id! module (+ 1 id))
1161 (cons module id)))
1162
1163 (define (module-unobserve token)
1164 (let ((module (car token))
1165 (id (cdr token)))
1166 (if (integer? id)
1167 (hash-remove! (module-weak-observers module) id)
1168 (set-module-observers! module (delq1! id (module-observers module)))))
1169 *unspecified*)
1170
1171 (define (module-modified m)
1172 (for-each (lambda (proc) (proc m)) (module-observers m))
1173 (hash-fold (lambda (id proc res) (proc m)) #f (module-weak-observers m)))
1174
1175 \f
1176 ;;; {Module Searching in General}
1177 ;;;
1178 ;;; We sometimes want to look for properties of a symbol
1179 ;;; just within the obarray of one module. If the property
1180 ;;; holds, then it is said to hold ``locally'' as in, ``The symbol
1181 ;;; DISPLAY is locally rebound in the module `safe-guile'.''
1182 ;;;
1183 ;;;
1184 ;;; Other times, we want to test for a symbol property in the obarray
1185 ;;; of M and, if it is not found there, try each of the modules in the
1186 ;;; uses list of M. This is the normal way of testing for some
1187 ;;; property, so we state these properties without qualification as
1188 ;;; in: ``The symbol 'fnord is interned in module M because it is
1189 ;;; interned locally in module M2 which is a member of the uses list
1190 ;;; of M.''
1191 ;;;
1192
1193 ;; module-search fn m
1194 ;;
1195 ;; return the first non-#f result of FN applied to M and then to
1196 ;; the modules in the uses of m, and so on recursively. If all applications
1197 ;; return #f, then so does this function.
1198 ;;
1199 (define (module-search fn m v)
1200 (define (loop pos)
1201 (and (pair? pos)
1202 (or (module-search fn (car pos) v)
1203 (loop (cdr pos)))))
1204 (or (fn m v)
1205 (loop (module-uses m))))
1206
1207
1208 ;;; {Is a symbol bound in a module?}
1209 ;;;
1210 ;;; Symbol S in Module M is bound if S is interned in M and if the binding
1211 ;;; of S in M has been set to some well-defined value.
1212 ;;;
1213
1214 ;; module-locally-bound? module symbol
1215 ;;
1216 ;; Is a symbol bound (interned and defined) locally in a given module?
1217 ;;
1218 (define (module-locally-bound? m v)
1219 (let ((var (module-local-variable m v)))
1220 (and var
1221 (variable-bound? var))))
1222
1223 ;; module-bound? module symbol
1224 ;;
1225 ;; Is a symbol bound (interned and defined) anywhere in a given module
1226 ;; or its uses?
1227 ;;
1228 (define (module-bound? m v)
1229 (module-search module-locally-bound? m v))
1230
1231 ;;; {Is a symbol interned in a module?}
1232 ;;;
1233 ;;; Symbol S in Module M is interned if S occurs in
1234 ;;; of S in M has been set to some well-defined value.
1235 ;;;
1236 ;;; It is possible to intern a symbol in a module without providing
1237 ;;; an initial binding for the corresponding variable. This is done
1238 ;;; with:
1239 ;;; (module-add! module symbol (make-undefined-variable))
1240 ;;;
1241 ;;; In that case, the symbol is interned in the module, but not
1242 ;;; bound there. The unbound symbol shadows any binding for that
1243 ;;; symbol that might otherwise be inherited from a member of the uses list.
1244 ;;;
1245
1246 (define (module-obarray-get-handle ob key)
1247 ((if (symbol? key) hashq-get-handle hash-get-handle) ob key))
1248
1249 (define (module-obarray-ref ob key)
1250 ((if (symbol? key) hashq-ref hash-ref) ob key))
1251
1252 (define (module-obarray-set! ob key val)
1253 ((if (symbol? key) hashq-set! hash-set!) ob key val))
1254
1255 (define (module-obarray-remove! ob key)
1256 ((if (symbol? key) hashq-remove! hash-remove!) ob key))
1257
1258 ;; module-symbol-locally-interned? module symbol
1259 ;;
1260 ;; is a symbol interned (not neccessarily defined) locally in a given module
1261 ;; or its uses? Interned symbols shadow inherited bindings even if
1262 ;; they are not themselves bound to a defined value.
1263 ;;
1264 (define (module-symbol-locally-interned? m v)
1265 (not (not (module-obarray-get-handle (module-obarray m) v))))
1266
1267 ;; module-symbol-interned? module symbol
1268 ;;
1269 ;; is a symbol interned (not neccessarily defined) anywhere in a given module
1270 ;; or its uses? Interned symbols shadow inherited bindings even if
1271 ;; they are not themselves bound to a defined value.
1272 ;;
1273 (define (module-symbol-interned? m v)
1274 (module-search module-symbol-locally-interned? m v))
1275
1276
1277 ;;; {Mapping modules x symbols --> variables}
1278 ;;;
1279
1280 ;; module-local-variable module symbol
1281 ;; return the local variable associated with a MODULE and SYMBOL.
1282 ;;
1283 ;;; This function is very important. It is the only function that can
1284 ;;; return a variable from a module other than the mutators that store
1285 ;;; new variables in modules. Therefore, this function is the location
1286 ;;; of the "lazy binder" hack.
1287 ;;;
1288 ;;; If symbol is defined in MODULE, and if the definition binds symbol
1289 ;;; to a variable, return that variable object.
1290 ;;;
1291 ;;; If the symbols is not found at first, but the module has a lazy binder,
1292 ;;; then try the binder.
1293 ;;;
1294 ;;; If the symbol is not found at all, return #f.
1295 ;;;
1296 (define (module-local-variable m v)
1297 ; (caddr
1298 ; (list m v
1299 (let ((b (module-obarray-ref (module-obarray m) v)))
1300 (or (and (variable? b) b)
1301 (and (module-binder m)
1302 ((module-binder m) m v #f)))))
1303 ;))
1304
1305 ;; module-variable module symbol
1306 ;;
1307 ;; like module-local-variable, except search the uses in the
1308 ;; case V is not found in M.
1309 ;;
1310 ;; NOTE: This function is superseded with C code (see modules.c)
1311 ;;; when using the standard eval closure.
1312 ;;
1313 (define (module-variable m v)
1314 (module-search module-local-variable m v))
1315
1316
1317 ;;; {Mapping modules x symbols --> bindings}
1318 ;;;
1319 ;;; These are similar to the mapping to variables, except that the
1320 ;;; variable is dereferenced.
1321 ;;;
1322
1323 ;; module-symbol-binding module symbol opt-value
1324 ;;
1325 ;; return the binding of a variable specified by name within
1326 ;; a given module, signalling an error if the variable is unbound.
1327 ;; If the OPT-VALUE is passed, then instead of signalling an error,
1328 ;; return OPT-VALUE.
1329 ;;
1330 (define (module-symbol-local-binding m v . opt-val)
1331 (let ((var (module-local-variable m v)))
1332 (if var
1333 (variable-ref var)
1334 (if (not (null? opt-val))
1335 (car opt-val)
1336 (error "Locally unbound variable." v)))))
1337
1338 ;; module-symbol-binding module symbol opt-value
1339 ;;
1340 ;; return the binding of a variable specified by name within
1341 ;; a given module, signalling an error if the variable is unbound.
1342 ;; If the OPT-VALUE is passed, then instead of signalling an error,
1343 ;; return OPT-VALUE.
1344 ;;
1345 (define (module-symbol-binding m v . opt-val)
1346 (let ((var (module-variable m v)))
1347 (if var
1348 (variable-ref var)
1349 (if (not (null? opt-val))
1350 (car opt-val)
1351 (error "Unbound variable." v)))))
1352
1353
1354 \f
1355 ;;; {Adding Variables to Modules}
1356 ;;;
1357 ;;;
1358
1359
1360 ;; module-make-local-var! module symbol
1361 ;;
1362 ;; ensure a variable for V in the local namespace of M.
1363 ;; If no variable was already there, then create a new and uninitialzied
1364 ;; variable.
1365 ;;
1366 (define (module-make-local-var! m v)
1367 (or (let ((b (module-obarray-ref (module-obarray m) v)))
1368 (and (variable? b)
1369 (begin
1370 (module-modified m)
1371 b)))
1372 (and (module-binder m)
1373 ((module-binder m) m v #t))
1374 (begin
1375 (let ((answer (make-undefined-variable v)))
1376 (module-obarray-set! (module-obarray m) v answer)
1377 (module-modified m)
1378 answer))))
1379
1380 ;; module-add! module symbol var
1381 ;;
1382 ;; ensure a particular variable for V in the local namespace of M.
1383 ;;
1384 (define (module-add! m v var)
1385 (if (not (variable? var))
1386 (error "Bad variable to module-add!" var))
1387 (module-obarray-set! (module-obarray m) v var)
1388 (module-modified m))
1389
1390 ;; module-remove!
1391 ;;
1392 ;; make sure that a symbol is undefined in the local namespace of M.
1393 ;;
1394 (define (module-remove! m v)
1395 (module-obarray-remove! (module-obarray m) v)
1396 (module-modified m))
1397
1398 (define (module-clear! m)
1399 (vector-fill! (module-obarray m) '())
1400 (module-modified m))
1401
1402 ;; MODULE-FOR-EACH -- exported
1403 ;;
1404 ;; Call PROC on each symbol in MODULE, with arguments of (SYMBOL VARIABLE).
1405 ;;
1406 (define (module-for-each proc module)
1407 (let ((obarray (module-obarray module)))
1408 (do ((index 0 (+ index 1))
1409 (end (vector-length obarray)))
1410 ((= index end))
1411 (for-each
1412 (lambda (bucket)
1413 (proc (car bucket) (cdr bucket)))
1414 (vector-ref obarray index)))))
1415
1416
1417 (define (module-map proc module)
1418 (let* ((obarray (module-obarray module))
1419 (end (vector-length obarray)))
1420
1421 (let loop ((i 0)
1422 (answer '()))
1423 (if (= i end)
1424 answer
1425 (loop (+ 1 i)
1426 (append!
1427 (map (lambda (bucket)
1428 (proc (car bucket) (cdr bucket)))
1429 (vector-ref obarray i))
1430 answer))))))
1431 \f
1432
1433 ;;; {Low Level Bootstrapping}
1434 ;;;
1435
1436 ;; make-root-module
1437
1438 ;; A root module uses the symhash table (the system's privileged
1439 ;; obarray). Being inside a root module is like using SCM without
1440 ;; any module system.
1441 ;;
1442
1443
1444 (define (root-module-closure m s define?)
1445 (let ((bi (and (symbol-interned? #f s)
1446 (builtin-variable s))))
1447 (and bi
1448 (or define? (variable-bound? bi))
1449 (begin
1450 (module-add! m s bi)
1451 bi))))
1452
1453 (define (make-root-module)
1454 (make-module 1019 '() root-module-closure))
1455
1456
1457 ;; make-scm-module
1458
1459 ;; An scm module is a module into which the lazy binder copies
1460 ;; variable bindings from the system symhash table. The mapping is
1461 ;; one way only; newly introduced bindings in an scm module are not
1462 ;; copied back into the system symhash table (and can be used to override
1463 ;; bindings from the symhash table).
1464 ;;
1465
1466 (define (scm-module-closure m s define?)
1467 (let ((bi (and (symbol-interned? #f s)
1468 (builtin-variable s))))
1469 (and bi
1470 (variable-bound? bi)
1471 (begin
1472 (module-add! m s bi)
1473 bi))))
1474
1475 (define (make-scm-module)
1476 (make-module 1019 '() scm-module-closure))
1477
1478
1479
1480 ;; the-module
1481 ;;
1482 ;; NOTE: This binding is used in libguile/modules.c.
1483 ;;
1484 (define the-module #f)
1485
1486 ;; scm:eval-transformer
1487 ;;
1488 (define scm:eval-transformer #f)
1489
1490 ;; set-current-module module
1491 ;;
1492 ;; set the current module as viewed by the normalizer.
1493 ;;
1494 ;; NOTE: This binding is used in libguile/modules.c.
1495 ;;
1496 (define (set-current-module m)
1497 (set! the-module m)
1498 (if m
1499 (begin
1500 (set! *top-level-lookup-closure* (module-eval-closure the-module))
1501 (set! scm:eval-transformer (module-transformer the-module)))
1502 (set! *top-level-lookup-closure* #f)))
1503
1504
1505 ;; current-module
1506 ;;
1507 ;; return the current module as viewed by the normalizer.
1508 ;;
1509 (define (current-module) the-module)
1510 \f
1511 ;;; {Module-based Loading}
1512 ;;;
1513
1514 (define (save-module-excursion thunk)
1515 (let ((inner-module (current-module))
1516 (outer-module #f))
1517 (dynamic-wind (lambda ()
1518 (set! outer-module (current-module))
1519 (set-current-module inner-module)
1520 (set! inner-module #f))
1521 thunk
1522 (lambda ()
1523 (set! inner-module (current-module))
1524 (set-current-module outer-module)
1525 (set! outer-module #f)))))
1526
1527 (define basic-load load)
1528
1529 (define (load-module filename)
1530 (save-module-excursion
1531 (lambda ()
1532 (let ((oldname (and (current-load-port)
1533 (port-filename (current-load-port)))))
1534 (basic-load (if (and oldname
1535 (> (string-length filename) 0)
1536 (not (char=? (string-ref filename 0) #\/))
1537 (not (string=? (dirname oldname) ".")))
1538 (string-append (dirname oldname) "/" filename)
1539 filename))))))
1540
1541
1542 \f
1543 ;;; {MODULE-REF -- exported}
1544 ;;
1545 ;; Returns the value of a variable called NAME in MODULE or any of its
1546 ;; used modules. If there is no such variable, then if the optional third
1547 ;; argument DEFAULT is present, it is returned; otherwise an error is signaled.
1548 ;;
1549 (define (module-ref module name . rest)
1550 (let ((variable (module-variable module name)))
1551 (if (and variable (variable-bound? variable))
1552 (variable-ref variable)
1553 (if (null? rest)
1554 (error "No variable named" name 'in module)
1555 (car rest) ; default value
1556 ))))
1557
1558 ;; MODULE-SET! -- exported
1559 ;;
1560 ;; Sets the variable called NAME in MODULE (or in a module that MODULE uses)
1561 ;; to VALUE; if there is no such variable, an error is signaled.
1562 ;;
1563 (define (module-set! module name value)
1564 (let ((variable (module-variable module name)))
1565 (if variable
1566 (variable-set! variable value)
1567 (error "No variable named" name 'in module))))
1568
1569 ;; MODULE-DEFINE! -- exported
1570 ;;
1571 ;; Sets the variable called NAME in MODULE to VALUE; if there is no such
1572 ;; variable, it is added first.
1573 ;;
1574 (define (module-define! module name value)
1575 (let ((variable (module-local-variable module name)))
1576 (if variable
1577 (begin
1578 (variable-set! variable value)
1579 (module-modified module))
1580 (module-add! module name (make-variable value name)))))
1581
1582 ;; MODULE-DEFINED? -- exported
1583 ;;
1584 ;; Return #t iff NAME is defined in MODULE (or in a module that MODULE
1585 ;; uses)
1586 ;;
1587 (define (module-defined? module name)
1588 (let ((variable (module-variable module name)))
1589 (and variable (variable-bound? variable))))
1590
1591 ;; MODULE-USE! module interface
1592 ;;
1593 ;; Add INTERFACE to the list of interfaces used by MODULE.
1594 ;;
1595 (define (module-use! module interface)
1596 (set-module-uses! module
1597 (cons interface (delq! interface (module-uses module))))
1598 (module-modified module))
1599
1600 \f
1601 ;;; {Recursive Namespaces}
1602 ;;;
1603 ;;;
1604 ;;; A hierarchical namespace emerges if we consider some module to be
1605 ;;; root, and variables bound to modules as nested namespaces.
1606 ;;;
1607 ;;; The routines in this file manage variable names in hierarchical namespace.
1608 ;;; Each variable name is a list of elements, looked up in successively nested
1609 ;;; modules.
1610 ;;;
1611 ;;; (nested-ref some-root-module '(foo bar baz))
1612 ;;; => <value of a variable named baz in the module bound to bar in
1613 ;;; the module bound to foo in some-root-module>
1614 ;;;
1615 ;;;
1616 ;;; There are:
1617 ;;;
1618 ;;; ;; a-root is a module
1619 ;;; ;; name is a list of symbols
1620 ;;;
1621 ;;; nested-ref a-root name
1622 ;;; nested-set! a-root name val
1623 ;;; nested-define! a-root name val
1624 ;;; nested-remove! a-root name
1625 ;;;
1626 ;;;
1627 ;;; (current-module) is a natural choice for a-root so for convenience there are
1628 ;;; also:
1629 ;;;
1630 ;;; local-ref name == nested-ref (current-module) name
1631 ;;; local-set! name val == nested-set! (current-module) name val
1632 ;;; local-define! name val == nested-define! (current-module) name val
1633 ;;; local-remove! name == nested-remove! (current-module) name
1634 ;;;
1635
1636
1637 (define (nested-ref root names)
1638 (let loop ((cur root)
1639 (elts names))
1640 (cond
1641 ((null? elts) cur)
1642 ((not (module? cur)) #f)
1643 (else (loop (module-ref cur (car elts) #f) (cdr elts))))))
1644
1645 (define (nested-set! root names val)
1646 (let loop ((cur root)
1647 (elts names))
1648 (if (null? (cdr elts))
1649 (module-set! cur (car elts) val)
1650 (loop (module-ref cur (car elts)) (cdr elts)))))
1651
1652 (define (nested-define! root names val)
1653 (let loop ((cur root)
1654 (elts names))
1655 (if (null? (cdr elts))
1656 (module-define! cur (car elts) val)
1657 (loop (module-ref cur (car elts)) (cdr elts)))))
1658
1659 (define (nested-remove! root names)
1660 (let loop ((cur root)
1661 (elts names))
1662 (if (null? (cdr elts))
1663 (module-remove! cur (car elts))
1664 (loop (module-ref cur (car elts)) (cdr elts)))))
1665
1666 (define (local-ref names) (nested-ref (current-module) names))
1667 (define (local-set! names val) (nested-set! (current-module) names val))
1668 (define (local-define names val) (nested-define! (current-module) names val))
1669 (define (local-remove names) (nested-remove! (current-module) names))
1670
1671
1672 \f
1673 ;;; {The (app) module}
1674 ;;;
1675 ;;; The root of conventionally named objects not directly in the top level.
1676 ;;;
1677 ;;; (app modules)
1678 ;;; (app modules guile)
1679 ;;;
1680 ;;; The directory of all modules and the standard root module.
1681 ;;;
1682
1683 (define (module-public-interface m)
1684 (module-ref m '%module-public-interface #f))
1685 (define (set-module-public-interface! m i)
1686 (module-define! m '%module-public-interface i))
1687 (define (set-system-module! m s)
1688 (set-procedure-property! (module-eval-closure m) 'system-module s))
1689 (define the-root-module (make-root-module))
1690 (define the-scm-module (make-scm-module))
1691 (set-module-public-interface! the-root-module the-scm-module)
1692 (set-module-name! the-root-module '(guile))
1693 (set-module-name! the-scm-module '(guile))
1694 (set-module-kind! the-scm-module 'interface)
1695 (for-each set-system-module! (list the-root-module the-scm-module) '(#t #t))
1696
1697 (set-current-module the-root-module)
1698
1699 (define app (make-module 31))
1700 (local-define '(app modules) (make-module 31))
1701 (local-define '(app modules guile) the-root-module)
1702
1703 ;; (define-special-value '(app modules new-ws) (lambda () (make-scm-module)))
1704
1705 (define (try-load-module name)
1706 (or (try-module-linked name)
1707 (try-module-autoload name)
1708 (try-module-dynamic-link name)))
1709
1710 ;; NOTE: This binding is used in libguile/modules.c.
1711 ;;
1712 (define (resolve-module name . maybe-autoload)
1713 (let ((full-name (append '(app modules) name)))
1714 (let ((already (local-ref full-name)))
1715 (if already
1716 ;; The module already exists...
1717 (if (and (or (null? maybe-autoload) (car maybe-autoload))
1718 (not (module-ref already '%module-public-interface #f)))
1719 ;; ...but we are told to load and it doesn't contain source, so
1720 (begin
1721 (try-load-module name)
1722 already)
1723 ;; simply return it.
1724 already)
1725 (begin
1726 ;; Try to autoload it if we are told so
1727 (if (or (null? maybe-autoload) (car maybe-autoload))
1728 (try-load-module name))
1729 ;; Get/create it.
1730 (make-modules-in (current-module) full-name))))))
1731
1732 (define (beautify-user-module! module)
1733 (let ((interface (module-public-interface module)))
1734 (if (or (not interface)
1735 (eq? interface module))
1736 (let ((interface (make-module 31)))
1737 (set-module-name! interface (module-name module))
1738 (set-module-kind! interface 'interface)
1739 (set-module-public-interface! module interface))))
1740 (if (and (not (memq the-scm-module (module-uses module)))
1741 (not (eq? module the-root-module)))
1742 (set-module-uses! module (append (module-uses module) (list the-scm-module)))))
1743
1744 (define (purify-module! module)
1745 "Removes bindings in MODULE which are inherited from the (guile) module."
1746 (let ((use-list (module-uses module)))
1747 (if (and (pair? use-list)
1748 (eq? (car (last-pair use-list)) the-scm-module))
1749 (set-module-uses! module (reverse (cdr (reverse use-list)))))))
1750
1751 ;; NOTE: This binding is used in libguile/modules.c.
1752 ;;
1753 (define (make-modules-in module name)
1754 (if (null? name)
1755 module
1756 (cond
1757 ((module-ref module (car name) #f)
1758 => (lambda (m) (make-modules-in m (cdr name))))
1759 (else (let ((m (make-module 31)))
1760 (set-module-kind! m 'directory)
1761 (set-module-name! m (append (or (module-name module)
1762 '())
1763 (list (car name))))
1764 (module-define! module (car name) m)
1765 (make-modules-in m (cdr name)))))))
1766
1767 (define (resolve-interface name)
1768 (let ((module (resolve-module name)))
1769 (and module (module-public-interface module))))
1770
1771
1772 (define %autoloader-developer-mode #t)
1773
1774 (define (process-define-module args)
1775 (let* ((module-id (car args))
1776 (module (resolve-module module-id #f))
1777 (kws (cdr args)))
1778 (beautify-user-module! module)
1779 (let loop ((kws kws)
1780 (reversed-interfaces '()))
1781 (if (null? kws)
1782 (for-each (lambda (interface)
1783 (module-use! module interface))
1784 reversed-interfaces)
1785 (let ((keyword (cond ((keyword? (car kws))
1786 (keyword->symbol (car kws)))
1787 ((and (symbol? (car kws))
1788 (eq? (string-ref (car kws) 0) #\:))
1789 (string->symbol (substring (car kws) 1)))
1790 (else #f))))
1791 (case keyword
1792 ((use-module use-syntax)
1793 (if (not (pair? (cdr kws)))
1794 (error "unrecognized defmodule argument" kws))
1795 (let* ((used-name (cadr kws))
1796 (used-module (resolve-module used-name)))
1797 (if (not (module-ref used-module
1798 '%module-public-interface
1799 #f))
1800 (begin
1801 ((if %autoloader-developer-mode warn error)
1802 "no code for module" (module-name used-module))
1803 (beautify-user-module! used-module)))
1804 (let ((interface (module-public-interface used-module)))
1805 (if (not interface)
1806 (error "missing interface for use-module"
1807 used-module))
1808 (if (eq? keyword 'use-syntax)
1809 (set-module-transformer!
1810 module
1811 (module-ref interface (car (last-pair used-name))
1812 #f)))
1813 (loop (cddr kws)
1814 (cons interface reversed-interfaces)))))
1815 ((autoload)
1816 (if (not (and (pair? (cdr kws)) (pair? (cddr kws))))
1817 (error "unrecognized defmodule argument" kws))
1818 (loop (cdddr kws)
1819 (cons (make-autoload-interface module
1820 (cadr kws)
1821 (caddr kws))
1822 reversed-interfaces)))
1823 ((no-backtrace)
1824 (set-system-module! module #t)
1825 (loop (cdr kws) reversed-interfaces))
1826 ((pure)
1827 (purify-module! module)
1828 (loop (cdr kws) reversed-interfaces))
1829 ((export)
1830 (if (not (and (pair? (cdr kws)) (pair? (cddr kws))))
1831 (error "unrecognized defmodule argument" kws))
1832 (module-export! module (cadr kws))
1833 (loop (cddr kws) reversed-interfaces))
1834 (else
1835 (error "unrecognized defmodule argument" kws))))))
1836 module))
1837
1838 ;;; {Autoload}
1839
1840 (define (make-autoload-interface module name bindings)
1841 (let ((b (lambda (a sym definep)
1842 (and (memq sym bindings)
1843 (let ((i (module-public-interface (resolve-module name))))
1844 (if (not i)
1845 (error "missing interface for module" name))
1846 ;; Replace autoload-interface with interface
1847 (set-car! (memq a (module-uses module)) i)
1848 (module-local-variable i sym))))))
1849 (module-constructor #() '() b #f #f name 'autoload
1850 '() (make-weak-value-hash-table 31) 0)))
1851
1852 \f
1853 ;;; {Autoloading modules}
1854
1855 (define autoloads-in-progress '())
1856
1857 (define (try-module-autoload module-name)
1858 (let* ((reverse-name (reverse module-name))
1859 (name (car reverse-name))
1860 (dir-hint-module-name (reverse (cdr reverse-name)))
1861 (dir-hint (apply symbol-append (map (lambda (elt) (symbol-append elt "/")) dir-hint-module-name))))
1862 (resolve-module dir-hint-module-name #f)
1863 (and (not (autoload-done-or-in-progress? dir-hint name))
1864 (let ((didit #f))
1865 (dynamic-wind
1866 (lambda () (autoload-in-progress! dir-hint name))
1867 (lambda ()
1868 (let ((full (%search-load-path (in-vicinity dir-hint name))))
1869 (if full
1870 (begin
1871 (save-module-excursion (lambda () (primitive-load full)))
1872 (set! didit #t)))))
1873 (lambda () (set-autoloaded! dir-hint name didit)))
1874 didit))))
1875
1876 \f
1877 ;;; Dynamic linking of modules
1878
1879 ;; Initializing a module that is written in C is a two step process.
1880 ;; First the module's `module init' function is called. This function
1881 ;; is expected to call `scm_register_module_xxx' to register the `real
1882 ;; init' function. Later, when the module is referenced for the first
1883 ;; time, this real init function is called in the right context. See
1884 ;; gtcltk-lib/gtcltk-module.c for an example.
1885 ;;
1886 ;; The code for the module can be in a regular shared library (so that
1887 ;; the `module init' function will be called when libguile is
1888 ;; initialized). Or it can be dynamically linked.
1889 ;;
1890 ;; You can safely call `scm_register_module_xxx' before libguile
1891 ;; itself is initialized. You could call it from an C++ constructor
1892 ;; of a static object, for example.
1893 ;;
1894 ;; To make your Guile extension into a dynamic linkable module, follow
1895 ;; these easy steps:
1896 ;;
1897 ;; - Find a name for your module, like (ice-9 gtcltk)
1898 ;; - Write a function with a name like
1899 ;;
1900 ;; scm_init_ice_9_gtcltk_module
1901 ;;
1902 ;; This is your `module init' function. It should call
1903 ;;
1904 ;; scm_register_module_xxx ("ice-9 gtcltk", scm_init_gtcltk);
1905 ;;
1906 ;; "ice-9 gtcltk" is the C version of the module name. Slashes are
1907 ;; replaced by spaces, the rest is untouched. `scm_init_gtcltk' is
1908 ;; the real init function that executes the usual initializations
1909 ;; like making new smobs, etc.
1910 ;;
1911 ;; - Make a shared library with your code and a name like
1912 ;;
1913 ;; ice-9/libgtcltk.so
1914 ;;
1915 ;; and put it somewhere in %load-path.
1916 ;;
1917 ;; - Then you can simply write `:use-module (ice-9 gtcltk)' and it
1918 ;; will be linked automatically.
1919 ;;
1920 ;; This is all very experimental.
1921
1922 (define (split-c-module-name str)
1923 (let loop ((rev '())
1924 (start 0)
1925 (pos 0)
1926 (end (string-length str)))
1927 (cond
1928 ((= pos end)
1929 (reverse (cons (string->symbol (substring str start pos)) rev)))
1930 ((eq? (string-ref str pos) #\space)
1931 (loop (cons (string->symbol (substring str start pos)) rev)
1932 (+ pos 1)
1933 (+ pos 1)
1934 end))
1935 (else
1936 (loop rev start (+ pos 1) end)))))
1937
1938 (define (convert-c-registered-modules dynobj)
1939 (let ((res (map (lambda (c)
1940 (list (split-c-module-name (car c)) (cdr c) dynobj))
1941 (c-registered-modules))))
1942 (c-clear-registered-modules)
1943 res))
1944
1945 (define registered-modules '())
1946
1947 (define (register-modules dynobj)
1948 (set! registered-modules
1949 (append! (convert-c-registered-modules dynobj)
1950 registered-modules)))
1951
1952 (define (init-dynamic-module modname)
1953 ;; Register any linked modules which has been registered on the C level
1954 (register-modules #f)
1955 (or-map (lambda (modinfo)
1956 (if (equal? (car modinfo) modname)
1957 (begin
1958 (set! registered-modules (delq! modinfo registered-modules))
1959 (let ((mod (resolve-module modname #f)))
1960 (save-module-excursion
1961 (lambda ()
1962 (set-current-module mod)
1963 (set-module-public-interface! mod mod)
1964 (dynamic-call (cadr modinfo) (caddr modinfo))
1965 ))
1966 #t))
1967 #f))
1968 registered-modules))
1969
1970 (define (dynamic-maybe-call name dynobj)
1971 (catch #t ; could use false-if-exception here
1972 (lambda ()
1973 (dynamic-call name dynobj))
1974 (lambda args
1975 #f)))
1976
1977 (define (dynamic-maybe-link filename)
1978 (catch #t ; could use false-if-exception here
1979 (lambda ()
1980 (dynamic-link filename))
1981 (lambda args
1982 #f)))
1983
1984 (define (find-and-link-dynamic-module module-name)
1985 (define (make-init-name mod-name)
1986 (string-append "scm_init"
1987 (list->string (map (lambda (c)
1988 (if (or (char-alphabetic? c)
1989 (char-numeric? c))
1990 c
1991 #\_))
1992 (string->list mod-name)))
1993 "_module"))
1994
1995 ;; Put the subdirectory for this module in the car of SUBDIR-AND-LIBNAME,
1996 ;; and the `libname' (the name of the module prepended by `lib') in the cdr
1997 ;; field. For example, if MODULE-NAME is the list (inet tcp-ip udp), then
1998 ;; SUBDIR-AND-LIBNAME will be the pair ("inet/tcp-ip" . "libudp").
1999 (let ((subdir-and-libname
2000 (let loop ((dirs "")
2001 (syms module-name))
2002 (if (null? (cdr syms))
2003 (cons dirs (string-append "lib" (car syms)))
2004 (loop (string-append dirs (car syms) "/") (cdr syms)))))
2005 (init (make-init-name (apply string-append
2006 (map (lambda (s)
2007 (string-append "_" s))
2008 module-name)))))
2009 (let ((subdir (car subdir-and-libname))
2010 (libname (cdr subdir-and-libname)))
2011
2012 ;; Now look in each dir in %LOAD-PATH for `subdir/libfoo.la'. If that
2013 ;; file exists, fetch the dlname from that file and attempt to link
2014 ;; against it. If `subdir/libfoo.la' does not exist, or does not seem
2015 ;; to name any shared library, look for `subdir/libfoo.so' instead and
2016 ;; link against that.
2017 (let check-dirs ((dir-list %load-path))
2018 (if (null? dir-list)
2019 #f
2020 (let* ((dir (in-vicinity (car dir-list) subdir))
2021 (sharlib-full
2022 (or (try-using-libtool-name dir libname)
2023 (try-using-sharlib-name dir libname))))
2024 (if (and sharlib-full (file-exists? sharlib-full))
2025 (link-dynamic-module sharlib-full init)
2026 (check-dirs (cdr dir-list)))))))))
2027
2028 (define (try-using-libtool-name libdir libname)
2029 (let ((libtool-filename (in-vicinity libdir
2030 (string-append libname ".la"))))
2031 (and (file-exists? libtool-filename)
2032 libtool-filename)))
2033
2034 (define (try-using-sharlib-name libdir libname)
2035 (in-vicinity libdir (string-append libname ".so")))
2036
2037 (define (link-dynamic-module filename initname)
2038 ;; Register any linked modules which has been registered on the C level
2039 (register-modules #f)
2040 (let ((dynobj (dynamic-link filename)))
2041 (dynamic-call initname dynobj)
2042 (register-modules dynobj)))
2043
2044 (define (try-module-linked module-name)
2045 (init-dynamic-module module-name))
2046
2047 (define (try-module-dynamic-link module-name)
2048 (and (find-and-link-dynamic-module module-name)
2049 (init-dynamic-module module-name)))
2050
2051
2052
2053 (define autoloads-done '((guile . guile)))
2054
2055 (define (autoload-done-or-in-progress? p m)
2056 (let ((n (cons p m)))
2057 (->bool (or (member n autoloads-done)
2058 (member n autoloads-in-progress)))))
2059
2060 (define (autoload-done! p m)
2061 (let ((n (cons p m)))
2062 (set! autoloads-in-progress
2063 (delete! n autoloads-in-progress))
2064 (or (member n autoloads-done)
2065 (set! autoloads-done (cons n autoloads-done)))))
2066
2067 (define (autoload-in-progress! p m)
2068 (let ((n (cons p m)))
2069 (set! autoloads-done
2070 (delete! n autoloads-done))
2071 (set! autoloads-in-progress (cons n autoloads-in-progress))))
2072
2073 (define (set-autoloaded! p m done?)
2074 (if done?
2075 (autoload-done! p m)
2076 (let ((n (cons p m)))
2077 (set! autoloads-done (delete! n autoloads-done))
2078 (set! autoloads-in-progress (delete! n autoloads-in-progress)))))
2079
2080
2081
2082
2083 \f
2084 ;;; {Macros}
2085 ;;;
2086
2087 (define (primitive-macro? m)
2088 (and (macro? m)
2089 (not (macro-transformer m))))
2090
2091 ;;; {Defmacros}
2092 ;;;
2093 (define macro-table (make-weak-key-hash-table 523))
2094 (define xformer-table (make-weak-key-hash-table 523))
2095
2096 (define (defmacro? m) (hashq-ref macro-table m))
2097 (define (assert-defmacro?! m) (hashq-set! macro-table m #t))
2098 (define (defmacro-transformer m) (hashq-ref xformer-table m))
2099 (define (set-defmacro-transformer! m t) (hashq-set! xformer-table m t))
2100
2101 (define defmacro:transformer
2102 (lambda (f)
2103 (let* ((xform (lambda (exp env)
2104 (copy-tree (apply f (cdr exp)))))
2105 (a (procedure->memoizing-macro xform)))
2106 (assert-defmacro?! a)
2107 (set-defmacro-transformer! a f)
2108 a)))
2109
2110
2111 (define defmacro
2112 (let ((defmacro-transformer
2113 (lambda (name parms . body)
2114 (let ((transformer `(lambda ,parms ,@body)))
2115 `(define ,name
2116 (,(lambda (transformer)
2117 (defmacro:transformer transformer))
2118 ,transformer))))))
2119 (defmacro:transformer defmacro-transformer)))
2120
2121 (define defmacro:syntax-transformer
2122 (lambda (f)
2123 (procedure->syntax
2124 (lambda (exp env)
2125 (copy-tree (apply f (cdr exp)))))))
2126
2127
2128 ;; XXX - should the definition of the car really be looked up in the
2129 ;; current module?
2130
2131 (define (macroexpand-1 e)
2132 (cond
2133 ((pair? e) (let* ((a (car e))
2134 (val (and (symbol? a) (local-ref (list a)))))
2135 (if (defmacro? val)
2136 (apply (defmacro-transformer val) (cdr e))
2137 e)))
2138 (#t e)))
2139
2140 (define (macroexpand e)
2141 (cond
2142 ((pair? e) (let* ((a (car e))
2143 (val (and (symbol? a) (local-ref (list a)))))
2144 (if (defmacro? val)
2145 (macroexpand (apply (defmacro-transformer val) (cdr e)))
2146 e)))
2147 (#t e)))
2148
2149 (define (gentemp)
2150 (gensym "scm:G"))
2151
2152 (provide 'defmacro)
2153
2154 \f
2155
2156 ;;; {Run-time options}
2157
2158 ((let* ((names '((eval-options-interface
2159 (eval-options eval-enable eval-disable)
2160 (eval-set!))
2161
2162 (debug-options-interface
2163 (debug-options debug-enable debug-disable)
2164 (debug-set!))
2165
2166 (evaluator-traps-interface
2167 (traps trap-enable trap-disable)
2168 (trap-set!))
2169
2170 (read-options-interface
2171 (read-options read-enable read-disable)
2172 (read-set!))
2173
2174 (print-options-interface
2175 (print-options print-enable print-disable)
2176 (print-set!))
2177
2178 (readline-options-interface
2179 (readline-options readline-enable readline-disable)
2180 (readline-set!))
2181 ))
2182 (option-name car)
2183 (option-value cadr)
2184 (option-documentation caddr)
2185
2186 (print-option (lambda (option)
2187 (display (option-name option))
2188 (if (< (string-length
2189 (symbol->string (option-name option)))
2190 8)
2191 (display #\tab))
2192 (display #\tab)
2193 (display (option-value option))
2194 (display #\tab)
2195 (display (option-documentation option))
2196 (newline)))
2197
2198 ;; Below follows the macros defining the run-time option interfaces.
2199
2200 (make-options (lambda (interface)
2201 `(lambda args
2202 (cond ((null? args) (,interface))
2203 ((list? (car args))
2204 (,interface (car args)) (,interface))
2205 (else (for-each ,print-option
2206 (,interface #t)))))))
2207
2208 (make-enable (lambda (interface)
2209 `(lambda flags
2210 (,interface (append flags (,interface)))
2211 (,interface))))
2212
2213 (make-disable (lambda (interface)
2214 `(lambda flags
2215 (let ((options (,interface)))
2216 (for-each (lambda (flag)
2217 (set! options (delq! flag options)))
2218 flags)
2219 (,interface options)
2220 (,interface)))))
2221
2222 (make-set! (lambda (interface)
2223 `((name exp)
2224 (,'quasiquote
2225 (begin (,interface (append (,interface)
2226 (list '(,'unquote name)
2227 (,'unquote exp))))
2228 (,interface))))))
2229 )
2230 (procedure->macro
2231 (lambda (exp env)
2232 (cons 'begin
2233 (apply append
2234 (map (lambda (group)
2235 (let ((interface (car group)))
2236 (append (map (lambda (name constructor)
2237 `(define ,name
2238 ,(constructor interface)))
2239 (cadr group)
2240 (list make-options
2241 make-enable
2242 make-disable))
2243 (map (lambda (name constructor)
2244 `(defmacro ,name
2245 ,@(constructor interface)))
2246 (caddr group)
2247 (list make-set!)))))
2248 names)))))))
2249
2250 \f
2251
2252 ;;; {Running Repls}
2253 ;;;
2254
2255 (define (repl read evaler print)
2256 (let loop ((source (read (current-input-port))))
2257 (print (evaler source))
2258 (loop (read (current-input-port)))))
2259
2260 ;; A provisional repl that acts like the SCM repl:
2261 ;;
2262 (define scm-repl-silent #f)
2263 (define (assert-repl-silence v) (set! scm-repl-silent v))
2264
2265 (define *unspecified* (if #f #f))
2266 (define (unspecified? v) (eq? v *unspecified*))
2267
2268 (define scm-repl-print-unspecified #f)
2269 (define (assert-repl-print-unspecified v) (set! scm-repl-print-unspecified v))
2270
2271 (define scm-repl-verbose #f)
2272 (define (assert-repl-verbosity v) (set! scm-repl-verbose v))
2273
2274 (define scm-repl-prompt "guile> ")
2275
2276 (define (set-repl-prompt! v) (set! scm-repl-prompt v))
2277
2278 (define (default-lazy-handler key . args)
2279 (save-stack lazy-handler-dispatch)
2280 (apply throw key args))
2281
2282 (define enter-frame-handler default-lazy-handler)
2283 (define apply-frame-handler default-lazy-handler)
2284 (define exit-frame-handler default-lazy-handler)
2285
2286 (define (lazy-handler-dispatch key . args)
2287 (case key
2288 ((apply-frame)
2289 (apply apply-frame-handler key args))
2290 ((exit-frame)
2291 (apply exit-frame-handler key args))
2292 ((enter-frame)
2293 (apply enter-frame-handler key args))
2294 (else
2295 (apply default-lazy-handler key args))))
2296
2297 (define abort-hook (make-hook))
2298
2299 ;; these definitions are used if running a script.
2300 ;; otherwise redefined in error-catching-loop.
2301 (define (set-batch-mode?! arg) #t)
2302 (define (batch-mode?) #t)
2303
2304 (define (error-catching-loop thunk)
2305 (let ((status #f)
2306 (interactive #t))
2307 (define (loop first)
2308 (let ((next
2309 (catch #t
2310
2311 (lambda ()
2312 (lazy-catch #t
2313 (lambda ()
2314 (dynamic-wind
2315 (lambda () (unmask-signals))
2316 (lambda ()
2317 (with-traps
2318 (lambda ()
2319 (first)
2320
2321 ;; This line is needed because mark
2322 ;; doesn't do closures quite right.
2323 ;; Unreferenced locals should be
2324 ;; collected.
2325 ;;
2326 (set! first #f)
2327 (let loop ((v (thunk)))
2328 (loop (thunk)))
2329 #f)))
2330 (lambda () (mask-signals))))
2331
2332 lazy-handler-dispatch))
2333
2334 (lambda (key . args)
2335 (case key
2336 ((quit)
2337 (set! status args)
2338 #f)
2339
2340 ((switch-repl)
2341 (apply throw 'switch-repl args))
2342
2343 ((abort)
2344 ;; This is one of the closures that require
2345 ;; (set! first #f) above
2346 ;;
2347 (lambda ()
2348 (run-hook abort-hook)
2349 (force-output (current-output-port))
2350 (display "ABORT: " (current-error-port))
2351 (write args (current-error-port))
2352 (newline (current-error-port))
2353 (if interactive
2354 (begin
2355 (if (and
2356 (not has-shown-debugger-hint?)
2357 (not (memq 'backtrace
2358 (debug-options-interface)))
2359 (stack? (fluid-ref the-last-stack)))
2360 (begin
2361 (newline (current-error-port))
2362 (display
2363 "Type \"(backtrace)\" to get more information or \"(debug)\" to enter the debugger.\n"
2364 (current-error-port))
2365 (set! has-shown-debugger-hint? #t)))
2366 (force-output (current-error-port)))
2367 (begin
2368 (primitive-exit 1)))
2369 (set! stack-saved? #f)))
2370
2371 (else
2372 ;; This is the other cons-leak closure...
2373 (lambda ()
2374 (cond ((= (length args) 4)
2375 (apply handle-system-error key args))
2376 (else
2377 (apply bad-throw key args))))))))))
2378 (if next (loop next) status)))
2379 (set! set-batch-mode?! (lambda (arg)
2380 (cond (arg
2381 (set! interactive #f)
2382 (restore-signals))
2383 (#t
2384 (error "sorry, not implemented")))))
2385 (set! batch-mode? (lambda () (not interactive)))
2386 (loop (lambda () #t))))
2387
2388 ;;(define the-last-stack (make-fluid)) Defined by scm_init_backtrace ()
2389 (define before-signal-stack (make-fluid))
2390 (define stack-saved? #f)
2391
2392 (define (save-stack . narrowing)
2393 (or stack-saved?
2394 (cond ((not (memq 'debug (debug-options-interface)))
2395 (fluid-set! the-last-stack #f)
2396 (set! stack-saved? #t))
2397 (else
2398 (fluid-set!
2399 the-last-stack
2400 (case (stack-id #t)
2401 ((repl-stack)
2402 (apply make-stack #t save-stack eval #t 0 narrowing))
2403 ((load-stack)
2404 (apply make-stack #t save-stack 0 #t 0 narrowing))
2405 ((tk-stack)
2406 (apply make-stack #t save-stack tk-stack-mark #t 0 narrowing))
2407 ((#t)
2408 (apply make-stack #t save-stack 0 1 narrowing))
2409 (else
2410 (let ((id (stack-id #t)))
2411 (and (procedure? id)
2412 (apply make-stack #t save-stack id #t 0 narrowing))))))
2413 (set! stack-saved? #t)))))
2414
2415 (define before-error-hook (make-hook))
2416 (define after-error-hook (make-hook))
2417 (define before-backtrace-hook (make-hook))
2418 (define after-backtrace-hook (make-hook))
2419
2420 (define has-shown-debugger-hint? #f)
2421
2422 (define (handle-system-error key . args)
2423 (let ((cep (current-error-port)))
2424 (cond ((not (stack? (fluid-ref the-last-stack))))
2425 ((memq 'backtrace (debug-options-interface))
2426 (run-hook before-backtrace-hook)
2427 (newline cep)
2428 (display "Backtrace:\n")
2429 (display-backtrace (fluid-ref the-last-stack) cep)
2430 (newline cep)
2431 (run-hook after-backtrace-hook)))
2432 (run-hook before-error-hook)
2433 (apply display-error (fluid-ref the-last-stack) cep args)
2434 (run-hook after-error-hook)
2435 (force-output cep)
2436 (throw 'abort key)))
2437
2438 (define (quit . args)
2439 (apply throw 'quit args))
2440
2441 (define exit quit)
2442
2443 ;;(define has-shown-backtrace-hint? #f) Defined by scm_init_backtrace ()
2444
2445 ;; Replaced by C code:
2446 ;;(define (backtrace)
2447 ;; (if (fluid-ref the-last-stack)
2448 ;; (begin
2449 ;; (newline)
2450 ;; (display-backtrace (fluid-ref the-last-stack) (current-output-port))
2451 ;; (newline)
2452 ;; (if (and (not has-shown-backtrace-hint?)
2453 ;; (not (memq 'backtrace (debug-options-interface))))
2454 ;; (begin
2455 ;; (display
2456 ;;"Type \"(debug-enable 'backtrace)\" if you would like a backtrace
2457 ;;automatically if an error occurs in the future.\n")
2458 ;; (set! has-shown-backtrace-hint? #t))))
2459 ;; (display "No backtrace available.\n")))
2460
2461 (define (error-catching-repl r e p)
2462 (error-catching-loop (lambda () (p (e (r))))))
2463
2464 (define (gc-run-time)
2465 (cdr (assq 'gc-time-taken (gc-stats))))
2466
2467 (define before-read-hook (make-hook))
2468 (define after-read-hook (make-hook))
2469
2470 ;;; The default repl-reader function. We may override this if we've
2471 ;;; the readline library.
2472 (define repl-reader
2473 (lambda (prompt)
2474 (display prompt)
2475 (force-output)
2476 (run-hook before-read-hook)
2477 (read (current-input-port))))
2478
2479 (define (scm-style-repl)
2480 (letrec (
2481 (start-gc-rt #f)
2482 (start-rt #f)
2483 (repl-report-start-timing (lambda ()
2484 (set! start-gc-rt (gc-run-time))
2485 (set! start-rt (get-internal-run-time))))
2486 (repl-report (lambda ()
2487 (display ";;; ")
2488 (display (inexact->exact
2489 (* 1000 (/ (- (get-internal-run-time) start-rt)
2490 internal-time-units-per-second))))
2491 (display " msec (")
2492 (display (inexact->exact
2493 (* 1000 (/ (- (gc-run-time) start-gc-rt)
2494 internal-time-units-per-second))))
2495 (display " msec in gc)\n")))
2496
2497 (consume-trailing-whitespace
2498 (lambda ()
2499 (let ((ch (peek-char)))
2500 (cond
2501 ((eof-object? ch))
2502 ((or (char=? ch #\space) (char=? ch #\tab))
2503 (read-char)
2504 (consume-trailing-whitespace))
2505 ((char=? ch #\newline)
2506 (read-char))))))
2507 (-read (lambda ()
2508 (let ((val
2509 (let ((prompt (cond ((string? scm-repl-prompt)
2510 scm-repl-prompt)
2511 ((thunk? scm-repl-prompt)
2512 (scm-repl-prompt))
2513 (scm-repl-prompt "> ")
2514 (else ""))))
2515 (repl-reader prompt))))
2516
2517 ;; As described in R4RS, the READ procedure updates the
2518 ;; port to point to the first character past the end of
2519 ;; the external representation of the object. This
2520 ;; means that it doesn't consume the newline typically
2521 ;; found after an expression. This means that, when
2522 ;; debugging Guile with GDB, GDB gets the newline, which
2523 ;; it often interprets as a "continue" command, making
2524 ;; breakpoints kind of useless. So, consume any
2525 ;; trailing newline here, as well as any whitespace
2526 ;; before it.
2527 ;; But not if EOF, for control-D.
2528 (if (not (eof-object? val))
2529 (consume-trailing-whitespace))
2530 (run-hook after-read-hook)
2531 (if (eof-object? val)
2532 (begin
2533 (repl-report-start-timing)
2534 (if scm-repl-verbose
2535 (begin
2536 (newline)
2537 (display ";;; EOF -- quitting")
2538 (newline)))
2539 (quit 0)))
2540 val)))
2541
2542 (-eval (lambda (sourc)
2543 (repl-report-start-timing)
2544 (start-stack 'repl-stack (eval sourc))))
2545
2546 (-print (lambda (result)
2547 (if (not scm-repl-silent)
2548 (begin
2549 (if (or scm-repl-print-unspecified
2550 (not (unspecified? result)))
2551 (begin
2552 (write result)
2553 (newline)))
2554 (if scm-repl-verbose
2555 (repl-report))
2556 (force-output)))))
2557
2558 (-quit (lambda (args)
2559 (if scm-repl-verbose
2560 (begin
2561 (display ";;; QUIT executed, repl exitting")
2562 (newline)
2563 (repl-report)))
2564 args))
2565
2566 (-abort (lambda ()
2567 (if scm-repl-verbose
2568 (begin
2569 (display ";;; ABORT executed.")
2570 (newline)
2571 (repl-report)))
2572 (repl -read -eval -print))))
2573
2574 (let ((status (error-catching-repl -read
2575 -eval
2576 -print)))
2577 (-quit status))))
2578
2579
2580 \f
2581 ;;; {IOTA functions: generating lists of numbers}
2582
2583 (define (iota n)
2584 (let loop ((count (1- n)) (result '()))
2585 (if (< count 0) result
2586 (loop (1- count) (cons count result)))))
2587
2588 \f
2589 ;;; {While}
2590 ;;;
2591 ;;; with `continue' and `break'.
2592 ;;;
2593
2594 (defmacro while (cond . body)
2595 `(letrec ((continue (lambda () (or (not ,cond) (begin (begin ,@ body) (continue)))))
2596 (break (lambda val (apply throw 'break val))))
2597 (catch 'break
2598 (lambda () (continue))
2599 (lambda v (cadr v)))))
2600
2601 ;;; {collect}
2602 ;;;
2603 ;;; Similar to `begin' but returns a list of the results of all constituent
2604 ;;; forms instead of the result of the last form.
2605 ;;; (The definition relies on the current left-to-right
2606 ;;; order of evaluation of operands in applications.)
2607
2608 (defmacro collect forms
2609 (cons 'list forms))
2610
2611 ;;; {with-fluids}
2612
2613 ;; with-fluids is a convenience wrapper for the builtin procedure
2614 ;; `with-fluids*'. The syntax is just like `let':
2615 ;;
2616 ;; (with-fluids ((fluid val)
2617 ;; ...)
2618 ;; body)
2619
2620 (defmacro with-fluids (bindings . body)
2621 `(with-fluids* (list ,@(map car bindings)) (list ,@(map cadr bindings))
2622 (lambda () ,@body)))
2623
2624 ;;; Environments
2625
2626 (define the-environment
2627 (procedure->syntax
2628 (lambda (x e)
2629 e)))
2630
2631 (define (environment-module env)
2632 (let ((closure (and (pair? env) (car (last-pair env)))))
2633 (and closure (procedure-property closure 'module))))
2634
2635 \f
2636
2637 ;;; {Macros}
2638 ;;;
2639
2640 ;; actually....hobbit might be able to hack these with a little
2641 ;; coaxing
2642 ;;
2643
2644 (defmacro define-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:transformer ,transformer))))
2651
2652
2653 (defmacro define-syntax-macro (first . rest)
2654 (let ((name (if (symbol? first) first (car first)))
2655 (transformer
2656 (if (symbol? first)
2657 (car rest)
2658 `(lambda ,(cdr first) ,@rest))))
2659 `(define ,name (defmacro:syntax-transformer ,transformer))))
2660 \f
2661 ;;; {Module System Macros}
2662 ;;;
2663
2664 (defmacro define-module args
2665 `(let* ((process-define-module process-define-module)
2666 (set-current-module set-current-module)
2667 (module (process-define-module ',args)))
2668 (set-current-module module)
2669 module))
2670
2671 ;; the guts of the use-modules macro. add the interfaces of the named
2672 ;; modules to the use-list of the current module, in order
2673 (define (process-use-modules module-names)
2674 (for-each (lambda (module-name)
2675 (let ((mod-iface (resolve-interface module-name)))
2676 (or mod-iface
2677 (error "no such module" module-name))
2678 (module-use! (current-module) mod-iface)))
2679 (reverse module-names)))
2680
2681 (defmacro use-modules modules
2682 `(process-use-modules ',modules))
2683
2684 (defmacro use-syntax (spec)
2685 `(begin
2686 ,@(if (pair? spec)
2687 `((process-use-modules ',(list spec))
2688 (set-module-transformer! (current-module)
2689 ,(car (last-pair spec))))
2690 `((set-module-transformer! (current-module) ,spec)))
2691 (set! scm:eval-transformer (module-transformer (current-module)))))
2692
2693 (define define-private define)
2694
2695 (defmacro define-public args
2696 (define (syntax)
2697 (error "bad syntax" (list 'define-public args)))
2698 (define (defined-name n)
2699 (cond
2700 ((symbol? n) n)
2701 ((pair? n) (defined-name (car n)))
2702 (else (syntax))))
2703 (cond
2704 ((null? args) (syntax))
2705
2706 (#t (let ((name (defined-name (car args))))
2707 `(begin
2708 (let ((public-i (module-public-interface (current-module))))
2709 ;; Make sure there is a local variable:
2710 ;;
2711 (module-define! (current-module)
2712 ',name
2713 (module-ref (current-module) ',name #f))
2714
2715 ;; Make sure that local is exported:
2716 ;;
2717 (module-add! public-i ',name
2718 (module-variable (current-module) ',name)))
2719
2720 ;; Now (re)define the var normally. Bernard URBAN
2721 ;; suggests we use eval here to accomodate Hobbit; it lets
2722 ;; the interpreter handle the define-private form, which
2723 ;; Hobbit can't digest.
2724 (eval '(define-private ,@ args)))))))
2725
2726
2727
2728 (defmacro defmacro-public args
2729 (define (syntax)
2730 (error "bad syntax" (list 'defmacro-public args)))
2731 (define (defined-name n)
2732 (cond
2733 ((symbol? n) n)
2734 (else (syntax))))
2735 (cond
2736 ((null? args) (syntax))
2737
2738 (#t (let ((name (defined-name (car args))))
2739 `(begin
2740 (let ((public-i (module-public-interface (current-module))))
2741 ;; Make sure there is a local variable:
2742 ;;
2743 (module-define! (current-module)
2744 ',name
2745 (module-ref (current-module) ',name #f))
2746
2747 ;; Make sure that local is exported:
2748 ;;
2749 (module-add! public-i ',name (module-variable (current-module) ',name)))
2750
2751 ;; Now (re)define the var normally.
2752 ;;
2753 (defmacro ,@ args))))))
2754
2755
2756 (define (module-export! m names)
2757 (let ((public-i (module-public-interface m)))
2758 (for-each (lambda (name)
2759 ;; Make sure there is a local variable:
2760 (module-define! m name (module-ref m name #f))
2761 ;; Make sure that local is exported:
2762 (module-add! public-i name (module-variable m name)))
2763 names)))
2764
2765 (defmacro export names
2766 `(module-export! (current-module) ',names))
2767
2768 (define export-syntax export)
2769
2770
2771 (define load load-module)
2772
2773
2774 \f
2775 ;;; {Load emacs interface support if emacs option is given.}
2776
2777 (define (load-emacs-interface)
2778 (if (memq 'debug-extensions *features*)
2779 (debug-enable 'backtrace))
2780 (define-module (guile-user) :use-module (ice-9 emacs)))
2781
2782 \f
2783
2784 (define using-readline?
2785 (let ((using-readline? (make-fluid)))
2786 (make-procedure-with-setter
2787 (lambda () (fluid-ref using-readline?))
2788 (lambda (v) (fluid-set! using-readline? v)))))
2789
2790 ;; this is just (scm-style-repl) with a wrapper to install and remove
2791 ;; signal handlers.
2792 (define (top-repl)
2793
2794 ;; Load emacs interface support if emacs option is given.
2795 (if (and (module-defined? the-root-module 'use-emacs-interface)
2796 use-emacs-interface)
2797 (load-emacs-interface))
2798
2799 ;; Place the user in the guile-user module.
2800 (define-module (guile-user)
2801 :use-module (guile) ;so that bindings will be checked here first
2802 :use-module (ice-9 session)
2803 :use-module (ice-9 debug)
2804 :autoload (ice-9 debugger) (debug)) ;load debugger on demand
2805 (if (memq 'threads *features*)
2806 (define-module (guile-user) :use-module (ice-9 threads)))
2807 (if (memq 'regex *features*)
2808 (define-module (guile-user) :use-module (ice-9 regex)))
2809
2810 (let ((old-handlers #f)
2811 (signals (if (provided? 'posix)
2812 `((,SIGINT . "User interrupt")
2813 (,SIGFPE . "Arithmetic error")
2814 (,SIGBUS . "Bad memory access (bus error)")
2815 (,SIGSEGV .
2816 "Bad memory access (Segmentation violation)"))
2817 '())))
2818
2819 (dynamic-wind
2820
2821 ;; call at entry
2822 (lambda ()
2823 (let ((make-handler (lambda (msg)
2824 (lambda (sig)
2825 ;; Make a backup copy of the stack
2826 (fluid-set! before-signal-stack
2827 (fluid-ref the-last-stack))
2828 (save-stack %deliver-signals)
2829 (scm-error 'signal
2830 #f
2831 msg
2832 #f
2833 (list sig))))))
2834 (set! old-handlers
2835 (map (lambda (sig-msg)
2836 (sigaction (car sig-msg)
2837 (make-handler (cdr sig-msg))))
2838 signals))))
2839
2840 ;; the protected thunk.
2841 (lambda ()
2842 (let ((status (scm-style-repl)))
2843 (run-hook exit-hook)
2844 status))
2845
2846 ;; call at exit.
2847 (lambda ()
2848 (map (lambda (sig-msg old-handler)
2849 (if (not (car old-handler))
2850 ;; restore original C handler.
2851 (sigaction (car sig-msg) #f)
2852 ;; restore Scheme handler, SIG_IGN or SIG_DFL.
2853 (sigaction (car sig-msg)
2854 (car old-handler)
2855 (cdr old-handler))))
2856 signals old-handlers)))))
2857
2858 (defmacro false-if-exception (expr)
2859 `(catch #t (lambda () ,expr)
2860 (lambda args #f)))
2861
2862 ;;; This hook is run at the very end of an interactive session.
2863 ;;;
2864 (define exit-hook (make-hook))
2865
2866 \f
2867 (define-module (guile))
2868
2869 (append! %load-path (cons "." ()))