Handle 'return' errors.

* gash/built-ins/utils.scm (string->exit-status): New procedure.
* gash/built-ins/return.scm (main): Use it to simplify argument
checking; check for too many arguments; and print messages on
errors.
* tests/functions.org (Too many arguments to return): New test.
This commit is contained in:
Timothy Sample 2019-06-14 20:58:23 -04:00
parent ffe9fc1f47
commit 150c6eac53
3 changed files with 35 additions and 15 deletions

View File

@ -17,8 +17,10 @@
;;; along with Gash. If not, see <http://www.gnu.org/licenses/>.
(define-module (gash built-ins return)
#:use-module (gash built-ins utils)
#:use-module (gash compat)
#:use-module (gash environment))
#:use-module (gash environment)
#:use-module (ice-9 match))
;;; Commentary:
;;;
@ -27,16 +29,14 @@
;;; Code:
(define (main . args)
(let* ((arg (or (and (pair? args)
(car (last-pair args)))
(number->string (get-status))
"0"))
(number (string->number arg))
(status (or (and (exact-integer? number)
(>= number 0)
(<= number 256)
number)
;; If the above is not true, the exit status is
;; undefined.
EXIT_FAILURE)))
(sh:return status)))
(match args
(() (main (number->string (get-status))))
((arg)
(match (string->exit-status arg)
(#f (format (current-error-port)
"gash: return: argument must be a number from 0 to 255~%")
EXIT_FAILURE)
(n (sh:return n))))
(_ (format (current-error-port)
"gash: return: too many arguments~%")
EXIT_FAILURE)))

View File

@ -21,7 +21,8 @@
#:export (get-evaluator
built-in?
split-assignment
string->positive-integer))
string->positive-integer
string->exit-status))
;;; Commentary:
;;;
@ -65,3 +66,11 @@
(and=> (and (string-every char-set:ascii-digit s) (string->number s))
(lambda (n)
(and (exact-integer? n) (> n 0) n))))
(define (string->exit-status s)
"Return the exit status represented by the string @var{s}. If
@var{s} does not represent an exit status (a decimal integer from 0 to
255) return @code{#f}."
(and=> (and (string-every char-set:ascii-digit s) (string->number s))
(lambda (n)
(and (exact-integer? n) (>= n 0) (<= n 255) n))))

View File

@ -72,3 +72,14 @@
cat $TEST_TMP/foo.tmp
rm $TEST_TMP/foo.tmp
#+end_src
* Too many arguments to return
:script:
#+begin_src sh
foo () {
return 1 2 3
}
foo
#+end_src
:status: 1