2 * WCMD - Wine-compatible command line interface.
9 * - No support for redirection, pipes, batch commands
10 * - 32-bit limit on file sizes in DIR command
11 * - Cannot handle parameters in quotes
12 * - Lots of functionality missing from builtins
18 /* external declaration here because we don't want to depend on Wine headers */
20 extern "C" HINSTANCE
MAIN_WinelibInit( int *argc
, char *argv
[] );
22 extern HINSTANCE
MAIN_WinelibInit( int *argc
, char *argv
[] );
26 char *inbuilt
[] = {"ATTRIB", "CALL", "CD", "CHDIR", "CLS", "COPY", "CTTY",
27 "DATE", "DEL", "DIR", "ECHO", "ERASE", "FOR", "GOTO",
28 "HELP", "IF", "LABEL", "MD", "MKDIR", "MOVE", "PATH", "PAUSE",
29 "PROMPT", "REM", "REN", "RENAME", "RD", "RMDIR", "SET", "SHIFT",
30 "TIME", "TYPE", "VERIFY", "VER", "VOL", "EXIT"};
35 char nyi
[] = "Not Yet Implemented\n\n";
36 char newline
[] = "\n";
37 char version_string
[] = "WCMD Version 0.10\n\n";
38 char anykey
[] = "Press any key to continue: ";
39 char quals
[MAX_PATH
], param1
[MAX_PATH
], param2
[MAX_PATH
];
41 /*****************************************************************************
42 * Main entry point. This is a console application so we have a main() not a
47 int main (int argc
, char *argv
[]) {
49 char string
[1024], args
[MAX_PATH
], param
[MAX_PATH
];
55 if (!(hinst
= MAIN_WinelibInit( &argc
, argv
))) return 0;
60 args
[0] = param
[0] = '\0';
62 for (i
=1; i
<argc
; i
++) {
63 if (argv
[i
][0] == '/') {
64 strcat (args
, argv
[i
]);
67 strcat (param
, argv
[i
]);
74 * Allocate a console and set it up.
77 status
= FreeConsole ();
78 if (!status
) WCMD_print_error();
79 status
= AllocConsole();
80 if (!status
) WCMD_print_error();
81 STDout
= GetStdHandle (STD_OUTPUT_HANDLE
);
82 STDin
= GetStdHandle (STD_INPUT_HANDLE
);
83 SetConsoleMode (STDin
, ENABLE_LINE_INPUT
| ENABLE_ECHO_INPUT
|
84 ENABLE_PROCESSED_INPUT
);
87 * Execute any command-line options.
90 if (strstr(args
, "/q") != NULL
) {
94 if (strstr(args
, "/c") != NULL
) {
95 WCMD_process_command (param
);
99 if (strstr(args
, "/k") != NULL
) {
100 WCMD_process_command (param
);
104 * If there is an AUTOEXEC.BAT file, try to execute it.
107 GetFullPathName ("\\autoexec.bat", sizeof(string
), string
, NULL
);
108 h
= CreateFile (string
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
109 if (h
!= INVALID_HANDLE_VALUE
) {
111 // WCMD_batch (string, " ");
115 * Loop forever getting commands and executing them.
121 ReadFile (STDin
, string
, sizeof(string
), &count
, NULL
);
123 string
[count
-1] = '\0'; /* ReadFile output is not null-terminated! */
124 if (string
[count
-2] == '\r') string
[count
-2] = '\0'; /* Under Windoze we get CRLF! */
125 if (lstrlen (string
) != 0) {
126 WCMD_process_command (string
);
133 /*****************************************************************************
134 * Process one command. If the command is EXIT this routine does not return.
135 * We will recurse through here executing batch files.
139 void WCMD_process_command (char *command
) {
147 * Throw away constructs we don't support yet
150 if ((strchr(command
,'<') != NULL
) || (strchr(command
,'>') != NULL
)) {
151 WCMD_output ("Redirection not yet implemented\n");
154 if (strchr(command
,'|') != NULL
) {
155 WCMD_output ("Pipes not yet implemented\n");
160 * Expand up environment variables.
163 status
= ExpandEnvironmentStrings (command
, cmd
, sizeof(cmd
));
170 * Changing default drive has to be handled as a special case.
173 if ((cmd
[1] == ':') && IsCharAlpha (cmd
[0]) && (strlen(cmd
) == 2)) {
174 status
= SetCurrentDirectory (cmd
);
175 if (!status
) WCMD_print_error ();
178 WCMD_output (newline
);
181 * Check if the command entered is internal. If it is, pass the rest of the
182 * line down to the command. If not try to run a program.
186 while (IsCharAlphaNumeric(cmd
[count
])) {
189 for (i
=0; i
<=WCMD_EXIT
; i
++) {
190 if (CompareString (LOCALE_USER_DEFAULT
, NORM_IGNORECASE
| SORT_STRINGSORT
,
191 cmd
, count
, inbuilt
[i
], -1) == 2) break;
193 p
= WCMD_strtrim_leading_spaces (&cmd
[count
]);
194 WCMD_parse (p
, quals
, param1
, param2
);
198 WCMD_setshow_attrib ();
201 WCMD_batch (param1
, p
);
205 WCMD_setshow_default ();
208 WCMD_clear_screen ();
217 WCMD_setshow_date ();
251 WCMD_setshow_path ();
257 WCMD_setshow_prompt ();
270 WCMD_setshow_env (p
);
276 WCMD_setshow_time ();
293 WCMD_run_program (cmd
);
297 /******************************************************************************
300 * Execute a command line as an external program. If no extension given then
301 * precedence is given to .BAT files. Must allow recursion.
303 * FIXME: Case sensitivity in suffixes!
306 void WCMD_run_program (char *command
) {
309 PROCESS_INFORMATION pe
;
312 char filetorun
[MAX_PATH
];
314 WCMD_parse (command
, quals
, param1
, param2
); /* Quick way to get the filename */
315 if (strpbrk (param1
, "\\:") == NULL
) { /* No explicit path given */
316 if ((strchr (param1
, '.') == NULL
) || (strstr (param1
, ".bat") != NULL
)) {
317 if (SearchPath (NULL
, param1
, ".bat", sizeof(filetorun
), filetorun
, NULL
)) {
318 WCMD_batch (filetorun
, command
);
323 else { /* Explicit path given */
324 if (strstr (param1
, ".bat") != NULL
) {
325 WCMD_batch (param1
, command
);
328 if (strchr (param1
, '.') == NULL
) {
329 strcpy (filetorun
, param1
);
330 strcat (filetorun
, ".bat");
331 h
= CreateFile (filetorun
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
332 if (h
!= INVALID_HANDLE_VALUE
) {
334 WCMD_batch (param1
, command
);
340 /* No batch file found, assume executable */
342 ZeroMemory (&st
, sizeof(STARTUPINFO
));
343 st
.cb
= sizeof(STARTUPINFO
);
344 status
= CreateProcess (NULL
, command
, NULL
, NULL
, FALSE
,
345 0, NULL
, NULL
, &st
, &pe
);
351 /******************************************************************************
354 * Display the prompt on STDout
358 void WCMD_show_prompt () {
361 char out_string
[MAX_PATH
], curdir
[MAX_PATH
], prompt_string
[MAX_PATH
];
364 status
= GetEnvironmentVariable ("PROMPT", prompt_string
, sizeof(prompt_string
));
365 if ((status
== 0) || (status
> sizeof(prompt_string
))) {
366 lstrcpy (prompt_string
, "$N$G");
378 switch (toupper(*p
)) {
386 GetDateFormat (LOCALE_USER_DEFAULT
, DATE_SHORTDATE
, NULL
, NULL
, q
, MAX_PATH
);
399 status
= GetCurrentDirectory (sizeof(curdir
), curdir
);
405 status
= GetCurrentDirectory (sizeof(curdir
), curdir
);
415 GetTimeFormat (LOCALE_USER_DEFAULT
, 0, NULL
, NULL
, q
, MAX_PATH
);
426 WCMD_output (out_string
);
429 /****************************************************************************
432 * Print the message for GetLastError - not much use yet as Wine doesn't have
433 * the messages available, so we show meaningful messages for the most likely.
436 void WCMD_print_error () {
440 error_code
= GetLastError ();
441 switch (error_code
) {
442 case ERROR_FILE_NOT_FOUND
:
443 WCMD_output ("File Not Found\n");
445 case ERROR_PATH_NOT_FOUND
:
446 WCMD_output ("Path Not Found\n");
449 FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
451 MAKELANGID(LANG_NEUTRAL
,SUBLANG_DEFAULT
),
452 (LPTSTR
) &lpMsgBuf
, 0, NULL
);
453 WCMD_output (lpMsgBuf
);
454 LocalFree ((HLOCAL
)lpMsgBuf
);
459 /*******************************************************************
460 * WCMD_parse - parse a command into parameters and qualifiers.
462 * On exit, all qualifiers are concatenated into q, the first string
463 * not beginning with "/" is in p1 and the
464 * second in p2. Any subsequent non-qualifier strings are lost.
465 * Parameters in quotes are handled.
468 void WCMD_parse (char *s
, char *q
, char *p1
, char *p2
) {
472 *q
= *p1
= *p2
= '\0';
477 while ((*s
!= '\0') && (*s
!= ' ') && *s
!= '/') {
478 *q
++ = toupper (*s
++);
487 while ((*s
!= '\0') && (*s
!= '"')) {
488 if (p
== 0) *p1
++ = *s
++;
489 else if (p
== 1) *p2
++ = *s
++;
492 if (p
== 0) *p1
= '\0';
493 if (p
== 1) *p2
= '\0';
500 while ((*s
!= '\0') && (*s
!= ' ') && (*s
!= '/')) {
501 if (p
== 0) *p1
++ = *s
++;
502 else if (p
== 1) *p2
++ = *s
++;
505 if (p
== 0) *p1
= '\0';
506 if (p
== 1) *p2
= '\0';
512 /*******************************************************************
513 * WCMD_output - send output to current standard output device.
517 void WCMD_output (char *format
, ...) {
524 vsprintf (string
, format
, ap
);
525 WriteFile (STDout
, string
, lstrlen(string
), &count
, NULL
);
530 /* Remove leading spaces from a string. Return a pointer to the first
531 * non-space character. Does not modify the input string
534 char *WCMD_strtrim_leading_spaces (char *string
) {
539 while (*ptr
== ' ') ptr
++;
543 /* Remove trailing spaces from a string. This routine modifies the input
544 * string by placing a null after the last non-space character
547 void WCMD_strtrim_trailing_spaces (char *string
) {
551 ptr
= string
+ lstrlen (string
) - 1;
552 while ((*ptr
== ' ') && (ptr
>= string
)) {