81 Commits

Author SHA1 Message Date
Jason Hood
06459edb69 Log CreateFile calls
Since I've hooked CreateFile use log level 32 to log how it's used,
providing a simple file monitor.
2018-08-23 17:34:34 +10:00
Jason Hood
eec487abb6 Prevent -p injecting when already injected
Don't inject into the parent if injection has already occurred.  This
prevents the reference count from increasing, allowing a single -pu to
unload, rather than one -pu for each -p.
2018-08-23 17:06:49 +10:00
Jason Hood
86512f67f6 Fix -e et al when redirecting to NUL
Redirecting ansicon to NUL would still write to the console, as NUL is
regarded as a character device.  Export IsConsoleHandle and use that
instead of _isatty.
2018-08-23 17:02:47 +10:00
Jason Hood
4e44ad519d Fix creating the wrap console buffer
If the console buffer was wider than the window then the wrap buffer
would not be created, resulting in erroneous movement.	Preserve the
height of the wrap buffer so the new width can be set.
2018-08-23 16:54:42 +10:00
Jason Hood
3ab935a1ad Release v1.84 2018-05-11 22:14:50 +10:00
Jason Hood
f2843b1d10 Explicitly link the VC exe with MSVCRT.dll
Create an import library containing the necessary CRT functions and link
with that, rather than the compiler's own version.
2018-05-11 15:14:58 +10:00
Jason Hood
abb0450de4 Workaround a Windows 10 1803 bug
I create a secondary buffer to assist with wrap detection.  On Win10
1803 writing to this buffer outputs to the active buffer.  Disable
virtual terminal processing to prevent it.
2018-05-10 21:57:52 +10:00
Jason Hood
e8ce3dffd3 Fixes
Fix RemoteLoad32 on 64-bit.
Better method to skip logging in ProcessType.
2018-05-09 11:37:34 +10:00
Jason Hood
9260451684 Scroll in new lines using the default attribute
When the window or buffer scrolls use the default attribute for the
resulting new lines, not the current.  Fixes #64.
2018-05-08 22:52:52 +10:00
Jason Hood
33ba31ad3c Remove dependence on the CRT; import DLL; fixes
Windows 10's MSVCRT will only work if the Win32 version in the header is
0 or 10.  Some PE's use it for something else, so when the DLL is
injected the process fails.  Provide custom routines for the C functions
used, so the DLL only depends on KERNEL32.

With the DLL independent of the CRT that would mean the exe would either
also need to be independent, or the source files would need to be built
twice (or just remove a linker warning).  Another option is to export
the functions from the DLL and have the exe import them, which turned
out to simplify things quite nicely.

A process that has a really long command line would not log properly, so
double the heap to accommodate it.

If ANSICON_DEF could not be parsed the default attribute would be zero
(black on black).  Use 7 or -7 instead.
2018-05-08 12:21:28 +10:00
Jason Hood
f8509c916c Inject by remote load if there's no IAT on Win8+
Windows 8 and later require the IDT to be within a section when there's
no IAT.  This prevents relocated imports from working, so we cannot add
ourself to the import table.  Use `LdrLoadDll` via `CreateRemoteThread`
for such a situation.
2018-05-04 11:45:10 +10:00
Jason Hood
852db64d91 Dynamically load WINMM, remove USER32
Prevent loading more libraries than necessary, so load WINMM the first
time the bell is used and use the CRT printf functions to avoid loading
USER32 at all.

I was also going to remove MSVCRT, but that turned out to be more
trouble than it's worth.  However, a side-effect that I kept is
replacing bsearch with a dedicated search routine.
2018-04-30 11:06:18 +10:00
Jason Hood
2cb2af78c8 Close the flush handles on detach 2018-04-26 10:02:58 +10:00
Jason Hood
3a16e68201 Create the flush thread on first use
Some processes just return from the entry point; this only exits the
thread, not the process.  It seems that when ANSICON created its flush
thread in DllMain, that became the primary thread, so when the entry
point returned the process was still waiting for the flush thread to
exit.  Creating the flush thread the first time it is used avoids this
problem, letting the process exit.
2018-02-16 17:18:25 +10:00
Jason Hood
a995352ff1 Use APIConsole for all console imports
Windows 10 has defined more console functions outside of `kernel32.dll`.
2018-02-13 12:31:40 +10:00
Jason Hood
af2c31c466 Add ANSICON_WRAP for programs that expect wrap
Some programs (notably less) rely on the right margin performing the
wrap.  Add such programs to the ANSICON_WRAP environment variable to
stop ANSICON suppressing the newline.
2018-02-12 16:44:55 +10:00
Jason Hood
3c61ef860d Improve palette usage
Support the entire 256-color palette.  When specifying an index or RGB
color, find the nearest match in the console palette.  Preserve bold and
underline when setting color by index.	Getting all palette entries will
stop at 15 if the initial index is below that (i.e. `0;*` will get 0 to
15 and `16;*` will get 16 to 255).
2017-12-30 11:13:03 +10:00
Jason Hood
44e5941a08 Fix CR and CRM issues
CR and BS would still be processed during CRM.	Fix all the partial CRM
sequences followed by a complete CRM sequence.

It's possible for some text files to have `\r\r\n` endings (Microsoft
converts LF to CRLF even when the CR is already there).  Collapse
multiple CRs to a single CR to catch this.  It is also necessary to
always delay flushing if the last character is CR, to see if the next
character is CR or LF.
2017-12-26 16:47:08 +10:00
Jason Hood
10575a0647 Recognise CR after wrap; update ANSICON_API docs
CR will match BS/CUB/HPB, moving back to previous line(s) after a wrap.

I hadn't updated the comment when I switched ANSICON_API from WriteFile
to WriteConsoleA.  Mention that is should be deleted when upgrading.
2017-12-24 10:11:57 +10:00
Jason Hood
a37657ac52 Add DECSTR and RIS
Implement soft and hard resets (handling intermediate bytes a little
better in the process).
2017-12-23 16:44:32 +10:00
Jason Hood
edaf7ccace Flush before accessing the console
Rather than flushing immediately after every write, only flush before
accessing the console.	This improves the speed, particularly for
programs that write character-by-character (which is basically every
program using Microsoft's printf).  Should it go wrong, there's a new
mode to restore the immediate flush: `\e[1+h`.
2017-12-22 18:08:51 +10:00
Jason Hood
77fabb78e0 Add DECOM, DECSTBM, SD and SU
Margins always use the window, never the buffer; the exception is
`\e[+r` which will remove the margins ('\e[r' will set the margins to
the window).  With the margins set, the window will not scroll when the
cursor is at the bottom, outside of the margins.  In this case, the
display is not quite right, since I copy the line from another buffer,
rather than actually overwrite the existing line.
2017-12-21 20:55:47 +10:00
Jason Hood
9242785570 Recognise wrap when moving backwards
BS/CUB/HPB will move back to previous line(s) if the line has wrapped.
2017-12-19 10:28:53 +10:00
Jason Hood
80d9e7da78 Add IRM
Setting IRM will cause characters to be inserted, discarding anything
that goes beyond the edge.

Turn off the wrap flag when the cursor moves.

SM/RM allow more than one parameter.
2017-12-17 11:13:12 +10:00
Jason Hood
66c527a624 Tweak colors
SGR parameters 90-97 are bright foreground (leaving bold unchanged) and
100-107 are bright background (leaving underline/blink unchanged);
`38;5;#` & `48;5;#` will work for the first 16 colors, setting both
foreground & bold or background & underline (0-7 bold/underline off,
8-15 bold/underline on).
2017-12-16 12:37:43 +10:00
Jason Hood
7c14985a5d Modify DECSC/DECRC
Save/restore the attributes and G0 character set (the ANSI version still
only does the cursor).

Add the comment I missed when adding SCS; remove `DEC` from the sequence
descriptions.
2017-12-11 11:34:32 +10:00
Jason Hood
d20ab7471f Add tab handling
Support setting (HTS & DECST8C) and clearing (TBC) tabs, overriding the
console's own tab processing.  I've extended TBC with `\e[8g` to restore
console processing and added an extra parameter to DECST8C to set a
particular tab size.
2017-12-10 22:52:38 +10:00
Jason Hood
0e87a03761 Add DA, DECCOLM, DECNSCM, DECSC & DECRC
DECSC (Save Cursor) & DECRC (Restore Cursor) are the same as their
ANSI.sys equivalents (`\e[s` & `\e[u`).

DA (Device Attributes) will respond with `\e[?62;1c` - VT220 with 132
columns.

Setting DECCOLM will size the buffer & window to 132 columns; resetting
will restore the original values (NOT set 80 columns).	Setting DECNSCM
will prevent the display from being erased; however, the first time
DECCOLM is used will scroll in a new window, if appropriate.
2017-12-09 11:11:52 +10:00
Jason Hood
eb997019c1 Add IND, NEL and RI
These controls always operate on the buffer, in keeping with LF.
2017-12-03 12:50:13 +10:00
Jason Hood
1a4d6b488f Add option -pu to unload from the parent 2017-11-30 21:05:41 +10:00
Jason Hood
523c478d67 Fix some things
Prevent `ESC LF` from performing the newline.
Only recognise `, ~`, not `,` anything.
Document BEL and the ignored sequences.
2017-11-30 11:38:15 +10:00
Jason Hood
e2f9b4e417 Add palette sequences
Recognise the xterm ESC]4 & ESC]104 OSC commands to set/reset colors.
Three color specs are recognised: `#RGB`, `#RRGGBB` and `R,G,B`; in
addition, multiple specs can be given (separated by commas) to
automatically increase the index.  I also allow `*` to query this index
and all subsequent ones.  Reset will restore the colors from when the
DLL was first loaded, not from the Console Properties.
2017-11-29 11:06:57 +10:00
Jason Hood
9fbe42a583 Escape control characters; fix CRM issues
Since ESC is now preserved for unrecognised sequences, I feel able to
use it to escape control characters in order to display them (e.g.
"\e\e" will display a single escape).

Control mode would not display the recognised controls (i.e. BEL, SO and
SI) and would not work if it was turned off in the same string.
2017-11-24 11:10:42 +10:00
Jason Hood
e44cbb848f Add + to use the buffer
Normally the display uses the window height; adding an intermediate
character of `+` will use the buffer height.  Report cursor position
will also output `+R` when used with `+n`.
2017-11-23 12:06:33 +10:00
Jason Hood
a4125753a4 Fix compiling with MinGW and TDM
MinGW lacks some definitions; TDM wants WINVER defined.

Assume a default `CC=cc` should really be `CC=gcc`.
2017-11-21 11:46:58 +10:00
Jason Hood
826b0b66ba Add DECPS Play Sound
There are some differences from the VT520: volume is ignored (although
silence is honored); duration is in 1/32 of a second up to and including
48 (1.5 seconds), after which it is in milliseconds (but still a maximum
of 8 seconds); notes are 1..25, anything else is frequency.
2017-11-19 12:07:28 +10:00
Jason Hood
aa8b0c6a36 Use the system default sound for the bell
Windows 7 uses Beep for the bell, which means if you accidentally view
a binary file, the console freezes until all the bells have finished.
Windows 10 uses PlaySound, which avoids the freeze, but prevents the
sound being played at all if the program immediately exits (in its own
host).	This uses PlaySound in its own thread, ignoring additional bells
whilst one is currently playing and waiting for it to finish before
terminating.
2017-11-17 21:53:58 +10:00
Jason Hood
8eb06aad27 Preserve ESC not part of a sequence
There are some instances when ESC should just be a normal character
(e.g. TCC uses it in aliases to clear the current line), so if no
sequence is recognised, pass the ESC through.
2017-11-07 11:35:42 +10:00
Jason Hood
43b3653c8a Fix problem with reporting cursor position
Turns out that sending duplicate characters would only send one (e.g.
"11" would just be "1").  Sending the key up events fixed it.
2017-11-03 23:04:51 +10:00
Jason Hood
2f3fe57b60 Hook CreateFile and CreateConsoleScreenBuffer
If console handles are created as GENERIC_WRITE, add GENERIC_READ in
order to be able to retrieve console information.  This fixes #93
(redirecting to CON).
2017-10-30 10:50:11 +10:00
Jason Hood
97dcb13634 Fix ANSICON's output when redirected to CON
ANSICON switches the standard streams to Unicode, but uses its own
routine to write directly to the console when appropriate.  However, the
newlines written by -e/-E/-t/-T were still done by the CRT, so when
output was redirected to CON, "\r\0\n\0" was being written.
2017-10-28 16:43:45 +10:00
Jason Hood
67f1d48801 Revert back to saving buffer cursor position
In v1.70 I had saved the cursor position relative to the window, but
that means should the window scroll, the cursor would appear to be
restored to the wrong position.  It will still be wrong should the
*buffer* be scrolled, but that can't really be helped (well, possibly
it could, but it would be more effort than it's worth).
2017-10-28 11:19:06 +10:00
Jason Hood
3fb6b5aa65 Fix unloading 2017-10-26 21:46:53 +10:00
Jason Hood
40f59c543c Many changes, bad programmer!
Just copying the history from the source:

recognize the standard handle defines in WriteFile;
minor speed improvement by caching GetConsoleMode;
keep track of three handles (ostensibly stdout, stderr and a file);
test a DOS header exists before writing to e_oemid;
more flexible/robust handling of data directories;
files writing to the console will always succeed;
log: use API file functions and a custom printf;
     add a blank line between processes;
     set function name for MyWriteConsoleA;
scan imports from "kernel32" (without extension);
added dynamic environment variable CLICOLOR;
removed _hwrite (it's the same address as _lwrite);
join multibyte characters split across separate writes;
remove wcstok, avoiding potential interference with the host;
similarly, use a private heap instead of malloc.
2017-07-25 18:18:34 +10:00
Jason Hood
1fe1d8f130 Recognise more sequences; ignore newline immediately after wrap.
Added support for CHT & CBT (move forward/backward by tabs), DECAWM (don't
wrap at EOL), CRM (display control characters, but still perform newline)
and REP (repeat last character, including BEL, BS, TAB, LF and CR).

It always bugged me that newline would add an unneeded blank line due to
wrap, but not enough to do anything about it.  For some reason, adding CRM
got me to thinking about it, so I finally did it.

Stopped \e[K from erasing the first character of the next line.
Restore cursor visibility on unload.
2014-02-26 21:18:45 +10:00
Jason Hood
7539473550 Use window height, not buffer.
Clear screen (\e[2J) will scroll in a new window the first time it's used,
or the window has scrolled, or the cursor is on the last line of the buffer.

Restore Cursor Position (\e[u) will recognise screen size changes and limit
itself to the new boundaries.
2014-02-20 18:06:28 +10:00
Jason Hood
9fa86512f3 Better parsing of escape & CSI sequences.
Recognise general Parameter & Intermediate Bytes, rather than testing
specific characters.  Explicitly ignore xterm 38 & 48 SGR values.
2014-02-18 21:34:39 +10:00
Jason Hood
bd696b55c8 Exclude entire programs; better hooking of dynamically-loaded libraries.
ANSICON_EXC can now be used to exclude an entire program (including children).
This is achieved by simply not specifying an extension: ANSICON_EXC=program.exe
will just ignore program.exe (its DLLs will still be hooked, as will its child-
ren), but ANSICON_EXC=program will not hook program at all (which also means
its children will not be hooked).

The various LoadLibrary hooks would only hook the DLL that was specified - any
DLLs it loaded would be missed.  That has now been rectified.  Similarly, a DLL
that is injected via CreateRemoteThread, using LoadLibraryA or LoadLibraryW as
its ThreadProc, will now be hooked.
2014-02-10 16:33:42 +10:00
Jason Hood
453db81af5 Fixed restoring original attributes on detach. 2014-02-08 12:04:43 +10:00
Jason Hood
db36552c42 Work with 64-bit AnyCPU; copy original IDT to IAT; log improvements. 2014-02-08 01:10:51 +10:00