cppcheck/democlient/democlient.cpp

104 lines
2.5 KiB
C++
Raw Normal View History

#include <ctime>
#include <cstdio>
2012-07-02 19:50:14 +02:00
#include <cstdlib>
2013-07-21 19:10:34 +02:00
#include <cstring>
2012-07-02 19:50:14 +02:00
#include <algorithm>
#include "cppcheck.h"
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 == '%') {
int code;
if (sscanf(src+1, "%2x", &code) != 1)
code = '?';
*dest = code;
src += 2;
} else
*dest = *src;
2012-07-02 19:50:14 +02:00
}
*dest = '\0';
}
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()
, stoptime(std::time(NULL)+2U)
, cppcheck(*this,false) {
2012-07-02 19:50:14 +02:00
cppcheck.settings().addEnabled("all");
cppcheck.settings().inconclusive = true;
}
2012-09-15 15:32:59 +02:00
void run(const char code[]) {
2012-07-02 19:50:14 +02:00
cppcheck.check("test.c", code);
}
void reportOut(const std::string &outmsg) { }
2012-09-15 15:32:59 +02:00
void reportErr(const ErrorLogger::ErrorMessage &msg) {
2014-01-01 15:55:18 +01:00
printf("%s\n", msg.toString(true).c_str());
2012-07-02 19:50:14 +02:00
}
2012-09-15 15:32:59 +02:00
void reportProgress(const
std::string &filename,
const char stage[],
const unsigned int value) {
if (std::time(NULL) >= stoptime) {
2012-07-02 19:50:14 +02:00
printf("time to analyse the "
"code is more than 1 "
"second. terminating."
"\n\n");
cppcheck.terminate();
}
}
};
int main()
{
char data[4096] = {0};
2014-01-01 15:55:18 +01:00
const char *query_string = getenv("QUERY_STRING");
if (query_string)
std::strncpy(data, query_string, sizeof(data)-2);
const char *lenstr = getenv("CONTENT_LENGTH");
if (lenstr) {
int len = std::min(1 + atoi(lenstr), (int)(sizeof(data) - 2));
fgets(data, len, stdin);
}
2012-07-02 19:50:14 +02:00
char code[4096] = {0};
unencode(data, code);
2014-01-01 15:55:18 +01:00
if (strlen(code) > 1000) {
puts("Content-type: text/html\r\n\r\n");
puts("<html><body>For performance reasons the code must be shorter than 1000 chars.</body></html>");
return EXIT_SUCCESS;
}
2013-07-13 18:15:20 +02:00
FILE *logfile = fopen("democlient.log", "at");
if (logfile != NULL) {
fprintf(logfile, "===========================================================\n%s\n", code);
fclose(logfile);
}
2014-01-01 15:55:18 +01:00
puts("Content-type: text/html\r\n\r\n");
puts("<html><body><pre>");
2012-07-02 19:50:14 +02:00
CppcheckExecutor cppcheckExecutor;
cppcheckExecutor.run(code);
2014-01-01 15:55:18 +01:00
puts("</pre>Done!</body></html>");
2012-07-02 19:50:14 +02:00
return EXIT_SUCCESS;
}