LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 11-11-2006, 01:33 PM   #1
studentlb
Member
 
Registered: Oct 2006
Posts: 53

Rep: Reputation: 15
Shell supporting Pipe


I m writing a Mini Shell that take a command from the user executes it and supports also I/O redirection + Piping
Due to all ur help, i was able to write the first part (execution) starting with the redirection But i m facing a big problem with the piping !!!!
Can anyone Help me with this part !!!
i m totally stucked, I need some advices, clues ....
Many Thanks
 
Old 11-12-2006, 07:50 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
"man popen" would be a good start.
 
Old 11-12-2006, 12:39 PM   #3
studentlb
Member
 
Registered: Oct 2006
Posts: 53

Original Poster
Rep: Reputation: 15
Well I know how to pipe
but the problem is that i donno the correct way to combine my program with the pipe :S
do i have to create child???
Honestly i m in the stage (not able to think) cz i did many researches and i m too confused :'(
 
Old 11-12-2006, 02:03 PM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by studentlb
Well I know how to pipe
So explain what you know, that would help understanding what you don't.
Quote:
but the problem is that i donno the correct way to combine my program with the pipe :S
popen is still my answer. Did you try it ?
Quote:
do i have to create child???
Hmm, did you even care reading the manual page I suggested ?
 
Old 11-13-2006, 11:32 AM   #5
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
I don't think you want popen(3); I'm pretty sure you're looking for pipe(2). And yes, you'll need to fork(2) off a process first. (I'm not revealing too much because this sounds like homework to me; I had to do this in college for a project.)
 
Old 11-13-2006, 01:06 PM   #6
studentlb
Member
 
Registered: Oct 2006
Posts: 53

Original Poster
Rep: Reputation: 15
yep it s true i m looking for pipe and i have created the prorgam for pipe,
Here is my problem
i m working on a shell that

1-takes a command & executes it
2-support redirection
3-piping


1-is working perfectly
i ve wriiten the redirection part and the piping part but i ve a problem joining them together :S
I need help in this part

in the first part,i ve created a parsing function that looks for the spaces and substitute it with 0 and then executes the command

i thought to do the same for pipe and redirection but i donno why i m facing too much problem

can u advise me how to solve this part ????
 
Old 11-13-2006, 03:57 PM   #7
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Normally when you say "redirection" you mean "getting from and sending to a file" using the shell operators <, >, and so on. When people say "pipe" they usually mean "sending output from one program as input to another" using the shell operator |. From what you say, this is what I understand you want; but these are operations that are performed independently. So what do you mean by "joining them together"? If you write the shell with the notions of input streams, output streams, files, and processes, you only need to handle the two operations because any glued-together interaction in the shell should be able to use the same code. Of course, you'd still have to check syntax to make sure that they don't do something like "cat alpha < beta > gamma | cat delta < epsilon > iota".
 
Old 11-13-2006, 04:50 PM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
This might help (includes code sample):
http://www.gnu.org/software/libc/man...o-a-Subprocess
 
Old 11-14-2006, 04:10 AM   #9
studentlb
Member
 
Registered: Oct 2006
Posts: 53

Original Poster
Rep: Reputation: 15
Well, what i need to implement is the following shell:

command1 [<input] [|command2 ] [>output]

what algorithm should i follow??can u help
 
Old 11-14-2006, 08:23 AM   #10
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Reading your previous posts, this looks very much like some project you're trying to do, and I guarantee that if that's the case, you'll get more out of it if you ask your professor and fellow classmates for help. I'm sorry, but I won't give you any straight answers, but I kinda feel bad, so I'll try to set you on the right track for how I would do it. Let's break it down functionally.

Code:
cmd,   pipe   :: Process
input, output :: File

rec func execStmt(cmd :opt input pipe output) =
  return doCmd(cmd, src, dest)
where
  src =
    if input = nil then stdin else openFile(input)
  dest =
    if pipe = nil then
      if output = nil then stdout else openFile(output)
    else
      execStmt(pipe, parseNextChunk())
The behavior of parseNextChunk() reads the input and breaks it up into tokens (and somehow magically by the power of this made-up language, transforms them into parameters). It only reads ahead as much as necessary to know what the next step in the shell statement execution is.

doCmd(cmd, src, dest) executes cmd, getting input from src and passing output to dest. doCmd(...) also produces an object that accepts input, so it can be chained recursively.

Last edited by taylor_venable; 11-14-2006 at 08:27 AM.
 
Old 11-14-2006, 08:27 AM   #11
studentlb
Member
 
Registered: Oct 2006
Posts: 53

Original Poster
Rep: Reputation: 15
Thanks a lot

I m working on z program
i need u to help me in the algorithm and not write the program
I will read ur post and try to figure it out
thanks again
by the way i m not registered in this course as u noticed i ve been asking for this 3 weeks ago, i don't have to submit it
I m just auditing and i m writing this program not to submit it to the instructor....

Last edited by studentlb; 11-14-2006 at 02:02 PM.
 
  


Reply



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
Shell script pipe input - bash mostly laikos Programming 4 11-09-2008 05:14 PM
Browser supporting php? Zeno McDohl Linux - Newbie 9 01-25-2006 06:48 PM
Why supporting Linux? Balage General 6 12-19-2004 02:39 PM
Textbrowsers supporting javascript ganninu Linux - Software 2 09-23-2003 12:56 PM
redhat 7.2 not supporting GUI archu Linux - Newbie 7 07-03-2003 12:51 PM

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

All times are GMT -5. The time now is 03:37 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