diff --git a/rash.rkt b/rash.rkt index bee2fcf..65f6d3f 100644 --- a/rash.rkt +++ b/rash.rkt @@ -1,16 +1,31 @@ #lang racket/base -(require readline/readline) (require racket/string) -(define (do command . args) - (let ([cmd (find-executable-path command)]) +(provide run) + +(define (run command . args) + (let ([cmd (find-executable-path command)] + [output-port (file-port? (current-output-port))] + [input-port (file-port? (current-input-port))] + [error-port (file-port? (current-error-port))]) (if cmd (let-values ([(subproc in out err) - (subprocess (current-output-port) - (current-input-port) - (current-error-port) + (subprocess output-port + input-port + error-port cmd (string-join args " "))]) - (subprocess-wait subproc)) - (raise (exn:fail:filesystem command))))) + (subprocess-wait subproc) + (when (input-port? in) + (close-input-port in)) + (when (output-port? out) + (close-output-port out)) + (when (output-port? err) + (close-output-port err))) + (raise (string-append command ": command not found"))))) + +(define (file-port? port) + (if (file-stream-port? port) + port + #f))