2012-09-06 20:23:48 +02:00
|
|
|
#include <ctime>
|
|
|
|
#include <cstdio>
|
2012-07-02 19:50:14 +02:00
|
|
|
#include <cstdlib>
|
2013-07-21 19:10:34 +02:00
|
|
|
#include <cstring>
|
2016-07-17 18:11:24 +02:00
|
|
|
#include <iostream>
|
2012-07-02 19:50:14 +02:00
|
|
|
|
|
|
|
#include "cppcheck.h"
|
2015-11-25 19:33:04 +01:00
|
|
|
#include "version.h"
|
2012-07-02 19:50:14 +02:00
|
|
|
|
|
|
|
static void unencode(const char *src, char *dest)
|
|
|
|
{
|
2012-09-15 15:32:59 +02:00
|
|
|
for (; *src; src++, dest++) {
|
|
|
|
if (*src == '+')
|
|
|
|
*dest = ' ';
|
|
|
|
else if (*src == '%') {
|
2016-01-15 09:45:47 +01:00
|
|
|
unsigned int code;
|
2016-07-17 18:11:24 +02:00
|
|
|
if (std::sscanf(src+1, "%2x", &code) != 1)
|
2012-09-15 15:32:59 +02:00
|
|
|
code = '?';
|
|
|
|
*dest = code;
|
|
|
|
src += 2;
|
|
|
|
} else
|
|
|
|
*dest = *src;
|
2012-07-02 19:50:14 +02:00
|
|
|
}
|
|
|
|
*dest = '\0';
|
|
|
|
}
|
|
|
|
|
2016-07-17 18:11:24 +02:00
|
|
|
static FILE *logfile = nullptr;
|
2012-07-02 19:50:14 +02:00
|
|
|
|
2012-09-15 15:32:59 +02:00
|
|
|
class CppcheckExecutor : public ErrorLogger {
|
2012-07-02 19:50:14 +02:00
|
|
|
private:
|
|
|
|
const std::time_t stoptime;
|
|
|
|
CppCheck cppcheck;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CppcheckExecutor()
|
2012-09-15 15:32:59 +02:00
|
|
|
: ErrorLogger()
|
2016-07-17 18:11:24 +02:00
|
|
|
, stoptime(std::time(nullptr)+2U)
|
|
|
|
, cppcheck(*this, false) {
|
2012-07-02 19:50:14 +02:00
|
|
|
cppcheck.settings().addEnabled("all");
|
|
|
|
cppcheck.settings().inconclusive = true;
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void run(const char code[]) {
|
2016-07-17 18:11:24 +02:00
|
|
|
cppcheck.check("test.cpp", code);
|
2012-07-02 19:50:14 +02:00
|
|
|
}
|
|
|
|
|
2019-01-13 13:18:50 +01:00
|
|
|
void reportOut(const std::string &outmsg) override { }
|
|
|
|
void reportErr(const ErrorLogger::ErrorMessage &msg) override {
|
2015-11-23 09:27:33 +01:00
|
|
|
const std::string s = msg.toString(true);
|
|
|
|
|
2016-07-17 18:11:24 +02:00
|
|
|
std::cout << s << std::endl;
|
2015-11-23 09:27:33 +01:00
|
|
|
|
2016-07-17 18:11:24 +02:00
|
|
|
if (logfile != nullptr)
|
|
|
|
std::fprintf(logfile, "%s\n", s.c_str());
|
2012-07-02 19:50:14 +02:00
|
|
|
}
|
|
|
|
|
2016-07-17 18:11:24 +02:00
|
|
|
void reportProgress(const std::string& filename,
|
2012-09-15 15:32:59 +02:00
|
|
|
const char stage[],
|
2019-02-09 13:31:50 +01:00
|
|
|
const std::size_t value) override {
|
2016-07-17 18:11:24 +02:00
|
|
|
if (std::time(nullptr) >= stoptime) {
|
|
|
|
std::cout << "Time to analyse the code exceeded 2 seconds. Terminating.\n\n";
|
2012-07-02 19:50:14 +02:00
|
|
|
cppcheck.terminate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2016-07-17 18:11:24 +02:00
|
|
|
std::cout << "Content-type: text/html\r\n\r\n"
|
|
|
|
<< "<!DOCTYPE html>\n";
|
|
|
|
|
2012-07-02 19:50:14 +02:00
|
|
|
char data[4096] = {0};
|
2012-11-28 10:22:15 +01:00
|
|
|
|
2016-07-17 18:11:24 +02:00
|
|
|
const char *query_string = std::getenv("QUERY_STRING");
|
2014-01-01 15:55:18 +01:00
|
|
|
if (query_string)
|
|
|
|
std::strncpy(data, query_string, sizeof(data)-2);
|
|
|
|
|
2016-07-17 18:11:24 +02:00
|
|
|
const char *lenstr = std::getenv("CONTENT_LENGTH");
|
2012-11-28 10:22:15 +01:00
|
|
|
if (lenstr) {
|
2016-07-17 18:11:24 +02:00
|
|
|
int len = std::min(1 + std::atoi(lenstr), (int)(sizeof(data) - 2));
|
|
|
|
std::fgets(data, len, stdin);
|
2012-11-28 10:22:15 +01:00
|
|
|
}
|
2012-07-02 19:50:14 +02:00
|
|
|
|
2015-11-23 09:27:33 +01:00
|
|
|
if (data[4000] != '\0') {
|
2016-07-17 18:11:24 +02:00
|
|
|
std::cout << "<html><body>For performance reasons the code must be shorter than 1000 chars.</body></html>";
|
2014-01-01 15:55:18 +01:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-11-23 09:27:33 +01:00
|
|
|
const char *pdata = data;
|
|
|
|
if (std::strncmp(pdata, "code=", 5)==0)
|
|
|
|
pdata += 5;
|
|
|
|
|
|
|
|
char code[4096] = {0};
|
|
|
|
unencode(pdata, code);
|
|
|
|
|
2016-07-17 18:11:24 +02:00
|
|
|
logfile = std::fopen("democlient.log", "at");
|
|
|
|
if (logfile != nullptr)
|
|
|
|
std::fprintf(logfile, "===========================================================\n%s\n", code);
|
2013-07-13 18:15:20 +02:00
|
|
|
|
2016-07-17 18:11:24 +02:00
|
|
|
std::cout << "<html><body>Cppcheck " CPPCHECK_VERSION_STRING "<pre>";
|
2012-07-02 19:50:14 +02:00
|
|
|
|
|
|
|
CppcheckExecutor cppcheckExecutor;
|
|
|
|
cppcheckExecutor.run(code);
|
|
|
|
|
2016-07-17 18:11:24 +02:00
|
|
|
std::fclose(logfile);
|
|
|
|
|
|
|
|
std::cout << "</pre>Done!</body></html>";
|
2014-01-01 15:55:18 +01:00
|
|
|
|
2012-07-02 19:50:14 +02:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|