tr: New builtin.

* gash/commands/tr.scm: New file.
* gash/bournish-commands.scm: Use it.
* build-aux/build-guile.sh: Compile it.
* configure: Wrap it.
* .gitignore: Ignore it.
This commit is contained in:
Jan Nieuwenhuizen 2018-12-01 22:48:57 +01:00
parent 353af765eb
commit a043f9ef09
5 changed files with 83 additions and 0 deletions

1
.gitignore vendored
View File

@ -20,6 +20,7 @@
/bin/sh
/bin/tar
/bin/touch
/bin/tr
/bin/wc
/bin/which
/.config.make

View File

@ -73,6 +73,7 @@ ${srcdest}gash/commands/rmdir.scm
${srcdest}gash/commands/sed.scm
${srcdest}gash/commands/tar.scm
${srcdest}gash/commands/touch.scm
${srcdest}gash/commands/tr.scm
${srcdest}gash/commands/wc.scm
${srcdest}gash/commands/which.scm
@ -97,6 +98,7 @@ ${srcdest}bin/rmdir
${srcdest}bin/sed
${srcdest}bin/tar
${srcdest}bin/touch
${srcdest}bin/tr
${srcdest}bin/wc
${srcdest}bin/which
"

1
configure vendored
View File

@ -109,6 +109,7 @@ rmdir
sed
tar
touch
tr
wc
which
"

View File

@ -46,6 +46,7 @@
#:use-module (gash commands sed)
#:use-module (gash commands tar)
#:use-module (gash commands touch)
#:use-module (gash commands tr)
#:use-module (gash commands wc)
#:use-module (gash commands which)
@ -66,6 +67,7 @@
sed-command
tar-command
touch-command
tr-command
rm-command
wc-command
which-command
@ -98,6 +100,7 @@
(define sed-command (wrap-command "sed" sed))
(define tar-command (wrap-command "tar" tar))
(define touch-command (wrap-command "touch" touch))
(define tr-command (wrap-command "tr" tr))
(define wc-command (wrap-command "wc" wc))
(define which-command (wrap-command "which" which))
@ -119,6 +122,7 @@
("sed" . ,sed-command)
("tar" . ,tar-command)
("touch" . ,touch-command)
("tr" . ,tr-command)
("wc" . ,wc-command)
("which" . ,which-command)
))

75
gash/commands/tr.scm Normal file
View File

@ -0,0 +1,75 @@
;;; 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 tr)
#:use-module (ice-9 getopt-long)
#:use-module (ice-9 rdelim)
#:use-module (gash config)
#:export (
tr
))
(define (string-replace-string string from to)
(cond ((string-contains string from)
=>
(lambda (i)
(string-replace string to i (+ i (string-length from)))))
(else string)))
(define (tr . args)
(let* ((option-spec
'((delete (single-char #\d))
(help (single-char #\h))
(version (single-char #\V))))
(options (getopt-long args option-spec))
(delete? (option-ref options 'delete #f))
(files (option-ref options '() '()))
(help? (option-ref options 'help #f))
(version? (option-ref options 'version #f))
(usage? (and (not help?) (not (or (and delete? (= (length files) 1))
(= (length files) 2))))))
(cond (version? (format #t "tr (GASH) ~a\n" %version) (exit 0))
((or help? usage?) (format (if usage? (current-error-port) #t)
"\
Usage: tr [OPTION]... SET1 [SET2]
Options:
-d, --delete delete characters in SET1, do not translate
-h, --help display this help and exit
-V, --version display version information and exit
")
(exit (if usage? 2 0)))
(delete?
(let* ((s (car files))
(s (string-replace-string s "\\n" "\n"))
(s (string-replace-string s "\\r" "\r"))
(s (string-replace-string s "\\t" "\t"))
(s (string->char-set s)))
(let loop ((line (read-line (current-input-port) 'concat)))
(if (eof-object? line) #t
(begin
(display (string-delete s line))
(loop (read-line (current-input-port) 'concat)))))))
(else
(format #t "TODO: TR A B\n")))))
(define main tr)