Exclude modules from being hooked; hook only selected GUI programs.
Added environment variable ANSICON_EXC to specify modules that should not be
hooked. This should work around the nvd3d9wrap.dll issue. Since it helps to
know what the modules are, logging is now always available, controlled by -l
or ANSICON_LOG. A side-effect caused debugstr.c to move to util.c.
GUI programs are once again not hooked, unless run by "ansicon" directly or in
the ANSICON_GUI environment variable. Since not hooking still leaves ANSICON in
the environment, created ANSICON_VER as a dynamic-only variable, which can also
serve as a version check.
Due to an email requesting a reverse video option, realised I always take the
current attributes as default. This means if you turned on reverse and ran a
program, it would take the reverse as its default. Created ANSICON_DEF variable
to explicitly set the default attribute, using the current if it doesn't exist.
The reverse video option is done via a "negative" attribute (e.g. "-m-f0" is
reversed black on white, meaning you'll get white on black, with foreground
sequences changing the background). (The difference from "\e[7m" is that it
won't be reset on "\e[m".)
A child program will inherit the parent's modes (but not shift); the parent will
read the child's modes on exit (but not unload). The exception is "ansicon",
which will always start with the default modes and leave the parent unchanged.
Improved the AutoRun entry, only running "ansicon" if ANSICON_VER doesn't exist.
The "ansicon" command is always first.
Stopped -u implying -p; return the program's exit code; don't restore the
original color when just using -p; output error messages to stderr.
2011-12-14 20:53:51 +10:00
|
|
|
/*
|
|
|
|
util.c - Utility functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ansicon.h"
|
2012-06-12 14:38:00 +10:00
|
|
|
#include "version.h"
|
Exclude modules from being hooked; hook only selected GUI programs.
Added environment variable ANSICON_EXC to specify modules that should not be
hooked. This should work around the nvd3d9wrap.dll issue. Since it helps to
know what the modules are, logging is now always available, controlled by -l
or ANSICON_LOG. A side-effect caused debugstr.c to move to util.c.
GUI programs are once again not hooked, unless run by "ansicon" directly or in
the ANSICON_GUI environment variable. Since not hooking still leaves ANSICON in
the environment, created ANSICON_VER as a dynamic-only variable, which can also
serve as a version check.
Due to an email requesting a reverse video option, realised I always take the
current attributes as default. This means if you turned on reverse and ran a
program, it would take the reverse as its default. Created ANSICON_DEF variable
to explicitly set the default attribute, using the current if it doesn't exist.
The reverse video option is done via a "negative" attribute (e.g. "-m-f0" is
reversed black on white, meaning you'll get white on black, with foreground
sequences changing the background). (The difference from "\e[7m" is that it
won't be reset on "\e[m".)
A child program will inherit the parent's modes (but not shift); the parent will
read the child's modes on exit (but not unload). The exception is "ansicon",
which will always start with the default modes and leave the parent unchanged.
Improved the AutoRun entry, only running "ansicon" if ANSICON_VER doesn't exist.
The "ansicon" command is always first.
Stopped -u implying -p; return the program's exit code; don't restore the
original color when just using -p; output error messages to stderr.
2011-12-14 20:53:51 +10:00
|
|
|
|
|
|
|
|
|
|
|
TCHAR prog_path[MAX_PATH];
|
|
|
|
LPTSTR prog;
|
2014-02-05 00:21:42 +10:00
|
|
|
|
Exclude modules from being hooked; hook only selected GUI programs.
Added environment variable ANSICON_EXC to specify modules that should not be
hooked. This should work around the nvd3d9wrap.dll issue. Since it helps to
know what the modules are, logging is now always available, controlled by -l
or ANSICON_LOG. A side-effect caused debugstr.c to move to util.c.
GUI programs are once again not hooked, unless run by "ansicon" directly or in
the ANSICON_GUI environment variable. Since not hooking still leaves ANSICON in
the environment, created ANSICON_VER as a dynamic-only variable, which can also
serve as a version check.
Due to an email requesting a reverse video option, realised I always take the
current attributes as default. This means if you turned on reverse and ran a
program, it would take the reverse as its default. Created ANSICON_DEF variable
to explicitly set the default attribute, using the current if it doesn't exist.
The reverse video option is done via a "negative" attribute (e.g. "-m-f0" is
reversed black on white, meaning you'll get white on black, with foreground
sequences changing the background). (The difference from "\e[7m" is that it
won't be reset on "\e[m".)
A child program will inherit the parent's modes (but not shift); the parent will
read the child's modes on exit (but not unload). The exception is "ansicon",
which will always start with the default modes and leave the parent unchanged.
Improved the AutoRun entry, only running "ansicon" if ANSICON_VER doesn't exist.
The "ansicon" command is always first.
Stopped -u implying -p; return the program's exit code; don't restore the
original color when just using -p; output error messages to stderr.
2011-12-14 20:53:51 +10:00
|
|
|
int log_level;
|
2014-02-05 00:21:42 +10:00
|
|
|
|
|
|
|
char ansi_dll[MAX_PATH];
|
|
|
|
DWORD ansi_len;
|
|
|
|
#ifdef _WIN64
|
|
|
|
char* ansi_bits;
|
|
|
|
#endif
|
Exclude modules from being hooked; hook only selected GUI programs.
Added environment variable ANSICON_EXC to specify modules that should not be
hooked. This should work around the nvd3d9wrap.dll issue. Since it helps to
know what the modules are, logging is now always available, controlled by -l
or ANSICON_LOG. A side-effect caused debugstr.c to move to util.c.
GUI programs are once again not hooked, unless run by "ansicon" directly or in
the ANSICON_GUI environment variable. Since not hooking still leaves ANSICON in
the environment, created ANSICON_VER as a dynamic-only variable, which can also
serve as a version check.
Due to an email requesting a reverse video option, realised I always take the
current attributes as default. This means if you turned on reverse and ran a
program, it would take the reverse as its default. Created ANSICON_DEF variable
to explicitly set the default attribute, using the current if it doesn't exist.
The reverse video option is done via a "negative" attribute (e.g. "-m-f0" is
reversed black on white, meaning you'll get white on black, with foreground
sequences changing the background). (The difference from "\e[7m" is that it
won't be reset on "\e[m".)
A child program will inherit the parent's modes (but not shift); the parent will
read the child's modes on exit (but not unload). The exception is "ansicon",
which will always start with the default modes and leave the parent unchanged.
Improved the AutoRun entry, only running "ansicon" if ANSICON_VER doesn't exist.
The "ansicon" command is always first.
Stopped -u implying -p; return the program's exit code; don't restore the
original color when just using -p; output error messages to stderr.
2011-12-14 20:53:51 +10:00
|
|
|
|
|
|
|
|
|
|
|
// Get just the name of the program: "C:\path\program.exe" -> "program".
|
|
|
|
// Returns a pointer within program; it is modified to remove the extension.
|
|
|
|
LPTSTR get_program_name( LPTSTR program )
|
|
|
|
{
|
|
|
|
LPTSTR name, ext;
|
|
|
|
|
|
|
|
if (program == NULL)
|
|
|
|
{
|
|
|
|
GetModuleFileName( NULL, prog_path, lenof(prog_path) );
|
|
|
|
program = prog_path;
|
|
|
|
}
|
|
|
|
name = wcsrchr( program, '\\' );
|
|
|
|
if (name != NULL)
|
|
|
|
++name;
|
|
|
|
else
|
|
|
|
name = program;
|
|
|
|
ext = wcsrchr( name, '.' );
|
|
|
|
if (ext != NULL && ext != name)
|
|
|
|
*ext = '\0';
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-05 00:21:42 +10:00
|
|
|
// Get the ANSI path of the DLL for the import. If it can't be converted,
|
|
|
|
// just use the name and hope it's on the PATH. Returns the length of the
|
|
|
|
// path/name, including padding to make it dword-aligned. The 64-bit version
|
|
|
|
// expects ansi_bits to point to the size within dll on entry.
|
|
|
|
void set_ansi_dll( LPTSTR dll )
|
|
|
|
{
|
|
|
|
BOOL bad;
|
|
|
|
|
|
|
|
ansi_len = WideCharToMultiByte( CP_ACP, WC_NO_BEST_FIT_CHARS, dll, -1,
|
|
|
|
NULL, 0, NULL, &bad );
|
|
|
|
if (bad || ansi_len > MAX_PATH)
|
|
|
|
{
|
|
|
|
ansi_len = 12;
|
|
|
|
memcpy( ansi_dll, "ANSI32.dll\0", 12 );
|
|
|
|
#ifdef _WIN64
|
|
|
|
if (*ansi_bits == '6')
|
|
|
|
{
|
|
|
|
ansi_dll[4] = '6';
|
|
|
|
ansi_dll[5] = '4';
|
|
|
|
}
|
|
|
|
ansi_bits = ansi_dll + 4;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WideCharToMultiByte( CP_ACP, WC_NO_BEST_FIT_CHARS, dll, -1,
|
|
|
|
ansi_dll, MAX_PATH, NULL, NULL );
|
|
|
|
#ifdef _WIN64
|
|
|
|
ansi_bits = ansi_dll + ansi_len - 7;
|
|
|
|
#endif
|
|
|
|
ansi_len = (ansi_len + 3) & ~3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Exclude modules from being hooked; hook only selected GUI programs.
Added environment variable ANSICON_EXC to specify modules that should not be
hooked. This should work around the nvd3d9wrap.dll issue. Since it helps to
know what the modules are, logging is now always available, controlled by -l
or ANSICON_LOG. A side-effect caused debugstr.c to move to util.c.
GUI programs are once again not hooked, unless run by "ansicon" directly or in
the ANSICON_GUI environment variable. Since not hooking still leaves ANSICON in
the environment, created ANSICON_VER as a dynamic-only variable, which can also
serve as a version check.
Due to an email requesting a reverse video option, realised I always take the
current attributes as default. This means if you turned on reverse and ran a
program, it would take the reverse as its default. Created ANSICON_DEF variable
to explicitly set the default attribute, using the current if it doesn't exist.
The reverse video option is done via a "negative" attribute (e.g. "-m-f0" is
reversed black on white, meaning you'll get white on black, with foreground
sequences changing the background). (The difference from "\e[7m" is that it
won't be reset on "\e[m".)
A child program will inherit the parent's modes (but not shift); the parent will
read the child's modes on exit (but not unload). The exception is "ansicon",
which will always start with the default modes and leave the parent unchanged.
Improved the AutoRun entry, only running "ansicon" if ANSICON_VER doesn't exist.
The "ansicon" command is always first.
Stopped -u implying -p; return the program's exit code; don't restore the
original color when just using -p; output error messages to stderr.
2011-12-14 20:53:51 +10:00
|
|
|
void DEBUGSTR( int level, LPTSTR szFormat, ... )
|
|
|
|
{
|
2014-02-05 00:21:42 +10:00
|
|
|
static char tempfile[MAX_PATH];
|
|
|
|
static DWORD pid;
|
|
|
|
|
|
|
|
TCHAR szBuffer[1024], szEscape[1024];
|
Exclude modules from being hooked; hook only selected GUI programs.
Added environment variable ANSICON_EXC to specify modules that should not be
hooked. This should work around the nvd3d9wrap.dll issue. Since it helps to
know what the modules are, logging is now always available, controlled by -l
or ANSICON_LOG. A side-effect caused debugstr.c to move to util.c.
GUI programs are once again not hooked, unless run by "ansicon" directly or in
the ANSICON_GUI environment variable. Since not hooking still leaves ANSICON in
the environment, created ANSICON_VER as a dynamic-only variable, which can also
serve as a version check.
Due to an email requesting a reverse video option, realised I always take the
current attributes as default. This means if you turned on reverse and ran a
program, it would take the reverse as its default. Created ANSICON_DEF variable
to explicitly set the default attribute, using the current if it doesn't exist.
The reverse video option is done via a "negative" attribute (e.g. "-m-f0" is
reversed black on white, meaning you'll get white on black, with foreground
sequences changing the background). (The difference from "\e[7m" is that it
won't be reset on "\e[m".)
A child program will inherit the parent's modes (but not shift); the parent will
read the child's modes on exit (but not unload). The exception is "ansicon",
which will always start with the default modes and leave the parent unchanged.
Improved the AutoRun entry, only running "ansicon" if ANSICON_VER doesn't exist.
The "ansicon" command is always first.
Stopped -u implying -p; return the program's exit code; don't restore the
original color when just using -p; output error messages to stderr.
2011-12-14 20:53:51 +10:00
|
|
|
va_list pArgList;
|
2014-02-05 00:21:42 +10:00
|
|
|
HANDLE mutex;
|
|
|
|
DWORD wait;
|
|
|
|
FILE* file;
|
Exclude modules from being hooked; hook only selected GUI programs.
Added environment variable ANSICON_EXC to specify modules that should not be
hooked. This should work around the nvd3d9wrap.dll issue. Since it helps to
know what the modules are, logging is now always available, controlled by -l
or ANSICON_LOG. A side-effect caused debugstr.c to move to util.c.
GUI programs are once again not hooked, unless run by "ansicon" directly or in
the ANSICON_GUI environment variable. Since not hooking still leaves ANSICON in
the environment, created ANSICON_VER as a dynamic-only variable, which can also
serve as a version check.
Due to an email requesting a reverse video option, realised I always take the
current attributes as default. This means if you turned on reverse and ran a
program, it would take the reverse as its default. Created ANSICON_DEF variable
to explicitly set the default attribute, using the current if it doesn't exist.
The reverse video option is done via a "negative" attribute (e.g. "-m-f0" is
reversed black on white, meaning you'll get white on black, with foreground
sequences changing the background). (The difference from "\e[7m" is that it
won't be reset on "\e[m".)
A child program will inherit the parent's modes (but not shift); the parent will
read the child's modes on exit (but not unload). The exception is "ansicon",
which will always start with the default modes and leave the parent unchanged.
Improved the AutoRun entry, only running "ansicon" if ANSICON_VER doesn't exist.
The "ansicon" command is always first.
Stopped -u implying -p; return the program's exit code; don't restore the
original color when just using -p; output error messages to stderr.
2011-12-14 20:53:51 +10:00
|
|
|
|
|
|
|
if ((log_level & 3) < level && !(level & 4 & log_level))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (*tempfile == '\0')
|
|
|
|
{
|
|
|
|
_snprintf( tempfile, MAX_PATH, "%s\\ansicon.log", getenv( "TEMP" ) );
|
|
|
|
pid = GetCurrentProcessId();
|
|
|
|
}
|
|
|
|
if (szFormat == NULL)
|
|
|
|
{
|
2014-02-05 00:21:42 +10:00
|
|
|
// Explicitly use 't', as _fmode might be binary.
|
2012-11-24 23:41:29 +10:00
|
|
|
file = fopen( tempfile, (log_level & 8) ? "at" : "wt" );
|
Exclude modules from being hooked; hook only selected GUI programs.
Added environment variable ANSICON_EXC to specify modules that should not be
hooked. This should work around the nvd3d9wrap.dll issue. Since it helps to
know what the modules are, logging is now always available, controlled by -l
or ANSICON_LOG. A side-effect caused debugstr.c to move to util.c.
GUI programs are once again not hooked, unless run by "ansicon" directly or in
the ANSICON_GUI environment variable. Since not hooking still leaves ANSICON in
the environment, created ANSICON_VER as a dynamic-only variable, which can also
serve as a version check.
Due to an email requesting a reverse video option, realised I always take the
current attributes as default. This means if you turned on reverse and ran a
program, it would take the reverse as its default. Created ANSICON_DEF variable
to explicitly set the default attribute, using the current if it doesn't exist.
The reverse video option is done via a "negative" attribute (e.g. "-m-f0" is
reversed black on white, meaning you'll get white on black, with foreground
sequences changing the background). (The difference from "\e[7m" is that it
won't be reset on "\e[m".)
A child program will inherit the parent's modes (but not shift); the parent will
read the child's modes on exit (but not unload). The exception is "ansicon",
which will always start with the default modes and leave the parent unchanged.
Improved the AutoRun entry, only running "ansicon" if ANSICON_VER doesn't exist.
The "ansicon" command is always first.
Stopped -u implying -p; return the program's exit code; don't restore the
original color when just using -p; output error messages to stderr.
2011-12-14 20:53:51 +10:00
|
|
|
if (file != NULL)
|
|
|
|
{
|
|
|
|
SYSTEMTIME now;
|
|
|
|
GetLocalTime( &now );
|
2013-07-19 00:14:54 +10:00
|
|
|
fprintf( file, "ANSICON (" BITSA "-bit) v" PVERSA " log (%d) started "
|
2012-06-12 14:38:00 +10:00
|
|
|
"%d-%.2d-%.2d %d:%.2d:%.2d\n",
|
|
|
|
log_level,
|
Exclude modules from being hooked; hook only selected GUI programs.
Added environment variable ANSICON_EXC to specify modules that should not be
hooked. This should work around the nvd3d9wrap.dll issue. Since it helps to
know what the modules are, logging is now always available, controlled by -l
or ANSICON_LOG. A side-effect caused debugstr.c to move to util.c.
GUI programs are once again not hooked, unless run by "ansicon" directly or in
the ANSICON_GUI environment variable. Since not hooking still leaves ANSICON in
the environment, created ANSICON_VER as a dynamic-only variable, which can also
serve as a version check.
Due to an email requesting a reverse video option, realised I always take the
current attributes as default. This means if you turned on reverse and ran a
program, it would take the reverse as its default. Created ANSICON_DEF variable
to explicitly set the default attribute, using the current if it doesn't exist.
The reverse video option is done via a "negative" attribute (e.g. "-m-f0" is
reversed black on white, meaning you'll get white on black, with foreground
sequences changing the background). (The difference from "\e[7m" is that it
won't be reset on "\e[m".)
A child program will inherit the parent's modes (but not shift); the parent will
read the child's modes on exit (but not unload). The exception is "ansicon",
which will always start with the default modes and leave the parent unchanged.
Improved the AutoRun entry, only running "ansicon" if ANSICON_VER doesn't exist.
The "ansicon" command is always first.
Stopped -u implying -p; return the program's exit code; don't restore the
original color when just using -p; output error messages to stderr.
2011-12-14 20:53:51 +10:00
|
|
|
now.wYear, now.wMonth, now.wDay,
|
|
|
|
now.wHour, now.wMinute, now.wSecond );
|
|
|
|
fclose( file );
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start( pArgList, szFormat );
|
|
|
|
_vsnwprintf( szBuffer, lenof(szBuffer), szFormat, pArgList );
|
|
|
|
va_end( pArgList );
|
|
|
|
|
|
|
|
szFormat = szBuffer;
|
|
|
|
if (*szFormat == '\33')
|
|
|
|
{
|
|
|
|
BOOL first = TRUE;
|
|
|
|
LPTSTR pos = szEscape;
|
|
|
|
while (*++szFormat != '\0' && pos < szEscape + lenof(szEscape) - 4)
|
|
|
|
{
|
|
|
|
if (*szFormat < 32)
|
|
|
|
{
|
|
|
|
*pos++ = '\\';
|
|
|
|
switch (*szFormat)
|
|
|
|
{
|
|
|
|
case '\a': *pos++ = 'a'; break;
|
|
|
|
case '\b': *pos++ = 'b'; break;
|
|
|
|
case '\t': *pos++ = 't'; break;
|
|
|
|
case '\r': *pos++ = 'r'; break;
|
|
|
|
case '\n': *pos++ = 'n'; break;
|
|
|
|
case 27 : *pos++ = 'e'; break;
|
|
|
|
default:
|
|
|
|
pos += _snwprintf( pos, 32, L"%.*o",
|
|
|
|
(szFormat[1] >= '0' && szFormat[1] <= '7') ? 3 : 1,
|
|
|
|
*szFormat );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (*szFormat == '"')
|
|
|
|
{
|
|
|
|
if (first)
|
|
|
|
first = FALSE;
|
|
|
|
else if (szFormat[1] != '\0')
|
|
|
|
*pos++ = '\\';
|
|
|
|
}
|
|
|
|
*pos++ = *szFormat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*pos = '\0';
|
|
|
|
szFormat = szEscape;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex = CreateMutex( NULL, FALSE, L"ANSICON_debug_file" );
|
|
|
|
wait = WaitForSingleObject( mutex, 500 );
|
2014-02-05 00:21:42 +10:00
|
|
|
file = fopen( tempfile, "at" );
|
Exclude modules from being hooked; hook only selected GUI programs.
Added environment variable ANSICON_EXC to specify modules that should not be
hooked. This should work around the nvd3d9wrap.dll issue. Since it helps to
know what the modules are, logging is now always available, controlled by -l
or ANSICON_LOG. A side-effect caused debugstr.c to move to util.c.
GUI programs are once again not hooked, unless run by "ansicon" directly or in
the ANSICON_GUI environment variable. Since not hooking still leaves ANSICON in
the environment, created ANSICON_VER as a dynamic-only variable, which can also
serve as a version check.
Due to an email requesting a reverse video option, realised I always take the
current attributes as default. This means if you turned on reverse and ran a
program, it would take the reverse as its default. Created ANSICON_DEF variable
to explicitly set the default attribute, using the current if it doesn't exist.
The reverse video option is done via a "negative" attribute (e.g. "-m-f0" is
reversed black on white, meaning you'll get white on black, with foreground
sequences changing the background). (The difference from "\e[7m" is that it
won't be reset on "\e[m".)
A child program will inherit the parent's modes (but not shift); the parent will
read the child's modes on exit (but not unload). The exception is "ansicon",
which will always start with the default modes and leave the parent unchanged.
Improved the AutoRun entry, only running "ansicon" if ANSICON_VER doesn't exist.
The "ansicon" command is always first.
Stopped -u implying -p; return the program's exit code; don't restore the
original color when just using -p; output error messages to stderr.
2011-12-14 20:53:51 +10:00
|
|
|
if (file != NULL)
|
|
|
|
{
|
|
|
|
fwprintf( file, L"%s (%lu): %s\n", prog, pid, szFormat );
|
|
|
|
fclose( file );
|
|
|
|
}
|
|
|
|
if (wait == WAIT_OBJECT_0)
|
|
|
|
ReleaseMutex( mutex );
|
|
|
|
CloseHandle( mutex );
|
|
|
|
}
|