summaryrefslogtreecommitdiff
path: root/services/unix_exec_service/main.cpp
blob: 32f9999c5544c254f6085146919172de5f533866 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// 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"

using namespace service;

struct alias
{
	std::string _alias, cmd;
	bool operator==(std::string a)
	{
		return this->_alias == a;
	}
};

std::list<alias> aliases;
std::list<service_info::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())
	{ //TODO: handle also stderr
		FILE *f = popen(cmd.c_str(), "r");
		if(f)
		{
			std::string *ret = new std::string;
			char buf[128];
			while(fgets(buf, 128, f))
				ret->append(buf);
			return (void*)ret;
		}
		else
		{
			//TODO: handle fail (need to extend service api ?)
		}
	}
	return (void*)new std::string;
}

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);
		}
		service_info::command c;
		if(!_alias.empty())
		{
			alias a;
			a._alias = _alias;
			a.cmd = cmd;
			c.command = _alias;
			aliases.push_back(a);
		}
		else
			c.command = cmd;
		c.description = descr;
		if(!c.command.empty())
			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_PTR;
	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); 
}