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
]);
71 * Allocate a console and set it up.
74 status
= FreeConsole ();
75 if (!status
) WCMD_print_error();
76 status
= AllocConsole();
77 if (!status
) WCMD_print_error();
78 SetConsoleMode (GetStdHandle(STD_INPUT_HANDLE
), ENABLE_LINE_INPUT
| ENABLE_ECHO_INPUT
|
79 ENABLE_PROCESSED_INPUT
);
80 SetConsoleTitle("Wine Command Prompt");
83 * Execute any command-line options.
86 if (strstr(args
, "/q") != NULL
) {
90 if (strstr(args
, "/c") != NULL
) {
91 WCMD_process_command (param
);
95 if (strstr(args
, "/k") != NULL
) {
96 WCMD_process_command (param
);
100 * If there is an AUTOEXEC.BAT file, try to execute it.
103 GetFullPathName ("\\autoexec.bat", sizeof(string
), string
, NULL
);
104 h
= CreateFile (string
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
105 if (h
!= INVALID_HANDLE_VALUE
) {
108 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 if (strchr(string
,'|') != NULL
) {
128 WCMD_process_command (string
);
136 /*****************************************************************************
137 * Process one command. If the command is EXIT this routine does not return.
138 * We will recurse through here executing batch files.
142 void WCMD_process_command (char *command
) {
148 HANDLE old_stdin
= 0, old_stdout
= 0, h
;
153 * Expand up environment variables.
156 status
= ExpandEnvironmentStrings (command
, cmd
, sizeof(cmd
));
163 * Changing default drive has to be handled as a special case.
166 if ((cmd
[1] == ':') && IsCharAlpha (cmd
[0]) && (strlen(cmd
) == 2)) {
167 status
= SetCurrentDirectory (cmd
);
168 if (!status
) WCMD_print_error ();
172 /* Dont issue newline WCMD_output (newline); @JED*/
175 * Redirect stdin and/or stdout if required.
178 if ((p
= strchr(cmd
,'<')) != NULL
) {
179 h
= CreateFile (WCMD_parameter (++p
, 0, NULL
), GENERIC_READ
, 0, NULL
, OPEN_EXISTING
,
180 FILE_ATTRIBUTE_NORMAL
, NULL
);
181 if (h
== INVALID_HANDLE_VALUE
) {
185 old_stdin
= GetStdHandle (STD_INPUT_HANDLE
);
186 SetStdHandle (STD_INPUT_HANDLE
, h
);
188 if ((p
= strchr(cmd
,'>')) != NULL
) {
189 h
= CreateFile (WCMD_parameter (++p
, 0, NULL
), GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
,
190 FILE_ATTRIBUTE_NORMAL
, NULL
);
191 if (h
== INVALID_HANDLE_VALUE
) {
195 old_stdout
= GetStdHandle (STD_OUTPUT_HANDLE
);
196 SetStdHandle (STD_OUTPUT_HANDLE
, h
);
199 if ((p
= strchr(cmd
,'<')) != NULL
) *p
= '\0';
202 * Strip leading whitespaces, and a '@' if supplied
204 whichcmd
= WCMD_strtrim_leading_spaces(cmd
);
205 if (whichcmd
[0] == '@') whichcmd
++;
208 * Check if the command entered is internal. If it is, pass the rest of the
209 * line down to the command. If not try to run a program.
213 while (IsCharAlphaNumeric(whichcmd
[count
])) {
216 for (i
=0; i
<=WCMD_EXIT
; i
++) {
217 if (CompareString (LOCALE_USER_DEFAULT
, NORM_IGNORECASE
| SORT_STRINGSORT
,
218 whichcmd
, count
, inbuilt
[i
], -1) == 2) break;
220 p
= WCMD_strtrim_leading_spaces (&whichcmd
[count
]);
221 WCMD_parse (p
, quals
, param1
, param2
);
225 WCMD_setshow_attrib ();
228 WCMD_batch (param1
, p
, 1);
232 WCMD_setshow_default ();
235 WCMD_clear_screen ();
244 WCMD_setshow_date ();
254 /* Use the unstripped version of the following data - step over the space */
255 /* but only if a parameter follows */
256 if (strlen(&whichcmd
[count
]) > 0)
257 WCMD_echo(&whichcmd
[count
+1]);
259 WCMD_echo(&whichcmd
[count
]);
284 WCMD_setshow_path ();
290 WCMD_setshow_prompt ();
303 WCMD_setshow_env (p
);
309 WCMD_setshow_time ();
312 if (strlen(&whichcmd
[count
]) > 0)
313 WCMD_title(&whichcmd
[count
+1]);
330 WCMD_run_program (whichcmd
);
333 CloseHandle (GetStdHandle (STD_INPUT_HANDLE
));
334 SetStdHandle (STD_INPUT_HANDLE
, old_stdin
);
337 CloseHandle (GetStdHandle (STD_OUTPUT_HANDLE
));
338 SetStdHandle (STD_OUTPUT_HANDLE
, old_stdout
);
342 /******************************************************************************
345 * Execute a command line as an external program. If no extension given then
346 * precedence is given to .BAT files. Must allow recursion.
348 * FIXME: Case sensitivity in suffixes!
351 void WCMD_run_program (char *command
) {
354 PROCESS_INFORMATION pe
;
360 char filetorun
[MAX_PATH
];
362 WCMD_parse (command
, quals
, param1
, param2
); /* Quick way to get the filename */
363 if (!(*param1
) && !(*param2
))
365 if (strpbrk (param1
, "\\:") == NULL
) { /* No explicit path given */
366 if ((strchr (param1
, '.') == NULL
) || (strstr (param1
, ".bat") != NULL
)) {
367 if (SearchPath (NULL
, param1
, ".bat", sizeof(filetorun
), filetorun
, NULL
)) {
368 WCMD_batch (filetorun
, command
, 0);
373 else { /* Explicit path given */
374 if (strstr (param1
, ".bat") != NULL
) {
375 WCMD_batch (param1
, command
, 0);
378 if (strchr (param1
, '.') == NULL
) {
379 strcpy (filetorun
, param1
);
380 strcat (filetorun
, ".bat");
381 h
= CreateFile (filetorun
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
382 if (h
!= INVALID_HANDLE_VALUE
) {
384 WCMD_batch (param1
, command
, 0);
390 /* No batch file found, assume executable */
392 hinst
= FindExecutable (param1
, NULL
, filetorun
);
393 if ((int)hinst
< 32) {
397 console
= SHGetFileInfo (filetorun
, 0, &psfi
, sizeof(psfi
), SHGFI_EXETYPE
);
402 ZeroMemory (&st
, sizeof(STARTUPINFO
));
403 st
.cb
= sizeof(STARTUPINFO
);
404 status
= CreateProcess (NULL
, command
, NULL
, NULL
, FALSE
,
405 0, NULL
, NULL
, &st
, &pe
);
409 if (!HIWORD(console
)) WaitForSingleObject (pe
.hProcess
, INFINITE
);
410 GetExitCodeProcess (pe
.hProcess
, &errorlevel
);
411 if (errorlevel
== STILL_ACTIVE
) errorlevel
= 0;
414 /******************************************************************************
417 * Display the prompt on STDout
421 void WCMD_show_prompt () {
424 char out_string
[MAX_PATH
], curdir
[MAX_PATH
], prompt_string
[MAX_PATH
];
427 status
= GetEnvironmentVariable ("PROMPT", prompt_string
, sizeof(prompt_string
));
428 if ((status
== 0) || (status
> sizeof(prompt_string
))) {
429 lstrcpy (prompt_string
, "$N$G");
441 switch (toupper(*p
)) {
449 GetDateFormat (LOCALE_USER_DEFAULT
, DATE_SHORTDATE
, NULL
, NULL
, q
, MAX_PATH
);
462 status
= GetCurrentDirectory (sizeof(curdir
), curdir
);
468 status
= GetCurrentDirectory (sizeof(curdir
), curdir
);
478 GetTimeFormat (LOCALE_USER_DEFAULT
, 0, NULL
, NULL
, q
, MAX_PATH
);
489 WCMD_output (out_string
);
492 /****************************************************************************
495 * Print the message for GetLastError
498 void WCMD_print_error () {
503 error_code
= GetLastError ();
504 status
= FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
505 NULL
, error_code
, 0, (LPTSTR
) &lpMsgBuf
, 0, NULL
);
507 WCMD_output ("FIXME: Cannot display message for error %d, status %d\n",
508 error_code
, GetLastError());
511 WCMD_output (lpMsgBuf
);
512 LocalFree ((HLOCAL
)lpMsgBuf
);
513 WCMD_output (newline
);
517 /*******************************************************************
518 * WCMD_parse - parse a command into parameters and qualifiers.
520 * On exit, all qualifiers are concatenated into q, the first string
521 * not beginning with "/" is in p1 and the
522 * second in p2. Any subsequent non-qualifier strings are lost.
523 * Parameters in quotes are handled.
526 void WCMD_parse (char *s
, char *q
, char *p1
, char *p2
) {
530 *q
= *p1
= *p2
= '\0';
535 while ((*s
!= '\0') && (*s
!= ' ') && *s
!= '/') {
536 *q
++ = toupper (*s
++);
545 while ((*s
!= '\0') && (*s
!= '"')) {
546 if (p
== 0) *p1
++ = *s
++;
547 else if (p
== 1) *p2
++ = *s
++;
550 if (p
== 0) *p1
= '\0';
551 if (p
== 1) *p2
= '\0';
558 while ((*s
!= '\0') && (*s
!= ' ')) {
559 if (p
== 0) *p1
++ = *s
++;
560 else if (p
== 1) *p2
++ = *s
++;
563 if (p
== 0) *p1
= '\0';
564 if (p
== 1) *p2
= '\0';
570 /*******************************************************************
571 * WCMD_output - send output to current standard output device.
575 void WCMD_output (char *format
, ...) {
582 vsprintf (string
, format
, ap
);
583 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE
), string
, lstrlen(string
), &count
, NULL
);
587 /*******************************************************************
588 * WCMD_output_asis - send output to current standard output device.
589 * without formatting eg. when message contains '%'
592 void WCMD_output_asis (char *message
) {
594 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE
), message
, lstrlen(message
), &count
, NULL
);
599 /***************************************************************************
600 * WCMD_strtrim_leading_spaces
602 * Remove leading spaces from a string. Return a pointer to the first
603 * non-space character. Does not modify the input string
606 char *WCMD_strtrim_leading_spaces (char *string
) {
611 while (*ptr
== ' ') ptr
++;
615 /*************************************************************************
616 * WCMD_strtrim_trailing_spaces
618 * Remove trailing spaces from a string. This routine modifies the input
619 * string by placing a null after the last non-space character
622 void WCMD_strtrim_trailing_spaces (char *string
) {
626 ptr
= string
+ lstrlen (string
) - 1;
627 while ((*ptr
== ' ') && (ptr
>= string
)) {
633 /*************************************************************************
636 * Handle pipes within a command - the DOS way using temporary files.
639 void WCMD_pipe (char *command
) {
642 char temp_path
[MAX_PATH
], temp_file
[MAX_PATH
], temp_file2
[MAX_PATH
], temp_cmd
[1024];
644 GetTempPath (sizeof(temp_path
), temp_path
);
645 GetTempFileName (temp_path
, "WCMD", 0, temp_file
);
646 p
= strchr(command
, '|');
648 wsprintf (temp_cmd
, "%s > %s", command
, temp_file
);
649 WCMD_process_command (temp_cmd
);
651 while ((p
= strchr(command
, '|'))) {
653 GetTempFileName (temp_path
, "WCMD", 0, temp_file2
);
654 wsprintf (temp_cmd
, "%s < %s > %s", command
, temp_file
, temp_file2
);
655 WCMD_process_command (temp_cmd
);
656 DeleteFile (temp_file
);
657 lstrcpy (temp_file
, temp_file2
);
660 wsprintf (temp_cmd
, "%s < %s", command
, temp_file
);
661 WCMD_process_command (temp_cmd
);
662 DeleteFile (temp_file
);