LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-03-2005, 05:08 PM   #1
SatYr_84
LQ Newbie
 
Registered: Oct 2005
Posts: 5

Rep: Reputation: 0
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
 
Old 10-03-2005, 05:49 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
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
 
Old 10-03-2005, 05:53 PM   #3
SatYr_84
LQ Newbie
 
Registered: Oct 2005
Posts: 5

Original Poster
Rep: Reputation: 0
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.
 
Old 10-03-2005, 11:39 PM   #4
destuxor
Member
 
Registered: Oct 2005
Posts: 51

Rep: Reputation: 16
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').
 
Old 10-04-2005, 12:17 AM   #5
SatYr_84
LQ Newbie
 
Registered: Oct 2005
Posts: 5

Original Poster
Rep: Reputation: 0
Hi there ..
The trouble lies with fact that I dont know how to detect the up button in linux.
 
Old 10-04-2005, 02:24 PM   #6
destuxor
Member
 
Registered: Oct 2005
Posts: 51

Rep: Reputation: 16
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.
 
Old 10-05-2005, 05:01 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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

Last edited by bigearsbilly; 10-05-2005 at 05:13 AM.
 
Old 10-05-2005, 05:08 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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
 
Old 10-06-2005, 12:44 AM   #9
SatYr_84
LQ Newbie
 
Registered: Oct 2005
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks a lot for all your help...It was really beneficial
specially destuxor's advice
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to implement a function table dragondad Programming 4 09-16-2005 12:37 PM
How to implement Snapshot feature? geethu Linux - Software 2 07-27-2005 02:49 AM
How do I implement MIPS instructions into C or C++? hippo100 Programming 7 11-07-2004 11:51 AM
ho to implement a minishell (bash) twista Linux - Newbie 1 10-09-2004 02:50 AM
How to implement multithreading in Socket ??? ponka128 Programming 5 05-23-2004 10:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration