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", "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);
82 * Execute any command-line options.
85 if (strstr(args, "/q") != NULL) {
89 if (strstr(args, "/c") != NULL) {
90 WCMD_process_command (param);
94 if (strstr(args, "/k") != NULL) {
95 WCMD_process_command (param);
99 * If there is an AUTOEXEC.BAT file, try to execute it.
102 GetFullPathName ("\\autoexec.bat", sizeof(string), string, NULL);
103 h = CreateFile (string, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
104 if (h != INVALID_HANDLE_VALUE) {
107 WCMD_batch (string, " ");
112 * Loop forever getting commands and executing them.
118 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
120 string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
121 if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
122 if (lstrlen (string) != 0) {
123 if (strchr(string,'|') != NULL) {
127 WCMD_process_command (string);
135 /*****************************************************************************
136 * Process one command. If the command is EXIT this routine does not return.
137 * We will recurse through here executing batch files.
141 void WCMD_process_command (char *command) {
147 HANDLE old_stdin = 0, old_stdout = 0, h;
152 * Expand up environment variables.
155 status = ExpandEnvironmentStrings (command, cmd, sizeof(cmd));
162 * Changing default drive has to be handled as a special case.
165 if ((cmd[1] == ':') && IsCharAlpha (cmd[0]) && (strlen(cmd) == 2)) {
166 status = SetCurrentDirectory (cmd);
167 if (!status) WCMD_print_error ();
171 /* Dont issue newline WCMD_output (newline); @JED*/
174 * Redirect stdin and/or stdout if required.
177 if ((p = strchr(cmd,'<')) != NULL) {
178 h = CreateFile (WCMD_parameter (++p, 0, NULL), GENERIC_READ, 0, NULL, OPEN_EXISTING,
179 FILE_ATTRIBUTE_NORMAL, NULL);
180 if (h == INVALID_HANDLE_VALUE) {
184 old_stdin = GetStdHandle (STD_INPUT_HANDLE);
185 SetStdHandle (STD_INPUT_HANDLE, h);
187 if ((p = strchr(cmd,'>')) != NULL) {
188 h = CreateFile (WCMD_parameter (++p, 0, NULL), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
189 FILE_ATTRIBUTE_NORMAL, NULL);
190 if (h == INVALID_HANDLE_VALUE) {
194 old_stdout = GetStdHandle (STD_OUTPUT_HANDLE);
195 SetStdHandle (STD_OUTPUT_HANDLE, h);
198 if ((p = strchr(cmd,'<')) != NULL) *p = '\0';
201 * Strip leading whitespaces, and a '@' if supplied
203 whichcmd = WCMD_strtrim_leading_spaces(cmd);
204 if (whichcmd[0] == '@') whichcmd++;
207 * Check if the command entered is internal. If it is, pass the rest of the
208 * line down to the command. If not try to run a program.
212 while (IsCharAlphaNumeric(whichcmd[count])) {
215 for (i=0; i<=WCMD_EXIT; i++) {
216 if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
217 whichcmd, count, inbuilt[i], -1) == 2) break;
219 p = WCMD_strtrim_leading_spaces (&whichcmd[count]);
220 WCMD_parse (p, quals, param1, param2);
224 WCMD_setshow_attrib ();
227 WCMD_batch (param1, p, 1);
231 WCMD_setshow_default ();
234 WCMD_clear_screen ();
243 WCMD_setshow_date ();
253 /* Use the unstripped version of the following data - step over the space */
254 /* but only if a parameter follows */
255 if (strlen(&whichcmd[count]) > 0)
256 WCMD_echo(&whichcmd[count+1]);
258 WCMD_echo(&whichcmd[count]);
283 WCMD_setshow_path ();
289 WCMD_setshow_prompt ();
302 WCMD_setshow_env (p);
308 WCMD_setshow_time ();
325 WCMD_run_program (whichcmd);
328 CloseHandle (GetStdHandle (STD_INPUT_HANDLE));
329 SetStdHandle (STD_INPUT_HANDLE, old_stdin);
332 CloseHandle (GetStdHandle (STD_OUTPUT_HANDLE));
333 SetStdHandle (STD_OUTPUT_HANDLE, old_stdout);
337 /******************************************************************************
340 * Execute a command line as an external program. If no extension given then
341 * precedence is given to .BAT files. Must allow recursion.
343 * FIXME: Case sensitivity in suffixes!
346 void WCMD_run_program (char *command) {
349 PROCESS_INFORMATION pe;
355 char filetorun[MAX_PATH];
357 WCMD_parse (command, quals, param1, param2); /* Quick way to get the filename */
358 if (strpbrk (param1, "\\:") == NULL) { /* No explicit path given */
359 if ((strchr (param1, '.') == NULL) || (strstr (param1, ".bat") != NULL)) {
360 if (SearchPath (NULL, param1, ".bat", sizeof(filetorun), filetorun, NULL)) {
361 WCMD_batch (filetorun, command, 0);
366 else { /* Explicit path given */
367 if (strstr (param1, ".bat") != NULL) {
368 WCMD_batch (param1, command, 0);
371 if (strchr (param1, '.') == NULL) {
372 strcpy (filetorun, param1);
373 strcat (filetorun, ".bat");
374 h = CreateFile (filetorun, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
375 if (h != INVALID_HANDLE_VALUE) {
377 WCMD_batch (param1, command, 0);
383 /* No batch file found, assume executable */
385 hinst = FindExecutable (param1, NULL, filetorun);
386 if ((int)hinst < 32) {
390 console = SHGetFileInfo (filetorun, 0, &psfi, sizeof(psfi), SHGFI_EXETYPE);
395 ZeroMemory (&st, sizeof(STARTUPINFO));
396 st.cb = sizeof(STARTUPINFO);
397 status = CreateProcess (NULL, command, NULL, NULL, FALSE,
398 0, NULL, NULL, &st, &pe);
402 if (!HIWORD(console)) WaitForSingleObject (pe.hProcess, INFINITE);
403 GetExitCodeProcess (pe.hProcess, &errorlevel);
404 if (errorlevel == STILL_ACTIVE) errorlevel = 0;
407 /******************************************************************************
410 * Display the prompt on STDout
414 void WCMD_show_prompt () {
417 char out_string[MAX_PATH], curdir[MAX_PATH], prompt_string[MAX_PATH];
420 status = GetEnvironmentVariable ("PROMPT", prompt_string, sizeof(prompt_string));
421 if ((status == 0) || (status > sizeof(prompt_string))) {
422 lstrcpy (prompt_string, "$N$G");
434 switch (toupper(*p)) {
442 GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, NULL, q, MAX_PATH);
455 status = GetCurrentDirectory (sizeof(curdir), curdir);
461 status = GetCurrentDirectory (sizeof(curdir), curdir);
471 GetTimeFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL, q, MAX_PATH);
482 WCMD_output (out_string);
485 /****************************************************************************
488 * Print the message for GetLastError
491 void WCMD_print_error () {
496 error_code = GetLastError ();
497 status = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
498 NULL, error_code, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
500 WCMD_output ("FIXME: Cannot display message for error %d, status %d\n",
501 error_code, GetLastError());
504 WCMD_output (lpMsgBuf);
505 LocalFree ((HLOCAL)lpMsgBuf);
506 WCMD_output (newline);
510 /*******************************************************************
511 * WCMD_parse - parse a command into parameters and qualifiers.
513 * On exit, all qualifiers are concatenated into q, the first string
514 * not beginning with "/" is in p1 and the
515 * second in p2. Any subsequent non-qualifier strings are lost.
516 * Parameters in quotes are handled.
519 void WCMD_parse (char *s, char *q, char *p1, char *p2) {
523 *q = *p1 = *p2 = '\0';
528 while ((*s != '\0') && (*s != ' ') && *s != '/') {
529 *q++ = toupper (*s++);
538 while ((*s != '\0') && (*s != '"')) {
539 if (p == 0) *p1++ = *s++;
540 else if (p == 1) *p2++ = *s++;
543 if (p == 0) *p1 = '\0';
544 if (p == 1) *p2 = '\0';
551 while ((*s != '\0') && (*s != ' ') && (*s != '/')) {
552 if (p == 0) *p1++ = *s++;
553 else if (p == 1) *p2++ = *s++;
556 if (p == 0) *p1 = '\0';
557 if (p == 1) *p2 = '\0';
563 /*******************************************************************
564 * WCMD_output - send output to current standard output device.
568 void WCMD_output (char *format, ...) {
575 vsprintf (string, format, ap);
576 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), string, lstrlen(string), &count, NULL);
580 /*******************************************************************
581 * WCMD_output_asis - send output to current standard output device.
582 * without formatting eg. when message contains '%'
585 void WCMD_output_asis (char *message) {
587 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), message, lstrlen(message), &count, NULL);
592 /***************************************************************************
593 * WCMD_strtrim_leading_spaces
595 * Remove leading spaces from a string. Return a pointer to the first
596 * non-space character. Does not modify the input string
599 char *WCMD_strtrim_leading_spaces (char *string) {
604 while (*ptr == ' ') ptr++;
608 /*************************************************************************
609 * WCMD_strtrim_trailing_spaces
611 * Remove trailing spaces from a string. This routine modifies the input
612 * string by placing a null after the last non-space character
615 void WCMD_strtrim_trailing_spaces (char *string) {
619 ptr = string + lstrlen (string) - 1;
620 while ((*ptr == ' ') && (ptr >= string)) {
626 /*************************************************************************
629 * Handle pipes within a command - the DOS way using temporary files.
632 void WCMD_pipe (char *command) {
635 char temp_path[MAX_PATH], temp_file[MAX_PATH], temp_file2[MAX_PATH], temp_cmd[1024];
637 GetTempPath (sizeof(temp_path), temp_path);
638 GetTempFileName (temp_path, "WCMD", 0, temp_file);
639 p = strchr(command, '|');
641 wsprintf (temp_cmd, "%s > %s", command, temp_file);
642 WCMD_process_command (temp_cmd);
644 while ((p = strchr(command, '|'))) {
646 GetTempFileName (temp_path, "WCMD", 0, temp_file2);
647 wsprintf (temp_cmd, "%s < %s > %s", command, temp_file, temp_file2);
648 WCMD_process_command (temp_cmd);
649 DeleteFile (temp_file);
650 lstrcpy (temp_file, temp_file2);
653 wsprintf (temp_cmd, "%s < %s", command, temp_file);
654 WCMD_process_command (temp_cmd);
655 DeleteFile (temp_file);