From mboxrd@z Thu Jan 1 00:00:00 1970 From: Efraim Flashner Subject: Re: Adding wc to Bournish Date: Sun, 5 Jun 2016 15:40:33 +0300 Message-ID: <20160605124033.GB859@debian-netbook> References: <20160524184720.GA27449@debian-netbook> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="32u276st3Jlj2kUU" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:45462) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b9XMH-00063k-T5 for guix-devel@gnu.org; Sun, 05 Jun 2016 08:40:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b9XMD-00044k-TF for guix-devel@gnu.org; Sun, 05 Jun 2016 08:40:49 -0400 Content-Disposition: inline In-Reply-To: <20160524184720.GA27449@debian-netbook> List-Id: "Development of GNU Guix and the GNU System distribution." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-devel-bounces+gcggd-guix-devel=m.gmane.org@gnu.org Sender: "Guix-devel" To: guix-devel@gnu.org, Ludovic =?utf-8?B?PT91dGYtOD9RP0NvdXJ0PUMzPUE4cz89?= , Eric Bavier --32u276st3Jlj2kUU Content-Type: multipart/mixed; boundary="f2QGlHpHGjS2mn6Y" Content-Disposition: inline --f2QGlHpHGjS2mn6Y Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable I've been working more on this and I think it's much better now. Currently it checks for `-l' or `-c' flags in the first position and with some borrowing of code from the ls-command I've worked out returning an error (out of order) when a file doesn't exist. And if there's no flag then it attempts to read the first argument as a file also. bournish@(guile-user)> wc -l gpl-3.0.txt foo guix_cve foo: No such file or directory 674 gpl-3.0.txt 25 guix_cve --=20 Efraim Flashner =D7=90=D7=A4=D7=A8=D7=99=D7=9D = =D7=A4=D7=9C=D7=A9=D7=A0=D7=A8 GPG key =3D A28B F40C 3E55 1372 662D 14F7 41AA E7DC CA3D 8351 Confidentiality cannot be guaranteed on emails sent or received unencrypted --f2QGlHpHGjS2mn6Y Content-Type: text/plain; charset=utf-8 Content-Disposition: attachment; filename="0001-Bournish-Add-wc-command.patch" Content-Transfer-Encoding: quoted-printable =46rom 0c37b8106ca8cf65e86029bdf89dfe50b7df8f87 Mon Sep 17 00:00:00 2001 =46rom: Efraim Flashner Date: Sun, 22 May 2016 14:56:06 +0300 Subject: [PATCH] Bournish: Add `wc' command. * guix/build/bournish.scm (%commands): Add wc command. --- guix/build/bournish.scm | 63 +++++++++++++++++++++++++++++++++++++++++++++= +++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/guix/build/bournish.scm b/guix/build/bournish.scm index 4022796..480ce5f 100644 --- a/guix/build/bournish.scm +++ b/guix/build/bournish.scm @@ -1,5 +1,6 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright =C2=A9 2016 Ludovic Court=C3=A8s +;;; Copyright =C2=A9 2016 Efraim Flashner ;;; ;;; This file is part of GNU Guix. ;;; @@ -25,7 +26,9 @@ #:use-module (ice-9 match) #:use-module (ice-9 ftw) #:use-module (srfi srfi-1) + #:use-module (srfi srfi-11) #:use-module (srfi srfi-26) + #:use-module (srfi srfi-41) #:export (%bournish-language)) =20 ;;; Commentary: @@ -103,6 +106,63 @@ characters." ((@ (guix build utils) dump-port) port (current-output-port)) *unspecified*))) =20 +(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=3D? chr #\newline)) + (port->stream (open-file file "r"))))) + +(define (lines+chars port) + ;; Return the number of lines and number of chars read from PORT. + ;; TODO: Also return the number of words. + (let loop ((lines 0) (chars 0)) + (match (read-char port) ; get the next char ready + ((? eof-object?) ;done! + (values lines chars)) + (#\newline ;recurse + (loop (1+ lines) (1+ chars))) + (_ ;recurse + (loop lines (1+ chars)))))) + +(define* (wc-command-implementation file #:optional args) + (let-values (((lines chars) + (call-with-input-file file lines+chars))) + (match args + (#\l + (format #t "~a ~a~%" lines file)) + (#\c + (format #t "~a ~a~%" chars file)) + (_ + (format #t "~a ~a ~a~%" lines chars file))))) + +(define (wc-command args . rest) + (let* ((flags (cond ((string=3D? args "-l") #\l) + ((string=3D? args "-c") #\c) + (else #\nul))) ; no flags, "args" is a file + (files (filter (lambda (file) + (catch 'system-error + (lambda () + (lstat file)) + (lambda args + (let ((errno (system-error-errno args))) + (format (current-error-port) "~a: ~a~%" + file (strerror errno)) + #f)))) + (if (char=3D? flags #\nul) (cons args rest) rest))= )) + (for-each + (lambda (file) + ((@@ (guix build bournish) wc-command-implementation) file flags)) + files))) + (define (help-command . _) (display "\ Hello, this is Bournish, a minimal Bourne-like shell in Guile! @@ -129,7 +189,8 @@ commands such as 'ls' and 'cd'; it lacks globbing, pipe= s---everything.\n")) ("help" ,help-command) ("ls" ,ls-command) ("which" ,which-command) - ("cat" ,cat-command))) + ("cat" ,cat-command) + ("wc" ,wc-command))) =20 (define (read-bournish port env) "Read a Bournish expression from PORT, and return the corresponding Sche= me --=20 2.8.3 --f2QGlHpHGjS2mn6Y-- --32u276st3Jlj2kUU Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCgAGBQJXVB29AAoJEPTB05F+rO6TosQP/05GBs0k11jqM99GtFtsumS1 svWINcbdfE7UWckCrx1fXaOr3OkuswEXqT7ujMjpM9frUUvECdfQLPpExyOXgFcd TKVfISun/fWjrrWjX6JQPq4RsN9KTBcbJchvQg7TLnjVaMoEjB5peOQADHEj/amu GOc3ZI9QJCuSRIDAOUnHn+UQL5Qka0Zrx2uevNooNnay2bs3XuP/yuy60fivs+Mq VpWhYylbDE1BD9I0BC9uI7TpfNdnlGgVnoWhkJmsQR4hgYPYwXYC2gfJTcZj1UCz fTBizWyiB1tEMeiUhqwHOeWcIQmMHUEp3e7kXbsjpKQ3soPz2CMy3nroR0zeyRIw qzokNdXsxCXOuYWALXdTvo/Wry0gdh5KuT6J9CIEvx8vBtUrjJLMceMgx+F0GQvk Yn9XqfV4RR07kKM2YT07Ww7PNo9hV1IpccP/a099FxHgtJxHpHgJhsFlBhwsAs8G zJfIrl7x0iqTxcTMphake3sx6PAQku358F65p7bzhBrxtCEW4ifevW1MnbD8Ccdb kEn/Mb/72Ypra5TH174H2BS1hhwQfjM8fkPUJdXfUcT8tR+7NKaO1mPz43nOcpXh aE9+Aap0wt3uUOJAQ00ckdDpDfeKpamJPUlsl8DtLax1gsRVoG87ioWoiwKxmogi JRC9nyL+fCXDy8Hqs0px =LsqF -----END PGP SIGNATURE----- --32u276st3Jlj2kUU--