Subversion Repositories HomeAutomation

Rev

Rev 1601 | Rev 1604 | 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.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()
  35. {
  36.     this->name_ = "system";
  37.                  
  38.     this->ImportFunction("Start");
  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.    
  54.     this->Call("Start", ArgumentListPointer(new ArgumentList));
  55. }
  56.  
  57. Value System::Export_Log(const v8::Arguments& args)
  58. {
  59.     v8::String::AsciiValue str(args[0]);
  60.    
  61.     LOG.Info(*str);
  62.    
  63.     return v8::Undefined();
  64. }
  65.  
  66. Value System::Export_LoadScript(const v8::Arguments& args)
  67. {
  68.     v8::String::AsciiValue str(args[0]);
  69.    
  70.     return v8::Boolean::New(Manager::Instance()->LoadScriptHandler(*str));
  71. }
  72.  
  73. Value System::Export_Execute(const v8::Arguments& args)
  74. {
  75.     v8::String::AsciiValue command(args[0]);
  76.    
  77.     std::string output_buffer;
  78.     char output[128];
  79.    
  80.     FILE* pipe = popen(*command, "r" );
  81.    
  82.     if (pipe == NULL)
  83.     {
  84.         LOG.Info("Failed to execute \"" + std::string(*command) + "\".");
  85.         return v8::Boolean::New(false);
  86.     }
  87.    
  88.     while (!feof(pipe))
  89.     {
  90.         if (fgets(output, 128, pipe) != NULL)
  91.         {
  92.             output_buffer += output;
  93.         }
  94.     }
  95.    
  96.     pclose(pipe);
  97.    
  98.     return v8::String::New(output_buffer.data());
  99. }
  100.    
  101. }; // namespace plugin
  102. }; // namespace vm
  103. }; // namespace atom
  104.