sed: Add quit function.

This commit is contained in:
Timothy Sample 2018-12-09 14:36:08 -05:00
parent 5be7ed331d
commit 489f092131
4 changed files with 18 additions and 4 deletions

View File

@ -145,6 +145,7 @@ tests='
100-sed-file
100-sed-fooRbar
100-sed-pattern-address
100-sed-quit
100-sed-autoconf-dirname
100-tar

View File

@ -87,6 +87,8 @@
(define extended? (make-parameter #f))
(define quit-tag (make-prompt-tag))
(define (substitute str pattern replacement flags)
(let* ((global? (memq 'g flags))
(flags (cons (if (extended?) regexp/extended regexp/basic)
@ -109,6 +111,7 @@
(match function
(('begin . commands)
(execute-commands commands str))
(('q) (abort-to-prompt quit-tag str))
(('s pattern replacement flags)
(substitute str pattern replacement flags))
(_ (error "SED: unsupported function" function))))
@ -131,10 +134,15 @@
(out (current-output-port)))
(let loop ((pattern-space (read-line in)))
(unless (eof-object? pattern-space)
(let ((result (execute-commands commands pattern-space)))
(display result out)
(newline out)
(loop (read-line in))))
(call-with-prompt quit-tag
(lambda ()
(let ((result (execute-commands commands pattern-space)))
(display result out)
(newline out)
(loop (read-line in))))
(lambda (cont result)
(display result out)
(newline out))))
#t))
(define (sed . args)

4
test/100-sed-quit.sh Normal file
View File

@ -0,0 +1,4 @@
input='foo
bar'
echo "$input" | \sed 's/foo/baz/ ; q ; s/baz/foo/'

1
test/100-sed-quit.stdout Normal file
View File

@ -0,0 +1 @@
baz