ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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').
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>
#include <stdlib.h>
int main() {
int current_char;
while ((current_char = fgetc(stdin)) != EOF)
{
printf("Enter something: ");
printf("You entered char #: %d\n", current_char);
}
return EXIT_SUCCESS;
}
But when I run it, press up, and then enter it spits out a string (note that I put like 30 seconds into this program, if there's a weird side effect I didn't notice or whatever you can read the Bash source code):
Code:
^[[A
Enter something: You entered char #: 27
Enter something: You entered char #: 91
Enter something: You entered char #: 65
Enter something: You entered char #: 10
Enter something: You entered char #: 10
hello
Enter something: You entered char #: 104
Enter something: You entered char #: 101
Enter something: You entered char #: 108
Enter something: You entered char #: 108
Enter something: You entered char #: 111
Enter something: You entered char #: 10
So my guess is you need to listen for the string ^[[A. You're listening for strings anyways, the trick here will likely be getting it to work without the user having to type enter to get their history back.
So my guess is you need to listen for the string ^[[A.
yeah, that's an escape sequence.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.