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"};
25 int echo_mode = 1, verify_mode = 0;
26 char nyi[] = "Not Yet Implemented\n\n";
27 char newline[] = "\n";
28 char version_string[] = "WCMD Version 0.15\n\n";
29 char anykey[] = "Press any key to continue: ";
30 char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
31 BATCH_CONTEXT *context = NULL;
33 /*****************************************************************************
34 * Main entry point. This is a console application so we have a main() not a
39 int wine_main (int argc, char *argv[]) {
41 char string[1024], args[MAX_PATH], param[MAX_PATH];
46 args[0] = param[0] = '\0';
48 for (i=1; i<argc; i++) {
49 if (argv[i][0] == '/') {
50 strcat (args, argv[i]);
53 strcat (param, argv[i]);
60 * Allocate a console and set it up.
63 status = FreeConsole ();
64 if (!status) WCMD_print_error();
65 status = AllocConsole();
66 if (!status) WCMD_print_error();
67 SetConsoleMode (GetStdHandle(STD_INPUT_HANDLE), ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT |
68 ENABLE_PROCESSED_INPUT);
71 * Execute any command-line options.
74 if (strstr(args, "/q") != NULL) {
78 if (strstr(args, "/c") != NULL) {
79 WCMD_process_command (param);
83 if (strstr(args, "/k") != NULL) {
84 WCMD_process_command (param);
88 * If there is an AUTOEXEC.BAT file, try to execute it.
91 GetFullPathName ("\\autoexec.bat", sizeof(string), string, NULL);
92 h = CreateFile (string, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
93 if (h != INVALID_HANDLE_VALUE) {
96 WCMD_batch (string, " ");
101 * Loop forever getting commands and executing them.
107 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
109 string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
110 if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
111 if (lstrlen (string) != 0) {
112 WCMD_process_command (string);
119 /*****************************************************************************
120 * Process one command. If the command is EXIT this routine does not return.
121 * We will recurse through here executing batch files.
125 void WCMD_process_command (char *command) {
131 HANDLE old_stdin = 0, old_stdout = 0, h;
135 * Throw away constructs we don't support yet
138 if (strchr(command,'|') != NULL) {
139 WCMD_output ("Pipes not yet implemented\n");
144 * Expand up environment variables.
147 status = ExpandEnvironmentStrings (command, cmd, sizeof(cmd));
154 * Changing default drive has to be handled as a special case.
157 if ((cmd[1] == ':') && IsCharAlpha (cmd[0]) && (strlen(cmd) == 2)) {
158 status = SetCurrentDirectory (cmd);
159 if (!status) WCMD_print_error ();
163 /* Dont issue newline WCMD_output (newline); @JED*/
166 * Redirect stdin and/or stdout if required.
169 if ((p = strchr(cmd,'<')) != NULL) {
170 h = CreateFile (WCMD_parameter (++p, 0, NULL), GENERIC_READ, 0, NULL, OPEN_EXISTING,
171 FILE_ATTRIBUTE_NORMAL, NULL);
172 if (h == INVALID_HANDLE_VALUE) {
176 old_stdin = GetStdHandle (STD_INPUT_HANDLE);
177 SetStdHandle (STD_INPUT_HANDLE, h);
179 if ((p = strchr(cmd,'>')) != NULL) {
180 h = CreateFile (WCMD_parameter (++p, 0, NULL), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
181 FILE_ATTRIBUTE_NORMAL, NULL);
182 if (h == INVALID_HANDLE_VALUE) {
186 old_stdout = GetStdHandle (STD_OUTPUT_HANDLE);
187 SetStdHandle (STD_OUTPUT_HANDLE, h);
190 if ((p = strchr(cmd,'<')) != NULL) *p = '\0';
193 * Strip leading whitespaces, and a '@' if supplied
195 whichcmd = WCMD_strtrim_leading_spaces(cmd);
196 if (whichcmd[0] == '@') whichcmd++;
199 * Check if the command entered is internal. If it is, pass the rest of the
200 * line down to the command. If not try to run a program.
204 while (IsCharAlphaNumeric(whichcmd[count])) {
207 for (i=0; i<=WCMD_EXIT; i++) {
208 if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
209 whichcmd, count, inbuilt[i], -1) == 2) break;
211 p = WCMD_strtrim_leading_spaces (&whichcmd[count]);
212 WCMD_parse (p, quals, param1, param2);
216 WCMD_setshow_attrib ();
219 WCMD_batch (param1, p, 1);
223 WCMD_setshow_default ();
226 WCMD_clear_screen ();
235 WCMD_setshow_date ();
245 /* Use the unstripped version of the following data - step over the space */
246 /* but only if a parameter follows */
247 if (strlen(&whichcmd[count]) > 0)
248 WCMD_echo(&whichcmd[count+1]);
250 WCMD_echo(&whichcmd[count]);
275 WCMD_setshow_path ();
281 WCMD_setshow_prompt ();
294 WCMD_setshow_env (p);
300 WCMD_setshow_time ();
317 WCMD_run_program (whichcmd);
320 CloseHandle (GetStdHandle (STD_INPUT_HANDLE));
321 SetStdHandle (STD_INPUT_HANDLE, old_stdin);
324 CloseHandle (GetStdHandle (STD_OUTPUT_HANDLE));
325 SetStdHandle (STD_OUTPUT_HANDLE, old_stdout);
329 /******************************************************************************
332 * Execute a command line as an external program. If no extension given then
333 * precedence is given to .BAT files. Must allow recursion.
335 * FIXME: Case sensitivity in suffixes!
338 void WCMD_run_program (char *command) {
341 PROCESS_INFORMATION pe;
347 char filetorun[MAX_PATH];
349 WCMD_parse (command, quals, param1, param2); /* Quick way to get the filename */
350 if (strpbrk (param1, "\\:") == NULL) { /* No explicit path given */
351 if ((strchr (param1, '.') == NULL) || (strstr (param1, ".bat") != NULL)) {
352 if (SearchPath (NULL, param1, ".bat", sizeof(filetorun), filetorun, NULL)) {
353 WCMD_batch (filetorun, command, 0);
358 else { /* Explicit path given */
359 if (strstr (param1, ".bat") != NULL) {
360 WCMD_batch (param1, command, 0);
363 if (strchr (param1, '.') == NULL) {
364 strcpy (filetorun, param1);
365 strcat (filetorun, ".bat");
366 h = CreateFile (filetorun, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
367 if (h != INVALID_HANDLE_VALUE) {
369 WCMD_batch (param1, command, 0);
375 /* No batch file found, assume executable */
377 hinst = FindExecutable (param1, NULL, filetorun);
378 if ((int)hinst < 32) {
382 console = SHGetFileInfo (filetorun, 0, &psfi, sizeof(psfi), SHGFI_EXETYPE);
387 ZeroMemory (&st, sizeof(STARTUPINFO));
388 st.cb = sizeof(STARTUPINFO);
389 status = CreateProcess (NULL, command, NULL, NULL, FALSE,
390 0, NULL, NULL, &st, &pe);
394 if (!HIWORD(console)) WaitForSingleObject (pe.hProcess, INFINITE);
395 GetExitCodeProcess (pe.hProcess, &errorlevel);
396 if (errorlevel == STILL_ACTIVE) errorlevel = 0;
399 /******************************************************************************
402 * Display the prompt on STDout
406 void WCMD_show_prompt () {
409 char out_string[MAX_PATH], curdir[MAX_PATH], prompt_string[MAX_PATH];
412 status = GetEnvironmentVariable ("PROMPT", prompt_string, sizeof(prompt_string));
413 if ((status == 0) || (status > sizeof(prompt_string))) {
414 lstrcpy (prompt_string, "$N$G");
426 switch (toupper(*p)) {
434 GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, NULL, q, MAX_PATH);
447 status = GetCurrentDirectory (sizeof(curdir), curdir);
453 status = GetCurrentDirectory (sizeof(curdir), curdir);
463 GetTimeFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL, q, MAX_PATH);
474 WCMD_output (out_string);
477 /****************************************************************************
480 * Print the message for GetLastError
483 void WCMD_print_error () {
488 error_code = GetLastError ();
489 status = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
490 NULL, error_code, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
492 WCMD_output ("FIXME: Cannot display message for error %d, status %d\n",
493 error_code, GetLastError());
496 WCMD_output (lpMsgBuf);
497 LocalFree ((HLOCAL)lpMsgBuf);
498 WCMD_output (newline);
502 /*******************************************************************
503 * WCMD_parse - parse a command into parameters and qualifiers.
505 * On exit, all qualifiers are concatenated into q, the first string
506 * not beginning with "/" is in p1 and the
507 * second in p2. Any subsequent non-qualifier strings are lost.
508 * Parameters in quotes are handled.
511 void WCMD_parse (char *s, char *q, char *p1, char *p2) {
515 *q = *p1 = *p2 = '\0';
520 while ((*s != '\0') && (*s != ' ') && *s != '/') {
521 *q++ = toupper (*s++);
530 while ((*s != '\0') && (*s != '"')) {
531 if (p == 0) *p1++ = *s++;
532 else if (p == 1) *p2++ = *s++;
535 if (p == 0) *p1 = '\0';
536 if (p == 1) *p2 = '\0';
543 while ((*s != '\0') && (*s != ' ') && (*s != '/')) {
544 if (p == 0) *p1++ = *s++;
545 else if (p == 1) *p2++ = *s++;
548 if (p == 0) *p1 = '\0';
549 if (p == 1) *p2 = '\0';
555 /*******************************************************************
556 * WCMD_output - send output to current standard output device.
560 void WCMD_output (char *format, ...) {
567 vsprintf (string, format, ap);
568 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), string, lstrlen(string), &count, NULL);
572 /*******************************************************************
573 * WCMD_output_asis - send output to current standard output device.
574 * without formatting eg. when message contains '%'
577 void WCMD_output_asis (char *message) {
579 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), message, lstrlen(message), &count, NULL);
584 /***************************************************************************
585 * WCMD_strtrim_leading_spaces
587 * Remove leading spaces from a string. Return a pointer to the first
588 * non-space character. Does not modify the input string
591 char *WCMD_strtrim_leading_spaces (char *string) {
596 while (*ptr == ' ') ptr++;
600 /*************************************************************************
601 * WCMD_strtrim_trailing_spaces
603 * Remove trailing spaces from a string. This routine modifies the input
604 * string by placing a null after the last non-space character
607 void WCMD_strtrim_trailing_spaces (char *string) {
611 ptr = string + lstrlen (string) - 1;
612 while ((*ptr == ' ') && (ptr >= string)) {