forked from danmar/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemoclient.cpp
116 lines (94 loc) · 2.89 KB
/
democlient.cpp
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
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include "cppcheck.h"
#include "version.h"
static void unencode(const char *src, char *dest)
{
for (; *src; src++, dest++) {
if (*src == '+')
*dest = ' ';
else if (*src == '%') {
unsigned int code;
if (sscanf(src+1, "%2x", &code) != 1)
code = '?';
*dest = code;
src += 2;
} else
*dest = *src;
}
*dest = '\0';
}
class CppcheckExecutor : public ErrorLogger {
private:
const std::time_t stoptime;
CppCheck cppcheck;
public:
CppcheckExecutor()
: ErrorLogger()
, stoptime(std::time(NULL)+2U)
, cppcheck(*this,false) {
cppcheck.settings().addEnabled("all");
cppcheck.settings().inconclusive = true;
}
void run(const char code[]) {
cppcheck.check("test.c", code);
}
void reportOut(const std::string &outmsg) { }
void reportErr(const ErrorLogger::ErrorMessage &msg) {
const std::string s = msg.toString(true);
printf("%s\n", s.c_str());
FILE *logfile = fopen("democlient.log", "at");
if (logfile != NULL) {
fprintf(logfile, "%s\n", s.c_str());
fclose(logfile);
}
}
void reportProgress(const
std::string &filename,
const char stage[],
const unsigned int value) {
if (std::time(NULL) >= stoptime) {
printf("time to analyse the "
"code is more than 1 "
"second. terminating."
"\n\n");
cppcheck.terminate();
}
}
};
int main()
{
char data[4096] = {0};
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);
}
if (data[4000] != '\0') {
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;
}
const char *pdata = data;
if (std::strncmp(pdata, "code=", 5)==0)
pdata += 5;
char code[4096] = {0};
unencode(pdata, code);
FILE *logfile = fopen("democlient.log", "at");
if (logfile != NULL) {
fprintf(logfile, "===========================================================\n%s\n", code);
fclose(logfile);
}
puts("Content-type: text/html\r\n\r\n");
puts("<html><body>Cppcheck version " CPPCHECK_VERSION_STRING "<pre>");
CppcheckExecutor cppcheckExecutor;
cppcheckExecutor.run(code);
puts("</pre>Done!</body></html>");
return EXIT_SUCCESS;
}