From mboxrd@z Thu Jan 1 00:00:00 1970 From: ludo@gnu.org (Ludovic =?utf-8?Q?Court=C3=A8s?=) Subject: Re: Adding wc to Bournish Date: Sun, 05 Jun 2016 22:37:12 +0200 Message-ID: <87eg8bpe2v.fsf@gnu.org> References: <20160524184720.GA27449@debian-netbook> <20160605124033.GB859@debian-netbook> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:54420) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b9enZ-0001z5-KV for guix-devel@gnu.org; Sun, 05 Jun 2016 16:37:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b9enX-0002PY-E3 for guix-devel@gnu.org; Sun, 05 Jun 2016 16:37:28 -0400 In-Reply-To: <20160605124033.GB859@debian-netbook> (Efraim Flashner's message of "Sun, 5 Jun 2016 15:40:33 +0300") 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: Efraim Flashner Cc: guix-devel@gnu.org Efraim Flashner skribis: > 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. Neat, thanks for the update! > From 0c37b8106ca8cf65e86029bdf89dfe50b7df8f87 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 (%commands): Add wc command. Incomplete. :-) [...] > +(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))) As discussed at , remember that =E2=80=98wc-command=E2=80=99 is called by the compiler to gen= erate Scheme code from the input shell code. Thus, it must emit code that does the job. However, here, it does the job directly, at compilation time, and emits the result of =E2=80=98for-each=E2=80=99 as code. I=E2=80=99ll commit a couple of fixes for bugs I just found and that preven= t us from doing: (compile "ls" #:from %bournish-language #:to 'scheme). This is useful to clearly understand what code is generated from the input. Ludo=E2=80=99.