From 5e3cb1867b8a78a29ba62a306ba9f2ed5d572be4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Delanoy?= Date: Thu, 6 Oct 2011 18:50:31 +0200 Subject: [PATCH] cmd: Infer handle type from handle value in WCMD_fgets and WCMD_ReadAndParseLine. --- programs/cmd/batch.c | 6 +++--- programs/cmd/builtins.c | 4 ++-- programs/cmd/wcmd.h | 9 ++++++--- programs/cmd/wcmdmain.c | 23 +++++++---------------- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/programs/cmd/batch.c b/programs/cmd/batch.c index 77a49c9a390..79bb0099d0f 100644 --- a/programs/cmd/batch.c +++ b/programs/cmd/batch.c @@ -89,7 +89,7 @@ void WCMD_batch (WCHAR *file, WCHAR *command, int called, WCHAR *startLabel, HAN while (context -> skip_rest == FALSE) { CMD_LIST *toExecute = NULL; /* Commands left to be executed */ - if (!WCMD_ReadAndParseLine(NULL, &toExecute, h, FALSE)) + if (!WCMD_ReadAndParseLine(NULL, &toExecute, h)) break; WCMD_process_commands(toExecute, FALSE, NULL, NULL); WCMD_free_commands(toExecute); @@ -186,7 +186,7 @@ WCHAR *WCMD_parameter (WCHAR *s, int n, WCHAR **where, WCHAR **end) { * NULL on error or EOF */ -WCHAR *WCMD_fgets(WCHAR *buf, int noChars, HANDLE h, const BOOL is_console_handle) +WCHAR *WCMD_fgets(WCHAR *buf, int noChars, HANDLE h) { DWORD bytes, charsRead; BOOL status; @@ -196,7 +196,7 @@ WCHAR *WCMD_fgets(WCHAR *buf, int noChars, HANDLE h, const BOOL is_console_handl between DOS and Unix. Also need to lose the LF (or CRLF) from the line. */ p = buf; - if (is_console_handle) { + if (WCMD_is_console_handle(h)) { status = ReadConsoleW(h, buf, noChars, &charsRead, NULL); if (!status) return NULL; if (buf[charsRead-2] == '\r') diff --git a/programs/cmd/builtins.c b/programs/cmd/builtins.c index d9eae312263..6b4d0ab3012 100644 --- a/programs/cmd/builtins.c +++ b/programs/cmd/builtins.c @@ -1133,7 +1133,7 @@ void WCMD_for (WCHAR *p, CMD_LIST **cmdList) { WCHAR buffer[MAXSTRING] = {'\0'}; WCHAR *where, *parm; - while (WCMD_fgets(buffer, sizeof(buffer)/sizeof(WCHAR), input, FALSE)) { + while (WCMD_fgets(buffer, sizeof(buffer)/sizeof(WCHAR), input)) { /* Skip blank lines*/ parm = WCMD_parameter (buffer, 0, &where, NULL); @@ -1393,7 +1393,7 @@ void WCMD_goto (CMD_LIST **cmdList) { if (*paramStart == ':') paramStart++; SetFilePointer (context -> h, 0, NULL, FILE_BEGIN); - while (WCMD_fgets (string, sizeof(string)/sizeof(WCHAR), context -> h, FALSE)) { + while (WCMD_fgets (string, sizeof(string)/sizeof(WCHAR), context -> h)) { str = string; while (isspaceW (*str)) str++; if (*str == ':') { diff --git a/programs/cmd/wcmd.h b/programs/cmd/wcmd.h index af75a5d0703..61cef8f00da 100644 --- a/programs/cmd/wcmd.h +++ b/programs/cmd/wcmd.h @@ -97,7 +97,11 @@ void WCMD_verify (const WCHAR *command); void WCMD_version (void); int WCMD_volume (BOOL set_label, const WCHAR *command); -WCHAR *WCMD_fgets (WCHAR *buf, int n, HANDLE stream, const BOOL is_console_handle); +static inline BOOL WCMD_is_console_handle(HANDLE h) +{ + return (((DWORD_PTR)h) & 3) == 3; +} +WCHAR *WCMD_fgets (WCHAR *buf, int n, HANDLE stream); WCHAR *WCMD_parameter (WCHAR *s, int n, WCHAR **where, WCHAR **end); WCHAR *WCMD_skip_leading_spaces (WCHAR *string); BOOL WCMD_keyword_ws_found(const WCHAR *keyword, int len, const WCHAR *ptr); @@ -110,8 +114,7 @@ WCHAR *WCMD_strdupW(const WCHAR *input); void WCMD_strsubstW(WCHAR *start, const WCHAR* next, const WCHAR* insert, int len); BOOL WCMD_ReadFile(const HANDLE hIn, WCHAR *intoBuf, const DWORD maxChars, LPDWORD charsRead); -WCHAR *WCMD_ReadAndParseLine(const WCHAR *initialcmd, CMD_LIST **output, - HANDLE readFrom, const BOOL is_console_handle); +WCHAR *WCMD_ReadAndParseLine(const WCHAR *initialcmd, CMD_LIST **output, HANDLE readFrom); CMD_LIST *WCMD_process_commands(CMD_LIST *thisCmd, BOOL oneBracket, const WCHAR *var, const WCHAR *val); void WCMD_free_commands(CMD_LIST *cmds); diff --git a/programs/cmd/wcmdmain.c b/programs/cmd/wcmdmain.c index 5611642deb4..a4726e864c4 100644 --- a/programs/cmd/wcmdmain.c +++ b/programs/cmd/wcmdmain.c @@ -233,11 +233,6 @@ void WCMD_leave_paged_mode(void) pagedMessage = NULL; } -static inline BOOL is_console_handle(HANDLE h) -{ - return (((DWORD_PTR)h) & 3) == 3; -} - /*************************************************************************** * WCMD_Readfile * @@ -248,7 +243,7 @@ BOOL WCMD_ReadFile(const HANDLE hIn, WCHAR *intoBuf, const DWORD maxChars, LPDWO DWORD numRead; char *buffer; - if (is_console_handle(hIn)) + if (WCMD_is_console_handle(hIn)) /* Try to read from console as Unicode */ return ReadConsoleW(hIn, intoBuf, maxChars, charsRead, NULL); @@ -1781,8 +1776,7 @@ static BOOL WCMD_IsEndQuote(const WCHAR *quote, int quoteIndex) * - Anything else gets put into the command string (including * redirects) */ -WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, - HANDLE readFrom, const BOOL is_console_handle) +WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE readFrom) { WCHAR *curPos; int inQuotes = 0; @@ -1827,7 +1821,7 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, } else if (readFrom == INVALID_HANDLE_VALUE) { WINE_FIXME("No command nor handle supplied\n"); } else { - if (!WCMD_fgets(extraSpace, MAXSTRING, readFrom, is_console_handle)) + if (!WCMD_fgets(extraSpace, MAXSTRING, readFrom)) return NULL; } curPos = extraSpace; @@ -2188,7 +2182,7 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, /* Read more, skipping any blank lines */ while (*extraSpace == 0x00) { if (!context) WCMD_output_asis( WCMD_LoadMessage(WCMD_MOREPROMPT)); - if (!WCMD_fgets(extraSpace, MAXSTRING, readFrom, is_console_handle)) + if (!WCMD_fgets(extraSpace, MAXSTRING, readFrom)) break; } curPos = extraSpace; @@ -2280,8 +2274,6 @@ int wmain (int argc, WCHAR *argvW[]) WCHAR string[1024]; WCHAR envvar[4]; int opt_q; - BOOL is_console; - DWORD dummy; int opt_t = 0; static const WCHAR promptW[] = {'P','R','O','M','P','T','\0'}; static const WCHAR defaultpromptW[] = {'$','P','$','G','\0'}; @@ -2497,7 +2489,7 @@ int wmain (int argc, WCHAR *argvW[]) */ /* Parse the command string, without reading any more input */ - WCMD_ReadAndParseLine(cmd, &toExecute, INVALID_HANDLE_VALUE, FALSE); + WCMD_ReadAndParseLine(cmd, &toExecute, INVALID_HANDLE_VALUE); WCMD_process_commands(toExecute, FALSE, NULL, NULL); WCMD_free_commands(toExecute); toExecute = NULL; @@ -2593,7 +2585,7 @@ int wmain (int argc, WCHAR *argvW[]) if (opt_k) { /* Parse the command string, without reading any more input */ - WCMD_ReadAndParseLine(cmd, &toExecute, INVALID_HANDLE_VALUE, FALSE); + WCMD_ReadAndParseLine(cmd, &toExecute, INVALID_HANDLE_VALUE); WCMD_process_commands(toExecute, FALSE, NULL, NULL); WCMD_free_commands(toExecute); toExecute = NULL; @@ -2606,13 +2598,12 @@ int wmain (int argc, WCHAR *argvW[]) SetEnvironmentVariableW(promptW, defaultpromptW); WCMD_version (); - is_console = !!GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dummy); while (TRUE) { /* Read until EOF (which for std input is never, but if redirect in place, may occur */ if (echo_mode) WCMD_show_prompt(); - if (!WCMD_ReadAndParseLine(NULL, &toExecute, GetStdHandle(STD_INPUT_HANDLE), is_console)) + if (!WCMD_ReadAndParseLine(NULL, &toExecute, GetStdHandle(STD_INPUT_HANDLE))) break; WCMD_process_commands(toExecute, FALSE, NULL, NULL); WCMD_free_commands(toExecute);