aboutsummaryrefslogtreecommitdiff
path: root/module/hnh/util/env.scm
blob: 32ea1cc1dedd26305cc6dae460aa0e3f42ca21af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
(define-module (hnh util env)
  :export (let-env
           with-working-directory
           with-locale1))

(define-syntax let-env
  (syntax-rules ()
    [(_ ((name value) ...)
        body ...)

     (let ((env-pairs #f))
       (dynamic-wind
         (lambda ()
           (set! env-pairs
             (map (lambda (n new-value)
                    (list n new-value (getenv n)))
                  (list (symbol->string (quote name)) ...)
                  (list value ...)))
           (for-each (lambda (pair) (setenv (car pair) (cadr pair)))
                     env-pairs))
         (lambda () body ...)
         (lambda ()
           (for-each (lambda (pair) (setenv (car pair) (caddr pair)))
                     env-pairs))))]))


;; TODO this probably isn't threadsafe... pthreads(7) notes
;; that chdir is shared between all threads.
(define-syntax-rule (with-working-directory directory thunk)
  (let ((old-cwd #f))
   (dynamic-wind
     (lambda ()
       (set! old-cwd (getcwd))
       (chdir directory))
     thunk
     (lambda () (chdir old-cwd)))))


(define-syntax-rule (with-locale1 category locale thunk)
  (let ((old #f))
    (dynamic-wind
      (lambda ()
        (set! old (setlocale category))
        (setlocale category locale))
      thunk
      (lambda () (setlocale category old)))))