unofficial mirror of guix-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Adding wc to Bournish
@ 2016-05-24 18:47 Efraim Flashner
  2016-05-25  9:03 ` Ricardo Wurmus
                   ` (4 more replies)
  0 siblings, 5 replies; 23+ messages in thread
From: Efraim Flashner @ 2016-05-24 18:47 UTC (permalink / raw)
  To: guix-devel, Ludovic =?utf-8?Q?Court=C3=A8s?=, Eric Bavier


[-- Attachment #1.1: Type: text/plain, Size: 1469 bytes --]

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
from 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.

-- 
Efraim Flashner   <efraim@flashner.co.il>   אפרים פלשנר
GPG key = A28B F40C 3E55 1372 662D  14F7 41AA E7DC CA3D 8351
Confidentiality cannot be guaranteed on emails sent or received unencrypted

[-- Attachment #1.2: 0001-Bournish-Add-wc-command.patch --]
[-- Type: text/plain, Size: 3259 bytes --]

From 73d46eb504a0b076614eaee88b09c1c56d409a54 Mon Sep 17 00:00:00 2001
From: Efraim Flashner <efraim@flashner.co.il>
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 <ludo@gnu.org>
+;;; Copyright © 2016 Efraim Flashner <efraim@flashner.co.il>
 ;;;
 ;;; 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


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply related	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2016-06-23  8:34 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-05-24 18:47 Adding wc to Bournish Efraim Flashner
2016-05-25  9:03 ` Ricardo Wurmus
2016-05-25  9:43   ` Efraim Flashner
2016-05-25  9:26 ` Ricardo Wurmus
2016-05-25 10:05   ` Efraim Flashner
2016-05-26  8:46 ` Ludovic Courtès
2016-05-26 17:50   ` Efraim Flashner
2016-05-26 19:27 ` Efraim Flashner
2016-05-27 13:37   ` Eric Bavier
2016-05-27 15:28     ` Taylan Ulrich Bayırlı/Kammer
2016-05-27 15:32       ` Thompson, David
2016-06-05 12:40 ` Efraim Flashner
2016-06-05 20:37   ` Ludovic Courtès
2016-06-07  7:41     ` Efraim Flashner
2016-06-08 15:43       ` Ludovic Courtès
2016-06-14  9:27         ` Efraim Flashner
2016-06-14  9:57           ` Ricardo Wurmus
2016-06-14 10:20             ` Efraim Flashner
2016-06-14 10:50               ` Efraim Flashner
2016-06-14 11:08                 ` Ricardo Wurmus
2016-06-15 13:56                 ` Ludovic Courtès
2016-06-15 20:28                   ` Efraim Flashner
2016-06-23  8:34                     ` Ludovic Courtès

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).