Democlient:

- Add <!DOCTYPE html> to output
- Avoid closing/reopening logfile
- Use C++ instead of C where possible
This commit is contained in:
PKEuS 2016-07-17 18:11:24 +02:00
parent 45ee29d5dc
commit 9422f0c558
1 changed files with 27 additions and 32 deletions

View File

@ -2,7 +2,7 @@
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <algorithm> #include <iostream>
#include "cppcheck.h" #include "cppcheck.h"
#include "version.h" #include "version.h"
@ -14,7 +14,7 @@ static void unencode(const char *src, char *dest)
*dest = ' '; *dest = ' ';
else if (*src == '%') { else if (*src == '%') {
unsigned int code; unsigned int code;
if (sscanf(src+1, "%2x", &code) != 1) if (std::sscanf(src+1, "%2x", &code) != 1)
code = '?'; code = '?';
*dest = code; *dest = code;
src += 2; src += 2;
@ -24,6 +24,7 @@ static void unencode(const char *src, char *dest)
*dest = '\0'; *dest = '\0';
} }
static FILE *logfile = nullptr;
class CppcheckExecutor : public ErrorLogger { class CppcheckExecutor : public ErrorLogger {
private: private:
@ -33,38 +34,31 @@ private:
public: public:
CppcheckExecutor() CppcheckExecutor()
: ErrorLogger() : ErrorLogger()
, stoptime(std::time(NULL)+2U) , stoptime(std::time(nullptr)+2U)
, cppcheck(*this,false) { , cppcheck(*this, false) {
cppcheck.settings().addEnabled("all"); cppcheck.settings().addEnabled("all");
cppcheck.settings().inconclusive = true; cppcheck.settings().inconclusive = true;
} }
void run(const char code[]) { void run(const char code[]) {
cppcheck.check("test.c", code); cppcheck.check("test.cpp", code);
} }
void reportOut(const std::string &outmsg) { } void reportOut(const std::string &outmsg) { }
void reportErr(const ErrorLogger::ErrorMessage &msg) { void reportErr(const ErrorLogger::ErrorMessage &msg) {
const std::string s = msg.toString(true); const std::string s = msg.toString(true);
printf("%s\n", s.c_str()); std::cout << s << std::endl;
FILE *logfile = fopen("democlient.log", "at"); if (logfile != nullptr)
if (logfile != NULL) { std::fprintf(logfile, "%s\n", s.c_str());
fprintf(logfile, "%s\n", s.c_str());
fclose(logfile);
}
} }
void reportProgress(const void reportProgress(const std::string& filename,
std::string &filename,
const char stage[], const char stage[],
const unsigned int value) { const unsigned int value) {
if (std::time(NULL) >= stoptime) { if (std::time(nullptr) >= stoptime) {
printf("time to analyse the " std::cout << "Time to analyse the code exceeded 2 seconds. Terminating.\n\n";
"code is more than 1 "
"second. terminating."
"\n\n");
cppcheck.terminate(); cppcheck.terminate();
} }
} }
@ -73,21 +67,23 @@ public:
int main() int main()
{ {
std::cout << "Content-type: text/html\r\n\r\n"
<< "<!DOCTYPE html>\n";
char data[4096] = {0}; char data[4096] = {0};
const char *query_string = getenv("QUERY_STRING"); const char *query_string = std::getenv("QUERY_STRING");
if (query_string) if (query_string)
std::strncpy(data, query_string, sizeof(data)-2); std::strncpy(data, query_string, sizeof(data)-2);
const char *lenstr = getenv("CONTENT_LENGTH"); const char *lenstr = std::getenv("CONTENT_LENGTH");
if (lenstr) { if (lenstr) {
int len = std::min(1 + atoi(lenstr), (int)(sizeof(data) - 2)); int len = std::min(1 + std::atoi(lenstr), (int)(sizeof(data) - 2));
fgets(data, len, stdin); std::fgets(data, len, stdin);
} }
if (data[4000] != '\0') { if (data[4000] != '\0') {
puts("Content-type: text/html\r\n\r\n"); std::cout << "<html><body>For performance reasons the code must be shorter than 1000 chars.</body></html>";
puts("<html><body>For performance reasons the code must be shorter than 1000 chars.</body></html>");
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -98,19 +94,18 @@ int main()
char code[4096] = {0}; char code[4096] = {0};
unencode(pdata, code); unencode(pdata, code);
FILE *logfile = fopen("democlient.log", "at"); logfile = std::fopen("democlient.log", "at");
if (logfile != NULL) { if (logfile != nullptr)
fprintf(logfile, "===========================================================\n%s\n", code); std::fprintf(logfile, "===========================================================\n%s\n", code);
fclose(logfile);
}
puts("Content-type: text/html\r\n\r\n"); std::cout << "<html><body>Cppcheck " CPPCHECK_VERSION_STRING "<pre>";
puts("<html><body>Cppcheck version " CPPCHECK_VERSION_STRING "<pre>");
CppcheckExecutor cppcheckExecutor; CppcheckExecutor cppcheckExecutor;
cppcheckExecutor.run(code); cppcheckExecutor.run(code);
puts("</pre>Done!</body></html>"); std::fclose(logfile);
std::cout << "</pre>Done!</body></html>";
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }