summaryrefslogtreecommitdiff
path: root/services/unix_exec_service/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'services/unix_exec_service/main.cpp')
-rw-r--r--services/unix_exec_service/main.cpp132
1 files changed, 132 insertions, 0 deletions
diff --git a/services/unix_exec_service/main.cpp b/services/unix_exec_service/main.cpp
new file mode 100644
index 0000000..ea7faef
--- /dev/null
+++ b/services/unix_exec_service/main.cpp
@@ -0,0 +1,132 @@
+// Copyright © 2013 sss
+//.
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//.
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//.
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+
+#include <stdio.h>
+
+#include <string>
+#include <list>
+#include <algorithm>
+#include <fstream>
+
+#include "api_service.h"
+
+struct alias
+{
+ std::string _alias, cmd;
+ bool operator==(std::string a)
+ {
+ return this->_alias == a;
+ }
+};
+
+std::list<alias> aliases;
+std::list<command> cmds;
+
+std::string extract_aliased_cmd(std::string cmd)
+{
+ std::list<alias>::iterator i = std::find(aliases.begin(), aliases.end(), cmd);
+ if(i != aliases.end())
+ return i->cmd;
+ else
+ return std::string();
+}
+
+
+void * shell_exec(void * t)
+{
+ char *c = (char*)t;
+ std::string cmd;
+ if(c[0] != '/')
+ cmd = extract_aliased_cmd(c);
+ else
+ cmd = c;
+ if(!cmd.empty())
+ {
+ }
+ return NULL;
+}
+
+void load_cmds()
+{
+ std::fstream f("./unix_exec_service.cmds", std::fstream::in);
+ if(!f.is_open())
+ f.open("./modules/unix_exec_service.cmds");
+ if(!f.is_open())
+ return;
+ char buf[2048];
+ while(f.good() && !f.eof())
+ {
+ std::string str, cmd, _alias, descr;
+ f.getline(buf, 2047);
+ if(buf[0] == '#')
+ continue;
+ str = buf;
+ if(buf[0] != '=')
+ {
+ std::string::size_type p2 = str.find("=");
+ if(p2 != std::string::npos)
+ {
+ _alias = str.substr(0, p2);
+ }
+ }
+ std::string::size_type p1 = str.find("="), p2 = 0;
+ if(p1 != std::string::npos)
+ {
+ p1++;
+ p2 = str.find(";", p1);
+ if(p2 != std::string::npos)
+ cmd = str.substr(p1, p2-p1);
+ }
+ p1 = str.find(";");
+ if(p1 != std::string::npos && p1 < str.length())
+ {
+ p1++;
+ descr = str.substr(p1);
+ }
+ if(!_alias.empty())
+ {
+ alias a;
+ a._alias = _alias;
+ a.cmd = cmd;
+ aliases.push_back(a);
+ }
+ command c;
+ c.command = cmd;
+ c.description = descr;
+ cmds.push_back(c);
+// printf("%s | %s | %s\n", _alias.c_str(), cmd.c_str(), descr.c_str());
+ }
+}
+
+init_func(init)
+{
+ load_cmds();
+}
+
+services(get_services)
+{
+ service_info shell_exec_service;
+ shell_exec_service.acc = ACC_CHAR_PTR;
+ shell_exec_service.ret = RET_STD_STRING;
+ shell_exec_service.name = "Shell exec";
+ shell_exec_service.description = "Run shell command and return output";
+ shell_exec_service.exec = &shell_exec;
+ shell_exec_service.predefined = cmds;
+ s.push_back(shell_exec_service);
+}
+
+