aboutsummaryrefslogtreecommitdiff
path: root/src/functionexecute.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/functionexecute.cpp68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/functionexecute.cpp b/src/functionexecute.cpp
new file mode 100644
index 0000000..619d2c2
--- /dev/null
+++ b/src/functionexecute.cpp
@@ -0,0 +1,68 @@
1#include "functionexecute.h"
2#include "context.h"
3#include "view.h"
4
5#include <bu/sio.h>
6#include <bu/process.h>
7using namespace Bu;
8
9FunctionExecute::FunctionExecute()
10{
11}
12
13FunctionExecute::~FunctionExecute()
14{
15}
16
17Bu::FString FunctionExecute::getName() const
18{
19 return "execute";
20}
21
22Variable FunctionExecute::call( Variable &/*input*/, VarList lParams )
23{
24 // TODO This is lame, really lame, we need to exec on our own and process
25 // output appropriately.
26 pContext->getView()->cmdStarted( lParams.first().getString() );
27 Process pCmd( Process::Both, "/bin/bash", "/bin/bash", "-c",
28 lParams.first().getString().getStr(), NULL );
29 FString sStdOut, sStdErr;
30 while( pCmd.isRunning() )
31 {
32 char buf[4096];
33 bool out, err;
34 pCmd.select( out, err );
35 if( err )
36 {
37 int iRead = pCmd.readErr( buf, 4096 );
38 sStdErr.append( buf, iRead );
39 //sio << "Read " << iRead << " bytes of stderr." << sio.nl;
40 }
41 if( out )
42 {
43 int iRead = pCmd.read( buf, 4096 );
44 sStdOut.append( buf, iRead );
45 //sio << "Read " << iRead << " bytes of stdout." << sio.nl;
46 }
47 }
48
49 pContext->getView()->cmdFinished(
50 sStdOut, sStdErr, pCmd.childExitStatus()
51 );
52 if( pCmd.childExited() )
53 {
54 if( pCmd.childExitStatus() != 0 )
55 {
56 throw Bu::ExceptionBase("Command exited with errorcode %d.", pCmd.childExitStatus() );
57 }
58 }
59 else
60 {
61 pContext->getView()->cmdFinished(
62 sStdOut, sStdErr, pCmd.childExitStatus()
63 );
64 throw Bu::ExceptionBase("Command Failed");
65 }
66 return Variable( pCmd.childExitStatus() );
67}
68