Subversion Repositories HomeAutomation

Rev

Rev 1648 | Rev 1657 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  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.  
  23. #include <v8-debug.h>
  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.    
  34. System::System(boost::asio::io_service& io_service) : Plugin(io_service)
  35. {
  36.     this->name_ = "system";
  37.    
  38.     this->ExportFunction("Require",    System::Export_Require);
  39.     this->ExportFunction("Execute",    System::Export_Execute);
  40. }
  41.  
  42. System::~System()
  43. {
  44.  
  45. }
  46.  
  47. void System::InitializeDone()
  48. {
  49.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  50.    
  51.     Plugin::InitializeDone();
  52.    
  53.     v8::Boolean::New(Manager::Instance()->LoadScript("interface/list.js"));
  54.     v8::Boolean::New(Manager::Instance()->LoadScript("autostart.js"));
  55. }
  56.  
  57. void System::CallOutput(unsigned int request_id, std::string output)
  58. {
  59.     LOG.Info(output);
  60. }
  61.  
  62. Value System::Export_Require(const v8::Arguments& args)
  63. {
  64.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  65.    
  66.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  67.    
  68.     if (args.Length() < 1)
  69.     {
  70.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  71.         return v8::Boolean::New(false);
  72.     }
  73.    
  74.     v8::String::AsciiValue str(args[0]);
  75.    
  76.     return v8::Boolean::New(Manager::Instance()->LoadScript(*str));
  77. }
  78.  
  79. Value System::Export_Execute(const v8::Arguments& args)
  80. {
  81.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  82.    
  83.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  84.    
  85.     if (args.Length() < 1)
  86.     {
  87.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  88.         return v8::Boolean::New(false);
  89.     }
  90.    
  91.     v8::String::AsciiValue command(args[0]);
  92.    
  93.     std::string output_buffer;
  94.     char output[128];
  95.    
  96.     FILE* pipe = popen(*command, "r" );
  97.    
  98.     if (pipe == NULL)
  99.     {
  100.         LOG.Info("Failed to execute \"" + std::string(*command) + "\".");
  101.         return v8::Boolean::New(false);
  102.     }
  103.    
  104.     while (!feof(pipe))
  105.     {
  106.         if (fgets(output, 128, pipe) != NULL)
  107.         {
  108.             output_buffer += output;
  109.         }
  110.     }
  111.    
  112.     pclose(pipe);
  113.    
  114.     return v8::String::New(output_buffer.data());
  115. }
  116.    
  117. }; // namespace plugin
  118. }; // namespace vm
  119. }; // namespace atom
  120.