From: ·········@yahoo.com
Subject: Piping shell commands in clisp
Date: 
Message-ID: <1127327972.010437.302080@g49g2000cwa.googlegroups.com>
Hi

How can I emulate a simple shell command say $ls -l | grep sometext in
CLisp.
In CMU it would be something like that (I can't run CMUCL now so it's
not tested):
(setq *ls* (run-program "ls" :arguments '("-l") wait: t :output
:stream))
(setq ls-output (process-output *ls*))
(setq *grep* (run-program "grep" '("sometext") wait: nil :output
:stream :input ls-output))
(setq grep-output (process-output *grep*))

I looked at the implementation notes of CLisp, looks like it doesn't
allow stream argument in :intput parameter, only :stream or :terminal
are allowed. I couldn't find any examples of using this command either.
Should MAKE-PIPE-IO-STREAM be used here?
I'd really appreciate any sample code that handles this.

Andrei

From: Matthew D Swank
Subject: Re: Piping shell commands in clisp
Date: 
Message-ID: <pan.2005.09.21.19.04.53.508391@c.net>
On Wed, 21 Sep 2005 11:39:32 -0700, astebakov wrote:

> Hi
> 
> How can I emulate a simple shell command say $ls -l | grep sometext in
> CLisp.

The troll answer is use scsh: http://www.scsh.net/ :)
Matt

-- 
"You do not really understand something unless you can
 explain it to your grandmother." — Albert Einstein.
From: Sam Steingold
Subject: Re: Piping shell commands in clisp
Date: 
Message-ID: <u1x3i9q8d.fsf@gnu.org>
> *  <·········@lnubb.pbz> [2005-09-21 11:39:32 -0700]:
>
> How can I emulate a simple shell command say $ls -l | grep sometext in
> CLisp.

(run-shell-command "ls -l | grep something")

-- 
Sam Steingold (http://www.podval.org/~sds) running w2k
<http://www.iris.org.il> <http://www.honestreporting.com>
<http://ffii.org/> <http://www.jihadwatch.org/>
There are no answers, only cross references.
From: Pascal Bourguignon
Subject: Re: Piping shell commands in clisp
Date: 
Message-ID: <87y85qql3o.fsf@thalassa.informatimago.com>
·········@yahoo.com writes:

> Hi
>
> How can I emulate a simple shell command say $ls -l | grep sometext in
> CLisp.
> In CMU it would be something like that (I can't run CMUCL now so it's
> not tested):
> (setq *ls* (run-program "ls" :arguments '("-l") wait: t :output
> :stream))
> (setq ls-output (process-output *ls*))
> (setq *grep* (run-program "grep" '("sometext") wait: nil :output
> :stream :input ls-output))
> (setq grep-output (process-output *grep*))
>
> I looked at the implementation notes of CLisp, looks like it doesn't
> allow stream argument in :intput parameter, only :stream or :terminal
> are allowed. I couldn't find any examples of using this command either.
> Should MAKE-PIPE-IO-STREAM be used here?
> I'd really appreciate any sample code that handles this.

(ext:shell "ls -l | grep sometext")

(with-open-stream (lsgrep (ext:run-shell-command "ls -l | grep sometext" 
                                                 :output :stream))
     (princ (read-line lsgrep)))

On linux, you can also revert to the basics:

(MULTIPLE-VALUE-BIND (RES H-GREP-LISP) (LINUX:|pipe|)
  (UNLESS (ZEROP RES) (ERROR "pipe error"))
  (WITH-OPEN-STREAM (LSGREP (EXT:MAKE-STREAM (AREF H-GREP-LISP 0)
                                             :DIRECTION :INPUT
                                             :ELEMENT-TYPE 'CHARACTER
                                             :EXTERNAL-FORMAT CHARSET:ISO-8859-1
                                             :BUFFERED T))
    (IF (ZEROP (LINUX:|fork|))
        (MULTIPLE-VALUE-BIND (RES H-LS-GREP) (LINUX:|pipe|)
          (UNLESS (ZEROP RES) (ERROR "pipe error"))
          (IF (ZEROP (LINUX:|fork|))
              (PROGN
                (LINUX:|close| (AREF H-LS-GREP 0))
                (LINUX:|close| 1)
                (LINUX:|dup2| (AREF H-LS-GREP 1) 1)
                (LINUX:|execl| "/bin/ls" "ls" "-l"))
              (PROGN
                (LINUX:|close| (AREF H-LS-GREP 1))
                (LINUX:|close| 0)
                (LINUX:|dup2| (AREF H-LS-GREP 0) 0)
                (LINUX:|close| 1)
                (LINUX:|dup2| (AREF H-GREP-LISP 1) 1)
                (LINUX:|execl| "/bin/grep" "grep" "lisp")))
          (LINUX:|exit| 1))
        (progn
          (LINUX:|close| (AREF H-GREP-LISP 1))
          (LOOP FOR LINE = (READ-LINE LSGREP NIL NIL)
           WHILE LINE
           DO (PRINC LINE) (TERPRI))))))

Of course, you can put all this in some nice macrology:

(pipe [ls -l]
      [grep lisp]
      (lambda (stream) 
          (loop for line = (read-line stream nil nil)
                while line do (princ line) (terpri))))

Or even:

(pipe [ls -l]
      [grep lisp]
      (loop for line = (read-line *standard-input* nil nil)
            while line do (princ line) (terpri)))

Which could be used as:

(pipe (loop repeat 10 do (print (random 10)))
      (loop for n = (read *standard-input* nil nil)
            while n if (< n 5) then (print n))
      (loop for n = (read *standard-input* nil nil)
            while n collect (oddp n)))


Have a look at scsh for some ideas.

I started something in:

cvs -z3 -d ··················@cvs.informatimago.com:/usr/local/cvs/public/chrooted-cvs/cvs co common/clisp/shell.lisp


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Kitty like plastic.
Confuses for litter box.
Don't leave tarp around.
From: ·········@yahoo.com
Subject: Re: Piping shell commands in clisp
Date: 
Message-ID: <1127332980.729028.247000@g49g2000cwa.googlegroups.com>
I tried (CLisp on Windows XP):
(cd "c:\\Linux\\CLAWK")
(setq lsgrep (ext:run-shell-command "ls -l | grep awk" :output
:stream))
(loop for line = (read-line lsgrep nil)
   while line do (format t "~a~%" line))

and the result is NIL. However if I do:
(setq lsgrep (ext:run-shell-command "ls -l" :output :stream))
(loop for line = (read-line lsgrep nil)
   while line do (format t "~a~%" line))

It'll print a few lines.
-rwx------+ 1 andr ???????? 43516 Nov 14  2002 clawk.lisp
-rwx------+ 1 andr ????????   626 Nov 14  2002 clawk.system
-rwx------+ 1 andr ????????   149 Mar 20  2002 clawk.translations
-rwx------+ 1 andr ????????  5846 Feb 11  2002 clawktest.lisp
-rwx------+ 1 andr ????????   118 Feb 10  2002 emp.data
-rwx------+ 1 andr ????????  1435 Sep 12  2002 license.txt
-rwx------+ 1 andr ????????  3010 Nov 14  2002 packages.lisp

I wonder if there is something wrong in "ls -l | grep awk" argument on
windows?

Andrei
From: Sam Steingold
Subject: Re: Piping shell commands in clisp
Date: 
Message-ID: <ull1q894x.fsf@gnu.org>
> *  <·········@lnubb.pbz> [2005-09-21 13:03:00 -0700]:
>
> I tried (CLisp on Windows XP):
> (cd "c:\\Linux\\CLAWK")
> (setq lsgrep (ext:run-shell-command "ls -l | grep awk" :output
> :stream))
> (loop for line = (read-line lsgrep nil)
>    while line do (format t "~a~%" line))
>
> and the result is NIL. However if I do:
> (setq lsgrep (ext:run-shell-command "ls -l" :output :stream))
> (loop for line = (read-line lsgrep nil)
>    while line do (format t "~a~%" line))
>
> It'll print a few lines.
> -rwx------+ 1 andr ???????? 43516 Nov 14  2002 clawk.lisp
> -rwx------+ 1 andr ????????   626 Nov 14  2002 clawk.system
> -rwx------+ 1 andr ????????   149 Mar 20  2002 clawk.translations
> -rwx------+ 1 andr ????????  5846 Feb 11  2002 clawktest.lisp
> -rwx------+ 1 andr ????????   118 Feb 10  2002 emp.data
> -rwx------+ 1 andr ????????  1435 Sep 12  2002 license.txt
> -rwx------+ 1 andr ????????  3010 Nov 14  2002 packages.lisp
>
> I wonder if there is something wrong in "ls -l | grep awk" argument on
> windows?

wfm on w2k:

[81]> (setq lsgrep (ext:run-shell-command "ls -l" :output :stream))

#<INPUT BUFFERED PIPE-INPUT-STREAM CHARACTER 1932>
[82]> (loop for line = (read-line lsgrep nil) while line do (format t "~a~%" line))
total 5382
drwxrwxrwx+ 2 sds mkgroup-l-d       0 Sep 20 17:13 CVS
-rwxrwxrwx  1 sds mkgroup-l-d    4288 Sep 21 14:10 TAGS
-rwxrwxrwx  1 sds mkgroup-l-d   17684 Sep 20 15:17 bn-samples.fas
-rwxrwxrwx  1 sds mkgroup-l-d    3581 Sep 20 15:17 bn-samples.lib
-rw-rw-rw-  1 sds mkgroup-l-d   15890 Aug 26 15:45 bn-samples.lisp
-rwxrwxrwx  1 sds mkgroup-l-d  151732 Sep 20 15:17 bn.fas
-rwxrwxrwx  1 sds mkgroup-l-d   60799 Sep 20 15:17 bn.lib
-rw-rw-rw-  1 sds mkgroup-l-d   50449 Sep 12 11:27 bn.lisp
-rw-rw-rw-  1 sds mkgroup-l-d 5072680 Jun 29 09:22 clocc.tgz
drwxrwxrwx+ 2 sds mkgroup-l-d       0 Sep  6 14:42 data
drwxrwxr-x+ 2 sds mkgroup-l-d       0 Sep  3 21:46 log
-rwxrwxrwx  1 sds mkgroup-l-d    5058 Sep 20 15:17 mba.fas
-rwxrwxrwx  1 sds mkgroup-l-d     830 Sep 20 15:17 mba.lib
-rw-rw-rw-  1 sds mkgroup-l-d    1461 Sep  9 16:06 mba.lisp
-rwxrwxrwx  1 sds mkgroup-l-d    1248 Jul 15 15:56 mba.system
-rwxrwxrwx  1 sds mkgroup-l-d     746 Jul 22 17:11 packages.fas
-rwxrwxrwx  1 sds mkgroup-l-d     334 Jul 22 17:11 packages.lib
-rwxrwxrwx  1 sds mkgroup-l-d     217 Jun 28 10:11 packages.lisp
-rwxrwxrwx  1 sds mkgroup-l-d     634 Sep 12 10:00 run-updates.sh
-rwxrwxrwx  1 sds mkgroup-l-d   76977 Sep 20 17:13 scratch.lisp
-rw-rw-rw-  1 sds mkgroup-l-d    3044 Sep  9 16:07 unbias.lisp
-rw-rw-rw-  1 sds mkgroup-l-d    2681 Sep  9 16:07 update.lisp
NIL
[83]> (close lsgrep)

T
[84]> (setq lsgrep (ext:run-shell-command "ls -l | grep bn" :output :stream))

#<INPUT BUFFERED PIPE-INPUT-STREAM CHARACTER 1668>
[85]> (loop for line = (read-line lsgrep nil) while line do (format t "~a~%" line))
-rwxrwxrwx  1 sds mkgroup-l-d   17684 Sep 20 15:17 bn-samples.fas
-rwxrwxrwx  1 sds mkgroup-l-d    3581 Sep 20 15:17 bn-samples.lib
-rw-rw-rw-  1 sds mkgroup-l-d   15890 Aug 26 15:45 bn-samples.lisp
-rwxrwxrwx  1 sds mkgroup-l-d  151732 Sep 20 15:17 bn.fas
-rwxrwxrwx  1 sds mkgroup-l-d   60799 Sep 20 15:17 bn.lib
-rw-rw-rw-  1 sds mkgroup-l-d   50449 Sep 12 11:27 bn.lisp
NIL
[86]> 

-- 
Sam Steingold (http://www.podval.org/~sds) running w2k
<http://ffii.org/> <http://www.memri.org/> <http://pmw.org.il/>
<http://www.mideasttruth.com/> <http://www.honestreporting.com>
There is an exception to every rule, including this one.
From: ·········@yahoo.com
Subject: Re: Piping shell commands in clisp
Date: 
Message-ID: <1127500776.621456.318840@z14g2000cwz.googlegroups.com>
I found what the problem was. I was using clisp 2.33.1 and now with the
latest 2.35 I don't have this problem anymore.

Thanks to all!
Andrei
From: Sam Steingold
Subject: Re: Piping shell commands in clisp
Date: 
Message-ID: <ur7bi8b68.fsf@gnu.org>
> * Pascal Bourguignon <····@zbhfr-cbgngb.pbz> [2005-09-21 21:25:31 +0200]:
>
>
> Have a look at scsh for some ideas.
>
> I started something in:
>
> cvs -z3 -d ··················@cvs.informatimago.com:/usr/local/cvs/public/chrooted-cvs/cvs co common/clisp/shell.lisp

see also <http://clisp.cons.org/clash.html>
it would be nice to extend CLISP to full scsh compatibility.

-- 
Sam Steingold (http://www.podval.org/~sds) running w2k
<http://www.openvotingconsortium.org/> <http://www.jihadwatch.org/>
<http://www.savegushkatif.org> <http://truepeace.org> <http://www.dhimmi.com/>
UNIX is as friendly to you as you are to it. Windows is hostile no matter what.
From: Matthew D Swank
Subject: Re: Piping shell commands in clisp
Date: 
Message-ID: <pan.2005.09.21.20.05.28.843083@c.net>
On Wed, 21 Sep 2005 15:37:19 -0400, Sam Steingold wrote:

> see also <http://clisp.cons.org/clash.html>
> it would be nice to extend CLISP to full scsh compatibility.

As far as the api or scsh's process notation?

-- 
"You do not really understand something unless you can
 explain it to your grandmother." — Albert Einstein.
From: Sam Steingold
Subject: Re: Piping shell commands in clisp
Date: 
Message-ID: <ufyry87lp.fsf@gnu.org>
> * Matthew D Swank <·······································@p.arg> [2005-09-21 15:05:29 -0500]:
>
> On Wed, 21 Sep 2005 15:37:19 -0400, Sam Steingold wrote:
>
>> see also <http://clisp.cons.org/clash.html>
>> it would be nice to extend CLISP to full scsh compatibility.
>
> As far as the api or scsh's process notation?

both.

-- 
Sam Steingold (http://www.podval.org/~sds) running w2k
<http://www.dhimmi.com/> <http://pmw.org.il/> <http://www.honestreporting.com>
<http://www.jihadwatch.org/> <http://www.openvotingconsortium.org/>
Don't take life too seriously, you'll never get out of it alive!
From: Rob Warnock
Subject: Re: Piping shell commands in clisp
Date: 
Message-ID: <uemdnZPbzfRWHa_eRVn-1A@speakeasy.net>
Sam Steingold  <···@gnu.org> wrote:
+---------------
| > * Matthew D Swank <·······································@p.arg>:
| > Sam Steingold wrote:
| >> it would be nice to extend CLISP to full scsh compatibility.
| >
| > As far as the api or scsh's process notation?
| 
| both.
+---------------

I've looked at this several times [and posted here on the topic
once or twice], but I don't believe it is even *possible* in
portable Common Lisp without changing the Scsh syntax -- only
slightly, but *extremely* critically -- to require an explicit
backquote in Scsh's "process forms". As I wrote back in June:

    From: ····@rpw3.org (Rob Warnock)
    Message-ID: <······················@speakeasy.net>
    Date: Mon, 06 Jun 2005 22:40:23 -0500
    ...

    [Scsh] allows one to write things like this:

	(let ((foo "filename"))   ; RUN is a Scsh macro that does what
	  (run (ls -l ,foo)))     ;  might be called "implicit quasiquoting".

	(run (cc ,file ,@flags))  ; Compile FILE with FLAGS.

    Unfortunately those are illegal to a standard CL reader, as you
    have noted, so in a (hypothetical) "CLsh" the above would have to
    look like this:

	(let ((foo "filename"))
	  (run `(ls -l ,foo)))    ; Note explicit backquote

	(run `(cc ,file ,@flags)) ; (ditto)

Perhaps CLISP supports naked "," & ",@" as an extension [in the
UNQUOTE & UNQUOTE-SPLCING fashion of Scheme, maybe?], but CLHS CL
does not.

Still, while it would not permit *direct* porting of Scsh code
[which would be of limited utility in any case, since Scsh, being
Scheme-based, is a Lisp-1], I would personally *love* to see a
fleshed-out "CLsh", even with the requirement for explicit backquotes
in the Scsh-like "process forms".


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Sam Steingold
Subject: Re: Piping shell commands in clisp
Date: 
Message-ID: <u64st8d7x.fsf@gnu.org>
> * Rob Warnock <····@ecj3.bet> [2005-09-22 05:00:43 -0500]:
>
> Sam Steingold  <···@gnu.org> wrote:
> +---------------
> | > * Matthew D Swank <·······································@p.arg>:
> | > Sam Steingold wrote:
> | >> it would be nice to extend CLISP to full scsh compatibility.
> | >
> | > As far as the api or scsh's process notation?
> | 
> | both.
> +---------------
>
> I've looked at this several times [and posted here on the topic
> once or twice], but I don't believe it is even *possible* in
> portable Common Lisp without changing the Scsh syntax -- only
> slightly, but *extremely* critically -- to require an explicit
> backquote in Scsh's "process forms".

there is no requirement to re-implement scsh in clisp: it is unlikely
that there is much scsh code that would be ported to clisp.
thus there is little reason to expend the huge effort required.
moreover, this is a good opportunity to rethink scsh features and
(maybe incompatibly) fix what is broken.
just be careful not to repeat csh/sh experience.

> Still, while it would not permit *direct* porting of Scsh code [which
> would be of limited utility in any case, since Scsh, being
> Scheme-based, is a Lisp-1], I would personally *love* to see a
> fleshed-out "CLsh", even with the requirement for explicit backquotes
> in the Scsh-like "process forms".

precisely.  would you like to work on this?

-- 
Sam Steingold (http://www.podval.org/~sds) running w2k
<http://www.honestreporting.com> <http://ffii.org/> <http://www.camera.org>
<http://pmw.org.il/> <http://www.palestinefacts.org/>
History doesn't repeat itself, but historians do repeat each other.