guimb.scmi
8.06 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#! /bin/sh
# aside from this initial boilerplate, this is actually -*- scheme -*- code
main='(module-ref (resolve-module '\''(scheme guimb)) '\'main')'
exec ${GUILE-guile} -l $0 -c "(apply $main (list (command-line)))" "$@"
!#
;;;; GNU Mailutils -- a suite of utilities for electronic mail
;;;; Copyright (C) 1999-2001, 2006-2007, 2009-2012, 2014-2017 Free
;;;; Software Foundation, Inc.
;;;;
;;;; GNU Mailutils is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 3, or (at your option)
;;;; any later version.
;;;;
;;;; GNU Mailutils is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License along
;;;; with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>.
;;;;
(if (not (member "%GUILE_SITE%" %load-path))
(set! %load-path (cons "%GUILE_SITE%" %load-path)))
(define-module (scheme guimb)
:export (guimb))
(use-modules (ice-9 getopt-long)
(ice-9 rdelim)
(ice-9 eval-string)
(srfi srfi-1)
(mailutils mailutils))
(define program-name "guimb")
(define output-mailbox-name #f)
(define output-mailbox-mode #f)
(define source-file-name #f)
(define source-expression #f)
(define user-name #f)
(define input-mailbox-names '())
(define script-arguments '())
(define output-mailbox #f)
(define (guimb-version)
(format #t "guimb (~A) ~A~%" mu-package mu-version)
(exit 0))
(define (guimb-help)
(format #t "usage: guimb [OPTIONS] [MAILBOX [MAILBOX...]]
guimb applies a scheme function to each message from a set of input mailboxes
The following options stop argument processing, and pass the remaining
arguments to the guimb-getopt function, if it is defined in the module:
-c, --code=EXPR execute given Scheme expression
-s, --source=MODNAME load Scheme module MODNAME
The following options have the same effect, but do not affect further
options parsing:
-e, --expression=EXPR execute given Scheme expression
-f, --file=MODNAME load Scheme module MODNAME
The module to be loaded is normally defined in a file named MODNAME.scm
somewhere in your %load-path.
Other options:
-M, --mailbox=NAME set output mailbox name
-u, --user[=NAME] direct output to the system mailbox of the
user NAME (default - current user)
-r, --read-only open mailbox in read-only mode
Script arguments:
-g, --guile-arg=ARG append ARG to the command line passed to script
-{ args... -} append args to the command line passed to script
--lparen args... --rparen likewise
-L, --load-path=PATH append PATH to the beginning of the %load-path
-?, --help give this help list
--usage give a short usage message
-V, --version print program version
Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.
")
(format #t "Report bugs to <~A>.~%" mu-bugreport)
(exit 0))
(define (guimb-usage)
; FIXME
(guimb-help))
(define (error fmt . rest)
(with-output-to-port
(current-error-port)
(lambda ()
(format #t "~A: " program-name)
(apply format #t fmt rest)
(newline))))
(define (extract-args arglist)
(let ((level 0))
(let ((result (filter
(lambda (x)
(cond
((or (string=? x "--lparen")
(string=? x "-{"))
(set! level (+ level 1))
#f)
((or (string=? x "--rparen")
(string=? x "-}"))
(if (> level 0)
(set! level (- level 1))
(set! script-arguments (append script-arguments
(list x))))
#f)
((> level 0)
(set! script-arguments (append script-arguments
(list x)))
#f)
(else
#t)))
arglist)))
(if (> level 0)
(error "missing closing -}"))
result)))
(define (parse-cmdline cmdline)
(let ((grammar `((source (single-char #\s)
(value #t))
(code (single-char #\c)
(value #t))
(file (single-char #\f)
(value #t))
(expression (single-char #\e)
(value #t))
(mailbox (single-char #\M)
(value #t))
(user (single-char #\u)
(value optional))
(read-only (single-char #\r))
(guile-arg (single-char #\g)
(value #t))
(load-path (single-char #\L)
(value #t))
(help (single-char #\?))
(usage)
(version (single-char #\V)))))
(do ((arglist (getopt-long (extract-args (command-line)) grammar)
(cdr arglist)))
((null? arglist))
(let ((x (car arglist)))
(case (car x)
((mailbox)
(set! output-mailbox-name (cdr x)))
((source file)
(set! source-file-name (cdr x)))
((code expression)
(set! source-expression (cdr x)))
((load-path)
(set! %load-path (append
(string-split (cdr x) #\:)
%load-path)))
((user)
(set! user-name (cdr x)))
((guile-arg)
(set! script-arguments (append script-arguments (list (cdr x)))))
((version)
(guimb-version))
((help)
(guimb-help))
((usage)
(guimb-usage))
((read-only)
(set! output-mailbox-mode "r"))
('()
(if (not (null? (cdr x)))
(set! input-mailbox-names (append input-mailbox-names
(cdr x))))))))))
(define guimb-module #f)
(define (get-module)
(if (not guimb-module)
(set! guimb-module (resolve-module '(scheme guimb))))
guimb-module)
(define-macro (bound? name)
`(and (module-defined? (get-module) ',name)
(procedure? ,name)))
(define (guimb-parse-command-line cmdline)
(let ((script-args '())
(argtail (find-tail
(lambda (x)
(or (string=? x "-c")
(string=? x "--code")
(string=? x "-s")
(string=? x "--source")
(string-prefix? "--code=" x)
(string-prefix? "--source=" x)))
cmdline)))
(cond
(argtail
(if (let ((x (car argtail)))
(not (or (string-prefix? "--code=" x)
(string-prefix? "--source=" x))))
(set! argtail (cdr argtail)))
(cond ((not (null? argtail))
(set! script-args (cdr argtail))
(set-cdr! argtail '())))))
(parse-cmdline cmdline)
(set! script-arguments (append script-arguments script-args))
(if (not output-mailbox-mode)
(set! output-mailbox-mode (if (null? input-mailbox-names) "wr" "a")))
(if (and (not output-mailbox-name) user-name)
(set! output-mailbox-name (if (string? user-name)
(string-append "%" user-name)
#f)))
(set! output-mailbox (mu-mailbox-open output-mailbox-name
output-mailbox-mode))
; (write output-mailbox)(newline)
(if source-file-name
(module-use!
(get-module)
(resolve-interface (list (string->symbol source-file-name)))))
(if source-expression
(eval-string source-expression))
(if (bound? guimb-getopt)
(guimb-getopt script-arguments)) ))
(define (guimb-single-mailbox mbox)
(let msg-loop ((msg (mu-mailbox-first-message mbox)))
(if (not (eof-object? msg))
(begin
(guimb-message msg)
(msg-loop (mu-mailbox-next-message mbox))))))
(define (guimb-process-mailbox mbox)
(if (not output-mailbox)
(guimb-single-mailbox mbox)
(let msg-loop ((msg (mu-mailbox-first-message mbox)))
(if (not (eof-object? msg))
(let ((x (guimb-message msg)))
(if (mu-message? x)
(mu-mailbox-append-message output-mailbox x))
(msg-loop (mu-mailbox-next-message mbox)))))))
(define (guimb cmdline)
(mu-register-format)
(guimb-parse-command-line cmdline)
(if (null? input-mailbox-names)
(guimb-single-mailbox output-mailbox)
(for-each
(lambda (mbox-name)
(let ((current-mailbox (mu-mailbox-open mbox-name "r")))
(guimb-process-mailbox current-mailbox)))
input-mailbox-names))
(if (bound? guimb-end)
(guimb-end)))
(debug-options '(show-file-name #t
stack 20000
backtrace
depth 20
width 79))
(define main guimb)
;;;; End of guimb