From: ···········@gmail.com
Subject: sbcl run-program "/bin/sh"
Date: 
Message-ID: <1149697365.793370.58990@i39g2000cwa.googlegroups.com>
(My apologize for posting sbcl specific question here. I tried
··········@lists.sourceforge.net
first but my message never reach the mailing list...)

Hi,

Can anyone explain how I can run a shell subprocess within sbcl repl?

I tried all the combination of the key parameters :input :output and
:pty but it doesn't
seem to work. I don't know enough of sbcl's internal to figure out
what's missing.

Attached are some sessions with sbcl and acl80 (which can run sh
without any problem) under linux 2.6.

Thanks in advance,
fungsin.

······@vmware-bavm:~$ sbcl
This is SBCL 0.9.11, an implementation of ANSI Common Lisp.

*  (run-program "/usr/bin/who" nil :input t :output t :wait t :pty nil)
vmware   :0           Jun  6 06:09
vmware   pts/2        Jun  6 06:21 (:pts/0:S.0)
vmware   pts/3        Jun  6 11:08 (:pts/0: S.1)
vmware   pts/4        Jun  6 11:11 (:pts/0:S.2)
vmware   pts/5        Jun  6 11:23 (:pts/0:S.3)

#<SB-IMPL::PROCESS 6986 :EXITED>

*  (run-program "/bin/sh" nil :input t :output t :wait t :pty nil)

Ctrl-\ Quit

*  (run-program "/bin/sh" nil :input t :output t :wait t :pty t)
Ctrl-\ Quit


;; ACL 80

CL-USER(2): (run-shell-command "who")
vmware   :0           Jun  6 06:09
vmware   pts/2        Jun  6 06:21 (:pts/0:S.0)
vmware   pts/3        Jun  6 11:08 (:pts/0:S.1)
vmware   pts/4        Jun  6 11:11 (:pts/0: S.2)
0
CL-USER(3): (shell "/bin/sh")
sh-3.00$ ls
gconfd-vmware  keyring-cgtpMG  orbit-vmware  ssh-PyhjPx6302

sh-3.00$ exit
exit
0

CL-USER(4):

From: GP lisper
Subject: Re: sbcl run-program "/bin/sh"
Date: 
Message-ID: <slrne8eg18.752.spambait@phoenix.clouddancer.com>
On 7 Jun 2006 09:22:45 -0700, <···········@gmail.com> wrote:
>
> Can anyone explain how I can run a shell subprocess within sbcl repl?

It appears that you are running the program, and expecting the
output. You need something similar to:

(defun read-from-ngrep-save-to-file ()
  "real-time processing, saved in /tmp"
  (with-open-stream (quotes (ext:process-output
			     (ext:run-program "/usr/bin/ngrep"
					      '("-qltW" "none" "src" "63.77.167.140" "and" "port" "443")
					      :output :stream :wait nil :error nil)))
    (with-open-file (*file* (pathname "/tmp/n100-today") :direction :output :if-exists :overwrite :if-does-not-exist :create)
    (when quotes
      (let ((first-line (read-line quotes nil))) (header first-line))
      (loop for line = (read-line quotes nil)
	    while line do 
....

which is some old cmucl cells code that runs a filter and leaves a trace in /tmp

The process-output needs to wrap the run-program, the cmucl docs might be of some use.

-- 
Reply-To email is ignored.

-- 
Posted via a free Usenet account from http://www.teranews.com
From: ···········@gmail.com
Subject: Re: sbcl run-program "/bin/sh"
Date: 
Message-ID: <1149718461.540613.208190@i39g2000cwa.googlegroups.com>
GP lisper wrote:
>
> It appears that you are running the program, and expecting the
> output.

Thanks for the response. Maybe I'm not so clear in my original post.

What I want to do is spawn a shell from the repl. Do whatever I want
with the shell.
When I quit the shell, I'll be back at the repl.

Running an external program to completion (like who or ngrep) is not a
problem. As seen from my post I can do this easily with sbcl and ACL.

But I can't spawn an interactive shell with sbcl's (run-program
"/bin/sh" nil :input t :output t :wait t :pty t)

I must be missing something very obvious...

TIA,
fungsin

;; ----------------------------
;; ACL 8.0

;; spawn a shell
 CL-USER(3): (shell "/bin/sh")
;; type whatever commands
sh-3.00$ ls

gconfd-vmware  keyring-cgtpMG  orbit-vmware  ssh-PyhjPx6302

;; done with shell
sh-3.00$ exit
exit
0
;; back to repl
CL-USER(4):


;; ----------------------------
;; sbcl

*  (run-program "/bin/sh" nil :input t :output t :wait t :pty nil)
;; nothing happens.
Ctrl-\ Quit

*  (run-program "/bin/sh" nil :input t :output t :wait t :pty t)
;; nothing happens.
Ctrl-\ Quit
From: GP lisper
Subject: Re: sbcl run-program "/bin/sh"
Date: 
Message-ID: <slrne8eooi.70p.spambait@phoenix.clouddancer.com>
On 7 Jun 2006 15:14:21 -0700, <···········@gmail.com> wrote:
>
> What I want to do is spawn a shell from the repl. Do whatever I want
> with the shell.
> When I quit the shell, I'll be back at the repl.

you mean that you want to suspend the lisp program, a la ^z

> Running an external program to completion (like who or ngrep) is not a
> problem. As seen from my post I can do this easily with sbcl and ACL.
>
> But I can't spawn an interactive shell with sbcl's (run-program
> "/bin/sh" nil :input t :output t :wait t :pty t)
>
> I must be missing something very obvious...

where did the input/output streams go?
what did you expect them to do?
the supplied code treated the output stream rather explicitly

on a windowing OS, simply popup a window: (ext:run-program "xterm" nil :wait nil)

on a non-windowing OS, use 'screen' or login again, there are dozens
of non-lisp solutions


>  CL-USER(3): (shell "/bin/sh")

the obvious answer is to investigate ACL's "shell" sourcecode.

-- 
Reply-To email is ignored.

-- 
Posted via a free Usenet account from http://www.teranews.com
From: Juho Snellman
Subject: Re: sbcl run-program "/bin/sh"
Date: 
Message-ID: <slrne8et9m.tqm.jsnell@sbz-30.cs.Helsinki.FI>
···········@gmail.com <···········@gmail.com> wrote:
> (My apologize for posting sbcl specific question here. I tried
> ··········@lists.sourceforge.net
> first but my message never reach the mailing list...)

The sourceforge mailing lists have been a bit erratic for the last
week or so.

> Attached are some sessions with sbcl and acl80 (which can run sh
> without any problem) under linux 2.6.
[...]
> *  (run-program "/bin/sh" nil :input t :output t :wait t :pty nil)

In principle this looks like it should work, and does indeed work on a
non-Linux system or (rather bizarrely) on Linux when a "strace -ff"
has been attached to the sbcl process. Based on a little bit of
poking, it looks like it's some sort of interaction with the shell's
job control and the tty-backed fds that end up getting used for stdin
/ stdout. 

You could roll your own from fork() and exec*(). e.g.

(require :sb-posix)

(define-alien-routine "execl" int
  (path c-string)
  (arg0 c-string)
  (arg1 c-string))

(defun run-shell ()
  (let ((pid (sb-posix:fork)))
    (if (= pid 0)
        (execl "/bin/sh" "/bin/sh" nil)
        (let ((status (make-array 1 :element-type '(signed-byte 32))))
          (sb-posix:waitpid pid 0 status)))))

[ Not really a proper implementation, more of a skeleton ]

-- 
Juho Snellman
From: Rob Warnock
Subject: Re: sbcl run-program "/bin/sh"
Date: 
Message-ID: <pu-dnekOMcAtcRrZnZ2dnUVZ_tmdnZ2d@speakeasy.net>
Juho Snellman <······@iki.fi> wrote:
+---------------
| ···········@gmail.com <···········@gmail.com> wrote:
| > Attached are some sessions with sbcl and acl80 (which can run sh
| > without any problem) under linux 2.6.
| [...]
| > *  (run-program "/bin/sh" nil :input t :output t :wait t :pty nil)
| 
| In principle this looks like it should work, and does indeed work on a
| non-Linux system or (rather bizarrely) on Linux when a "strace -ff"
| has been attached to the sbcl process. Based on a little bit of
| poking, it looks like it's some sort of interaction with the shell's
| job control and the tty-backed fds that end up getting used for stdin
| / stdout. 
+---------------

Further confirmation of that is what I see in CMUCL-19c on FreeBSD:

    > (run-program "/bin/sh" nil :input t :output t :wait t :pty nil)
    sh: can't access tty; job control turned off
    $ date
    Thu Jun  8 02:04:19 PDT 2006
    $ tty
    /dev/ttype
    $ exit 12

    #<process 1434 :EXITED>
    > 

That is, it works, but "sh" complains about the job control environment.
[And, as you say, not at all on Linux (without tracing).]

I dug into this once before when I was having trouble piping
output from CMUCL into "less", and the problem there was that
"less" was unable to access /dev/tty to read the interactive
control input. ISTR something in "cmucl/src/runprog.c" that
gets used to run sub-processes... Yes, here it is. The problem
was the line marked[*]:

       pid_t
       spawn(char *program, char *argv[], char *envp[], char *pty_name,
	     int stdin, int stdout, int stderr)
       {
	   pid_t pid;
	   sigset_t set;
	   int fd;

	   pid = fork();
	   if (pid != 0)
	       return pid;

	   /* Put us in our own process group. */
[*]==>   setsid();
	   ...
       }

That line caused any "read()"s that "less" did from /dev/tty to
get EOFs [which meant I couldn't move around in the file; the whole
thing just scrolled to the end and "less" exited immediately].
I *think* my solution was to remove that line[1], which isn't
there in other programs I looked at which run sub-proccesses,
but my memory is blurry as to whether it worked completely.

Anyway, you [fungsin.lui] might look in that area...


-Rob

[1] I didn't rebuild CMUCL to do that [since I hadn't yet learned
    how at that point]. What I actually did instead was create a
    "my-runprog.c" containing a modified "my_spawn()", then compiled
    that into a "my-runprog.so" loaded with LOAD-FOREIGN, then
    hacked up a version of EXTENSIONS:RUN-PROGRAM that called
    "my_spawn" instead of "spawn".

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: ···········@gmail.com
Subject: Re: sbcl run-program "/bin/sh"
Date: 
Message-ID: <1149792280.074199.298610@f6g2000cwb.googlegroups.com>
Thanks Juho and Rob, those are very helpful.

For now the ffi execl method serves my immediate need. I'll report back
if I have time to investigate.

fungsin