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