-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibrary-files.s6l
executable file
·141 lines (118 loc) · 4.81 KB
/
library-files.s6l
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!r6rs
;;; SPDX-FileCopyrightText: 2010 Derick Eddington
;;;
;;; SPDX-License-Identifier: MIT
;; Refer to the SRFI document at:
;; http://srfi.schemers.org/srfi-103/srfi-103.html
;; The "CUSTOMIZE" comments below indicate things which can or should be changed
;; for particular Scheme systems or platforms.
(library (srfi :103 library-files)
(export
library-name->file-name
searched-directories
file-name-extensions
fold-find-library-file-names
;; Needed by the test program.
#;test:file-exists?)
(import
(rename (rnrs base (6))
(assertion-violation AV))
(only (rnrs control (6))
unless)
(rename (rnrs lists (6))
(for-all andmap))
(prefix (only (rnrs files (6))
file-exists?)
rnrs:)
(only (srfi :39 parameters)
make-parameter)
(only (srfi :98 os-environment-variables)
get-environment-variable))
;;----------------------------------------------------------------------------
;; Uncomment for the test program.
#;(define test:file-exists? (make-parameter #F))
(define (file-exists? x)
;; Comment-out for the test program.
(rnrs:file-exists? x)
;; Uncomment for the test program.
#;((test:file-exists?) x))
;;----------------------------------------------------------------------------
(define (file-name-join . fns)
(define (string-intersperse sl c)
(define (intersperse l x)
(let loop ((l l) (a '()))
(if (null? l)
(if (null? a) '() (reverse (cdr a)))
(loop (cdr l) (cons* x (car l) a)))))
(apply string-append (intersperse sl (string c))))
(string-intersperse (filter (lambda (x) (positive? (string-length x))) fns)
file-name-component-separator))
(define (from-env-var ev)
(define (string-split s c)
(if (zero? (string-length s))
'()
(let loop ((l (reverse (string->list s))) (g '()) (a '()))
(if (null? l)
(cons (list->string g) a)
(if (char=? (car l) c)
(loop (cdr l) '() (cons (list->string g) a))
(loop (cdr l) (cons (car l) g) a))))))
(let ((x (get-environment-variable ev)))
(and x (string-split x env-var-element-separator))))
(define (library-name? x)
(define (non-empty-list? x) (and (list? x) (pair? x)))
(and (non-empty-list? x) (andmap symbol? x)))
;;----------------------------------------------------------------------------
;; CUSTOMIZE: file-name-component-separator and env-var-element-separator
;; should be changed for Windows.
(define file-name-component-separator #\/)
(define env-var-element-separator #\:)
(define-syntax define-parameter
(syntax-rules ()
((_ name env-var alt-init)
(define name
(make-parameter (or (from-env-var env-var)
alt-init)
(lambda (x)
(if (and (list? x) (andmap string? x))
x
(AV 'name "not a list of strings" x))))))))
;; CUSTOMIZE: searched-directories can be initialized to include additional
;; elements.
(define-parameter searched-directories
"SCHEME_LIB_PATH"
'())
;; CUSTOMIZE: file-name-extensions can be initialized to include additional
;; elements, and ".acme-s6l" should have the Scheme system's name substituted
;; for "acme".
(define-parameter file-name-extensions
"SCHEME_LIB_EXTENSIONS"
'(".acme-s6l" ".s6l"))
;;----------------------------------------------------------------------------
(define (library-name->file-name lib-name extension)
(define (check x ? s)
(unless (? x)
(AV 'library-name->file-name (string-append "not a " s) x)))
(check lib-name library-name? "library name")
(check extension string? "string")
(string-append (apply file-name-join (map symbol->string lib-name))
extension))
(define (fold-find-library-file-names lib-name proc . seeds)
(unless (library-name? lib-name)
(AV 'fold-find-library-file-names "not a library name" lib-name))
(let ((fn (apply file-name-join (map symbol->string lib-name))))
(let loop-s ((sds (searched-directories)) (seeds seeds))
(if (pair? sds)
(let ((sd (car sds)))
(let loop-e ((exts (file-name-extensions)) (seeds seeds))
(if (pair? exts)
(let ((fn (string-append fn (car exts))))
(if (file-exists? (file-name-join sd fn))
(let-values (((cont . seeds) (apply proc sd fn seeds)))
(if cont
(loop-e (cdr exts) seeds)
(apply values seeds)))
(loop-e (cdr exts) seeds)))
(loop-s (cdr sds) seeds))))
(apply values seeds)))))
)