From f014281c29465f01c2aed37a8359b121cf9fb677 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Mon, 5 Nov 2018 20:24:54 +0100 Subject: [PATCH] dirname: New builtin. * gash/commands/dirname.scm: New file. * build-aux/build-guile.sh: Compile it. * configure: Create script. * gash/bournish-commands.scm (dirname-command): New variable. (%bournish-commands): Add it. --- .gitignore | 1 + build-aux/build-guile.sh | 2 ++ configure | 1 + gash/bournish-commands.scm | 4 +++ gash/commands/dirname.scm | 71 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+) create mode 100644 gash/commands/dirname.scm diff --git a/.gitignore b/.gitignore index fcf7df3..4dac3d9 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ /bin/chmod /bin/compress /bin/cp +/bin/dirname /bin/find /bin/gash /bin/grep diff --git a/build-aux/build-guile.sh b/build-aux/build-guile.sh index 358b409..4bd7749 100755 --- a/build-aux/build-guile.sh +++ b/build-aux/build-guile.sh @@ -53,6 +53,7 @@ ${srcdest}gash/commands/cat.scm ${srcdest}gash/commands/chmod.scm ${srcdest}gash/commands/compress.scm ${srcdest}gash/commands/cp.scm +${srcdest}gash/commands/dirname.scm ${srcdest}gash/commands/find.scm ${srcdest}gash/commands/grep.scm ${srcdest}gash/commands/ls.scm @@ -73,6 +74,7 @@ ${srcdest}bin/cat ${srcdest}bin/chmod ${srcdest}bin/compress ${srcdest}bin/cp +${srcdest}bin/dirname ${srcdest}bin/find ${srcdest}bin/gash ${srcdest}bin/grep diff --git a/configure b/configure index 2ee8c9b..b6c3669 100755 --- a/configure +++ b/configure @@ -93,6 +93,7 @@ cat chmod compress cp +dirname find grep ls diff --git a/gash/bournish-commands.scm b/gash/bournish-commands.scm index 9ae0d93..a0d8ad4 100644 --- a/gash/bournish-commands.scm +++ b/gash/bournish-commands.scm @@ -34,6 +34,7 @@ #:use-module (gash commands cat) #:use-module (gash commands compress) #:use-module (gash commands cp) + #:use-module (gash commands dirname) #:use-module (gash commands find) #:use-module (gash commands grep) #:use-module (gash commands ls) @@ -50,6 +51,7 @@ cat-command compress-command cp-command + dirname-command find-command grep-command ls-command @@ -75,6 +77,7 @@ (define cat-command (wrap-command cat "cat")) (define compress-command (wrap-command "compress" compress)) (define cp-command (wrap-command "cp" cp)) +(define dirname-command (wrap-command "dirname" dirname)) (define find-command (wrap-command "find" find)) (define grep-command (wrap-command "grep" grep)) (define ls-command (wrap-command "ls" ls)) @@ -93,6 +96,7 @@ ("cat" . ,cat-command) ("compress" . ,compress-command) ("cp" . ,cp-command) + ("dirname" . ,dirname-command) ("find" . ,find-command) ("grep" . ,grep-command) ("ls" . ,ls-command) diff --git a/gash/commands/dirname.scm b/gash/commands/dirname.scm new file mode 100644 index 0000000..6a61ab8 --- /dev/null +++ b/gash/commands/dirname.scm @@ -0,0 +1,71 @@ +;;; 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 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 + )) + +(define (dirname . args) + (let* ((option-spec + '((help (single-char #\h)) + (version (single-char #\V)) + (zero (single-char #\z)))) + (options (getopt-long args option-spec)) + (help? (option-ref options 'help #f)) + (version? (option-ref options 'version #f)) + (files (option-ref options '() '())) + (zero? (option-ref options 'zero #f)) + (usage? (and (not help?) (null? files)))) + (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... +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 +") + (exit (if usage? 2 0))) + (else + (for-each (lambda (file) + (display ((@ (guile) dirname) file)) + (if zero? (display #\nul) (newline))) + files))))) + +(define main dirname)