Various improvements by Carl Nettelblad.
[reactos.git] / rosapps / cmd / cmd.c
index 56eb568..7d5c8c6 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: cmd.c,v 1.19 2000/02/18 00:53:11 ekohl Exp $
+/* $Id: cmd.c,v 1.24 2001/02/28 22:33:23 ekohl Exp $
  *
  *  CMD.C - command-line interface.
  *
  *
  *    28-Dec-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
  *        Restore window title after program/batch execution
+ *
+ *    03-Feb-2001 (Eric Kohl <ekohl@rz-online.de>)
+ *        Workaround because argc[0] is NULL under ReactOS
+ *
+ *    23-Feb-2001 (Carl Nettelblad <cnettel@hem.passagen.se>)
+ *        %envvar% replacement conflicted with for.
  */
 
 #include "config.h"
 #include "batch.h"
 
 
-#define CMDLINE_LENGTH  1024
-
-
 BOOL bExit = FALSE;       /* indicates EXIT was typed */
 BOOL bCanExit = TRUE;     /* indicates if this shell is exitable */
 BOOL bCtrlBreak = FALSE;  /* Ctrl-Break or Ctrl-C hit */
@@ -143,8 +146,8 @@ HANDLE hIn;
 HANDLE hOut;
 
 #ifdef INCLUDE_CMD_COLOR
-WORD wColor = FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN; /* current color */
-WORD wDefColor = FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN; /* default color */
+WORD wColor;              /* current color */
+WORD wDefColor;           /* default color */
 #endif
 
 
@@ -246,7 +249,7 @@ Execute (LPTSTR first, LPTSTR rest)
        {
                /* exec the program */
 #ifndef __REACTOS__
-               TCHAR szFullCmdLine [1024];
+               TCHAR szFullCmdLine [CMDLINE_LENGTH];
 #endif
                PROCESS_INFORMATION prci;
                STARTUPINFO stui;
@@ -797,7 +800,7 @@ ProcessInput (BOOL bFlag)
                                                break;
 
                                        default:
-                                               if ((tp = _tcschr (ip, _T('%'))))
+                                               if ((tp = _tcschr (ip, _T('%'))) && (tp<=(unsigned int)strchr(ip,_T(' '))-1))
                                                {
                                                        char evar[512];
                                                        *tp = _T('\0');
@@ -809,6 +812,10 @@ ProcessInput (BOOL bFlag)
 
                                                        ip = tp + 1;
                                                }
+                                               else
+                                               {
+                                                       *cp++ = _T('%');
+                                               }
                                                break;
                                }
                                continue;
@@ -822,8 +829,7 @@ ProcessInput (BOOL bFlag)
                *cp = _T('\0');
 
                /* strip trailing spaces */
-               while ((--cp >= commandline) && _istspace (*cp))
-                       ;
+               while ((--cp >= commandline) && _istspace (*cp));
 
                *(cp + 1) = _T('\0');
 
@@ -898,27 +904,9 @@ VOID RemoveBreakHandler (VOID)
 static VOID
 ShowCommands (VOID)
 {
-       LPCOMMAND cmdptr;
-       INT y;
-
+       /* print command list */
        ConOutPrintf (_T("\nInternal commands available:\n"));
-       y = 0;
-       cmdptr = cmds;
-       while (cmdptr->name)
-       {
-               if (++y == 8)
-               {
-                       ConOutPuts (cmdptr->name);
-                       y = 0;
-               }
-               else
-                       ConOutPrintf (_T("%-10s"), cmdptr->name);
-
-               cmdptr++;
-       }
-
-       if (y != 0)
-               ConOutChar ('\n');
+       PrintCommandList ();
 
        /* print feature list */
        ConOutPuts ("\nFeatures available:");
@@ -1024,28 +1012,38 @@ Initialize (int argc, char *argv[])
                        {
                                /* This just runs a program and exits */
                                ++i;
-                               _tcscpy (commandline, argv[i]);
-                               while (argv[++i])
+                               if (argv[i])
                                {
-                                       _tcscat (commandline, " ");
-                                       _tcscat (commandline, argv[i]);
+                                       _tcscpy (commandline, argv[i]);
+                                       while (argv[++i])
+                                       {
+                                               _tcscat (commandline, " ");
+                                               _tcscat (commandline, argv[i]);
+                                       }
+
+                                       ParseCommandLine(commandline);
+                                       ExitProcess (ProcessInput (TRUE));
+                               }
+                               else
+                               {
+                                       ExitProcess (0);
                                }
-
-                               ParseCommandLine(commandline);
-                               ExitProcess (ProcessInput (TRUE));
                        }
                        else if (!_tcsicmp (argv[i], _T("/k")))
                        {
                                /* This just runs a program and remains */
                                ++i;
-                               _tcscpy (commandline, argv[i]);
-                               while (argv[++i])
+                               if (argv[i])
                                {
-                                       _tcscat (commandline, " ");
-                                       _tcscat (commandline, argv[i]);
+                                       _tcscpy (commandline, argv[i]);
+                                       while (argv[++i])
+                                       {
+                                               _tcscat (commandline, " ");
+                                               _tcscat (commandline, argv[i]);
+                                       }
+
+                                       ParseCommandLine(commandline);
                                }
-
-                               ParseCommandLine(commandline);
                        }
 #ifdef INCLUDE_CMD_COLOR
                        else if (!_tcsnicmp (argv[i], _T("/t:"), 3))
@@ -1071,6 +1069,7 @@ Initialize (int argc, char *argv[])
        {
                ParseCommandLine (_T("\\cmdstart.bat"));
        }
+#ifndef __REACTOS__
        else
        {
                /* try to run cmdstart.bat from install dir */
@@ -1086,6 +1085,7 @@ Initialize (int argc, char *argv[])
                        ParseCommandLine (commandline);
                }
        }
+#endif
 
 #ifdef FEATURE_DIR_STACK
        /* initialize directory stack */
@@ -1122,6 +1122,7 @@ static VOID Cleanup (int argc, char *argv[])
                ConErrPrintf ("Running \\cmdexit.bat...\n");
                ParseCommandLine (_T("\\cmdexit.bat"));
        }
+#ifndef __REACTOS__
        else
        {
                /* try to run cmdexit.bat from install dir */
@@ -1138,6 +1139,7 @@ static VOID Cleanup (int argc, char *argv[])
                        ParseCommandLine (commandline);
                }
        }
+#endif
 
 #ifdef FEATURE_ALIASES
        DestroyAlias ();
@@ -1168,10 +1170,15 @@ static VOID Cleanup (int argc, char *argv[])
 int main (int argc, char *argv[])
 {
        INT nExitCode;
+       CONSOLE_SCREEN_BUFFER_INFO Info;
 
        AllocConsole ();
        SetFileApisToOEM ();
 
+       if( GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &Info ) == FALSE )
+          printf( "GetConsoleScreenBufferInfo: Error: %ld\n", GetLastError() );
+       wColor = Info.wAttributes;
+       wDefColor = wColor;
        /* check switches on command-line */
        Initialize (argc, argv);