]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MICmnStreamStdinWindows.cpp
Merge ^/head r274961 through r275386.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / tools / lldb-mi / MICmnStreamStdinWindows.cpp
1 //===-- MICmnStreamStdinWindows.cpp -----------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 //++
11 // File:                MIUtilStreamStdin.cpp
12 //
13 // Overview:    CMICmnStreamStdinWindows implementation.
14 //
15 // Environment: Compilers:      Visual C++ 12.
16 //                                                      gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17 //                              Libraries:      See MIReadmetxt. 
18 //
19 // Copyright:   None.
20 //--
21
22 // Third Party Headers:
23 #if defined( _MSC_VER )
24 #include <stdio.h>
25 #include <Windows.h>
26 #include <io.h>
27 #include <conio.h>
28 #endif // defined( _MSC_VER )
29 #include <string.h>
30
31 // In-house headers:
32 #include "MICmnStreamStdinWindows.h"
33 #include "MICmnLog.h"
34 #include "MICmnResources.h"
35 #include "MIUtilSystemWindows.h"
36 #include "MIUtilSingletonHelper.h"
37
38 //++ ------------------------------------------------------------------------------------
39 // Details:     CMICmnStreamStdinWindows constructor.
40 // Type:        Method.
41 // Args:        None.
42 // Return:      None.
43 // Throws:      None.
44 //--
45 CMICmnStreamStdinWindows::CMICmnStreamStdinWindows( void )
46 :       m_constBufferSize( 1024 )
47 ,       m_pStdin( nullptr )
48 ,       m_pCmdBuffer( nullptr )
49 ,       m_pStdinBuffer( nullptr )
50 ,       m_nBytesToBeRead( 0 )
51 ,       m_bRunningInConsoleWin( false )
52 {
53 }
54
55 //++ ------------------------------------------------------------------------------------
56 // Details:     CMICmnStreamStdinWindows destructor.
57 // Type:        Overridable.
58 // Args:        None.
59 // Return:      None.
60 // Throws:      None.
61 //--
62 CMICmnStreamStdinWindows::~CMICmnStreamStdinWindows( void )
63 {
64         Shutdown();
65 }
66
67 //++ ------------------------------------------------------------------------------------
68 // Details:     Initialize resources for *this Stdin stream.
69 // Type:        Method.
70 // Args:        None.
71 // Return:      MIstatus::success - Functional succeeded.
72 //                      MIstatus::failure - Functional failed.
73 // Throws:      None.
74 //--
75 bool CMICmnStreamStdinWindows::Initialize( void )
76 {
77         if( m_bInitialized )
78                 return MIstatus::success;
79
80         bool bOk = MIstatus::success;
81         CMIUtilString errMsg;
82  
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 );
86
87         // Other resources required
88         if( bOk )
89         {
90                 m_pCmdBuffer = new MIchar[ m_constBufferSize ];
91                 m_pStdin = stdin;
92
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
98
99                 // Clear error indicators for std input
100                 ::clearerr( stdin );
101
102 #if defined( _MSC_VER )
103                 m_bRunningInConsoleWin = ::_isatty( ::fileno( stdin ) );
104 #endif // #if defined( _MSC_VER )
105         }
106
107         m_bInitialized = bOk;
108
109         if( !bOk )
110         {
111                 CMIUtilString strInitError( CMIUtilString::Format( MIRSRC( IDS_MI_INIT_ERR_OS_STDIN_HANDLER ), errMsg.c_str() ) );
112                 SetErrorDescription( strInitError );
113                 return MIstatus::failure;
114         }
115
116         return  MIstatus::success;
117 }
118
119 //++ ------------------------------------------------------------------------------------
120 // Details:     Release resources for *this Stdin stream.
121 // Type:        Method.
122 // Args:        None.
123 // Return:      MIstatus::success - Functional succeeded.
124 //                      MIstatus::failure - Functional failed.
125 // Throws:      None.
126 //--
127 bool CMICmnStreamStdinWindows::Shutdown( void )
128 {
129         if( !m_bInitialized )
130                 return MIstatus::success;
131
132         m_bInitialized = false;
133
134         ClrErrorDescription();
135
136         bool bOk = MIstatus::success;
137         CMIUtilString errMsg;
138
139         // Tidy up
140         if( m_pCmdBuffer != nullptr )
141         {
142                 delete [] m_pCmdBuffer;
143                 m_pCmdBuffer = nullptr;
144         }
145         m_pStdin = nullptr;
146
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
152
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 );
156
157         if( !bOk )
158         {
159                 SetErrorDescriptionn( MIRSRC( IDS_MI_SHTDWN_ERR_OS_STDIN_HANDLER ), errMsg.c_str() );
160         }
161
162         return MIstatus::success;
163 }       
164
165 //++ ------------------------------------------------------------------------------------
166 // Details:     Determine if stdin has any characters present in its buffer.
167 // Type:        Method.
168 // Args:        vwbAvail        - (W) True = There is chars available, false = nothing there.
169 // Return:      MIstatus::success - Functional succeeded.
170 //                      MIstatus::failure - Functional failed.
171 // Throws:      None.
172 //--
173 bool CMICmnStreamStdinWindows::InputAvailable( bool & vwbAvail )
174 {
175         return m_bRunningInConsoleWin ? InputAvailableConsoleWin( vwbAvail ) : InputAvailableApplication( vwbAvail );
176 }
177
178 //++ ------------------------------------------------------------------------------------
179 // Details:     Determine if stdin has any characters present in its buffer. If running in a 
180 //                      terminal use _kbhit().
181 // Type:        Method.
182 // Args:        vwbAvail        - (W) True = There is chars available, false = nothing there.
183 // Return:      MIstatus::success - Functional succeeded.
184 //                      MIstatus::failure - Functional failed.
185 // Throws:      None.
186 //--
187 bool CMICmnStreamStdinWindows::InputAvailableConsoleWin( bool & vwbAvail )
188 {
189 #if defined( _MSC_VER )
190   if( m_nBytesToBeRead == 0 )
191    {
192         // Get a windows handle to std input stream
193         HANDLE handle = ::GetStdHandle( STD_INPUT_HANDLE );
194         DWORD nBytesWaiting = ::_kbhit();
195         
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;
198
199         // Return state of whether bytes are waiting or not
200         vwbAvail = (nBytesWaiting > 0);
201     }
202 #endif // #if defined( _MSC_VER )
203
204         return MIstatus::success;
205 }
206
207 //++ ------------------------------------------------------------------------------------
208 // Details:     Determine if stdin has any characters present in its buffer.
209 // Type:        Method.
210 // Args:        vwbAvail        - (W) True = There is chars available, false = nothing there.
211 // Return:      MIstatus::success - Functional succeeded.
212 //                      MIstatus::failure - Functional failed.
213 // Throws:      None.
214 //--
215 bool CMICmnStreamStdinWindows::InputAvailableApplication( bool & vwbAvail )
216 {
217  #if defined( _MSC_VER )
218   if( m_nBytesToBeRead == 0 )
219    {
220         // Get a windows handle to std input stream
221         HANDLE handle = ::GetStdHandle( STD_INPUT_HANDLE );
222         DWORD nBytesWaiting = 0;
223
224         // Ask how many bytes are available
225         if( ::PeekNamedPipe( handle, nullptr, 0, nullptr, &nBytesWaiting, nullptr ) == FALSE )
226         {
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;
232         }
233  
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;
236
237         // Return state of whether bytes are waiting or not
238         vwbAvail = (nBytesWaiting > 0);
239     }
240 #endif // #if defined( _MSC_VER )
241
242         return MIstatus::success;
243 }
244
245 //++ ------------------------------------------------------------------------------------
246 // Details:     Wait on new line of data from stdin stream (completed by '\n' or '\r').
247 // Type:        Method.
248 // Args:        vwErrMsg        - (W) Empty string ok or error description.                     
249 // Return:      MIchar * - text buffer pointer or NULL on failure.
250 // Throws:      None.
251 //--
252 const MIchar * CMICmnStreamStdinWindows::ReadLine( CMIUtilString & vwErrMsg )
253 {
254         vwErrMsg.clear();
255         
256         // Read user input
257         const MIchar * pText = ::fgets( &m_pCmdBuffer[ 0 ], m_constBufferSize, stdin );
258         if( pText == nullptr )
259         {
260                 if( ::ferror( m_pStdin ) != 0 )
261                         vwErrMsg = ::strerror( errno );
262                 return nullptr;
263         }
264
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 );
267         
268         // Strip off new line characters
269         for( MIchar * pI = m_pCmdBuffer; *pI != '\0'; pI++ )
270         {
271                 if( (*pI == '\n') || (*pI == '\r') )
272                 {
273                         *pI = '\0';
274                         break;
275                 }
276         }
277
278         return pText;
279 }