Make 'trap' handle a numerical action.

* gash/built-ins/utils.scm (string->nonnegative-integer): New procedure.
* gash/built-ins/trap.scm (main): If the first argument is a number,
treat it as if the action were '-'.
This commit is contained in:
Timothy Sample 2019-10-11 09:43:19 -04:00
parent 38001cb76d
commit 6990d656bc
2 changed files with 12 additions and 0 deletions

View File

@ -17,6 +17,7 @@
;;; along with Gash. If not, see <http://www.gnu.org/licenses/>.
(define-module (gash built-ins trap)
#:use-module (gash built-ins utils)
#:use-module (gash compat)
#:use-module (gash environment)
#:use-module (ice-9 match))
@ -54,6 +55,8 @@
(match args
(() "print")
(("--" . args) (main args))
(((? string->nonnegative-integer n) conditions ...)
(apply main (cons "-" args)))
((action conditions ..1)
(let ((handler (action->handler action)))
(for-each (lambda (condition)

View File

@ -23,6 +23,7 @@
built-in?
split-assignment
string->positive-integer
string->nonnegative-integer
string->exit-status))
;;; Commentary:
@ -68,6 +69,14 @@
(lambda (n)
(and (exact-integer? n) (> n 0) n))))
(define (string->nonnegative-integer s)
"Return the nonnegative integer represented by the string @var{s}.
If @var{s} does not represent a nonnegative, decimal integer return
@code{#f}."
(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