Add cache for codepage so it does not call hole time on csrss when it wring out text
[reactos.git] / reactos / subsys / system / cmd / cmd.c
1 /*
2 * CMD.C - command-line interface.
3 *
4 *
5 * History:
6 *
7 * 17 Jun 1994 (Tim Norman)
8 * started.
9 *
10 * 08 Aug 1995 (Matt Rains)
11 * I have cleaned up the source code. changes now bring this source
12 * into guidelines for recommended programming practice.
13 *
14 * A added the the standard FreeDOS GNU licence test to the
15 * initialize() function.
16 *
17 * Started to replace puts() with printf(). this will help
18 * standardize output. please follow my lead.
19 *
20 * I have added some constants to help making changes easier.
21 *
22 * 15 Dec 1995 (Tim Norman)
23 * major rewrite of the code to make it more efficient and add
24 * redirection support (finally!)
25 *
26 * 06 Jan 1996 (Tim Norman)
27 * finished adding redirection support! Changed to use our own
28 * exec code (MUCH thanks to Svante Frey!!)
29 *
30 * 29 Jan 1996 (Tim Norman)
31 * added support for CHDIR, RMDIR, MKDIR, and ERASE, as per
32 * suggestion of Steffan Kaiser
33 *
34 * changed "file not found" error message to "bad command or
35 * filename" thanks to Dustin Norman for noticing that confusing
36 * message!
37 *
38 * changed the format to call internal commands (again) so that if
39 * they want to split their commands, they can do it themselves
40 * (none of the internal functions so far need that much power, anyway)
41 *
42 * 27 Aug 1996 (Tim Norman)
43 * added in support for Oliver Mueller's ALIAS command
44 *
45 * 14 Jun 1997 (Steffan Kaiser)
46 * added ctrl-break handling and error level
47 *
48 * 16 Jun 1998 (Rob Lake)
49 * Runs command.com if /P is specified in command line. Command.com
50 * also stays permanent. If /C is in the command line, starts the
51 * program next in the line.
52 *
53 * 21 Jun 1998 (Rob Lake)
54 * Fixed up /C so that arguments for the program
55 *
56 * 08-Jul-1998 (John P. Price)
57 * Now sets COMSPEC environment variable
58 * misc clean up and optimization
59 * added date and time commands
60 * changed to using spawnl instead of exec. exec does not copy the
61 * environment to the child process!
62 *
63 * 14 Jul 1998 (Hans B Pufal)
64 * Reorganised source to be more efficient and to more closely
65 * follow MS-DOS conventions. (eg %..% environment variable
66 * replacement works form command line as well as batch file.
67 *
68 * New organisation also properly support nested batch files.
69 *
70 * New command table structure is half way towards providing a
71 * system in which COMMAND will find out what internal commands
72 * are loaded
73 *
74 * 24 Jul 1998 (Hans B Pufal) [HBP_003]
75 * Fixed return value when called with /C option
76 *
77 * 27 Jul 1998 John P. Price
78 * added config.h include
79 *
80 * 28 Jul 1998 John P. Price
81 * added showcmds function to show commands and options available
82 *
83 * 07-Aug-1998 (John P Price <linux-guru@gcfl.net>)
84 * Fixed carrage return output to better match MSDOS with echo
85 * on or off. (marked with "JPP 19980708")
86 *
87 * 07-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
88 * First ReactOS release.
89 * Extended length of commandline buffers to 512.
90 *
91 * 13-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
92 * Added COMSPEC environment variable.
93 * Added "/t" support (color) on cmd command line.
94 *
95 * 07-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
96 * Added help text ("cmd /?").
97 *
98 * 25-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
99 * Unicode and redirection safe!
100 * Fixed redirections and piping.
101 * Piping is based on temporary files, but basic support
102 * for anonymous pipes already exists.
103 *
104 * 27-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
105 * Replaced spawnl() by CreateProcess().
106 *
107 * 22-Oct-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
108 * Added break handler.
109 *
110 * 15-Dec-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
111 * Fixed current directory
112 *
113 * 28-Dec-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
114 * Restore window title after program/batch execution
115 *
116 * 03-Feb-2001 (Eric Kohl <ekohl@rz-online.de>)
117 * Workaround because argc[0] is NULL under ReactOS
118 *
119 * 23-Feb-2001 (Carl Nettelblad <cnettel@hem.passagen.se>)
120 * %envvar% replacement conflicted with for.
121 *
122 * 30-Apr-2004 (Filip Navara <xnavara@volny.cz>)
123 * Make MakeSureDirectoryPathExistsEx unicode safe.
124 *
125 * 28-Mai-2004 (Hartmut Birr)
126 * Removed MakeSureDirectoryPathExistsEx.
127 * Use the current directory if GetTempPath fails.
128 *
129 * 12-Jul-2004 (Jens Collin <jens.collin@lakhei.com>)
130 * Added ShellExecute call when all else fails to be able to "launch" any file.
131 *
132 * 02-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
133 * Remove all hardcode string to En.rc
134 */
135
136 #include "precomp.h"
137 #include "resource.h"
138
139 #ifndef NT_SUCCESS
140 #define NT_SUCCESS(StatCode) ((NTSTATUS)(StatCode) >= 0)
141 #endif
142
143 typedef NTSTATUS (STDCALL *NtQueryInformationProcessProc)(HANDLE, PROCESSINFOCLASS,
144 PVOID, ULONG, PULONG);
145 typedef NTSTATUS (STDCALL *NtReadVirtualMemoryProc)(HANDLE, PVOID, PVOID, ULONG, PULONG);
146
147 BOOL bExit = FALSE; /* indicates EXIT was typed */
148 BOOL bCanExit = TRUE; /* indicates if this shell is exitable */
149 BOOL bCtrlBreak = FALSE; /* Ctrl-Break or Ctrl-C hit */
150 BOOL bIgnoreEcho = FALSE; /* Ignore 'newline' before 'cls' */
151 INT nErrorLevel = 0; /* Errorlevel of last launched external program */
152 BOOL bChildProcessRunning = FALSE;
153 DWORD dwChildProcessId = 0;
154 OSVERSIONINFO osvi;
155 HANDLE hIn;
156 HANDLE hOut;
157 HANDLE hConsole;
158
159 static NtQueryInformationProcessProc NtQueryInformationProcessPtr;
160 static NtReadVirtualMemoryProc NtReadVirtualMemoryPtr;
161 static BOOL NtDllChecked = FALSE;
162
163 #ifdef INCLUDE_CMD_COLOR
164 WORD wColor; /* current color */
165 WORD wDefColor; /* default color */
166 #endif
167
168 /*
169 * is character a delimeter when used on first word?
170 *
171 */
172
173 static BOOL IsDelimiter (TCHAR c)
174 {
175 return (c == _T('/') || c == _T('=') || c == _T('\0') || _istspace (c));
176 }
177
178 /*
179 * Is a process a console process?
180 */
181 static BOOL IsConsoleProcess(HANDLE Process)
182 {
183 NTSTATUS Status;
184 PROCESS_BASIC_INFORMATION Info;
185 PEB ProcessPeb;
186 ULONG BytesRead;
187 HMODULE NtDllModule;
188
189 /* Some people like to run ReactOS cmd.exe on Win98, it helps in the
190 build process. So don't link implicitly against ntdll.dll, load it
191 dynamically instead */
192 if (! NtDllChecked)
193 {
194 NtDllChecked = TRUE;
195 NtDllModule = LoadLibrary(_T("ntdll.dll"));
196 if (NULL == NtDllModule)
197 {
198 /* Probably non-WinNT system. Just wait for the commands
199 to finish. */
200 NtQueryInformationProcessPtr = NULL;
201 NtReadVirtualMemoryPtr = NULL;
202 return TRUE;
203 }
204 NtQueryInformationProcessPtr = (NtQueryInformationProcessProc)
205 GetProcAddress(NtDllModule, "NtQueryInformationProcess");
206 NtReadVirtualMemoryPtr = (NtReadVirtualMemoryProc)
207 GetProcAddress(NtDllModule, "NtReadVirtualMemory");
208 }
209
210 if (NULL == NtQueryInformationProcessPtr || NULL == NtReadVirtualMemoryPtr)
211 {
212 return TRUE;
213 }
214
215 Status = NtQueryInformationProcessPtr(Process, ProcessBasicInformation,
216 &Info, sizeof(PROCESS_BASIC_INFORMATION), NULL);
217 if (! NT_SUCCESS(Status))
218 {
219 #ifdef _DEBUG
220 DebugPrintf (_T("NtQueryInformationProcess failed with status %08x\n"), Status);
221 #endif
222 return TRUE;
223 }
224 Status = NtReadVirtualMemoryPtr(Process, Info.PebBaseAddress, &ProcessPeb,
225 sizeof(PEB), &BytesRead);
226 if (! NT_SUCCESS(Status) || sizeof(PEB) != BytesRead)
227 {
228 #ifdef _DEBUG
229 DebugPrintf (_T("Couldn't read virt mem status %08x bytes read %lu\n"), Status, BytesRead);
230 #endif
231 return TRUE;
232 }
233
234 return IMAGE_SUBSYSTEM_WINDOWS_CUI == ProcessPeb.ImageSubSystem;
235 }
236
237
238
239 #ifdef _UNICODE
240 #define SHELLEXECUTETEXT "ShellExecuteW"
241 #else
242 #define SHELLEXECUTETEXT "ShellExecuteA"
243 #endif
244
245 typedef HINSTANCE (WINAPI *MYEX)(
246 HWND hwnd,
247 LPCTSTR lpOperation,
248 LPCTSTR lpFile,
249 LPCTSTR lpParameters,
250 LPCTSTR lpDirectory,
251 INT nShowCmd
252 );
253
254
255
256 static BOOL RunFile(LPTSTR filename)
257 {
258 HMODULE hShell32;
259 MYEX hShExt;
260 HINSTANCE ret;
261
262 #ifdef _DEBUG
263 DebugPrintf (_T("RunFile(%s)\n"), filename);
264 #endif
265 hShell32 = LoadLibrary(_T("SHELL32.DLL"));
266 if (!hShell32)
267 {
268 #ifdef _DEBUG
269 DebugPrintf (_T("RunFile: couldn't load SHELL32.DLL!\n"));
270 #endif
271 return FALSE;
272 }
273
274 hShExt = (MYEX)(FARPROC)GetProcAddress(hShell32, SHELLEXECUTETEXT);
275 if (!hShExt)
276 {
277 #ifdef _DEBUG
278 DebugPrintf (_T("RunFile: couldn't find ShellExecuteA/W in SHELL32.DLL!\n"));
279 #endif
280 FreeLibrary(hShell32);
281 return FALSE;
282 }
283
284 #ifdef _DEBUG
285 DebugPrintf (_T("RunFile: ShellExecuteA/W is at %x\n"), hShExt);
286 #endif
287
288 ret = (hShExt)(NULL, _T("open"), filename, NULL, NULL, SW_SHOWNORMAL);
289
290 #ifdef _DEBUG
291 DebugPrintf (_T("RunFile: ShellExecuteA/W returned %d\n"), (DWORD)ret);
292 #endif
293
294 FreeLibrary(hShell32);
295 return (((DWORD)ret) > 32);
296 }
297
298
299
300 /*
301 * This command (in first) was not found in the command table
302 *
303 * first - first word on command line
304 * rest - rest of command line
305 */
306
307 static VOID
308 Execute (LPTSTR full, LPTSTR first, LPTSTR rest)
309 {
310 TCHAR szFullName[MAX_PATH];
311 #ifndef __REACTOS__
312 TCHAR szWindowTitle[MAX_PATH];
313 #endif
314 DWORD dwExitCode = 0;
315
316 #ifdef _DEBUG
317 DebugPrintf (_T("Execute: \'%s\' \'%s\'\n"), first, rest);
318 #endif
319
320 /* check for a drive change */
321 if ((_istalpha (first[0])) && (!_tcscmp (first + 1, _T(":"))))
322 {
323 BOOL working = TRUE;
324 if (!SetCurrentDirectory(first))
325 /* Guess they changed disc or something, handle that gracefully and get to root */
326 {
327 TCHAR str[4];
328 str[0]=first[0];
329 str[1]=_T(':');
330 str[2]=_T('\\');
331 str[3]=0;
332 working = SetCurrentDirectory(str);
333 }
334
335 if (!working) ConErrPuts (INVALIDDRIVE);
336
337 return;
338 }
339
340 /* get the PATH environment variable and parse it */
341 /* search the PATH environment variable for the binary */
342 if (!SearchForExecutable (first, szFullName))
343 {
344 error_bad_command ();
345 return;
346 }
347
348 #ifndef __REACTOS__
349 GetConsoleTitle (szWindowTitle, MAX_PATH);
350 #endif
351
352 /* check if this is a .BAT or .CMD file */
353 if (!_tcsicmp (_tcsrchr (szFullName, _T('.')), _T(".bat")) ||
354 !_tcsicmp (_tcsrchr (szFullName, _T('.')), _T(".cmd")))
355 {
356 #ifdef _DEBUG
357 DebugPrintf (_T("[BATCH: %s %s]\n"), szFullName, rest);
358 #endif
359 Batch (szFullName, first, rest);
360 }
361 else
362 {
363 /* exec the program */
364 PROCESS_INFORMATION prci;
365 STARTUPINFO stui;
366
367 #ifdef _DEBUG
368 DebugPrintf (_T("[EXEC: %s %s]\n"), full, rest);
369 #endif
370 /* build command line for CreateProcess() */
371
372 /* fill startup info */
373 memset (&stui, 0, sizeof (STARTUPINFO));
374 stui.cb = sizeof (STARTUPINFO);
375 stui.dwFlags = STARTF_USESHOWWINDOW;
376 stui.wShowWindow = SW_SHOWDEFAULT;
377
378 // return console to standard mode
379 SetConsoleMode (GetStdHandle(STD_INPUT_HANDLE),
380 ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_ECHO_INPUT );
381
382 if (CreateProcess (szFullName,
383 full,
384 NULL,
385 NULL,
386 TRUE,
387 CREATE_NEW_PROCESS_GROUP,
388 NULL,
389 NULL,
390 &stui,
391 &prci))
392 {
393 if (IsConsoleProcess(prci.hProcess))
394 {
395 /* FIXME: Protect this with critical section */
396 bChildProcessRunning = TRUE;
397 dwChildProcessId = prci.dwProcessId;
398
399 WaitForSingleObject (prci.hProcess, INFINITE);
400
401 /* FIXME: Protect this with critical section */
402 bChildProcessRunning = FALSE;
403
404 GetExitCodeProcess (prci.hProcess, &dwExitCode);
405 nErrorLevel = (INT)dwExitCode;
406 }
407 CloseHandle (prci.hThread);
408 CloseHandle (prci.hProcess);
409 }
410 else
411 {
412 #ifdef _DEBUG
413 DebugPrintf (_T("[ShellExecute: %s]\n"), full);
414 #endif
415 // See if we can run this with ShellExecute() ie myfile.xls
416 if (!RunFile(full))
417 {
418 #ifdef _DEBUG
419 DebugPrintf (_T("[ShellExecute failed!: %s]\n"), full);
420 #endif
421 error_bad_command ();
422 }
423 }
424 // restore console mode
425 SetConsoleMode( GetStdHandle( STD_INPUT_HANDLE ),
426 ENABLE_PROCESSED_INPUT );
427 }
428
429 /* Get code page if it has been change */
430 GetCodePage = GetConsoleCP();
431 OutCodePage = GetConsoleOutputCP();
432 #ifndef __REACTOS__
433 SetConsoleTitle (szWindowTitle);
434 #endif
435 }
436
437
438 /*
439 * look through the internal commands and determine whether or not this
440 * command is one of them. If it is, call the command. If not, call
441 * execute to run it as an external program.
442 *
443 * line - the command line of the program to run
444 *
445 */
446
447 static VOID
448 DoCommand (LPTSTR line)
449 {
450 TCHAR com[CMDLINE_LENGTH]; /* the first word in the command */
451 LPTSTR cp = com;
452 LPTSTR cstart;
453 LPTSTR rest; /* pointer to the rest of the command line */
454 INT cl;
455 LPCOMMAND cmdptr;
456
457 #ifdef _DEBUG
458 DebugPrintf (_T("DoCommand: (\'%s\')\n"), line);
459 #endif /* DEBUG */
460
461 /* Skip over initial white space */
462 while (_istspace (*line))
463 line++;
464 rest = line;
465
466 cstart = rest;
467
468 /* Anything to do ? */
469 if (*rest)
470 {
471 if (*rest == _T('"'))
472 {
473 /* treat quoted words specially */
474
475 rest++;
476
477 while(*rest != _T('\0') && *rest != _T('"'))
478 *cp++ = _totlower (*rest++);
479 if (*rest == _T('"'))
480 rest++;
481 }
482 else
483 {
484 while (!IsDelimiter (*rest))
485 *cp++ = _totlower (*rest++);
486 }
487
488
489 /* Terminate first word */
490 *cp = _T('\0');
491
492 /* commands are limited to MAX_PATH */
493 if(_tcslen(com) > MAX_PATH)
494 {
495 error_bad_command();
496 return;
497 }
498
499 /* Skip over whitespace to rest of line */
500 while (_istspace (*rest))
501 rest++;
502
503 /* Scan internal command table */
504 for (cmdptr = cmds;; cmdptr++)
505 {
506 /* If end of table execute ext cmd */
507 if (cmdptr->name == NULL)
508 {
509 Execute (line, com, rest);
510 break;
511 }
512
513 if (!_tcscmp (com, cmdptr->name))
514 {
515 cmdptr->func (com, rest);
516 break;
517 }
518
519 /* The following code handles the case of commands like CD which
520 * are recognised even when the command name and parameter are
521 * not space separated.
522 *
523 * e.g dir..
524 * cd\freda
525 */
526
527 /* Get length of command name */
528 cl = _tcslen (cmdptr->name);
529
530 if ((cmdptr->flags & CMD_SPECIAL) &&
531 (!_tcsncmp (cmdptr->name, com, cl)) &&
532 (_tcschr (_T("\\.-"), *(com + cl))))
533 {
534 /* OK its one of the specials...*/
535
536 /* Terminate first word properly */
537 com[cl] = _T('\0');
538
539 /* Call with new rest */
540 cmdptr->func (com, cstart + cl);
541 break;
542 }
543 }
544 }
545 }
546
547
548 /*
549 * process the command line and execute the appropriate functions
550 * full input/output redirection and piping are supported
551 */
552
553 VOID ParseCommandLine (LPTSTR cmd)
554 {
555 TCHAR szMsg[RC_STRING_MAX_SIZE];
556 TCHAR cmdline[CMDLINE_LENGTH];
557 LPTSTR s;
558 #ifdef FEATURE_REDIRECTION
559 TCHAR in[CMDLINE_LENGTH] = _T("");
560 TCHAR out[CMDLINE_LENGTH] = _T("");
561 TCHAR err[CMDLINE_LENGTH] = _T("");
562 TCHAR szTempPath[MAX_PATH] = _T(".\\");
563 TCHAR szFileName[2][MAX_PATH] = {_T(""), _T("")};
564 HANDLE hFile[2] = {INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE};
565 LPTSTR t = NULL;
566 INT num = 0;
567 INT nRedirFlags = 0;
568 INT Length;
569 UINT Attributes;
570
571 HANDLE hOldConIn;
572 HANDLE hOldConOut;
573 HANDLE hOldConErr;
574 #endif /* FEATURE_REDIRECTION */
575
576 _tcscpy (cmdline, cmd);
577 s = &cmdline[0];
578
579 #ifdef _DEBUG
580 DebugPrintf (_T("ParseCommandLine: (\'%s\')\n"), s);
581 #endif /* DEBUG */
582
583 #ifdef FEATURE_ALIASES
584 /* expand all aliases */
585 ExpandAlias (s, CMDLINE_LENGTH);
586 #endif /* FEATURE_ALIAS */
587
588 #ifdef FEATURE_REDIRECTION
589 /* find the temp path to store temporary files */
590 Length = GetTempPath (MAX_PATH, szTempPath);
591 if (Length > 0 && Length < MAX_PATH)
592 {
593 Attributes = GetFileAttributes(szTempPath);
594 if (Attributes == 0xffffffff ||
595 !(Attributes & FILE_ATTRIBUTE_DIRECTORY))
596 {
597 Length = 0;
598 }
599 }
600 if (Length == 0 || Length >= MAX_PATH)
601 {
602 _tcscpy(szTempPath, _T(".\\"));
603 }
604 if (szTempPath[_tcslen (szTempPath) - 1] != _T('\\'))
605 _tcscat (szTempPath, _T("\\"));
606
607 /* get the redirections from the command line */
608 num = GetRedirection (s, in, out, err, &nRedirFlags);
609
610 /* more efficient, but do we really need to do this? */
611 for (t = in; _istspace (*t); t++)
612 ;
613 _tcscpy (in, t);
614
615 for (t = out; _istspace (*t); t++)
616 ;
617 _tcscpy (out, t);
618
619 for (t = err; _istspace (*t); t++)
620 ;
621 _tcscpy (err, t);
622
623 /* Set up the initial conditions ... */
624 /* preserve STDIN, STDOUT and STDERR handles */
625 hOldConIn = GetStdHandle (STD_INPUT_HANDLE);
626 hOldConOut = GetStdHandle (STD_OUTPUT_HANDLE);
627 hOldConErr = GetStdHandle (STD_ERROR_HANDLE);
628
629 /* redirect STDIN */
630 if (in[0])
631 {
632 HANDLE hFile;
633 SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
634
635 hFile = CreateFile (in, GENERIC_READ, FILE_SHARE_READ, &sa, OPEN_EXISTING,
636 FILE_ATTRIBUTE_NORMAL, NULL);
637 if (hFile == INVALID_HANDLE_VALUE)
638 {
639 LoadString(GetModuleHandle(NULL), STRING_CMD_ERROR1, szMsg, RC_STRING_MAX_SIZE);
640 ConErrPrintf(szMsg, in);
641 return;
642 }
643
644 if (!SetStdHandle (STD_INPUT_HANDLE, hFile))
645 {
646 LoadString(GetModuleHandle(NULL), STRING_CMD_ERROR1, szMsg, RC_STRING_MAX_SIZE);
647 ConErrPrintf(szMsg, in);
648 return;
649 }
650 #ifdef _DEBUG
651 DebugPrintf (_T("Input redirected from: %s\n"), in);
652 #endif
653 }
654
655 /* Now do all but the last pipe command */
656 *szFileName[0] = _T('\0');
657 hFile[0] = INVALID_HANDLE_VALUE;
658
659 while (num-- > 1)
660 {
661 SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
662
663 /* Create unique temporary file name */
664 GetTempFileName (szTempPath, _T("CMD"), 0, szFileName[1]);
665
666 /* Set current stdout to temporary file */
667 hFile[1] = CreateFile (szFileName[1], GENERIC_WRITE, 0, &sa,
668 TRUNCATE_EXISTING, FILE_ATTRIBUTE_TEMPORARY, NULL);
669 if (hFile[1] == INVALID_HANDLE_VALUE)
670 {
671 LoadString(GetModuleHandle(NULL), STRING_CMD_ERROR2, szMsg, RC_STRING_MAX_SIZE);
672 ConErrPrintf(szMsg);
673 return;
674 }
675
676 SetStdHandle (STD_OUTPUT_HANDLE, hFile[1]);
677
678 DoCommand (s);
679
680 /* close stdout file */
681 SetStdHandle (STD_OUTPUT_HANDLE, hOldConOut);
682 if ((hFile[1] != INVALID_HANDLE_VALUE) && (hFile[1] != hOldConOut))
683 {
684 CloseHandle (hFile[1]);
685 hFile[1] = INVALID_HANDLE_VALUE;
686 }
687
688 /* close old stdin file */
689 SetStdHandle (STD_INPUT_HANDLE, hOldConIn);
690 if ((hFile[0] != INVALID_HANDLE_VALUE) && (hFile[0] != hOldConIn))
691 {
692 /* delete old stdin file, if it is a real file */
693 CloseHandle (hFile[0]);
694 hFile[0] = INVALID_HANDLE_VALUE;
695 DeleteFile (szFileName[0]);
696 *szFileName[0] = _T('\0');
697 }
698
699 /* copy stdout file name to stdin file name */
700 _tcscpy (szFileName[0], szFileName[1]);
701 *szFileName[1] = _T('\0');
702
703 /* open new stdin file */
704 hFile[0] = CreateFile (szFileName[0], GENERIC_READ, 0, &sa,
705 OPEN_EXISTING, FILE_ATTRIBUTE_TEMPORARY, NULL);
706 SetStdHandle (STD_INPUT_HANDLE, hFile[0]);
707
708 s = s + _tcslen (s) + 1;
709 }
710
711 /* Now set up the end conditions... */
712 /* redirect STDOUT */
713 if (out[0])
714 {
715 /* Final output to here */
716 HANDLE hFile;
717 SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
718
719 hFile = CreateFile (out, GENERIC_WRITE, FILE_SHARE_READ, &sa,
720 (nRedirFlags & OUTPUT_APPEND) ? OPEN_ALWAYS : CREATE_ALWAYS,
721 FILE_ATTRIBUTE_NORMAL, NULL);
722 if (hFile == INVALID_HANDLE_VALUE)
723 {
724 LoadString(GetModuleHandle(NULL), STRING_CMD_ERROR3, szMsg, RC_STRING_MAX_SIZE);
725 ConErrPrintf(szMsg, out);
726 return;
727 }
728
729 if (!SetStdHandle (STD_OUTPUT_HANDLE, hFile))
730 {
731 LoadString(GetModuleHandle(NULL), STRING_CMD_ERROR3, szMsg, RC_STRING_MAX_SIZE);
732 ConErrPrintf(szMsg, out);
733 return;
734 }
735
736 if (nRedirFlags & OUTPUT_APPEND)
737 {
738 LONG lHighPos = 0;
739
740 if (GetFileType (hFile) == FILE_TYPE_DISK)
741 SetFilePointer (hFile, 0, &lHighPos, FILE_END);
742 }
743 #ifdef _DEBUG
744 DebugPrintf (_T("Output redirected to: %s\n"), out);
745 #endif
746 }
747 else if (hOldConOut != INVALID_HANDLE_VALUE)
748 {
749 /* Restore original stdout */
750 HANDLE hOut = GetStdHandle (STD_OUTPUT_HANDLE);
751 SetStdHandle (STD_OUTPUT_HANDLE, hOldConOut);
752 if (hOldConOut != hOut)
753 CloseHandle (hOut);
754 hOldConOut = INVALID_HANDLE_VALUE;
755 }
756
757 /* redirect STDERR */
758 if (err[0])
759 {
760 /* Final output to here */
761 HANDLE hFile;
762 SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
763
764 if (!_tcscmp (err, out))
765 {
766 #ifdef _DEBUG
767 DebugPrintf (_T("Stdout and stderr will use the same file!!\n"));
768 #endif
769 DuplicateHandle (GetCurrentProcess (),
770 GetStdHandle (STD_OUTPUT_HANDLE),
771 GetCurrentProcess (),
772 &hFile, 0, TRUE, DUPLICATE_SAME_ACCESS);
773 }
774 else
775 {
776 hFile = CreateFile (err,
777 GENERIC_WRITE,
778 0,
779 &sa,
780 (nRedirFlags & ERROR_APPEND) ? OPEN_ALWAYS : CREATE_ALWAYS,
781 FILE_ATTRIBUTE_NORMAL,
782 NULL);
783 if (hFile == INVALID_HANDLE_VALUE)
784 {
785 LoadString(GetModuleHandle(NULL), STRING_CMD_ERROR3, szMsg, RC_STRING_MAX_SIZE);
786 ConErrPrintf(szMsg, err);
787 return;
788 }
789 }
790
791 if (!SetStdHandle (STD_ERROR_HANDLE, hFile))
792 {
793 LoadString(GetModuleHandle(NULL), STRING_CMD_ERROR3, szMsg, RC_STRING_MAX_SIZE);
794 ConErrPrintf(szMsg, err);
795 return;
796 }
797
798 if (nRedirFlags & ERROR_APPEND)
799 {
800 LONG lHighPos = 0;
801
802 if (GetFileType (hFile) == FILE_TYPE_DISK)
803 SetFilePointer (hFile, 0, &lHighPos, FILE_END);
804 }
805 #ifdef _DEBUG
806 DebugPrintf (_T("Error redirected to: %s\n"), err);
807 #endif
808 }
809 else if (hOldConErr != INVALID_HANDLE_VALUE)
810 {
811 /* Restore original stderr */
812 HANDLE hErr = GetStdHandle (STD_ERROR_HANDLE);
813 SetStdHandle (STD_ERROR_HANDLE, hOldConErr);
814 if (hOldConErr != hErr)
815 CloseHandle (hErr);
816 hOldConErr = INVALID_HANDLE_VALUE;
817 }
818 #endif
819
820 /* process final command */
821 DoCommand (s);
822
823 #ifdef FEATURE_REDIRECTION
824 /* close old stdin file */
825 #if 0 /* buggy implementation */
826 SetStdHandle (STD_INPUT_HANDLE, hOldConIn);
827 if ((hFile[0] != INVALID_HANDLE_VALUE) &&
828 (hFile[0] != hOldConIn))
829 {
830 /* delete old stdin file, if it is a real file */
831 CloseHandle (hFile[0]);
832 hFile[0] = INVALID_HANDLE_VALUE;
833 DeleteFile (szFileName[0]);
834 *szFileName[0] = _T('\0');
835 }
836
837 /* Restore original STDIN */
838 if (hOldConIn != INVALID_HANDLE_VALUE)
839 {
840 HANDLE hIn = GetStdHandle (STD_INPUT_HANDLE);
841 SetStdHandle (STD_INPUT_HANDLE, hOldConIn);
842 if (hOldConIn != hIn)
843 CloseHandle (hIn);
844 hOldConIn = INVALID_HANDLE_VALUE;
845 }
846 else
847 {
848 #ifdef _DEBUG
849 DebugPrintf (_T("Can't restore STDIN! Is invalid!!\n"), out);
850 #endif
851 }
852 #endif /* buggy implementation */
853
854
855 if (hOldConIn != INVALID_HANDLE_VALUE)
856 {
857 HANDLE hIn = GetStdHandle (STD_INPUT_HANDLE);
858 SetStdHandle (STD_INPUT_HANDLE, hOldConIn);
859 if (hIn == INVALID_HANDLE_VALUE)
860 {
861 #ifdef _DEBUG
862 DebugPrintf (_T("Previous STDIN is invalid!!\n"));
863 #endif
864 }
865 else
866 {
867 if (GetFileType (hIn) == FILE_TYPE_DISK)
868 {
869 if (hFile[0] == hIn)
870 {
871 CloseHandle (hFile[0]);
872 hFile[0] = INVALID_HANDLE_VALUE;
873 DeleteFile (szFileName[0]);
874 *szFileName[0] = _T('\0');
875 }
876 else
877 {
878 #ifdef _DEBUG
879 DebugPrintf (_T("hFile[0] and hIn dont match!!!\n"));
880 #endif
881 }
882 }
883 }
884 }
885
886
887 /* Restore original STDOUT */
888 if (hOldConOut != INVALID_HANDLE_VALUE)
889 {
890 HANDLE hOut = GetStdHandle (STD_OUTPUT_HANDLE);
891 SetStdHandle (STD_OUTPUT_HANDLE, hOldConOut);
892 if (hOldConOut != hOut)
893 CloseHandle (hOut);
894 hOldConOut = INVALID_HANDLE_VALUE;
895 }
896
897 /* Restore original STDERR */
898 if (hOldConErr != INVALID_HANDLE_VALUE)
899 {
900 HANDLE hErr = GetStdHandle (STD_ERROR_HANDLE);
901 SetStdHandle (STD_ERROR_HANDLE, hOldConErr);
902 if (hOldConErr != hErr)
903 CloseHandle (hErr);
904 hOldConErr = INVALID_HANDLE_VALUE;
905 }
906 #endif /* FEATURE_REDIRECTION */
907 }
908
909
910 /*
911 * do the prompt/input/process loop
912 *
913 */
914
915 static INT
916 ProcessInput (BOOL bFlag)
917 {
918 TCHAR commandline[CMDLINE_LENGTH];
919 TCHAR readline[CMDLINE_LENGTH];
920 LPTSTR tp = NULL;
921 LPTSTR ip;
922 LPTSTR cp;
923 BOOL bEchoThisLine;
924
925
926 do
927 {
928 /* if no batch input then... */
929 if (!(ip = ReadBatchLine (&bEchoThisLine)))
930 {
931 if (bFlag)
932 return 0;
933
934 ReadCommand (readline, CMDLINE_LENGTH);
935 ip = readline;
936 bEchoThisLine = FALSE;
937 }
938
939 cp = commandline;
940 while (*ip)
941 {
942 if (*ip == _T('%'))
943 {
944 switch (*++ip)
945 {
946 case _T('%'):
947 *cp++ = *ip++;
948 break;
949
950 case _T('0'):
951 case _T('1'):
952 case _T('2'):
953 case _T('3'):
954 case _T('4'):
955 case _T('5'):
956 case _T('6'):
957 case _T('7'):
958 case _T('8'):
959 case _T('9'):
960 if ((tp = FindArg (*ip - _T('0'))))
961 {
962 cp = _stpcpy (cp, tp);
963 ip++;
964 }
965 else
966 *cp++ = _T('%');
967 break;
968
969 case _T('?'):
970 cp += _stprintf (cp, _T("%u"), nErrorLevel);
971 ip++;
972 break;
973
974 default:
975 tp = _tcschr(ip, _T('%'));
976 if ((tp != NULL) &&
977 (tp <= _tcschr(ip, _T(' ')) - 1))
978 {
979 TCHAR evar[512];
980 *tp = _T('\0');
981
982 /* FIXME: This is just a quick hack!! */
983 /* Do a proper memory allocation!! */
984 if (GetEnvironmentVariable (ip, evar, 512))
985 cp = _stpcpy (cp, evar);
986
987 ip = tp + 1;
988 }
989 else
990 {
991 *cp++ = _T('%');
992 }
993 break;
994 }
995 continue;
996 }
997
998 if (_istcntrl (*ip))
999 *ip = _T(' ');
1000 *cp++ = *ip++;
1001 }
1002
1003 *cp = _T('\0');
1004
1005 /* strip trailing spaces */
1006 while ((--cp >= commandline) && _istspace (*cp));
1007
1008 *(cp + 1) = _T('\0');
1009
1010 /* JPP 19980807 */
1011 /* Echo batch file line */
1012 if (bEchoThisLine)
1013 {
1014 PrintPrompt ();
1015 ConOutPuts (commandline);
1016 }
1017
1018 if (*commandline)
1019 {
1020 ParseCommandLine (commandline);
1021 if (bEcho && !bIgnoreEcho)
1022 ConOutChar ('\n');
1023 bIgnoreEcho = FALSE;
1024 }
1025 }
1026 while (!bCanExit || !bExit);
1027
1028 return 0;
1029 }
1030
1031
1032 /*
1033 * control-break handler.
1034 */
1035 BOOL WINAPI BreakHandler (DWORD dwCtrlType)
1036 {
1037
1038 if ((dwCtrlType != CTRL_C_EVENT) &&
1039 (dwCtrlType != CTRL_BREAK_EVENT))
1040 return FALSE;
1041
1042 if (bChildProcessRunning == TRUE)
1043 {
1044 GenerateConsoleCtrlEvent (CTRL_C_EVENT,
1045 dwChildProcessId);
1046 return TRUE;
1047 }
1048
1049 /* FIXME: Handle batch files */
1050
1051 /* FIXME: Print "^C" */
1052
1053
1054 return TRUE;
1055 }
1056
1057
1058 VOID AddBreakHandler (VOID)
1059 {
1060 SetConsoleCtrlHandler ((PHANDLER_ROUTINE)BreakHandler, TRUE);
1061 }
1062
1063
1064 VOID RemoveBreakHandler (VOID)
1065 {
1066 SetConsoleCtrlHandler ((PHANDLER_ROUTINE)BreakHandler, FALSE);
1067 }
1068
1069
1070 /*
1071 * show commands and options that are available.
1072 *
1073 */
1074 #if 0
1075 static VOID
1076 ShowCommands (VOID)
1077 {
1078 TCHAR szMsg[RC_STRING_MAX_SIZE];
1079
1080 /* print command list */
1081 LoadString(GetModuleHandle(NULL), STRING_CMD_HELP1, szMsg, RC_STRING_MAX_SIZE);
1082 ConOutPrintf(szMsg);
1083 PrintCommandList();
1084
1085 /* print feature list */
1086 LoadString(GetModuleHandle(NULL), STRING_CMD_HELP2, szMsg, RC_STRING_MAX_SIZE);
1087 ConOutPuts(szMsg);
1088
1089 #ifdef FEATURE_ALIASES
1090 LoadString(GetModuleHandle(NULL), STRING_CMD_HELP3, szMsg, RC_STRING_MAX_SIZE);
1091 ConOutPuts(szMsg);
1092 #endif
1093 #ifdef FEATURE_HISTORY
1094 LoadString(GetModuleHandle(NULL), STRING_CMD_HELP4, szMsg, RC_STRING_MAX_SIZE);
1095 ConOutPuts(szMsg);
1096 #endif
1097 #ifdef FEATURE_UNIX_FILENAME_COMPLETION
1098 LoadString(GetModuleHandle(NULL), STRING_CMD_HELP5, szMsg, RC_STRING_MAX_SIZE);
1099 ConOutPuts(szMsg);
1100 #endif
1101 #ifdef FEATURE_DIRECTORY_STACK
1102 LoadString(GetModuleHandle(NULL), STRING_CMD_HELP6, szMsg, RC_STRING_MAX_SIZE);
1103 ConOutPuts(szMsg);
1104 #endif
1105 #ifdef FEATURE_REDIRECTION
1106 LoadString(GetModuleHandle(NULL), STRING_CMD_HELP7, szMsg, RC_STRING_MAX_SIZE);
1107 ConOutPuts(szMsg);
1108 #endif
1109 ConOutChar(_T('\n'));
1110 }
1111 #endif
1112
1113 /*
1114 * set up global initializations and process parameters
1115 *
1116 * argc - number of parameters to command.com
1117 * argv - command-line parameters
1118 *
1119 */
1120 static VOID
1121 Initialize (int argc, TCHAR* argv[])
1122 {
1123 TCHAR szMsg[RC_STRING_MAX_SIZE];
1124 TCHAR commandline[CMDLINE_LENGTH];
1125 TCHAR ModuleName[_MAX_PATH + 1];
1126 INT i;
1127
1128 //INT len;
1129 //TCHAR *ptr, *cmdLine;
1130
1131
1132 #ifdef _DEBUG
1133 INT x;
1134
1135 DebugPrintf (_T("[command args:\n"));
1136 for (x = 0; x < argc; x++)
1137 {
1138 DebugPrintf (_T("%d. %s\n"), x, argv[x]);
1139 }
1140 DebugPrintf (_T("]\n"));
1141 #endif
1142
1143 /* get version information */
1144 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1145 GetVersionEx (&osvi);
1146
1147 InitLocale ();
1148
1149 /* get default input and output console handles */
1150 hOut = GetStdHandle (STD_OUTPUT_HANDLE);
1151 hIn = GetStdHandle (STD_INPUT_HANDLE);
1152
1153
1154 if (argc >= 2 && !_tcsncmp (argv[1], _T("/?"), 2))
1155 {
1156 LoadString(GetModuleHandle(NULL), STRING_CMD_HELP8, szMsg, RC_STRING_MAX_SIZE);
1157 ConOutPuts(szMsg);
1158 ExitProcess(0);
1159 }
1160 SetConsoleMode (hIn, ENABLE_PROCESSED_INPUT);
1161
1162 #ifdef INCLUDE_CMD_CHDIR
1163 InitLastPath ();
1164 #endif
1165
1166 #ifdef FATURE_ALIASES
1167 InitializeAlias ();
1168 #endif
1169
1170 if (argc >= 2)
1171 {
1172 for (i = 1; i < argc; i++)
1173 {
1174 if (!_tcsicmp (argv[i], _T("/p")))
1175 {
1176 if (!IsExistingFile (_T("\\autoexec.bat")))
1177 {
1178 #ifdef INCLUDE_CMD_DATE
1179 cmd_date (_T(""), _T(""));
1180 #endif
1181 #ifdef INCLUDE_CMD_TIME
1182 cmd_time (_T(""), _T(""));
1183 #endif
1184 }
1185 else
1186 {
1187 ParseCommandLine (_T("\\autoexec.bat"));
1188 }
1189 bCanExit = FALSE;
1190 }
1191 else if (!_tcsicmp (argv[i], _T("/c")))
1192 {
1193 /* This just runs a program and exits */
1194 ++i;
1195 if (i < argc)
1196 {
1197 _tcscpy (commandline, argv[i]);
1198 while (++i < argc)
1199 {
1200 _tcscat (commandline, _T(" "));
1201 _tcscat (commandline, argv[i]);
1202 }
1203
1204 ParseCommandLine(commandline);
1205 ExitProcess (ProcessInput (TRUE));
1206 }
1207 else
1208 {
1209 ExitProcess (0);
1210 }
1211 }
1212 else if (!_tcsicmp (argv[i], _T("/k")))
1213 {
1214 /* This just runs a program and remains */
1215 ++i;
1216 if (i < argc)
1217 {
1218 _tcscpy (commandline, argv[i]);
1219 while (++i < argc)
1220 {
1221 _tcscat (commandline, _T(" "));
1222 _tcscat (commandline, argv[i]);
1223 }
1224
1225 ParseCommandLine(commandline);
1226 }
1227 }
1228 #ifdef INCLUDE_CMD_COLOR
1229 else if (!_tcsnicmp (argv[i], _T("/t:"), 3))
1230 {
1231 /* process /t (color) argument */
1232 wDefColor = (WORD)_tcstoul (&argv[i][3], NULL, 16);
1233 wColor = wDefColor;
1234 SetScreenColor (wColor, TRUE);
1235 }
1236 #endif
1237 }
1238 }
1239
1240 /* run cmdstart.bat */
1241 if (IsExistingFile (_T("cmdstart.bat")))
1242 {
1243 ParseCommandLine (_T("cmdstart.bat"));
1244 }
1245 else if (IsExistingFile (_T("\\cmdstart.bat")))
1246 {
1247 ParseCommandLine (_T("\\cmdstart.bat"));
1248 }
1249 #ifndef __REACTOS__
1250 else
1251 {
1252 /* try to run cmdstart.bat from install dir */
1253 LPTSTR p;
1254
1255 _tcscpy (commandline, argv[0]);
1256 p = _tcsrchr (commandline, _T('\\')) + 1;
1257 _tcscpy (p, _T("cmdstart.bat"));
1258
1259 if (IsExistingFile (_T("commandline")))
1260 {
1261 LoadString(GetModuleHandle(NULL), STRING_CMD_ERROR4, szMsg, RC_STRING_MAX_SIZE);
1262 ConErrPrintf(szMsg, commandline);
1263 ParseCommandLine (commandline);
1264 }
1265 }
1266 #endif
1267
1268 #ifdef FEATURE_DIR_STACK
1269 /* initialize directory stack */
1270 InitDirectoryStack ();
1271 #endif
1272
1273
1274 #ifdef FEATURE_HISTORY
1275 /*initialize history*/
1276 InitHistory();
1277 #endif
1278
1279 /* Set COMSPEC environment variable */
1280 if (0 != GetModuleFileName (NULL, ModuleName, _MAX_PATH + 1))
1281 {
1282 ModuleName[_MAX_PATH] = _T('\0');
1283 SetEnvironmentVariable (_T("COMSPEC"), ModuleName);
1284 }
1285
1286 /* add ctrl break handler */
1287 AddBreakHandler ();
1288 }
1289
1290
1291 static VOID Cleanup (int argc, TCHAR *argv[])
1292 {
1293 TCHAR szMsg[RC_STRING_MAX_SIZE];
1294
1295 /* run cmdexit.bat */
1296 if (IsExistingFile (_T("cmdexit.bat")))
1297 {
1298 LoadString(GetModuleHandle(NULL), STRING_CMD_ERROR5, szMsg, RC_STRING_MAX_SIZE);
1299 ConErrPrintf(szMsg);
1300
1301 ParseCommandLine (_T("cmdexit.bat"));
1302 }
1303 else if (IsExistingFile (_T("\\cmdexit.bat")))
1304 {
1305 LoadString( GetModuleHandle(NULL), STRING_CMD_ERROR5, szMsg, RC_STRING_MAX_SIZE);
1306 ConErrPrintf ((LPTSTR)szMsg);
1307 ParseCommandLine (_T("\\cmdexit.bat"));
1308 }
1309 #ifndef __REACTOS__
1310 else
1311 {
1312 /* try to run cmdexit.bat from install dir */
1313 TCHAR commandline[CMDLINE_LENGTH];
1314 LPTSTR p;
1315
1316 _tcscpy (commandline, argv[0]);
1317 p = _tcsrchr (commandline, _T('\\')) + 1;
1318 _tcscpy (p, _T("cmdexit.bat"));
1319
1320 if (IsExistingFile (_T("commandline")))
1321 {
1322 LoadString(GetModuleHandle(NULL), STRING_CMD_ERROR4, szMsg, RC_STRING_MAX_SIZE);
1323 ConErrPrintf(szMsg, commandline);
1324 ParseCommandLine (commandline);
1325 }
1326 }
1327 #endif
1328
1329 #ifdef FEATURE_ALIASES
1330 DestroyAlias ();
1331 #endif
1332
1333 #ifdef FEATURE_DIECTORY_STACK
1334 /* destroy directory stack */
1335 DestroyDirectoryStack ();
1336 #endif
1337
1338 #ifdef INCLUDE_CMD_CHDIR
1339 FreeLastPath ();
1340 #endif
1341
1342 #ifdef FEATURE_HISTORY
1343 CleanHistory();
1344 #endif
1345
1346
1347 /* remove ctrl break handler */
1348 RemoveBreakHandler ();
1349 SetConsoleMode( GetStdHandle( STD_INPUT_HANDLE ),
1350 ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_ECHO_INPUT );
1351 }
1352
1353 #ifdef __REACTOS__
1354 #ifdef _UNICODE
1355 PWCHAR * _CommandLineToArgvW(PWCHAR lpCmdLine, int *pNumArgs)
1356 {
1357 PWCHAR * argvw = NULL;
1358 PWCHAR ptr = lpCmdLine;
1359 PWCHAR str;
1360 int len;
1361 int NumArgs;
1362
1363 NumArgs = 0;
1364
1365 while(lpCmdLine && *lpCmdLine)
1366 {
1367 while (iswspace(*lpCmdLine)) lpCmdLine++;
1368 if (*lpCmdLine)
1369 {
1370 if ((NumArgs % 10)==0)
1371 {
1372 PWCHAR * old_argvw = argvw;
1373 argvw = malloc((NumArgs + 10) * sizeof(PWCHAR));
1374 memcpy(argvw, old_argvw, NumArgs * sizeof(PWCHAR));
1375 free(old_argvw);
1376 }
1377 ptr = wcschr(lpCmdLine, L' ');
1378 if (ptr)
1379 {
1380 len = ptr - lpCmdLine;
1381 }
1382 else
1383 {
1384 len = wcslen(lpCmdLine);
1385 }
1386 str = malloc((len + 1) * sizeof(WCHAR));
1387 memcpy(str, lpCmdLine, len * sizeof(WCHAR));
1388 str[len] = 0;
1389 argvw[NumArgs]=str;
1390 NumArgs++;
1391 lpCmdLine = ptr;
1392 }
1393 }
1394 *pNumArgs = NumArgs;
1395 return argvw;
1396 }
1397 #endif
1398 #endif
1399
1400 /*
1401 * main function
1402 */
1403 #ifdef _UNICODE
1404 int main(void)
1405 #else
1406 int main (int argc, char *argv[])
1407 #endif
1408 {
1409 CONSOLE_SCREEN_BUFFER_INFO Info;
1410 INT nExitCode;
1411 #ifdef _UNICODE
1412 PWCHAR * argv;
1413 int argc=0;
1414 #ifdef __REACTOS__
1415 argv = _CommandLineToArgvW(GetCommandLineW(), &argc);
1416 #else
1417 argv = CommandLineToArgvW(GetCommandLineW(), &argc);
1418 #endif
1419 #endif
1420
1421 SetFileApisToOEM();
1422 GetCodePage = 0;
1423 OutCodePage = 0;
1424
1425 hConsole = CreateFile(_T("CONOUT$"), GENERIC_READ|GENERIC_WRITE,
1426 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
1427 OPEN_EXISTING, 0, NULL);
1428 if (GetConsoleScreenBufferInfo(hConsole, &Info) == FALSE)
1429 {
1430 ConOutFormatMessage(GetLastError());
1431 return(1);
1432 }
1433 wColor = Info.wAttributes;
1434 wDefColor = wColor;
1435
1436 GetCodePage = GetConsoleCP();
1437 OutCodePage = GetConsoleOutputCP();
1438
1439 /* check switches on command-line */
1440 Initialize(argc, argv);
1441
1442 /* call prompt routine */
1443 nExitCode = ProcessInput(FALSE);
1444
1445 /* do the cleanup */
1446 Cleanup(argc, argv);
1447
1448 return(nExitCode);
1449 }
1450
1451 /* EOF */