From 1e51c5cbd1c6162fb2fce2115df95bd533388b64 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Mon, 5 Nov 2018 20:45:26 +0100 Subject: [PATCH] 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. --- .gitignore | 1 + build-aux/build-guile.sh | 2 + configure | 1 + gash/bournish-commands.scm | 6 +++ gash/commands/basename.scm | 76 ++++++++++++++++++++++++++++++++++++++ gash/commands/dirname.scm | 13 +------ 6 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 gash/commands/basename.scm diff --git a/.gitignore b/.gitignore index 4dac3d9..53c658d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *.go *~ +/bin/basename /bin/bash /bin/cat /bin/chmod diff --git a/build-aux/build-guile.sh b/build-aux/build-guile.sh index 4bd7749..10529a1 100755 --- a/build-aux/build-guile.sh +++ b/build-aux/build-guile.sh @@ -49,6 +49,7 @@ ${srcdest}gash/shell-utils.scm ${srcdest}gash/ustar.scm ${srcdest}gash/util.scm +${srcdest}gash/commands/basename.scm ${srcdest}gash/commands/cat.scm ${srcdest}gash/commands/chmod.scm ${srcdest}gash/commands/compress.scm @@ -70,6 +71,7 @@ ${srcdest}gash/commands/which.scm " SCRIPTS=" +${srcdest}bin/basename ${srcdest}bin/cat ${srcdest}bin/chmod ${srcdest}bin/compress diff --git a/configure b/configure index b6c3669..2cef0b8 100755 --- a/configure +++ b/configure @@ -89,6 +89,7 @@ bash sh " BUILTINS=" +basename cat chmod compress diff --git a/gash/bournish-commands.scm b/gash/bournish-commands.scm index a0d8ad4..4a0d089 100644 --- a/gash/bournish-commands.scm +++ b/gash/bournish-commands.scm @@ -31,6 +31,7 @@ #:use-module (gash config) #:use-module (gash shell-utils) + #:use-module (gash commands basename) #:use-module (gash commands cat) #:use-module (gash commands compress) #:use-module (gash commands cp) @@ -48,6 +49,7 @@ #:export ( %bournish-commands + basename-command cat-command compress-command cp-command @@ -58,6 +60,8 @@ reboot-command rm-command sed-command + tar-command + touch-command rm-command wc-command which-command @@ -74,6 +78,7 @@ ((quit) (car args)) (else 1))))))) +(define basename-command (wrap-command basename "basename")) (define cat-command (wrap-command cat "cat")) (define compress-command (wrap-command "compress" compress)) (define cp-command (wrap-command "cp" cp)) @@ -93,6 +98,7 @@ (define (%bournish-commands) `( + ("basename" . ,basename-command) ("cat" . ,cat-command) ("compress" . ,compress-command) ("cp" . ,cp-command) diff --git a/gash/commands/basename.scm b/gash/commands/basename.scm new file mode 100644 index 0000000..441f5b4 --- /dev/null +++ b/gash/commands/basename.scm @@ -0,0 +1,76 @@ +;;; Gash -- Guile As SHell +;;; Copyright © 2018 Jan (janneke) Nieuwenhuizen +;;; +;;; 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 . + +;;; 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) diff --git a/gash/commands/dirname.scm b/gash/commands/dirname.scm index 6a61ab8..567542e 100644 --- a/gash/commands/dirname.scm +++ b/gash/commands/dirname.scm @@ -22,17 +22,8 @@ (define-module (gash commands dirname) #: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 guix-utils) - #:use-module (gash shell-utils) #:export ( dirname @@ -52,14 +43,14 @@ (cond (version? (format #t "dirname (GASH) ~a\n" %version) (exit 0)) ((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 removed; if NAME contains no /'s, output '.' (meaning the current directory). Options: --help display this help 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))) (else