📄 cmd.c
字号:
{#ifdef _DEBUG DebugPrintf (_T("Stdout and stderr will use the same file!!\n"));#endif DebugPrintf(_T("Redirecting stdout and stderr to the same file is not supported\n")); _wfreopen(out, L"wb",stdout); } else { if (nRedirFlags & ERROR_APPEND) file = _wfreopen(err, L"ab", stderr); else file = _wfreopen(err, L"wb", stderr); if (!file) { ConErrPrintf (_T("Can't redirect stderr to file %s\n"), err); return; } }#ifdef _DEBUG DebugPrintf (_T("Error redirected to: %s\n"), err);#endif } else if (*szOldConErr) { /* Restore original stderr */ WCHAR szErr[MAX_PATH]; GetStdioPathW(2, szErr, &dwSize); _wfreopen(szOldConErr, L"wb", stderr); *szOldConErr = 0; }#endif /* process final command */ DoCommand (s);#ifdef FEATURE_REDIRECTION /* close old stdin file */ if (*szOldConIn) { WCHAR szIn[MAX_PATH]; GetStdioPathW (0, szIn, &dwSize); _wfreopen ( szOldConIn, L"rb", stdin); if (*szIn) { DeleteFile (szIn); } } /* Restore original STDOUT */ if (*szOldConOut) { WCHAR szOut[MAX_PATH]; GetStdioPathW (1, szOut, &dwSize); _wfreopen(szOldConOut, L"wb", stdout); *szOldConOut = 0; } /* Restore original STDERR */ if (*szOldConErr) { WCHAR szErr[MAX_PATH]; GetStdioPathW(2, szErr, &dwSize); _wfreopen(szOldConErr, L"wb", stderr); *szOldConErr = 0; }#endif /* FEATURE_REDIRECTION */}/* * do the prompt/input/process loop * */static INT ProcessInput (BOOL bFlag){ TCHAR commandline[CMDLINE_LENGTH]; TCHAR readline[CMDLINE_LENGTH]; LPTSTR tp = NULL; LPTSTR ip; LPTSTR cp; BOOL bEchoThisLine; do { /* if no batch input then... */ if (!(ip = ReadBatchLine (&bEchoThisLine))) { if (bFlag) return 0; if(!ReadCommand (readline, CMDLINE_LENGTH)) { ConOutPuts(_T("\n")); continue; } ip = readline; bEchoThisLine = FALSE; } cp = commandline; while (*ip) { if (*ip == _T('%')) { switch (*++ip) { case _T('%'): *cp++ = *ip++; break; case _T('0'): case _T('1'): case _T('2'): case _T('3'): case _T('4'): case _T('5'): case _T('6'): case _T('7'): case _T('8'): case _T('9'): if ((tp = FindArg (*ip - _T('0')))) { cp = stpcpy (cp, tp); ip++; } else *cp++ = _T('%'); break; case _T('?'): cp += _stprintf (cp, _T("%u"), nErrorLevel); ip++; break; default: if ((tp == _tcschr(ip, _T('%'))) && (tp <= _tcschr(ip, _T(' '))-1)) { TCHAR evar[512]; *tp = _T('\0'); /* FIXME: This is just a quick hack!! */ /* Do a proper memory allocation!! */ if (GetEnvironmentVariable (ip, evar, 512)) cp = stpcpy (cp, evar); ip = tp + 1; } else { *cp++ = _T('%'); } break; } continue; } if (_istcntrl (*ip)) *ip = _T(' '); *cp++ = *ip++; } *cp = _T('\0'); /* strip trailing spaces */ while ((--cp >= commandline) && _istspace (*cp)); *(cp + 1) = _T('\0'); /* JPP 19980807 */ /* Echo batch file line */ if (bEchoThisLine) { PrintPrompt (); ConOutPuts (commandline); } if (*commandline) { ParseCommandLine (commandline); if (bEcho && !bIgnoreEcho) ConOutChar ('\n'); bIgnoreEcho = FALSE; } } while (!bCanExit || !bExit); return 0;}/* * control-break handler. */BOOL BreakHandler (DWORD dwCtrlType){ if ((dwCtrlType == CTRL_C_EVENT)) { if(bChildProcessRunning) { TerminateProcess((HANDLE)dwChildProcessId, 0); } } /* FIXME: Handle batch files */ //ConOutPuts(_T("^C")); return TRUE;}VOID AddBreakHandler (VOID){ SetConsoleCtrlHandler ((PHANDLER_ROUTINE)&BreakHandler, TRUE);}VOID RemoveBreakHandler (VOID){ SetConsoleCtrlHandler (NULL, FALSE);}/* * show commands and options that are available. * */static VOIDShowCommands (VOID){ /* print command list */ ConOutPrintf (_T("\nInternal commands available:\n")); PrintCommandList (); /* print feature list */ ConOutPuts (_T("\nFeatures available:"));#ifdef FEATURE_ALIASES ConOutPuts (_T(" [aliases]"));#endif#ifdef FEATURE_UNIX_FILENAME_COMPLETION ConOutPuts (_T(" [unix filename completion]"));#endif#ifdef FEATURE_DIRECTORY_STACK ConOutPuts (_T(" [directory stack]"));#endif#ifdef FEATURE_REDIRECTION ConOutPuts (_T(" [redirections and piping]"));#endif ConOutChar (_T('\n'));}/* * set up global initializations and process parameters * * argc - number of parameters to command.com * argv - command-line parameters * */static VOIDInitialize (int argc, TCHAR *argv[]){ TCHAR commandline[CMDLINE_LENGTH]; INT i;#ifdef _DEBUG INT x; DebugPrintf (_T("[command args:\n")); for (x = 0; x < argc; x++) { DebugPrintf (_T("%d. %s\n"), x, argv[x]); } DebugPrintf (_T("]\n"));#endif /* get version information */ osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); GetVersionEx (&osvi); InitLocale ();#ifdef RAWINPUT SetConsoleMode (GetStdHandle(STD_INPUT_HANDLE), ENABLE_PROCESSED_INPUT);#else#endif if (argc >= 2 && !_tcsncmp (argv[1], _T("/?"), 2)) { ConOutPuts (_T("Starts a new instance of the ReactOS command line interpreter.\n" _T("\n") _T("CMD [/[C|K] command][/P][/Q][/T:bf]\n") _T("\n") _T(" /C command Runs the specified command and terminates.\n") _T(" /K command Runs the specified command and remains.\n") _T(" /P CMD becomes permanent and runs autoexec.bat\n") _T(" (cannot be terminated).\n") _T(" /T:bf Sets the background/foreground color (see COLOR command)."))); _exit(0); }#ifdef INCLUDE_CMD_CHDIR InitLastPath ();#endif#ifdef FATURE_ALIASES InitializeAlias ();#endif if (argc >= 2) { for (i = 1; i < argc; i++) { if (!_tcsicmp (argv[i], _T("/p"))) { if (!IsValidFileName (_T("\\autoexec.bat"))) {#ifdef INCLUDE_CMD_DATE cmd_date (_T(""), _T(""));#endif#ifdef INCLUDE_CMD_TIME cmd_time (_T(""), _T(""));#endif } else { ParseCommandLine (_T("\\autoexec.bat")); } bCanExit = FALSE; } else if (!_tcsicmp (argv[i], _T("/c"))) { /* This just runs a program and exits */ ++i; if (argv[i]) { _tcscpy (commandline, argv[i]); while (argv[++i]) { _tcscat (commandline, _T(" ")); _tcscat (commandline, argv[i]); } ParseCommandLine(commandline); _exit (ProcessInput (TRUE)); } else { _exit (0); } } else if (!_tcsicmp (argv[i], _T("/k"))) { /* This just runs a program and remains */ ++i; if (argv[i]) { _tcscpy (commandline, argv[i]); while (argv[++i]) { _tcscat (commandline, _T(" ")); _tcscat (commandline, argv[i]); } ParseCommandLine(commandline); } }#ifdef INCLUDE_CMD_COLOR else if (!_tcsnicmp (argv[i], _T("/t:"), 3)) { /* process /t (color) argument */ wDefColor = (WORD)_tcstoul (&argv[i][3], NULL, 16); wColor = wDefColor; SetScreenColor (wColor, TRUE); }#endif } } ShortVersion (); ShowCommands (); /* run cmdstart.bat */ if (IsValidFileName (_T("\\cmdstart.bat"))) { ParseCommandLine (_T("\\cmdstart.bat")); } else { /* try to run cmdstart.bat from install dir */ LPTSTR p; _tcscpy (commandline, argv[0]); p = _tcsrchr (commandline, _T('\\')) + 1; _tcscpy (p, _T("cmdstart.bat")); if (IsValidFileName (_T("commandline"))) { ConErrPrintf (_T("Running %s...\n"), commandline); ParseCommandLine (commandline); } }#ifdef FEATURE_DIR_STACK /* initialize directory stack */ InitDirectoryStack ();#endif /* Set COMSPEC environment variable */ if (argv) SetEnvironmentVariable (_T("COMSPEC"), argv[0]);#ifdef _WIN32_WCE SetEnvironmentVariable (_T("PATH"), _T("\\windows;"));#endif /* add ctrl break handler */ AddBreakHandler ();}static VOID Cleanup (int argc, TCHAR *argv[]){ /* run cmdexit.bat */ if (IsValidFileName (_T("cmdexit.bat"))) { ConErrPrintf (_T("Running cmdexit.bat...\n")); ParseCommandLine (_T("cmdexit.bat")); } else if (IsValidFileName (_T("\\cmdexit.bat"))) { ConErrPrintf (_T("Running \\cmdexit.bat...\n")); ParseCommandLine (_T("\\cmdexit.bat")); } else { /* try to run cmdexit.bat from install dir */ TCHAR commandline[CMDLINE_LENGTH]; LPTSTR p; _tcscpy (commandline, argv[0]); p = _tcsrchr (commandline, _T('\\')) + 1; _tcscpy (p, _T("cmdexit.bat")); if (IsValidFileName (_T("commandline"))) { ConErrPrintf (_T("Running %s...\n"), commandline); ParseCommandLine (commandline); } }#ifdef FEATURE_ALIASES DestroyAlias ();#endif#ifdef FEATURE_DIECTORY_STACK /* destroy directory stack */ DestroyDirectoryStack ();#endif#ifdef INCLUDE_CMD_CHDIR FreeLastPath ();#endif /* remove ctrl break handler */ RemoveBreakHandler ();}/* * main function */int _tmain (int argc, TCHAR *argv[]){ INT nExitCode; CONSOLE_SCREEN_BUFFER_INFO Info; xceinit(NULL);// AllocConsole ();// SetFileApisToOEM (); if( GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &Info ) == FALSE ) _tprintf( _T("GetConsoleScreenBufferInfo: Error: %ld\n"), GetLastError() ); wColor = Info.wAttributes; wDefColor = wColor; /* check switches on command-line */ Initialize (argc, argv); /* call prompt routine */ nExitCode = ProcessInput (FALSE); /* do the cleanup */ Cleanup (argc, argv);// FreeConsole (); return nExitCode;}/* EOF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -