From 45479fc651f7cb48b87e5e1c534c2a7488e9349f Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Sun, 4 Nov 2018 08:43:04 +0100 Subject: [PATCH] rmdir: New builtin. * gash/commands/rmdir.scm: New file. * build-aux/build-guile.sh: Compile it. * configure: Create script. * gash/bournish-commands.scm (rmdir-command): New variable. (%bournish-commands): Add it. * gash/shell-utils.scm (rmdir-p): New function. --- .gitignore | 1 + build-aux/build-guile.sh | 2 ++ configure | 1 + gash/bournish-commands.scm | 2 ++ gash/commands/rmdir.scm | 71 ++++++++++++++++++++++++++++++++++++++ gash/shell-utils.scm | 9 +++++ 6 files changed, 86 insertions(+) create mode 100644 gash/commands/rmdir.scm diff --git a/.gitignore b/.gitignore index dcb87c6..fcf7df3 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ /bin/mkdir /bin/reboot /bin/rm +/bin/rmdir /bin/sed /bin/sh /bin/tar diff --git a/build-aux/build-guile.sh b/build-aux/build-guile.sh index 0b60054..60902ae 100755 --- a/build-aux/build-guile.sh +++ b/build-aux/build-guile.sh @@ -59,6 +59,7 @@ ${srcdest}gash/commands/ls.scm ${srcdest}gash/commands/mkdir.scm ${srcdest}gash/commands/reboot.scm ${srcdest}gash/commands/rm.scm +${srcdest}gash/commands/rmdir.scm ${srcdest}gash/commands/sed.scm ${srcdest}gash/commands/tar.scm ${srcdest}gash/commands/wc.scm @@ -78,6 +79,7 @@ ${srcdest}bin/ls ${srcdest}bin/mkdir ${srcdest}bin/reboot ${srcdest}bin/rm +${srcdest}bin/rmdir ${srcdest}bin/sed ${srcdest}bin/tar ${srcdest}bin/wc diff --git a/configure b/configure index 891034e..33f3478 100755 --- a/configure +++ b/configure @@ -99,6 +99,7 @@ ls mkdir reboot rm +rmdir sed tar wc diff --git a/gash/bournish-commands.scm b/gash/bournish-commands.scm index 155e1de..3b56ccd 100644 --- a/gash/bournish-commands.scm +++ b/gash/bournish-commands.scm @@ -80,6 +80,7 @@ (define mkdir-command (wrap-command "mkdir" mkdir)) (define reboot-command (wrap-command "reboot" reboot')) (define rm-command (wrap-command "rm" rm)) +(define rmdir-command (wrap-command "rmdir" rmdir)) (define sed-command (wrap-command "sed" sed)) (define tar-command (wrap-command "tar" tar)) (define wc-command (wrap-command "wc" wc)) @@ -96,6 +97,7 @@ ("mkdir" . ,mkdir) ("reboot" . ,reboot-command) ("rm" . ,rm-command) + ("rmdir" . ,rmdir-command) ("sed" . ,sed-command) ("tar" . ,tar-command) ("wc" . ,wc-command) diff --git a/gash/commands/rmdir.scm b/gash/commands/rmdir.scm new file mode 100644 index 0000000..3c21de8 --- /dev/null +++ b/gash/commands/rmdir.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 rmdir) + #: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 ( + rmdir + )) + +(define (rmdir . args) + (let* ((option-spec + '((help (single-char #\h)) + (parents (single-char #\p)) + (version (single-char #\V)))) + (options (getopt-long args option-spec)) + (files (option-ref options '() '())) + (parents? (option-ref options 'parents #f)) + (help? (option-ref options 'help #f)) + (version? (option-ref options 'version #f)) + (files (option-ref options '() '())) + (usage? (and (not help?) (null? files)))) + (cond (version? (format #t "rmdir (GASH) ~a\n" %version) (exit 0)) + ((or help? usage?) (format (if usage? (current-error-port) #t) + "\ +Usage: rmdir [OPTION]... DIRECTORY... +Remove the DIRECTORY(ies), if they are empty. + +Options: + --help display this help and exit + -p, --parents remove DIRECTORY and its ancestors; e.g., 'rmdir -p a/b/c' is + similar to 'rmdir a/b/c a/b a' + --version output version information and exit + +") + (exit (if usage? 2 0))) + (else + (if parents? (for-each rmdir-p files) + (for-each rmdir files)))))) + +(define main rmdir) diff --git a/gash/shell-utils.scm b/gash/shell-utils.scm index ad65612..6883579 100644 --- a/gash/shell-utils.scm +++ b/gash/shell-utils.scm @@ -70,6 +70,7 @@ grep-match-column grep-match-end-column mkdir-p + rmdir-p multi-opt directory-exists? @@ -270,6 +271,14 @@ transferred and the continuation of the transfer as a thunk." (apply throw args)))))) (() #t)))) +(define (rmdir-p dir) + "Remove directory DIR and all its ancestors." + (rmdir dir) + (let loop ((dir (dirname dir))) + (when (not (equal? dir ".")) + (rmdir dir) + (loop (dirname dir))))) + (define (file-exists?* file) "Like 'file-exists?' but emits a warning if FILE is not accessible." (catch 'system-error