From 73d46eb504a0b076614eaee88b09c1c56d409a54 Mon Sep 17 00:00:00 2001 From: Efraim Flashner Date: Sun, 22 May 2016 14:56:06 +0300 Subject: [PATCH] Bournish: Add `wc' command. * guix/build/bournish.scm (file-size, wc-c-command, wc-w-command, wc-l-command, wc-command): New variables. (%commands): Add wc command. --- guix/build/bournish.scm | 52 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/guix/build/bournish.scm b/guix/build/bournish.scm index 4022796..336a650 100644 --- a/guix/build/bournish.scm +++ b/guix/build/bournish.scm @@ -1,5 +1,6 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2016 Ludovic Courtès +;;; Copyright © 2016 Efraim Flashner ;;; ;;; This file is part of GNU Guix. ;;; @@ -103,6 +104,54 @@ characters." ((@ (guix build utils) dump-port) port (current-output-port)) *unspecified*))) +(define (file-contents file) + `(make-stream + (port->stream (open-input-file ,file) read-char))) + +(define (file-size file) + (if (and (file-exists? file) + (access? file 4)) + (stat:size (stat file)) + *unspecified*)) + +(define (wc-c-command file) + ((@@ (guix build bournish) file-size) file)) + +(define (wc-w-command file) + (let* ((input-file (open-file file "r")) + (line (make-string 80)) + (word-size (read-delimited! " \t\n" line input-file)) + (word-count 0)) + (while (not (port-closed? input-file)) + (if (not (zero? word-size)) + (set! word-count (1+ word-count))) + (set! line (make-string 80)) + (if (not (eof-object? (peek-char input-file))) + (set! word-size (read-delimited! " \t\n" line input-file)) + (close-input-port input-file))) + word-count)) + +(define (wc-l-command file) + (let* ((input-file (open-file file "r")) + (line (read-line input-file)) + (line-count 0)) + (while (not (eof-object? line)) + (set! line-count (1+ line-count)) + (set! line (read-line input-file))) + line-count)) + +(define (wc-command file) + (if (and (file-exists? file) (access? file 4)) + (let* ((wc-l ((@@ (guix build bournish) wc-l-command) file)) + (wc-w ((@@ (guix build bournish) wc-w-command) file)) + (wc-c ((@@ (guix build bournish) wc-c-command) file))) + (begin + (display wc-l)(display #\space) + (display wc-w)(display #\space) + (display wc-c)(display #\space) + (display file) + (newline))))) + (define (help-command . _) (display "\ Hello, this is Bournish, a minimal Bourne-like shell in Guile! @@ -129,7 +178,8 @@ commands such as 'ls' and 'cd'; it lacks globbing, pipes---everything.\n")) ("help" ,help-command) ("ls" ,ls-command) ("which" ,which-command) - ("cat" ,cat-command))) + ("cat" ,cat-command) + ("wc" ,wc-command))) (define (read-bournish port env) "Read a Bournish expression from PORT, and return the corresponding Scheme -- 2.8.3