Pipes- resetting in-stream

For questions related to the projects (only post pseudocode).
Moderator: Tracy Camp

Pipes- resetting in-stream

Postby ngerstle » Fri Oct 30, 2009 9:04 pm

I'm try to get the pipe functionality of my shell working correctly- Things pipe correctly between themselves, however, once a piped command is done, the standard input stream is screwed up.
The command completes, but then I can't read anything and keep getting null pointers instead of strings from an rl_gets().
I tried
Code: Select all
         int stdin = open("/dev/tty");
         dup2(stdin,0);

in hopes of pointing standard input back to the terminal, with no success.
Am I doing something wrong with my pipe, or is there some way I can reset the in-stream?
Thanks,
Nick
Last edited by ngerstle on Fri Oct 30, 2009 9:41 pm, edited 1 time in total.
ngerstle
Systems nut
Systems nut
 
Posts: 20
Joined: Thu Jan 08, 2009 10:39 pm

Re: Pipes- resetting in-stream

Postby TheaGab » Fri Oct 30, 2009 9:39 pm

First of all, this may be a little too much code for ore. Could you edit your post and replace with pseudo-code or a more general description of what you are doing (you can still describe what functions you are using or use example code, just don't pull chunks directly from your project).

Secondly, are you piping or doing redirection? You are opening a file descriptor, which makes it seem like redirection to a file. With pipes you'll be using the pipe() function not the open() function. Secondly, a pipe needs to be an int array of size 2, not a single int (like a file descriptor).

Also, you might not want to use the name stdin for your int. "stdin" is also of type FILE * and can be passed as an argument to fileno() (see man pages for fileno(3)). To avoid confusion I might pick another name.
User avatar
TheaGab
Code poet
Code poet
 
Posts: 56
Joined: Wed Oct 24, 2007 6:14 pm

Re: Pipes- resetting in-stream

Postby ngerstle » Fri Oct 30, 2009 9:48 pm

Well- I have the pipe going between two commands, but as soon as I try and grab anything after I've closed my pipes, I get
Code: Select all
terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_S_construct NULL not valid
Aborted

Apparently, when I call
Code: Select all
line=rl_gets();

(where char * line) after the piped commands, line is a null pointer (line=0).

The previous code was an attempt to reset standard-in so it didn't point at a closed pipe. I don't have any other idea what else could have happened.
Thanks,
Nick
ngerstle
Systems nut
Systems nut
 
Posts: 20
Joined: Thu Jan 08, 2009 10:39 pm

Re: Pipes- resetting in-stream

Postby TheaGab » Fri Oct 30, 2009 10:09 pm

Ah okay, I misunderstood what you were trying to do above. I'm still not sure why rl_gets() would be causing a problem. You might need to check not only if "line" is NULL
Code: Select all
if (!line)

as provided, but also if the length of "line" is 0 (you should allow the program to continue if you have an empty line). I don't know if that could be the problem?

If this doesn't work, you can send me your code by email and I can take a look at it tomorrow. I may need to see more of what you're doing with the piping to help.
User avatar
TheaGab
Code poet
Code poet
 
Posts: 56
Joined: Wed Oct 24, 2007 6:14 pm

Re: Pipes- resetting in-stream

Postby ngerstle » Fri Oct 30, 2009 10:20 pm

I can't check the length of the line because it isn't a proper string. if I do
Code: Select all
string(line).size()
it throws the error. As far as I can tell, the cause is that line is 0. not the size, but the pointer to the string is 0.
If I add a
Code: Select all
If(line==0)
to the program, it just goes into an infinite loop because that's always true.
I'll send my code- thanks a million.
Nick
ngerstle
Systems nut
Systems nut
 
Posts: 20
Joined: Thu Jan 08, 2009 10:39 pm

Re: Pipes- resetting in-stream

Postby TheaGab » Sat Oct 31, 2009 6:11 pm

Using the strlen() function will check for a null character terminating a character array. That's what you should use to check the length of "line" because it is of type char *.
User avatar
TheaGab
Code poet
Code poet
 
Posts: 56
Joined: Wed Oct 24, 2007 6:14 pm


Return to OS Projects

Who is online

Users browsing this forum: No registered users and 1 guest

cron