implement a shell in repl

239 views
Skip to first unread message

jaime

unread,
Sep 20, 2011, 3:38:10 AM9/20/11
to Clojure
Hi guys,

I want to implement a shell on top of REPL but without any ideas on
how to do it. Could you please suggest?

What I want is something that can let user do things like:
1. in REPL, user enter "(myshell)" (or some other command name)
2. then a prompt string will show up so that user can interact
with Clojure
3. built-in commands support is possible
4. prompt for user input is possible (I think this is the most
import part that I want, but I don't know how I can make REPL prompts
a user input...)
5. real Clojure form can be dispatch to REPL and the shell can
show the result returned by REPL

is there any project already on this, or is it possible to implement
it in some kind of ways??

Thanks,
Jaime

Sanel Zukan

unread,
Sep 20, 2011, 4:03:45 AM9/20/11
to Clojure
I had quite similar task, integrating custom shell with clojure repl,
where all expressions between parenthesis
were interpreted as clojure code and rest as custom shell programs.

The best way to do this is to explore (clojure.main/repl) function.
One of the parameters will be :eval function; there clojure repl will
send
all expressions for evaluation. It could look like this:

(defn evaluate [expr]
(if (re-find #"^(.*)$" expr)
(eval expr)
;; here you call some java code for executing system commands
;; like (.exec (Runtime/getRuntime) expr) or something like that
) )

(clojure.main/repl
:eval evaluate)

With clojure.main/repl you can do even more: write custom input,
output and such. Feel free to explore it ;)

Sanel

Ken Wesson

unread,
Sep 20, 2011, 2:08:00 PM9/20/11
to clo...@googlegroups.com
Wouldn't the simplest way be to simply use the REPL itself as the
shell, with a few things defined like this?

(def dir (atom (System/getProperty "user.home")))

(defn pwd [] @dir)

(defn cd [dir] (reset! @dir dir))

(defn ... )

--
Protege: What is this seething mass of parentheses?!
Master: Your father's Lisp REPL. This is the language of a true
hacker. Not as clumsy or random as C++; a language for a more
civilized age.

jaime

unread,
Sep 20, 2011, 9:52:37 PM9/20/11
to Clojure
Good point. I think I need to find out how to make the "shell" prompt
for user input, e.g. when the user log into a system, the shell will
prompt to ask for a password....

jaime

unread,
Sep 20, 2011, 9:53:16 PM9/20/11
to Clojure
This may not work the way I want...but yes I will give it a shot. :-)

Timothy Washington

unread,
Sep 21, 2011, 5:57:28 PM9/21/11
to clo...@googlegroups.com

+1. My project has a shell on top of a repl, which is just load in a different namespace and associated functions and macros. 


This assumes that your shell language is an extension of Clojure / LISP, which is certainly the path I chose. I replaced a customed DSL I designed in Java, using SableCC. 


Incidentally, I was able to pull in the SableCC library and use my custom language on top of the repl, exactly as you describe. The lexer (waiting for user input) was using a PushbackReader. But I imagine you can use anything that does a blocked read on stdin. 


But in the end, I realised that a few custom Clojure functions and macros pretty much covered what my DSL was doing. So now my "shell" is just a namespace inside of a repl. 


HTH 

Tim 




--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply all
Reply to author
Forward
0 new messages