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
36 int main (int argc
, char *argv
[]) {
38 char string
[1024], args
[MAX_PATH
], param
[MAX_PATH
];
43 args
[0] = param
[0] = '\0';
45 for (i
=1; i
<argc
; i
++) {
46 if (argv
[i
][0] == '/') {
47 strcat (args
, argv
[i
]);
50 strcat (param
, argv
[i
]);
57 * Allocate a console and set it up.
60 status
= FreeConsole ();
61 if (!status
) WCMD_print_error();
62 status
= AllocConsole();
63 if (!status
) WCMD_print_error();
64 SetConsoleMode (GetStdHandle(STD_INPUT_HANDLE
), ENABLE_LINE_INPUT
| ENABLE_ECHO_INPUT
|
65 ENABLE_PROCESSED_INPUT
);
68 * Execute any command-line options.
71 if (strstr(args
, "/q") != NULL
) {
75 if (strstr(args
, "/c") != NULL
) {
76 WCMD_process_command (param
);
80 if (strstr(args
, "/k") != NULL
) {
81 WCMD_process_command (param
);
85 * If there is an AUTOEXEC.BAT file, try to execute it.
88 GetFullPathName ("\\autoexec.bat", sizeof(string
), string
, NULL
);
89 h
= CreateFile (string
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
90 if (h
!= INVALID_HANDLE_VALUE
) {
93 WCMD_batch (string
, " ");
98 * Loop forever getting commands and executing them.
104 ReadFile (GetStdHandle(STD_INPUT_HANDLE
), string
, sizeof(string
), &count
, NULL
);
106 string
[count
-1] = '\0'; /* ReadFile output is not null-terminated! */
107 if (string
[count
-2] == '\r') string
[count
-2] = '\0'; /* Under Windoze we get CRLF! */
108 if (lstrlen (string
) != 0) {
109 if (strchr(string
,'|') != NULL
) {
113 WCMD_process_command (string
);
121 /*****************************************************************************
122 * Process one command. If the command is EXIT this routine does not return.
123 * We will recurse through here executing batch files.
127 void WCMD_process_command (char *command
) {
133 HANDLE old_stdin
= 0, old_stdout
= 0, h
;
138 * Expand up environment variables.
141 status
= ExpandEnvironmentStrings (command
, cmd
, sizeof(cmd
));
148 * Changing default drive has to be handled as a special case.
151 if ((cmd
[1] == ':') && IsCharAlpha (cmd
[0]) && (strlen(cmd
) == 2)) {
152 status
= SetCurrentDirectory (cmd
);
153 if (!status
) WCMD_print_error ();
157 /* Dont issue newline WCMD_output (newline); @JED*/
160 * Redirect stdin and/or stdout if required.
163 if ((p
= strchr(cmd
,'<')) != NULL
) {
164 h
= CreateFile (WCMD_parameter (++p
, 0, NULL
), GENERIC_READ
, 0, NULL
, OPEN_EXISTING
,
165 FILE_ATTRIBUTE_NORMAL
, NULL
);
166 if (h
== INVALID_HANDLE_VALUE
) {
170 old_stdin
= GetStdHandle (STD_INPUT_HANDLE
);
171 SetStdHandle (STD_INPUT_HANDLE
, h
);
173 if ((p
= strchr(cmd
,'>')) != NULL
) {
174 h
= CreateFile (WCMD_parameter (++p
, 0, NULL
), GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
,
175 FILE_ATTRIBUTE_NORMAL
, NULL
);
176 if (h
== INVALID_HANDLE_VALUE
) {
180 old_stdout
= GetStdHandle (STD_OUTPUT_HANDLE
);
181 SetStdHandle (STD_OUTPUT_HANDLE
, h
);
184 if ((p
= strchr(cmd
,'<')) != NULL
) *p
= '\0';
187 * Strip leading whitespaces, and a '@' if supplied
189 whichcmd
= WCMD_strtrim_leading_spaces(cmd
);
190 if (whichcmd
[0] == '@') whichcmd
++;
193 * Check if the command entered is internal. If it is, pass the rest of the
194 * line down to the command. If not try to run a program.
198 while (IsCharAlphaNumeric(whichcmd
[count
])) {
201 for (i
=0; i
<=WCMD_EXIT
; i
++) {
202 if (CompareString (LOCALE_USER_DEFAULT
, NORM_IGNORECASE
| SORT_STRINGSORT
,
203 whichcmd
, count
, inbuilt
[i
], -1) == 2) break;
205 p
= WCMD_strtrim_leading_spaces (&whichcmd
[count
]);
206 WCMD_parse (p
, quals
, param1
, param2
);
210 WCMD_setshow_attrib ();
213 WCMD_batch (param1
, p
, 1);
217 WCMD_setshow_default ();
220 WCMD_clear_screen ();
229 WCMD_setshow_date ();
239 /* Use the unstripped version of the following data - step over the space */
240 /* but only if a parameter follows */
241 if (strlen(&whichcmd
[count
]) > 0)
242 WCMD_echo(&whichcmd
[count
+1]);
244 WCMD_echo(&whichcmd
[count
]);
269 WCMD_setshow_path ();
275 WCMD_setshow_prompt ();
288 WCMD_setshow_env (p
);
294 WCMD_setshow_time ();
311 WCMD_run_program (whichcmd
);
314 CloseHandle (GetStdHandle (STD_INPUT_HANDLE
));
315 SetStdHandle (STD_INPUT_HANDLE
, old_stdin
);
318 CloseHandle (GetStdHandle (STD_OUTPUT_HANDLE
));
319 SetStdHandle (STD_OUTPUT_HANDLE
, old_stdout
);
323 /******************************************************************************
326 * Execute a command line as an external program. If no extension given then
327 * precedence is given to .BAT files. Must allow recursion.
329 * FIXME: Case sensitivity in suffixes!
332 void WCMD_run_program (char *command
) {
335 PROCESS_INFORMATION pe
;
341 char filetorun
[MAX_PATH
];
343 WCMD_parse (command
, quals
, param1
, param2
); /* Quick way to get the filename */
344 if (strpbrk (param1
, "\\:") == NULL
) { /* No explicit path given */
345 if ((strchr (param1
, '.') == NULL
) || (strstr (param1
, ".bat") != NULL
)) {
346 if (SearchPath (NULL
, param1
, ".bat", sizeof(filetorun
), filetorun
, NULL
)) {
347 WCMD_batch (filetorun
, command
, 0);
352 else { /* Explicit path given */
353 if (strstr (param1
, ".bat") != NULL
) {
354 WCMD_batch (param1
, command
, 0);
357 if (strchr (param1
, '.') == NULL
) {
358 strcpy (filetorun
, param1
);
359 strcat (filetorun
, ".bat");
360 h
= CreateFile (filetorun
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
361 if (h
!= INVALID_HANDLE_VALUE
) {
363 WCMD_batch (param1
, command
, 0);
369 /* No batch file found, assume executable */
371 hinst
= FindExecutable (param1
, NULL
, filetorun
);
372 if ((int)hinst
< 32) {
376 console
= SHGetFileInfo (filetorun
, 0, &psfi
, sizeof(psfi
), SHGFI_EXETYPE
);
381 ZeroMemory (&st
, sizeof(STARTUPINFO
));
382 st
.cb
= sizeof(STARTUPINFO
);
383 status
= CreateProcess (NULL
, command
, NULL
, NULL
, FALSE
,
384 0, NULL
, NULL
, &st
, &pe
);
388 if (!HIWORD(console
)) WaitForSingleObject (pe
.hProcess
, INFINITE
);
389 GetExitCodeProcess (pe
.hProcess
, &errorlevel
);
390 if (errorlevel
== STILL_ACTIVE
) errorlevel
= 0;
393 /******************************************************************************
396 * Display the prompt on STDout
400 void WCMD_show_prompt () {
403 char out_string
[MAX_PATH
], curdir
[MAX_PATH
], prompt_string
[MAX_PATH
];
406 status
= GetEnvironmentVariable ("PROMPT", prompt_string
, sizeof(prompt_string
));
407 if ((status
== 0) || (status
> sizeof(prompt_string
))) {
408 lstrcpy (prompt_string
, "$N$G");
420 switch (toupper(*p
)) {
428 GetDateFormat (LOCALE_USER_DEFAULT
, DATE_SHORTDATE
, NULL
, NULL
, q
, MAX_PATH
);
441 status
= GetCurrentDirectory (sizeof(curdir
), curdir
);
447 status
= GetCurrentDirectory (sizeof(curdir
), curdir
);
457 GetTimeFormat (LOCALE_USER_DEFAULT
, 0, NULL
, NULL
, q
, MAX_PATH
);
468 WCMD_output (out_string
);
471 /****************************************************************************
474 * Print the message for GetLastError
477 void WCMD_print_error () {
482 error_code
= GetLastError ();
483 status
= FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
484 NULL
, error_code
, 0, (LPTSTR
) &lpMsgBuf
, 0, NULL
);
486 WCMD_output ("FIXME: Cannot display message for error %d, status %d\n",
487 error_code
, GetLastError());
490 WCMD_output (lpMsgBuf
);
491 LocalFree ((HLOCAL
)lpMsgBuf
);
492 WCMD_output (newline
);
496 /*******************************************************************
497 * WCMD_parse - parse a command into parameters and qualifiers.
499 * On exit, all qualifiers are concatenated into q, the first string
500 * not beginning with "/" is in p1 and the
501 * second in p2. Any subsequent non-qualifier strings are lost.
502 * Parameters in quotes are handled.
505 void WCMD_parse (char *s
, char *q
, char *p1
, char *p2
) {
509 *q
= *p1
= *p2
= '\0';
514 while ((*s
!= '\0') && (*s
!= ' ') && *s
!= '/') {
515 *q
++ = toupper (*s
++);
524 while ((*s
!= '\0') && (*s
!= '"')) {
525 if (p
== 0) *p1
++ = *s
++;
526 else if (p
== 1) *p2
++ = *s
++;
529 if (p
== 0) *p1
= '\0';
530 if (p
== 1) *p2
= '\0';
537 while ((*s
!= '\0') && (*s
!= ' ') && (*s
!= '/')) {
538 if (p
== 0) *p1
++ = *s
++;
539 else if (p
== 1) *p2
++ = *s
++;
542 if (p
== 0) *p1
= '\0';
543 if (p
== 1) *p2
= '\0';
549 /*******************************************************************
550 * WCMD_output - send output to current standard output device.
554 void WCMD_output (char *format
, ...) {
561 vsprintf (string
, format
, ap
);
562 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE
), string
, lstrlen(string
), &count
, NULL
);
566 /*******************************************************************
567 * WCMD_output_asis - send output to current standard output device.
568 * without formatting eg. when message contains '%'
571 void WCMD_output_asis (char *message
) {
573 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE
), message
, lstrlen(message
), &count
, NULL
);
578 /***************************************************************************
579 * WCMD_strtrim_leading_spaces
581 * Remove leading spaces from a string. Return a pointer to the first
582 * non-space character. Does not modify the input string
585 char *WCMD_strtrim_leading_spaces (char *string
) {
590 while (*ptr
== ' ') ptr
++;
594 /*************************************************************************
595 * WCMD_strtrim_trailing_spaces
597 * Remove trailing spaces from a string. This routine modifies the input
598 * string by placing a null after the last non-space character
601 void WCMD_strtrim_trailing_spaces (char *string
) {
605 ptr
= string
+ lstrlen (string
) - 1;
606 while ((*ptr
== ' ') && (ptr
>= string
)) {
612 /*************************************************************************
615 * Handle pipes within a command - the DOS way using temporary files.
618 void WCMD_pipe (char *command
) {
621 char temp_path
[MAX_PATH
], temp_file
[MAX_PATH
], temp_file2
[MAX_PATH
], temp_cmd
[1024];
623 GetTempPath (sizeof(temp_path
), temp_path
);
624 GetTempFileName (temp_path
, "WCMD", 0, temp_file
);
625 p
= strchr(command
, '|');
627 wsprintf (temp_cmd
, "%s > %s", command
, temp_file
);
628 WCMD_process_command (temp_cmd
);
630 while ((p
= strchr(command
, '|'))) {
632 GetTempFileName (temp_path
, "WCMD", 0, temp_file2
);
633 wsprintf (temp_cmd
, "%s < %s > %s", command
, temp_file
, temp_file2
);
634 WCMD_process_command (temp_cmd
);
635 DeleteFile (temp_file
);
636 lstrcpy (temp_file
, temp_file2
);
639 wsprintf (temp_cmd
, "%s < %s", command
, temp_file
);
640 WCMD_process_command (temp_cmd
);
641 DeleteFile (temp_file
);