aboutsummaryrefslogtreecommitdiff
path: root/tools/lldb-mi/MICmnStreamStdinLinux.cpp
blob: f73dc32d8258288b8fb4a5eed721860ac85b9a0a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//===-- MICmnStreamStdinLinux.cpp --------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

//++
// File:		MIUtilStreamStdin.cpp
//
// Overview:	CMICmnStreamStdinLinux implementation.
//
// Environment:	Compilers:	Visual C++ 12.
//							gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
//				Libraries:	See MIReadmetxt. 
//
// Copyright:	None.
//--

// Third Party Headers:
#if !defined( _MSC_VER )
#include <sys/select.h>
#include <termios.h>
#endif // !defined( _MSC_VER )
#include <string.h> // For std::strerror()

// In-house headers:
#include "MICmnStreamStdinLinux.h"
#include "MICmnLog.h"
#include "MICmnResources.h"
#include "MIUtilSingletonHelper.h"

//++ ------------------------------------------------------------------------------------
// Details:	CMICmnStreamStdinLinux constructor.
// Type:	Method.
// Args:	None.
// Return:	None.
// Throws:	None.
//--
CMICmnStreamStdinLinux::CMICmnStreamStdinLinux( void )
:	m_constBufferSize( 1024 )
,	m_pStdin( nullptr )
,	m_pCmdBuffer( nullptr )
{
}

//++ ------------------------------------------------------------------------------------
// Details:	CMICmnStreamStdinLinux destructor.
// Type:	Overridable.
// Args:	None.
// Return:	None.
// Throws:	None.
//--
CMICmnStreamStdinLinux::~CMICmnStreamStdinLinux( void )
{
	Shutdown();
}

//++ ------------------------------------------------------------------------------------
// Details:	Initialize resources for *this Stdin stream.
// Type:	Method.
// Args:	None.
// Return:	MIstatus::success - Functional succeeded.
//			MIstatus::failure - Functional failed.
// Throws:	None.
//--
bool CMICmnStreamStdinLinux::Initialize( void )
{
	if( m_bInitialized )
		return MIstatus::success;

	bool bOk = MIstatus::success;
	CMIUtilString errMsg;
 
	// Note initialisation order is important here as some resources depend on previous
	MI::ModuleInit< CMICmnLog >      ( IDS_MI_INIT_ERR_LOG      , bOk, errMsg );
	MI::ModuleInit< CMICmnResources >( IDS_MI_INIT_ERR_RESOURCES, bOk, errMsg );

	// Other resources required
	if( bOk )
	{
		m_pCmdBuffer = new MIchar[ m_constBufferSize ];
		m_pStdin = stdin;
	}

	// Clear error indicators for std input
	::clearerr( stdin );

	m_bInitialized = bOk;

	if( !bOk )
	{
		CMIUtilString strInitError( CMIUtilString::Format( MIRSRC( IDS_MI_INIT_ERR_OS_STDIN_HANDLER ), errMsg.c_str() ) );
		SetErrorDescription( strInitError );
		return MIstatus::failure;
	}

	return  MIstatus::success;
}

//++ ------------------------------------------------------------------------------------
// Details:	Release resources for *this Stdin stream.
// Type:	Method.
// Args:	None.
// Return:	MIstatus::success - Functional succeeded.
//			MIstatus::failure - Functional failed.
// Throws:	None.
//--
bool CMICmnStreamStdinLinux::Shutdown( void )
{
	if( !m_bInitialized )
		return MIstatus::success;

	m_bInitialized = false;

	ClrErrorDescription();

	bool bOk = MIstatus::success;
	CMIUtilString errMsg;

	// Tidy up
	if( m_pCmdBuffer != nullptr )
	{
		delete [] m_pCmdBuffer;
		m_pCmdBuffer = nullptr;
	}
	m_pStdin = nullptr;

	// Note shutdown order is important here 	
	MI::ModuleShutdown< CMICmnResources >( IDS_MI_INIT_ERR_RESOURCES, bOk, errMsg );
	MI::ModuleShutdown< CMICmnLog >      ( IDS_MI_INIT_ERR_LOG      , bOk, errMsg );

	if( !bOk )
	{
		SetErrorDescriptionn( MIRSRC( IDS_MI_SHTDWN_ERR_OS_STDIN_HANDLER ), errMsg.c_str() );
	}

	return MIstatus::success;
}	

//++ ------------------------------------------------------------------------------------
// Details:	Determine if stdin has any characters present in its buffer.
// Type:	Method.
// Args:	vwbAvail	- (W) True = There is chars available, false = nothing there.
// Return:	MIstatus::success - Functional succeeded.
//			MIstatus::failure - Functional failed.
// Throws:	None.
//--
bool CMICmnStreamStdinLinux::InputAvailable( bool & vwbAvail )
{
/* AD: Not used ATM but could come in handy just in case we need to do
       this, poll for input

	static const int STDIN = 0;
    static bool bInitialized = false;

    if( !bInitialized )
	{
        // Use termios to turn off line buffering
        ::termios term;
        ::tcgetattr( STDIN, &term );
        ::term.c_lflag &= ~ICANON;
        ::tcsetattr( STDIN, TCSANOW, &term );
        ::setbuf( stdin, NULL );
        bInitialized = true;
    }

    int nBytesWaiting;
    ::ioctl( STDIN, FIONREAD, &nBytesWaiting );
    vwbAvail = (nBytesWaiting > 0);

	return MIstatus::success;
*/

	return MIstatus::success;
}

//++ ------------------------------------------------------------------------------------
// Details:	Wait on new line of data from stdin stream (completed by '\n' or '\r').
// Type:	Method.
// Args:	vwErrMsg	- (W) Empty string ok or error description.			
// Return:	MIchar * - text buffer pointer or NULL on failure.
// Throws:	None.
//--
const MIchar * CMICmnStreamStdinLinux::ReadLine( CMIUtilString & vwErrMsg )
{
	vwErrMsg.clear();
	
	// Read user input
	const MIchar * pText = ::fgets( &m_pCmdBuffer[ 0 ], m_constBufferSize, stdin );
	if( pText == nullptr )
	{
		if( ::ferror( m_pStdin ) != 0 )
			vwErrMsg = ::strerror( errno );
		return nullptr;
	}

    // Strip off new line characters
	for( MIchar * pI = m_pCmdBuffer; *pI != '\0'; pI++ )
	{
		if( (*pI == '\n') || (*pI == '\r') )
		{
			*pI = '\0';
			break;
		}
	}

	return pText;
}