3

I'm working on a command line application for Solaris, written in Java6. I'd like to be able to scroll through a history of previous commands using the up and down arrows like many Unix tools allow (shells, VIM command mode prompt, etc).

Is there any standard way of achieving this, or do I have to roll my own?

jrouquie
  • 4,315
  • 4
  • 27
  • 43
Andrew
  • 11,894
  • 12
  • 69
  • 85

5 Answers5

5

Yes, use the GNU readline library.

Pat Notz
  • 208,672
  • 30
  • 90
  • 92
  • I don't give downvotes unless the answer is "bad", but I'm surprised that this is accepted answer given the GPL factor. – Jason S Jan 05 '09 at 18:39
  • @Jason: what GPL factor? You don't have to distribute `readline` with your code. You can assume the client has it in its system, or provide instructions on how to install it. This is similar to the way Audacity uses ffmpeg. They don't have a license to distribute it, but they do tell you how to get it, and they use it in their software. – Nathan Fellman Dec 22 '10 at 15:26
  • from their webpage (http://tiswww.case.edu/php/chet/readline/rltop.html): "Readline is free software, distributed under the terms of the GNU General Public License, version 3. This means that if you want to use Readline in a program that you release or distribute to anyone, the program must be free software and have a GPL-compatible license." So it spreads the GPL-ness from a library to the entire program. Contrast with LGPL which allows runtime linking to a library, and requires open-sourcing only for improvements to the library itself. – Jason S Dec 22 '10 at 16:26
  • Any software you download will have some kind of license and you're well advised to abide by it if you choose to use the software. The fact remains that GNU Readline is very capable and widely used. What's more surprising is that this is the accepted answer given that GNU Readline is a C library. When I originally posted I apparently didn't notice the Java requirement. – Pat Notz Dec 23 '10 at 22:02
3

I think you are looking for something like JLine but I've never used it so cannot attest to its quality.

She can apparently deal with autocompletion and command line history, and the last release was recently (feb this year) so it's by no means dead.

SCdF
  • 57,260
  • 24
  • 77
  • 113
2

ledit is great on linux for that sort of thing. It's probably easily compiled on solaris.

Clarification: ledit wraps the call to your other command line app, and can even be passed a file to persistently store your history.

Here's the homepage: http://cristal.inria.fr/~ddr/ledit/

TREE
  • 1,292
  • 2
  • 12
  • 21
1

warning: GNU readline is subject to GPL licensing terms:

Readline is free software, distributed under the terms of the GNU General Public License, version 2. This means that if you want to use Readline in a program that you release or distribute to anyone, the program must be free software and have a GPL-compatible license. If you would like advice on making your license GPL-compatible, contact licensing@gnu.org.

In other words, use of Readline spreads the GPL-ness from a library to the entire program. (Contrast with LGPL, which allows runtime linking to a library, and requires open-sourcing only for improvements to the library itself.)

For those of us in the commercial world, even if we're not developing commercial applications, this is a show-stopper.

Anyway, the wikipedia page lists several alternatives, including JLine, which sounds promising.

Just as an aside: I work for a company that designs medical products. We make zero (0) dollars off of PC software. Nearly all our software runs on the embedded systems that we design (and we don't make any money off sales/upgrades of this software, only the products themselves); sometimes we do have software diagnostic tools that can run on the end-users' PCs. (design/manufacture/test software that's not released to customers I would think might be possible to use GPL libraries but I'm not sure) Medical products have fairly tight controls; you basically have to prove to the FDA that it's safe for users, it's not like the end user can decide "oh, I don't like this software, I'll just tweak it or use company XYZ's aftermarket replacement" -- that would leave device manufacturers open to a huge liability.

Jason S
  • 184,598
  • 164
  • 608
  • 970
1

There is a SourceForge project, http://java-readline.sourceforge.net/, that provides JNI-based bindings to GNU readline. I've played around with it (not used in an actual project), and it certainly covers all of the functionality.

rjray
  • 5,525
  • 4
  • 31
  • 37