From: Francesco Chemolli Date: Mon, 21 Dec 2015 10:22:45 +0000 (+0100) Subject: C++-ify basic_fake_auth X-Git-Tag: SQUID_4_0_4~31 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=900809ea458a449aec1b18387addc2f82d41b444;p=thirdparty%2Fsquid.git C++-ify basic_fake_auth --- diff --git a/compat/debug.h b/compat/debug.h index a00bc28d11..b38c7b80d9 100644 --- a/compat/debug.h +++ b/compat/debug.h @@ -31,6 +31,12 @@ SQUIDCEXTERN int debug_enabled; fprintf(stderr,X); \ } else (void)0 +#define ndebug(content) ndebug_(__FILE__, __LINE__, content) +#define ndebug_(file, line, content) if (debug_enabled) { \ + std::cerr << file << '(' << line << ')' << ": pid=" << getpid() << ':' \ + << content << std::endl; \ + } else (void)0 + #else /* __GNUC__ */ /* non-GCC compilers can't do the above macro define yet. */ diff --git a/helpers/basic_auth/fake/fake.cc b/helpers/basic_auth/fake/fake.cc index c111ff9cf8..ae16165183 100644 --- a/helpers/basic_auth/fake/fake.cc +++ b/helpers/basic_auth/fake/fake.cc @@ -45,23 +45,23 @@ #include "squid.h" #include "helpers/defines.h" -#include +#include +#include /** * options: * -d enable debugging. * -h interface help. */ -char *program_name = NULL; +std::string program_name; static void usage(void) { - fprintf(stderr, - "Usage: %s [-d] [-v] [-h]\n" - " -d enable debugging.\n" - " -h this message\n\n", - program_name); + std::cerr << + "Usage: " << program_name << " [-d] [-h]" << std::endl << + " -d enable debugging." << std::endl << + " -h this message" << std::endl << std::endl; } static void @@ -79,7 +79,8 @@ process_options(int argc, char *argv[]) usage(); exit(0); default: - fprintf(stderr, "%s: FATAL: unknown option: -%c. Exiting\n", program_name, opt); + std::cerr << program_name << ": FATAL: unknown option: -" << + static_cast(optopt) << ". Exiting" << std::endl; usage(); exit(1); } @@ -89,33 +90,20 @@ process_options(int argc, char *argv[]) int main(int argc, char *argv[]) { - char buf[HELPER_INPUT_BUFFER]; - int buflen = 0; - - setbuf(stdout, NULL); - setbuf(stderr, NULL); - program_name = argv[0]; - process_options(argc, argv); - debug("%s " VERSION " " SQUID_BUILD_INFO " starting up...\n", program_name); - - while (fgets(buf, HELPER_INPUT_BUFFER, stdin) != NULL) { - char *p; - - if ((p = strchr(buf, '\n')) != NULL) { - *p = '\0'; /* strip \n */ - buflen = p - buf; /* length is known already */ - } else - buflen = strlen(buf); /* keep this so we only scan the buffer for \0 once per loop */ - - debug("Got %d bytes '%s' from Squid\n", buflen, buf); + ndebug(program_name << ' ' << VERSION << ' ' << SQUID_BUILD_INFO << + " starting up..."); + std::string buf; + while (getline(std::cin,buf)) { // will return false at EOF + ndebug("Got " << buf.length() << " bytes '" << buf << "' from Squid"); /* send 'OK' result back to Squid */ SEND_OK(""); } - debug("%s " VERSION " " SQUID_BUILD_INFO " shutting down...\n", program_name); - exit(0); + ndebug(program_name << ' ' << VERSION << ' ' << SQUID_BUILD_INFO << + " shutting down..."); + return 0; } diff --git a/helpers/defines.h b/helpers/defines.h index cb7b9e60b1..5a1c3afebb 100644 --- a/helpers/defines.h +++ b/helpers/defines.h @@ -46,16 +46,16 @@ #define HELPER_INPUT_BUFFER 8196 /* send OK result to Squid with a string parameter. */ -#define SEND_OK(x) fprintf(stdout, "OK %s\n",x) +#define SEND_OK(x) std::cout << "OK " << x << std::endl /* send ERR result to Squid with a string parameter. */ -#define SEND_ERR(x) fprintf(stdout, "ERR %s\n",x) +#define SEND_ERR(x) std::cout << "ERR " << x << std::endl /* send ERR result to Squid with a string parameter. */ -#define SEND_BH(x) fprintf(stdout, "BH %s\n",x) +#define SEND_BH(x) std::cout << "BH " << x << std::endl /* send TT result to Squid with a string parameter. */ -#define SEND_TT(x) fprintf(stdout, "TT %s\n",x) +#define SEND_TT(x) std::cout << "TT " << x << std::endl #endif /* __SQUID_HELPERS_DEFINES_H */