I would like to write my own interactive shell for Linux.
limited set of my own commands
no need to execute external binaries
It is not hard to do it, but I would like to do somethimg more user friendly.
TAB auto completion
interactive line editing
history of last commands
Is there some library/framework/minimal shell that can be used for this?
So I don't need to write everything myself.
Prefered license is some permissive (MIT, BSD, Apache, ..)
Prefered language is python, c, c++, javascript or sh
For Python, the standard-library cmd module does this: https://docs.python.org/2.7/library/cmd.html
For C, you have several options available for managing interactive editing, history, and tab completion:
readline, used by bash itself, but GPL-licensed.
editline, a widespread BSD alternative.
linenoise, a newer alternative.
By the way -- this question is explicitly off-topic; I expect it to be closed.
If you need job control (i.e. backgrounding) you can have a look at the glibc manual on Implementing a Shell.
Related
I need to do a interactive shell programmatically (in Node.js, but any language will do).
The most important is that I want to start a REPL of any language (node, ruby, erlang etc.) and be able to get autocompletion
pri\t
which I want to get output in stderr (for instance)
print() println() printf()
And because it's in language REPL compgen cannot be used.
I've tried many ways but it seems that it fails because shell can't be interactive while opened programmatically.
Launchin /bin/sh -i just yeilds errors like:
bash: cannot set terminal process group (XXXX): Inappropriate ioctl for device
I've heard that I can't start interactive shell without the terminal, but when doing SSH autocompletion does work which means it can be emulated in some way.
I'd appreciate Your help. I can't continue my work without finding a solution to this.
You can emulate a terminal using a pseudoterminal. This is how ssh does it. There's a lot to using a pseudoterminal though and the SO answer box isn't big enough for all of it. So check the man page for pty(7).
I have an interest in playing and fuxing with prolog, I have installed the swi-prolog and added the repository, just in case anyone is interested on which one commands I used:
% sudo apt-add-repository ppa:swi-prolog/stable
% sudo apt-get update
% sudo apt-get install swi-prolog
How do I actually begin to write prolog codes on my linux machine? for my regular programming I use VIM to write/edit/debug and terminal to compile. Can I use vim to write prolog? How do i compile or use the prolog interpreter(i think that is what it is called)?
Yes, you can use any text editor, incl. VIM. Once you have written a Prolog source file, say, file.pl, you can load it into SWI-Prolog like so:
swipl -s file.pl
This will compile your file and take you to an interactive shell where you can then ask queries against the definitions in your file.
If you want to use your Prolog program in batch mode, you can use:
swipl -s file.pl -t goal
where goal is the goal/query you want to evaluate. Note that in this case you won't be getting the option to ask for alternative solutions.
On Ubunutu, I started off using emacs, which at least does syntax highlighting:
http://www.swi-prolog.org/FAQ/GnuEmacs.html
(2 emacs suggestions on that page ^)
But now I use prolog in anger, I use an Eclipse plugin called PDT:
http://sewiki.iai.uni-bonn.de/research/pdt/docs/v2.1/start
Especially useful is the real-time line by line debug and trace, so you can step into, step over individual predicates, monitor variables names etc.. just like an other real IDE you would find in eclipse.
Probably only worth installing if you're going to use it a LOT, since the install is a lot of work, but it's a great IDE.
But if you like your low level editors like VIM, you will have to use the debug and trace tools built into swi-prolog, see:
http://www.swi-prolog.org/pldoc/man?section=debugger
To work out how the strange and beautiful prolog interpreter works, using a tracer of some kind is a must-have.
I personally use gprolog or swipl in the interpreted environment.
So you write facts and rules in a mydb.pl file, and open the interpreter in the same directory.
Once the prompt shows up you can query
['mydb.pl'].
for loading your database. now you can either see the warnings\errors or start querying from inside the prolog interpreter.
buddy I also use vim to edit prolog code, What I personally do is I save my prolog file with the '.pl' extension, and then on the terminal, I use prolog interactive environment to consult my file
e.g:
To initiate a prolog interactive environment just type On terminal:
prolog
Now that you have entered in SWI-prolog you can use 'consult' i.e pre-defined pseudo-predicates allow one to load Prolog code into a running Prolog interpreter:
?- consult("filename.pl")
that's it!
You can use any text editor to write your code. Just make sure to save your code with the .pl extension like fibo.pl.
After that open the terminal and go to the location where you have saved your code.
After that type prolog
After that write the name of your file without .pl extension and end it with . ['fibo']. and press return
Eg - cd /home/student/14917
prolog
['fibo'].
Here fibo.pl is my program name
I use SWI prolog with Sublime Text on mac. Works really nice. In Sublime Text you just hit cmd - B to run the code, and the output appears in a window within Sublime Text. There is a package for it here.
I've noticed that sometimes commands can be tab completed.
e.g. the xm command in xen.
you type xm[space][tab] and it prints out the valid options
which are:
addlabel destroy info network-attach resume sysrq vnet-delete
block-attach dmesg labels network-detach rmlabel top vnet-list
block-detach domid list network-list save trigger vtpm-list
block-list domname loadpolicy new sched-credit unpause
cfgbootpolicy dry-run log pause sched-sedf uptime
console dump-core makepolicy reboot serve vcpu-list
create dumppolicy mem-max rename shutdown vcpu-pin
debug-keys getlabel mem-set resources start vcpu-set
delete help migrate restore suspend vnet-create
That's pretty slick!
How can I implement my own tab command completion in Linux?
This is a pretty broad question, but the general idea is that you register something with the either the compgen or complete builtin. They're both documented in the manual. The previous section documents the general topic of programmable completion, going through how completion attempts are processed.
For a whole ton of examples, see /etc/bash_completion, which provides all the default completion that comes with bash (beyond the totally built-in stuff like filename completion). For even more examples, see anything in /etc/bash_completion.d; those are automatically sourced by /etc/bash_completion as a way of extending the default completion.
This is done via the shell through the use of the GNU Readline library in the case of bash
bash's smart completion is handled by a series of scripted bash functions. On Debian, probably Ubuntu, and maybe other Linux distributions, you can find your system's installed completions in /etc/bash_completion.d.
The official documentation on this mechanism is at http://www.gnu.org/software/bash/manual/bash.html#Programmable-Completion
See this:
How does bash tab completion work?
and this:
http://www.debian-administration.org/articles/316
How to make a Linux program in the Command Line Interface who display a nice user interface?
For example when I use "wget" to download a file from internet, I can see the download advancement in the Command Line Interface. How can I do that?
ncurses is a popular option, there are APIs for lots of programming languages.
Take a look at curses. It is a library for text based UI.
You can get a basic interface by using \r to go to the beginning of the current line.
Slightly more advanced is ncurses.
The next step up is Newt.
If you want to do a GUI for Bash scripts or to wrap around other command line utilities, you can use dialog (man page).
Here two great dialog tutorial to get you started :
Dialog: An Introductory Tutorial
Improve Bash Shell Scripts Using Dialog.
If you only need a progress bar, this can be done directly with a simple print (that prints the bar), followed by the carriage return character (ANSI character #13), which puts you back at the beginning of the line. The line can then be later updated by printing over it.
For more complicated needs, ncurses is indeed the standard way to go.
I wouldn't call wget's progress report as a 'nice gui', but anyway, the classic library for building graphical interfaces without X Windows is Linux and UNIX systems is ncurses.
Recently a C# version of ncurses has been started, check out details here
If you're using Mono, you could use MonoCurses
Try curses, it is a well documented API for text based UI.Also, there is so much open source projects that are using curses for you see and learn
wget does not really have a GUI, all I see is stuff that you can already achieve using stdout and echos (e.g. printf() or std::cout)
Anyways, for simple dialog boxes of the MessageBox kind, but not limited to that, also have a look at dialog
http://linux.die.net/man/1/dialog
http://hightek.org/dialog/
You can just use ANSI escape codes. A simple example in bash
echo -e "\033[H\033[2J \033[20;20H \033[4mThis is a underlined line.\033[0m"
One should mention FTXUI. Functional Terminal (X) User interface: A simple C++ library for terminal-based user interfaces!
Cross-Platform
Support for UTF8 and fullwidth chars (→ 测试)
No dependencies
etc
I have some directories with a number of "hidden" files. One example of this is I'm in a source controlled sandbox and some of the files have not been checked out yet.
When I hit TAB, I'd like the option of seeing these files.
A similar question has been asked before: CVS Tab completion for modules under linux
The answers to that question summarize to: "Ubuntu's got that built in".
I don't have the option of switching to Ubuntu, but surely I can use the same mechanisms.
how can I hook into the TAB-completion feature of tcsh to add additional file Support for CVS, SVN and BitKeeper would all be useful.
More important than support for a specific source control system is the ability to control the returned list myself.
An acceptable solution would also be to use a key-binding other than TAB. (ctrl- perhaps)
From the manpage:
the complete builtin command can be used to tell the shell how to complete words other than filenames, commands and variables
might get you started
I do not know how to program in tcsh. But if you can, then you could look at the file named "bash_completion" from the archive (find the download link here.)
On line 1673 begins CVS completion code - and this might be portable to csh if you are familiar with the differences between bash/tcsh.
On my ubuntu machine, there is also a section for SVN completion (in /etc/bash_completion) that doesn't seem to be present in the maintainer's archive.
That's not Ubuntu-specific behavior, it's the bash-completion project.
You could use that, if you can switch from tcsh to bash.