1 /* $Id: cmd.c,v 1.24 2001/02/28 22:33:23 ekohl 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
117 * 03-Feb-2001 (Eric Kohl <ekohl@rz-online.de>)
118 * Workaround because argc[0] is NULL under ReactOS
120 * 23-Feb-2001 (Carl Nettelblad <cnettel@hem.passagen.se>)
121 * %envvar% replacement conflicted with for.
137 BOOL bExit
= FALSE
; /* indicates EXIT was typed */
138 BOOL bCanExit
= TRUE
; /* indicates if this shell is exitable */
139 BOOL bCtrlBreak
= FALSE
; /* Ctrl-Break or Ctrl-C hit */
140 BOOL bIgnoreEcho
= FALSE
; /* Ignore 'newline' before 'cls' */
141 INT nErrorLevel
= 0; /* Errorlevel of last launched external program */
142 BOOL bChildProcessRunning
= FALSE
;
143 DWORD dwChildProcessId
= 0;
148 #ifdef INCLUDE_CMD_COLOR
149 WORD wColor
; /* current color */
150 WORD wDefColor
; /* default color */
155 * is character a delimeter when used on first word?
159 static BOOL
IsDelimiter (TCHAR c
)
161 return (c
== _T('/') || c
== _T('=') || c
== _T('\0') || _istspace (c
));
166 * This command (in first) was not found in the command table
168 * first - first word on command line
169 * rest - rest of command line
173 Execute (LPTSTR first
, LPTSTR rest
)
175 TCHAR szFullName
[MAX_PATH
];
177 TCHAR szWindowTitle
[MAX_PATH
];
179 DWORD dwExitCode
= 0;
182 DebugPrintf ("Execute: \'%s\' \'%s\'\n", first
, rest
);
185 /* check for a drive change */
186 if ((_istalpha (first
[0])) && (!_tcscmp (first
+ 1, _T(":"))))
188 TCHAR szPath
[MAX_PATH
];
192 DebugPrintf ("Drive change to drive %s\n", first
);
194 /* save curent directory in environment variable */
195 GetCurrentDirectory (MAX_PATH
, szPath
);
197 _tcscpy (szVar
, _T("=A:"));
198 szVar
[1] = _totupper (szPath
[0]);
200 SetEnvironmentVariable (szVar
, szPath
);
203 /* check for current directory of new drive */
204 _tcscpy (szVar
, _T("=A:"));
205 szVar
[1] = _totupper (*first
);
207 if (GetEnvironmentVariable (szVar
, szPath
, MAX_PATH
) == 0)
209 /* no environment variable found */
210 _tcscpy (szPath
, _T("A:\\"));
211 szPath
[0] = _totupper (*first
);
215 DebugPrintf ("Drive change to drive %s\n", szPath
);
218 /* set new current directory */
219 SetCurrentDirectory (szPath
);
220 GetCurrentDirectory (MAX_PATH
, szPath
);
221 if (szPath
[0] != (TCHAR
)_totupper (*first
))
222 ConErrPuts (INVALIDDRIVE
);
227 /* get the PATH environment variable and parse it */
228 /* search the PATH environment variable for the binary */
229 if (!SearchForExecutable (first
, szFullName
))
231 error_bad_command ();
236 GetConsoleTitle (szWindowTitle
, MAX_PATH
);
239 /* check if this is a .BAT or .CMD file */
240 if (!_tcsicmp (_tcsrchr (szFullName
, _T('.')), _T(".bat")) ||
241 !_tcsicmp (_tcsrchr (szFullName
, _T('.')), _T(".cmd")))
244 DebugPrintf ("[BATCH: %s %s]\n", szFullName
, rest
);
246 Batch (szFullName
, first
, rest
);
250 /* exec the program */
252 TCHAR szFullCmdLine
[CMDLINE_LENGTH
];
254 PROCESS_INFORMATION prci
;
258 DebugPrintf ("[EXEC: %s %s]\n", szFullName
, rest
);
261 /* build command line for CreateProcess() */
262 _tcscpy (szFullCmdLine
, szFullName
);
263 _tcscat (szFullCmdLine
, _T(" "));
264 _tcscat (szFullCmdLine
, rest
);
267 /* fill startup info */
268 memset (&stui
, 0, sizeof (STARTUPINFO
));
269 stui
.cb
= sizeof (STARTUPINFO
);
270 stui
.dwFlags
= STARTF_USESHOWWINDOW
;
271 stui
.wShowWindow
= SW_SHOWDEFAULT
;
274 if (CreateProcess (NULL
,
279 CREATE_NEW_PROCESS_GROUP
,
285 if (CreateProcess (szFullName
,
297 /* FIXME: Protect this with critical section */
298 bChildProcessRunning
= TRUE
;
299 dwChildProcessId
= prci
.dwProcessId
;
301 WaitForSingleObject (prci
.hProcess
, INFINITE
);
303 /* FIXME: Protect this with critical section */
304 bChildProcessRunning
= TRUE
;
306 GetExitCodeProcess (prci
.hProcess
, &dwExitCode
);
307 nErrorLevel
= (INT
)dwExitCode
;
308 CloseHandle (prci
.hThread
);
309 CloseHandle (prci
.hProcess
);
313 ErrorMessage (GetLastError (),
314 "Error executing CreateProcess()!!\n");
319 SetConsoleTitle (szWindowTitle
);
325 * look through the internal commands and determine whether or not this
326 * command is one of them. If it is, call the command. If not, call
327 * execute to run it as an external program.
329 * line - the command line of the program to run
334 DoCommand (LPTSTR line
)
336 TCHAR com
[MAX_PATH
]; /* the first word in the command */
339 LPTSTR rest
= line
; /* pointer to the rest of the command line */
344 DebugPrintf ("DoCommand: (\'%s\')\n", line
);
347 /* Skip over initial white space */
348 while (isspace (*rest
))
353 /* Anything to do ? */
356 /* Copy over 1st word as lower case */
357 while (!IsDelimiter (*rest
))
358 *cp
++ = _totlower (*rest
++);
360 /* Terminate first word */
363 /* Skip over whitespace to rest of line */
364 while (_istspace (*rest
))
367 /* Scan internal command table */
368 for (cmdptr
= cmds
;; cmdptr
++)
370 /* If end of table execute ext cmd */
371 if (cmdptr
->name
== NULL
)
377 if (!_tcscmp (com
, cmdptr
->name
))
379 cmdptr
->func (com
, rest
);
383 /* The following code handles the case of commands like CD which
384 * are recognised even when the command name and parameter are
385 * not space separated.
391 /* Get length of command name */
392 cl
= _tcslen (cmdptr
->name
);
394 if ((cmdptr
->flags
& CMD_SPECIAL
) &&
395 (!_tcsncmp (cmdptr
->name
, com
, cl
)) &&
396 (_tcschr (_T("\\.-"), *(com
+ cl
))))
398 /* OK its one of the specials...*/
400 /* Terminate first word properly */
403 /* Call with new rest */
404 cmdptr
->func (com
, cstart
+ cl
);
413 * process the command line and execute the appropriate functions
414 * full input/output redirection and piping are supported
417 VOID
ParseCommandLine (LPTSTR cmd
)
419 TCHAR cmdline
[CMDLINE_LENGTH
];
421 #ifdef FEATURE_REDIRECTION
422 TCHAR in
[CMDLINE_LENGTH
] = "";
423 TCHAR out
[CMDLINE_LENGTH
] = "";
424 TCHAR err
[CMDLINE_LENGTH
] = "";
425 TCHAR szTempPath
[MAX_PATH
] = _T(".\\");
426 TCHAR szFileName
[2][MAX_PATH
] = {"", ""};
427 HANDLE hFile
[2] = {INVALID_HANDLE_VALUE
, INVALID_HANDLE_VALUE
};
435 #endif /* FEATURE_REDIRECTION */
437 _tcscpy (cmdline
, cmd
);
441 DebugPrintf ("ParseCommandLine: (\'%s\')\n", s
);
444 #ifdef FEATURE_ALIASES
445 /* expand all aliases */
446 ExpandAlias (s
, CMDLINE_LENGTH
);
447 #endif /* FEATURE_ALIAS */
449 #ifdef FEATURE_REDIRECTION
450 /* find the temp path to store temporary files */
451 GetTempPath (MAX_PATH
, szTempPath
);
452 if (szTempPath
[_tcslen (szTempPath
) - 1] != _T('\\'))
453 _tcscat (szTempPath
, _T("\\"));
455 /* get the redirections from the command line */
456 num
= GetRedirection (s
, in
, out
, err
, &nRedirFlags
);
458 /* more efficient, but do we really need to do this? */
459 for (t
= in
; _istspace (*t
); t
++)
463 for (t
= out
; _istspace (*t
); t
++)
467 for (t
= err
; _istspace (*t
); t
++)
471 /* Set up the initial conditions ... */
472 /* preserve STDIN, STDOUT and STDERR handles */
473 hOldConIn
= GetStdHandle (STD_INPUT_HANDLE
);
474 hOldConOut
= GetStdHandle (STD_OUTPUT_HANDLE
);
475 hOldConErr
= GetStdHandle (STD_ERROR_HANDLE
);
482 hFile
= CreateFile (in
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
,
483 FILE_ATTRIBUTE_NORMAL
, NULL
);
484 if (hFile
== INVALID_HANDLE_VALUE
)
486 ConErrPrintf ("Can't redirect input from file %s\n", in
);
490 if (!SetStdHandle (STD_INPUT_HANDLE
, hFile
))
492 ConErrPrintf ("Can't redirect input from file %s\n", in
);
496 DebugPrintf (_T("Input redirected from: %s\n"), in
);
500 /* Now do all but the last pipe command */
501 *szFileName
[0] = '\0';
502 hFile
[0] = INVALID_HANDLE_VALUE
;
506 /* Create unique temporary file name */
507 GetTempFileName (szTempPath
, "CMD", 0, szFileName
[1]);
509 /* Set current stdout to temporary file */
510 hFile
[1] = CreateFile (szFileName
[1], GENERIC_WRITE
, 0, NULL
,
511 TRUNCATE_EXISTING
, FILE_ATTRIBUTE_TEMPORARY
, NULL
);
512 SetStdHandle (STD_OUTPUT_HANDLE
, hFile
[1]);
516 /* close stdout file */
517 SetStdHandle (STD_OUTPUT_HANDLE
, hOldConOut
);
518 if ((hFile
[1] != INVALID_HANDLE_VALUE
) && (hFile
[1] != hOldConOut
))
520 CloseHandle (hFile
[1]);
521 hFile
[1] = INVALID_HANDLE_VALUE
;
524 /* close old stdin file */
525 SetStdHandle (STD_INPUT_HANDLE
, hOldConIn
);
526 if ((hFile
[0] != INVALID_HANDLE_VALUE
) && (hFile
[0] != hOldConIn
))
528 /* delete old stdin file, if it is a real file */
529 CloseHandle (hFile
[0]);
530 hFile
[0] = INVALID_HANDLE_VALUE
;
531 DeleteFile (szFileName
[0]);
532 *szFileName
[0] = _T('\0');
535 /* copy stdout file name to stdin file name */
536 _tcscpy (szFileName
[0], szFileName
[1]);
537 *szFileName
[1] = _T('\0');
539 /* open new stdin file */
540 hFile
[0] = CreateFile (szFileName
[0], GENERIC_READ
, 0, NULL
,
541 OPEN_EXISTING
, FILE_ATTRIBUTE_TEMPORARY
, NULL
);
542 SetStdHandle (STD_INPUT_HANDLE
, hFile
[0]);
544 s
= s
+ _tcslen (s
) + 1;
547 /* Now set up the end conditions... */
548 /* redirect STDOUT */
551 /* Final output to here */
554 hFile
= CreateFile (out
, GENERIC_WRITE
, 0, NULL
,
555 (nRedirFlags
& OUTPUT_APPEND
) ? OPEN_ALWAYS
: CREATE_ALWAYS
,
556 FILE_ATTRIBUTE_NORMAL
, NULL
);
557 if (hFile
== INVALID_HANDLE_VALUE
)
559 ConErrPrintf ("Can't redirect to file %s\n", out
);
563 if (!SetStdHandle (STD_OUTPUT_HANDLE
, hFile
))
565 ConErrPrintf ("Can't redirect to file %s\n", out
);
569 if (nRedirFlags
& OUTPUT_APPEND
)
573 if (GetFileType (hFile
) == FILE_TYPE_DISK
)
574 SetFilePointer (hFile
, 0, &lHighPos
, FILE_END
);
577 DebugPrintf (_T("Output redirected to: %s\n"), out
);
580 else if (hOldConOut
!= INVALID_HANDLE_VALUE
)
582 /* Restore original stdout */
583 HANDLE hOut
= GetStdHandle (STD_OUTPUT_HANDLE
);
584 SetStdHandle (STD_OUTPUT_HANDLE
, hOldConOut
);
585 if (hOldConOut
!= hOut
)
587 hOldConOut
= INVALID_HANDLE_VALUE
;
590 /* redirect STDERR */
593 /* Final output to here */
596 if (!_tcscmp (err
, out
))
599 DebugPrintf (_T("Stdout and stderr will use the same file!!\n"));
601 DuplicateHandle (GetCurrentProcess (),
602 GetStdHandle (STD_OUTPUT_HANDLE
),
603 GetCurrentProcess (),
604 &hFile
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
608 hFile
= CreateFile (err
,
612 (nRedirFlags
& ERROR_APPEND
) ? OPEN_ALWAYS
: CREATE_ALWAYS
,
613 FILE_ATTRIBUTE_NORMAL
,
615 if (hFile
== INVALID_HANDLE_VALUE
)
617 ConErrPrintf ("Can't redirect to file %s\n", err
);
621 if (!SetStdHandle (STD_ERROR_HANDLE
, hFile
))
623 ConErrPrintf ("Can't redirect to file %s\n", err
);
627 if (nRedirFlags
& ERROR_APPEND
)
631 if (GetFileType (hFile
) == FILE_TYPE_DISK
)
632 SetFilePointer (hFile
, 0, &lHighPos
, FILE_END
);
635 DebugPrintf (_T("Error redirected to: %s\n"), err
);
638 else if (hOldConErr
!= INVALID_HANDLE_VALUE
)
640 /* Restore original stderr */
641 HANDLE hErr
= GetStdHandle (STD_ERROR_HANDLE
);
642 SetStdHandle (STD_ERROR_HANDLE
, hOldConErr
);
643 if (hOldConErr
!= hErr
)
645 hOldConErr
= INVALID_HANDLE_VALUE
;
649 /* process final command */
652 #ifdef FEATURE_REDIRECTION
653 /* close old stdin file */
654 #if 0 /* buggy implementation */
655 SetStdHandle (STD_INPUT_HANDLE
, hOldConIn
);
656 if ((hFile
[0] != INVALID_HANDLE_VALUE
) &&
657 (hFile
[0] != hOldConIn
))
659 /* delete old stdin file, if it is a real file */
660 CloseHandle (hFile
[0]);
661 hFile
[0] = INVALID_HANDLE_VALUE
;
662 DeleteFile (szFileName
[0]);
663 *szFileName
[0] = _T('\0');
666 /* Restore original STDIN */
667 if (hOldConIn
!= INVALID_HANDLE_VALUE
)
669 HANDLE hIn
= GetStdHandle (STD_INPUT_HANDLE
);
670 SetStdHandle (STD_INPUT_HANDLE
, hOldConIn
);
671 if (hOldConIn
!= hIn
)
673 hOldConIn
= INVALID_HANDLE_VALUE
;
678 DebugPrintf (_T("Can't restore STDIN! Is invalid!!\n"), out
);
681 #endif /* buggy implementation */
684 if (hOldConIn
!= INVALID_HANDLE_VALUE
)
686 HANDLE hIn
= GetStdHandle (STD_INPUT_HANDLE
);
687 SetStdHandle (STD_INPUT_HANDLE
, hOldConIn
);
688 if (hIn
== INVALID_HANDLE_VALUE
)
691 DebugPrintf (_T("Previous STDIN is invalid!!\n"));
696 if (GetFileType (hIn
) == FILE_TYPE_DISK
)
700 CloseHandle (hFile
[0]);
701 hFile
[0] = INVALID_HANDLE_VALUE
;
702 DeleteFile (szFileName
[0]);
703 *szFileName
[0] = _T('\0');
708 DebugPrintf (_T("hFile[0] and hIn dont match!!!\n"));
716 /* Restore original STDOUT */
717 if (hOldConOut
!= INVALID_HANDLE_VALUE
)
719 HANDLE hOut
= GetStdHandle (STD_OUTPUT_HANDLE
);
720 SetStdHandle (STD_OUTPUT_HANDLE
, hOldConOut
);
721 if (hOldConOut
!= hOut
)
723 hOldConOut
= INVALID_HANDLE_VALUE
;
726 /* Restore original STDERR */
727 if (hOldConErr
!= INVALID_HANDLE_VALUE
)
729 HANDLE hErr
= GetStdHandle (STD_ERROR_HANDLE
);
730 SetStdHandle (STD_ERROR_HANDLE
, hOldConErr
);
731 if (hOldConErr
!= hErr
)
733 hOldConErr
= INVALID_HANDLE_VALUE
;
735 #endif /* FEATURE_REDIRECTION */
740 * do the prompt/input/process loop
745 ProcessInput (BOOL bFlag
)
747 TCHAR commandline
[CMDLINE_LENGTH
];
748 TCHAR readline
[CMDLINE_LENGTH
];
756 /* if no batch input then... */
757 if (!(ip
= ReadBatchLine (&bEchoThisLine
)))
762 ReadCommand (readline
, CMDLINE_LENGTH
);
764 bEchoThisLine
= FALSE
;
788 if ((tp
= FindArg (*ip
- _T('0'))))
790 cp
= stpcpy (cp
, tp
);
798 cp
+= _stprintf (cp
, _T("%u"), nErrorLevel
);
803 if ((tp
= _tcschr (ip
, _T('%'))) && (tp
<=(unsigned int)strchr(ip
,_T(' '))-1))
808 /* FIXME: This is just a quick hack!! */
809 /* Do a proper memory allocation!! */
810 if (GetEnvironmentVariable (ip
, evar
, 512))
811 cp
= stpcpy (cp
, evar
);
831 /* strip trailing spaces */
832 while ((--cp
>= commandline
) && _istspace (*cp
));
834 *(cp
+ 1) = _T('\0');
837 /* Echo batch file line */
841 ConOutPuts (commandline
);
846 ParseCommandLine (commandline
);
847 if (bEcho
&& !bIgnoreEcho
)
852 while (!bCanExit
|| !bExit
);
859 * control-break handler.
861 BOOL
BreakHandler (DWORD dwCtrlType
)
863 if ((dwCtrlType
!= CTRL_C_EVENT
) &&
864 (dwCtrlType
!= CTRL_BREAK_EVENT
))
867 if (bChildProcessRunning
== TRUE
)
869 GenerateConsoleCtrlEvent (CTRL_C_EVENT
,
874 /* FIXME: Handle batch files */
876 /* FIXME: Print "^C" */
883 VOID
AddBreakHandler (VOID
)
886 SetConsoleCtrlHandler ((PHANDLER_ROUTINE
)&BreakHandler
,
892 VOID
RemoveBreakHandler (VOID
)
895 SetConsoleCtrlHandler (NULL
, FALSE
);
901 * show commands and options that are available.
907 /* print command list */
908 ConOutPrintf (_T("\nInternal commands available:\n"));
911 /* print feature list */
912 ConOutPuts ("\nFeatures available:");
913 #ifdef FEATURE_ALIASES
914 ConOutPuts (" [aliases]");
916 #ifdef FEATURE_HISTORY
917 ConOutPuts (" [history]");
919 #ifdef FEATURE_UNIX_FILENAME_COMPLETION
920 ConOutPuts (" [unix filename completion]");
922 #ifdef FEATURE_DIRECTORY_STACK
923 ConOutPuts (" [directory stack]");
925 #ifdef FEATURE_REDIRECTION
926 ConOutPuts (" [redirections and piping]");
933 * set up global initializations and process parameters
935 * argc - number of parameters to command.com
936 * argv - command-line parameters
940 Initialize (int argc
, char *argv
[])
942 TCHAR commandline
[CMDLINE_LENGTH
];
948 DebugPrintf ("[command args:\n");
949 for (x
= 0; x
< argc
; x
++)
951 DebugPrintf ("%d. %s\n", x
, argv
[x
]);
956 /* get version information */
957 osvi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
958 GetVersionEx (&osvi
);
962 /* get default input and output console handles */
963 hOut
= GetStdHandle (STD_OUTPUT_HANDLE
);
964 hIn
= GetStdHandle (STD_INPUT_HANDLE
);
966 SetConsoleMode (hIn
, ENABLE_PROCESSED_INPUT
);
968 if (argc
>= 2 && !_tcsncmp (argv
[1], _T("/?"), 2))
970 ConOutPuts (_T("Starts a new instance of the ReactOS command line interpreter.\n"
972 "CMD [/[C|K] command][/P][/Q][/T:bf]\n"
974 " /C command Runs the specified command and terminates.\n"
975 " /K command Runs the specified command and remains.\n"
976 " /P CMD becomes permanent and runs autoexec.bat\n"
977 " (cannot be terminated).\n"
978 " /T:bf Sets the background/foreground color (see COLOR command)."));
982 #ifdef INCLUDE_CMD_CHDIR
986 #ifdef FATURE_ALIASES
992 for (i
= 1; i
< argc
; i
++)
994 if (!_tcsicmp (argv
[i
], _T("/p")))
996 if (!IsValidFileName (_T("\\autoexec.bat")))
998 #ifdef INCLUDE_CMD_DATE
1001 #ifdef INCLUDE_CMD_TIME
1007 ParseCommandLine (_T("\\autoexec.bat"));
1011 else if (!_tcsicmp (argv
[i
], _T("/c")))
1013 /* This just runs a program and exits */
1017 _tcscpy (commandline
, argv
[i
]);
1020 _tcscat (commandline
, " ");
1021 _tcscat (commandline
, argv
[i
]);
1024 ParseCommandLine(commandline
);
1025 ExitProcess (ProcessInput (TRUE
));
1032 else if (!_tcsicmp (argv
[i
], _T("/k")))
1034 /* This just runs a program and remains */
1038 _tcscpy (commandline
, argv
[i
]);
1041 _tcscat (commandline
, " ");
1042 _tcscat (commandline
, argv
[i
]);
1045 ParseCommandLine(commandline
);
1048 #ifdef INCLUDE_CMD_COLOR
1049 else if (!_tcsnicmp (argv
[i
], _T("/t:"), 3))
1051 /* process /t (color) argument */
1052 wDefColor
= (WORD
)strtoul (&argv
[i
][3], NULL
, 16);
1054 SetScreenColor (wColor
, TRUE
);
1063 /* run cmdstart.bat */
1064 if (IsValidFileName (_T("cmdstart.bat")))
1066 ParseCommandLine (_T("cmdstart.bat"));
1068 else if (IsValidFileName (_T("\\cmdstart.bat")))
1070 ParseCommandLine (_T("\\cmdstart.bat"));
1075 /* try to run cmdstart.bat from install dir */
1078 _tcscpy (commandline
, argv
[0]);
1079 p
= _tcsrchr (commandline
, _T('\\')) + 1;
1080 _tcscpy (p
, _T("cmdstart.bat"));
1082 if (IsValidFileName (_T("commandline")))
1084 ConErrPrintf ("Running %s...\n", commandline
);
1085 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"));
1128 /* try to run cmdexit.bat from install dir */
1129 TCHAR commandline
[CMDLINE_LENGTH
];
1132 _tcscpy (commandline
, argv
[0]);
1133 p
= _tcsrchr (commandline
, _T('\\')) + 1;
1134 _tcscpy (p
, _T("cmdexit.bat"));
1136 if (IsValidFileName (_T("commandline")))
1138 ConErrPrintf ("Running %s...\n", commandline
);
1139 ParseCommandLine (commandline
);
1144 #ifdef FEATURE_ALIASES
1148 #ifdef FEATURE_DIECTORY_STACK
1149 /* destroy directory stack */
1150 DestroyDirectoryStack ();
1153 #ifdef INCLUDE_CMD_CHDIR
1157 #ifdef FEATURE_HISTORY
1162 /* remove ctrl break handler */
1163 RemoveBreakHandler ();
1170 int main (int argc
, char *argv
[])
1173 CONSOLE_SCREEN_BUFFER_INFO Info
;
1176 SetFileApisToOEM ();
1178 if( GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE
), &Info
) == FALSE
)
1179 printf( "GetConsoleScreenBufferInfo: Error: %ld\n", GetLastError() );
1180 wColor
= Info
.wAttributes
;
1182 /* check switches on command-line */
1183 Initialize (argc
, argv
);
1185 /* call prompt routine */
1186 nExitCode
= ProcessInput (FALSE
);
1188 /* do the cleanup */
1189 Cleanup (argc
, argv
);