From f909c39530c727f414c8c827bdbccb2285ad1cc5 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 | 56 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/guix/build/bournish.scm b/guix/build/bournish.scm index 4022796..d133796 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. ;;; @@ -21,11 +22,13 @@ #:use-module (system base compile) #:use-module (system repl command) #:use-module (system repl common) + ;#:use-module (ice-9 streams) #:use-module (ice-9 rdelim) #:use-module (ice-9 match) #:use-module (ice-9 ftw) #:use-module (srfi srfi-1) #:use-module (srfi srfi-26) + #:use-module (srfi srfi-41) #:export (%bournish-language)) ;;; Commentary: @@ -103,6 +106,56 @@ characters." ((@ (guix build utils) dump-port) port (current-output-port)) *unspecified*))) +(define (file-size file) + (stat:size (stat file))) + +(define (wc-c-command file) + ;; Faster when only `wc -c' is called + (file-size file)) + +(define (wc-l-command file) + ;; Faster when only `wc -l' is called + (stream-length + (stream-filter + (lambda (chr) + (char=? chr #\newline)) + (port->stream (open-file file "r"))))) + +;(define (wc-l-command file) +; (call-with-input-file file +; (lambda (port) + +(define (lines+chars port) + ;; Return the number of lines and number of chars read from PORT. + (let loop ((lines 0) (words 0) (chars 0)) + (match (read-char port) + ((? eof-object?) ;done! + (values lines words chars)) + (#\newline ;recurse + (loop (1+ lines) (1+ words) (1+ chars))) + ((and (char-set<= ? char-set:blank) ; need to fix/replace the '?' + (char-set<= (peek-char port) char-set:graphic)) + (loop lines (1+ words) (1+ chars))) + (_ ;recurse + (loop lines words (1+ chars)))))) + +(define (wc-command file) + ((lambda (lines words chars) ; lambda doesn't like 3 variables + (format #t "~a ~a ~a ~a~%" lines words chars file)) + (call-with-input-file file lines+chars))) + +; let-values is undefined +; (let-values (((lines words chars) +; (call-with-input-file file lines+chars))) +; (format #t "~a ~a ~a ~a~%" lines chars words file))) + +;(define (wc-command file) +; (let ((wc-l (wc-l-command file)) +; (wc-w (wc-w-command file)) +; (wc-c (wc-c-command file))) +; (format #t "~{~a ~}\n" +; (list wc-l wc-w wc-c file)))) + (define (help-command . _) (display "\ Hello, this is Bournish, a minimal Bourne-like shell in Guile! @@ -129,7 +182,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