1 /* $Id: cmd.c,v 1.21 2000/05/26 06:06:05 phreak Exp $
3 * CMD.C - command-line interface.
8 * 17 Jun 1994 (Tim Norman)
11 * 08 Aug 1995 (Matt Rains)
12 * I have cleaned up the source code. changes now bring this source
13 * into guidelines for recommended programming practice.
15 * A added the the standard FreeDOS GNU licence test to the
16 * initialize() function.
18 * Started to replace puts() with printf(). this will help
19 * standardize output. please follow my lead.
21 * I have added some constants to help making changes easier.
23 * 15 Dec 1995 (Tim Norman)
24 * major rewrite of the code to make it more efficient and add
25 * redirection support (finally!)
27 * 06 Jan 1996 (Tim Norman)
28 * finished adding redirection support! Changed to use our own
29 * exec code (MUCH thanks to Svante Frey!!)
31 * 29 Jan 1996 (Tim Norman)
32 * added support for CHDIR, RMDIR, MKDIR, and ERASE, as per
33 * suggestion of Steffan Kaiser
35 * changed "file not found" error message to "bad command or
36 * filename" thanks to Dustin Norman for noticing that confusing
39 * changed the format to call internal commands (again) so that if
40 * they want to split their commands, they can do it themselves
41 * (none of the internal functions so far need that much power, anyway)
43 * 27 Aug 1996 (Tim Norman)
44 * added in support for Oliver Mueller's ALIAS command
46 * 14 Jun 1997 (Steffan Kaiser)
47 * added ctrl-break handling and error level
49 * 16 Jun 1998 (Rob Lake)
50 * Runs command.com if /P is specified in command line. Command.com
51 * also stays permanent. If /C is in the command line, starts the
52 * program next in the line.
54 * 21 Jun 1998 (Rob Lake)
55 * Fixed up /C so that arguments for the program
57 * 08-Jul-1998 (John P. Price)
58 * Now sets COMSPEC environment variable
59 * misc clean up and optimization
60 * added date and time commands
61 * changed to using spawnl instead of exec. exec does not copy the
62 * environment to the child process!
64 * 14 Jul 1998 (Hans B Pufal)
65 * Reorganised source to be more efficient and to more closely
66 * follow MS-DOS conventions. (eg %..% environment variable
67 * replacement works form command line as well as batch file.
69 * New organisation also properly support nested batch files.
71 * New command table structure is half way towards providing a
72 * system in which COMMAND will find out what internal commands
75 * 24 Jul 1998 (Hans B Pufal) [HBP_003]
76 * Fixed return value when called with /C option
78 * 27 Jul 1998 John P. Price
79 * added config.h include
81 * 28 Jul 1998 John P. Price
82 * added showcmds function to show commands and options available
84 * 07-Aug-1998 (John P Price <linux-guru@gcfl.net>)
85 * Fixed carrage return output to better match MSDOS with echo
86 * on or off. (marked with "JPP 19980708")
88 * 07-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
89 * First ReactOS release.
90 * Extended length of commandline buffers to 512.
92 * 13-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
93 * Added COMSPEC environment variable.
94 * Added "/t" support (color) on cmd command line.
96 * 07-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
97 * Added help text ("cmd /?").
99 * 25-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
100 * Unicode and redirection safe!
101 * Fixed redirections and piping.
102 * Piping is based on temporary files, but basic support
103 * for anonymous pipes already exists.
105 * 27-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
106 * Replaced spawnl() by CreateProcess().
108 * 22-Oct-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
109 * Added break handler.
111 * 15-Dec-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
112 * Fixed current directory
114 * 28-Dec-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
115 * Restore window title after program/batch execution
131 #define CMDLINE_LENGTH 2048 //1024
134 BOOL bExit
= FALSE
; /* indicates EXIT was typed */
135 BOOL bCanExit
= TRUE
; /* indicates if this shell is exitable */
136 BOOL bCtrlBreak
= FALSE
; /* Ctrl-Break or Ctrl-C hit */
137 BOOL bIgnoreEcho
= FALSE
; /* Ignore 'newline' before 'cls' */
138 INT nErrorLevel
= 0; /* Errorlevel of last launched external program */
139 BOOL bChildProcessRunning
= FALSE
;
140 DWORD dwChildProcessId
= 0;
145 #ifdef INCLUDE_CMD_COLOR
146 WORD wColor
; /* current color */
147 WORD wDefColor
; /* default color */
152 * is character a delimeter when used on first word?
156 static BOOL
IsDelimiter (TCHAR c
)
158 return (c
== _T('/') || c
== _T('=') || c
== _T('\0') || _istspace (c
));
163 * This command (in first) was not found in the command table
165 * first - first word on command line
166 * rest - rest of command line
170 Execute (LPTSTR first
, LPTSTR rest
)
172 TCHAR szFullName
[MAX_PATH
];
174 TCHAR szWindowTitle
[MAX_PATH
];
176 DWORD dwExitCode
= 0;
179 DebugPrintf ("Execute: \'%s\' \'%s\'\n", first
, rest
);
182 /* check for a drive change */
183 if ((_istalpha (first
[0])) && (!_tcscmp (first
+ 1, _T(":"))))
185 TCHAR szPath
[MAX_PATH
];
189 DebugPrintf ("Drive change to drive %s\n", first
);
191 /* save curent directory in environment variable */
192 GetCurrentDirectory (MAX_PATH
, szPath
);
194 _tcscpy (szVar
, _T("=A:"));
195 szVar
[1] = _totupper (szPath
[0]);
197 SetEnvironmentVariable (szVar
, szPath
);
200 /* check for current directory of new drive */
201 _tcscpy (szVar
, _T("=A:"));
202 szVar
[1] = _totupper (*first
);
204 if (GetEnvironmentVariable (szVar
, szPath
, MAX_PATH
) == 0)
206 /* no environment variable found */
207 _tcscpy (szPath
, _T("A:\\"));
208 szPath
[0] = _totupper (*first
);
212 DebugPrintf ("Drive change to drive %s\n", szPath
);
215 /* set new current directory */
216 SetCurrentDirectory (szPath
);
217 GetCurrentDirectory (MAX_PATH
, szPath
);
218 if (szPath
[0] != (TCHAR
)_totupper (*first
))
219 ConErrPuts (INVALIDDRIVE
);
224 /* get the PATH environment variable and parse it */
225 /* search the PATH environment variable for the binary */
226 if (!SearchForExecutable (first
, szFullName
))
228 error_bad_command ();
233 GetConsoleTitle (szWindowTitle
, MAX_PATH
);
236 /* check if this is a .BAT or .CMD file */
237 if (!_tcsicmp (_tcsrchr (szFullName
, _T('.')), _T(".bat")) ||
238 !_tcsicmp (_tcsrchr (szFullName
, _T('.')), _T(".cmd")))
241 DebugPrintf ("[BATCH: %s %s]\n", szFullName
, rest
);
243 Batch (szFullName
, first
, rest
);
247 /* exec the program */
249 TCHAR szFullCmdLine
[1024];
251 PROCESS_INFORMATION prci
;
255 DebugPrintf ("[EXEC: %s %s]\n", szFullName
, rest
);
258 /* build command line for CreateProcess() */
259 _tcscpy (szFullCmdLine
, szFullName
);
260 _tcscat (szFullCmdLine
, _T(" "));
261 _tcscat (szFullCmdLine
, rest
);
264 /* fill startup info */
265 memset (&stui
, 0, sizeof (STARTUPINFO
));
266 stui
.cb
= sizeof (STARTUPINFO
);
267 stui
.dwFlags
= STARTF_USESHOWWINDOW
;
268 stui
.wShowWindow
= SW_SHOWDEFAULT
;
271 if (CreateProcess (NULL
,
276 CREATE_NEW_PROCESS_GROUP
,
282 if (CreateProcess (szFullName
,
294 /* FIXME: Protect this with critical section */
295 bChildProcessRunning
= TRUE
;
296 dwChildProcessId
= prci
.dwProcessId
;
298 WaitForSingleObject (prci
.hProcess
, INFINITE
);
300 /* FIXME: Protect this with critical section */
301 bChildProcessRunning
= TRUE
;
303 GetExitCodeProcess (prci
.hProcess
, &dwExitCode
);
304 nErrorLevel
= (INT
)dwExitCode
;
305 CloseHandle (prci
.hThread
);
306 CloseHandle (prci
.hProcess
);
310 ErrorMessage (GetLastError (),
311 "Error executing CreateProcess()!!\n");
316 SetConsoleTitle (szWindowTitle
);
322 * look through the internal commands and determine whether or not this
323 * command is one of them. If it is, call the command. If not, call
324 * execute to run it as an external program.
326 * line - the command line of the program to run
331 DoCommand (LPTSTR line
)
333 TCHAR com
[MAX_PATH
]; /* the first word in the command */
336 LPTSTR rest
= line
; /* pointer to the rest of the command line */
341 DebugPrintf ("DoCommand: (\'%s\')\n", line
);
344 /* Skip over initial white space */
345 while (isspace (*rest
))
350 /* Anything to do ? */
353 /* Copy over 1st word as lower case */
354 while (!IsDelimiter (*rest
))
355 *cp
++ = _totlower (*rest
++);
357 /* Terminate first word */
360 /* Skip over whitespace to rest of line */
361 while (_istspace (*rest
))
364 /* Scan internal command table */
365 for (cmdptr
= cmds
;; cmdptr
++)
367 /* If end of table execute ext cmd */
368 if (cmdptr
->name
== NULL
)
374 if (!_tcscmp (com
, cmdptr
->name
))
376 cmdptr
->func (com
, rest
);
380 /* The following code handles the case of commands like CD which
381 * are recognised even when the command name and parameter are
382 * not space separated.
388 /* Get length of command name */
389 cl
= _tcslen (cmdptr
->name
);
391 if ((cmdptr
->flags
& CMD_SPECIAL
) &&
392 (!_tcsncmp (cmdptr
->name
, com
, cl
)) &&
393 (_tcschr (_T("\\.-"), *(com
+ cl
))))
395 /* OK its one of the specials...*/
397 /* Terminate first word properly */
400 /* Call with new rest */
401 cmdptr
->func (com
, cstart
+ cl
);
410 * process the command line and execute the appropriate functions
411 * full input/output redirection and piping are supported
414 VOID
ParseCommandLine (LPTSTR cmd
)
416 TCHAR cmdline
[CMDLINE_LENGTH
];
418 #ifdef FEATURE_REDIRECTION
419 TCHAR in
[CMDLINE_LENGTH
] = "";
420 TCHAR out
[CMDLINE_LENGTH
] = "";
421 TCHAR err
[CMDLINE_LENGTH
] = "";
422 TCHAR szTempPath
[MAX_PATH
] = _T(".\\");
423 TCHAR szFileName
[2][MAX_PATH
] = {"", ""};
424 HANDLE hFile
[2] = {INVALID_HANDLE_VALUE
, INVALID_HANDLE_VALUE
};
432 #endif /* FEATURE_REDIRECTION */
434 _tcscpy (cmdline
, cmd
);
438 DebugPrintf ("ParseCommandLine: (\'%s\')\n", s
);
441 #ifdef FEATURE_ALIASES
442 /* expand all aliases */
443 ExpandAlias (s
, CMDLINE_LENGTH
);
444 #endif /* FEATURE_ALIAS */
446 #ifdef FEATURE_REDIRECTION
447 /* find the temp path to store temporary files */
448 GetTempPath (MAX_PATH
, szTempPath
);
449 if (szTempPath
[_tcslen (szTempPath
) - 1] != _T('\\'))
450 _tcscat (szTempPath
, _T("\\"));
452 /* get the redirections from the command line */
453 num
= GetRedirection (s
, in
, out
, err
, &nRedirFlags
);
455 /* more efficient, but do we really need to do this? */
456 for (t
= in
; _istspace (*t
); t
++)
460 for (t
= out
; _istspace (*t
); t
++)
464 for (t
= err
; _istspace (*t
); t
++)
468 /* Set up the initial conditions ... */
469 /* preserve STDIN, STDOUT and STDERR handles */
470 hOldConIn
= GetStdHandle (STD_INPUT_HANDLE
);
471 hOldConOut
= GetStdHandle (STD_OUTPUT_HANDLE
);
472 hOldConErr
= GetStdHandle (STD_ERROR_HANDLE
);
479 hFile
= CreateFile (in
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
,
480 FILE_ATTRIBUTE_NORMAL
, NULL
);
481 if (hFile
== INVALID_HANDLE_VALUE
)
483 ConErrPrintf ("Can't redirect input from file %s\n", in
);
487 if (!SetStdHandle (STD_INPUT_HANDLE
, hFile
))
489 ConErrPrintf ("Can't redirect input from file %s\n", in
);
493 DebugPrintf (_T("Input redirected from: %s\n"), in
);
497 /* Now do all but the last pipe command */
498 *szFileName
[0] = '\0';
499 hFile
[0] = INVALID_HANDLE_VALUE
;
503 /* Create unique temporary file name */
504 GetTempFileName (szTempPath
, "CMD", 0, szFileName
[1]);
506 /* Set current stdout to temporary file */
507 hFile
[1] = CreateFile (szFileName
[1], GENERIC_WRITE
, 0, NULL
,
508 TRUNCATE_EXISTING
, FILE_ATTRIBUTE_TEMPORARY
, NULL
);
509 SetStdHandle (STD_OUTPUT_HANDLE
, hFile
[1]);
513 /* close stdout file */
514 SetStdHandle (STD_OUTPUT_HANDLE
, hOldConOut
);
515 if ((hFile
[1] != INVALID_HANDLE_VALUE
) && (hFile
[1] != hOldConOut
))
517 CloseHandle (hFile
[1]);
518 hFile
[1] = INVALID_HANDLE_VALUE
;
521 /* close old stdin file */
522 SetStdHandle (STD_INPUT_HANDLE
, hOldConIn
);
523 if ((hFile
[0] != INVALID_HANDLE_VALUE
) && (hFile
[0] != hOldConIn
))
525 /* delete old stdin file, if it is a real file */
526 CloseHandle (hFile
[0]);
527 hFile
[0] = INVALID_HANDLE_VALUE
;
528 DeleteFile (szFileName
[0]);
529 *szFileName
[0] = _T('\0');
532 /* copy stdout file name to stdin file name */
533 _tcscpy (szFileName
[0], szFileName
[1]);
534 *szFileName
[1] = _T('\0');
536 /* open new stdin file */
537 hFile
[0] = CreateFile (szFileName
[0], GENERIC_READ
, 0, NULL
,
538 OPEN_EXISTING
, FILE_ATTRIBUTE_TEMPORARY
, NULL
);
539 SetStdHandle (STD_INPUT_HANDLE
, hFile
[0]);
541 s
= s
+ _tcslen (s
) + 1;
544 /* Now set up the end conditions... */
545 /* redirect STDOUT */
548 /* Final output to here */
551 hFile
= CreateFile (out
, GENERIC_WRITE
, 0, NULL
,
552 (nRedirFlags
& OUTPUT_APPEND
) ? OPEN_ALWAYS
: CREATE_ALWAYS
,
553 FILE_ATTRIBUTE_NORMAL
, NULL
);
554 if (hFile
== INVALID_HANDLE_VALUE
)
556 ConErrPrintf ("Can't redirect to file %s\n", out
);
560 if (!SetStdHandle (STD_OUTPUT_HANDLE
, hFile
))
562 ConErrPrintf ("Can't redirect to file %s\n", out
);
566 if (nRedirFlags
& OUTPUT_APPEND
)
570 if (GetFileType (hFile
) == FILE_TYPE_DISK
)
571 SetFilePointer (hFile
, 0, &lHighPos
, FILE_END
);
574 DebugPrintf (_T("Output redirected to: %s\n"), out
);
577 else if (hOldConOut
!= INVALID_HANDLE_VALUE
)
579 /* Restore original stdout */
580 HANDLE hOut
= GetStdHandle (STD_OUTPUT_HANDLE
);
581 SetStdHandle (STD_OUTPUT_HANDLE
, hOldConOut
);
582 if (hOldConOut
!= hOut
)
584 hOldConOut
= INVALID_HANDLE_VALUE
;
587 /* redirect STDERR */
590 /* Final output to here */
593 if (!_tcscmp (err
, out
))
596 DebugPrintf (_T("Stdout and stderr will use the same file!!\n"));
598 DuplicateHandle (GetCurrentProcess (),
599 GetStdHandle (STD_OUTPUT_HANDLE
),
600 GetCurrentProcess (),
601 &hFile
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
605 hFile
= CreateFile (err
,
609 (nRedirFlags
& ERROR_APPEND
) ? OPEN_ALWAYS
: CREATE_ALWAYS
,
610 FILE_ATTRIBUTE_NORMAL
,
612 if (hFile
== INVALID_HANDLE_VALUE
)
614 ConErrPrintf ("Can't redirect to file %s\n", err
);
618 if (!SetStdHandle (STD_ERROR_HANDLE
, hFile
))
620 ConErrPrintf ("Can't redirect to file %s\n", err
);
624 if (nRedirFlags
& ERROR_APPEND
)
628 if (GetFileType (hFile
) == FILE_TYPE_DISK
)
629 SetFilePointer (hFile
, 0, &lHighPos
, FILE_END
);
632 DebugPrintf (_T("Error redirected to: %s\n"), err
);
635 else if (hOldConErr
!= INVALID_HANDLE_VALUE
)
637 /* Restore original stderr */
638 HANDLE hErr
= GetStdHandle (STD_ERROR_HANDLE
);
639 SetStdHandle (STD_ERROR_HANDLE
, hOldConErr
);
640 if (hOldConErr
!= hErr
)
642 hOldConErr
= INVALID_HANDLE_VALUE
;
646 /* process final command */
649 #ifdef FEATURE_REDIRECTION
650 /* close old stdin file */
651 #if 0 /* buggy implementation */
652 SetStdHandle (STD_INPUT_HANDLE
, hOldConIn
);
653 if ((hFile
[0] != INVALID_HANDLE_VALUE
) &&
654 (hFile
[0] != hOldConIn
))
656 /* delete old stdin file, if it is a real file */
657 CloseHandle (hFile
[0]);
658 hFile
[0] = INVALID_HANDLE_VALUE
;
659 DeleteFile (szFileName
[0]);
660 *szFileName
[0] = _T('\0');
663 /* Restore original STDIN */
664 if (hOldConIn
!= INVALID_HANDLE_VALUE
)
666 HANDLE hIn
= GetStdHandle (STD_INPUT_HANDLE
);
667 SetStdHandle (STD_INPUT_HANDLE
, hOldConIn
);
668 if (hOldConIn
!= hIn
)
670 hOldConIn
= INVALID_HANDLE_VALUE
;
675 DebugPrintf (_T("Can't restore STDIN! Is invalid!!\n"), out
);
678 #endif /* buggy implementation */
681 if (hOldConIn
!= INVALID_HANDLE_VALUE
)
683 HANDLE hIn
= GetStdHandle (STD_INPUT_HANDLE
);
684 SetStdHandle (STD_INPUT_HANDLE
, hOldConIn
);
685 if (hIn
== INVALID_HANDLE_VALUE
)
688 DebugPrintf (_T("Previous STDIN is invalid!!\n"));
693 if (GetFileType (hIn
) == FILE_TYPE_DISK
)
697 CloseHandle (hFile
[0]);
698 hFile
[0] = INVALID_HANDLE_VALUE
;
699 DeleteFile (szFileName
[0]);
700 *szFileName
[0] = _T('\0');
705 DebugPrintf (_T("hFile[0] and hIn dont match!!!\n"));
713 /* Restore original STDOUT */
714 if (hOldConOut
!= INVALID_HANDLE_VALUE
)
716 HANDLE hOut
= GetStdHandle (STD_OUTPUT_HANDLE
);
717 SetStdHandle (STD_OUTPUT_HANDLE
, hOldConOut
);
718 if (hOldConOut
!= hOut
)
720 hOldConOut
= INVALID_HANDLE_VALUE
;
723 /* Restore original STDERR */
724 if (hOldConErr
!= INVALID_HANDLE_VALUE
)
726 HANDLE hErr
= GetStdHandle (STD_ERROR_HANDLE
);
727 SetStdHandle (STD_ERROR_HANDLE
, hOldConErr
);
728 if (hOldConErr
!= hErr
)
730 hOldConErr
= INVALID_HANDLE_VALUE
;
732 #endif /* FEATURE_REDIRECTION */
737 * do the prompt/input/process loop
742 ProcessInput (BOOL bFlag
)
744 TCHAR commandline
[CMDLINE_LENGTH
];
745 TCHAR readline
[CMDLINE_LENGTH
];
753 /* if no batch input then... */
754 if (!(ip
= ReadBatchLine (&bEchoThisLine
)))
759 ReadCommand (readline
, CMDLINE_LENGTH
);
761 bEchoThisLine
= FALSE
;
785 if ((tp
= FindArg (*ip
- _T('0'))))
787 cp
= stpcpy (cp
, tp
);
795 cp
+= _stprintf (cp
, _T("%u"), nErrorLevel
);
800 if ((tp
= _tcschr (ip
, _T('%'))))
805 /* FIXME: This is just a quick hack!! */
806 /* Do a proper memory allocation!! */
807 if (GetEnvironmentVariable (ip
, evar
, 512))
808 cp
= stpcpy (cp
, evar
);
824 /* strip trailing spaces */
825 while ((--cp
>= commandline
) && _istspace (*cp
))
828 *(cp
+ 1) = _T('\0');
831 /* Echo batch file line */
835 ConOutPuts (commandline
);
840 ParseCommandLine (commandline
);
841 if (bEcho
&& !bIgnoreEcho
)
846 while (!bCanExit
|| !bExit
);
853 * control-break handler.
855 BOOL
BreakHandler (DWORD dwCtrlType
)
857 if ((dwCtrlType
!= CTRL_C_EVENT
) &&
858 (dwCtrlType
!= CTRL_BREAK_EVENT
))
861 if (bChildProcessRunning
== TRUE
)
863 GenerateConsoleCtrlEvent (CTRL_C_EVENT
,
868 /* FIXME: Handle batch files */
870 /* FIXME: Print "^C" */
877 VOID
AddBreakHandler (VOID
)
880 SetConsoleCtrlHandler ((PHANDLER_ROUTINE
)&BreakHandler
,
886 VOID
RemoveBreakHandler (VOID
)
889 SetConsoleCtrlHandler (NULL
, FALSE
);
895 * show commands and options that are available.
904 ConOutPrintf (_T("\nInternal commands available:\n"));
911 ConOutPuts (cmdptr
->name
);
915 ConOutPrintf (_T("%-10s"), cmdptr
->name
);
923 /* print feature list */
924 ConOutPuts ("\nFeatures available:");
925 #ifdef FEATURE_ALIASES
926 ConOutPuts (" [aliases]");
928 #ifdef FEATURE_HISTORY
929 ConOutPuts (" [history]");
931 #ifdef FEATURE_UNIX_FILENAME_COMPLETION
932 ConOutPuts (" [unix filename completion]");
934 #ifdef FEATURE_DIRECTORY_STACK
935 ConOutPuts (" [directory stack]");
937 #ifdef FEATURE_REDIRECTION
938 ConOutPuts (" [redirections and piping]");
945 * set up global initializations and process parameters
947 * argc - number of parameters to command.com
948 * argv - command-line parameters
952 Initialize (int argc
, char *argv
[])
954 TCHAR commandline
[CMDLINE_LENGTH
];
960 DebugPrintf ("[command args:\n");
961 for (x
= 0; x
< argc
; x
++)
963 DebugPrintf ("%d. %s\n", x
, argv
[x
]);
968 /* get version information */
969 osvi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
970 GetVersionEx (&osvi
);
974 /* get default input and output console handles */
975 hOut
= GetStdHandle (STD_OUTPUT_HANDLE
);
976 hIn
= GetStdHandle (STD_INPUT_HANDLE
);
978 SetConsoleMode (hIn
, ENABLE_PROCESSED_INPUT
);
980 if (argc
>= 2 && !_tcsncmp (argv
[1], _T("/?"), 2))
982 ConOutPuts (_T("Starts a new instance of the ReactOS command line interpreter.\n"
984 "CMD [/[C|K] command][/P][/Q][/T:bf]\n"
986 " /C command Runs the specified command and terminates.\n"
987 " /K command Runs the specified command and remains.\n"
988 " /P CMD becomes permanent and runs autoexec.bat\n"
989 " (cannot be terminated).\n"
990 " /T:bf Sets the background/foreground color (see COLOR command)."));
994 #ifdef INCLUDE_CMD_CHDIR
998 #ifdef FATURE_ALIASES
1004 for (i
= 1; i
< argc
; i
++)
1006 if (!_tcsicmp (argv
[i
], _T("/p")))
1008 if (!IsValidFileName (_T("\\autoexec.bat")))
1010 #ifdef INCLUDE_CMD_DATE
1013 #ifdef INCLUDE_CMD_TIME
1019 ParseCommandLine (_T("\\autoexec.bat"));
1023 else if (!_tcsicmp (argv
[i
], _T("/c")))
1025 /* This just runs a program and exits */
1027 _tcscpy (commandline
, argv
[i
]);
1030 _tcscat (commandline
, " ");
1031 _tcscat (commandline
, argv
[i
]);
1034 ParseCommandLine(commandline
);
1035 ExitProcess (ProcessInput (TRUE
));
1037 else if (!_tcsicmp (argv
[i
], _T("/k")))
1039 /* This just runs a program and remains */
1041 _tcscpy (commandline
, argv
[i
]);
1044 _tcscat (commandline
, " ");
1045 _tcscat (commandline
, argv
[i
]);
1048 ParseCommandLine(commandline
);
1050 #ifdef INCLUDE_CMD_COLOR
1051 else if (!_tcsnicmp (argv
[i
], _T("/t:"), 3))
1053 /* process /t (color) argument */
1054 wDefColor
= (WORD
)strtoul (&argv
[i
][3], NULL
, 16);
1056 SetScreenColor (wColor
, TRUE
);
1065 /* run cmdstart.bat */
1066 if (IsValidFileName (_T("cmdstart.bat")))
1068 ParseCommandLine (_T("cmdstart.bat"));
1070 else if (IsValidFileName (_T("\\cmdstart.bat")))
1072 ParseCommandLine (_T("\\cmdstart.bat"));
1076 /* try to run cmdstart.bat from install dir */
1079 _tcscpy (commandline
, argv
[0]);
1080 p
= _tcsrchr (commandline
, _T('\\')) + 1;
1081 _tcscpy (p
, _T("cmdstart.bat"));
1083 if (IsValidFileName (_T("commandline")))
1085 ConErrPrintf ("Running %s...\n", commandline
);
1086 ParseCommandLine (commandline
);
1090 #ifdef FEATURE_DIR_STACK
1091 /* initialize directory stack */
1092 InitDirectoryStack ();
1096 #ifdef FEATURE_HISTORY
1097 /*initialize history*/
1101 /* Set COMSPEC environment variable */
1104 SetEnvironmentVariable (_T("COMSPEC"), argv
[0]);
1107 /* add ctrl break handler */
1112 static VOID
Cleanup (int argc
, char *argv
[])
1114 /* run cmdexit.bat */
1115 if (IsValidFileName (_T("cmdexit.bat")))
1117 ConErrPrintf ("Running cmdexit.bat...\n");
1118 ParseCommandLine (_T("cmdexit.bat"));
1120 else if (IsValidFileName (_T("\\cmdexit.bat")))
1122 ConErrPrintf ("Running \\cmdexit.bat...\n");
1123 ParseCommandLine (_T("\\cmdexit.bat"));
1127 /* try to run cmdexit.bat from install dir */
1128 TCHAR commandline
[CMDLINE_LENGTH
];
1131 _tcscpy (commandline
, argv
[0]);
1132 p
= _tcsrchr (commandline
, _T('\\')) + 1;
1133 _tcscpy (p
, _T("cmdexit.bat"));
1135 if (IsValidFileName (_T("commandline")))
1137 ConErrPrintf ("Running %s...\n", commandline
);
1138 ParseCommandLine (commandline
);
1142 #ifdef FEATURE_ALIASES
1146 #ifdef FEATURE_DIECTORY_STACK
1147 /* destroy directory stack */
1148 DestroyDirectoryStack ();
1151 #ifdef INCLUDE_CMD_CHDIR
1155 #ifdef FEATURE_HISTORY
1160 /* remove ctrl break handler */
1161 RemoveBreakHandler ();
1168 int main (int argc
, char *argv
[])
1171 CONSOLE_SCREEN_BUFFER_INFO Info
;
1174 SetFileApisToOEM ();
1176 if( GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE
), &Info
) == FALSE
)
1177 printf( "GetConsoleScreenBufferInfo: Error: %d\n", GetLastError() );
1178 wColor
= Info
.wAttributes
;
1180 /* check switches on command-line */
1181 Initialize (argc
, argv
);
1183 /* call prompt routine */
1184 nExitCode
= ProcessInput (FALSE
);
1186 /* do the cleanup */
1187 Cleanup (argc
, argv
);