Browse Source

Merge pull request #1997 from linusyang92/mingw-color

MinGW: Support colored text in Windows console
pull/1994/merge
Max Lv 7 years ago
committed by GitHub
parent
commit
f2ccc5d559
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 126 additions and 18 deletions
  1. 28
      docker/mingw/build.sh
  2. 3
      docker/mingw/deps.sh
  3. 19
      src/utils.h
  4. 88
      src/winsock.c
  5. 6
      src/winsock.h

28
docker/mingw/build.sh

@ -31,11 +31,15 @@ build_proj() {
dep=${PREFIX}/$arch
cd "$SRC"
git clone ${PROJ_URL} proj
cd proj
git checkout ${PROJ_REV}
git submodule update --init
./autogen.sh
if ! [ -d proj ]; then
git clone ${PROJ_URL} proj
cd proj
git checkout ${PROJ_REV}
git submodule update --init
./autogen.sh
else
cd proj
fi
./configure --host=${host} --prefix=${prefix} \
--disable-documentation \
--with-ev="$dep" \
@ -55,11 +59,15 @@ build_proj() {
PLUGIN_REV=master
cd "$SRC"
git clone ${PLUGIN_URL} plugin
cd plugin
git checkout ${PLUGIN_REV}
git submodule update --init
./autogen.sh
if ! [ -d plugin ]; then
git clone ${PLUGIN_URL} plugin
cd plugin
git checkout ${PLUGIN_REV}
git submodule update --init
./autogen.sh
else
cd plugin
fi
./configure --host=${host} --prefix=${prefix} \
--disable-documentation \
--with-ev="$dep"

3
docker/mingw/deps.sh

@ -60,8 +60,7 @@ build_deps() {
cd "$SRC/$PCRE_SRC"
./configure $args \
--enable-jit --disable-cpp \
--enable-unicode-properties \
--enable-pcre16 --enable-pcre32
--enable-unicode-properties
make clean
make install

19
src/utils.h

@ -98,8 +98,10 @@ extern FILE *logfile;
time_t now = time(NULL); \
char timestr[20]; \
strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \
fprintf(stdout, " %s INFO: " format "\n", timestr, \
## __VA_ARGS__); \
ss_color_info(); \
fprintf(stdout, " %s INFO: ", timestr); \
ss_color_reset(); \
fprintf(stdout, format "\n", ## __VA_ARGS__); \
} \
while (0)
@ -108,8 +110,10 @@ extern FILE *logfile;
time_t now = time(NULL); \
char timestr[20]; \
strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \
fprintf(stdout, " %s ERROR: " format "\n", timestr, \
## __VA_ARGS__); \
ss_color_error(); \
fprintf(stdout, " %s ERROR: ", timestr); \
ss_color_reset(); \
fprintf(stdout, format "\n", ## __VA_ARGS__); \
} \
while (0)
@ -196,7 +200,12 @@ extern int use_syslog;
#undef ERROR
#endif
#define ERROR(s) ss_error(s)
void ss_error(const char *s); // Implemented in winsock.c
// Implemented in winsock.c
void ss_error(const char *s);
void ss_color_info(void);
void ss_color_error(void);
void ss_color_reset(void);
#else
void ERROR(const char *s);
#endif

88
src/winsock.c

@ -33,6 +33,14 @@
#define STD_INPUT_HANDLE ((DWORD)-10)
#endif
#ifndef ENABLE_EXTENDED_FLAGS
#define ENABLE_EXTENDED_FLAGS 0x0080
#endif
#ifndef STD_OUTPUT_HANDLE
#define STD_OUTPUT_HANDLE ((DWORD)-11)
#endif
static void
disable_quick_edit(void)
{
@ -40,7 +48,9 @@ disable_quick_edit(void)
HANDLE console = GetStdHandle(STD_INPUT_HANDLE);
// Get current console mode
if (console == NULL || !GetConsoleMode(console, &mode)) {
if (console == NULL ||
console == INVALID_HANDLE_VALUE ||
!GetConsoleMode(console, &mode)) {
return;
}
@ -99,6 +109,82 @@ ss_error(const char *s)
}
}
char *
ss_gai_strerror(int ecode)
{
static TCHAR buff[GAI_STRERROR_BUFFER_SIZE + 1];
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_MAX_WIDTH_MASK,
NULL, ecode,
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
(LPTSTR)buff, GAI_STRERROR_BUFFER_SIZE, NULL);
return (char *)buff;
}
static BOOL
get_conattr(HANDLE console, WORD *out_attr)
{
static BOOL done = FALSE;
static WORD saved_attr = 0;
if (!done) {
CONSOLE_SCREEN_BUFFER_INFO info;
if (GetConsoleScreenBufferInfo(console, &info)) {
saved_attr = info.wAttributes;
done = TRUE;
}
}
if (out_attr != NULL) {
*out_attr = saved_attr;
}
return done;
}
static BOOL
set_concolor(WORD color, BOOL reset)
{
static HANDLE console = NULL;
if (console == NULL) {
console = GetStdHandle(STD_OUTPUT_HANDLE);
}
if (console == NULL ||
console == INVALID_HANDLE_VALUE) {
// If no console is available, we will not try again
console = INVALID_HANDLE_VALUE;
return FALSE;
}
WORD attr;
if (!get_conattr(console, &attr)) {
return FALSE;
}
if (!reset) {
// Only override foreground color without changing background
attr &= ~(FOREGROUND_RED | FOREGROUND_GREEN |
FOREGROUND_BLUE | FOREGROUND_INTENSITY);
attr |= (color | FOREGROUND_INTENSITY);
}
return SetConsoleTextAttribute(console, attr);
}
void
ss_color_info(void)
{
set_concolor(FOREGROUND_GREEN, FALSE);
}
void
ss_color_error(void)
{
set_concolor(FOREGROUND_RED | FOREGROUND_BLUE, FALSE);
}
void
ss_color_reset(void)
{
set_concolor(0, TRUE);
}
#ifdef TCP_FASTOPEN_WINSOCK
LPFN_CONNECTEX
winsock_getconnectex(void)

6
src/winsock.h

@ -98,6 +98,12 @@
#endif
#define ERROR(s) ss_error(s)
#ifdef gai_strerror
#undef gai_strerror
#endif
#define gai_strerror(e) ss_gai_strerror(e)
char *ss_gai_strerror(int ecode);
// Missing Unix functions
#define sleep(x) Sleep((x) * 1000)
#define bzero(s,n) memset(s,0,n)

Loading…
Cancel
Save