1 //===-- MICmnStreamStdinLinux.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: CMICmnStreamStdinLinux 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 )
24 #include <sys/select.h>
26 #endif // !defined( _MSC_VER )
27 #include <string.h> // For std::strerror()
30 #include "MICmnStreamStdinLinux.h"
32 #include "MICmnResources.h"
33 #include "MIUtilSingletonHelper.h"
35 //++ ------------------------------------------------------------------------------------
36 // Details: CMICmnStreamStdinLinux constructor.
42 CMICmnStreamStdinLinux::CMICmnStreamStdinLinux( void )
43 : m_constBufferSize( 1024 )
45 , m_pCmdBuffer( nullptr )
49 //++ ------------------------------------------------------------------------------------
50 // Details: CMICmnStreamStdinLinux destructor.
56 CMICmnStreamStdinLinux::~CMICmnStreamStdinLinux( void )
61 //++ ------------------------------------------------------------------------------------
62 // Details: Initialize resources for *this Stdin stream.
65 // Return: MIstatus::success - Functional succeeded.
66 // MIstatus::failure - Functional failed.
69 bool CMICmnStreamStdinLinux::Initialize( void )
72 return MIstatus::success;
74 bool bOk = MIstatus::success;
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 );
81 // Other resources required
84 m_pCmdBuffer = new MIchar[ m_constBufferSize ];
88 // Clear error indicators for std input
95 CMIUtilString strInitError( CMIUtilString::Format( MIRSRC( IDS_MI_INIT_ERR_OS_STDIN_HANDLER ), errMsg.c_str() ) );
96 SetErrorDescription( strInitError );
97 return MIstatus::failure;
100 return MIstatus::success;
103 //++ ------------------------------------------------------------------------------------
104 // Details: Release resources for *this Stdin stream.
107 // Return: MIstatus::success - Functional succeeded.
108 // MIstatus::failure - Functional failed.
111 bool CMICmnStreamStdinLinux::Shutdown( void )
113 if( !m_bInitialized )
114 return MIstatus::success;
116 m_bInitialized = false;
118 ClrErrorDescription();
120 bool bOk = MIstatus::success;
121 CMIUtilString errMsg;
124 if( m_pCmdBuffer != nullptr )
126 delete [] m_pCmdBuffer;
127 m_pCmdBuffer = nullptr;
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 );
137 SetErrorDescriptionn( MIRSRC( IDS_MI_SHTDWN_ERR_OS_STDIN_HANDLER ), errMsg.c_str() );
140 return MIstatus::success;
143 //++ ------------------------------------------------------------------------------------
144 // Details: Determine if stdin has any characters present in its buffer.
146 // Args: vwbAvail - (W) True = There is chars available, false = nothing there.
147 // Return: MIstatus::success - Functional succeeded.
148 // MIstatus::failure - Functional failed.
151 bool CMICmnStreamStdinLinux::InputAvailable( bool & vwbAvail )
153 /* AD: Not used ATM but could come in handy just in case we need to do
156 static const int STDIN = 0;
157 static bool bInitialized = false;
161 // Use termios to turn off line buffering
163 ::tcgetattr( STDIN, &term );
164 ::term.c_lflag &= ~ICANON;
165 ::tcsetattr( STDIN, TCSANOW, &term );
166 ::setbuf( stdin, NULL );
171 ::ioctl( STDIN, FIONREAD, &nBytesWaiting );
172 vwbAvail = (nBytesWaiting > 0);
174 return MIstatus::success;
177 return MIstatus::success;
180 //++ ------------------------------------------------------------------------------------
181 // Details: Wait on new line of data from stdin stream (completed by '\n' or '\r').
183 // Args: vwErrMsg - (W) Empty string ok or error description.
184 // Return: MIchar * - text buffer pointer or NULL on failure.
187 const MIchar * CMICmnStreamStdinLinux::ReadLine( CMIUtilString & vwErrMsg )
192 const MIchar * pText = ::fgets( &m_pCmdBuffer[ 0 ], m_constBufferSize, stdin );
193 if( pText == nullptr )
195 if( ::ferror( m_pStdin ) != 0 )
196 vwErrMsg = ::strerror( errno );
200 // Strip off new line characters
201 for( MIchar * pI = m_pCmdBuffer; *pI != '\0'; pI++ )
203 if( (*pI == '\n') || (*pI == '\r') )