tags:

views:

99

answers:

3

I am implementing my own shell. But to supprt for command history, I need to use monitor up/down arrow key as in standard shells. Please tell me how to handle arrow keys as input or does these keys generate signals? Please elaborate.

+2  A: 

Arrows and other special keys send you special strings that depend on the terminal being used or emulated. To deal with this most simply, you could use a library such as termcap. Even simpler, given your stated purpose (command history support), would be to use readline, which basically does it for you (and lets the user customize aspects of their preferred mode of working across the many applications that link to the same library).

Alex Martelli
Is there anything that we can do without using these libraries means if in my program, can I test for these keys as input?
avd
You can reinvent the wheel but do you want to spend that much time on a minor aspect of your shell? readline is exactly what you want but it is GPL not LGPL if that is an issue for you.
Duck
I am doing as part of a course assignment in which I am not allowed to use other external librraies that why I need.
avd
I suppose you should check with your prof but I wouldn't consider termcap a lib so much as a system resource. It is part of every unix. readline is a different story.
Duck
You should use `terminfo` rather than `termcap`.
Dennis Williamson
A: 

Does the assignment specifically say you need to have a "cursor key driven" command history?

Simple option is to mimic the shells fc e.g.

$ ls
... file listing ...
$ fc -l
1 ls
2 fc -l
$ fc -r 1
... file listing ...

and (while I'm presenting established ideas as my own, might as well go all the way) to be able to edit the command line you could use

fc -e start end

to write the history from start to end into a file, launch an editor and then execute the resulting file as a script. This way your shell is not using a library, but launching commands and executing scripts, which is what shells are supposed to do anyway.

Marko Teiste
You have not understood the question well enough.
Alex Xander
That is why it started "Does the assignment specifically say" :) It would not be the first time somebody is given an assingment, they make they own assumption and then ask a question based on the assumption even though a simpler solution would've been enough, considering that use of readline (the obvious solution) was ruled out by "you are not allowed to use external libraries" :)
Marko Teiste
+1  A: 

It depends on how gnarly you're expected to go. Worse case, you're writing the keyboard interrupt handler. Best case, something like readline.

Check with your prof for direction here. Also check your course materials to see if the prof has given links/examples regarding this.

Paul Nathan