basename: New builtin.
* gash/commands/basename.scm: New file. * build-aux/build-guile.sh: Compile it. * configure: Create script. * gash/bournish-commands.scm (basename-command): New variable. (%bournish-commands): Add it.
This commit is contained in:
parent
f014281c29
commit
1e51c5cbd1
|
@ -1,5 +1,6 @@
|
||||||
*.go
|
*.go
|
||||||
*~
|
*~
|
||||||
|
/bin/basename
|
||||||
/bin/bash
|
/bin/bash
|
||||||
/bin/cat
|
/bin/cat
|
||||||
/bin/chmod
|
/bin/chmod
|
||||||
|
|
|
@ -49,6 +49,7 @@ ${srcdest}gash/shell-utils.scm
|
||||||
${srcdest}gash/ustar.scm
|
${srcdest}gash/ustar.scm
|
||||||
${srcdest}gash/util.scm
|
${srcdest}gash/util.scm
|
||||||
|
|
||||||
|
${srcdest}gash/commands/basename.scm
|
||||||
${srcdest}gash/commands/cat.scm
|
${srcdest}gash/commands/cat.scm
|
||||||
${srcdest}gash/commands/chmod.scm
|
${srcdest}gash/commands/chmod.scm
|
||||||
${srcdest}gash/commands/compress.scm
|
${srcdest}gash/commands/compress.scm
|
||||||
|
@ -70,6 +71,7 @@ ${srcdest}gash/commands/which.scm
|
||||||
"
|
"
|
||||||
|
|
||||||
SCRIPTS="
|
SCRIPTS="
|
||||||
|
${srcdest}bin/basename
|
||||||
${srcdest}bin/cat
|
${srcdest}bin/cat
|
||||||
${srcdest}bin/chmod
|
${srcdest}bin/chmod
|
||||||
${srcdest}bin/compress
|
${srcdest}bin/compress
|
||||||
|
|
|
@ -89,6 +89,7 @@ bash
|
||||||
sh
|
sh
|
||||||
"
|
"
|
||||||
BUILTINS="
|
BUILTINS="
|
||||||
|
basename
|
||||||
cat
|
cat
|
||||||
chmod
|
chmod
|
||||||
compress
|
compress
|
||||||
|
|
|
@ -31,6 +31,7 @@
|
||||||
#:use-module (gash config)
|
#:use-module (gash config)
|
||||||
#:use-module (gash shell-utils)
|
#:use-module (gash shell-utils)
|
||||||
|
|
||||||
|
#:use-module (gash commands basename)
|
||||||
#:use-module (gash commands cat)
|
#:use-module (gash commands cat)
|
||||||
#:use-module (gash commands compress)
|
#:use-module (gash commands compress)
|
||||||
#:use-module (gash commands cp)
|
#:use-module (gash commands cp)
|
||||||
|
@ -48,6 +49,7 @@
|
||||||
|
|
||||||
#:export (
|
#:export (
|
||||||
%bournish-commands
|
%bournish-commands
|
||||||
|
basename-command
|
||||||
cat-command
|
cat-command
|
||||||
compress-command
|
compress-command
|
||||||
cp-command
|
cp-command
|
||||||
|
@ -58,6 +60,8 @@
|
||||||
reboot-command
|
reboot-command
|
||||||
rm-command
|
rm-command
|
||||||
sed-command
|
sed-command
|
||||||
|
tar-command
|
||||||
|
touch-command
|
||||||
rm-command
|
rm-command
|
||||||
wc-command
|
wc-command
|
||||||
which-command
|
which-command
|
||||||
|
@ -74,6 +78,7 @@
|
||||||
((quit) (car args))
|
((quit) (car args))
|
||||||
(else 1)))))))
|
(else 1)))))))
|
||||||
|
|
||||||
|
(define basename-command (wrap-command basename "basename"))
|
||||||
(define cat-command (wrap-command cat "cat"))
|
(define cat-command (wrap-command cat "cat"))
|
||||||
(define compress-command (wrap-command "compress" compress))
|
(define compress-command (wrap-command "compress" compress))
|
||||||
(define cp-command (wrap-command "cp" cp))
|
(define cp-command (wrap-command "cp" cp))
|
||||||
|
@ -93,6 +98,7 @@
|
||||||
|
|
||||||
(define (%bournish-commands)
|
(define (%bournish-commands)
|
||||||
`(
|
`(
|
||||||
|
("basename" . ,basename-command)
|
||||||
("cat" . ,cat-command)
|
("cat" . ,cat-command)
|
||||||
("compress" . ,compress-command)
|
("compress" . ,compress-command)
|
||||||
("cp" . ,cp-command)
|
("cp" . ,cp-command)
|
||||||
|
|
|
@ -0,0 +1,76 @@
|
||||||
|
;;; Gash -- Guile As SHell
|
||||||
|
;;; Copyright © 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
|
||||||
|
;;;
|
||||||
|
;;; This file is part of Gash.
|
||||||
|
;;;
|
||||||
|
;;; Gash is free software; you can redistribute it and/or modify it
|
||||||
|
;;; under the terms of the GNU General Public License as published by
|
||||||
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
||||||
|
;;; your option) any later version.
|
||||||
|
;;;
|
||||||
|
;;; Gash is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
;;; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||||
|
;;; details.
|
||||||
|
;;;
|
||||||
|
;;; You should have received a copy of the GNU General Public License
|
||||||
|
;;; along with Gash. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
;;; Commentary:
|
||||||
|
|
||||||
|
;;; Code:
|
||||||
|
|
||||||
|
(define-module (gash commands basename)
|
||||||
|
#:use-module (ice-9 getopt-long)
|
||||||
|
#:use-module (ice-9 receive)
|
||||||
|
|
||||||
|
#:use-module (gash config)
|
||||||
|
|
||||||
|
#:export (
|
||||||
|
basename
|
||||||
|
))
|
||||||
|
|
||||||
|
(define (basename . args)
|
||||||
|
(let* ((option-spec
|
||||||
|
'((multiple (single-char #\a))
|
||||||
|
(help (single-char #\h))
|
||||||
|
(version (single-char #\V))
|
||||||
|
(suffix (single-char #\s) (value #t))
|
||||||
|
(zero (single-char #\z))))
|
||||||
|
(options (getopt-long args option-spec))
|
||||||
|
(help? (option-ref options 'help #f))
|
||||||
|
(version? (option-ref options 'version #f))
|
||||||
|
(suffix (option-ref options 'suffix #f))
|
||||||
|
(mutliple? (or suffix (option-ref options 'multiple #f)))
|
||||||
|
(zero? (option-ref options 'zero #f))
|
||||||
|
(files (option-ref options '() '()))
|
||||||
|
(usage? (and (not help?) (null? files))))
|
||||||
|
(cond (version? (format #t "basename (GASH) ~a\n" %version) (exit 0))
|
||||||
|
((or help? usage?) (format (if usage? (current-error-port) #t)
|
||||||
|
"\
|
||||||
|
Usage: basename NAME [SUFFIX]
|
||||||
|
or: basename OPTION... NAME...
|
||||||
|
|
||||||
|
Options:
|
||||||
|
-a, --multiple support multiple arguments and treat each as a NAME
|
||||||
|
--help display this help and exit
|
||||||
|
-s, --suffix=SUFFIX remove a trailing SUFFIX; implies -a
|
||||||
|
--version output version information and exit
|
||||||
|
-z, --zero end each output line with NUL, not newline
|
||||||
|
")
|
||||||
|
(exit (if usage? 2 0)))
|
||||||
|
(else
|
||||||
|
(receive (files suffix)
|
||||||
|
(if suffix (values files suffix)
|
||||||
|
(values (list-head files 1) (and (pair? (cdr files)) (cadr files))))
|
||||||
|
(for-each (lambda (file)
|
||||||
|
(let ((file
|
||||||
|
(if (and (> (string-length file) 1)
|
||||||
|
(string-suffix? "/" file)) (string-drop-right file 1)
|
||||||
|
file)))
|
||||||
|
(if suffix (display ((@ (guile) basename) file suffix))
|
||||||
|
(display ((@ (guile) basename) file))))
|
||||||
|
(if zero? (display #\nul) (newline)))
|
||||||
|
files))))))
|
||||||
|
|
||||||
|
(define main basename)
|
|
@ -22,17 +22,8 @@
|
||||||
|
|
||||||
(define-module (gash commands dirname)
|
(define-module (gash commands dirname)
|
||||||
#:use-module (ice-9 getopt-long)
|
#:use-module (ice-9 getopt-long)
|
||||||
#:use-module (ice-9 match)
|
|
||||||
#:use-module (ice-9 receive)
|
|
||||||
#:use-module (ice-9 regex)
|
|
||||||
|
|
||||||
#:use-module (srfi srfi-1)
|
|
||||||
#:use-module (srfi srfi-9 gnu)
|
|
||||||
#:use-module (srfi srfi-26)
|
|
||||||
|
|
||||||
#:use-module (gash config)
|
#:use-module (gash config)
|
||||||
#:use-module (gash guix-utils)
|
|
||||||
#:use-module (gash shell-utils)
|
|
||||||
|
|
||||||
#:export (
|
#:export (
|
||||||
dirname
|
dirname
|
||||||
|
@ -52,14 +43,14 @@
|
||||||
(cond (version? (format #t "dirname (GASH) ~a\n" %version) (exit 0))
|
(cond (version? (format #t "dirname (GASH) ~a\n" %version) (exit 0))
|
||||||
((or help? usage?) (format (if usage? (current-error-port) #t)
|
((or help? usage?) (format (if usage? (current-error-port) #t)
|
||||||
"\
|
"\
|
||||||
Usage: dirname [OPTION] FILE...
|
Usage: dirname [OPTION] NAME...
|
||||||
Output each NAME with its last non-slash component and trailing slashes
|
Output each NAME with its last non-slash component and trailing slashes
|
||||||
removed; if NAME contains no /'s, output '.' (meaning the current directory).
|
removed; if NAME contains no /'s, output '.' (meaning the current directory).
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
--help display this help and exit
|
--help display this help and exit
|
||||||
--version output version information and exit
|
--version output version information and exit
|
||||||
-z, --zero end each output line with NUL, not newline
|
-z, --zero end each output line with NUL, not newline
|
||||||
")
|
")
|
||||||
(exit (if usage? 2 0)))
|
(exit (if usage? 2 0)))
|
||||||
(else
|
(else
|
||||||
|
|
Loading…
Reference in New Issue