srfi-37.scm 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. ;;; NOTE: This implementation uses the following SRFIs:
  29. ;;; "SRFI 9: Defining Record Types"
  30. ;;; "SRFI 11: Syntax for receiving multiple values"
  31. ;;;
  32. ;;; NOTE: The scsh-utils and Chicken implementations use regular
  33. ;;; expressions. These might be easier to read and understand.
  34. (define-record-type option-type :option
  35. (option names required-arg? optional-arg? processor)
  36. option?
  37. (names option-names)
  38. (required-arg? option-required-arg?)
  39. (optional-arg? option-optional-arg?)
  40. (processor option-processor))
  41. (define args-fold
  42. (lambda (args
  43. options
  44. unrecognized-option-proc
  45. operand-proc
  46. . seeds)
  47. (letrec
  48. ((find
  49. (lambda (l ?)
  50. (cond ((null? l) #f)
  51. ((? (car l)) (car l))
  52. (else (find (cdr l) ?)))))
  53. (find-option
  54. ;; ISSUE: This is a brute force search. Could use a table.
  55. (lambda (name)
  56. (find
  57. options
  58. (lambda (option)
  59. (find
  60. (option-names option)
  61. (lambda (test-name)
  62. (equal? name test-name)))))))
  63. (scan-short-options
  64. (lambda (index shorts args seeds)
  65. (if (= index (string-length shorts))
  66. (scan-args args seeds)
  67. (let* ((name (string-ref shorts index))
  68. (option (or (find-option name)
  69. (option (list name)
  70. #f
  71. #f
  72. unrecognized-option-proc))))
  73. (cond ((and (< (+ index 1) (string-length shorts))
  74. (or (option-required-arg? option)
  75. (option-optional-arg? option)))
  76. (let-values
  77. ((seeds (apply (option-processor option)
  78. option
  79. name
  80. (substring
  81. shorts
  82. (+ index 1)
  83. (string-length shorts))
  84. seeds)))
  85. (scan-args args seeds)))
  86. ((and (option-required-arg? option)
  87. (pair? args))
  88. (let-values
  89. ((seeds (apply (option-processor option)
  90. option
  91. name
  92. (car args)
  93. seeds)))
  94. (scan-args (cdr args) seeds)))
  95. (else
  96. (let-values
  97. ((seeds (apply (option-processor option)
  98. option
  99. name
  100. #f
  101. seeds)))
  102. (scan-short-options
  103. (+ index 1)
  104. shorts
  105. args
  106. seeds))))))))
  107. (scan-operands
  108. (lambda (operands seeds)
  109. (if (null? operands)
  110. (apply values seeds)
  111. (let-values ((seeds (apply operand-proc
  112. (car operands)
  113. seeds)))
  114. (scan-operands (cdr operands) seeds)))))
  115. (scan-args
  116. (lambda (args seeds)
  117. (if (null? args)
  118. (apply values seeds)
  119. (let ((arg (car args))
  120. (args (cdr args)))
  121. ;; NOTE: This string matching code would be simpler
  122. ;; using a regular expression matcher.
  123. (cond
  124. (;; (rx bos "--" eos)
  125. (string=? "--" arg)
  126. ;; End option scanning:
  127. (scan-operands args seeds))
  128. (;;(rx bos
  129. ;; "--"
  130. ;; (submatch (+ (~ "=")))
  131. ;; "="
  132. ;; (submatch (* any)))
  133. (and (> (string-length arg) 4)
  134. (char=? #\- (string-ref arg 0))
  135. (char=? #\- (string-ref arg 1))
  136. (not (char=? #\= (string-ref arg 2)))
  137. (let loop ((index 3))
  138. (cond ((= index (string-length arg))
  139. #f)
  140. ((char=? #\= (string-ref arg index))
  141. index)
  142. (else
  143. (loop (+ 1 index))))))
  144. ;; Found long option with arg:
  145. => (lambda (=-index)
  146. (let*-values
  147. (((name)
  148. (substring arg 2 =-index))
  149. ((option-arg)
  150. (substring arg
  151. (+ =-index 1)
  152. (string-length arg)))
  153. ((option)
  154. (or (find-option name)
  155. (option (list name)
  156. #t
  157. #f
  158. unrecognized-option-proc)))
  159. (seeds
  160. (apply (option-processor option)
  161. option
  162. name
  163. option-arg
  164. seeds)))
  165. (scan-args args seeds))))
  166. (;;(rx bos "--" (submatch (+ any)))
  167. (and (> (string-length arg) 3)
  168. (char=? #\- (string-ref arg 0))
  169. (char=? #\- (string-ref arg 1)))
  170. ;; Found long option:
  171. (let* ((name (substring arg 2 (string-length arg)))
  172. (option (or (find-option name)
  173. (option
  174. (list name)
  175. #f
  176. #f
  177. unrecognized-option-proc))))
  178. (if (and (option-required-arg? option)
  179. (pair? args))
  180. (let-values
  181. ((seeds (apply (option-processor option)
  182. option
  183. name
  184. (car args)
  185. seeds)))
  186. (scan-args (cdr args) seeds))
  187. (let-values
  188. ((seeds (apply (option-processor option)
  189. option
  190. name
  191. #f
  192. seeds)))
  193. (scan-args args seeds)))))
  194. (;; (rx bos "-" (submatch (+ any)))
  195. (and (> (string-length arg) 1)
  196. (char=? #\- (string-ref arg 0)))
  197. ;; Found short options
  198. (let ((shorts (substring arg 1 (string-length arg))))
  199. (scan-short-options 0 shorts args seeds)))
  200. (else
  201. (let-values ((seeds (apply operand-proc arg seeds)))
  202. (scan-args args seeds)))))))))
  203. (scan-args args seeds))))