2 * WCMD - Wine-compatible command line interface.
4 * (C) 1999 - 2001 D A Pickles
9 * - Cannot handle parameters in quotes
10 * - Lots of functionality missing from builtins
15 char *inbuilt[] = {"ATTRIB", "CALL", "CD", "CHDIR", "CLS", "COPY", "CTTY",
16 "DATE", "DEL", "DIR", "ECHO", "ERASE", "FOR", "GOTO",
17 "HELP", "IF", "LABEL", "MD", "MKDIR", "MOVE", "PATH", "PAUSE",
18 "PROMPT", "REM", "REN", "RENAME", "RD", "RMDIR", "SET", "SHIFT",
19 "TIME", "TYPE", "VERIFY", "VER", "VOL", "EXIT"};
23 int echo_mode = 1, verify_mode = 0;
24 char nyi[] = "Not Yet Implemented\n\n";
25 char newline[] = "\n";
26 char version_string[] = "WCMD Version 0.17\n\n";
27 char anykey[] = "Press any key to continue: ";
28 char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
29 BATCH_CONTEXT *context = NULL;
31 /*****************************************************************************
32 * Main entry point. This is a console application so we have a main() not a
37 int wine_main (int argc, char *argv[]) {
39 char string[1024], args[MAX_PATH], param[MAX_PATH];
44 args[0] = param[0] = '\0';
46 for (i=1; i<argc; i++) {
47 if (argv[i][0] == '/') {
48 strcat (args, argv[i]);
51 strcat (param, argv[i]);
58 * Allocate a console and set it up.
61 status = FreeConsole ();
62 if (!status) WCMD_print_error();
63 status = AllocConsole();
64 if (!status) WCMD_print_error();
65 SetConsoleMode (GetStdHandle(STD_INPUT_HANDLE), ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT |
66 ENABLE_PROCESSED_INPUT);
69 * Execute any command-line options.
72 if (strstr(args, "/q") != NULL) {
76 if (strstr(args, "/c") != NULL) {
77 WCMD_process_command (param);
81 if (strstr(args, "/k") != NULL) {
82 WCMD_process_command (param);
86 * If there is an AUTOEXEC.BAT file, try to execute it.
89 GetFullPathName ("\\autoexec.bat", sizeof(string), string, NULL);
90 h = CreateFile (string, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
91 if (h != INVALID_HANDLE_VALUE) {
94 WCMD_batch (string, " ");
99 * Loop forever getting commands and executing them.
105 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
107 string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
108 if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
109 if (lstrlen (string) != 0) {
110 if (strchr(string,'|') != NULL) {
114 WCMD_process_command (string);
122 /*****************************************************************************
123 * Process one command. If the command is EXIT this routine does not return.
124 * We will recurse through here executing batch files.
128 void WCMD_process_command (char *command) {
134 HANDLE old_stdin = 0, old_stdout = 0, h;
139 * Expand up environment variables.
142 status = ExpandEnvironmentStrings (command, cmd, sizeof(cmd));
149 * Changing default drive has to be handled as a special case.
152 if ((cmd[1] == ':') && IsCharAlpha (cmd[0]) && (strlen(cmd) == 2)) {
153 status = SetCurrentDirectory (cmd);
154 if (!status) WCMD_print_error ();
158 /* Dont issue newline WCMD_output (newline); @JED*/
161 * Redirect stdin and/or stdout if required.
164 if ((p = strchr(cmd,'<')) != NULL) {
165 h = CreateFile (WCMD_parameter (++p, 0, NULL), GENERIC_READ, 0, NULL, OPEN_EXISTING,
166 FILE_ATTRIBUTE_NORMAL, NULL);
167 if (h == INVALID_HANDLE_VALUE) {
171 old_stdin = GetStdHandle (STD_INPUT_HANDLE);
172 SetStdHandle (STD_INPUT_HANDLE, h);
174 if ((p = strchr(cmd,'>')) != NULL) {
175 h = CreateFile (WCMD_parameter (++p, 0, NULL), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
176 FILE_ATTRIBUTE_NORMAL, NULL);
177 if (h == INVALID_HANDLE_VALUE) {
181 old_stdout = GetStdHandle (STD_OUTPUT_HANDLE);
182 SetStdHandle (STD_OUTPUT_HANDLE, h);
185 if ((p = strchr(cmd,'<')) != NULL) *p = '\0';
188 * Strip leading whitespaces, and a '@' if supplied
190 whichcmd = WCMD_strtrim_leading_spaces(cmd);
191 if (whichcmd[0] == '@') whichcmd++;
194 * Check if the command entered is internal. If it is, pass the rest of the
195 * line down to the command. If not try to run a program.
199 while (IsCharAlphaNumeric(whichcmd[count])) {
202 for (i=0; i<=WCMD_EXIT; i++) {
203 if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
204 whichcmd, count, inbuilt[i], -1) == 2) break;
206 p = WCMD_strtrim_leading_spaces (&whichcmd[count]);
207 WCMD_parse (p, quals, param1, param2);
211 WCMD_setshow_attrib ();
214 WCMD_batch (param1, p, 1);
218 WCMD_setshow_default ();
221 WCMD_clear_screen ();
230 WCMD_setshow_date ();
240 /* Use the unstripped version of the following data - step over the space */
241 /* but only if a parameter follows */
242 if (strlen(&whichcmd[count]) > 0)
243 WCMD_echo(&whichcmd[count+1]);
245 WCMD_echo(&whichcmd[count]);
270 WCMD_setshow_path ();
276 WCMD_setshow_prompt ();
289 WCMD_setshow_env (p);
295 WCMD_setshow_time ();
312 WCMD_run_program (whichcmd);
315 CloseHandle (GetStdHandle (STD_INPUT_HANDLE));
316 SetStdHandle (STD_INPUT_HANDLE, old_stdin);
319 CloseHandle (GetStdHandle (STD_OUTPUT_HANDLE));
320 SetStdHandle (STD_OUTPUT_HANDLE, old_stdout);
324 /******************************************************************************
327 * Execute a command line as an external program. If no extension given then
328 * precedence is given to .BAT files. Must allow recursion.
330 * FIXME: Case sensitivity in suffixes!
333 void WCMD_run_program (char *command) {
336 PROCESS_INFORMATION pe;
342 char filetorun[MAX_PATH];
344 WCMD_parse (command, quals, param1, param2); /* Quick way to get the filename */
345 if (strpbrk (param1, "\\:") == NULL) { /* No explicit path given */
346 if ((strchr (param1, '.') == NULL) || (strstr (param1, ".bat") != NULL)) {
347 if (SearchPath (NULL, param1, ".bat", sizeof(filetorun), filetorun, NULL)) {
348 WCMD_batch (filetorun, command, 0);
353 else { /* Explicit path given */
354 if (strstr (param1, ".bat") != NULL) {
355 WCMD_batch (param1, command, 0);
358 if (strchr (param1, '.') == NULL) {
359 strcpy (filetorun, param1);
360 strcat (filetorun, ".bat");
361 h = CreateFile (filetorun, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
362 if (h != INVALID_HANDLE_VALUE) {
364 WCMD_batch (param1, command, 0);
370 /* No batch file found, assume executable */
372 hinst = FindExecutable (param1, NULL, filetorun);
373 if ((int)hinst < 32) {
377 console = SHGetFileInfo (filetorun, 0, &psfi, sizeof(psfi), SHGFI_EXETYPE);
382 ZeroMemory (&st, sizeof(STARTUPINFO));
383 st.cb = sizeof(STARTUPINFO);
384 status = CreateProcess (NULL, command, NULL, NULL, FALSE,
385 0, NULL, NULL, &st, &pe);
389 if (!HIWORD(console)) WaitForSingleObject (pe.hProcess, INFINITE);
390 GetExitCodeProcess (pe.hProcess, &errorlevel);
391 if (errorlevel == STILL_ACTIVE) errorlevel = 0;
394 /******************************************************************************
397 * Display the prompt on STDout
401 void WCMD_show_prompt () {
404 char out_string[MAX_PATH], curdir[MAX_PATH], prompt_string[MAX_PATH];
407 status = GetEnvironmentVariable ("PROMPT", prompt_string, sizeof(prompt_string));
408 if ((status == 0) || (status > sizeof(prompt_string))) {
409 lstrcpy (prompt_string, "$N$G");
421 switch (toupper(*p)) {
429 GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, NULL, q, MAX_PATH);
442 status = GetCurrentDirectory (sizeof(curdir), curdir);
448 status = GetCurrentDirectory (sizeof(curdir), curdir);
458 GetTimeFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL, q, MAX_PATH);
469 WCMD_output (out_string);
472 /****************************************************************************
475 * Print the message for GetLastError
478 void WCMD_print_error () {
483 error_code = GetLastError ();
484 status = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
485 NULL, error_code, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
487 WCMD_output ("FIXME: Cannot display message for error %d, status %d\n",
488 error_code, GetLastError());
491 WCMD_output (lpMsgBuf);
492 LocalFree ((HLOCAL)lpMsgBuf);
493 WCMD_output (newline);
497 /*******************************************************************
498 * WCMD_parse - parse a command into parameters and qualifiers.
500 * On exit, all qualifiers are concatenated into q, the first string
501 * not beginning with "/" is in p1 and the
502 * second in p2. Any subsequent non-qualifier strings are lost.
503 * Parameters in quotes are handled.
506 void WCMD_parse (char *s, char *q, char *p1, char *p2) {
510 *q = *p1 = *p2 = '\0';
515 while ((*s != '\0') && (*s != ' ') && *s != '/') {
516 *q++ = toupper (*s++);
525 while ((*s != '\0') && (*s != '"')) {
526 if (p == 0) *p1++ = *s++;
527 else if (p == 1) *p2++ = *s++;
530 if (p == 0) *p1 = '\0';
531 if (p == 1) *p2 = '\0';
538 while ((*s != '\0') && (*s != ' ') && (*s != '/')) {
539 if (p == 0) *p1++ = *s++;
540 else if (p == 1) *p2++ = *s++;
543 if (p == 0) *p1 = '\0';
544 if (p == 1) *p2 = '\0';
550 /*******************************************************************
551 * WCMD_output - send output to current standard output device.
555 void WCMD_output (char *format, ...) {
562 vsprintf (string, format, ap);
563 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), string, lstrlen(string), &count, NULL);
567 /*******************************************************************
568 * WCMD_output_asis - send output to current standard output device.
569 * without formatting eg. when message contains '%'
572 void WCMD_output_asis (char *message) {
574 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), message, lstrlen(message), &count, NULL);
579 /***************************************************************************
580 * WCMD_strtrim_leading_spaces
582 * Remove leading spaces from a string. Return a pointer to the first
583 * non-space character. Does not modify the input string
586 char *WCMD_strtrim_leading_spaces (char *string) {
591 while (*ptr == ' ') ptr++;
595 /*************************************************************************
596 * WCMD_strtrim_trailing_spaces
598 * Remove trailing spaces from a string. This routine modifies the input
599 * string by placing a null after the last non-space character
602 void WCMD_strtrim_trailing_spaces (char *string) {
606 ptr = string + lstrlen (string) - 1;
607 while ((*ptr == ' ') && (ptr >= string)) {
613 /*************************************************************************
616 * Handle pipes within a command - the DOS way using temporary files.
619 void WCMD_pipe (char *command) {
622 char temp_path[MAX_PATH], temp_file[MAX_PATH], temp_file2[MAX_PATH], temp_cmd[1024];
624 GetTempPath (sizeof(temp_path), temp_path);
625 GetTempFileName (temp_path, "WCMD", 0, temp_file);
626 p = strchr(command, '|');
628 wsprintf (temp_cmd, "%s > %s", command, temp_file);
629 WCMD_process_command (temp_cmd);
631 while ((p = strchr(command, '|'))) {
633 GetTempFileName (temp_path, "WCMD", 0, temp_file2);
634 wsprintf (temp_cmd, "%s < %s > %s", command, temp_file, temp_file2);
635 WCMD_process_command (temp_cmd);
636 DeleteFile (temp_file);
637 lstrcpy (temp_file, temp_file2);
640 wsprintf (temp_cmd, "%s < %s", command, temp_file);
641 WCMD_process_command (temp_cmd);
642 DeleteFile (temp_file);