Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Restoring STDIN after redirection on command line ...: (6 Items)
   
Restoring STDIN after redirection on command line ...  
A bit of a newbie question, possibly.

I am trying to have a QNX 650 built utility that normally accepts its input from stdin but can be run with a script file
 with "utility < scriptfile" and then revert to input from the keyboard when it hits EOF.

What is the best way to do this ??

Currently, at EOF I close "stdin" and then fopen("/dev/tty", "r") - this works well for the debug serial port but not 
for telnet, etc.

TIA,
Bob
RE: Restoring STDIN after redirection on command line ...  
Hi Bob,

I'm not sure if there is a really clean solution to your problem.

To achieve the desired behavior, you would need to:
- find out which device should become your new stdin 
  (as you noticed, /dev/tty isn't necessarily the right choice), and
- redirect the stream 'stdin' to take its input from that device.

Finding the appropriate device may be anywhere between not too hard 
and impossible. In the easy case, stdout or stderr refer to the TTY 
you want to use. In the worst case, they have been redirected to 
files or closed entirely, and your process has detached from its 
controlling terminal (i.e., become a daemon) - in this scenario, 
there'd be no reasonable guess which device to attach to.

Let's assume a simple case - stdout refers to a TTY.

You could of course just call  dup2( 1, 0 )  to duplicate the 
stdout file descriptor and have the duplicate your current stdin 
fd. The problem is that the (FILE) stream object represented by 
*stdin will not be aware of this change. There is the  fdopen()  
function to wrap a FILE object around an existing file descriptor, 
but that will allocate a new object, not use an existing one...
...and alas, *stdin/-out/-err are static _objects_ - so we cannot 
merely replace stdin by a new pointer. 

You could try to flush and rewind the stdin stream, then 
call  dup2( 1, 0 )  to replace the stdin file descriptor.

I'd suggest, though, to provide your input file via a command line 
argument and have a few custom input functions that will read either 
from that file or, once it reached EOF, the original stdin.

Cheers,
Thomas

-----Original Message-----
From: Bob Selby [mailto:robert.selby@snellgroup.com] 
Sent: Mittwoch, 2. Mai 2012 10:46
To: ostech-core_os
Subject: Restoring STDIN after redirection on command line ...

A bit of a newbie question, possibly.

I am trying to have a QNX 650 built utility that normally accepts its input from stdin but can be run with a script file
 with "utility < scriptfile" and then revert to input from the keyboard when it hits EOF.

What is the best way to do this ??

Currently, at EOF I close "stdin" and then fopen("/dev/tty", "r") - this works well for the debug serial port but not 
for telnet, etc.

TIA,
Bob




_______________________________________________

OSTech
http://community.qnx.com/sf/go/post92871
To cancel your subscription to this discussion, please e-mail ostech-core_os-unsubscribe@community.qnx.com
RE: Restoring STDIN after redirection on command line ...  
Thanks for that :-)

I tried the following for "util < scriptfile" and it seems to do what I
want ...

-----------------------------------------------------
FILE * fc = stdin;
...
fgets(fc, ... // Reads from redirected script file
...
On EOF I do ...
fclose(fc);
fc = fdopen(STDOUT_FILENO, "r") ) == NULL )
...
fgets(fc, ... // Reads from keyboard
...
-----------------------------------------------------



Bob Selby
Senior Engineer, Software

Snell
Innovation in the 
Multi-Screen World
 
T +44 (0) 118 921 4592 (direct)
T +44 (0) 118 986 6123 (switchboard)
F +44 (0) 118 975 4920

snellgroup.com
 
Hartman House, Danehill, Lower Earley, Reading, Berkshire, RG6 4PB  UK

Routing | Modular Infrastructure | Conversion & Restoration | Live
Production | Automation & Media Management | Control & Monitoring




-----Original Message-----
From: Thomas Haupt [mailto:thaupt@qnx.com] 
Sent: 02 May 2012 11:24
To: 'ostech-core_os@community.qnx.com'
Subject: RE: Restoring STDIN after redirection on command line ...

Hi Bob,

I'm not sure if there is a really clean solution to your problem.

To achieve the desired behavior, you would need to:
- find out which device should become your new stdin
  (as you noticed, /dev/tty isn't necessarily the right choice), and
- redirect the stream 'stdin' to take its input from that device.

Finding the appropriate device may be anywhere between not too hard and
impossible. In the easy case, stdout or stderr refer to the TTY you want
to use. In the worst case, they have been redirected to files or closed
entirely, and your process has detached from its controlling terminal
(i.e., become a daemon) - in this scenario, there'd be no reasonable
guess which device to attach to.

Let's assume a simple case - stdout refers to a TTY.

You could of course just call  dup2( 1, 0 )  to duplicate the stdout
file descriptor and have the duplicate your current stdin fd. The
problem is that the (FILE) stream object represented by *stdin will not
be aware of this change. There is the  fdopen() function to wrap a FILE
object around an existing file descriptor, but that will allocate a new
object, not use an existing one...
...and alas, *stdin/-out/-err are static _objects_ - so we cannot merely
replace stdin by a new pointer. 

You could try to flush and rewind the stdin stream, then call  dup2( 1,
0 )  to replace the stdin file descriptor.

I'd suggest, though, to provide your input file via a command line
argument and have a few custom input functions that will read either
from that file or, once it reached EOF, the original stdin.

Cheers,
Thomas

-----Original Message-----
From: Bob Selby [mailto:robert.selby@snellgroup.com]
Sent: Mittwoch, 2. Mai 2012 10:46
To: ostech-core_os
Subject: Restoring STDIN after redirection on command line ...

A bit of a newbie question, possibly.

I am trying to have a QNX 650 built utility that normally accepts its
input from stdin but can be run with a script file with "utility <
scriptfile" and then revert to input from the keyboard when it hits EOF.

What is the best way to do this ??

Currently, at EOF I close "stdin" and then fopen("/dev/tty", "r") - this
works well for the debug serial port but not for telnet, etc.

TIA,
Bob




_______________________________________________

OSTech
http://community.qnx.com/sf/go/post92871
To cancel your subscription to this discussion, please e-mail
ostech-core_os-unsubscribe@community.qnx.com




_______________________________________________

OSTech
http://community.qnx.com/sf/go/post92875
To cancel your subscription to this discussion, please e-mail
ostech-core_os-unsubscribe@community.qnx.com

This email and any attachments are confidential, may be legally privileged and are intended for the use of the addressee
 only. If you are not the intended recipient, please note that any use, disclosure, printing or copying of...
View Full Message
RE: Restoring STDIN after redirection on command line ...  
Hi Bob,

that looks mostly reasonable. I'd still add a 'dup' or 'dup2', 
though, to have separate file descriptors for stdin and stdout.

Cheers,
Thomas

-----Original Message-----
From: Robert Selby [mailto:Robert.Selby@snellgroup.com] 
Sent: Mittwoch, 2. Mai 2012 13:12
To: ostech-core_os@community.qnx.com
Subject: RE: Restoring STDIN after redirection on command line ...

Thanks for that :-)

I tried the following for "util < scriptfile" and it seems to do what I want ...

-----------------------------------------------------
FILE * fc = stdin;
...
fgets(fc, ... // Reads from redirected script file ...
On EOF I do ...
fclose(fc);
fc = fdopen(STDOUT_FILENO, "r") ) == NULL ) ...
fgets(fc, ... // Reads from keyboard
...
-----------------------------------------------------



Bob Selby
Senior Engineer, Software

Snell
Innovation in the
Multi-Screen World
 
T +44 (0) 118 921 4592 (direct)
T +44 (0) 118 986 6123 (switchboard)
F +44 (0) 118 975 4920

snellgroup.com
 
Hartman House, Danehill, Lower Earley, Reading, Berkshire, RG6 4PB  UK

Routing | Modular Infrastructure | Conversion & Restoration | Live Production | Automation & Media Management | Control & Monitoring




-----Original Message-----
From: Thomas Haupt [mailto:thaupt@qnx.com]
Sent: 02 May 2012 11:24
To: 'ostech-core_os@community.qnx.com'
Subject: RE: Restoring STDIN after redirection on command line ...

Hi Bob,

I'm not sure if there is a really clean solution to your problem.

To achieve the desired behavior, you would need to:
- find out which device should become your new stdin
  (as you noticed, /dev/tty isn't necessarily the right choice), and
- redirect the stream 'stdin' to take its input from that device.

Finding the appropriate device may be anywhere between not too hard and impossible. In the easy case, stdout or stderr refer to the TTY you want to use. In the worst case, they have been redirected to files or closed entirely, and your process has detached from its controlling terminal (i.e., become a daemon) - in this scenario, there'd be no reasonable guess which device to attach to.

Let's assume a simple case - stdout refers to a TTY.

You could of course just call  dup2( 1, 0 )  to duplicate the stdout file descriptor and have the duplicate your current stdin fd. The problem is that the (FILE) stream object represented by *stdin will not be aware of this change. There is the  fdopen() function to wrap a FILE object around an existing file descriptor, but that will allocate a new object, not use an existing one...
...and alas, *stdin/-out/-err are static _objects_ - so we cannot merely replace stdin by a new pointer. 

You could try to flush and rewind the stdin stream, then call  dup2( 1,
0 )  to replace the stdin file descriptor.

I'd suggest, though, to provide your input file via a command line argument and have a few custom input functions that will read either from that file or, once it reached EOF, the original stdin.

Cheers,
Thomas

-----Original Message-----
From: Bob Selby [mailto:robert.selby@snellgroup.com]
Sent: Mittwoch, 2. Mai 2012 10:46
To: ostech-core_os
Subject: Restoring STDIN after redirection on command line ...

A bit of a newbie question, possibly.

I am trying to have a QNX 650 built utility that normally accepts its input from stdin but can be run with a script file with "utility < scriptfile" and then revert to input from the keyboard when it hits EOF.


What is the best way to do this ??

Currently, at EOF I close "stdin" and then fopen("/dev/tty", "r") - this works well for the debug serial port but not 
for telnet, etc.

TIA,
Bob




_______________________________________________

OSTech
http://community.qnx.com/sf/go/post92871
To cancel your subscription to this discussion, please e-mail...
View Full Message
RE: Restoring STDIN after redirection on command line ...  
Very true ...

fc = fdopen(dup2(1, 0), "r");

seems to work OK too :-)

Thanks very much for the help :-)

Bob Selby
Senior Engineer, Software

Snell
Innovation in the 
Multi-Screen World
 
T +44 (0) 118 921 4592 (direct)
T +44 (0) 118 986 6123 (switchboard)
F +44 (0) 118 975 4920

snellgroup.com
 
Hartman House, Danehill, Lower Earley, Reading, Berkshire, RG6 4PB  UK

Routing | Modular Infrastructure | Conversion & Restoration | Live
Production | Automation & Media Management | Control & Monitoring




-----Original Message-----
From: Thomas Haupt [mailto:thaupt@qnx.com] 
Sent: 02 May 2012 12:29
To: 'ostech-core_os@community.qnx.com'
Subject: RE: Restoring STDIN after redirection on command line ...

Hi Bob,

that looks mostly reasonable. I'd still add a 'dup' or 'dup2', though,
to have separate file descriptors for stdin and stdout.

Cheers,
Thomas

-----Original Message-----
From: Robert Selby [mailto:Robert.Selby@snellgroup.com]
Sent: Mittwoch, 2. Mai 2012 13:12
To: ostech-core_os@community.qnx.com
Subject: RE: Restoring STDIN after redirection on command line ...

Thanks for that :-)

I tried the following for "util < scriptfile" and it seems to do what I
want ...

-----------------------------------------------------
FILE * fc = stdin;
...
fgets(fc, ... // Reads from redirected script file ...
On EOF I do ...
fclose(fc);
fc = fdopen(STDOUT_FILENO, "r") ) == NULL ) ...
fgets(fc, ... // Reads from keyboard
...
-----------------------------------------------------



Bob Selby
Senior Engineer, Software

Snell
Innovation in the
Multi-Screen World
 
T +44 (0) 118 921 4592 (direct)
T +44 (0) 118 986 6123 (switchboard)
F +44 (0) 118 975 4920

snellgroup.com
 
Hartman House, Danehill, Lower Earley, Reading, Berkshire, RG6 4PB  UK

Routing | Modular Infrastructure | Conversion & Restoration | Live
Production | Automation & Media Management | Control & Monitoring




-----Original Message-----
From: Thomas Haupt [mailto:thaupt@qnx.com]
Sent: 02 May 2012 11:24
To: 'ostech-core_os@community.qnx.com'
Subject: RE: Restoring STDIN after redirection on command line ...

Hi Bob,

I'm not sure if there is a really clean solution to your problem.

To achieve the desired behavior, you would need to:
- find out which device should become your new stdin
  (as you noticed, /dev/tty isn't necessarily the right choice), and
- redirect the stream 'stdin' to take its input from that device.

Finding the appropriate device may be anywhere between not too hard and
impossible. In the easy case, stdout or stderr refer to the TTY you want
to use. In the worst case, they have been redirected to files or closed
entirely, and your process has detached from its controlling terminal
(i.e., become a daemon) - in this scenario, there'd be no reasonable
guess which device to attach to.

Let's assume a simple case - stdout refers to a TTY.

You could of course just call  dup2( 1, 0 )  to duplicate the stdout
file descriptor and have the duplicate your current stdin fd. The
problem is that the (FILE) stream object represented by *stdin will not
be aware of this change. There is the  fdopen() function to wrap a FILE
object around an existing file descriptor, but that will allocate a new
object, not use an existing one...
...and alas, *stdin/-out/-err are static _objects_ - so we cannot merely
replace stdin by a new pointer. 

You could try to flush and rewind the stdin stream, then call  dup2( 1,
0 )  to replace the stdin file descriptor.

I'd suggest, though, to provide your input file via a command line
argument and have a few custom input functions that will read either
from that file or, once it reached EOF, the original stdin.

Cheers,
Thomas

-----Original Message-----
From: Bob Selby [mailto:robert.selby@snellgroup.com]
Sent: Mittwoch, 2. Mai 2012 10:46
To: ostech-core_os
Subject: Restoring STDIN after redirection on...
View Full Message
RE: Restoring STDIN after redirection on command line ...  
Glad to help. :)

- Thomas

-----Original Message-----
From: Robert Selby [mailto:Robert.Selby@snellgroup.com] 
Sent: Mittwoch, 2. Mai 2012 13:34
To: ostech-core_os@community.qnx.com
Subject: RE: Restoring STDIN after redirection on command line ...

Very true ...

fc = fdopen(dup2(1, 0), "r");

seems to work OK too :-)

Thanks very much for the help :-)

Bob Selby
Senior Engineer, Software

Snell
Innovation in the
Multi-Screen World
 
T +44 (0) 118 921 4592 (direct)
T +44 (0) 118 986 6123 (switchboard)
F +44 (0) 118 975 4920

snellgroup.com
 
Hartman House, Danehill, Lower Earley, Reading, Berkshire, RG6 4PB  UK

Routing | Modular Infrastructure | Conversion & Restoration | Live Production | Automation & Media Management | Control & Monitoring




-----Original Message-----
From: Thomas Haupt [mailto:thaupt@qnx.com]
Sent: 02 May 2012 12:29
To: 'ostech-core_os@community.qnx.com'
Subject: RE: Restoring STDIN after redirection on command line ...

Hi Bob,

that looks mostly reasonable. I'd still add a 'dup' or 'dup2', though, to have separate file descriptors for stdin and stdout.

Cheers,
Thomas

-----Original Message-----
From: Robert Selby [mailto:Robert.Selby@snellgroup.com]
Sent: Mittwoch, 2. Mai 2012 13:12
To: ostech-core_os@community.qnx.com
Subject: RE: Restoring STDIN after redirection on command line ...

Thanks for that :-)

I tried the following for "util < scriptfile" and it seems to do what I want ...

-----------------------------------------------------
FILE * fc = stdin;
...
fgets(fc, ... // Reads from redirected script file ...
On EOF I do ...
fclose(fc);
fc = fdopen(STDOUT_FILENO, "r") ) == NULL ) ...
fgets(fc, ... // Reads from keyboard
...
-----------------------------------------------------



Bob Selby
Senior Engineer, Software

Snell
Innovation in the
Multi-Screen World
 
T +44 (0) 118 921 4592 (direct)
T +44 (0) 118 986 6123 (switchboard)
F +44 (0) 118 975 4920

snellgroup.com
 
Hartman House, Danehill, Lower Earley, Reading, Berkshire, RG6 4PB  UK

Routing | Modular Infrastructure | Conversion & Restoration | Live Production | Automation & Media Management | Control 
& Monitoring




-----Original Message-----
From: Thomas Haupt [mailto:thaupt@qnx.com]
Sent: 02 May 2012 11:24
To: 'ostech-core_os@community.qnx.com'
Subject: RE: Restoring STDIN after redirection on command line ...

Hi Bob,

I'm not sure if there is a really clean solution to your problem.

To achieve the desired behavior, you would need to:
- find out which device should become your new stdin
  (as you noticed, /dev/tty isn't necessarily the right choice), and
- redirect the stream 'stdin' to take its input from that device.

Finding the appropriate device may be anywhere between not too hard and impossible. In the easy case, stdout or stderr 
refer to the TTY you want to use. In the worst case, they have been redirected to files or closed entirely, and your 
process has detached from its controlling terminal (i.e., become a daemon) - in this scenario, there'd be no reasonable 
guess which device to attach to.

Let's assume a simple case - stdout refers to a TTY.

You could of course just call  dup2( 1, 0 )  to duplicate the stdout file descriptor and have the duplicate your current
 stdin fd. The problem is that the (FILE) stream object represented by *stdin will not be aware of this change. There is
 the  fdopen() function to wrap a FILE object around an existing file descriptor, but that will allocate a new object, 
not use an existing one...
...and alas, *stdin/-out/-err are static _objects_ - so we cannot merely replace stdin by a new pointer. 

You could try to flush and rewind the stdin stream, then call  dup2( 1,
0 )  to replace the stdin file descriptor.

I'd suggest, though, to provide your input file via a command line argument and have a few custom input functions that 
will read either from that file or, once it...
View Full Message