diff --git a/programs/cmd/batch.c b/programs/cmd/batch.c index 5007db38a35..09bb9c4cb00 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) == NULL) + if (!WCMD_ReadAndParseLine(NULL, &toExecute, h, FALSE)) break; WCMD_process_commands(toExecute, FALSE, NULL, NULL); WCMD_free_commands(toExecute); @@ -181,22 +181,20 @@ WCHAR *WCMD_parameter (WCHAR *s, int n, WCHAR **where, WCHAR **end) { * the LF (or CRLF) from the line. */ -WCHAR *WCMD_fgets (WCHAR *s, int noChars, HANDLE h) +WCHAR *WCMD_fgets(WCHAR *s, int noChars, HANDLE h, BOOL is_console_handle) { DWORD bytes, charsRead; BOOL status; WCHAR *p; p = s; - if ((status = ReadConsoleW(h, s, noChars, &charsRead, NULL))) { + if (is_console_handle) { + status = ReadConsoleW(h, s, noChars, &charsRead, NULL); + if (!status) return NULL; s[charsRead-2] = '\0'; /* Strip \r\n */ return p; } - /* Continue only if we have no console (i.e. a file) handle */ - if (GetLastError() != ERROR_INVALID_HANDLE) - return NULL; - /* TODO: More intelligent buffering for reading lines from files */ do { status = WCMD_ReadFile (h, s, 1, &bytes, NULL); diff --git a/programs/cmd/builtins.c b/programs/cmd/builtins.c index 9fe50d9116d..b7900fc7e29 100644 --- a/programs/cmd/builtins.c +++ b/programs/cmd/builtins.c @@ -1134,7 +1134,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)) { + while (WCMD_fgets(buffer, sizeof(buffer)/sizeof(WCHAR), input, FALSE)) { /* Skip blank lines*/ parm = WCMD_parameter (buffer, 0, &where, NULL); @@ -1394,7 +1394,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)) { + while (WCMD_fgets (string, sizeof(string)/sizeof(WCHAR), context -> h, FALSE)) { str = string; while (isspaceW (*str)) str++; if (*str == ':') { diff --git a/programs/cmd/wcmd.h b/programs/cmd/wcmd.h index d37076419ab..5133d20808f 100644 --- a/programs/cmd/wcmd.h +++ b/programs/cmd/wcmd.h @@ -97,7 +97,7 @@ void WCMD_verify (const WCHAR *command); void WCMD_version (void); int WCMD_volume (BOOL set_label, const WCHAR *command); -WCHAR *WCMD_fgets (WCHAR *s, int n, HANDLE stream); +WCHAR *WCMD_fgets (WCHAR *s, int n, HANDLE stream, const BOOL is_console_handle); 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); @@ -111,7 +111,8 @@ void WCMD_strsubstW(WCHAR *start, const WCHAR* next, const WCHAR* insert, int le BOOL WCMD_ReadFile(const HANDLE hIn, WCHAR *intoBuf, const DWORD maxChars, LPDWORD charsRead, const LPOVERLAPPED unused); -WCHAR *WCMD_ReadAndParseLine(const WCHAR *initialcmd, CMD_LIST **output, HANDLE readFrom); +WCHAR *WCMD_ReadAndParseLine(const WCHAR *initialcmd, CMD_LIST **output, + HANDLE readFrom, const BOOL is_console_handle); 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 8e222a35da2..a6aee38a8b8 100644 --- a/programs/cmd/wcmdmain.c +++ b/programs/cmd/wcmdmain.c @@ -1786,8 +1786,9 @@ 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) { - +WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, + HANDLE readFrom, const BOOL is_console_handle) +{ WCHAR *curPos; int inQuotes = 0; WCHAR curString[MAXSTRING]; @@ -1831,7 +1832,8 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE } else if (readFrom == INVALID_HANDLE_VALUE) { WINE_FIXME("No command nor handle supplied\n"); } else { - if (WCMD_fgets(extraSpace, MAXSTRING, readFrom) == NULL) return NULL; + if (!WCMD_fgets(extraSpace, MAXSTRING, readFrom, is_console_handle)) + return NULL; } curPos = extraSpace; @@ -2191,7 +2193,8 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE /* Read more, skipping any blank lines */ while (*extraSpace == 0x00) { if (!context) WCMD_output_asis( WCMD_LoadMessage(WCMD_MOREPROMPT)); - if (WCMD_fgets(extraSpace, MAXSTRING, readFrom) == NULL) break; + if (!WCMD_fgets(extraSpace, MAXSTRING, readFrom, is_console_handle)) + break; } curPos = extraSpace; if (context) handleExpansion(extraSpace, FALSE, NULL, NULL); @@ -2282,6 +2285,8 @@ 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 +2502,7 @@ int wmain (int argc, WCHAR *argvW[]) */ /* Parse the command string, without reading any more input */ - WCMD_ReadAndParseLine(cmd, &toExecute, INVALID_HANDLE_VALUE); + WCMD_ReadAndParseLine(cmd, &toExecute, INVALID_HANDLE_VALUE, FALSE); WCMD_process_commands(toExecute, FALSE, NULL, NULL); WCMD_free_commands(toExecute); toExecute = NULL; @@ -2593,7 +2598,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); + WCMD_ReadAndParseLine(cmd, &toExecute, INVALID_HANDLE_VALUE, FALSE); WCMD_process_commands(toExecute, FALSE, NULL, NULL); WCMD_free_commands(toExecute); toExecute = NULL; @@ -2606,13 +2611,13 @@ 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)) == NULL) + if (!WCMD_ReadAndParseLine(NULL, &toExecute, GetStdHandle(STD_INPUT_HANDLE), is_console)) break; WCMD_process_commands(toExecute, FALSE, NULL, NULL); WCMD_free_commands(toExecute);