--- SVN-Latests/nsock/tests/tests_main.c 2013-08-10 22:06:37 +0000 +++ nsock/tests/tests_main.c 2013-08-10 22:28:34 +0000 @@ -3,22 +3,39 @@ * Same license as nmap -- see http://nmap.org/book/man-legal.html */ - #include "test-common.h" +#ifdef WIN32 + #define BOLDRED (FOREGROUND_RED | FOREGROUND_INTENSITY) + #define BOLDGREEN (FOREGROUND_GREEN | FOREGROUND_INTENSITY) + + /* Initial WinCon state */ + static CONSOLE_SCREEN_BUFFER_INFO csbi; + static HANDLE stdout_hnd = INVALID_HANDLE_VALUE; + + static void win_init (void); + static void set_attr (int attr); + + #define PRINT_OK() do { \ + printf ("["); set_attr (BOLDGREEN); \ + printf ("OK"); set_attr (0); \ + printf ("]\n"); \ + } while (0) + + #define PRINT_FAILED(str) do { \ + printf ("["); set_attr (BOLDRED); \ + printf ("FAILED"); set_attr (0); \ + printf ("] (%s)\n", str); \ + } while (0) -#ifndef WIN32 +#else #define RESET "\033[0m" #define BOLDRED "\033[1m\033[31m" #define BOLDGREEN "\033[1m\033[32m" - #define TEST_FAILED "[" BOLDRED "FAILED" RESET "]" - #define TEST_OK "[" BOLDGREEN "OK" RESET "]" -#else - /* WIN32 terminal has no ANSI driver */ - #define TEST_FAILED "[FAILED]" - #define TEST_OK "[OK]" -#endif + #define PRINT_OK() printf ("[" BOLDGREEN "OK" RESET "]\n"); + #define PRINT_FAILED(str) printf ("[" BOLDRED "FAILED" RESET "] (%s)\n", str); +#endif /* socket_strerror() comes from nbase @@ -66,6 +83,10 @@ int main(int ac, char **av) { int rc, i; +#ifdef WIN32 + win_init(); +#endif + for (i = 0; TestCases[i] != NULL; i++) { const struct test_case *current = TestCases[i]; const char *name = get_test_name(current); @@ -74,11 +95,30 @@ fflush(stdout); rc = test_case_run(current); if (rc) { - printf(TEST_FAILED " (%s)\n", socket_strerror(-rc)); + PRINT_FAILED (socket_strerror(-rc)); break; } - printf(TEST_OK "\n"); + PRINT_OK(); } return rc; } +#ifdef WIN32 +static void win_init (void) { + WSADATA data; + + stdout_hnd = GetStdHandle (STD_OUTPUT_HANDLE); + GetConsoleScreenBufferInfo (stdout_hnd, &csbi); + + if (WSAStartup(MAKEWORD(1,1), &data) != 0) + fatal ("failed to start winsock.\n"); +} + +/* Sets only foreground colour by masking out the default background colour bits. + */ +static void set_attr (int attr) { + if (attr) + SetConsoleTextAttribute (stdout_hnd, (csbi.wAttributes & 0xF0) | attr); + else SetConsoleTextAttribute (stdout_hnd, csbi.wAttributes); +} +#endif