Shell implement in c
I am implementing a program called shell.c in which I have to write code for a basic shell.The problem is it must include history option of shell i.e. whenever I press up key it must show the last command.Could you please tell me how to detect that up key is pressed and what to do after that.Thanks in advance
|
Please do not post the same thread in more than one forum. Picking the most relevant forum and posting it once there makes it easier for other members to help you and keeps the discussion all in one place.
http://www.linuxquestions.org/rules.php |
Hi mate,
I am very sorry for the mistake .But I searched google for about 2 hours for the solution and couldnot get it .You can imagine how desperate I am. |
Seems like you could push each command entered as a line into a history file, and listen for command with the scanf call. Keep the last command in a buffer, and when scanf detects the "up" key entered (I hope that works, it might not) print out the string held in the buffer with a proceeding backspace character ('\b').
|
Hi there ..
The trouble lies with fact that I dont know how to detect the up button in linux. |
Ok I started to look into it and realised that it wouldn't be as simple as I had expected.
Using this program I was able to listen for char values (ascii, in essence): Code:
#include <stdio.h> Code:
^[[A |
you will probably need to put the terminal into
raw (unbuffered) mode. a.k.a. non -canonical. (I take it you don't want to press return) Then naturally life will get more difficult as you need to build up the line manually. man termio I've written some of this sort of stuff, but it's at home. Here's a shell script that should give some clue as to how to set up the termio struct. min and time make all the difference. Code:
!/bin/ksh |
Quote:
which (in this case) is 4 characters. ESCAPE [ [ A escape is '\033' so generally you get input a char at a time, and if you detect an '\033' it's going to be an escape sequence so then you need to collect the rest of the sequence (but not grab any more!!!!) It's terrific fun ;) |
Thanks a lot for all your help...It was really beneficial
specially destuxor's advice |
All times are GMT -5. The time now is 12:19 PM. |