commander-s/scheme/config.scm

54 lines
1.4 KiB
Scheme

(define *configuration* '())
(define (init-config config-file)
(with-input-file
config-file
(set! *configuration* (read))))
(define (read-config config-file)
(call-with-input-file config-file read))
(define (config option)
(let ((probe (assq option *configuration*)))
(define (config-file-to-use)
(let ((from-env (getenv "CMDRSRC"))
(default-location
(string-append (home-dir) "/.cmdrsrc")))
(if (and from-env (file-exists? from-env))
from-env
default-location)))
(define (read-config-file!)
(with-errno-handler
((errno data)
(else #f))
(let ((conf-file (config-file-to-use)))
(for-each
(lambda (option.value)
(cond
((and (pair? option.value)
(pair? (car option.value))
(symbol? (caar option.value))
(symbol? (cdar option.value))
(assoc (car option.value) *configuration*))
=> (lambda (p)
(set-cdr! p (cdr option.value))))
(else
(error "Unknown option value or ill-formed option"
option.value conf-file))))
(read-config conf-file)))))
(define (config module option)
(let ((probe (assoc (cons module option) *configuration*)))
(if probe
(cdr probe)
(error "unknown configuration option" option))))
(define (define-option module option-name default-value)
(set! *configuration*
(cons (cons (cons module option-name) default-value)
*configuration*)))