This is the mail archive of the
ecos-discuss@sources.redhat.com
mailing list for the eCos project.
RE: Change output stream for printf
- From: Gary Thomas <gary at mlbassoc dot com>
- To: Nicolas Brouard <nicolas dot brouard at silicomp dot ca>
- Cc: Nick Garnett <nickg at ecoscentric dot com>,Andrew Lunn <andrew at lunn dot ch>,eCos Discussion <ecos-discuss at ecos dot sourceware dot org>
- Date: Tue, 16 Nov 2004 09:25:37 -0700
- Subject: RE: [ECOS] Change output stream for printf
- Organization: MLB Associates
- References: <MFENKKFPNAKOOKBAEHLHGEIECEAA.nicolas.brouard@silicomp.ca>
On Tue, 2004-11-16 at 09:18, Nicolas Brouard wrote:
> Hi,
> I would like to use the tty driver (specially the CYG_TTY_OUT_FLAGS_CRLF
> functionnality) over a socket.
> When I use serial port for printf, I have this simplified callgraph:
> printf
> -> puts (stdioinlines.cxx)
> -> fputs (fputs.cxx)
> -> StdioStream::write (stream.cxx)
> -> write (io.cxx)
> -> dev_fo_write (devfs.cxx)
> -> tty_write (tty.c)
> -> serial_write (serial.c)
>
> With the socket (after dup2(socket_descriptor, stdout_no)), I have this
> simplified callgraph:
> printf
> -> puts (stdioinlines.cxx)
> -> fputs (fputs.cxx)
> -> StdioStream::write (stream.cxx)
> -> write (io.cxx)
> -> bsd_write (sockio.c)
>
> And I would like to use tty. So how can I have that?
> printf
> -> puts (stdioinlines.cxx)
> -> fputs (fputs.cxx)
> -> StdioStream::write (stream.cxx)
> -> write (io.cxx)
> => dev_fo_write (devfs.cxx)
> => tty_write (tty.c)
> -> bsd_write (sockio.c)
Why would you want to do this? It would require pseudo-tty (pty)
devices, which are not implemented in eCos (and IMO, need not be)
>
> Another question: I don't find in the sources where is defined printf.
> I found printf(const char *format, ...) which calls vfnprintf, but I didn't
> find the printf which calls puts. In fact, when I call printf with one
> argument, the second form is used. Where is this defined?
This is GCC - recent versions will turn certain uses of printf() into
calls to puts() instead, presumably as an optimization.
>
> Thanks in advance.
>
> Nicolas Brouard
>
> -----Original Message-----
> From: Nicolas Brouard [mailto:nicolas.brouard@silicomp.ca]
> Sent: Monday, November 15, 2004 2:22 PM
> To: Nick Garnett; Andrew Lunn
> Cc: eCos Discussion
> Subject: RE: [ECOS] Change output stream for printf
>
>
> Thank you very much.
> The redirection (or pipe) mechanism of *NIX works !
> This is my code:
> // Create a socket
> socket(...);
> bind(...);
> listen(...);
> int socket_descriptor = accept(...);
>
> // Duplicate socket_descriptor to stdout descriptor
> int stdout_no = fileno(stdout);
> close(stdout_no);
> dup2(socket_descriptor, stdout_no); // dup(socket_descriptor) works as well
>
> printf("This is printed over the socket\n");
> diag_printf("This is printed over serial port\n");
>
> Nicolas Brouard
>
> -----Original Message-----
> From: Nick Garnett [mailto:nickg@ecoscentric.com]
> Sent: Monday, November 15, 2004 1:51 PM
> To: Andrew Lunn
> Cc: Nicolas Brouard; eCos Discussion
> Subject: Re: [ECOS] Change output stream for printf
>
>
> Andrew Lunn <andrew@lunn.ch> writes:
>
> > > Another approach would be to use fileno(stdout) to get the file
> > > descriptor underneath stdout (it should be 1). close() it and then
> > > dup2() the socket into that descriptor. This is how it is done in *NIX
> > > and it should work in eCos. You may need to do a bit of business to
> > > flush any data still in the stream buffers.
> >
> > I seemed to remember something about this not working, which i why i
> > did not suggest it. Now you have mentioned it, i went googling. Its
> > probably worth reading the thread:
> >
> > http://sources.redhat.com/ml/ecos-discuss/2003-04/msg00135.html
>
> Yes, I recall that not working as expected. Which is why my approach
> manipulates the file descriptors underneath the C library, rather than
> mess with the C library itself. If this is done early on in the
> program, before any output is generated, it is logically equivalent to
> redirecting or pipelining a *NIX application in the shell.
>
> --
> Nick Garnett eCos Kernel Architect
> http://www.ecoscentric.com/ The eCos and RedBoot experts
>
>
>
>
> --
> Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
> and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss
--
Gary Thomas <gary@mlbassoc.com>
MLB Associates
--
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss