aboutsummaryrefslogtreecommitdiff
path: root/src/functionexecute.cpp
blob: 0b0aabfff9bbbcffeac956fab767945c5c494626 (plain)
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
#include "functionexecute.h"
#include "context.h"
#include "view.h"

#include <bu/sio.h>
#include <bu/process.h>
using namespace Bu;

#include <bu/plugger.h>
PluginInterface3( pluginFunctionExecute, execute, FunctionExecute, Function,
		"Mike Buland", 0, 1 );

FunctionExecute::FunctionExecute()
{
}

FunctionExecute::~FunctionExecute()
{
}

Bu::FString FunctionExecute::getName() const
{
	return "execute";
}

Variable FunctionExecute::call( Variable &/*input*/, VarList lParams )
{
	// TODO This is lame, really lame, we need to exec on our own and process
	// output appropriately.
	pContext->getView()->cmdStarted( lParams.first().getString() );
	Process pCmd( Process::Both, "/bin/bash", "/bin/bash", "-c",
		lParams.first().getString().getStr(), NULL );
	FString sStdOut, sStdErr;
	do
	{
		char buf[4096];
		bool out, err;
		pCmd.select( out, err );
		if( err )
		{
			int iRead = pCmd.readErr( buf, 4096 );
			sStdErr.append( buf, iRead );
			//sio << "Read " << iRead << " bytes of stderr." << sio.nl;
		}
		if( out )
		{
			int iRead = pCmd.read( buf, 4096 );
			sStdOut.append( buf, iRead );
			//sio << "Read " << iRead << " bytes of stdout." << sio.nl;
		}
	}
	while( pCmd.isRunning() );

	pContext->getView()->cmdFinished(
		sStdOut, sStdErr, pCmd.childExitStatus()
		);
	if( pCmd.childExited() )
	{
		if( pCmd.childExitStatus() != 0 )
		{
			throw Bu::ExceptionBase("Command exited with errorcode %d.", pCmd.childExitStatus() );
		}
	}
	else
	{
		pContext->getView()->cmdFinished(
			sStdOut, sStdErr, pCmd.childExitStatus()
			);
		throw Bu::ExceptionBase("Command Failed");
	}
	return Variable( pCmd.childExitStatus() );
}