home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / runtime / option.scm < prev    next >
Text File  |  2001-03-16  |  5KB  |  153 lines

  1. #| -*-Scheme-*-
  2.  
  3. $Id: option.scm,v 14.37 2001/03/16 20:17:48 cph Exp $
  4.  
  5. Copyright (c) 1988-2001 Massachusetts Institute of Technology
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. 02111-1307, USA.
  21. |#
  22.  
  23. ;;;; Option Loader
  24. ;;; package: (runtime options)
  25.  
  26. (declare (usual-integrations))
  27.  
  28. (define (load-option name #!optional no-error?)
  29.   (let ((no-error? (and (not (default-object? no-error?)) no-error?)))
  30.  
  31.     (define (find-option)
  32.       (cond ((assq name *options*) => load-entry)
  33.         ((force* *parent*) => search-parent)
  34.         ((not no-error?) (error "Unknown option name:" name))
  35.         (else #f)))
  36.  
  37.     (define (load-entry entry)
  38.       (for-each (lambda (thunk) (thunk)) (cdr entry))
  39.       (set! loaded-options (cons name loaded-options))
  40.       name)
  41.  
  42.     (define (search-parent file)
  43.       (fluid-let ((*options* '())
  44.           (*parent* #f))
  45.     (fluid-let ((load/suppress-loading-message? #t))
  46.       (load-latest (merge-pathnames file (library-directory-pathname ""))
  47.                (make-load-environment)
  48.                system-global-syntax-table
  49.                #f))
  50.     (find-option)))
  51.  
  52.     (define (make-load-environment)
  53.       (eval '(LET () (THE-ENVIRONMENT)) system-global-environment))
  54.  
  55.     (fluid-let ((*parser-canonicalize-symbols?* #t))
  56.       (if (memq name loaded-options)
  57.       name
  58.       (find-option)))))
  59.  
  60. (define (define-load-option name . loaders)
  61.   (set! *options* (cons (cons name loaders) *options*))
  62.   unspecific)
  63.  
  64. (define (further-load-options place)
  65.   (set! *parent* place)
  66.   unspecific)
  67.  
  68. (define (initial-load-options)
  69.   (or *initial-options-file*
  70.       (get-environment-variable "MITSCHEME_LOAD_OPTIONS")
  71.       (local-load-options)))
  72.  
  73. (define (local-load-options)
  74.   (or (library-file? "optiondb")
  75.       (standard-load-options)))
  76.  
  77. (define (standard-load-options)
  78.   (or (library-file? "options/optiondb")
  79.       (error "Cannot locate a load-option database")
  80.       "optiondb"))
  81.  
  82. (define (library-file? library-internal-path)
  83.   (let* ((library (library-directory-pathname ""))
  84.      (pathname (merge-pathnames library-internal-path library)))
  85.     (let loop ((file-types load/default-types))
  86.       (and (not (null? file-types))
  87.        (let ((full-pathname
  88.           (pathname-new-type pathname (caar file-types))))
  89.          (if (file-exists? full-pathname)
  90.          ;; not full-pathname to allow load-latest
  91.          pathname        
  92.          (loop (cdr file-types))))))))
  93.  
  94. (define loaded-options '())
  95. (define *options* '())            ; Current options.
  96. (define *parent* initial-load-options)    ; A thunk or a pathname/string or #f.
  97. (define *initial-options-file* #f)
  98.  
  99. (define (standard-option-loader package-name init-expression . files)
  100.   (lambda ()
  101.     (let ((environment (package/environment (find-package package-name)))
  102.       (runtime (pathname-as-directory "runtime")))
  103.       (for-each (lambda (file)
  104.           (let ((file (force* file)))
  105.             (cond 
  106.              (((ucode-primitive initialize-c-compiled-block 1)
  107.                (string-append "runtime_" file))
  108.               => (lambda (obj)
  109.                (purify obj)
  110.                (scode-eval obj environment)))
  111.              (else
  112.               (let* ((options (library-directory-pathname "options"))
  113.                  (pathname (merge-pathnames file options)))
  114.             (with-directory-rewriting-rule options runtime
  115.               (lambda ()
  116.                 (with-working-directory-pathname
  117.                 (directory-pathname pathname)
  118.                   (lambda ()
  119.                 (load pathname
  120.                       environment
  121.                       syntax-table/system-internal
  122.                       #t))))))))))
  123.         files)
  124.       (flush-purification-queue!)
  125.       (eval init-expression environment))))
  126.  
  127. (define (declare-shared-library shared-library thunk)
  128.   (let ((thunk-valid?
  129.      (lambda (thunk)
  130.        (not (condition? (ignore-errors thunk))))))
  131.     (add-event-receiver!
  132.      event:after-restore
  133.      (lambda ()
  134.        (if (not (thunk-valid? thunk))
  135.        (fluid-let ((load/suppress-loading-message? #t))
  136.          (load
  137.           (merge-pathnames shared-library
  138.                    (library-directory-pathname "shared")))))))))
  139.  
  140. (define (force* value)
  141.   (cond    ((procedure? value) (force* (value)))
  142.     ((promise? value) (force* (force value)))
  143.     (else value)))
  144.  
  145. (define (library-directory-pathname name)
  146.   (or (system-library-directory-pathname name)
  147.       (library-directory-pathname
  148.        (error:file-operation name
  149.                  "find"
  150.                  "directory"
  151.                  "no such directory in system library path"
  152.                  library-directory-pathname
  153.                  (list name)))))