Does anybody know of an implementation of POSIX shell like language for scripting things in Java?
If this is not available, does anybody know if there is a ANTLR or JavaCC grammar available somewhere which I might have missed?
edit:
I know that I have Jython, JRuby, Groovy, JavaScript available for scripting, but none of them have bash like syntax.
This would not be used for scripting together Java code, but to allow people to run predefined commands which would manipulate with a big third party Media Asset Management system.
I would like to run things like:
ls | grep "something" > output
Where ls and grep would be Java commands. (This is just for illustrative purposes)
Thanks
All the POSIX shell implementations I know of are written in C, but I haven't particularly researched the question.
POSIX shell (I assume that's what you mean by bash-like) syntax is fairly complex. There is no clear separation between lexing and parsing. On the other hand the parsing hardly requires any backtracking. So a parser generator might not help so much.
EDIT since you've clarified you want shell syntax:
I think your best bet is to use an existing shell. Here are a few architecture considerations:
You can just link your application into an existing shell. Add built-ins that manipulate your asset management system. There may be licensing issues. This gives one application instance per shell instance.
You can use a simple client-server architecture, where the server is part of the application and just responds to simple commands with no control logic, and the client is linked into the shell and doesn't access application data directly. Several shells (bash, ksh, zsh) already have means for TCP access.
You might not need to reinvent a communication protocol; consider going over HTTP(S), for which server and client implementations are readily available. In fact you might even get away with having only scripts around wget or curl on the client side, so wouldn't need to patch the shell at all (this would make keeping complex state on the client side).
If you need to patch the shell (say, to add builtins), consider zsh, which has a module system. Your application (or the client part) would appear as a module that defines builtins and whatever else you need (for example the zsh distribution includes modules for things like ftp and mmap, ).
Groovy allows scripting - and the syntax is close to Java
I'm not exactly sure what you're looking for -- something like JShell? Something like Rhino (Javascript implementation in Java)? Something like Groovy (a dynamic language that produces code for the JVM)?
You could try the Groovy Shell http://groovy.codehaus.org/Groovy+Shell
Or beanshell http://www.beanshell.org/
Both are dynamic languages that run in the jvm, and have a syntax close to java while letting you call any existing java classes.
You can try using the Apache Commons CLI library that has implementations for parsing POSIX, GNU like command line arguments.
crashub bash
There is a pure Java bash interpreter implementation here:
https://github.com/crashub/bash
It doesn't currently implement a full bash based shell, but it might be a good start for the syntax parsing part of the problem.
Check out this duscussion: http://www.antlr.org/pipermail/antlr-interest/2006-May/016235.html
I guess you don't need a full grammar. Define a subset of a language, experiment a little bit. Maybe you don't even need antlr and you can write a parser by hand. Or use scala, you can turn it into any language you need.
Related
In scripting languages like Ruby/Python/Perl, we can start an interactive session and create new variables, and essentially execute whatever statements.
But in Java, I only know of a way to print arbitrary expressions - in Eclipse's debug-expression view.
There is no way to create new vars, and later utilize that var (though you can assign to pre-existing vars).
Is there any way to run Java in a interpreted IDE environment just like scripting languages?
Check out Groovy. If you want to stick to pure Java, I think Groovy will happily execute any valid Java code. You can poke at it via the GroovySH interactive shell.
Check out Beanshell It has been around a while. Also you can look at IDEOne website for an online IDE-like environment for several languages (not quite a shell). And yeah, I meant to mention Groovy too, which has the groovy shell.
As of Java 9 there will be a REPL available to allow you to start an interactive Java session.
It's called JShell, and you can read the original proposal: JEP 222: jshell: The Java Shell (Read-Eval-Print Loop)
There are a number of blogs and articles exploring some of the features of JShell, if you'd like to see it in action.
I know C (or why not C++) is the natural choice for this, but for personal reasons I want to try it in Java.
What I want to implement:
|
>
>>
some built-ins like echo and if
I was thinking of using ProcessBuilder, PipedInputStream, PipedOutputStream and the likes to achieve my goal.
Do you think this is possible without relying on JNI?
Should I use a tool like antlr to help me with the grammar, or do you think it's overkill for what I need?
Any other suggestions, materials?
The only real challenge for this is the actual parsing of the input, especially if you want to simulate say bash for example, in which cause I would definitely use Antlr to build the grammar and the associated code for that. I do not think that using Antlr would be overkill as it would help provide correct parsing and make your life implementing the actual shell functions easier because it would remove a significant portion of code that you would have to write. Some areas may need JNI, but I do not think it would be a very significant portion of code besides a few special cases at most.
I would look at other shell implementations also I know there are a few college courses that cover Operating Systems that have assignments similar to this.
This is going to be an exercise of duplication, but if you want to do it just for the sake of doing it, you should start with jline for the console handling part if you want to duplicate a console. jline handles all the native stuff so there is no JNI that you have to deal with. jline works with OSX, Linux and Windows equally.
ANTLR will not be of any benefit for doing something as simple as this, but a command line parser like JSAP will be of use.
jline and JSAP would give you everything you need to build a functional shell and command line parsing for "commands" that the shell supports.
There is http://www.beanshell.org/ which has been around for years and is used in many IDE's debuggers for evaluated expressions.
Yes, it is possible. Indeed it has been done, in JNode. JNode includes a shell called "bjorne" that is more or less complete, and more or less POSIX compatible.
The catch is that in its current form "bjorne" runs commands that execute on/under the JNode operating system, rather than on a Linux or Windows platform. So the "interface" used to launch commands does not rely on fork/exec or Process, like an implementation on a classic JVM would need to. But that was the whole point of the exercise ...
If you are implementing this yourself, Antlr won't help much because the basic shell grammar is context sensitive. Besides, the "hard bits" in implementing POSIX are in getting quoting and expansion right, and in handling subshells, redirection and the like.
It is possible, especially with simple requirements like | > >> and for. However, you won't be able to implement some functionalities like exec without native code. You might use JShell as a starting point. There is some work to do to make it compile with current JDK and libraries though.
I'm thinking about trying to convert a Scons (Python) script to another build system but was wondering if there was a Python-analysis library available in order to 'interrogate' the Scons/Python script?
What I'm [possibly] after is something along the lines of Java's reflection mechanism, in fact, if this is possible via say Jython/Java, coding in Java, that would be best for me as a Java dev (I have no real background in Python).
What I need to be able to do is extract the variable assigment values etc. for certain named class types and methods within the script, so that I can transfer them to my new output format.
Any ideas?
Thanks
Rich
If your current scons files are very regular and consistent it may be easier to do something "dumb" with standard text-editing tools. If you want to get smarter, you should notice that scons is itself a Python program, and it loads your build files which are also Python. So you could make your own "special" version of scons which implements the functions your build scripts use (to add programs, libraries, whatever). Then you could run your build scripts in your "fake" scons program and have your functions dump their arguments in a format suitable for your new build system.
In other words, don't think of the problem in terms of analyzing the Python grammar completely--realize that you can actually run your build scripts as Python code and hijack their behavior.
Easier said than done, I'm sure.
I doubt it's the best tool for migrating scons, but python's inspect module offers some reflection facilities. For the rest, you can simply poke inside live classes and objects: Python has some data hiding but does not enforce access restrictions.
I'm looking for an interactive shell that I can bolt into a Java application for use in debugging and scripting. I'm not terribly interested in scripting in Java, so some more shell-like syntax would be fine, but I would like to be able to load and run 'external commands' on the fly (which would be Java .class files).
Unfortunately my platform is very limited in space --- double digits megabytes of heap space. As such languages like Groovy, Jython and JRuby are unsuitable.
I have found BeanShell, which looks okay, but it appears to have been dead for years and the syntax is rather cumbersome for command line use --- e.g. it would be nice to have stuff like 'help' typed in produce an error message, rather than silence.
Are there any other systems I should look at?
Edit: ...aaaaand I've just discovered that BeanShell2 requires reflection abilities that my platform doesn't have, and not even RetroWeaver can help me with this. I shall try BeanShell 1, but I'm not confident.
beanShell is just fine. It is done, not dead. And there seems to be a beanshell 2.0 branch.
You can take a look at Rhino.
Rhino is an open-source implementation of JavaScript written entirely
in Java. It is typically embedded into Java applications to provide
scripting to end users. It is embedded in J2SE 6 as the default Java
scripting engine.
Cliche can expose user-specified methods that can be called during runtime.
I'd like to use the Bean Scripting Framework to make some of my Java classes be available to users at my company who wish to write scripts + who may be familiar with Javascript or Python (via Jython) or Ruby (via JRuby).
I can't really find much tutorial documentation on how to get BSF working... what I would like to do is make a Java app that runs a shell, either in Javascript or Jython or JRuby, and exposes some Java classes of mine to the scripting language.
Any pointers? I've glanced through some of the docs at the BSF Resources page but I have a feeling I'm missing something obvious to get started.
(like there must be some shell already out there, complete w/ a rudimentary debugger...)
edit: To clarify -- I know how to run bsf.jar, it works fine. And I know how to run the Javascript shell with Rhino -- but that's specific to Javascript and has nothing to do with BSF. My question is, is there a language-agnostic shell that works with BSF?
To use BSF you need bsf.jar and the library for your scripting language of choice on the classpath. To execute Javascript, no additional libraries are required.
To open an interactive Javascript console try:
java org.mozilla.javascript.tools.shell.Main
Why do you need BSF?
Any JVM language can access Java classes directly. JRuby's way, Jython's. And any JVM language will have support for debugging somehow (an example here).
Check out dynamic JVM programming languages like Groovy or JRuby! You can use your java classes without any modification.
I don't know any language agnostic shell.
You could try jline + javax.script + $language to write it yourself. Hope it helps :)