2 * WCMD - Wine-compatible command line interface.
9 * - No support for pipes
10 * - 32-bit limit on file sizes in DIR command
11 * - Cannot handle parameters in quotes
12 * - Lots of functionality missing from builtins
18 /* external declaration here because we don't want to depend on Wine headers */
20 extern "C" HINSTANCE MAIN_WinelibInit( int *argc, char *argv[] );
22 extern HINSTANCE MAIN_WinelibInit( int *argc, char *argv[] );
26 char *inbuilt[] = {"ATTRIB", "CALL", "CD", "CHDIR", "CLS", "COPY", "CTTY",
27 "DATE", "DEL", "DIR", "ECHO", "ERASE", "FOR", "GOTO",
28 "HELP", "IF", "LABEL", "MD", "MKDIR", "MOVE", "PATH", "PAUSE",
29 "PROMPT", "REM", "REN", "RENAME", "RD", "RMDIR", "SET", "SHIFT",
30 "TIME", "TYPE", "VERIFY", "VER", "VOL", "EXIT"};
33 int echo_mode = 1, verify_mode = 0;
34 char nyi[] = "Not Yet Implemented\n\n";
35 char newline[] = "\n";
36 char version_string[] = "WCMD Version 0.12\n\n";
37 char anykey[] = "Press any key to continue: ";
38 char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
39 BATCH_CONTEXT *context = NULL;
41 /*****************************************************************************
42 * Main entry point. This is a console application so we have a main() not a
47 int main (int argc, char *argv[]) {
49 char string[1024], args[MAX_PATH], param[MAX_PATH];
55 if (!(hinst = MAIN_WinelibInit( &argc, argv ))) return 0;
60 args[0] = param[0] = '\0';
62 for (i=1; i<argc; i++) {
63 if (argv[i][0] == '/') {
64 strcat (args, argv[i]);
67 strcat (param, argv[i]);
74 * Allocate a console and set it up.
77 status = FreeConsole ();
78 if (!status) WCMD_print_error();
79 status = AllocConsole();
80 if (!status) WCMD_print_error();
81 SetConsoleMode (GetStdHandle(STD_INPUT_HANDLE), ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT |
82 ENABLE_PROCESSED_INPUT);
85 * Execute any command-line options.
88 if (strstr(args, "/q") != NULL) {
92 if (strstr(args, "/c") != NULL) {
93 WCMD_process_command (param);
97 if (strstr(args, "/k") != NULL) {
98 WCMD_process_command (param);
102 * If there is an AUTOEXEC.BAT file, try to execute it.
105 GetFullPathName ("\\autoexec.bat", sizeof(string), string, NULL);
106 h = CreateFile (string, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
107 if (h != INVALID_HANDLE_VALUE) {
109 // WCMD_batch (string, " ");
113 * Loop forever getting commands and executing them.
119 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
121 string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
122 if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
123 if (lstrlen (string) != 0) {
124 WCMD_process_command (string);
131 /*****************************************************************************
132 * Process one command. If the command is EXIT this routine does not return.
133 * We will recurse through here executing batch files.
137 void WCMD_process_command (char *command) {
143 HANDLE old_stdin = 0, old_stdout = 0, h;
146 * Throw away constructs we don't support yet
149 if (strchr(command,'|') != NULL) {
150 WCMD_output ("Pipes not yet implemented\n");
155 * Expand up environment variables.
158 status = ExpandEnvironmentStrings (command, cmd, sizeof(cmd));
165 * Changing default drive has to be handled as a special case.
168 if ((cmd[1] == ':') && IsCharAlpha (cmd[0]) && (strlen(cmd) == 2)) {
169 status = SetCurrentDirectory (cmd);
170 if (!status) WCMD_print_error ();
173 WCMD_output (newline);
176 * Redirect stdin and/or stdout if required.
179 if ((p = strchr(cmd,'<')) != NULL) {
180 h = CreateFile (WCMD_parameter (++p, 0, NULL), GENERIC_READ, 0, NULL, OPEN_EXISTING,
181 FILE_ATTRIBUTE_NORMAL, 0);
182 if (h == INVALID_HANDLE_VALUE) {
186 old_stdin = GetStdHandle (STD_INPUT_HANDLE);
187 SetStdHandle (STD_INPUT_HANDLE, h);
189 if ((p = strchr(cmd,'>')) != NULL) {
190 h = CreateFile (WCMD_parameter (++p, 0, NULL), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
191 FILE_ATTRIBUTE_NORMAL, 0);
192 if (h == INVALID_HANDLE_VALUE) {
196 old_stdout = GetStdHandle (STD_OUTPUT_HANDLE);
197 SetStdHandle (STD_OUTPUT_HANDLE, h);
200 if ((p = strchr(cmd,'<')) != NULL) *p = '\0';
203 * Check if the command entered is internal. If it is, pass the rest of the
204 * line down to the command. If not try to run a program.
208 while (IsCharAlphaNumeric(cmd[count])) {
211 for (i=0; i<=WCMD_EXIT; i++) {
212 if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
213 cmd, count, inbuilt[i], -1) == 2) break;
215 p = WCMD_strtrim_leading_spaces (&cmd[count]);
216 WCMD_parse (p, quals, param1, param2);
220 WCMD_setshow_attrib ();
223 WCMD_batch (param1, p, 1);
227 WCMD_setshow_default ();
230 WCMD_clear_screen ();
239 WCMD_setshow_date ();
274 WCMD_setshow_path ();
280 WCMD_setshow_prompt ();
293 WCMD_setshow_env (p);
299 WCMD_setshow_time ();
316 WCMD_run_program (cmd);
319 CloseHandle (GetStdHandle (STD_INPUT_HANDLE));
320 SetStdHandle (STD_INPUT_HANDLE, old_stdin);
323 CloseHandle (GetStdHandle (STD_OUTPUT_HANDLE));
324 SetStdHandle (STD_OUTPUT_HANDLE, old_stdout);
328 /******************************************************************************
331 * Execute a command line as an external program. If no extension given then
332 * precedence is given to .BAT files. Must allow recursion.
334 * FIXME: Case sensitivity in suffixes!
337 void WCMD_run_program (char *command) {
340 PROCESS_INFORMATION pe;
343 char filetorun[MAX_PATH];
345 WCMD_parse (command, quals, param1, param2); /* Quick way to get the filename */
346 if (strpbrk (param1, "\\:") == NULL) { /* No explicit path given */
347 if ((strchr (param1, '.') == NULL) || (strstr (param1, ".bat") != NULL)) {
348 if (SearchPath (NULL, param1, ".bat", sizeof(filetorun), filetorun, NULL)) {
349 WCMD_batch (filetorun, command, 0);
354 else { /* Explicit path given */
355 if (strstr (param1, ".bat") != NULL) {
356 WCMD_batch (param1, command, 0);
359 if (strchr (param1, '.') == NULL) {
360 strcpy (filetorun, param1);
361 strcat (filetorun, ".bat");
362 h = CreateFile (filetorun, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
363 if (h != INVALID_HANDLE_VALUE) {
365 WCMD_batch (param1, command, 0);
371 /* No batch file found, assume executable */
373 ZeroMemory (&st, sizeof(STARTUPINFO));
374 st.cb = sizeof(STARTUPINFO);
375 status = CreateProcess (NULL, command, NULL, NULL, FALSE,
376 0, NULL, NULL, &st, &pe);
382 /******************************************************************************
385 * Display the prompt on STDout
389 void WCMD_show_prompt () {
392 char out_string[MAX_PATH], curdir[MAX_PATH], prompt_string[MAX_PATH];
395 status = GetEnvironmentVariable ("PROMPT", prompt_string, sizeof(prompt_string));
396 if ((status == 0) || (status > sizeof(prompt_string))) {
397 lstrcpy (prompt_string, "$N$G");
409 switch (toupper(*p)) {
417 GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, NULL, q, MAX_PATH);
430 status = GetCurrentDirectory (sizeof(curdir), curdir);
436 status = GetCurrentDirectory (sizeof(curdir), curdir);
446 GetTimeFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL, q, MAX_PATH);
457 WCMD_output (out_string);
460 /****************************************************************************
463 * Print the message for GetLastError - not much use yet as Wine doesn't have
464 * the messages available, so we show meaningful messages for the most likely.
467 void WCMD_print_error () {
471 error_code = GetLastError ();
472 switch (error_code) {
473 case ERROR_FILE_NOT_FOUND:
474 WCMD_output ("File Not Found\n");
476 case ERROR_PATH_NOT_FOUND:
477 WCMD_output ("Path Not Found\n");
480 FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
482 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
483 (LPTSTR) &lpMsgBuf, 0, NULL);
484 WCMD_output (lpMsgBuf);
485 LocalFree ((HLOCAL)lpMsgBuf);
490 /*******************************************************************
491 * WCMD_parse - parse a command into parameters and qualifiers.
493 * On exit, all qualifiers are concatenated into q, the first string
494 * not beginning with "/" is in p1 and the
495 * second in p2. Any subsequent non-qualifier strings are lost.
496 * Parameters in quotes are handled.
499 void WCMD_parse (char *s, char *q, char *p1, char *p2) {
503 *q = *p1 = *p2 = '\0';
508 while ((*s != '\0') && (*s != ' ') && *s != '/') {
509 *q++ = toupper (*s++);
518 while ((*s != '\0') && (*s != '"')) {
519 if (p == 0) *p1++ = *s++;
520 else if (p == 1) *p2++ = *s++;
523 if (p == 0) *p1 = '\0';
524 if (p == 1) *p2 = '\0';
531 while ((*s != '\0') && (*s != ' ') && (*s != '/')) {
532 if (p == 0) *p1++ = *s++;
533 else if (p == 1) *p2++ = *s++;
536 if (p == 0) *p1 = '\0';
537 if (p == 1) *p2 = '\0';
543 /*******************************************************************
544 * WCMD_output - send output to current standard output device.
548 void WCMD_output (char *format, ...) {
555 vsprintf (string, format, ap);
556 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), string, lstrlen(string), &count, NULL);
561 /* Remove leading spaces from a string. Return a pointer to the first
562 * non-space character. Does not modify the input string
565 char *WCMD_strtrim_leading_spaces (char *string) {
570 while (*ptr == ' ') ptr++;
574 /* Remove trailing spaces from a string. This routine modifies the input
575 * string by placing a null after the last non-space character
578 void WCMD_strtrim_trailing_spaces (char *string) {
582 ptr = string + lstrlen (string) - 1;
583 while ((*ptr == ' ') && (ptr >= string)) {