From fcf3ec2877f042c6957d808e562807089e23ebe6 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Tue, 9 Apr 2024 08:58:58 +0100 Subject: ash: add title built-in Implement a 'title' built-in for ash. It's very simple-minded, performs almost no error checking and is completely non-portable. - With no arguments it prints the current console title. - If arguments are provided the *first only* is set as the console title. Costs 88-116 bytes. (GitHub issue #401) --- include/mingw.h | 1 + shell/ash.c | 21 +++++++++++++++++++++ win32/winansi.c | 8 ++++++-- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/include/mingw.h b/include/mingw.h index 4abdcf459..41d4cd940 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -158,6 +158,7 @@ BOOL conToCharA(LPSTR d); BOOL readConsoleInput_utf8(HANDLE h, INPUT_RECORD *r, DWORD len, DWORD *got); void set_title(const char *str); +int get_title(char *buf, int len); void move_cursor_row(int n); void reset_screen(void); int winansi_putchar(int c); diff --git a/shell/ash.c b/shell/ash.c index d312cb8cd..df27e9c8d 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -11253,6 +11253,9 @@ static int readcmd(int, char **) FAST_FUNC; static int setcmd(int, char **) FAST_FUNC; static int shiftcmd(int, char **) FAST_FUNC; static int timescmd(int, char **) FAST_FUNC; +#if ENABLE_PLATFORM_MINGW32 +static int titlecmd(int, char **) FAST_FUNC; +#endif static int trapcmd(int, char **) FAST_FUNC; static int umaskcmd(int, char **) FAST_FUNC; static int unsetcmd(int, char **) FAST_FUNC; @@ -11352,6 +11355,9 @@ static const struct builtincmd builtintab[] = { { BUILTIN_REGULAR "test" , testcmd }, #endif { BUILTIN_SPEC_REG "times" , timescmd }, +#if ENABLE_PLATFORM_MINGW32 + { BUILTIN_REGULAR "title" , titlecmd }, +#endif { BUILTIN_SPEC_REG "trap" , trapcmd }, { BUILTIN_REGULAR "true" , truecmd }, { BUILTIN_REGULAR "type" , typecmd }, @@ -15549,6 +15555,21 @@ timescmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) return 0; } +#if ENABLE_PLATFORM_MINGW32 +static int FAST_FUNC +titlecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) +{ + if (*argptr == NULL) { + char buffer[256]; + if (get_title(buffer, sizeof(buffer))) + puts(buffer); + } else { + set_title(*argptr); + } + return 0; +} +#endif + #if ENABLE_FEATURE_SH_MATH /* * The let builtin. Partially stolen from GNU Bash, the Bourne Again SHell. diff --git a/win32/winansi.c b/win32/winansi.c index 0dd2e17f2..3b7744b39 100644 --- a/win32/winansi.c +++ b/win32/winansi.c @@ -179,8 +179,12 @@ int terminal_mode(int reset) void set_title(const char *str) { - if (is_console(STDOUT_FILENO)) - SetConsoleTitle(str); + SetConsoleTitle(str); +} + +int get_title(char *buf, int len) +{ + return GetConsoleTitle(buf, len); } static HANDLE dup_handle(HANDLE h) -- cgit v1.2.3-55-g6feb