LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell implement in c (https://www.linuxquestions.org/questions/programming-9/shell-implement-in-c-369489/)

SatYr_84 10-03-2005 05:08 PM

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

acid_kewpie 10-03-2005 05:49 PM

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

SatYr_84 10-03-2005 05:53 PM

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.

destuxor 10-03-2005 11:39 PM

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').

SatYr_84 10-04-2005 12:17 AM

Hi there ..
The trouble lies with fact that I dont know how to detect the up button in linux.

destuxor 10-04-2005 02:24 PM

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.

bigearsbilly 10-05-2005 05:01 AM

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
#$Id: kbhit,v 1.3 2005/02/16 13:10:59 billym Exp $
#$Log: kbhit,v $
#Revision 1.3  2005/02/16 13:10:59  billym
#x
#
#Revision 1.2  2004/12/02 15:13:04  billym
#safe
#
#Revision 1.1  2004/12/02 13:49:51  billym
#shell script kbhit
#
#
#set -o nounset

trap cleanup EXIT HUP TERM

save=`stty -g`
count=0

cleanup()
{
    stty $save
    echo '\033c'
}

get_key()
{
    2>/dev/null dd bs=10 count=1
}

ttyset()
{
# if min is zero it won't wait
# if min is 1 it waits for 1 char etc.
#
#
# if it's zero it doesn't wait if it's non-zero
# it returns the minimum amount
   
stty    cs8 -icanon igncr \
            min 1\
            time 0\
            -echo isig \
            -inpck -opost
}

ESC=


echo '\033c'
echo "\n\nPress a key (q = quit)"
ttyset
while true ;do
    x=`get_key`
    count=`expr $count + 1`
    [ "$x" = q ] && exit
    #[ "$x" = $ESC ] && break

    echo '\033[H' "$count\033[10H\033[1K got"
    print  -n "$x"|od -c
done
print  -n "$x"|od -c


bigearsbilly 10-05-2005 05:08 AM

Quote:

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 ;)

SatYr_84 10-06-2005 12:44 AM

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.