]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
C++-ify basic_fake_auth
authorFrancesco Chemolli <kinkie@squid-cache.org>
Mon, 21 Dec 2015 10:22:45 +0000 (11:22 +0100)
committerFrancesco Chemolli <kinkie@squid-cache.org>
Mon, 21 Dec 2015 10:22:45 +0000 (11:22 +0100)
compat/debug.h
helpers/basic_auth/fake/fake.cc
helpers/defines.h

index a00bc28d11aef4f7ae09dd44456314589652a6ca..b38c7b80d934d19ae5a9fcf853d2c66ff08d15aa 100644 (file)
@@ -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. */
index c111ff9cf84775b9972cb87468a139481d059298..ae1616518369e7d2d1bfa11e4768510889c87296 100644 (file)
 #include "squid.h"
 #include "helpers/defines.h"
 
-#include <cstring>
+#include <iostream>
+#include <string>
 
 /**
  * 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<char>(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;
 }
 
index cb7b9e60b1f0da9b8f1738d0f8e010714ab6e4c9..5a1c3afebb0d89a3b93041e58b10bbd70ba9e147 100644 (file)
 #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 */