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
17 char *inbuilt
[] = {"ATTRIB", "CALL", "CD", "CHDIR", "CLS", "COPY", "CTTY",
18 "DATE", "DEL", "DIR", "ECHO", "ERASE", "FOR", "GOTO",
19 "HELP", "IF", "LABEL", "MD", "MKDIR", "MOVE", "PATH", "PAUSE",
20 "PROMPT", "REM", "REN", "RENAME", "RD", "RMDIR", "SET", "SHIFT",
21 "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.12\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
, 0);
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 WCMD_process_command (string
);
117 /*****************************************************************************
118 * Process one command. If the command is EXIT this routine does not return.
119 * We will recurse through here executing batch files.
123 void WCMD_process_command (char *command
) {
129 HANDLE old_stdin
= 0, old_stdout
= 0, h
;
132 * Throw away constructs we don't support yet
135 if (strchr(command
,'|') != NULL
) {
136 WCMD_output ("Pipes not yet implemented\n");
141 * Expand up environment variables.
144 status
= ExpandEnvironmentStrings (command
, cmd
, sizeof(cmd
));
151 * Changing default drive has to be handled as a special case.
154 if ((cmd
[1] == ':') && IsCharAlpha (cmd
[0]) && (strlen(cmd
) == 2)) {
155 status
= SetCurrentDirectory (cmd
);
156 if (!status
) WCMD_print_error ();
159 WCMD_output (newline
);
162 * Redirect stdin and/or stdout if required.
165 if ((p
= strchr(cmd
,'<')) != NULL
) {
166 h
= CreateFile (WCMD_parameter (++p
, 0, NULL
), GENERIC_READ
, 0, NULL
, OPEN_EXISTING
,
167 FILE_ATTRIBUTE_NORMAL
, 0);
168 if (h
== INVALID_HANDLE_VALUE
) {
172 old_stdin
= GetStdHandle (STD_INPUT_HANDLE
);
173 SetStdHandle (STD_INPUT_HANDLE
, h
);
175 if ((p
= strchr(cmd
,'>')) != NULL
) {
176 h
= CreateFile (WCMD_parameter (++p
, 0, NULL
), GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
,
177 FILE_ATTRIBUTE_NORMAL
, 0);
178 if (h
== INVALID_HANDLE_VALUE
) {
182 old_stdout
= GetStdHandle (STD_OUTPUT_HANDLE
);
183 SetStdHandle (STD_OUTPUT_HANDLE
, h
);
186 if ((p
= strchr(cmd
,'<')) != NULL
) *p
= '\0';
189 * Check if the command entered is internal. If it is, pass the rest of the
190 * line down to the command. If not try to run a program.
194 while (IsCharAlphaNumeric(cmd
[count
])) {
197 for (i
=0; i
<=WCMD_EXIT
; i
++) {
198 if (CompareString (LOCALE_USER_DEFAULT
, NORM_IGNORECASE
| SORT_STRINGSORT
,
199 cmd
, count
, inbuilt
[i
], -1) == 2) break;
201 p
= WCMD_strtrim_leading_spaces (&cmd
[count
]);
202 WCMD_parse (p
, quals
, param1
, param2
);
206 WCMD_setshow_attrib ();
209 WCMD_batch (param1
, p
, 1);
213 WCMD_setshow_default ();
216 WCMD_clear_screen ();
225 WCMD_setshow_date ();
260 WCMD_setshow_path ();
266 WCMD_setshow_prompt ();
279 WCMD_setshow_env (p
);
285 WCMD_setshow_time ();
302 WCMD_run_program (cmd
);
305 CloseHandle (GetStdHandle (STD_INPUT_HANDLE
));
306 SetStdHandle (STD_INPUT_HANDLE
, old_stdin
);
309 CloseHandle (GetStdHandle (STD_OUTPUT_HANDLE
));
310 SetStdHandle (STD_OUTPUT_HANDLE
, old_stdout
);
314 /******************************************************************************
317 * Execute a command line as an external program. If no extension given then
318 * precedence is given to .BAT files. Must allow recursion.
320 * FIXME: Case sensitivity in suffixes!
323 void WCMD_run_program (char *command
) {
326 PROCESS_INFORMATION pe
;
329 char filetorun
[MAX_PATH
];
331 WCMD_parse (command
, quals
, param1
, param2
); /* Quick way to get the filename */
332 if (strpbrk (param1
, "\\:") == NULL
) { /* No explicit path given */
333 if ((strchr (param1
, '.') == NULL
) || (strstr (param1
, ".bat") != NULL
)) {
334 if (SearchPath (NULL
, param1
, ".bat", sizeof(filetorun
), filetorun
, NULL
)) {
335 WCMD_batch (filetorun
, command
, 0);
340 else { /* Explicit path given */
341 if (strstr (param1
, ".bat") != NULL
) {
342 WCMD_batch (param1
, command
, 0);
345 if (strchr (param1
, '.') == NULL
) {
346 strcpy (filetorun
, param1
);
347 strcat (filetorun
, ".bat");
348 h
= CreateFile (filetorun
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
349 if (h
!= INVALID_HANDLE_VALUE
) {
351 WCMD_batch (param1
, command
, 0);
357 /* No batch file found, assume executable */
359 ZeroMemory (&st
, sizeof(STARTUPINFO
));
360 st
.cb
= sizeof(STARTUPINFO
);
361 status
= CreateProcess (NULL
, command
, NULL
, NULL
, FALSE
,
362 0, NULL
, NULL
, &st
, &pe
);
368 /******************************************************************************
371 * Display the prompt on STDout
375 void WCMD_show_prompt () {
378 char out_string
[MAX_PATH
], curdir
[MAX_PATH
], prompt_string
[MAX_PATH
];
381 status
= GetEnvironmentVariable ("PROMPT", prompt_string
, sizeof(prompt_string
));
382 if ((status
== 0) || (status
> sizeof(prompt_string
))) {
383 lstrcpy (prompt_string
, "$N$G");
395 switch (toupper(*p
)) {
403 GetDateFormat (LOCALE_USER_DEFAULT
, DATE_SHORTDATE
, NULL
, NULL
, q
, MAX_PATH
);
416 status
= GetCurrentDirectory (sizeof(curdir
), curdir
);
422 status
= GetCurrentDirectory (sizeof(curdir
), curdir
);
432 GetTimeFormat (LOCALE_USER_DEFAULT
, 0, NULL
, NULL
, q
, MAX_PATH
);
443 WCMD_output (out_string
);
446 /****************************************************************************
449 * Print the message for GetLastError - not much use yet as Wine doesn't have
450 * the messages available, so we show meaningful messages for the most likely.
453 void WCMD_print_error () {
457 error_code
= GetLastError ();
458 switch (error_code
) {
459 case ERROR_FILE_NOT_FOUND
:
460 WCMD_output ("File Not Found\n");
462 case ERROR_PATH_NOT_FOUND
:
463 WCMD_output ("Path Not Found\n");
466 FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
468 MAKELANGID(LANG_NEUTRAL
,SUBLANG_DEFAULT
),
469 (LPTSTR
) &lpMsgBuf
, 0, NULL
);
470 WCMD_output (lpMsgBuf
);
471 LocalFree ((HLOCAL
)lpMsgBuf
);
476 /*******************************************************************
477 * WCMD_parse - parse a command into parameters and qualifiers.
479 * On exit, all qualifiers are concatenated into q, the first string
480 * not beginning with "/" is in p1 and the
481 * second in p2. Any subsequent non-qualifier strings are lost.
482 * Parameters in quotes are handled.
485 void WCMD_parse (char *s
, char *q
, char *p1
, char *p2
) {
489 *q
= *p1
= *p2
= '\0';
494 while ((*s
!= '\0') && (*s
!= ' ') && *s
!= '/') {
495 *q
++ = toupper (*s
++);
504 while ((*s
!= '\0') && (*s
!= '"')) {
505 if (p
== 0) *p1
++ = *s
++;
506 else if (p
== 1) *p2
++ = *s
++;
509 if (p
== 0) *p1
= '\0';
510 if (p
== 1) *p2
= '\0';
517 while ((*s
!= '\0') && (*s
!= ' ') && (*s
!= '/')) {
518 if (p
== 0) *p1
++ = *s
++;
519 else if (p
== 1) *p2
++ = *s
++;
522 if (p
== 0) *p1
= '\0';
523 if (p
== 1) *p2
= '\0';
529 /*******************************************************************
530 * WCMD_output - send output to current standard output device.
534 void WCMD_output (char *format
, ...) {
541 vsprintf (string
, format
, ap
);
542 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE
), string
, lstrlen(string
), &count
, NULL
);
547 /* Remove leading spaces from a string. Return a pointer to the first
548 * non-space character. Does not modify the input string
551 char *WCMD_strtrim_leading_spaces (char *string
) {
556 while (*ptr
== ' ') ptr
++;
560 /* Remove trailing spaces from a string. This routine modifies the input
561 * string by placing a null after the last non-space character
564 void WCMD_strtrim_trailing_spaces (char *string
) {
568 ptr
= string
+ lstrlen (string
) - 1;
569 while ((*ptr
== ' ') && (ptr
>= string
)) {