1 //===-- MICmnStreamStdinWindows.cpp -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 // File: MIUtilStreamStdin.cpp
13 // Overview: CMICmnStreamStdinWindows implementation.
15 // Environment: Compilers: Visual C++ 12.
16 // gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17 // Libraries: See MIReadmetxt.
22 // Third Party Headers:
23 #if defined( _MSC_VER )
28 #endif // defined( _MSC_VER )
32 #include "MICmnStreamStdinWindows.h"
34 #include "MICmnResources.h"
35 #include "MIUtilSystemWindows.h"
36 #include "MIUtilSingletonHelper.h"
38 //++ ------------------------------------------------------------------------------------
39 // Details: CMICmnStreamStdinWindows constructor.
45 CMICmnStreamStdinWindows::CMICmnStreamStdinWindows( void )
46 : m_constBufferSize( 1024 )
48 , m_pCmdBuffer( nullptr )
49 , m_pStdinBuffer( nullptr )
50 , m_nBytesToBeRead( 0 )
51 , m_bRunningInConsoleWin( false )
55 //++ ------------------------------------------------------------------------------------
56 // Details: CMICmnStreamStdinWindows destructor.
62 CMICmnStreamStdinWindows::~CMICmnStreamStdinWindows( void )
67 //++ ------------------------------------------------------------------------------------
68 // Details: Initialize resources for *this Stdin stream.
71 // Return: MIstatus::success - Functional succeeded.
72 // MIstatus::failure - Functional failed.
75 bool CMICmnStreamStdinWindows::Initialize( void )
78 return MIstatus::success;
80 bool bOk = MIstatus::success;
83 // Note initialisation order is important here as some resources depend on previous
84 MI::ModuleInit< CMICmnLog > ( IDS_MI_INIT_ERR_LOG , bOk, errMsg );
85 MI::ModuleInit< CMICmnResources >( IDS_MI_INIT_ERR_RESOURCES, bOk, errMsg );
87 // Other resources required
90 m_pCmdBuffer = new MIchar[ m_constBufferSize ];
93 #if MICONFIG_CREATE_OWN_STDIN_BUFFER
94 // Give stdinput a user defined buffer
95 m_pStdinBuffer = new char[ 1024 ];
96 ::setbuf( stdin, m_pStdinBuffer );
97 #endif // MICONFIG_CREATE_OWN_STDIN_BUFFER
99 // Clear error indicators for std input
102 #if defined( _MSC_VER )
103 m_bRunningInConsoleWin = ::_isatty( ::fileno( stdin ) );
104 #endif // #if defined( _MSC_VER )
107 m_bInitialized = bOk;
111 CMIUtilString strInitError( CMIUtilString::Format( MIRSRC( IDS_MI_INIT_ERR_OS_STDIN_HANDLER ), errMsg.c_str() ) );
112 SetErrorDescription( strInitError );
113 return MIstatus::failure;
116 return MIstatus::success;
119 //++ ------------------------------------------------------------------------------------
120 // Details: Release resources for *this Stdin stream.
123 // Return: MIstatus::success - Functional succeeded.
124 // MIstatus::failure - Functional failed.
127 bool CMICmnStreamStdinWindows::Shutdown( void )
129 if( !m_bInitialized )
130 return MIstatus::success;
132 m_bInitialized = false;
134 ClrErrorDescription();
136 bool bOk = MIstatus::success;
137 CMIUtilString errMsg;
140 if( m_pCmdBuffer != nullptr )
142 delete [] m_pCmdBuffer;
143 m_pCmdBuffer = nullptr;
147 #if MICONFIG_CREATE_OWN_STDIN_BUFFER
148 if ( m_pStdinBuffer )
149 delete [] m_pStdinBuffer;
150 m_pStdinBuffer = nullptr;
151 #endif // MICONFIG_CREATE_OWN_STDIN_BUFFER
153 // Note shutdown order is important here
154 MI::ModuleShutdown< CMICmnResources >( IDS_MI_INIT_ERR_RESOURCES, bOk, errMsg );
155 MI::ModuleShutdown< CMICmnLog > ( IDS_MI_INIT_ERR_LOG , bOk, errMsg );
159 SetErrorDescriptionn( MIRSRC( IDS_MI_SHTDWN_ERR_OS_STDIN_HANDLER ), errMsg.c_str() );
162 return MIstatus::success;
165 //++ ------------------------------------------------------------------------------------
166 // Details: Determine if stdin has any characters present in its buffer.
168 // Args: vwbAvail - (W) True = There is chars available, false = nothing there.
169 // Return: MIstatus::success - Functional succeeded.
170 // MIstatus::failure - Functional failed.
173 bool CMICmnStreamStdinWindows::InputAvailable( bool & vwbAvail )
175 return m_bRunningInConsoleWin ? InputAvailableConsoleWin( vwbAvail ) : InputAvailableApplication( vwbAvail );
178 //++ ------------------------------------------------------------------------------------
179 // Details: Determine if stdin has any characters present in its buffer. If running in a
180 // terminal use _kbhit().
182 // Args: vwbAvail - (W) True = There is chars available, false = nothing there.
183 // Return: MIstatus::success - Functional succeeded.
184 // MIstatus::failure - Functional failed.
187 bool CMICmnStreamStdinWindows::InputAvailableConsoleWin( bool & vwbAvail )
189 #if defined( _MSC_VER )
190 if( m_nBytesToBeRead == 0 )
192 // Get a windows handle to std input stream
193 HANDLE handle = ::GetStdHandle( STD_INPUT_HANDLE );
194 DWORD nBytesWaiting = ::_kbhit();
196 // Save the number of bytes to be read so that we can check if input is available to be read
197 m_nBytesToBeRead = nBytesWaiting;
199 // Return state of whether bytes are waiting or not
200 vwbAvail = (nBytesWaiting > 0);
202 #endif // #if defined( _MSC_VER )
204 return MIstatus::success;
207 //++ ------------------------------------------------------------------------------------
208 // Details: Determine if stdin has any characters present in its buffer.
210 // Args: vwbAvail - (W) True = There is chars available, false = nothing there.
211 // Return: MIstatus::success - Functional succeeded.
212 // MIstatus::failure - Functional failed.
215 bool CMICmnStreamStdinWindows::InputAvailableApplication( bool & vwbAvail )
217 #if defined( _MSC_VER )
218 if( m_nBytesToBeRead == 0 )
220 // Get a windows handle to std input stream
221 HANDLE handle = ::GetStdHandle( STD_INPUT_HANDLE );
222 DWORD nBytesWaiting = 0;
224 // Ask how many bytes are available
225 if( ::PeekNamedPipe( handle, nullptr, 0, nullptr, &nBytesWaiting, nullptr ) == FALSE )
227 // This can occur when the client i.e. Eclipse closes the stdin stream 'cause it deems its work is finished
228 // for that debug session. May be we should be handling SIGKILL somehow?
229 const CMIUtilString osErrMsg( CMIUtilSystemWindows().GetOSLastError().StripCRAll() );
230 SetErrorDescription( CMIUtilString::Format( MIRSRC( IDS_STDIN_ERR_CHKING_BYTE_AVAILABLE ), osErrMsg.c_str() ) );
231 return MIstatus::failure;
234 // Save the number of bytes to be read so that we can check if input is available to be read
235 m_nBytesToBeRead = nBytesWaiting;
237 // Return state of whether bytes are waiting or not
238 vwbAvail = (nBytesWaiting > 0);
240 #endif // #if defined( _MSC_VER )
242 return MIstatus::success;
245 //++ ------------------------------------------------------------------------------------
246 // Details: Wait on new line of data from stdin stream (completed by '\n' or '\r').
248 // Args: vwErrMsg - (W) Empty string ok or error description.
249 // Return: MIchar * - text buffer pointer or NULL on failure.
252 const MIchar * CMICmnStreamStdinWindows::ReadLine( CMIUtilString & vwErrMsg )
257 const MIchar * pText = ::fgets( &m_pCmdBuffer[ 0 ], m_constBufferSize, stdin );
258 if( pText == nullptr )
260 if( ::ferror( m_pStdin ) != 0 )
261 vwErrMsg = ::strerror( errno );
265 // Subtract the number of bytes read so that we can check if input is available to be read
266 m_nBytesToBeRead = m_nBytesToBeRead - ::strlen( pText );
268 // Strip off new line characters
269 for( MIchar * pI = m_pCmdBuffer; *pI != '\0'; pI++ )
271 if( (*pI == '\n') || (*pI == '\r') )