107 Commits

Author SHA1 Message Date
Jason Hood
0dd40f6d74 Simplify getting buffer info 2019-04-29 20:11:00 +10:00
Jason Hood
d7a2d5e962 Fix ANSICON environment variable
Creating a console handle in `set_ansicon` was assumed to succeed, but
that is not the case when there is no console (if the process was
started detached or freed its console).  This left the console info
uninitialised, causing my `printf` replacement to get stuck in a loop.

Resolves #127.
2019-03-01 17:47:05 +10:00
Jason Hood
a1bf74dff9 Fix crash when some programs start
A few functions that are hooked are not imported, so testing if my
import was already hooked would fail.

SetCurrentConsoleFontEx should have been hooked, but wasn't.
2019-02-03 18:49:28 +10:00
Jason Hood
eccbbe7a54 Fixes
Unhook on terminate, to properly exit (Vista CMD.EXE and a MinGW-built
ansicon.exe would seem to exit okay, but the exit code was wrong).
Fixes #123.

Verify the DLL exists prior to injection.  With the DLL now being added
to the import table, failing to find it would prevent the process itself
from loading.
2018-11-04 11:17:23 +10:00
Jason Hood
0472db80b2 Preserve last error
Hooked functions that failed and logged debug messages were causing an
incorrect last error to be set.
2018-08-23 17:38:43 +10:00
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
4e84582f02 Fix running directly via ansicon
A program run directly by `ansicon` should always be hooked, regardless
if it's GUI or excluded.  This was broken in v1.84 due to the DLL being
imported.
2018-08-23 17:26:52 +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
f215223810 Update comments 2018-05-10 22:02:29 +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
f4697b59fa Test if the real WriteFile handle is console 2018-05-03 23:46:33 +10:00
Jason Hood
d7a2be9964 Exit process if the primary thread is detached
Processes on Windows 10 that return rather than call `ExitProcess` may
have a 30 second delay before terminating.  This seems due to the
console creating a thread.  Detect a detached primary thread and
explicitly call `ExitProcess`.
2018-05-02 18:12:57 +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
c232c1ff37 Release v1.82 2018-02-13 16:12:07 +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
392337b3b1 Wrap the cache in a critical section
Git's error messages were being truncated.  Turns out git used two
threads, one writing to the console and one to file.  This caused a
conflict in `IsConsoleHandle` when the cache was updated.  Making it a
critical section solves the issue.
2018-02-12 21:50:43 +10:00
Jason Hood
0366daad05 Flush and invalidate cache on closing handle 2018-02-12 21:49:43 +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
a2c3595477 Release 1.81 2017-12-30 11:46:02 +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
05e765c881 Fix buffer overflow
Turns out testing for an empty buffer outside the critical section
wasn't a good idea (testing in a virtual 10 started the thread between
the test and enter, causing buffer overflow in the small loop).
2017-12-26 18:43:56 +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
2120ff3efa Fix CR
Having CR go back to the beginning of a wrapped line is not a good idea
when it's followed by LF.
2017-12-24 23:44:26 +10:00
Jason Hood
75f707fc20 Hide cursor on move
Setting the cursor position on a non-active buffer still displays it on
the active buffer.  Hide the cursor, set the position, show the cursor
is a sufficient workaround.
2017-12-24 22:39:53 +10:00
Jason Hood
e8247f3aa7 Fix CRM
Small writes would not normally display controls.
2017-12-24 18:23:32 +10:00
Jason Hood
62f7d0240e Flush when idle
Flush 15ms after the last write.  This would typically be needed to show
a progress display.
2017-12-24 16:41:06 +10:00
Jason Hood
b258a2ea2d Fix standard controls in insert mode 2017-12-24 10:58:19 +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
4502a49bab Set margins per console; fix windowless processes
I had intended for margins to be set per process, but during testing I
was wondering why they weren't working, so per console it is.

In changing the above, I realised that `pState` may stay `NULL` when a
process is created without a window.  Initialise it to a default state,
updating it when the window becomes available.
2017-12-23 18:40:59 +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
d04feca91c Add mnemonics to sequence comments 2017-12-21 21:48:15 +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
645f57e59c Fix parameters
Explicit zero parameters should still default to one.  Limit parameters
to a maximum value of 32767.
2017-12-09 22:16:47 +10:00
Jason Hood
c11447fc8b Add partial SCS support
Allow G0 to be set to the DEC Special Graphics Character Set or ASCII.
2017-12-09 21:01:13 +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