Subversion Repositories HomeAutomation

Rev

Rev 1608 | Rev 1625 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1601 runge 1
/*
2
 *
3
 *  Copyright (C) 2010  Mattias Runge
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License along
16
 *  with this program; if not, write to the Free Software Foundation, Inc.,
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
19
 */
20
 
21
#include "System.h"
22
 
1604 runge 23
#include <v8-debug.h>
1601 runge 24
#include <stdio.h>
25
 
26
#include "vm/Manager.h"
27
 
28
namespace atom {
29
namespace vm {
30
namespace plugin {
31
 
32
logging::Logger System::LOG("vm::plugin::System");
33
 
1609 runge 34
System::System(bool legacy)
1601 runge 35
{
36
    this->name_ = "system";
1609 runge 37
 
38
    this->legacy_ = legacy;
1601 runge 39
 
40
    this->ExportFunction("Log",        System::Export_Log);
41
    this->ExportFunction("LoadScript", System::Export_LoadScript);
42
    this->ExportFunction("Execute",    System::Export_Execute);
43
}
44
 
45
System::~System()
46
{
47
 
48
}
49
 
50
void System::InitializeDone()
51
{
52
    Plugin::InitializeDone();
53
 
1608 runge 54
    this->ImportFunction("Start");
55
 
1609 runge 56
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
57
 
58
    arguments->push_back(v8::Boolean::New(this->legacy_));
59
 
60
    this->Call(0, "Start", arguments);
1601 runge 61
}
62
 
63
Value System::Export_Log(const v8::Arguments& args)
64
{
1604 runge 65
    if (args.Length() < 1)
66
    {
67
        LOG.Error("To few arguments to log.");
68
    }
69
 
1601 runge 70
    v8::String::AsciiValue str(args[0]);
71
 
72
    LOG.Info(*str);
73
 
74
    return v8::Undefined();
75
}
76
 
77
Value System::Export_LoadScript(const v8::Arguments& args)
78
{
1609 runge 79
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
1604 runge 80
 
81
    if (args.Length() < 1)
82
    {
83
        LOG.Error("To few arguments.");
84
    }
85
 
1601 runge 86
    v8::String::AsciiValue str(args[0]);
87
 
1608 runge 88
    return v8::Boolean::New(Manager::Instance()->LoadScript(*str));
1601 runge 89
}
90
 
91
Value System::Export_Execute(const v8::Arguments& args)
92
{
1609 runge 93
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
1604 runge 94
 
95
    if (args.Length() < 1)
96
    {
97
        LOG.Error("To few arguments.");
98
    }
99
 
1601 runge 100
    v8::String::AsciiValue command(args[0]);
101
 
102
    std::string output_buffer;
103
    char output[128];
104
 
105
    FILE* pipe = popen(*command, "r" );
106
 
107
    if (pipe == NULL)
108
    {
109
        LOG.Info("Failed to execute \"" + std::string(*command) + "\".");
110
        return v8::Boolean::New(false);
111
    }
112
 
113
    while (!feof(pipe))
114
    {
115
        if (fgets(output, 128, pipe) != NULL)
116
        {
117
            output_buffer += output;
118
        }
119
    }
120
 
121
    pclose(pipe);
122
 
123
    return v8::String::New(output_buffer.data());
124
}
125
 
126
}; // namespace plugin
127
}; // namespace vm
128
}; // namespace atom