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
280
281
282
283
284
285
286
287
288
289
290
291
|
;;
;; SCSH process form notation
;;
;; See http://www.scsh.net/docu/html/man-Z-H-3.html#node_chap_2
;;
;; Some minor changes due to Chicken- and R7RS-incompatible identifiers:
;; | was changed to pipe, |+ was changed to pipe+
;;
;; || wasn't changed, but it's really the zero-length symbol
;;
;; BIG FAT WARNING: Don't mix this with threading, or Bad Things will happen
;;
;;; Copyright (c) 2012, Peter Bex
;; All rights reserved.
;
; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions
; are met:
;
; 1. Redistributions of source code must retain the above copyright
; notice, this list of conditions and the following disclaimer.
; 2. Redistributions in binary form must reproduce the above copyright
; notice, this list of conditions and the following disclaimer in the
; documentation and/or other materials provided with the distribution.
;
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
; FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
; COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
; INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
; STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
; OF THE POSSIBILITY OF SUCH DAMAGE.
(module scsh-process
(;; procedures
exec-path fork/pipe %fork/pipe fork/pipe+ %fork/pipe+
run/collecting* run/string* run/strings* run/port* run/file* run/sexp* run/sexps*
;; macros
run/collecting run/string run/strings run/port run/file run/sexp run/sexps
|| && (& run-final-thunk maybe->string) (run maybe->string) (exec-epf maybe->string))
(import chicken scheme data-structures)
(use extras utils files ports posix srfi-1)
;; TODO: Perhaps expose environment, and mess around with the path so that
;; execve can be used in a sensible way? Scsh has its own PATH, so we could
;; use something similar to that, but it's more work.
(define (exec-path prog . args)
(process-execute (maybe->string prog) (map maybe->string args)))
;; TODO: continue-threads argument
(define (fork/pipe #!optional thunk)
(fork/pipe+ '((1 2 0)) thunk))
;; Run a thunk and exit 0 after the thunk returns.
;; If an exception occurs, handle it and exit 1.
(define (run-final-thunk thunk)
(handle-exceptions exn
;; TODO: Figure out how SCSH does this. It shows the error
;; on stderr in the REPL, but then still quits it.
;; If we just invoke current-handler, it'll get a second REPL
(begin (print-error-message exn) (exit 1))
(thunk)
(exit 0)))
(define (fork/pipe+ conns #!optional thunk)
;; Blergh, this is silly overhead we don't really need
(let* ((from-fds (map (lambda (x) (drop-right x 1)) conns))
(to-fds (map last conns))
(pipe-pairs (map (lambda _ (receive (create-pipe))) to-fds))
(pid (process-fork)))
(if (zero? pid) ; Child
(begin
(for-each (lambda (p from-fds-for-this-p)
;; Close receiving ends of pipes in child.
(file-close (car p))
;; Set up linkage from output fds to created pipes.
(for-each (lambda (from-fd)
(duplicate-fileno (cadr p) from-fd))
from-fds-for-this-p)
;; Not needed anymore after duplication is complete.
(file-close (cadr p)))
pipe-pairs from-fds)
(if thunk (run-final-thunk thunk) #f))
(begin ; Parent
(for-each (lambda (p to-fd)
;; Close sending end in parent.
(file-close (cadr p))
;; Set up linkage from created pipes to the input fds.
(duplicate-fileno (car p) to-fd)
;; No longer needed after duplication.
(file-close (car p)))
pipe-pairs to-fds)
pid))))
;; TODO: Differentiate between fork and %fork
(define %fork/pipe fork/pipe)
(define %fork/pipe+ fork/pipe+)
(define (maybe->string s)
(cond ((string? s) s)
((or (symbol? s) (number? s)) (->string s))
(else (error "Expected a string, symbol or number"))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Baroque procedural interface ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Documented under http://www.scsh.net/docu/html/man-Z-H-3.html#node_sec_2.4.2
(define (run/collecting* fds thunk)
(let* ((temp-files (map (lambda (fd)
(let* ((file-name (create-temporary-file))
(port (open-input-file file-name)))
(delete-file file-name)
port))
fds))
(conns (map (lambda (from-fd temp-file)
(list from-fd (port->fileno temp-file)))
fds temp-files)))
(receive (p s code)
(process-wait (fork/pipe+ conns thunk))
(apply values code temp-files))))
(define (run/port* thunk)
(receive (in out)
(create-pipe)
(process-fork
(lambda ()
(run-final-thunk
(lambda ()
(file-close in)
(duplicate-fileno out 1)
(duplicate-fileno out 2)
(with-output-to-port (open-output-file* out)
(lambda ()
(with-error-output-to-port (open-output-file* out) thunk)))))))
(file-close out)
(open-input-file* in)))
(define (run/file* thunk)
(let* ((temp-file (create-temporary-file)))
(process-wait ; This is peculiar
(fork/pipe (lambda ()
(let ((fd (file-open temp-file open/wronly)))
(duplicate-fileno fd 1)
(duplicate-fileno fd 2)
(with-output-to-port (open-output-file* 1)
(lambda ()
(with-error-output-to-port (open-output-file* 2) thunk)))))))
temp-file))
(define (run/string* thunk)
(read-string #f (run/port* thunk)))
(define (run/strings* thunk)
(read-lines (run/port* thunk)))
(define (run/sexp* thunk)
(let* ((p (run/port* thunk))
(res (read p)))
(close-input-port p)
res))
(define (run/sexps* thunk)
(read-file (run/port* thunk)))
;;;;;;;;;;;;
;; Syntax ;;
;;;;;;;;;;;;
(define-syntax run/collecting
(syntax-rules ()
((_ ?fds ?epf ...) (run/collecting* `?fds (lambda () (exec-epf ?epf ...))))))
(define-syntax run/file
(syntax-rules ()
((_ ?epf ...) (run/file* (lambda () (exec-epf ?epf ...))))))
(define-syntax run/port
(syntax-rules ()
((_ ?epf ...) (run/port* (lambda () (exec-epf ?epf ...))))))
(define-syntax run/string
(syntax-rules ()
((_ ?epf ...) (run/string* (lambda () (exec-epf ?epf ...))))))
(define-syntax run/strings
(syntax-rules ()
((_ ?epf ...) (run/strings* (lambda () (exec-epf ?epf ...))))))
(define-syntax run/sexp
(syntax-rules ()
((_ ?epf ...) (run/sexp* (lambda () (exec-epf ?epf ...))))))
(define-syntax run/sexps
(syntax-rules ()
((_ ?epf ...) (run/sexps* (lambda () (exec-epf ?epf ...))))))
(define-syntax &&
(syntax-rules ()
((_ ?epf ...) (and (zero? (run ?epf)) ...))))
(define-syntax ||
(syntax-rules ()
((_ ?epf ...) (or (zero? (run ?epf)) ...))))
(define-syntax &
(syntax-rules ()
((_ ?epf ...)
(process-fork (lambda ()
(run-final-thunk (lambda () (exec-epf ?epf ...))))))))
(define-syntax run
(syntax-rules ()
((_ ?epf ...)
;; We reorder the values as they make more sense this way for SCSH compat:
;; scsh returns just the exit code, and conveniently we allow MV in single
;; value continuations, which makes it compatible.
(receive (pid normal-exit? exit-status)
(process-wait (& ?epf ...))
(values exit-status normal-exit? pid)))))
;; Perhaps this should really be a procedure?
(define-syntax setup-redirection
(syntax-rules (< > << >> = - stdports)
((_ (< ?file-name)) (setup-redirection (< 0 ?file-name)))
((_ (<< ?object)) (setup-redirection (<< 0 ?object)))
((_ (> ?file-name)) (setup-redirection (> 1 ?file-name)))
((_ (>> ?file-name)) (setup-redirection (>> 1 ?file-name)))
((_ (> ?fd ?file-name))
(duplicate-fileno (file-open (maybe->string `?file-name)
(fx+ open/wronly open/creat))
`?fd))
((_ (>> ?fd ?file-name))
(duplicate-fileno (file-open (maybe->string `?file-name)
(fx+ open/wronly (fx+ open/append open/creat)))
`?fd))
((_ (< ?fd ?file-name))
(duplicate-fileno (file-open (maybe->string `?file-name) open/rdonly)
`?fd))
((_ (<< ?fd ?object))
(fork/pipe (lambda () (display `?object))))
((_ (= ?fd-from ?fd/port-to))
(let* ((fd/port-to ?fd/port-to) ; Evaluate once
(fd-to (if (port? fd/port-to)
(port->fileno fd/port-to)
fd/port-to)))
(duplicate-fileno ?fd-from fd-to)))
((_ (- ?fd/port))
(let ((o `?fd/port))
(cond
((fixnum? ?fd/port) (file-close o))
((output-port? ?fd/port) (close-output-port o))
((input-port? ?fd/port) (close-input-port o))
(else (error "Can only close i/o-ports and file descriptor numbers" o)))))
((_ stdports)
(begin (setup-redirection (= 0 (current-input-port)))
(setup-redirection (= 1 (current-output-port)))
(setup-redirection (= 2 (current-error-port)))))
((_ ?arg0 ...)
(syntax-error "Invalid redirection pattern: " `?arg0 ...))))
;; The most "core" syntax form
(define-syntax exec-epf
;; The nested let-syntaxes exist to let us pre-empt the fallthrough
;; whenever we see one of the recognised special rules so we don't end up
;; with the generic one if we happen to make a small mistake
(syntax-rules (pipe pipe+ begin epf)
((_ (pipe ?pf0 ...) ?redir0 ...)
(exec-epf (pipe+ ((1 2 0)) ?pf0 ...) ?redir0 ...))
((_ (pipe+ ?args ...) ?redir0 ...)
(let-syntax
((pipe+
(syntax-rules ___ ()
((_ ((?from0 ?from1 ___ ?to) ___) ?pf0 ___ ?last-pf)
(let ((conns `((?from0 ?from1 ___ ?to) ___)))
(setup-redirection ?redir0) ...
(begin (fork/pipe+ conns (lambda () (exec-epf (epf ?pf0))))
___
(exec-epf (epf ?last-pf))))))))
(pipe+ ?args ...)))
((_ (begin ?expr0 ...))
(begin (setup-redirection (= 0 (current-input-port)))
(setup-redirection (= 1 (current-output-port)))
(setup-redirection (= 2 (current-error-port)))
?expr0 ...))
((_ (epf ?args ...)) ; This disambiguates redirection
(exec-epf ?args ...))
((_ (?prog ?arg0 ...) ?redir0 ...)
(begin
(setup-redirection ?redir0) ...
(exec-path `?prog `?arg0 ...)))))
)
|