1 /* $Id: shell.c,v 1.38 2000/05/01 14:15:01 ea Exp $
3 * PROJECT : ReactOS Operating System
4 * DESCRIPTION: ReactOS' Native Shell
5 * LICENSE : See top level directory
18 HANDLE InputHandle
, OutputHandle
;
22 void debug_printf(char* fmt
, ...)
28 vsprintf(buffer
,fmt
,args
);
30 WriteConsoleA(OutputHandle
, buffer
, strlen(buffer
), NULL
, NULL
);
37 "Reactos Simple Shell\n(compiled on %s, at %s)\n",
43 void ExecuteCd(char* cmdline
)
45 if (!SetCurrentDirectoryA(cmdline
))
47 debug_printf("Invalid directory\n");
51 void ExecuteMd (char *cmdline
)
53 if (!CreateDirectoryA (cmdline
, NULL
))
55 debug_printf("Create Directory failed!\n");
59 void ExecuteDir(char* cmdline
)
62 WIN32_FIND_DATA FindData
;
67 shandle
= FindFirstFile("*",&FindData
);
69 if (shandle
==INVALID_HANDLE_VALUE
)
71 debug_printf("File not found\n\n");
76 debug_printf("%-15.15s",FindData
.cAlternateFileName
);
77 if(FindData
.dwFileAttributes
&FILE_ATTRIBUTE_DIRECTORY
)
78 debug_printf("<DIR> "),nRep
++;
80 debug_printf(" %10d ",FindData
.nFileSizeLow
),nFile
++;
82 FileTimeToLocalFileTime(&FindData
.ftLastWriteTime
,&fTime
);
83 FileTimeToSystemTime(&fTime
, &sTime
);
84 debug_printf("%02d/%02d/%04d %02d:%02d:%02d "
85 ,sTime
.wMonth
,sTime
.wDay
,sTime
.wYear
86 ,sTime
.wHour
,sTime
.wMinute
,sTime
.wSecond
);
88 debug_printf("%s\n",FindData
.cFileName
);
89 } while(FindNextFile(shandle
,&FindData
));
90 debug_printf("\n %d files\n %d directories\n\n",nFile
,nRep
);
95 void ExecuteReboot(char* cmdline
)
97 NtShutdownSystem (ShutdownReboot
);
101 void ExecuteType(char* cmdline
)
107 FileHandle
= CreateFile(cmdline
,
114 if (FileHandle
== NULL
)
116 debug_printf("Unknown file\n");
119 while (ReadFile(FileHandle
,
125 debug_printf("%c",c
);
128 CloseHandle(FileHandle
);
131 int ExecuteProcess(char* name
, char* cmdline
, BOOL detached
)
133 PROCESS_INFORMATION ProcessInformation
;
134 STARTUPINFO StartupInfo
;
137 memset(&StartupInfo
, 0, sizeof(StartupInfo
));
138 StartupInfo
.cb
= sizeof (STARTUPINFO
);
139 StartupInfo
.lpTitle
= name
;
141 ret
= CreateProcessA(name
,
155 if (TRUE
== detached
)
159 debug_printf("%s detached:\n"
160 "\thProcess = %08X\n"
165 ProcessInformation
.hProcess
,
166 ProcessInformation
.hThread
,
167 ProcessInformation
.dwProcessId
,
168 ProcessInformation
.dwThreadId
);
169 CloseHandle(ProcessInformation
.hProcess
);
170 CloseHandle(ProcessInformation
.hThread
);
174 debug_printf("Could not detach %s\n", name
);
181 // debug_printf("ProcessInformation.hThread %x\n",
182 // ProcessInformation.hThread);
183 WaitForSingleObject(ProcessInformation
.hProcess
, INFINITE
);
184 CloseHandle(ProcessInformation
.hProcess
);
185 // debug_printf("Thandle %x\n", ProcessInformation.hThread);
186 CloseHandle(ProcessInformation
.hThread
);
192 void ExecuteStart(char* CommandLine
)
194 char *ImageName
= CommandLine
;
198 && (*CommandLine
!= ' ')
199 && (*CommandLine
!= '\t')
203 *CommandLine
++ = '\0';
204 while ( (*CommandLine
)
205 && ( (*CommandLine
== ' ')
206 || (*CommandLine
== '\t')
218 ExecuteKill(char * lpPid
)
223 dwProcessId
= (DWORD
) atol(lpPid
);
224 debug_printf("dwProcessId %d\n",dwProcessId
);
225 hProcess
= OpenProcess(
230 if (NULL
== hProcess
)
233 "Could not open the process with PID = %d\n",
238 if (FALSE
== TerminateProcess(
244 "Could not terminate the process with PID = %d\n",
248 CloseHandle(hProcess
);
252 void ExecuteHelp (void * dummy
)
254 debug_printf ("A:\t\t\tCurrent drive is A:\n");
255 debug_printf ("C:\t\t\tCurrent drive is C:\n");
256 debug_printf ("cd [directory]\t\tChange current directory\n");
257 debug_printf ("dir [directory]\t\tList directory\n");
258 debug_printf ("exit\t\t\tTerminate the shell\n");
259 debug_printf ("help\t\t\tPrint this help message\n");
260 debug_printf ("kill [pid]\t\tKill process which PID is pid\n");
261 debug_printf ("md [directory]\t\tCreate a new directory\n");
262 debug_printf ("reboot\t\t\tRestart the system\n");
263 debug_printf ("start [program.exe]\tDetach program.exe\n");
264 debug_printf ("type [file]\t\tPrint the file on console\n");
265 debug_printf ("validate\t\tValidate the process' heap\n");
266 debug_printf ("ver\t\t\tPrint version information\n");
267 debug_printf ("[program.exe]\t\tStart synchronously program.exe\n\n");
270 void ExecuteCommand(char* line
)
275 if (isalpha(line
[0]) && line
[1] == ':' && line
[2] == 0)
277 char szPath
[MAX_PATH
];
280 /* save curent directory in environment variable */
281 GetCurrentDirectory (MAX_PATH
, szPath
);
283 strcpy (szVar
, "=A:");
284 szVar
[1] = toupper (szPath
[0]);
285 SetEnvironmentVariable (szVar
, szPath
);
287 /* check for current directory of new drive */
288 strcpy (szVar
, "=A:");
289 szVar
[1] = toupper (line
[0]);
290 if (GetEnvironmentVariable (szVar
, szPath
, MAX_PATH
) == 0)
292 /* no environment variable found */
293 strcpy (szPath
, "A:\\");
294 szPath
[0] = toupper (line
[0]);
297 /* set new current directory */
298 SetCurrentDirectory (szPath
);
299 GetCurrentDirectory (MAX_PATH
, szPath
);
300 if (szPath
[0] != (char)toupper (line
[0]))
301 debug_printf("Invalid drive\n");
307 while ((*tail
)!=' ' && (*tail
)!=0)
319 if (cmd
==NULL
|| *cmd
== '\0' )
323 if (strcmp(cmd
,"cd")==0)
328 if (strcmp(cmd
,"dir")==0)
333 if (strcmp(cmd
,"kill")==0)
338 if (strcmp(cmd
,"md")==0)
343 if (strcmp(cmd
,"reboot")==0)
348 if (strcmp(cmd
,"type")==0)
353 if (strcmp(cmd
,"ver")==0)
358 if (strcmp(cmd
,"validate")==0)
360 debug_printf("Validating heap...");
361 if (HeapValidate(GetProcessHeap(),0,NULL
))
363 debug_printf("succeeded\n");
367 debug_printf("failed\n");
371 if (strcmp(cmd
,"start") == 0)
376 if ((strcmp(cmd
,"help") * strcmp(cmd
,"?")) == 0)
381 if (strcmp(cmd
,"exit")==0)
383 if (bCanExit
== TRUE
)
389 if (ExecuteProcess(cmd
,tail
,FALSE
))
393 debug_printf("Unknown command '%s'\n", cmd
);
396 void ReadLine(char* line
)
399 UCHAR CurrentDir
[255];
403 GetCurrentDirectoryA(255,CurrentDir
);
404 debug_printf("%s>", CurrentDir
);
408 if (!ReadConsoleA(InputHandle
,
414 debug_printf("Failed to read from console\n");
422 debug_printf("\b \b");
430 debug_printf( "%c", ch
);
434 } while (ch
!= '\n');
439 void ParseCommandLine (void)
441 char *pszCommandLine
;
443 pszCommandLine
= GetCommandLineA ();
444 _strupr (pszCommandLine
);
445 if (strstr (pszCommandLine
, "/P") != NULL
)
447 debug_printf("Permanent shell\n");
454 static char line
[255];
457 InputHandle
= GetStdHandle(STD_INPUT_HANDLE
);
458 OutputHandle
= GetStdHandle(STD_OUTPUT_HANDLE
);
460 debug_printf("Shell Starting...\n");
467 ExecuteCommand(line
);