aboutsummaryrefslogtreecommitdiff
path: root/win32/winansi.c (follow)
Commit message (Collapse)AuthorAgeFilesLines
* win32: delay adjusting code pageRon Yorston2023-03-171-1/+12
| | | | | | | | | | | Commit 93a63809f9 (win32: add support for the euro currency symbol) caused all invocations of busybox-w32 to change code page 850 to 858. This has been known to cause problems with fonts in PowerShell (GitHub issue #207). Delay changing the code page until an i/o operation is imminent. Instances of PowerShell started by the `drop` applet during ssh login thus no longer have their code page adjusted.
* win32: more changes to console/terminal modesRon Yorston2023-03-071-19/+19
| | | | | | | | | | | | | | | | | | Allowing virtual terminal input mode to be set if available proved to be unhelpful: cmd.exe doesn't understand such newfangled stuff (though PowerShell does). The allowed values of BB_TERMINAL_MODE are changed to: 0 Force console mode. 1 Force virtual terminal mode for output. 2 Force virtual terminal mode for input. 3 Force virtual terminal mode for input and output. 4 Support virtual terminal input if enabled. Don't alter mode. 5 Support virtual terminal input if enabled. Set virtual terminal mode for output, if possible. The default is 5.
* win32: changes to console mode handlingRon Yorston2023-03-051-20/+56
| | | | | | | | | | | | | | | | | | | Add the environment variable BB_TERMINAL_MODE as a more general way of controlling console/terminal mode setting. The default remains unchanged: use virtual terminal mode for output if possible but fall back to the console API with emulated ANSI escape sequences. Currently valid settings are: 0 Force use of console mode 1 Force use of virtual terminal mode for output 5 Prefer virtual terminal mode for output, fall back to console Other values won't do anything useful until code elsewhere has been updated. BB_SKIP_ANSI_EMULATION remains available for backwards compatibility. If both variables are set BB_TERMINAL_MODE takes precedence.
* win32: try to recover from strange console modeRon Yorston2023-02-211-4/+14
| | | | | | | | | | | | | | | | | | | | | | Execute a remote command on a Windows server using ssh: ~$ ssh localhost 'echo hello' rmy@localhost's password: hello If such a command is run from a busybox-w32 shell the console enters a strange mode when the command completes. Attempt to recover by setting the console back to a default mode. - This doesn't work in a legacy console. - The new mode may not be _exactly_ correct, but it's better than how ssh leaves it. Costs 80-96 bytes. (GitHub issue #288)
* win32: shuffle skip_ansi_emulation() codeRon Yorston2023-02-191-6/+8
| | | | | Rearrange code to avoid unnecessary tests when the environment variable BB_SKIP_ANSI_EMULATION is set.
* win32: work around problem with ConEmuRon Yorston2023-02-191-1/+3
| | | | | | | | | | | | | | | | | | | | | | | | When running the busybox-w32 shell in ConEmu, if: - ANSI emulation is disabled (which it will be, by default) - a non-builtin command is run there is a window of a few seconds after the command completes during which ConEmu is in XTerm mode. During this time any attempt to use the arrows keys will result in [A[B[C[D appearing. This appears to be a common problem: https://github.com/Maximus5/ConEmu/issues/2316 Try to detect if we're running under ConEmu and alter the default behaviour to prefer ANSI emulation. The user can override this preference by setting BB_SKIP_ANSI_EMULATION if desired. By a quirk of fate, old MSYS2 programs (from 2021) which required a workaround in busybox-w32 (commit 54d2ea4b4) are immune to the problem with ConEmu. (GitHib issue #287)
* win32: only change console mode if necessaryRon Yorston2023-02-191-7/+10
| | | | | In skip_ansi_emulation() only call SetConsoleMode() if the new mode differs from the current mode.
* win32: speed up getc(3) wrapperRon Yorston2022-12-301-2/+2
| | | | | | | | The grep applet was found to be rather slow. A major reason for this is the implementation of getc(3). Using _getc_nolock() and inlining is_console_in() speeds things up by a factor of three. (GitHub issue #278)
* ash: export certain variables to the environment immediatelyRon Yorston2022-05-081-1/+1
| | | | | | | | | | | The environment variables BB_OVERRIDE_APPLETS, BB_SKIP_ANSI_EMULATION and BB_SYSTEMROOT affect of the behaviour of the shell itself. Setting them as shell variables is insufficient for them to affect the current shell. When these three variables are exported from the shell they are now placed in the environment immediately. Conversely, when they're unset or unexported they're removed from the environment.
* winansi: detect if running under WineRon Yorston2021-12-221-2/+11
| | | | | | | | | | | | | | Detect if running under Wine by checking for the wine_get_version function in ntdll.dll. This is how the Wine Developer FAQ suggests doing it. If running under Wine and not otherwise configured: - use ANSI emulation; - don't use alternate screen buffer in vi/less. Explicit settings of BB_SKIP_ANSI_EMULATION and BB_ALT_BUFFER will override the Wine defaults.
* winansi: add missing va_end()Ron Yorston2021-07-111-0/+1
|
* winansi: fix ansi emulationRon Yorston2021-03-051-12/+13
| | | | | | | | | | | | | | | | | | | | | | | | | | The following commands (reported in GitHub issue #201): printf "\033[38;2;255;0;0mX\033[m\n" printf "\033[38;2;255;0;0m;\033[m\n" produce different results. The first correctly displays a red 'X' while the second incorrectly displays a white ';'. The problem is that process_24bit() overruns the extent of the escape sequence. As a result the loop in process_escape() which handles 'ESC[...m' sequences sees the ';' in the text as a continuation of the escape sequence. Fix this by: - reworking process_24bit() so that the overrun is avoided; - changing the test in the loop in process_escape() so that even if an overrun happens it stops processing at the end of the escape sequence. Also, save a few bytes by replacing '++str' with 'str + 1' in a few places.
* winansi: code shrink and improvementsRon Yorston2021-02-171-41/+15
| | | | | | | | | | | | | | | | | | | | Commit b0b7ab792 (winansi: code shrink) noted that combining reverse vidoe escape sequences (e.g. ESC[7;27m) didn't work properly. Make further changes to improve the situation: - revert to keeping the current attributes in a static variable; - when reverse video is enabled switch the intensity as well as the colour components; - move the code from set_console_attr() into its only caller, process_escape(); - use 0xffff instead of 0 as a flag to indicate the attributes haven't been initialised. Saves 44 bytes and seems to work better.
* winansi: don't check return from xmallocRon Yorston2021-02-161-3/+0
|
* winansi: allow alternative screen buffer to be disabledRon Yorston2021-02-151-0/+7
| | | | | | | | The alternative console screen buffer (used by less and vi) doesn't work in Wine. Setting the environment variable BB_ALT_BUFFER to 0 causes a screen reset instead.
* winansi: allow test suite to run on WineRon Yorston2021-02-151-3/+3
| | | | | | | | | | | | Running the test suite on Wine failed because in seq 4 0 8 | head -n 10 'seq' didn't detect the broken pipe when 'head' terminated and carried on forever. Fix this by adding a call to ferror(3) in winansi_vfprintf(). Also, use xstrdup() and xmalloc() in a couple of places.
* winansi: code shrinkRon Yorston2021-02-081-13/+13
| | | | | | Mark floating-point constants as being of type 'float'. Saves 72 bytes.
* winansi: more accurate colour mappingRon Yorston2021-02-071-20/+99
| | | | | | Use a more accurate technique to map RGB colours to standard Windows console colours. Since this costs 648 bytes it's configurable but is enabled by default.
* winansi: code shrinkRon Yorston2021-02-071-90/+71
| | | | | | | | | | | | | | | | | | | Refactor handling of ESC[38...m and ESC[48...m: - At lower levels deal only with the standard console foreground colours. This makes handling of foreground and background colours more similar. - Many '|=' assignments (to combine attribute values) have been replaced by simple assinments. - Use a common routine to convert RGB to console colours; colours in the 8-bit 6x6x6 cube are scaled up to make use of this. - Detect invalid escape sequences to avoid setting incorrect colour values. Saves 296 bytes.
* winansi: tweak colour mappingRon Yorston2021-02-041-19/+9
|
* winansi: extend ANSI emulationRon Yorston2021-02-031-49/+177
| | | | | | | | | | | | | | | | Extend ANSI emulation to include: ESC[38;2;R;G;Bm 24-bit foreground colour ESC[48;2;R;G;Bm 24-bit background colour ESC[38;5;Nm 8-bit foreground colour ESC[48;5;Nm 8-bit background colour The colours are selected from the 16 standard console colours. This is unlikely to be aesthetically pleasing but at least it's better than raw escape sequences. Also, add ESC[9m (strike through) as a known but unsupported sequence.
* winansi: change default 'skip ANSI emulation' settingRon Yorston2021-02-031-13/+3
| | | | | | | | | Change the default 'skip ANSI emulation' setting which is used when the environment variable BB_SKIP_ANSI_EMULATION is unset. Previously it was 0 (always emulate) now it's 2 (detect whether or not the terminal supports ANSI escape sequences and behave accordingly). The default value is also now a build-time option.
* winansi: intercept calls to fputc(3)Ron Yorston2020-12-071-1/+19
| | | | | | | | | | | | | Perform code page translation for fputc(3). It's only used in a few places but is needed to fix things like: $ echo € | dos2unix Ç $ paste -d € file1 file2 1Ç2 Unfortunately it breaks the inventive use of dos2unix in GitHub issue #203.
* winansi: suppress console flags redefinition warningBiswa962020-06-071-0/+5
| | | | Modern versions of MinGW-w64 define more console modes.
* winansi: more fine-grained control of ANSI emulationRon Yorston2020-04-131-2/+34
| | | | | | | | | | | | | | | | Change the meaning of the BB_SKIP_ANSI_EMULATION variable: - if it isn't set or if it has any value other than 1 or 2, use ANSI emulation; - if it's set to 1, always emit ANSI escape codes; - if it's set to 2, attempt to enable virtual terminal processing in the current console. If that works emit ANSI escape codes, if not revert to ANSI emulation. On all platforms I've tested (Windows 10, Windows 8.1, ReactOS 0.4.13, plus ConEmu) BB_SKIP_ANSI_EMULATION=2 does the right thing.
* ash: reset ANSI emulation when BB_SKIP_ANSI_EMULATION changesRon Yorston2020-04-131-10/+7
| | | | | Update the skip status on the first call to skip_ansi_emulation() and whenever the BB_SKIP_ANSI_EMULATION variable changes.
* winansi: fix alternate screen buffer in Windows 7Ron Yorston2020-02-061-6/+10
| | | | | | | | | | | | | Closing 'vi' in Windows 7 resulted in the console window failing to echo any output correctly until the 'reset' builtin was run. This didn't happen in Windows XP, 8 or 10. The problem is fixed by only duplicating the console handle when switching *to* the alternate screen buffer. Add sanity checks so that switching to the alternate buffer requires the cached handled to be invalid, while it must be valid when switching from the alternate buffer.
* winansi: code shrinkRon Yorston2020-01-081-37/+31
| | | | | | | | | | | | | | Rework the ANSI emulation code to reduce its size. - Fetch console attributes when required rather than caching them. The init() function is no longer required; the only remaining initialisation is now performed in is_console(). - Turning off inverse video (ESC[27m) didn't work properly. This has been improved though it still doesn't work in some unlikely cases (ESC[7;27m). These changes save 180 bytes.
* winansi: restore correct attributes after lsRon Yorston2020-01-071-1/+7
| | | | | | | | | As reported in GitHub issue #178 the last colour set by the ls command is retained in the console. This is due to the previous commit reinitialising the stored attributes more frequently than it should. Attriubutes should only be initialised once.
* winansi: fix escape processing in Windows 10 release 1809Ron Yorston2020-01-061-20/+20
| | | | | | | | | | | | | | As reported in GitHub issue #177, ANSI escape sequences don't work reliably after shell redirection. The problem appears to be due to a change in the behaviour of dup2() in Windows 10 release 1809. The escape handling code kept a copy of the console handle for internal use. Prior to Windows 10 release 1809 this worked; since then unwinding shell redirections results in the actual console handle changing from time to time for reasons that are unknowable without access to the source code. Fix this by always fetching the console handle instead of caching it.
* vi: use alternate screen bufferRon Yorston2019-04-071-6/+68
| | | | | | | Implement the "ESC[?1049h" and "ESC[?1049l" ANSI escape sequences to switch between the alternate and normal screen buffers. This allows vi to restore the original screen contents on exit.
* winansi: code shrinkRon Yorston2019-04-071-20/+13
| | | | Add a common function to clear a section of the screen buffer.
* winansi: code shrinkRon Yorston2019-04-061-46/+17
| | | | | | | | | Use INVALID_HANDLE_VALUE (not NULL) to indicate that the console and console_in handles haven't been initialised. Replace many explicit comparisons against INVALID_HANDLE_VALUE with tests on the return value of GetConsoleScreenBufferInfo() which fails for an invalid handle.
* winansi: more robust handling of invalid escape sequencesRon Yorston2019-03-191-34/+46
| | | | | | | | Commit 399b1dd64 (winansi: support escape sequence to set window title) failed to handle some invalid escape sequences correctly. Make the code more robust. Fixes GitHub issue #153.
* winansi: support escape sequence to set window titleRon Yorston2019-03-091-12/+32
| | | | | | | | To set the window title from the command line: echo -en '\e]0;Hello World\007' The same sequence can be used in the shell's prompt (PS1).
* su: change title of console windowRon Yorston2019-03-091-0/+6
|
* win32: add support for the euro currency symbolRon Yorston2019-02-021-0/+68
| | | | | | | | | | | | | | | | | | | | | | The euro currency symbol was added to some OEM code pages. See: https://www.aivosto.com/articles/charsets-codepages-dos.html Add a configuration option (enabled by default) to support this. When enabled: - The read_key() function requests wide character key events. This allows the euro symbol to be entered regardless of the console OEM code page, though it needs to be available in the ANSI code page. - Conversions between OEM and ANSI code pages in winansi.c are modified to work around a bug in the Microsoft routines. - If the OEM code page is 850 when BusyBox starts it's changed to 858. This is the only currently supported OEM code page. Also, the shell read builtin is modified to use read_key() whenever input is being taken from the console.
* win32: implement vsnprintf(2)Ron Yorston2019-01-081-0/+21
| | | | | The Microsoft C runtime may include a defective version of vsnprintf. Implement a standards-compliant replacement.
* win32: emulate SIGPIPERon Yorston2018-12-111-3/+23
| | | | | | | | | | | | | | | | | | | The code to check whether a write error is due to a broken pipe can now either: - return with error EPIPE; - cause the process to exit with code 128+SIGPIPE. The default is the latter but the behaviour can be changed by issuing signal(SIGPIPE, SIG_IGN) and signal(SIGPIPE, SIG_DFL) calls. No actual signal is involved so kill can't send SIGPIPE and handlers other than SIG_IGN and SIG_DFL aren't supported. This does, however, avoid unsightly 'broken pipe' errors from commands like the example in GitHub issue #99: dd if=/dev/urandom | tr -dc _A-Z-a-z-0-9 | head -c${1:-32};echo;
* win32: improvements to get_terminal_width_heightRon Yorston2018-04-031-17/+0
| | | | | | | | - move winansi_get_terminal_width_height from winansi.c to ioctl.c, the only caller; - check both stdout and stderr for a connection to a console; - omit unnecessary code in get_terminal_width_height (because the WIN32 implementation ignores the file descriptor).
* win32: simplify isatty replacementRon Yorston2018-04-031-5/+1
| | | | | Don't treat input and output file descriptors differently: if we aren't connected to a console GetConsoleMode will fail for either.
* winansi: check for broken pipe in winansi_writeRon Yorston2018-03-091-7/+21
| | | | | | | write(2) is commonly used in applets like cat and tr so we should check for broken pipes there too. See issue #99.
* winansi: check for broken pipeRon Yorston2018-03-061-6/+29
| | | | | | | | | | | | | | Microsoft Windows doesn't support SIGPIPE and its support for the EPIPE error is somewhat inadequate. Most of the time a broken pipe leads to an EINVAL error which is misleading: $ yes hello | head -1 hello yes: Invalid argument Add a function to test for the underlying Windows error that indicates a broken pipe and set errno to EPIPE. Call this function in a few strategic places.
* winansi: changes to puts, fputs and fwriteRon Yorston2018-03-061-18/+3
| | | | | | | Rewrite winansi_puts using winansi_fputs and putchar. There's no need to use winansi_putchar. Improve error return values from winansi_fputs and winansi_fwrite.
* winansi: improve error return values in ANSI emulationRon Yorston2018-03-061-20/+23
| | | | Also tighten up the code slightly.
* winansi: create and use functions to test for consoleRon Yorston2018-03-061-51/+20
|
* win32: handle /dev/zero and /dev/urandom in open and read functionsRon Yorston2018-02-221-1/+1
| | | | | | | | | | | | | | | | | | | | | | | Currently /dev/zero is handled as a special case in dd. Add hacks to the open and read functions in mingw.c to handle the zero and urandom devices. - Opening /dev/zero or /dev/urandom actually opens the special Windows file 'nul' which behaves like /dev/null. This allows manipulation of the file descriptor with things like seek and close - When /dev/zero or /dev/urandom is opened the resulting file descriptor is stored and used to override the behaviour of read. - No attempt is made to track duplicated file descriptors, so using these devices for redirections in the shell isn't going to work and won't be permitted. (Could be, but won't.) - Limited control of the special file descriptors is provided by allowing the internal variables to be changed. - The numbers from /dev/urandom aren't very random.
* mingw: accommodate for BusyBox' assumptions about isatty()Johannes Schindelin2017-08-241-0/+27
| | | | | | | | | | | | | | | On Windows, isatty(fd) determines whether the file descriptor refers to a character device. The thing is: even NUL or a printer is a character device. BusyBox thinks, however, that isatty() only returns non-zero for an interactive terminal. So let's shadow isatty() by a version that answers the question BusyBox wants to have answered. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Ron Yorston <rmy@pobox.com>
* Provide vfprintf replacementRon Yorston2017-02-071-0/+1
| | | | | vfprintf is used in ash_vmsg. It should be passed through the codepage conversion in winansi.c.
* win32: allow ANSI emulation to be disabledRon Yorston2016-11-101-2/+15
| | | | | If the environment variable BB_SKIP_ANSI_EMULATION is set (the value doesn't matter) escape sequences are passed through to the terminal.