views:

174

answers:

3

I have implemented a simple linux shell in c. Now, I am adding some features and one I immediately thought about was to be able to show the last commands with the up arrow.

Question 1:
However, I have no idea how to accomplish this. Do you?

Question 2:
Any comment on how to store the "history" commands are also appreciated. I suppose something like a queue which allows access to all elements would be a good idea. Am I wrong? Do I have to implement it or is there already some good implementation out there I should know about?

Thanks.

+4  A: 

Build libedit or readline support into your shell.

Ignacio Vazquez-Abrams
Thanks. That's a very helpful link. I am looking for simpler, implementation, though.
nunos
Info about integrating readline history: http://tiswww.case.edu/php/chet/readline/history.html#SEC6
dkamins
+1  A: 

If you want to be lazy, you can use rlwrap:

rlwrap prog
R Samuel Klatchko
I don't want to be that lazy. I don't know if I will be able to do it, but I am looking for a solution that requires me doing the programming rather than just going to find the solution elsewhere. Thanks for the info, though.
nunos
A: 

I wrote the shell for HelenOS. Grab the bzr repo and navigate to uspace/app/bdsh (bdsh stands for the (b)rain (d)ead (sh)ell).

Other contributors have since added line editing / history / tab completion to the functions that handle input. Its written purely in ANSI C, does not link against glibc and implements its own functions. The code (both in the shell and underlying HelenOS libc) is 3 clause BSD, you can use it in anything.

If nothing else, it might help to just examine the implementation to get started.

Tim Post