aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2023-07-12 11:52:06 +0100
committerRon Yorston <rmy@pobox.com>2023-07-12 11:52:06 +0100
commit2a4b086d4848616306f97f6378e0f10a48d41929 (patch)
tree361555080da82c8aa1b35aed25e645354c282f1a
parent63f2f555277c8a4b2b992367aa26d497931deaeb (diff)
downloadbusybox-w32-2a4b086d4848616306f97f6378e0f10a48d41929.tar.gz
busybox-w32-2a4b086d4848616306f97f6378e0f10a48d41929.tar.bz2
busybox-w32-2a4b086d4848616306f97f6378e0f10a48d41929.zip
ash: properly echo console input to 'read' built-in
The 'read' shell built-in echoed console input to stdout. Echo directly to the console instead. Costs 124-136 bytes.
-rw-r--r--include/mingw.h1
-rw-r--r--shell/ash.c4
-rw-r--r--shell/shell_common.c4
-rw-r--r--win32/winansi.c11
4 files changed, 16 insertions, 4 deletions
diff --git a/include/mingw.h b/include/mingw.h
index f11316205..ae34666e8 100644
--- a/include/mingw.h
+++ b/include/mingw.h
@@ -174,6 +174,7 @@ size_t winansi_fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
174int winansi_getc(FILE *stream); 174int winansi_getc(FILE *stream);
175int winansi_getchar(void); 175int winansi_getchar(void);
176char *winansi_fgets(char *s, int size, FILE *stream); 176char *winansi_fgets(char *s, int size, FILE *stream);
177void console_write(const char *str, int len);
177 178
178#define putchar winansi_putchar 179#define putchar winansi_putchar
179#define puts winansi_puts 180#define puts winansi_puts
diff --git a/shell/ash.c b/shell/ash.c
index 17af816d4..f3eb98d85 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -15775,8 +15775,8 @@ exitshell(void)
15775 out: 15775 out:
15776#if ENABLE_SUW32 15776#if ENABLE_SUW32
15777 if (delayexit) { 15777 if (delayexit) {
15778 freopen("CONOUT$", "w", stdout); 15778#define EXIT_MSG "Press any key to exit..."
15779 fputs_stdout("Press any key to exit..."); 15779 console_write(EXIT_MSG, sizeof(EXIT_MSG) - 1);
15780 _getch(); 15780 _getch();
15781 } 15781 }
15782#endif 15782#endif
diff --git a/shell/shell_common.c b/shell/shell_common.c
index 716d3aebb..99e56a050 100644
--- a/shell/shell_common.c
+++ b/shell/shell_common.c
@@ -235,7 +235,7 @@ shell_builtin_read(struct builtin_read_params *params)
235 --bufpos; 235 --bufpos;
236 ++nchars; 236 ++nchars;
237 if (!(read_flags & BUILTIN_READ_SILENT)) { 237 if (!(read_flags & BUILTIN_READ_SILENT)) {
238 printf("\b \b"); 238 console_write("\b \b", 3);
239 } 239 }
240 } 240 }
241 goto loop; 241 goto loop;
@@ -243,7 +243,7 @@ shell_builtin_read(struct builtin_read_params *params)
243 buffer[bufpos] = key == '\r' ? '\n' : key; 243 buffer[bufpos] = key == '\r' ? '\n' : key;
244 if (!(read_flags & BUILTIN_READ_SILENT)) { 244 if (!(read_flags & BUILTIN_READ_SILENT)) {
245 /* echo input if not in silent mode */ 245 /* echo input if not in silent mode */
246 putchar(buffer[bufpos]); 246 console_write(buffer + bufpos, 1);
247 } 247 }
248 } 248 }
249 else { 249 else {
diff --git a/win32/winansi.c b/win32/winansi.c
index 818ab1793..bc3e69163 100644
--- a/win32/winansi.c
+++ b/win32/winansi.c
@@ -1405,3 +1405,14 @@ BOOL readConsoleInput_utf8(HANDLE h, INPUT_RECORD *r, DWORD len, DWORD *got)
1405 return FALSE; 1405 return FALSE;
1406} 1406}
1407#endif 1407#endif
1408
1409void console_write(const char *str, int len)
1410{
1411 char *buf = xmemdup(str, len);
1412 int fd = _open("CONOUT$", _O_WRONLY);
1413 HANDLE fh = (HANDLE)_get_osfhandle(fd);
1414 charToConBuffA(buf, len);
1415 WriteConsole(fh, buf, len, NULL, NULL);
1416 close(fd);
1417 free(buf);
1418}