From mboxrd@z Thu Jan 1 00:00:00 1970 From: Efraim Flashner Subject: Adding wc to Bournish Date: Tue, 24 May 2016 21:47:20 +0300 Message-ID: <20160524184720.GA27449@debian-netbook> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="WhfpMioaduB5tiZL" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:43832) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b5HMe-0006ER-Ul for guix-devel@gnu.org; Tue, 24 May 2016 14:47:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b5HMa-0008HP-Vz for guix-devel@gnu.org; Tue, 24 May 2016 14:47:36 -0400 Content-Disposition: inline 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 --WhfpMioaduB5tiZL Content-Type: multipart/mixed; boundary="gBBFr7Ir9EOA20Yy" Content-Disposition: inline --gBBFr7Ir9EOA20Yy Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable GSoC just started and so far I have 13 tabs from the guile manual open on my computer. I almost implemented `du' at the same time as `wc' but at the time I was getting tripped up with functions returning functions or returning values. `wc-l' and `wc-w' are two separate functions ATM. It should be possible to combine the two and increment the line-count if the delimiter is \n (or EOF) but I didn't want to spend too much time implementing and re-implementing the commands. `wc-w' closes the file after it finishes but `wc-l' doesn't. Good hygiene? Let the garbage collector take care of it? I also made liberal use of `make-string', this seems like a memory leak nightmare to me. I wrapped `file-size' and `wc-command' in checks that the file actually exists and can be read by the user. One of the things drilled into me =66rom turning in assignments in school was to wrap the code in checks so it'd pass the TAs checks. `wc-command', it doesn't accept arguments and I'm not happy with the method of printing out the results but it works. bournish@(guile-user)> wc gpl-3.0.txt 674 5644 35147 gpl-3.0.txt What should I add to my .guile to auto-load guix.scm from the repo? (load /path/to/guix/guix.scm) didn't work for me. --=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 --gBBFr7Ir9EOA20Yy Content-Type: text/plain; charset=utf-8 Content-Disposition: attachment; filename="0001-Bournish-Add-wc-command.patch" Content-Transfer-Encoding: quoted-printable =46rom 73d46eb504a0b076614eaee88b09c1c56d409a54 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 (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 =C2=A9 2016 Ludovic Court=C3=A8s +;;; Copyright =C2=A9 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*))) =20 +(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=20 + (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, 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 --gBBFr7Ir9EOA20Yy-- --WhfpMioaduB5tiZL Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCgAGBQJXRKGzAAoJEPTB05F+rO6TEksP/3QExUdC87lW62wfzXUdvzkC KPaNHGCBzMKS3IUDZAqswVv2ZArvzyu3SnUvoWCsMPilTNmMcTuFG8NbN16DizhS nrDSI8sWRBGKgFaKT8TuHZ48MRhlm4qra+JE364UqPjrMbMhUnQePioan/nAk57c YQGbbijyPIskY5NTPgcg2VSh+Argxr6cx4ze3m/oYrKDQM+7DOYmSIyqNG1PhUEB JRGzcAf++rrfHaO6NACZBrh4a5AbXVVbpXEqAm07QmDBGZOUzxy5HJfihEkYIfMj w+KtHYnvoVNov324GFWu2MYttS5VeGN9YO7PRhhShRg1WN1LMr13Oboqx3CtT66A JNEh0jK1KZcPiQNvfAAT++nDQ0dnJM3b12Z5vWAkTYQOPa22DgdSv7mDZe0zh58H RtdKnLLcEJMGH8mhWEsFTRchbW4WNKK2gap8OzJznt66IZMlh3IglkDTSJQC0hsA XeWxyK8ri/PeJEWNy0BOrQ47SHAFllCSCSu/ac6pnRitzNlMCJIJlL6X2pCgM1AJ HMHYe2EUakEf6VxV/mv0NikBkmfFJoyPD9JXiG940fMl1MY1DgqbrvyKzixAj1gd BH+soz+JwcDMQf3ZsRwIj8Db22LzMBNvkoiVa0NzMq1ZDP/9FSjTI0hMHBllp2ua radBXiKaZuHfR+uZmgqv =0Qdl -----END PGP SIGNATURE----- --WhfpMioaduB5tiZL--