aboutsummaryrefslogtreecommitdiff
path: root/src/functionexecute.cpp
blob: 8d87047e47863022bb676b2b1dc07aadc3cf7ba3 (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
74
75
76
77
78
79
80
/*
 * Copyright (C) 2007-2014 Xagasoft, All rights reserved.
 *
 * This file is part of the Xagasoft Build and is released under the
 * terms of the license contained in the file LICENSE.
 */

#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::String 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 );
    String 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() );
}