controlapi/mod_yparse: avoid double code ...

... by moving functions to execute scripts to helpers.cpp|h
Change outType from ExecCGI to text/plain because all arguments
are passed to the script. So no format can be defined.


Origin commit data
------------------
Branch: ni/coolstream
Commit: b5e2998d54
Author: vanhofen <vanhofen@gmx.de>
Date: 2016-02-22 (Mon, 22 Feb 2016)

Origin message was:
------------------
- controlapi/mod_yparse: avoid double code ...

... by moving functions to execute scripts to helpers.cpp|h
Change outType from ExecCGI to text/plain because all arguments
are passed to the script. So no format can be defined.


------------------
This commit was generated by Migit
This commit is contained in:
vanhofen
2016-02-22 14:15:02 +01:00
parent dbb4706326
commit a9b725c9b5
6 changed files with 70 additions and 122 deletions

View File

@@ -11,8 +11,11 @@
#include <sstream>
#include <iomanip>
#include <unistd.h>
// yhttpd
#include <yconfig.h>
#include <tuxboxapi/controlapi.h>
#include "ytypes_globals.h"
#include "helper.h"
#include "ylogging.h"
@@ -354,3 +357,51 @@ std::string json_convert_string(std::string s) {
}
return ss.str();
}
std::string yExecuteScript(std::string cmd) {
std::string script, para, result;
bool found = false;
//aprintf("%s: %s\n", __func__, cmd.c_str());
// split script and parameters
int pos;
if ((pos = cmd.find_first_of(" ")) > 0) {
script = cmd.substr(0, pos);
para = cmd.substr(pos + 1, cmd.length() - (pos + 1)); // snip
} else
script = cmd;
// get file
std::string fullfilename;
script += ".sh"; //add script extention
char cwd[255];
getcwd(cwd, 254);
for (unsigned int i = 0; i < CControlAPI::PLUGIN_DIR_COUNT && !found; i++) {
fullfilename = CControlAPI::PLUGIN_DIRS[i] + "/" + script;
FILE *test = fopen(fullfilename.c_str(), "r"); // use fopen: popen does not work
if (test != NULL) {
fclose(test);
chdir(CControlAPI::PLUGIN_DIRS[i].c_str());
FILE *f = popen((fullfilename + " " + para).c_str(), "r"); //execute
if (f != NULL) {
found = true;
char output[1024];
while (fgets(output, 1024, f)) // get script output
result += output;
pclose(f);
}
}
}
chdir(cwd);
if (!found) {
printf("%s: script %s not found in:\n", __func__, script.c_str());
for (unsigned int i = 0; i < CControlAPI::PLUGIN_DIR_COUNT; i++) {
printf("\t%s\n", CControlAPI::PLUGIN_DIRS[i].c_str());
}
result = "error";
}
return result;
}