Restore console title after running a command
[reactos.git] / reactos / subsys / system / cmd / cmd.c
index ad5954e..d9327a5 100644 (file)
@@ -296,22 +296,49 @@ static VOID
 Execute (LPTSTR Full, LPTSTR First, LPTSTR Rest)
 {
        TCHAR szFullName[MAX_PATH];
-       TCHAR first[CMDLINE_LENGTH];
-       TCHAR rest[CMDLINE_LENGTH];
-       TCHAR full[CMDLINE_LENGTH];
-#ifndef __REACTOS__
+       TCHAR *first = NULL;
+       TCHAR *rest = NULL; 
+       TCHAR *full = NULL; 
        TCHAR szWindowTitle[MAX_PATH];
-#endif
        DWORD dwExitCode = 0;
 
 #ifdef _DEBUG
        DebugPrintf (_T("Execute: \'%s\' \'%s\'\n"), first, rest);
 #endif
 
+       /* we need biger buffer that First, Rest, Full are already 
+          need rewrite some code to use realloc when it need instead 
+          of add 512bytes extra */
+
+       first = malloc ( _tcslen(First) + 512 * sizeof(TCHAR)); 
+       if (first == NULL)
+       {
+        error_out_of_memory();
+        return ;
+       }
+
+       rest =  malloc ( _tcslen(Rest) + 512 * sizeof(TCHAR)); 
+       if (rest == NULL)
+       {
+        free (full);
+        error_out_of_memory();
+        return ;
+       }
+
+       full =  malloc ( _tcslen(Full) + 512 * sizeof(TCHAR));
+       if (full == NULL)
+       {
+        free (full);
+        free (rest);
+        error_out_of_memory();
+        return ;
+       }
+
+
        /* Though it was already parsed once, we have a different set of rules
           for parsing before we pass to CreateProccess */
        if(!_tcschr(Full,_T('\"')))
-       {
+       {                
                _tcscpy(first,First);
                _tcscpy(rest,Rest);
                _tcscpy(full,Full);
@@ -341,8 +368,8 @@ Execute (LPTSTR Full, LPTSTR First, LPTSTR Rest)
                /* remove any slashes */
                while(i < _tcslen(first))
                {
-                       if(!_tcsncmp (&first[i], _T("\""), 1))
-                               memcpy(&first[i],&first[i + 1], _tcslen(&first[i]));
+                       if(first[i] == _T('\"'))
+                               memmove(&first[i],&first[i + 1], _tcslen(&first[i]) * sizeof(TCHAR));
                        else
                                i++;
                }
@@ -381,9 +408,7 @@ Execute (LPTSTR Full, LPTSTR First, LPTSTR Rest)
                return;
        }
 
-#ifndef __REACTOS__
        GetConsoleTitle (szWindowTitle, MAX_PATH);
-#endif
 
        /* check if this is a .BAT or .CMD file */
        if (!_tcsicmp (_tcsrchr (szFullName, _T('.')), _T(".bat")) ||
@@ -465,9 +490,11 @@ Execute (LPTSTR Full, LPTSTR First, LPTSTR Rest)
        /* Get code page if it has been change */
        InputCodePage= GetConsoleCP();
     OutputCodePage = GetConsoleOutputCP();
-#ifndef __REACTOS__
-       SetConsoleTitle (szWindowTitle);
-#endif
+    SetConsoleTitle (szWindowTitle);
+
+ free(first);
+ free(rest);
+ free(full);
 }
 
 
@@ -796,9 +823,9 @@ VOID ParseCommandLine (LPTSTR cmd)
       }
       
       out[size]=_T('\0');
-      hFile = CreateFile (out, GENERIC_WRITE, FILE_SHARE_WRITE, &sa,
+      hFile = CreateFile (out, GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, &sa,
                                    (nRedirFlags & OUTPUT_APPEND) ? OPEN_ALWAYS : CREATE_ALWAYS,
-                                   FILE_ATTRIBUTE_NORMAL, NULL);
+                                   FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, NULL);
 
      if (hFile == INVALID_HANDLE_VALUE)
      {
@@ -858,10 +885,10 @@ VOID ParseCommandLine (LPTSTR cmd)
                {
                        hFile = CreateFile (err,
                                            GENERIC_WRITE,
-                                           0,
+                                           FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
                                            &sa,
                                            (nRedirFlags & ERROR_APPEND) ? OPEN_ALWAYS : CREATE_ALWAYS,
-                                           FILE_ATTRIBUTE_NORMAL,
+                                           FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH,
                                            NULL);
                        if (hFile == INVALID_HANDLE_VALUE)
                        {
@@ -1010,7 +1037,7 @@ ProcessInput (BOOL bFlag)
        LPTSTR cp;
        BOOL bEchoThisLine;
 
-
        do
        {
                /* if no batch input then... */
@@ -1027,7 +1054,7 @@ ProcessInput (BOOL bFlag)
                cp = commandline;
                while (*ip)
                {
-                       if (*ip == _T('%'))
+         if (*ip == _T('%'))
                        {
                                switch (*++ip)
                                {
@@ -1077,7 +1104,6 @@ ProcessInput (BOOL bFlag)
                            GetCurrentDirectory (MAX_PATH, szPath);
                 cp = _stpcpy (cp, szPath);                 
               }
-
               /* %TIME% */
               else if (_tcsicmp(ip,_T("time")) ==0)
               {
@@ -1146,8 +1172,18 @@ ProcessInput (BOOL bFlag)
                 evar = malloc ( 512 * sizeof(TCHAR));
                 if (evar==NULL) 
                     return 1; 
-
+                                        SetLastError(0);
                 size = GetEnvironmentVariable (ip, evar, 512);
+                                        if(GetLastError() == ERROR_ENVVAR_NOT_FOUND)
+                                        {
+                                                /* if no env var is found you must 
+                                                   continue with what was input*/
+                                          cp = _stpcpy (cp, _T("%"));
+                                               cp = _stpcpy (cp, ip);
+                                               cp = _stpcpy (cp, _T("%"));
+                                        }
+                                        else
+                                        {
                 if (size > 512)
                 {
                     evar = realloc(evar,size * sizeof(TCHAR) );
@@ -1162,6 +1198,7 @@ ProcessInput (BOOL bFlag)
                 {
                                                                 cp = _stpcpy (cp, evar);
                 }
+                                        }
 
                 free(evar);
               }
@@ -1301,6 +1338,7 @@ Initialize (int argc, TCHAR* argv[])
        TCHAR commandline[CMDLINE_LENGTH];
        TCHAR ModuleName[_MAX_PATH + 1];
        INT i;
+       TCHAR lpBuffer[2];
 
        //INT len;
        //TCHAR *ptr, *cmdLine;
@@ -1348,6 +1386,13 @@ Initialize (int argc, TCHAR* argv[])
        hOut = GetStdHandle (STD_OUTPUT_HANDLE);
        hIn  = GetStdHandle (STD_INPUT_HANDLE);
 
+       /* Set EnvironmentVariable PROMPT if it does not exists any env value. 
+          for you can change the EnvirommentVariable for prompt before cmd start 
+          this patch are not 100% right, if it does not exists a PROMPT value cmd should use
+          $P$G as defualt not set EnvirommentVariable PROMPT to $P$G if it does not exists */
+       if (GetEnvironmentVariable(_T("PROMPT"),lpBuffer, 2 * sizeof(TCHAR)) == 0) 
+           SetEnvironmentVariable (_T("PROMPT"), _T("$P$G"));
+
 
        if (argc >= 2 && !_tcsncmp (argv[1], _T("/?"), 2))
        {