refuse an output naming the source
-o naming the input wrote over it, whatever was asked for.
This commit is contained in:
9
sexc.scm
9
sexc.scm
@@ -5,6 +5,7 @@
|
|||||||
(chicken condition) ; handle-exceptions
|
(chicken condition) ; handle-exceptions
|
||||||
(chicken file)
|
(chicken file)
|
||||||
(chicken module)
|
(chicken module)
|
||||||
|
(only (chicken pathname) normalize-pathname)
|
||||||
(chicken plist)
|
(chicken plist)
|
||||||
(chicken pretty-print)
|
(chicken pretty-print)
|
||||||
(chicken process)
|
(chicken process)
|
||||||
@@ -155,6 +156,12 @@
|
|||||||
(dash? 'stdin)
|
(dash? 'stdin)
|
||||||
(else (car inputs)))))
|
(else (car inputs)))))
|
||||||
|
|
||||||
|
;;; Whether OUTPUT is the file INPUT, spelled however it is
|
||||||
|
(define (same-file? input output)
|
||||||
|
(and (string? input) (string? output) (not (stdout-output? output))
|
||||||
|
(equal? (normalize-pathname (to-absolute-pathname input))
|
||||||
|
(normalize-pathname (to-absolute-pathname output)))))
|
||||||
|
|
||||||
(define (stdout-output? output)
|
(define (stdout-output? output)
|
||||||
(or (eq? output 'default) (equal? output "-")))
|
(or (eq? output 'default) (equal? output "-")))
|
||||||
|
|
||||||
@@ -278,6 +285,8 @@ status, which is ours to pass on."
|
|||||||
(return #f))
|
(return #f))
|
||||||
(when (and (equal? output "-") (not (text-output? args)))
|
(when (and (equal? output "-") (not (text-output? args)))
|
||||||
(error "-o - is stdout, which takes only -C, -m or --public-interface"))
|
(error "-o - is stdout, which takes only -C, -m or --public-interface"))
|
||||||
|
(when (same-file? input output)
|
||||||
|
(error "-o names the input, and would overwrite its source" input))
|
||||||
;; Read time comes before everything, so the features have to be
|
;; Read time comes before everything, so the features have to be
|
||||||
;; in place before the first form is read
|
;; in place before the first form is read
|
||||||
(current-features
|
(current-features
|
||||||
|
|||||||
@@ -98,6 +98,16 @@
|
|||||||
(test "and leaves no file named -" #f
|
(test "and leaves no file named -" #f
|
||||||
(file-exists? "-"))
|
(file-exists? "-"))
|
||||||
|
|
||||||
|
;;; -o naming the source overwrote it, whatever was asked for
|
||||||
|
(call-with-output-file "victim.sex" (lambda (p) (display hello-source p)))
|
||||||
|
(test-assert "an output naming the source is refused"
|
||||||
|
(let ((run (sexc-run #f "victim.sex" "-C" "-o" "./victim.sex")))
|
||||||
|
(and (not (zero? (car run)))
|
||||||
|
(substring-index "would overwrite its source" (caddr run))
|
||||||
|
#t)))
|
||||||
|
(test "and the source is as it was" hello-source
|
||||||
|
(call-with-input-file "victim.sex" (lambda (p) (read-string #f p))))
|
||||||
|
|
||||||
(delete-directory scratch #t)
|
(delete-directory scratch #t)
|
||||||
|
|
||||||
(test-exit)
|
(test-exit)
|
||||||
|
|||||||
Reference in New Issue
Block a user