home *** CD-ROM | disk | FTP | other *** search
/ HAKERIS 11 / HAKERIS 11.ISO / linux / system / LinuxConsole 0.4 / linuxconsole0.4install-en.iso / guile0.4.lcm / share / guile / 1.6.0 / ice-9 / boot-9.scm < prev    next >
Encoding:
Text File  |  2004-01-06  |  89.3 KB  |  3,099 lines

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