diff --git a/retropikzel/shell.scm b/retropikzel/shell.scm index 54353c1..0d6ede2 100644 --- a/retropikzel/shell.scm +++ b/retropikzel/shell.scm @@ -6,6 +6,8 @@ (define-c-procedure c-tempnam libc 'tempnam 'pointer '(pointer pointer)) (define-c-procedure c-system libc 'system 'int '(pointer)) +(define previous-exit-code #f) + (define (shell cmd) (let* ((temp-prefix (string->c-utf8 "npcmd")) (temp-name (lambda () @@ -19,7 +21,7 @@ input-path " & "))) (create-pipe input-path 0777) - (c-system (string->c-utf8 shell-command)) + (set! previous-exit-code (c-system (string->c-utf8 shell-command))) (pipe-read-string 64000 (open-input-pipe input-path #t)))) (define (lines->list port result) @@ -33,3 +35,5 @@ (define (shell->sexp cmd) (read (open-input-string (shell cmd)))) + +(define (shell-exit-code) previous-exit-code) diff --git a/retropikzel/shell.sld b/retropikzel/shell.sld index 45ed35e..1fdc677 100644 --- a/retropikzel/shell.sld +++ b/retropikzel/shell.sld @@ -8,7 +8,8 @@ (retropikzel named-pipes)) (export shell shell->list - shell->sexp) + shell->sexp + shell-exit-code) (include "shell.scm")) diff --git a/retropikzel/shell/README.md b/retropikzel/shell/README.md index 9649f22..178f8dd 100644 --- a/retropikzel/shell/README.md +++ b/retropikzel/shell/README.md @@ -15,3 +15,7 @@ Run given cmd string and return output as list of lines. Run given cmd string and return output as sexp using read. + +(**shell-exit-code**) + +Returns exit code of previous command that was run. diff --git a/retropikzel/shell/test.scm b/retropikzel/shell/test.scm index 4e25a7a..1020d27 100644 --- a/retropikzel/shell/test.scm +++ b/retropikzel/shell/test.scm @@ -7,13 +7,14 @@ (test-begin "shell") -(write (shell "ls")) -(newline) +(test-equal "Linux\n" (shell "uname")) +(test-equal 0 (shell-exit-code)) -(write (shell->list "ls")) -(newline) +(test-equal '("Linux") (shell->list "uname")) +(test-equal 0 (shell-exit-code)) + +(test-equal '(1 2 3) (shell->sexp "echo '(1 2 3)'")) +(test-equal 0 (shell-exit-code)) -(write (shell->sexp "echo '(1 2 3)'")) -(newline) (test-end "shell")