Exemple #1
0
void CDataLog::Destroy()
{   _STT();

    // Write remaining data to disk
    Flush( 0 );

    // Lose logging objects
    for ( oexINT i = 0; i < eMaxKeys; i++ )
        if ( m_pLogs[ i ] )
            OexAllocDelete( m_pLogs[ i ] ),
                            m_pLogs[ i ] = oexNULL;

    // Forget the root
    m_sRoot.Destroy();
}
Exemple #2
0
int run( oex::CPropertyBag &pbCmdLine )
{_STT();

	oex::oexBOOL bFile = oex::oexTRUE;
	oex::CStr sCmd = pbCmdLine[ 0 ].ToString();
	oex::oexBOOL bInline = pbCmdLine.IsKey( "i" );

	// Calculate a module name if not specified
	if ( !sCmd.Length() )
	{
		// Raw script on the command line?
		if ( pbCmdLine.IsKey( "s" ) )
			bFile = oex::oexFALSE, sCmd = pbCmdLine[ "s" ].ToString();
		
		// Base64 encoded script?
		if ( pbCmdLine.IsKey( "b" ) )
			bFile = oex::oexFALSE, sCmd = oex::CBase64::Decode( pbCmdLine[ "b" ].ToString() );

		// Scrit?
		else if ( pbCmdLine.IsKey( "script" ) )
			bFile = oex::oexFALSE, sCmd = pbCmdLine[ "script" ].ToString();
			
		else
		{	
			// Look for a .cfg file
			oex::CStr sSettings = oexGetModuleFileName() << oexT( ".cfg" );
			if ( oex::CFile::Exists( sSettings.Ptr() ) )
			{
				// Decode settings file
				oex::CPropertyBag pb = oex::CParser::DecodeIni( oexMbToStr( oex::CFile().OpenExisting( sSettings.Ptr() ).Read() ) );

				// Command line?
				if ( pb.IsKey( oexT( "cmd" ) ) )
					sCmd = pb[ oexT( "cmd" ) ].ToString();

			} // end if

			else
			{
				sSettings = oexGetModuleFileName() << oexT( ".cfg.txt" );
				if ( oex::CFile::Exists( sSettings.Ptr() ) )
				{
					// Decode settings file
					oex::CPropertyBag pb = oex::CParser::DecodeIni( oexMbToStr( oex::CFile().OpenExisting( sSettings.Ptr() ).Read() ) );

					// Command line?
					if ( pb.IsKey( oexT( "cmd" ) ) )
						sCmd = pb[ oexT( "cmd" ) ].ToString();

				} // end if

			} // end else
			
		} // end else

	} // end if

	// Stdin?
	if ( !sCmd.Length() )
		sCmd = oexReadStdin(), bFile = oex::oexFALSE;

	// Do we have a script
	if ( !sCmd.Length() )
		return oexERROR( -1, oexT( "Script not specified" ) );

	// Ensure the file exists
	if ( bFile && !oexExists( sCmd.Ptr() ) )
	{
		if ( oexExists( oexGetModulePath( oexT( "scripts" ) ).BuildPath( sCmd ).Ptr() ) )
			sCmd = oexGetModulePath( oexT( "scripts" ) ).BuildPath( sCmd );

		else
		{	oexEcho( oexMks( oexT( "Script not found : " ), sCmd ).Ptr() );
			return oexERROR( -2, oexMks( oexT( "Script not found : " ), sCmd ) );
		} // end else

	} // end if

	// Check for inline type
	if ( bFile && sCmd.GetFileExtension().ToLower() == oexT( "squ" ) )
		bInline = oex::oexTRUE;
	
	// Create objects
	g_psqScriptThread = OexAllocConstruct< sqbind::CScriptThread >();
	if ( !oexCHECK_PTR( g_psqScriptThread ) )
		return oexERROR( -3, oexT( "Out of memory!" ) );

	g_psqModuleManager = OexAllocConstruct< sqbind::CModuleManager >();
	if ( !oexCHECK_PTR( g_psqModuleManager ) )
		return oexERROR( -4, oexT( "Out of memory!" ) );

	// Log the script name
	oexNOTICE( 0, oexMks( oexT( "Running script : " ), sCmd ) );

	sqbind::CScriptThread::SetAppInfo( oexAppNamePtr(), oexAppNameProcPtr(), oexAppLongNamePtr(), oexAppDescPtr() );

	g_psqScriptThread->setInline( bInline );

	g_psqScriptThread->SetScriptName( bFile ? sCmd.Ptr() : oexT( "buffer" ) );

	g_psqScriptThread->SetModuleManager( g_psqModuleManager );

	g_psqScriptThread->SetScript( oexStrToMb( sCmd ), bFile );

	g_psqScriptThread->SetExportFunction( SQBIND_Export_Symbols, oexNULL );

	g_psqScriptThread->Pb()[ oexT( "cmdline" ) ] = pbCmdLine.Copy();

	// Push the stack if inlined
	if ( bInline )
		g_psqScriptThread->GetEngine()->push_stack( oexNULL );

	// Start the thread
	if ( g_psqScriptThread->Start() )
		return oexERROR( -5, oexT( "Failed to start script thread" ) );

	// Attempt to execute idle function
	oex::oexINT gc = 0;
	while ( g_psqScriptThread->IsRunning() )
	{
		// Clean up binary shares
		if ( gc ) gc--; else { gc = 10; oexCleanupBin(); }

		// Wait
		oexSleep( 100 );

	} // end while

	// Echo output if inline
	if ( bInline )
	{	sqbind::stdString sRet = g_psqScriptThread->GetEngine()->pop_stack();
		oexEcho( sRet.c_str(), sRet.length() );
	} // end if	
	
	oexNOTICE( 0, oexT( "Script thread has terminated" ) );

	if ( oexCHECK_PTR( g_psqScriptThread ) )
	{	g_psqScriptThread->Destroy();
		OexAllocDelete( g_psqScriptThread );
	} // end if

	if ( oexCHECK_PTR( g_psqModuleManager ) )
	{	g_psqModuleManager->Destroy();
		OexAllocDelete( g_psqModuleManager );
	} // end if

	return 0;
}