io-redirect: some progress, some new tests.

This commit is contained in:
Jan Nieuwenhuizen 2018-11-14 22:41:24 +01:00
parent 049c4a20dc
commit b6340bd612
8 changed files with 29 additions and 3 deletions

View File

@ -75,6 +75,9 @@ tests="
41-dot
50-iohere
50-redirect
50-redirect-space
50-redirect-in
60-function
60-subst

View File

@ -78,7 +78,7 @@
assign < '='
io-redirect <-- [0-9]* (io-here / io-file)
io-file <-- io-op ([0-9]+ / word)
io-file <-- io-op ([0-9]+ / sp* word)
io-op <- '<&' / '>&' / '>>' / '>' / '<>'/ '<' / '>|'
io-here <-- io-here-op io-here-label sp* eol io-here-document
io-here-op <- '<<-' / '<<'

View File

@ -74,7 +74,7 @@
(('pipeline command) (pke 'background: `(pipeline+ #f ,command)))
(_ term)))
(define (command . args)
(define (exec-command . args)
(define (exec command)
(cond ((procedure? command) command)
((assoc-ref %functions (car command))
@ -177,6 +177,20 @@
(define-syntax-rule (substitution commands)
(string-trim-right (with-output-to-string (lambda _ commands))))
(define-syntax command
(lambda (x)
(syntax-case x ()
((_ word ... (io-redirect (io-file "<" file-name)))
#'(with-input-from-file file-name (command word ...)))
((_ word ... (io-redirect (io-file ">" file-name)))
#'(with-output-to-file file-name (command word ...)))
((_ word ... (io-redirect "1" (io-file ">" file-name)))
#'(with-output-to-file file-name (command word ...)))
((_ word ... (io-redirect "2" (io-file ">" file-name)))
#'(with-error-to-file file-name (command word ...)))
((_ word ...)
#'(exec-command word ...)))))
(define-syntax-rule (ignore-error o)
(let ((errexit (shell-opt? "errexit")))
(when errexit
@ -408,4 +422,4 @@
o)
(define (test . o) ;; TODO replace with implementation in scheme
(apply command (cons "test" o)))
(command (cons "test" o)))

1
test/50-redirect-in.sh Normal file
View File

@ -0,0 +1 @@
\cat < test/data/foo

View File

@ -0,0 +1,3 @@
echo foo > bar
cat bar
rm bar

View File

@ -0,0 +1 @@
foo

3
test/50-redirect.sh Normal file
View File

@ -0,0 +1,3 @@
echo foo 1>./bar
cat bar
rm bar

1
test/50-redirect.stdout Normal file
View File

@ -0,0 +1 @@
foo