Debian Codegraph @

%3a37/srfi-37-reference.scm

raw symbols SCIP
1 ;;; args-fold.scm - a program argument processor
2 ;;;
3 ;;; Copyright (c) 2002 Anthony Carrico
4 ;;;
5 ;;; All rights reserved.
6 ;;;
7 ;;; Redistribution and use in source and binary forms, with or without
8 ;;; modification, are permitted provided that the following conditions
9 ;;; are met:
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;; 2. Redistributions in binary form must reproduce the above copyright
13 ;;; notice, this list of conditions and the following disclaimer in the
14 ;;; documentation and/or other materials provided with the distribution.
15 ;;; 3. The name of the authors may not be used to endorse or promote products
16 ;;; derived from this software without specific prior written permission.
17 ;;;
18 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
19 ;;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 ;;; OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 ;;; IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 ;;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 ;;; NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 ;;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 ;;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 ;;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 ;;; THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 ;;; NOTE: This implementation uses the following SRFIs:
30 ;;; "SRFI 9: Defining Record Types"
31 ;;; "SRFI 11: Syntax for receiving multiple values"
32 ;;;
33 ;;; NOTE: The scsh-utils and Chicken implementations use regular
34 ;;; expressions. These might be easier to read and understand.
35
36 #|
37 (define option #f)
38 (define option-names #f)
39 (define option-required-arg? #f)
40 (define option-optional-arg? #f)
41 (define option-processor #f)
42 (define option? #f)
43
44 (let ()
45 (define-record-type option-type
46 ($option names required-arg? optional-arg? processor)
47 $option?
48 (names $option-names)
49 (required-arg? $option-required-arg?)
50 (optional-arg? $option-optional-arg?)
51 (processor $option-processor))
52 (set! option $option)
53 (set! option-names $option-names)
54 (set! option-required-arg? $option-required-arg?)
55 (set! option-optional-arg? $option-optional-arg?)
56 (set! option-processor $option-processor)
57 (set! option? $option?))
58 |#
59
60 (define args-fold
61 (lambda (args
62 options
63 unrecognized-option-proc
64 operand-proc
65 . seeds)
66 (letrec
67 ((find
68 (lambda (l ?)
69 (cond ((null? l) #f)
70 ((? (car l)) (car l))
71 (else (find (cdr l) ?)))))
72 (find-option
73 ;; ISSUE: This is a brute force search. Could use a table.
74 (lambda (name)
75 (find
76 options
77 (lambda (option)
78 (find
79 (option-names option)
80 (lambda (test-name)
81 (equal? name test-name)))))))
82 (scan-short-options
83 (lambda (index shorts args seeds)
84 (if (= index (string-length shorts))
85 (scan-args args seeds)
86 (let* ((name (string-ref shorts index))
87 (option (or (find-option name)
88 (option (list name)
89 #f
90 #f
91 unrecognized-option-proc))))
92 (cond ((and (< (+ index 1) (string-length shorts))
93 (or (option-required-arg? option)
94 (option-optional-arg? option)))
95 (let-values
96 ((seeds (apply (option-processor option)
97 option
98 name
99 (substring
100 shorts
101 (+ index 1)
102 (string-length shorts))
103 seeds)))
104 (scan-args args seeds)))
105 ((and (option-required-arg? option)
106 (pair? args))
107 (let-values
108 ((seeds (apply (option-processor option)
109 option
110 name
111 (car args)
112 seeds)))
113 (scan-args (cdr args) seeds)))
114 (else
115 (let-values
116 ((seeds (apply (option-processor option)
117 option
118 name
119 #f
120 seeds)))
121 (scan-short-options
122 (+ index 1)
123 shorts
124 args
125 seeds))))))))
126 (scan-operands
127 (lambda (operands seeds)
128 (if (null? operands)
129 (apply values seeds)
130 (let-values ((seeds (apply operand-proc
131 (car operands)
132 seeds)))
133 (scan-operands (cdr operands) seeds)))))
134 (scan-args
135 (lambda (args seeds)
136 (if (null? args)
137 (apply values seeds)
138 (let ((arg (car args))
139 (args (cdr args)))
140 ;; NOTE: This string matching code would be simpler
141 ;; using a regular expression matcher.
142 (cond
143 (;; (rx bos "--" eos)
144 (string=? "--" arg)
145 ;; End option scanning:
146 (scan-operands args seeds))
147 (;;(rx bos
148 ;; "--"
149 ;; (submatch (+ (~ "=")))
150 ;; "="
151 ;; (submatch (* any)))
152 (and (> (string-length arg) 4)
153 (char=? #\- (string-ref arg 0))
154 (char=? #\- (string-ref arg 1))
155 (not (char=? #\= (string-ref arg 2)))
156 (let loop ((index 3))
157 (cond ((= index (string-length arg))
158 #f)
159 ((char=? #\= (string-ref arg index))
160 index)
161 (else
162 (loop (+ 1 index))))))
163 ;; Found long option with arg:
164 => (lambda (=-index)
165 (let*-values
166 (((name)
167 (substring arg 2 =-index))
168 ((option-arg)
169 (substring arg
170 (+ =-index 1)
171 (string-length arg)))
172 ((option)
173 (or (find-option name)
174 (option (list name)
175 #t
176 #f
177 unrecognized-option-proc)))
178 (seeds
179 (apply (option-processor option)
180 option
181 name
182 option-arg
183 seeds)))
184 (scan-args args seeds))))
185 (;;(rx bos "--" (submatch (+ any)))
186 (and (> (string-length arg) 3)
187 (char=? #\- (string-ref arg 0))
188 (char=? #\- (string-ref arg 1)))
189 ;; Found long option:
190 (let* ((name (substring arg 2 (string-length arg)))
191 (option (or (find-option name)
192 (option
193 (list name)
194 #f
195 #f
196 unrecognized-option-proc))))
197 (if (and (option-required-arg? option)
198 (pair? args))
199 (let-values
200 ((seeds (apply (option-processor option)
201 option
202 name
203 (car args)
204 seeds)))
205 (scan-args (cdr args) seeds))
206 (let-values
207 ((seeds (apply (option-processor option)
208 option
209 name
210 #f
211 seeds)))
212 (scan-args args seeds)))))
213 (;; (rx bos "-" (submatch (+ any)))
214 (and (> (string-length arg) 1)
215 (char=? #\- (string-ref arg 0)))
216 ;; Found short options
217 (let ((shorts (substring arg 1 (string-length arg))))
218 (scan-short-options 0 shorts args seeds)))
219 (else
220 (let-values ((seeds (apply operand-proc arg seeds)))
221 (scan-args args seeds)))))))))
222 (scan-args args seeds))))