Minor shell.c MSVC backcompat issues
(1) By Florian Balmer (florian.balmer) on 2023-12-03 10:26:53 [link] [source]
With the new (great!) Windows UTF-16 console I/O changes, some minor backcompat issues have been introduced for MSVC builds. (Linking to shell.c from the Fossil project, as I can't find all the sources to generate this file, right now.)
- Calls to
SetConsoleMode()
with theENABLE_VIRTUAL_TERMINAL_PROCESSING
flag included will fail on older versions of Windows (before Windows 10). This will mostly go unnoticed, as well-behaved shells will always keep the console mode in a reasonable state useful to most programs. Also, I think theENABLE_VIRTUAL_TERMINAL_PROCESSING
flag should only be used for programs to explicitly print VT sequences, otherwise output may look surprising.
C:\...>ver
Microsoft Windows XP [Version 5.1.2600]
C:\...>type sample.c
#include <windows.h>
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif
main(){
if( !SetConsoleMode(
GetStdHandle(STD_OUTPUT_HANDLE),
ENABLE_VIRTUAL_TERMINAL_PROCESSING) ){
DWORD dwNumberOfCharsWritten;
WriteConsoleA(
GetStdHandle(STD_OUTPUT_HANDLE),
"Failed to set ENABLE_VIRTUAL_TERMINAL_PROCESSING.\n",
50,
&dwNumberOfCharsWritten,
NULL);
}
}
C:\...>sample.exe
Failed to set ENABLE_VIRTUAL_TERMINAL_PROCESSING.
- The
<stdint.h>
header is not available for older MSVC compilers, breaking the build. But the header is also not required for the latest MSVC compilers, so I'd like to ask for this include directive to be dropped.
(2) By Florian Balmer (florian.balmer) on 2023-12-03 10:39:57 in reply to 1 [link] [source]
Hm, the addition of <stdint.h>
is probably is unrelated to the UTF-16
console I/O changes--I was maybe just folled by the temporal coincidence when
updating my Fossil builds.
(3) By Larry Brasfield (larrybr) on 2023-12-03 16:38:40 in reply to 1 [link] [source]
Calls to SetConsoleMode() with the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag included will fail on older versions of Windows (before Windows 10).
The new console I/O package doe not check the return value from SetConsoleMode() for this reason.
This will mostly go unnoticed, as well-behaved shells will always keep the console mode in a reasonable state useful to most programs.
Part of my thinking about this is that the SQLite CLI, (also a shell, albeit one with limited purposes), will keep the console mode in a state useful to itself, including when it is linked with a line-editing package. This keeping should include both startup and resumption from .shell commands, regardless of how ill-behaved the invoked program is with respect to restoring the console mode upon its exit.
Also, I think the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag should only be used for programs to explicitly print VT sequences, otherwise output may look surprising.
My understanding of the flag differs from that. The flag's effect is to enable interpretation of VT sequences rather than printing them.
I have extremely limited sympathy for those who cause non-printable codes to be sent directly to their console and are then both surprised and willing to complain about it. If they want to discern what codes or bytes are in some datum, the hex() function is available to avoid such surprise and simplify human interpretation.
On backwards compatibility:
Simple patches to accommodate ancient compilers and operating systems will be carefully considered. The objectives for older platforms include basic functionality. But sacrificing utility on modern systems to avoid quirks on obsolete systems may not happen.
(4) By Florian Balmer (florian.balmer) on 2023-12-03 21:43:13 in reply to 3 [source]
The new console I/O package doe not check the return value from SetConsoleMode() for this reason.
Example:
SetConsoleMode(SOME_OTHER_FLAGS|ENABLE_VIRTUAL_TERMINAL_PROCESSING);
If this call is made on Windows 7, without ENABLE_VIRTUAL_TERMINAL_PROCESSING
support, it will fail, and SOME_OTHER_FLAGS
won't be applied, either!
If you're lucky, CMD.EXE or PowerShell.exe--or whatever was running before the
sqlite3.exe shell was started--has already set SOME_OTHER_FLAGS
for the
current console, and you're fine. But if not, you may end up with a console mode
without SOME_OTHER_FLAGS
enabled, possibly causing grief.
I have extremely limited sympathy for those who cause non-printable codes to be sent directly to their console and are then both surprised and willing to complain about it.
True, but...
In the SQLite shell, you may not have full control about what is stored in a database (for example, if you're analyzing a 3rd party database)--and hence you won't have full control about all the output sent to the console, and this may causes surprises.
The example in this screenshot is constructed, I agree, but illustrates my
point why I think setting ENABLE_VIRTUAL_TERMINAL_PROCESSING
is a bad idea.
(5) By Donal Fellows (dkfellows) on 2023-12-04 09:25:36 in reply to 2 [link] [source]
It's also much more likely to be needed by other compilers (if the types it defines are, of course).
(6) By Florian Balmer (florian.balmer) on 2023-12-04 14:22:36 in reply to 5 [link] [source]
Yes, I see. I noticed those changes at the same time in the Fossil project, and erroneously concluded they were related.
(7) By Florian Balmer (florian.balmer) on 2023-12-04 14:23:34 in reply to 4 [link] [source]
An even simpler example without any BLOB to TEXT conversion magic:
SELECT char(0x1b,0x5b,0x34,0x37,0x6d)||'Can you read this?';
My opinion is that there may be people who think it's nice to keep colored error message fragments in a SQL table, and the SQLite shell should not be tricked into displaying invisible text when dealing with such cases.
But I also see--as I just noticed--that the Windows builds should be bug-for-bug compatible with the *nix builds, the latter permitting arbitrary VT sequences to mess with SQLite shell output, as well! ;-)