1 /* $Id: cmd.c,v 1.1 2003/03/20 19:19:22 rcampbell 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(":"))))
189 if (!SetCurrentDirectory(first
))
190 /* Guess they changed disc or something, handle that gracefully and get to root */
197 working
= SetCurrentDirectory(str
);
200 if (!working
) ConErrPuts (INVALIDDRIVE
);
205 /* get the PATH environment variable and parse it */
206 /* search the PATH environment variable for the binary */
207 if (!SearchForExecutable (first
, szFullName
))
209 error_bad_command ();
214 GetConsoleTitle (szWindowTitle
, MAX_PATH
);
217 /* check if this is a .BAT or .CMD file */
218 if (!_tcsicmp (_tcsrchr (szFullName
, _T('.')), _T(".bat")) ||
219 !_tcsicmp (_tcsrchr (szFullName
, _T('.')), _T(".cmd")))
222 DebugPrintf ("[BATCH: %s %s]\n", szFullName
, rest
);
224 Batch (szFullName
, first
, rest
);
228 /* exec the program */
229 TCHAR szFullCmdLine
[CMDLINE_LENGTH
];
230 PROCESS_INFORMATION prci
;
234 DebugPrintf ("[EXEC: %s %s]\n", szFullName
, rest
);
236 /* build command line for CreateProcess() */
237 _tcscpy (szFullCmdLine
, first
);
238 _tcscat (szFullCmdLine
, _T(" "));
239 _tcscat (szFullCmdLine
, rest
);
241 /* fill startup info */
242 memset (&stui
, 0, sizeof (STARTUPINFO
));
243 stui
.cb
= sizeof (STARTUPINFO
);
244 stui
.dwFlags
= STARTF_USESHOWWINDOW
;
245 stui
.wShowWindow
= SW_SHOWDEFAULT
;
247 // return console to standard mode
248 SetConsoleMode( GetStdHandle( STD_INPUT_HANDLE
),
249 ENABLE_LINE_INPUT
| ENABLE_PROCESSED_INPUT
| ENABLE_ECHO_INPUT
);
251 if (CreateProcess (szFullName
,
256 CREATE_NEW_PROCESS_GROUP
,
262 /* FIXME: Protect this with critical section */
263 bChildProcessRunning
= TRUE
;
264 dwChildProcessId
= prci
.dwProcessId
;
266 WaitForSingleObject (prci
.hProcess
, INFINITE
);
268 /* FIXME: Protect this with critical section */
269 bChildProcessRunning
= TRUE
;
271 GetExitCodeProcess (prci
.hProcess
, &dwExitCode
);
272 nErrorLevel
= (INT
)dwExitCode
;
273 CloseHandle (prci
.hThread
);
274 CloseHandle (prci
.hProcess
);
278 ErrorMessage (GetLastError (),
279 "Error executing CreateProcess()!!\n");
281 // restore console mode
282 SetConsoleMode( GetStdHandle( STD_INPUT_HANDLE
),
283 ENABLE_PROCESSED_INPUT
);
287 SetConsoleTitle (szWindowTitle
);
293 * look through the internal commands and determine whether or not this
294 * command is one of them. If it is, call the command. If not, call
295 * execute to run it as an external program.
297 * line - the command line of the program to run
302 DoCommand (LPTSTR line
)
304 TCHAR com
[MAX_PATH
]; /* the first word in the command */
307 LPTSTR rest
= line
; /* pointer to the rest of the command line */
312 DebugPrintf ("DoCommand: (\'%s\')\n", line
);
315 /* Skip over initial white space */
316 while (isspace (*rest
))
321 /* Anything to do ? */
324 /* Copy over 1st word as lower case */
325 while (!IsDelimiter (*rest
))
326 *cp
++ = _totlower (*rest
++);
328 /* Terminate first word */
331 /* Skip over whitespace to rest of line */
332 while (_istspace (*rest
))
335 /* Scan internal command table */
336 for (cmdptr
= cmds
;; cmdptr
++)
338 /* If end of table execute ext cmd */
339 if (cmdptr
->name
== NULL
)
345 if (!_tcscmp (com
, cmdptr
->name
))
347 cmdptr
->func (com
, rest
);
351 /* The following code handles the case of commands like CD which
352 * are recognised even when the command name and parameter are
353 * not space separated.
359 /* Get length of command name */
360 cl
= _tcslen (cmdptr
->name
);
362 if ((cmdptr
->flags
& CMD_SPECIAL
) &&
363 (!_tcsncmp (cmdptr
->name
, com
, cl
)) &&
364 (_tcschr (_T("\\.-"), *(com
+ cl
))))
366 /* OK its one of the specials...*/
368 /* Terminate first word properly */
371 /* Call with new rest */
372 cmdptr
->func (com
, cstart
+ cl
);
381 * process the command line and execute the appropriate functions
382 * full input/output redirection and piping are supported
385 VOID
ParseCommandLine (LPTSTR cmd
)
387 TCHAR cmdline
[CMDLINE_LENGTH
];
389 #ifdef FEATURE_REDIRECTION
390 TCHAR in
[CMDLINE_LENGTH
] = "";
391 TCHAR out
[CMDLINE_LENGTH
] = "";
392 TCHAR err
[CMDLINE_LENGTH
] = "";
393 TCHAR szTempPath
[MAX_PATH
] = _T(".\\");
394 TCHAR szFileName
[2][MAX_PATH
] = {"", ""};
395 HANDLE hFile
[2] = {INVALID_HANDLE_VALUE
, INVALID_HANDLE_VALUE
};
403 #endif /* FEATURE_REDIRECTION */
405 _tcscpy (cmdline
, cmd
);
409 DebugPrintf ("ParseCommandLine: (\'%s\')\n", s
);
412 #ifdef FEATURE_ALIASES
413 /* expand all aliases */
414 ExpandAlias (s
, CMDLINE_LENGTH
);
415 #endif /* FEATURE_ALIAS */
417 #ifdef FEATURE_REDIRECTION
418 /* find the temp path to store temporary files */
419 GetTempPath (MAX_PATH
, szTempPath
);
420 if (szTempPath
[_tcslen (szTempPath
) - 1] != _T('\\'))
421 _tcscat (szTempPath
, _T("\\"));
423 /* get the redirections from the command line */
424 num
= GetRedirection (s
, in
, out
, err
, &nRedirFlags
);
426 /* more efficient, but do we really need to do this? */
427 for (t
= in
; _istspace (*t
); t
++)
431 for (t
= out
; _istspace (*t
); t
++)
435 for (t
= err
; _istspace (*t
); t
++)
439 /* Set up the initial conditions ... */
440 /* preserve STDIN, STDOUT and STDERR handles */
441 hOldConIn
= GetStdHandle (STD_INPUT_HANDLE
);
442 hOldConOut
= GetStdHandle (STD_OUTPUT_HANDLE
);
443 hOldConErr
= GetStdHandle (STD_ERROR_HANDLE
);
449 SECURITY_ATTRIBUTES sa
= {sizeof(SECURITY_ATTRIBUTES
), NULL
, TRUE
};
451 hFile
= CreateFile (in
, GENERIC_READ
, FILE_SHARE_READ
, &sa
, OPEN_EXISTING
,
452 FILE_ATTRIBUTE_NORMAL
, NULL
);
453 if (hFile
== INVALID_HANDLE_VALUE
)
455 ConErrPrintf ("Can't redirect input from file %s\n", in
);
459 if (!SetStdHandle (STD_INPUT_HANDLE
, hFile
))
461 ConErrPrintf ("Can't redirect input from file %s\n", in
);
465 DebugPrintf (_T("Input redirected from: %s\n"), in
);
469 /* Now do all but the last pipe command */
470 *szFileName
[0] = '\0';
471 hFile
[0] = INVALID_HANDLE_VALUE
;
475 SECURITY_ATTRIBUTES sa
= {sizeof(SECURITY_ATTRIBUTES
), NULL
, TRUE
};
477 /* Create unique temporary file name */
478 GetTempFileName (szTempPath
, "CMD", 0, szFileName
[1]);
480 /* Set current stdout to temporary file */
481 hFile
[1] = CreateFile (szFileName
[1], GENERIC_WRITE
, 0, &sa
,
482 TRUNCATE_EXISTING
, FILE_ATTRIBUTE_TEMPORARY
, NULL
);
483 SetStdHandle (STD_OUTPUT_HANDLE
, hFile
[1]);
487 /* close stdout file */
488 SetStdHandle (STD_OUTPUT_HANDLE
, hOldConOut
);
489 if ((hFile
[1] != INVALID_HANDLE_VALUE
) && (hFile
[1] != hOldConOut
))
491 CloseHandle (hFile
[1]);
492 hFile
[1] = INVALID_HANDLE_VALUE
;
495 /* close old stdin file */
496 SetStdHandle (STD_INPUT_HANDLE
, hOldConIn
);
497 if ((hFile
[0] != INVALID_HANDLE_VALUE
) && (hFile
[0] != hOldConIn
))
499 /* delete old stdin file, if it is a real file */
500 CloseHandle (hFile
[0]);
501 hFile
[0] = INVALID_HANDLE_VALUE
;
502 DeleteFile (szFileName
[0]);
503 *szFileName
[0] = _T('\0');
506 /* copy stdout file name to stdin file name */
507 _tcscpy (szFileName
[0], szFileName
[1]);
508 *szFileName
[1] = _T('\0');
510 /* open new stdin file */
511 hFile
[0] = CreateFile (szFileName
[0], GENERIC_READ
, 0, &sa
,
512 OPEN_EXISTING
, FILE_ATTRIBUTE_TEMPORARY
, NULL
);
513 SetStdHandle (STD_INPUT_HANDLE
, hFile
[0]);
515 s
= s
+ _tcslen (s
) + 1;
518 /* Now set up the end conditions... */
519 /* redirect STDOUT */
522 /* Final output to here */
524 SECURITY_ATTRIBUTES sa
= {sizeof(SECURITY_ATTRIBUTES
), NULL
, TRUE
};
526 hFile
= CreateFile (out
, GENERIC_WRITE
, FILE_SHARE_READ
, &sa
,
527 (nRedirFlags
& OUTPUT_APPEND
) ? OPEN_ALWAYS
: CREATE_ALWAYS
,
528 FILE_ATTRIBUTE_NORMAL
, NULL
);
529 if (hFile
== INVALID_HANDLE_VALUE
)
531 ConErrPrintf ("Can't redirect to file %s\n", out
);
535 if (!SetStdHandle (STD_OUTPUT_HANDLE
, hFile
))
537 ConErrPrintf ("Can't redirect to file %s\n", out
);
541 if (nRedirFlags
& OUTPUT_APPEND
)
545 if (GetFileType (hFile
) == FILE_TYPE_DISK
)
546 SetFilePointer (hFile
, 0, &lHighPos
, FILE_END
);
549 DebugPrintf (_T("Output redirected to: %s\n"), out
);
552 else if (hOldConOut
!= INVALID_HANDLE_VALUE
)
554 /* Restore original stdout */
555 HANDLE hOut
= GetStdHandle (STD_OUTPUT_HANDLE
);
556 SetStdHandle (STD_OUTPUT_HANDLE
, hOldConOut
);
557 if (hOldConOut
!= hOut
)
559 hOldConOut
= INVALID_HANDLE_VALUE
;
562 /* redirect STDERR */
565 /* Final output to here */
567 SECURITY_ATTRIBUTES sa
= {sizeof(SECURITY_ATTRIBUTES
), NULL
, TRUE
};
569 if (!_tcscmp (err
, out
))
572 DebugPrintf (_T("Stdout and stderr will use the same file!!\n"));
574 DuplicateHandle (GetCurrentProcess (),
575 GetStdHandle (STD_OUTPUT_HANDLE
),
576 GetCurrentProcess (),
577 &hFile
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
581 hFile
= CreateFile (err
,
585 (nRedirFlags
& ERROR_APPEND
) ? OPEN_ALWAYS
: CREATE_ALWAYS
,
586 FILE_ATTRIBUTE_NORMAL
,
588 if (hFile
== INVALID_HANDLE_VALUE
)
590 ConErrPrintf ("Can't redirect to file %s\n", err
);
594 if (!SetStdHandle (STD_ERROR_HANDLE
, hFile
))
596 ConErrPrintf ("Can't redirect to file %s\n", err
);
600 if (nRedirFlags
& ERROR_APPEND
)
604 if (GetFileType (hFile
) == FILE_TYPE_DISK
)
605 SetFilePointer (hFile
, 0, &lHighPos
, FILE_END
);
608 DebugPrintf (_T("Error redirected to: %s\n"), err
);
611 else if (hOldConErr
!= INVALID_HANDLE_VALUE
)
613 /* Restore original stderr */
614 HANDLE hErr
= GetStdHandle (STD_ERROR_HANDLE
);
615 SetStdHandle (STD_ERROR_HANDLE
, hOldConErr
);
616 if (hOldConErr
!= hErr
)
618 hOldConErr
= INVALID_HANDLE_VALUE
;
622 /* process final command */
625 #ifdef FEATURE_REDIRECTION
626 /* close old stdin file */
627 #if 0 /* buggy implementation */
628 SetStdHandle (STD_INPUT_HANDLE
, hOldConIn
);
629 if ((hFile
[0] != INVALID_HANDLE_VALUE
) &&
630 (hFile
[0] != hOldConIn
))
632 /* delete old stdin file, if it is a real file */
633 CloseHandle (hFile
[0]);
634 hFile
[0] = INVALID_HANDLE_VALUE
;
635 DeleteFile (szFileName
[0]);
636 *szFileName
[0] = _T('\0');
639 /* Restore original STDIN */
640 if (hOldConIn
!= INVALID_HANDLE_VALUE
)
642 HANDLE hIn
= GetStdHandle (STD_INPUT_HANDLE
);
643 SetStdHandle (STD_INPUT_HANDLE
, hOldConIn
);
644 if (hOldConIn
!= hIn
)
646 hOldConIn
= INVALID_HANDLE_VALUE
;
651 DebugPrintf (_T("Can't restore STDIN! Is invalid!!\n"), out
);
654 #endif /* buggy implementation */
657 if (hOldConIn
!= INVALID_HANDLE_VALUE
)
659 HANDLE hIn
= GetStdHandle (STD_INPUT_HANDLE
);
660 SetStdHandle (STD_INPUT_HANDLE
, hOldConIn
);
661 if (hIn
== INVALID_HANDLE_VALUE
)
664 DebugPrintf (_T("Previous STDIN is invalid!!\n"));
669 if (GetFileType (hIn
) == FILE_TYPE_DISK
)
673 CloseHandle (hFile
[0]);
674 hFile
[0] = INVALID_HANDLE_VALUE
;
675 DeleteFile (szFileName
[0]);
676 *szFileName
[0] = _T('\0');
681 DebugPrintf (_T("hFile[0] and hIn dont match!!!\n"));
689 /* Restore original STDOUT */
690 if (hOldConOut
!= INVALID_HANDLE_VALUE
)
692 HANDLE hOut
= GetStdHandle (STD_OUTPUT_HANDLE
);
693 SetStdHandle (STD_OUTPUT_HANDLE
, hOldConOut
);
694 if (hOldConOut
!= hOut
)
696 hOldConOut
= INVALID_HANDLE_VALUE
;
699 /* Restore original STDERR */
700 if (hOldConErr
!= INVALID_HANDLE_VALUE
)
702 HANDLE hErr
= GetStdHandle (STD_ERROR_HANDLE
);
703 SetStdHandle (STD_ERROR_HANDLE
, hOldConErr
);
704 if (hOldConErr
!= hErr
)
706 hOldConErr
= INVALID_HANDLE_VALUE
;
708 #endif /* FEATURE_REDIRECTION */
713 * do the prompt/input/process loop
718 ProcessInput (BOOL bFlag
)
720 TCHAR commandline
[CMDLINE_LENGTH
];
721 TCHAR readline
[CMDLINE_LENGTH
];
729 /* if no batch input then... */
730 if (!(ip
= ReadBatchLine (&bEchoThisLine
)))
735 ReadCommand (readline
, CMDLINE_LENGTH
);
737 bEchoThisLine
= FALSE
;
761 if ((tp
= FindArg (*ip
- _T('0'))))
763 cp
= stpcpy (cp
, tp
);
771 cp
+= _stprintf (cp
, _T("%u"), nErrorLevel
);
776 tp
= _tcschr(ip
, _T('%'));
778 (tp
<= _tcschr(ip
, _T(' ')) - 1))
783 /* FIXME: This is just a quick hack!! */
784 /* Do a proper memory allocation!! */
785 if (GetEnvironmentVariable (ip
, evar
, 512))
786 cp
= stpcpy (cp
, evar
);
806 /* strip trailing spaces */
807 while ((--cp
>= commandline
) && _istspace (*cp
));
809 *(cp
+ 1) = _T('\0');
812 /* Echo batch file line */
816 ConOutPuts (commandline
);
821 ParseCommandLine (commandline
);
822 if (bEcho
&& !bIgnoreEcho
)
827 while (!bCanExit
|| !bExit
);
834 * control-break handler.
836 BOOL
BreakHandler (DWORD dwCtrlType
)
838 if ((dwCtrlType
!= CTRL_C_EVENT
) &&
839 (dwCtrlType
!= CTRL_BREAK_EVENT
))
842 if (bChildProcessRunning
== TRUE
)
844 GenerateConsoleCtrlEvent (CTRL_C_EVENT
,
849 /* FIXME: Handle batch files */
851 /* FIXME: Print "^C" */
858 VOID
AddBreakHandler (VOID
)
861 SetConsoleCtrlHandler ((PHANDLER_ROUTINE
)&BreakHandler
,
867 VOID
RemoveBreakHandler (VOID
)
870 SetConsoleCtrlHandler (NULL
, FALSE
);
876 * show commands and options that are available.
882 /* print command list */
883 ConOutPrintf (_T("\nInternal commands available:\n"));
886 /* print feature list */
887 ConOutPuts ("\nFeatures available:");
888 #ifdef FEATURE_ALIASES
889 ConOutPuts (" [aliases]");
891 #ifdef FEATURE_HISTORY
892 ConOutPuts (" [history]");
894 #ifdef FEATURE_UNIX_FILENAME_COMPLETION
895 ConOutPuts (" [unix filename completion]");
897 #ifdef FEATURE_DIRECTORY_STACK
898 ConOutPuts (" [directory stack]");
900 #ifdef FEATURE_REDIRECTION
901 ConOutPuts (" [redirections and piping]");
908 * set up global initializations and process parameters
910 * argc - number of parameters to command.com
911 * argv - command-line parameters
915 Initialize (int argc
, char *argv
[])
917 TCHAR commandline
[CMDLINE_LENGTH
];
918 TCHAR ModuleName
[_MAX_PATH
+ 1];
924 DebugPrintf ("[command args:\n");
925 for (x
= 0; x
< argc
; x
++)
927 DebugPrintf ("%d. %s\n", x
, argv
[x
]);
932 /* get version information */
933 osvi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
934 GetVersionEx (&osvi
);
938 /* get default input and output console handles */
939 hOut
= GetStdHandle (STD_OUTPUT_HANDLE
);
940 hIn
= GetStdHandle (STD_INPUT_HANDLE
);
943 if (argc
>= 2 && !_tcsncmp (argv
[1], _T("/?"), 2))
945 ConOutPuts (_T("Starts a new instance of the ReactOS command line interpreter.\n"
947 "CMD [/[C|K] command][/P][/Q][/T:bf]\n"
949 " /C command Runs the specified command and terminates.\n"
950 " /K command Runs the specified command and remains.\n"
951 " /P CMD becomes permanent and runs autoexec.bat\n"
952 " (cannot be terminated).\n"
953 " /T:bf Sets the background/foreground color (see COLOR command)."));
956 SetConsoleMode (hIn
, ENABLE_PROCESSED_INPUT
);
958 #ifdef INCLUDE_CMD_CHDIR
962 #ifdef FATURE_ALIASES
968 for (i
= 1; i
< argc
; i
++)
970 if (!_tcsicmp (argv
[i
], _T("/p")))
972 if (!IsValidFileName (_T("\\autoexec.bat")))
974 #ifdef INCLUDE_CMD_DATE
977 #ifdef INCLUDE_CMD_TIME
983 ParseCommandLine (_T("\\autoexec.bat"));
987 else if (!_tcsicmp (argv
[i
], _T("/c")))
989 /* This just runs a program and exits */
993 _tcscpy (commandline
, argv
[i
]);
996 _tcscat (commandline
, " ");
997 _tcscat (commandline
, argv
[i
]);
1000 ParseCommandLine(commandline
);
1001 ExitProcess (ProcessInput (TRUE
));
1008 else if (!_tcsicmp (argv
[i
], _T("/k")))
1010 /* This just runs a program and remains */
1014 _tcscpy (commandline
, argv
[i
]);
1017 _tcscat (commandline
, " ");
1018 _tcscat (commandline
, argv
[i
]);
1021 ParseCommandLine(commandline
);
1024 #ifdef INCLUDE_CMD_COLOR
1025 else if (!_tcsnicmp (argv
[i
], _T("/t:"), 3))
1027 /* process /t (color) argument */
1028 wDefColor
= (WORD
)strtoul (&argv
[i
][3], NULL
, 16);
1030 SetScreenColor (wColor
, TRUE
);
1036 /* run cmdstart.bat */
1037 if (IsValidFileName (_T("cmdstart.bat")))
1039 ParseCommandLine (_T("cmdstart.bat"));
1041 else if (IsValidFileName (_T("\\cmdstart.bat")))
1043 ParseCommandLine (_T("\\cmdstart.bat"));
1048 /* try to run cmdstart.bat from install dir */
1051 _tcscpy (commandline
, argv
[0]);
1052 p
= _tcsrchr (commandline
, _T('\\')) + 1;
1053 _tcscpy (p
, _T("cmdstart.bat"));
1055 if (IsValidFileName (_T("commandline")))
1057 ConErrPrintf ("Running %s...\n", commandline
);
1058 ParseCommandLine (commandline
);
1063 #ifdef FEATURE_DIR_STACK
1064 /* initialize directory stack */
1065 InitDirectoryStack ();
1069 #ifdef FEATURE_HISTORY
1070 /*initialize history*/
1074 /* Set COMSPEC environment variable */
1075 if (0 != GetModuleFileName (NULL
, ModuleName
, _MAX_PATH
+ 1))
1077 ModuleName
[_MAX_PATH
] = _T('\0');
1078 SetEnvironmentVariable (_T("COMSPEC"), ModuleName
);
1081 /* add ctrl break handler */
1086 static VOID
Cleanup (int argc
, char *argv
[])
1088 /* run cmdexit.bat */
1089 if (IsValidFileName (_T("cmdexit.bat")))
1091 ConErrPrintf ("Running cmdexit.bat...\n");
1092 ParseCommandLine (_T("cmdexit.bat"));
1094 else if (IsValidFileName (_T("\\cmdexit.bat")))
1096 ConErrPrintf ("Running \\cmdexit.bat...\n");
1097 ParseCommandLine (_T("\\cmdexit.bat"));
1102 /* try to run cmdexit.bat from install dir */
1103 TCHAR commandline
[CMDLINE_LENGTH
];
1106 _tcscpy (commandline
, argv
[0]);
1107 p
= _tcsrchr (commandline
, _T('\\')) + 1;
1108 _tcscpy (p
, _T("cmdexit.bat"));
1110 if (IsValidFileName (_T("commandline")))
1112 ConErrPrintf ("Running %s...\n", commandline
);
1113 ParseCommandLine (commandline
);
1118 #ifdef FEATURE_ALIASES
1122 #ifdef FEATURE_DIECTORY_STACK
1123 /* destroy directory stack */
1124 DestroyDirectoryStack ();
1127 #ifdef INCLUDE_CMD_CHDIR
1131 #ifdef FEATURE_HISTORY
1136 /* remove ctrl break handler */
1137 RemoveBreakHandler ();
1138 SetConsoleMode( GetStdHandle( STD_INPUT_HANDLE
),
1139 ENABLE_LINE_INPUT
| ENABLE_PROCESSED_INPUT
| ENABLE_ECHO_INPUT
);
1146 int main (int argc
, char *argv
[])
1148 CONSOLE_SCREEN_BUFFER_INFO Info
;
1154 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE
), &Info
) == FALSE
)
1156 fprintf(stderr
, "GetConsoleScreenBufferInfo: Error: %ld\n", GetLastError());
1158 /* On ReactOS GetConsoleScreenBufferInfo returns an error if the stdin
1159 handle is redirected to a pipe or file. This stops windres from working. */
1163 wColor
= Info
.wAttributes
;
1166 /* check switches on command-line */
1167 Initialize(argc
, argv
);
1169 /* call prompt routine */
1170 nExitCode
= ProcessInput(FALSE
);
1172 /* do the cleanup */
1173 Cleanup(argc
, argv
);