[3/4] cmd: Avoid checking whether we're in console mode for every read

Frédéric Delanoy frederic.delanoy at gmail.com
Mon Sep 26 17:42:42 CDT 2011


---
 programs/cmd/batch.c    |    9 ++++-----
 programs/cmd/builtins.c |    4 ++--
 programs/cmd/wcmd.h     |    5 +++--
 programs/cmd/wcmdmain.c |   21 +++++++++++++--------
 4 files changed, 22 insertions(+), 17 deletions(-)

diff --git a/programs/cmd/batch.c b/programs/cmd/batch.c
index 2285caf..aef0fa0 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,14 +181,13 @@ 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, const BOOL is_console_handle)
 {
-  DWORD bytes, charsRead, dummy;
-  BOOL status, is_console_handle;
+  DWORD bytes, charsRead;
+  BOOL status;
   WCHAR *p;
 
   p = s;
-  is_console_handle = !!GetConsoleMode(h, &dummy);
   if (is_console_handle) {
     status = ReadConsoleW(h, s, noChars, &charsRead, NULL);
     if (!status) return NULL;
diff --git a/programs/cmd/builtins.c b/programs/cmd/builtins.c
index e1f6447..440ee38 100644
--- a/programs/cmd/builtins.c
+++ b/programs/cmd/builtins.c
@@ -1137,7 +1137,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);
@@ -1397,7 +1397,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 d370764..5133d20 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 47096853..6e5ed16 100644
--- a/programs/cmd/wcmdmain.c
+++ b/programs/cmd/wcmdmain.c
@@ -1775,8 +1775,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];
@@ -1820,7 +1821,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;
 
@@ -2180,7 +2182,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);
@@ -2271,6 +2274,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'};
@@ -2486,7 +2491,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;
@@ -2582,7 +2587,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;
@@ -2595,13 +2600,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);
-- 
1.7.6.3




More information about the wine-patches mailing list