2 * WCMD - Wine-compatible command line interface.
4 * Copyright (C) 1999 - 2001 D A Pickles
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * - Cannot handle parameters in quotes
24 * - Lots of functionality missing from builtins
29 char *inbuilt[] = {"ATTRIB", "CALL", "CD", "CHDIR", "CLS", "COPY", "CTTY",
30 "DATE", "DEL", "DIR", "ECHO", "ERASE", "FOR", "GOTO",
31 "HELP", "IF", "LABEL", "MD", "MKDIR", "MOVE", "PATH", "PAUSE",
32 "PROMPT", "REM", "REN", "RENAME", "RD", "RMDIR", "SET", "SHIFT",
33 "TIME", "TITLE", "TYPE", "VERIFY", "VER", "VOL", "EXIT" };
37 int echo_mode = 1, verify_mode = 0;
38 char nyi[] = "Not Yet Implemented\n\n";
39 char newline[] = "\n";
40 char version_string[] = "WCMD Version 0.17\n\n";
41 char anykey[] = "Press any key to continue: ";
42 char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
43 BATCH_CONTEXT *context = NULL;
45 /*****************************************************************************
46 * Main entry point. This is a console application so we have a main() not a
50 int main (int argc, char *argv[]) {
52 char string[1024], args[MAX_PATH], param[MAX_PATH];
57 args[0] = param[0] = '\0';
59 for (i=1; i<argc; i++) {
60 if (argv[i][0] == '/') {
61 strcat (args, argv[i]);
64 strcat (param, argv[i]);
70 /* If we do a "wcmd /c command", we don't want to allocate a new
71 * console since the command returns immediately. Rather, we use
72 * the surrently allocated input and output handles. This allows
73 * us to pipe to and read from the command interpreter.
75 if (strstr(args, "/c") != NULL) {
76 WCMD_process_command (param);
81 * Allocate a console and set it up.
84 status = FreeConsole ();
85 if (!status) WCMD_print_error();
86 status = AllocConsole();
87 if (!status) WCMD_print_error();
88 SetConsoleMode (GetStdHandle(STD_INPUT_HANDLE), ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT |
89 ENABLE_PROCESSED_INPUT);
90 SetConsoleTitle("Wine Command Prompt");
93 * Execute any command-line options.
96 if (strstr(args, "/q") != NULL) {
100 if (strstr(args, "/k") != NULL) {
101 WCMD_process_command (param);
105 * If there is an AUTOEXEC.BAT file, try to execute it.
108 GetFullPathName ("\\autoexec.bat", sizeof(string), string, NULL);
109 h = CreateFile (string, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
110 if (h != INVALID_HANDLE_VALUE) {
113 WCMD_batch (string, " ");
118 * Loop forever getting commands and executing them.
124 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
126 string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
127 if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
128 if (lstrlen (string) != 0) {
129 if (strchr(string,'|') != NULL) {
133 WCMD_process_command (string);
141 /*****************************************************************************
142 * Process one command. If the command is EXIT this routine does not return.
143 * We will recurse through here executing batch files.
147 void WCMD_process_command (char *command) {
153 HANDLE old_stdin = 0, old_stdout = 0, h;
158 * Expand up environment variables.
161 status = ExpandEnvironmentStrings (command, cmd, sizeof(cmd));
168 * Changing default drive has to be handled as a special case.
171 if ((cmd[1] == ':') && IsCharAlpha (cmd[0]) && (strlen(cmd) == 2)) {
172 status = SetCurrentDirectory (cmd);
173 if (!status) WCMD_print_error ();
177 /* Dont issue newline WCMD_output (newline); @JED*/
180 * Redirect stdin and/or stdout if required.
183 if ((p = strchr(cmd,'<')) != NULL) {
184 h = CreateFile (WCMD_parameter (++p, 0, NULL), GENERIC_READ, 0, NULL, OPEN_EXISTING,
185 FILE_ATTRIBUTE_NORMAL, NULL);
186 if (h == INVALID_HANDLE_VALUE) {
190 old_stdin = GetStdHandle (STD_INPUT_HANDLE);
191 SetStdHandle (STD_INPUT_HANDLE, h);
193 if ((p = strchr(cmd,'>')) != NULL) {
194 h = CreateFile (WCMD_parameter (++p, 0, NULL), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
195 FILE_ATTRIBUTE_NORMAL, NULL);
196 if (h == INVALID_HANDLE_VALUE) {
200 old_stdout = GetStdHandle (STD_OUTPUT_HANDLE);
201 SetStdHandle (STD_OUTPUT_HANDLE, h);
204 if ((p = strchr(cmd,'<')) != NULL) *p = '\0';
207 * Strip leading whitespaces, and a '@' if supplied
209 whichcmd = WCMD_strtrim_leading_spaces(cmd);
210 if (whichcmd[0] == '@') whichcmd++;
213 * Check if the command entered is internal. If it is, pass the rest of the
214 * line down to the command. If not try to run a program.
218 while (IsCharAlphaNumeric(whichcmd[count])) {
221 for (i=0; i<=WCMD_EXIT; i++) {
222 if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
223 whichcmd, count, inbuilt[i], -1) == 2) break;
225 p = WCMD_strtrim_leading_spaces (&whichcmd[count]);
226 WCMD_parse (p, quals, param1, param2);
230 WCMD_setshow_attrib ();
233 WCMD_batch (param1, p, 1);
237 WCMD_setshow_default ();
240 WCMD_clear_screen ();
249 WCMD_setshow_date ();
259 /* Use the unstripped version of the following data - step over the space */
260 /* but only if a parameter follows */
261 if (strlen(&whichcmd[count]) > 0)
262 WCMD_echo(&whichcmd[count+1]);
264 WCMD_echo(&whichcmd[count]);
289 WCMD_setshow_path ();
295 WCMD_setshow_prompt ();
308 WCMD_setshow_env (p);
314 WCMD_setshow_time ();
317 if (strlen(&whichcmd[count]) > 0)
318 WCMD_title(&whichcmd[count+1]);
335 WCMD_run_program (whichcmd);
338 CloseHandle (GetStdHandle (STD_INPUT_HANDLE));
339 SetStdHandle (STD_INPUT_HANDLE, old_stdin);
342 CloseHandle (GetStdHandle (STD_OUTPUT_HANDLE));
343 SetStdHandle (STD_OUTPUT_HANDLE, old_stdout);
347 /******************************************************************************
350 * Execute a command line as an external program. If no extension given then
351 * precedence is given to .BAT files. Must allow recursion.
353 * FIXME: Case sensitivity in suffixes!
356 void WCMD_run_program (char *command) {
359 PROCESS_INFORMATION pe;
365 char filetorun[MAX_PATH];
367 WCMD_parse (command, quals, param1, param2); /* Quick way to get the filename */
368 if (!(*param1) && !(*param2))
370 if (strpbrk (param1, "\\:") == NULL) { /* No explicit path given */
371 if ((strchr (param1, '.') == NULL) || (strstr (param1, ".bat") != NULL)) {
372 if (SearchPath (NULL, param1, ".bat", sizeof(filetorun), filetorun, NULL)) {
373 WCMD_batch (filetorun, command, 0);
378 else { /* Explicit path given */
379 if (strstr (param1, ".bat") != NULL) {
380 WCMD_batch (param1, command, 0);
383 if (strchr (param1, '.') == NULL) {
384 strcpy (filetorun, param1);
385 strcat (filetorun, ".bat");
386 h = CreateFile (filetorun, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
387 if (h != INVALID_HANDLE_VALUE) {
389 WCMD_batch (param1, command, 0);
395 /* No batch file found, assume executable */
397 hinst = FindExecutable (param1, NULL, filetorun);
398 if ((int)hinst < 32) {
402 console = SHGetFileInfo (filetorun, 0, &psfi, sizeof(psfi), SHGFI_EXETYPE);
403 ZeroMemory (&st, sizeof(STARTUPINFO));
404 st.cb = sizeof(STARTUPINFO);
405 status = CreateProcess (NULL, command, NULL, NULL, FALSE,
406 0, NULL, NULL, &st, &pe);
411 if (!console) errorlevel = 0;
414 if (!HIWORD(console)) WaitForSingleObject (pe.hProcess, INFINITE);
415 GetExitCodeProcess (pe.hProcess, &errorlevel);
416 if (errorlevel == STILL_ACTIVE) errorlevel = 0;
420 /******************************************************************************
423 * Display the prompt on STDout
427 void WCMD_show_prompt () {
430 char out_string[MAX_PATH], curdir[MAX_PATH], prompt_string[MAX_PATH];
433 status = GetEnvironmentVariable ("PROMPT", prompt_string, sizeof(prompt_string));
434 if ((status == 0) || (status > sizeof(prompt_string))) {
435 lstrcpy (prompt_string, "$N$G");
447 switch (toupper(*p)) {
455 GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, NULL, q, MAX_PATH);
468 status = GetCurrentDirectory (sizeof(curdir), curdir);
474 status = GetCurrentDirectory (sizeof(curdir), curdir);
484 GetTimeFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL, q, MAX_PATH);
495 WCMD_output (out_string);
498 /****************************************************************************
501 * Print the message for GetLastError
504 void WCMD_print_error () {
509 error_code = GetLastError ();
510 status = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
511 NULL, error_code, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
513 WCMD_output ("FIXME: Cannot display message for error %d, status %d\n",
514 error_code, GetLastError());
517 WCMD_output (lpMsgBuf);
518 LocalFree ((HLOCAL)lpMsgBuf);
519 WCMD_output (newline);
523 /*******************************************************************
524 * WCMD_parse - parse a command into parameters and qualifiers.
526 * On exit, all qualifiers are concatenated into q, the first string
527 * not beginning with "/" is in p1 and the
528 * second in p2. Any subsequent non-qualifier strings are lost.
529 * Parameters in quotes are handled.
532 void WCMD_parse (char *s, char *q, char *p1, char *p2) {
536 *q = *p1 = *p2 = '\0';
541 while ((*s != '\0') && (*s != ' ') && *s != '/') {
542 *q++ = toupper (*s++);
551 while ((*s != '\0') && (*s != '"')) {
552 if (p == 0) *p1++ = *s++;
553 else if (p == 1) *p2++ = *s++;
556 if (p == 0) *p1 = '\0';
557 if (p == 1) *p2 = '\0';
564 while ((*s != '\0') && (*s != ' ')) {
565 if (p == 0) *p1++ = *s++;
566 else if (p == 1) *p2++ = *s++;
569 if (p == 0) *p1 = '\0';
570 if (p == 1) *p2 = '\0';
576 /*******************************************************************
577 * WCMD_output - send output to current standard output device.
581 void WCMD_output (char *format, ...) {
588 vsprintf (string, format, ap);
589 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), string, lstrlen(string), &count, NULL);
593 /*******************************************************************
594 * WCMD_output_asis - send output to current standard output device.
595 * without formatting eg. when message contains '%'
598 void WCMD_output_asis (char *message) {
600 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), message, lstrlen(message), &count, NULL);
605 /***************************************************************************
606 * WCMD_strtrim_leading_spaces
608 * Remove leading spaces from a string. Return a pointer to the first
609 * non-space character. Does not modify the input string
612 char *WCMD_strtrim_leading_spaces (char *string) {
617 while (*ptr == ' ') ptr++;
621 /*************************************************************************
622 * WCMD_strtrim_trailing_spaces
624 * Remove trailing spaces from a string. This routine modifies the input
625 * string by placing a null after the last non-space character
628 void WCMD_strtrim_trailing_spaces (char *string) {
632 ptr = string + lstrlen (string) - 1;
633 while ((*ptr == ' ') && (ptr >= string)) {
639 /*************************************************************************
642 * Handle pipes within a command - the DOS way using temporary files.
645 void WCMD_pipe (char *command) {
648 char temp_path[MAX_PATH], temp_file[MAX_PATH], temp_file2[MAX_PATH], temp_cmd[1024];
650 GetTempPath (sizeof(temp_path), temp_path);
651 GetTempFileName (temp_path, "WCMD", 0, temp_file);
652 p = strchr(command, '|');
654 wsprintf (temp_cmd, "%s > %s", command, temp_file);
655 WCMD_process_command (temp_cmd);
657 while ((p = strchr(command, '|'))) {
659 GetTempFileName (temp_path, "WCMD", 0, temp_file2);
660 wsprintf (temp_cmd, "%s < %s > %s", command, temp_file, temp_file2);
661 WCMD_process_command (temp_cmd);
662 DeleteFile (temp_file);
663 lstrcpy (temp_file, temp_file2);
666 wsprintf (temp_cmd, "%s < %s", command, temp_file);
667 WCMD_process_command (temp_cmd);
668 DeleteFile (temp_file);