]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MICmnStreamStdinLinux.cpp
Merge ^/head r274961 through r276472.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / tools / lldb-mi / MICmnStreamStdinLinux.cpp
1 //===-- MICmnStreamStdinLinux.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:    CMICmnStreamStdinLinux 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 <sys/select.h>
25 #include <termios.h>
26 #endif // !defined( _MSC_VER )
27 #include <string.h> // For std::strerror()
28
29 // In-house headers:
30 #include "MICmnStreamStdinLinux.h"
31 #include "MICmnLog.h"
32 #include "MICmnResources.h"
33 #include "MIUtilSingletonHelper.h"
34
35 //++ ------------------------------------------------------------------------------------
36 // Details:     CMICmnStreamStdinLinux constructor.
37 // Type:        Method.
38 // Args:        None.
39 // Return:      None.
40 // Throws:      None.
41 //--
42 CMICmnStreamStdinLinux::CMICmnStreamStdinLinux( void )
43 :       m_constBufferSize( 1024 )
44 ,       m_pStdin( nullptr )
45 ,       m_pCmdBuffer( nullptr )
46 {
47 }
48
49 //++ ------------------------------------------------------------------------------------
50 // Details:     CMICmnStreamStdinLinux destructor.
51 // Type:        Overridable.
52 // Args:        None.
53 // Return:      None.
54 // Throws:      None.
55 //--
56 CMICmnStreamStdinLinux::~CMICmnStreamStdinLinux( void )
57 {
58         Shutdown();
59 }
60
61 //++ ------------------------------------------------------------------------------------
62 // Details:     Initialize resources for *this Stdin stream.
63 // Type:        Method.
64 // Args:        None.
65 // Return:      MIstatus::success - Functional succeeded.
66 //                      MIstatus::failure - Functional failed.
67 // Throws:      None.
68 //--
69 bool CMICmnStreamStdinLinux::Initialize( void )
70 {
71         if( m_bInitialized )
72                 return MIstatus::success;
73
74         bool bOk = MIstatus::success;
75         CMIUtilString errMsg;
76  
77         // Note initialisation order is important here as some resources depend on previous
78         MI::ModuleInit< CMICmnLog >      ( IDS_MI_INIT_ERR_LOG      , bOk, errMsg );
79         MI::ModuleInit< CMICmnResources >( IDS_MI_INIT_ERR_RESOURCES, bOk, errMsg );
80
81         // Other resources required
82         if( bOk )
83         {
84                 m_pCmdBuffer = new MIchar[ m_constBufferSize ];
85                 m_pStdin = stdin;
86         }
87
88         // Clear error indicators for std input
89         ::clearerr( stdin );
90
91         m_bInitialized = bOk;
92
93         if( !bOk )
94         {
95                 CMIUtilString strInitError( CMIUtilString::Format( MIRSRC( IDS_MI_INIT_ERR_OS_STDIN_HANDLER ), errMsg.c_str() ) );
96                 SetErrorDescription( strInitError );
97                 return MIstatus::failure;
98         }
99
100         return  MIstatus::success;
101 }
102
103 //++ ------------------------------------------------------------------------------------
104 // Details:     Release resources for *this Stdin stream.
105 // Type:        Method.
106 // Args:        None.
107 // Return:      MIstatus::success - Functional succeeded.
108 //                      MIstatus::failure - Functional failed.
109 // Throws:      None.
110 //--
111 bool CMICmnStreamStdinLinux::Shutdown( void )
112 {
113         if( !m_bInitialized )
114                 return MIstatus::success;
115
116         m_bInitialized = false;
117
118         ClrErrorDescription();
119
120         bool bOk = MIstatus::success;
121         CMIUtilString errMsg;
122
123         // Tidy up
124         if( m_pCmdBuffer != nullptr )
125         {
126                 delete [] m_pCmdBuffer;
127                 m_pCmdBuffer = nullptr;
128         }
129         m_pStdin = nullptr;
130
131         // Note shutdown order is important here        
132         MI::ModuleShutdown< CMICmnResources >( IDS_MI_INIT_ERR_RESOURCES, bOk, errMsg );
133         MI::ModuleShutdown< CMICmnLog >      ( IDS_MI_INIT_ERR_LOG      , bOk, errMsg );
134
135         if( !bOk )
136         {
137                 SetErrorDescriptionn( MIRSRC( IDS_MI_SHTDWN_ERR_OS_STDIN_HANDLER ), errMsg.c_str() );
138         }
139
140         return MIstatus::success;
141 }       
142
143 //++ ------------------------------------------------------------------------------------
144 // Details:     Determine if stdin has any characters present in its buffer.
145 // Type:        Method.
146 // Args:        vwbAvail        - (W) True = There is chars available, false = nothing there.
147 // Return:      MIstatus::success - Functional succeeded.
148 //                      MIstatus::failure - Functional failed.
149 // Throws:      None.
150 //--
151 bool CMICmnStreamStdinLinux::InputAvailable( bool & vwbAvail )
152 {
153 /* AD: Not used ATM but could come in handy just in case we need to do
154        this, poll for input
155
156         static const int STDIN = 0;
157     static bool bInitialized = false;
158
159     if( !bInitialized )
160         {
161         // Use termios to turn off line buffering
162         ::termios term;
163         ::tcgetattr( STDIN, &term );
164         ::term.c_lflag &= ~ICANON;
165         ::tcsetattr( STDIN, TCSANOW, &term );
166         ::setbuf( stdin, NULL );
167         bInitialized = true;
168     }
169
170     int nBytesWaiting;
171     ::ioctl( STDIN, FIONREAD, &nBytesWaiting );
172     vwbAvail = (nBytesWaiting > 0);
173
174         return MIstatus::success;
175 */
176
177         return MIstatus::success;
178 }
179
180 //++ ------------------------------------------------------------------------------------
181 // Details:     Wait on new line of data from stdin stream (completed by '\n' or '\r').
182 // Type:        Method.
183 // Args:        vwErrMsg        - (W) Empty string ok or error description.                     
184 // Return:      MIchar * - text buffer pointer or NULL on failure.
185 // Throws:      None.
186 //--
187 const MIchar * CMICmnStreamStdinLinux::ReadLine( CMIUtilString & vwErrMsg )
188 {
189         vwErrMsg.clear();
190         
191         // Read user input
192         const MIchar * pText = ::fgets( &m_pCmdBuffer[ 0 ], m_constBufferSize, stdin );
193         if( pText == nullptr )
194         {
195                 if( ::ferror( m_pStdin ) != 0 )
196                         vwErrMsg = ::strerror( errno );
197                 return nullptr;
198         }
199
200     // Strip off new line characters
201         for( MIchar * pI = m_pCmdBuffer; *pI != '\0'; pI++ )
202         {
203                 if( (*pI == '\n') || (*pI == '\r') )
204                 {
205                         *pI = '\0';
206                         break;
207                 }
208         }
209
210         return pText;
211 }
212