aboutsummaryrefslogtreecommitdiff
path: root/module
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2020-10-13 16:40:51 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2020-10-13 16:40:51 +0200
commitc6ecb5325a8afdbb39b0bc90e85fdec04c61330d (patch)
treebbd89b20bb10ef5da278d7615fc283169d3a14c0 /module
parentHTML fix date input. (diff)
downloadcalp-c6ecb5325a8afdbb39b0bc90e85fdec04c61330d.tar.gz
calp-c6ecb5325a8afdbb39b0bc90e85fdec04c61330d.tar.xz
Config loading now in 'sandbox'.
Diffstat (limited to 'module')
-rw-r--r--module/calp/main.scm64
1 files changed, 52 insertions, 12 deletions
diff --git a/module/calp/main.scm b/module/calp/main.scm
index 407f7b81..2eb1ee05 100644
--- a/module/calp/main.scm
+++ b/module/calp/main.scm
@@ -15,6 +15,8 @@
:use-module (ice-9 getopt-long)
:use-module (ice-9 regex)
:use-module ((ice-9 popen) :select (open-input-pipe))
+ :use-module ((ice-9 sandbox) :select
+ (make-sandbox-module all-pure-and-impure-bindings))
:use-module (statprof)
:use-module (calp repl)
@@ -99,6 +101,11 @@
(if (null? a)
b a))
+(define (bindings-for module-name)
+ ;; Wrapping list so we can later export sub-modules.
+ (list (cons module-name
+ (module-map (lambda (a . _) a)
+ (resolve-interface module-name)))))
(define (wrapped-main args)
(define opts (getopt-long args (getopt-opt options) #:stop-at-first-non-option #t))
@@ -106,6 +113,20 @@
(define repl (option-ref opts 'repl #f))
(define altconfig (option-ref opts 'config #f))
+ (define config-file
+ (cond [altconfig
+ (if (file-exists? altconfig)
+ altconfig
+ (throw 'option-error
+ "Configuration file ~a missing" altconfig))]
+ ;; altconfig could be placed in the list below. But I want to raise an error
+ ;; if an explicitly given config is missing.
+ [(find file-exists?
+ (list
+ (path-append (xdg-config-home) "/calp/config.scm")
+ (path-append (xdg-sysconfdir) "/calp/config.scm")))
+ => identity]))
+
(when stprof (statprof-start))
(cond [(eqv? #t repl) (repl-start (format #f "~a/calp-~a"
@@ -113,18 +134,37 @@
(getpid)))]
[repl => repl-start])
- (if altconfig
- (begin
- (if (file-exists? altconfig)
- (primitive-load altconfig)
- (throw 'option-error "Configuration file ~a missing" altconfig)))
- ;; if not altconfig, then regular config
-
- (awhen (find file-exists?
- (list
- (path-append (xdg-config-home) "/calp/config.scm")
- (path-append (xdg-sysconfdir) "/calp/config.scm")))
- (primitive-load it)))
+
+ ;; load config
+ (catch #t
+ (lambda ()
+ (eval
+ `(begin
+ (use-modules (srfi srfi-1)
+ (srfi srfi-88)
+ (datetime)
+ (vcomponent)
+ (calp util config)
+ (glob))
+ ,@(with-input-from-file config-file
+ (lambda ()
+ (let loop ((done '()))
+ (let ((form (read)))
+ (if (eof-object? form)
+ (reverse done)
+ (loop (cons form done))))))))
+ (make-sandbox-module
+ `(((guile) use-modules)
+ ,@all-pure-and-impure-bindings
+ ))
+ ))
+ (lambda args
+ (format (current-error-port)
+ "Failed loading config file ~a~%~s~%"
+ config-file
+ args
+ )))
+
;; NOTE this doesn't stop at first non-option, meaning that -o flags