The solution (provided in Microsoft's documentation) is to add: #include <cstdio> #include <io.h> #include <fcntl.h> and run: int result = _setmode( _fileno(stdin), _O_BINARY ); if( result == -1 ) { std::cerr<<"Cannot set input mode to binary."<<std::endl; return 1; } result = _setmode( _fileno(stdout), _O_BINARY ); if( result == -1 ) { std::cerr<<"Cannot set output mode to binary."<<std::endl; return 1; } before using cin or cout. I'm not sure where to add this however, since it needs to be called before any reads or writes are done, but only needs to be called once. Any suggestions? At the moment in my own code, I'm adding them to the tools source files, since that's where it's determined that cin/cout can be used for input/output. ~Michael -----Original Message----- From: llvmdev-bounces at cs.uiuc.edu [mailto:llvmdev-bounces at cs.uiuc.edu] On Behalf Of Chris Lattner Sent: Monday, May 22, 2006 4:36 PM To: LLVM Developers Mailing List Subject: RE: [LLVMdev] Binary output to cout on Windows On Mon, 22 May 2006, Michael Smith wrote:> llvm-as interprets "-" as using standard output (cout), so llvm-as < > input.ll -o - | opt has the same behavior. You'll actually find a > comment on it in llvm-as.cpp, so I guess I shouldn't hold out hopethat> there's a good way to do it.Please see: http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.13 We'd welcome patches to do this, and we can localize this in our system support library, but you'd have to figure out the magic to make this happen on win32. -Chris
On Tue, 23 May 2006, Michael Smith wrote:> The solution (provided in Microsoft's documentation) is to add: > #include <cstdio> > #include <io.h> > #include <fcntl.h> > and run: > int result = _setmode( _fileno(stdin), _O_BINARY ); > if( result == -1 ) > { std::cerr<<"Cannot set input mode to binary."<<std::endl; return 1; > } > result = _setmode( _fileno(stdout), _O_BINARY ); > if( result == -1 ) > { std::cerr<<"Cannot set output mode to binary."<<std::endl; return 1; > } > before using cin or cout. I'm not sure where to add this however, since > it needs to be called before any reads or writes are done, but only > needs to be called once. Any suggestions? At the moment in my own code, > I'm adding them to the tools source files, since that's where it's > determined that cin/cout can be used for input/output.Reid is the guru here, but I'll inject my opinion. I think that this should be a method somewhere in the System library. Given that, you should change llvm-as.cpp (and all other tools with similar issues) from: } else { // Specified stdout // FIXME: cout is not binary! Out = &std::cout; } To: } else { // Specified stdout sys::ChangeStandardStreamToBinary(std::cout); Out = &std::cout; } ... where "ChangeStandardStreamToBinary" is something that Reid likes. :) The implementation of ChangeStandardStreamToBinary would do the code above in the win32 case, and would be a noop in the unix case. This is just one way to do it, I defer to Reid for what the "right" way to do it is :) -Chris> -----Original Message----- > From: llvmdev-bounces at cs.uiuc.edu [mailto:llvmdev-bounces at cs.uiuc.edu] > On Behalf Of Chris Lattner > Sent: Monday, May 22, 2006 4:36 PM > To: LLVM Developers Mailing List > Subject: RE: [LLVMdev] Binary output to cout on Windows > > On Mon, 22 May 2006, Michael Smith wrote: >> llvm-as interprets "-" as using standard output (cout), so llvm-as < >> input.ll -o - | opt has the same behavior. You'll actually find a >> comment on it in llvm-as.cpp, so I guess I shouldn't hold out hope > that >> there's a good way to do it. > > Please see: > http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.13 > > We'd welcome patches to do this, and we can localize this in our system > support library, but you'd have to figure out the magic to make this > happen on win32. > > -Chris > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >-Chris -- http://nondot.org/sabre/ http://llvm.org/
Hi Michael, The approach to setting binary mode varies from platform to platform. Thanks for providing the Win32 version. We will also need to work up a Unix version (with variants). The correct way to do this is to add a function to include/llvm/System/Program.h and then implement that function variantly for the different platforms. The function can then be called from the main function of whatever program wants its stdin/stdout to be binary. We can do this. Can you create a bug report with this code snippet in it so we can keep track of it? That way, I won't forget that this needs to be done. http://llvm.org/bugs Reid. On Tue, 2006-05-23 at 10:27 -0700, Michael Smith wrote:> The solution (provided in Microsoft's documentation) is to add: > #include <cstdio> > #include <io.h> > #include <fcntl.h> > and run: > int result = _setmode( _fileno(stdin), _O_BINARY ); > if( result == -1 ) > { std::cerr<<"Cannot set input mode to binary."<<std::endl; return 1; > } > result = _setmode( _fileno(stdout), _O_BINARY ); > if( result == -1 ) > { std::cerr<<"Cannot set output mode to binary."<<std::endl; return 1; > } > before using cin or cout. I'm not sure where to add this however, since > it needs to be called before any reads or writes are done, but only > needs to be called once. Any suggestions? At the moment in my own code, > I'm adding them to the tools source files, since that's where it's > determined that cin/cout can be used for input/output. > > ~Michael > > -----Original Message----- > From: llvmdev-bounces at cs.uiuc.edu [mailto:llvmdev-bounces at cs.uiuc.edu] > On Behalf Of Chris Lattner > Sent: Monday, May 22, 2006 4:36 PM > To: LLVM Developers Mailing List > Subject: RE: [LLVMdev] Binary output to cout on Windows > > On Mon, 22 May 2006, Michael Smith wrote: > > llvm-as interprets "-" as using standard output (cout), so llvm-as < > > input.ll -o - | opt has the same behavior. You'll actually find a > > comment on it in llvm-as.cpp, so I guess I shouldn't hold out hope > that > > there's a good way to do it. > > Please see: > http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.13 > > We'd welcome patches to do this, and we can localize this in our system > support library, but you'd have to figure out the magic to make this > happen on win32. > > -Chris > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev-------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20060523/09c7eba0/attachment.sig>
On Tue, 2006-05-23 at 13:12 -0500, Chris Lattner wrote:> Reid is the guru here, but I'll inject my opinion. I think that this > should be a method somewhere in the System library. Given that, you > should change llvm-as.cpp (and all other tools with similar issues) from: > > } else { // Specified stdout > // FIXME: cout is not binary! > Out = &std::cout; > } > > To: > > } else { // Specified stdout > sys::ChangeStandardStreamToBinary(std::cout); > Out = &std::cout; > } > > ... where "ChangeStandardStreamToBinary" is something that Reid likes. :) > > The implementation of ChangeStandardStreamToBinary would do the code above > in the win32 case, and would be a noop in the unix case. This is just one > way to do it, I defer to Reid for what the "right" way to do it is :) >Looks like we're on the same page here. Reid. -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20060523/a94d821e/attachment.sig>