2 * WCMD - Wine-compatible command line interface - built-in functions.
4 * Copyright (C) 1999 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 * On entry to each function, global variables quals, param1, param2 contain
24 * the qualifiers (uppercased and concatenated) and parameters entered, with
25 * environment-variable and batch parameter substitution already done.
30 * - No support for pipes, shell parameters
31 * - Lots of functionality missing from builtins
32 * - Messages etc need international support
35 #define WIN32_LEAN_AND_MEAN
39 void WCMD_execute (char *orig_command, char *parameter, char *substitution);
43 struct env_stack *next;
47 struct env_stack *saved_environment;
49 extern HINSTANCE hinst;
50 extern char *inbuilt[];
51 extern int echo_mode, verify_mode;
52 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
53 extern BATCH_CONTEXT *context;
54 extern DWORD errorlevel;
58 /****************************************************************************
61 * Clear the terminal screen.
64 void WCMD_clear_screen (void) {
66 /* Emulate by filling the screen from the top left to bottom right with
67 spaces, then moving the cursor to the top left afterwards */
68 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
69 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
71 if (GetConsoleScreenBufferInfo(hStdOut, &consoleInfo))
76 screenSize = consoleInfo.dwSize.X * (consoleInfo.dwSize.Y + 1);
80 FillConsoleOutputCharacter(hStdOut, ' ', screenSize, topLeft, &screenSize);
81 SetConsoleCursorPosition(hStdOut, topLeft);
85 /****************************************************************************
88 * Change the default i/o device (ie redirect STDin/STDout).
91 void WCMD_change_tty (void) {
97 /****************************************************************************
100 * Copy a file or wildcarded set.
101 * FIXME: No wildcard support
104 void WCMD_copy (void) {
110 static const char overwrite[] = "Overwrite file (Y/N)?";
111 char string[8], outpath[MAX_PATH], inpath[MAX_PATH], *infile;
113 if ((strchr(param1,'*') != NULL) && (strchr(param1,'%') != NULL)) {
114 WCMD_output ("Wildcards not yet supported\n");
118 /* If no destination supplied, assume current directory */
119 if (param2[0] == 0x00) {
123 GetFullPathName (param2, sizeof(outpath), outpath, NULL);
124 hff = FindFirstFile (outpath, &fd);
125 if (hff != INVALID_HANDLE_VALUE) {
126 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
127 GetFullPathName (param1, sizeof(inpath), inpath, &infile);
128 strcat (outpath, "\\");
129 strcat (outpath, infile);
134 force = (strstr (quals, "/Y") != NULL);
136 hff = FindFirstFile (outpath, &fd);
137 if (hff != INVALID_HANDLE_VALUE) {
139 WCMD_output (overwrite);
140 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
141 if (toupper(string[0]) == 'Y') force = TRUE;
146 status = CopyFile (param1, outpath, FALSE);
147 if (!status) WCMD_print_error ();
151 /****************************************************************************
154 * Create a directory.
156 * this works recursivly. so mkdir dir1\dir2\dir3 will create dir1 and dir2 if
157 * they do not already exist.
160 BOOL create_full_path(CHAR* path)
166 new_path = HeapAlloc(GetProcessHeap(),0,strlen(path)+1);
167 strcpy(new_path,path);
169 while ((len = strlen(new_path)) && new_path[len - 1] == '\\')
170 new_path[len - 1] = 0;
172 while (!CreateDirectory(new_path,NULL))
175 DWORD last_error = GetLastError();
176 if (last_error == ERROR_ALREADY_EXISTS)
179 if (last_error != ERROR_PATH_NOT_FOUND)
185 if (!(slash = strrchr(new_path,'\\')) && ! (slash = strrchr(new_path,'/')))
191 len = slash - new_path;
193 if (!create_full_path(new_path))
198 new_path[len] = '\\';
200 HeapFree(GetProcessHeap(),0,new_path);
204 void WCMD_create_dir (void) {
206 if (!create_full_path(param1)) WCMD_print_error ();
209 /****************************************************************************
212 * Delete a file or wildcarded set.
216 void WCMD_delete (int recurse) {
220 char fpath[MAX_PATH];
223 hff = FindFirstFile (param1, &fd);
224 if (hff == INVALID_HANDLE_VALUE) {
225 WCMD_output ("%s :File Not Found\n",param1);
228 if ((strchr(param1,'*') == NULL) && (strchr(param1,'?') == NULL)
229 && (!recurse) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
230 strcat (param1, "\\*");
235 if ((strchr(param1,'*') != NULL) || (strchr(param1,'?') != NULL)) {
236 strcpy (fpath, param1);
238 p = strrchr (fpath, '\\');
241 strcat (fpath, fd.cFileName);
243 else strcpy (fpath, fd.cFileName);
244 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
245 if (!DeleteFile (fpath)) WCMD_print_error ();
247 } while (FindNextFile(hff, &fd) != 0);
251 if (!DeleteFile (param1)) WCMD_print_error ();
256 /****************************************************************************
259 * Echo input to the screen (or not). We don't try to emulate the bugs
260 * in DOS (try typing "ECHO ON AGAIN" for an example).
263 void WCMD_echo (const char *command) {
265 static const char eon[] = "Echo is ON\n", eoff[] = "Echo is OFF\n";
268 if ((command[0] == '.') && (command[1] == 0)) {
269 WCMD_output (newline);
274 count = strlen(command);
276 if (echo_mode) WCMD_output (eon);
277 else WCMD_output (eoff);
280 if (lstrcmpi(command, "ON") == 0) {
284 if (lstrcmpi(command, "OFF") == 0) {
288 WCMD_output_asis (command);
289 WCMD_output (newline);
293 /**************************************************************************
296 * Batch file loop processing.
297 * FIXME: We don't exhaustively check syntax. Any command which works in MessDOS
298 * will probably work here, but the reverse is not necessarily the case...
301 void WCMD_for (char *p) {
306 char set[MAX_PATH], param[MAX_PATH];
309 if (lstrcmpi (WCMD_parameter (p, 1, NULL), "in")
310 || lstrcmpi (WCMD_parameter (p, 3, NULL), "do")
311 || (param1[0] != '%')) {
312 WCMD_output ("Syntax error\n");
315 lstrcpyn (set, WCMD_parameter (p, 2, NULL), sizeof(set));
316 WCMD_parameter (p, 4, &cmd);
317 lstrcpy (param, param1);
320 * If the parameter within the set has a wildcard then search for matching files
321 * otherwise do a literal substitution.
325 while (*(item = WCMD_parameter (set, i, NULL))) {
326 if (strpbrk (item, "*?")) {
327 hff = FindFirstFile (item, &fd);
328 if (hff == INVALID_HANDLE_VALUE) {
332 WCMD_execute (cmd, param, fd.cFileName);
333 } while (FindNextFile(hff, &fd) != 0);
337 WCMD_execute (cmd, param, item);
343 /*****************************************************************************
346 * Execute a command after substituting variable text for the supplied parameter
349 void WCMD_execute (char *orig_cmd, char *param, char *subst) {
351 char *new_cmd, *p, *s, *dup;
354 size = lstrlen (orig_cmd);
355 new_cmd = (char *) LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, size);
356 dup = s = strdup (orig_cmd);
358 while ((p = strstr (s, param))) {
360 size += lstrlen (subst);
361 new_cmd = (char *) LocalReAlloc ((HANDLE)new_cmd, size, 0);
363 strcat (new_cmd, subst);
364 s = p + lstrlen (param);
367 WCMD_process_command (new_cmd);
369 LocalFree ((HANDLE)new_cmd);
373 /**************************************************************************
376 * Simple on-line help. Help text is stored in the resource file.
379 void WCMD_give_help (char *command) {
384 command = WCMD_strtrim_leading_spaces(command);
385 if (lstrlen(command) == 0) {
386 LoadString (hinst, 1000, buffer, sizeof(buffer));
387 WCMD_output_asis (buffer);
390 for (i=0; i<=WCMD_EXIT; i++) {
391 if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
392 param1, -1, inbuilt[i], -1) == 2) {
393 LoadString (hinst, i, buffer, sizeof(buffer));
394 WCMD_output_asis (buffer);
398 WCMD_output ("No help available for %s\n", param1);
403 /****************************************************************************
406 * Batch file jump instruction. Not the most efficient algorithm ;-)
407 * Prints error message if the specified label cannot be found - the file pointer is
408 * then at EOF, effectively stopping the batch file.
409 * FIXME: DOS is supposed to allow labels with spaces - we don't.
412 void WCMD_goto (void) {
414 char string[MAX_PATH];
416 if (context != NULL) {
417 SetFilePointer (context -> h, 0, NULL, FILE_BEGIN);
418 while (WCMD_fgets (string, sizeof(string), context -> h)) {
419 if ((string[0] == ':') && (strcmp (&string[1], param1) == 0)) return;
421 WCMD_output ("Target to GOTO not found\n");
427 /****************************************************************************
430 * Batch file conditional.
431 * FIXME: Much more syntax checking needed!
434 void WCMD_if (char *p) {
436 int negate = 0, test = 0;
437 char condition[MAX_PATH], *command, *s;
439 if (!lstrcmpi (param1, "not")) {
441 lstrcpy (condition, param2);
444 lstrcpy (condition, param1);
446 if (!lstrcmpi (condition, "errorlevel")) {
447 if (errorlevel >= atoi(WCMD_parameter (p, 1+negate, NULL))) test = 1;
449 WCMD_parameter (p, 2+negate, &command);
451 else if (!lstrcmpi (condition, "exist")) {
452 if (GetFileAttributesA(WCMD_parameter (p, 1+negate, NULL)) != INVALID_FILE_ATTRIBUTES) {
455 WCMD_parameter (p, 2+negate, &command);
457 else if ((s = strstr (p, "=="))) {
459 if (!lstrcmpi (condition, WCMD_parameter (s, 0, NULL))) test = 1;
460 WCMD_parameter (s, 1, &command);
463 WCMD_output ("Syntax error\n");
466 if (test != negate) {
467 command = strdup (command);
468 WCMD_process_command (command);
473 /****************************************************************************
476 * Move a file, directory tree or wildcarded set of files.
477 * FIXME: Needs input and output files to be fully specified.
480 void WCMD_move (void) {
483 char outpath[MAX_PATH], inpath[MAX_PATH], *infile;
487 if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
488 WCMD_output ("Wildcards not yet supported\n");
492 /* If no destination supplied, assume current directory */
493 if (param2[0] == 0x00) {
497 /* If 2nd parm is directory, then use original filename */
498 GetFullPathName (param2, sizeof(outpath), outpath, NULL);
499 hff = FindFirstFile (outpath, &fd);
500 if (hff != INVALID_HANDLE_VALUE) {
501 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
502 GetFullPathName (param1, sizeof(inpath), inpath, &infile);
503 strcat (outpath, "\\");
504 strcat (outpath, infile);
509 status = MoveFile (param1, outpath);
510 if (!status) WCMD_print_error ();
513 /****************************************************************************
516 * Wait for keyboard input.
519 void WCMD_pause (void) {
524 WCMD_output (anykey);
525 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
528 /****************************************************************************
531 * Delete a directory.
534 void WCMD_remove_dir (void) {
536 if (!RemoveDirectory (param1)) WCMD_print_error ();
539 /****************************************************************************
543 * FIXME: Needs input and output files to be fully specified.
546 void WCMD_rename (void) {
550 if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
551 WCMD_output ("Wildcards not yet supported\n");
554 status = MoveFile (param1, param2);
555 if (!status) WCMD_print_error ();
558 /*****************************************************************************
561 * Make a copy of the environment.
563 static WCHAR *WCMD_dupenv( const WCHAR *env )
573 len += (lstrlenW(&env[len]) + 1);
575 env_copy = LocalAlloc (LMEM_FIXED, (len+1) * sizeof (WCHAR) );
578 WCMD_output ("out of memory\n");
581 memcpy (env_copy, env, len*sizeof (WCHAR));
587 /*****************************************************************************
590 * setlocal pushes the environment onto a stack
591 * Save the environment as unicode so we don't screw anything up.
593 void WCMD_setlocal (const char *s) {
595 struct env_stack *env_copy;
597 /* DISABLEEXTENSIONS ignored */
599 env_copy = LocalAlloc (LMEM_FIXED, sizeof (struct env_stack));
602 WCMD_output ("out of memory\n");
606 env = GetEnvironmentStringsW ();
608 env_copy->strings = WCMD_dupenv (env);
609 if (env_copy->strings)
611 env_copy->next = saved_environment;
612 saved_environment = env_copy;
615 LocalFree (env_copy);
617 FreeEnvironmentStringsW (env);
620 /*****************************************************************************
623 static inline WCHAR *WCMD_strchrW(WCHAR *str, WCHAR ch)
634 /*****************************************************************************
637 * endlocal pops the environment off a stack
639 void WCMD_endlocal (void) {
640 WCHAR *env, *old, *p;
641 struct env_stack *temp;
644 if (!saved_environment)
647 /* pop the old environment from the stack */
648 temp = saved_environment;
649 saved_environment = temp->next;
651 /* delete the current environment, totally */
652 env = GetEnvironmentStringsW ();
653 old = WCMD_dupenv (GetEnvironmentStringsW ());
656 n = lstrlenW(&old[len]) + 1;
657 p = WCMD_strchrW(&old[len], '=');
661 SetEnvironmentVariableW (&old[len], NULL);
666 FreeEnvironmentStringsW (env);
668 /* restore old environment */
672 n = lstrlenW(&env[len]) + 1;
673 p = WCMD_strchrW(&env[len], '=');
677 SetEnvironmentVariableW (&env[len], p);
685 /*****************************************************************************
686 * WCMD_setshow_attrib
688 * Display and optionally sets DOS attributes on a file or directory
690 * FIXME: Wine currently uses the Unix stat() function to get file attributes.
691 * As a result only the Readonly flag is correctly reported, the Archive bit
692 * is always set and the rest are not implemented. We do the Right Thing anyway.
694 * FIXME: No SET functionality.
698 void WCMD_setshow_attrib (void) {
703 char flags[9] = {" "};
705 if (param1[0] == '-') {
710 if (lstrlen(param1) == 0) {
711 GetCurrentDirectory (sizeof(param1), param1);
712 strcat (param1, "\\*");
715 hff = FindFirstFile (param1, &fd);
716 if (hff == INVALID_HANDLE_VALUE) {
717 WCMD_output ("%s: File Not Found\n",param1);
721 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
722 if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
725 if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
728 if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
731 if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
734 if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) {
737 if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) {
740 WCMD_output ("%s %s\n", flags, fd.cFileName);
741 for (count=0; count < 8; count++) flags[count] = ' ';
743 } while (FindNextFile(hff, &fd) != 0);
748 /*****************************************************************************
749 * WCMD_setshow_default
751 * Set/Show the current default directory
754 void WCMD_setshow_default (void) {
759 if (strlen(param1) == 0) {
760 GetCurrentDirectory (sizeof(string), string);
761 strcat (string, "\n");
762 WCMD_output (string);
765 status = SetCurrentDirectory (param1);
774 /****************************************************************************
777 * Set/Show the system date
778 * FIXME: Can't change date yet
781 void WCMD_setshow_date (void) {
783 char curdate[64], buffer[64];
786 if (lstrlen(param1) == 0) {
787 if (GetDateFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL,
788 curdate, sizeof(curdate))) {
789 WCMD_output ("Current Date is %s\nEnter new date: ", curdate);
790 ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
795 else WCMD_print_error ();
802 /****************************************************************************
805 static int WCMD_compare( const void *a, const void *b )
808 const char * const *str_a = a, * const *str_b = b;
809 r = CompareString( LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
810 *str_a, -1, *str_b, -1 );
811 if( r == CSTR_LESS_THAN ) return -1;
812 if( r == CSTR_GREATER_THAN ) return 1;
816 /****************************************************************************
817 * WCMD_setshow_sortenv
819 * sort variables into order for display
821 static void WCMD_setshow_sortenv(const char *s)
823 UINT count=0, len=0, i;
826 /* count the number of strings, and the total length */
828 len += (lstrlen(&s[len]) + 1);
832 /* add the strings to an array */
833 str = LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, count * sizeof (char*) );
837 for( i=1; i<count; i++ )
838 str[i] = str[i-1] + lstrlen(str[i-1]) + 1;
841 qsort( str, count, sizeof (char*), WCMD_compare );
844 for( i=0; i<count; i++ ) {
845 WCMD_output_asis(str[i]);
846 WCMD_output_asis("\n");
852 /****************************************************************************
855 * Set/Show the environment variables
858 void WCMD_setshow_env (char *s) {
865 if (strlen(param1) == 0) {
866 env = GetEnvironmentStrings ();
867 WCMD_setshow_sortenv( env );
873 /* FIXME: Emulate Win98 for now, ie "SET C" looks ONLY for an
874 environment variable C, whereas on NT it shows ALL variables
877 status = GetEnvironmentVariable(s, buffer, sizeof(buffer));
879 WCMD_output_asis( s);
880 WCMD_output_asis( "=");
881 WCMD_output_asis( buffer);
882 WCMD_output_asis( "\n");
884 WCMD_output ("Environment variable %s not defined\n", s);
890 if (strlen(p) == 0) p = NULL;
891 status = SetEnvironmentVariable (s, p);
892 if ((!status) & (GetLastError() != ERROR_ENVVAR_NOT_FOUND)) WCMD_print_error();
894 /* WCMD_output (newline); @JED*/
897 /****************************************************************************
900 * Set/Show the path environment variable
903 void WCMD_setshow_path (char *command) {
908 if (strlen(param1) == 0) {
909 status = GetEnvironmentVariable ("PATH", string, sizeof(string));
911 WCMD_output_asis ( "PATH=");
912 WCMD_output_asis ( string);
913 WCMD_output_asis ( "\n");
916 WCMD_output ("PATH not found\n");
920 status = SetEnvironmentVariable ("PATH", command);
921 if (!status) WCMD_print_error();
925 /****************************************************************************
926 * WCMD_setshow_prompt
928 * Set or show the command prompt.
931 void WCMD_setshow_prompt (void) {
935 if (strlen(param1) == 0) {
936 SetEnvironmentVariable ("PROMPT", NULL);
940 while ((*s == '=') || (*s == ' ')) s++;
941 if (strlen(s) == 0) {
942 SetEnvironmentVariable ("PROMPT", NULL);
944 else SetEnvironmentVariable ("PROMPT", s);
948 /****************************************************************************
951 * Set/Show the system time
952 * FIXME: Can't change time yet
955 void WCMD_setshow_time (void) {
957 char curtime[64], buffer[64];
961 if (strlen(param1) == 0) {
963 if (GetTimeFormat (LOCALE_USER_DEFAULT, 0, &st, NULL,
964 curtime, sizeof(curtime))) {
965 WCMD_output ("Current Time is %s\nEnter new time: ", curtime);
966 ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
971 else WCMD_print_error ();
978 /****************************************************************************
981 * Shift batch parameters.
984 void WCMD_shift (void) {
986 if (context != NULL) context -> shift_count++;
990 /****************************************************************************
993 * Set the console title
995 void WCMD_title (char *command) {
996 SetConsoleTitle(command);
999 /****************************************************************************
1002 * Copy a file to standard output.
1005 void WCMD_type (void) {
1011 h = CreateFile (param1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
1012 FILE_ATTRIBUTE_NORMAL, NULL);
1013 if (h == INVALID_HANDLE_VALUE) {
1014 WCMD_print_error ();
1017 while (ReadFile (h, buffer, sizeof(buffer), &count, NULL)) {
1018 if (count == 0) break; /* ReadFile reports success on EOF! */
1020 WCMD_output_asis (buffer);
1025 /****************************************************************************
1028 * Display verify flag.
1029 * FIXME: We don't actually do anything with the verify flag other than toggle
1033 void WCMD_verify (char *command) {
1035 static const char von[] = "Verify is ON\n", voff[] = "Verify is OFF\n";
1038 count = strlen(command);
1040 if (verify_mode) WCMD_output (von);
1041 else WCMD_output (voff);
1044 if (lstrcmpi(command, "ON") == 0) {
1048 else if (lstrcmpi(command, "OFF") == 0) {
1052 else WCMD_output ("Verify must be ON or OFF\n");
1055 /****************************************************************************
1058 * Display version info.
1061 void WCMD_version (void) {
1063 WCMD_output (version_string);
1067 /****************************************************************************
1070 * Display volume info and/or set volume label. Returns 0 if error.
1073 int WCMD_volume (int mode, char *path) {
1075 DWORD count, serial;
1076 char string[MAX_PATH], label[MAX_PATH], curdir[MAX_PATH];
1079 if (lstrlen(path) == 0) {
1080 status = GetCurrentDirectory (sizeof(curdir), curdir);
1082 WCMD_print_error ();
1085 status = GetVolumeInformation (NULL, label, sizeof(label), &serial, NULL,
1089 if ((path[1] != ':') || (lstrlen(path) != 2)) {
1090 WCMD_output_asis("Syntax Error\n\n");
1093 wsprintf (curdir, "%s\\", path);
1094 status = GetVolumeInformation (curdir, label, sizeof(label), &serial, NULL,
1098 WCMD_print_error ();
1101 WCMD_output ("Volume in drive %c is %s\nVolume Serial Number is %04x-%04x\n\n",
1102 curdir[0], label, HIWORD(serial), LOWORD(serial));
1104 WCMD_output ("Volume label (11 characters, ENTER for none)?");
1105 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
1107 string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
1108 if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
1110 if (lstrlen(path) != 0) {
1111 if (!SetVolumeLabel (curdir, string)) WCMD_print_error ();
1114 if (!SetVolumeLabel (NULL, string)) WCMD_print_error ();