2 * CMD - 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(cmd);
43 void WCMD_execute (char *orig_command, char *parameter, char *substitution);
45 struct env_stack *saved_environment;
46 struct env_stack *pushd_directories;
48 extern HINSTANCE hinst;
49 extern char *inbuilt[];
50 extern int echo_mode, verify_mode;
51 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
52 extern BATCH_CONTEXT *context;
53 extern DWORD errorlevel;
57 /****************************************************************************
60 * Clear the terminal screen.
63 void WCMD_clear_screen (void) {
65 /* Emulate by filling the screen from the top left to bottom right with
66 spaces, then moving the cursor to the top left afterwards */
67 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
68 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
70 if (GetConsoleScreenBufferInfo(hStdOut, &consoleInfo))
75 screenSize = consoleInfo.dwSize.X * (consoleInfo.dwSize.Y + 1);
79 FillConsoleOutputCharacter(hStdOut, ' ', screenSize, topLeft, &screenSize);
80 SetConsoleCursorPosition(hStdOut, topLeft);
84 /****************************************************************************
87 * Change the default i/o device (ie redirect STDin/STDout).
90 void WCMD_change_tty (void) {
96 /****************************************************************************
99 * Copy a file or wildcarded set.
100 * FIXME: No wildcard support
103 void WCMD_copy (void) {
109 static const char overwrite[] = "Overwrite file (Y/N)?";
110 char string[8], outpath[MAX_PATH], inpath[MAX_PATH], *infile, copycmd[3];
113 if (param1[0] == 0x00) {
114 WCMD_output ("Argument missing\n");
118 if ((strchr(param1,'*') != NULL) && (strchr(param1,'%') != NULL)) {
119 WCMD_output ("Wildcards not yet supported\n");
123 /* If no destination supplied, assume current directory */
124 if (param2[0] == 0x00) {
128 GetFullPathName (param2, sizeof(outpath), outpath, NULL);
129 if (outpath[strlen(outpath) - 1] == '\\')
130 outpath[strlen(outpath) - 1] = '\0';
131 hff = FindFirstFile (outpath, &fd);
132 if (hff != INVALID_HANDLE_VALUE) {
133 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
134 GetFullPathName (param1, sizeof(inpath), inpath, &infile);
135 strcat (outpath, "\\");
136 strcat (outpath, infile);
141 /* /-Y has the highest priority, then /Y and finally the COPYCMD env. variable */
142 if (strstr (quals, "/-Y"))
144 else if (strstr (quals, "/Y"))
147 len = GetEnvironmentVariable ("COPYCMD", copycmd, sizeof(copycmd));
148 force = (len && len < sizeof(copycmd) && ! lstrcmpi (copycmd, "/Y"));
152 hff = FindFirstFile (outpath, &fd);
153 if (hff != INVALID_HANDLE_VALUE) {
155 WCMD_output (overwrite);
156 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
157 if (toupper(string[0]) == 'Y') force = TRUE;
162 status = CopyFile (param1, outpath, FALSE);
163 if (!status) WCMD_print_error ();
167 /****************************************************************************
170 * Create a directory.
172 * this works recursivly. so mkdir dir1\dir2\dir3 will create dir1 and dir2 if
173 * they do not already exist.
176 BOOL create_full_path(CHAR* path)
182 new_path = HeapAlloc(GetProcessHeap(),0,strlen(path)+1);
183 strcpy(new_path,path);
185 while ((len = strlen(new_path)) && new_path[len - 1] == '\\')
186 new_path[len - 1] = 0;
188 while (!CreateDirectory(new_path,NULL))
191 DWORD last_error = GetLastError();
192 if (last_error == ERROR_ALREADY_EXISTS)
195 if (last_error != ERROR_PATH_NOT_FOUND)
201 if (!(slash = strrchr(new_path,'\\')) && ! (slash = strrchr(new_path,'/')))
207 len = slash - new_path;
209 if (!create_full_path(new_path))
214 new_path[len] = '\\';
216 HeapFree(GetProcessHeap(),0,new_path);
220 void WCMD_create_dir (void) {
222 if (param1[0] == 0x00) {
223 WCMD_output ("Argument missing\n");
226 if (!create_full_path(param1)) WCMD_print_error ();
229 /****************************************************************************
232 * Delete a file or wildcarded set.
235 * - Testing shows /A is repeatable, eg. /a-r /ar matches all files
236 * - Each set is a pattern, eg /ahr /as-r means
237 * readonly+hidden OR nonreadonly system files
238 * - The '-' applies to a single field, ie /a:-hr means read only
242 void WCMD_delete (int recurse) {
246 char fpath[MAX_PATH];
249 if (param1[0] == 0x00) {
250 WCMD_output ("Argument missing\n");
254 /* If filename part of parameter is * or *.*, prompt unless
256 if ((strstr (quals, "/Q") == NULL) && (strstr (quals, "/P") == NULL)) {
260 char fname[MAX_PATH];
263 /* Convert path into actual directory spec */
264 GetFullPathName (param1, sizeof(fpath), fpath, NULL);
265 WCMD_splitpath(fpath, drive, dir, fname, ext);
267 /* Only prompt for * and *.*, not *a, a*, *.a* etc */
268 if ((strcmp(fname, "*") == 0) &&
269 (*ext == 0x00 || (strcmp(ext, ".*") == 0))) {
271 char question[MAXSTRING];
273 /* Ask for confirmation */
274 sprintf(question, "%s, ", fpath);
275 ok = WCMD_ask_confirm(question, TRUE);
277 /* Abort if answer is 'N' */
282 hff = FindFirstFile (param1, &fd);
283 if (hff == INVALID_HANDLE_VALUE) {
284 WCMD_output ("%s :File Not Found\n",param1);
287 /* Support del <dirname> by just deleting all files dirname\* */
288 if ((strchr(param1,'*') == NULL) && (strchr(param1,'?') == NULL)
289 && (!recurse) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
290 strcat (param1, "\\*");
297 /* Build the filename to delete as <supplied directory>\<findfirst filename> */
298 strcpy (fpath, param1);
300 p = strrchr (fpath, '\\');
303 strcat (fpath, fd.cFileName);
305 else strcpy (fpath, fd.cFileName);
306 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
308 char *nextA = strstr (quals, "/A");
310 /* Handle attribute matching (/A) */
313 while (nextA != NULL && !ok) {
315 char *thisA = (nextA+2);
318 /* Skip optional : */
319 if (*thisA == ':') thisA++;
321 /* Parse each of the /A[:]xxx in turn */
322 while (*thisA && *thisA != '/') {
324 BOOL attribute = FALSE;
326 /* Match negation of attribute first */
332 /* Match attribute */
334 case 'R': attribute = (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY);
336 case 'H': attribute = (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN);
338 case 'S': attribute = (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM);
340 case 'A': attribute = (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE);
343 WCMD_output ("Syntax error\n");
346 /* Now check result, keeping a running boolean about whether it
347 matches all parsed attribues so far */
348 if (attribute && !negate) {
350 } else if (!attribute && negate) {
358 /* Save the running total as the final result */
361 /* Step on to next /A set */
362 nextA = strstr (nextA+1, "/A");
366 /* /P means prompt for each file */
367 if (ok && strstr (quals, "/P") != NULL) {
368 char question[MAXSTRING];
370 /* Ask for confirmation */
371 sprintf(question, "%s, Delete", fpath);
372 ok = WCMD_ask_confirm(question, FALSE);
375 /* Only proceed if ok to */
378 /* If file is read only, and /F supplied, delete it */
379 if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY &&
380 strstr (quals, "/F") != NULL) {
381 SetFileAttributes(fpath, fd.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY);
384 /* Now do the delete */
385 if (!DeleteFile (fpath)) WCMD_print_error ();
389 } while (FindNextFile(hff, &fd) != 0);
394 /****************************************************************************
397 * Echo input to the screen (or not). We don't try to emulate the bugs
398 * in DOS (try typing "ECHO ON AGAIN" for an example).
401 void WCMD_echo (const char *command) {
403 static const char eon[] = "Echo is ON\n", eoff[] = "Echo is OFF\n";
406 if ((command[0] == '.') && (command[1] == 0)) {
407 WCMD_output (newline);
412 count = strlen(command);
414 if (echo_mode) WCMD_output (eon);
415 else WCMD_output (eoff);
418 if (lstrcmpi(command, "ON") == 0) {
422 if (lstrcmpi(command, "OFF") == 0) {
426 WCMD_output_asis (command);
427 WCMD_output (newline);
431 /**************************************************************************
434 * Batch file loop processing.
435 * FIXME: We don't exhaustively check syntax. Any command which works in MessDOS
436 * will probably work here, but the reverse is not necessarily the case...
439 void WCMD_for (char *p) {
444 char set[MAX_PATH], param[MAX_PATH];
447 if (lstrcmpi (WCMD_parameter (p, 1, NULL), "in")
448 || lstrcmpi (WCMD_parameter (p, 3, NULL), "do")
449 || (param1[0] != '%')) {
450 WCMD_output ("Syntax error\n");
453 lstrcpyn (set, WCMD_parameter (p, 2, NULL), sizeof(set));
454 WCMD_parameter (p, 4, &cmd);
455 lstrcpy (param, param1);
458 * If the parameter within the set has a wildcard then search for matching files
459 * otherwise do a literal substitution.
463 while (*(item = WCMD_parameter (set, i, NULL))) {
464 if (strpbrk (item, "*?")) {
465 hff = FindFirstFile (item, &fd);
466 if (hff == INVALID_HANDLE_VALUE) {
470 WCMD_execute (cmd, param, fd.cFileName);
471 } while (FindNextFile(hff, &fd) != 0);
475 WCMD_execute (cmd, param, item);
481 /*****************************************************************************
484 * Execute a command after substituting variable text for the supplied parameter
487 void WCMD_execute (char *orig_cmd, char *param, char *subst) {
489 char *new_cmd, *p, *s, *dup;
492 size = lstrlen (orig_cmd);
493 new_cmd = (char *) LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, size);
494 dup = s = strdup (orig_cmd);
496 while ((p = strstr (s, param))) {
498 size += lstrlen (subst);
499 new_cmd = (char *) LocalReAlloc ((HANDLE)new_cmd, size, 0);
501 strcat (new_cmd, subst);
502 s = p + lstrlen (param);
505 WCMD_process_command (new_cmd);
507 LocalFree ((HANDLE)new_cmd);
511 /**************************************************************************
514 * Simple on-line help. Help text is stored in the resource file.
517 void WCMD_give_help (char *command) {
522 command = WCMD_strtrim_leading_spaces(command);
523 if (lstrlen(command) == 0) {
524 LoadString (hinst, 1000, buffer, sizeof(buffer));
525 WCMD_output_asis (buffer);
528 for (i=0; i<=WCMD_EXIT; i++) {
529 if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
530 param1, -1, inbuilt[i], -1) == 2) {
531 LoadString (hinst, i, buffer, sizeof(buffer));
532 WCMD_output_asis (buffer);
536 WCMD_output ("No help available for %s\n", param1);
541 /****************************************************************************
544 * Batch file jump instruction. Not the most efficient algorithm ;-)
545 * Prints error message if the specified label cannot be found - the file pointer is
546 * then at EOF, effectively stopping the batch file.
547 * FIXME: DOS is supposed to allow labels with spaces - we don't.
550 void WCMD_goto (void) {
552 char string[MAX_PATH];
554 if (param1[0] == 0x00) {
555 WCMD_output ("Argument missing\n");
558 if (context != NULL) {
559 char *paramStart = param1;
561 /* Handle special :EOF label */
562 if (lstrcmpi (":eof", param1) == 0) {
563 context -> skip_rest = TRUE;
567 /* Support goto :label as well as goto label */
568 if (*paramStart == ':') paramStart++;
570 SetFilePointer (context -> h, 0, NULL, FILE_BEGIN);
571 while (WCMD_fgets (string, sizeof(string), context -> h)) {
572 if ((string[0] == ':') && (lstrcmpi (&string[1], paramStart) == 0)) return;
574 WCMD_output ("Target to GOTO not found\n");
579 /*****************************************************************************
582 * Push a directory onto the stack
585 void WCMD_pushd (void) {
586 struct env_stack *curdir;
590 curdir = LocalAlloc (LMEM_FIXED, sizeof (struct env_stack));
591 thisdir = LocalAlloc (LMEM_FIXED, 1024 * sizeof(WCHAR));
592 if( !curdir || !thisdir ) {
595 WCMD_output ("out of memory\n");
599 GetCurrentDirectoryW (1024, thisdir);
600 status = SetCurrentDirectoryA (param1);
607 curdir -> next = pushd_directories;
608 curdir -> strings = thisdir;
609 if (pushd_directories == NULL) {
610 curdir -> stackdepth = 1;
612 curdir -> stackdepth = pushd_directories -> stackdepth + 1;
614 pushd_directories = curdir;
619 /*****************************************************************************
622 * Pop a directory from the stack
625 void WCMD_popd (void) {
626 struct env_stack *temp = pushd_directories;
628 if (!pushd_directories)
631 /* pop the old environment from the stack, and make it the current dir */
632 pushd_directories = temp->next;
633 SetCurrentDirectoryW(temp->strings);
634 LocalFree (temp->strings);
638 /****************************************************************************
641 * Batch file conditional.
642 * FIXME: Much more syntax checking needed!
645 void WCMD_if (char *p) {
647 int negate = 0, test = 0;
648 char condition[MAX_PATH], *command, *s;
650 if (!lstrcmpi (param1, "not")) {
652 lstrcpy (condition, param2);
655 lstrcpy (condition, param1);
657 if (!lstrcmpi (condition, "errorlevel")) {
658 if (errorlevel >= atoi(WCMD_parameter (p, 1+negate, NULL))) test = 1;
659 WCMD_parameter (p, 2+negate, &command);
661 else if (!lstrcmpi (condition, "exist")) {
662 if (GetFileAttributesA(WCMD_parameter (p, 1+negate, NULL)) != INVALID_FILE_ATTRIBUTES) {
665 WCMD_parameter (p, 2+negate, &command);
667 else if (!lstrcmpi (condition, "defined")) {
668 if (GetEnvironmentVariableA(WCMD_parameter (p, 1+negate, NULL), NULL, 0) > 0) {
671 WCMD_parameter (p, 2+negate, &command);
673 else if ((s = strstr (p, "=="))) {
675 if (!lstrcmpi (condition, WCMD_parameter (s, 0, NULL))) test = 1;
676 WCMD_parameter (s, 1, &command);
679 WCMD_output ("Syntax error\n");
682 if (test != negate) {
683 command = strdup (command);
684 WCMD_process_command (command);
689 /****************************************************************************
692 * Move a file, directory tree or wildcarded set of files.
693 * FIXME: Needs input and output files to be fully specified.
696 void WCMD_move (void) {
699 char outpath[MAX_PATH], inpath[MAX_PATH], *infile;
703 if (param1[0] == 0x00) {
704 WCMD_output ("Argument missing\n");
708 if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
709 WCMD_output ("Wildcards not yet supported\n");
713 /* If no destination supplied, assume current directory */
714 if (param2[0] == 0x00) {
718 /* If 2nd parm is directory, then use original filename */
719 GetFullPathName (param2, sizeof(outpath), outpath, NULL);
720 if (outpath[strlen(outpath) - 1] == '\\')
721 outpath[strlen(outpath) - 1] = '\0';
722 hff = FindFirstFile (outpath, &fd);
723 if (hff != INVALID_HANDLE_VALUE) {
724 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
725 GetFullPathName (param1, sizeof(inpath), inpath, &infile);
726 strcat (outpath, "\\");
727 strcat (outpath, infile);
732 status = MoveFile (param1, outpath);
733 if (!status) WCMD_print_error ();
736 /****************************************************************************
739 * Wait for keyboard input.
742 void WCMD_pause (void) {
747 WCMD_output (anykey);
748 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
751 /****************************************************************************
754 * Delete a directory.
757 void WCMD_remove_dir (char *command) {
760 int argsProcessed = 0;
761 char *argN = command;
763 /* Loop through all args */
765 char *thisArg = WCMD_parameter (command, argno++, &argN);
766 if (argN && argN[0] != '/') {
767 WINE_TRACE("rd: Processing arg %s (quals:%s)\n", thisArg, quals);
770 /* If subdirectory search not supplied, just try to remove
771 and report error if it fails (eg if it contains a file) */
772 if (strstr (quals, "/S") == NULL) {
773 if (!RemoveDirectory (thisArg)) WCMD_print_error ();
775 /* Otherwise use ShFileOp to recursively remove a directory */
778 SHFILEOPSTRUCT lpDir;
781 if (strstr (quals, "/Q") == NULL) {
783 char question[MAXSTRING];
785 /* Ask for confirmation */
786 sprintf(question, "%s, ", thisArg);
787 ok = WCMD_ask_confirm(question, TRUE);
789 /* Abort if answer is 'N' */
796 lpDir.pFrom = thisArg;
797 lpDir.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI;
798 lpDir.wFunc = FO_DELETE;
799 if (SHFileOperationA(&lpDir)) WCMD_print_error ();
804 /* Handle no valid args */
805 if (argsProcessed == 0) {
806 WCMD_output ("Argument missing\n");
812 /****************************************************************************
816 * FIXME: Needs input and output files to be fully specified.
819 void WCMD_rename (void) {
823 if (param1[0] == 0x00 || param2[0] == 0x00) {
824 WCMD_output ("Argument missing\n");
827 if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
828 WCMD_output ("Wildcards not yet supported\n");
831 status = MoveFile (param1, param2);
832 if (!status) WCMD_print_error ();
835 /*****************************************************************************
838 * Make a copy of the environment.
840 static WCHAR *WCMD_dupenv( const WCHAR *env )
850 len += (lstrlenW(&env[len]) + 1);
852 env_copy = LocalAlloc (LMEM_FIXED, (len+1) * sizeof (WCHAR) );
855 WCMD_output ("out of memory\n");
858 memcpy (env_copy, env, len*sizeof (WCHAR));
864 /*****************************************************************************
867 * setlocal pushes the environment onto a stack
868 * Save the environment as unicode so we don't screw anything up.
870 void WCMD_setlocal (const char *s) {
872 struct env_stack *env_copy;
874 /* DISABLEEXTENSIONS ignored */
876 env_copy = LocalAlloc (LMEM_FIXED, sizeof (struct env_stack));
879 WCMD_output ("out of memory\n");
883 env = GetEnvironmentStringsW ();
885 env_copy->strings = WCMD_dupenv (env);
886 if (env_copy->strings)
888 env_copy->next = saved_environment;
889 saved_environment = env_copy;
892 LocalFree (env_copy);
894 FreeEnvironmentStringsW (env);
897 /*****************************************************************************
900 static inline WCHAR *WCMD_strchrW(WCHAR *str, WCHAR ch)
911 /*****************************************************************************
914 * endlocal pops the environment off a stack
916 void WCMD_endlocal (void) {
917 WCHAR *env, *old, *p;
918 struct env_stack *temp;
921 if (!saved_environment)
924 /* pop the old environment from the stack */
925 temp = saved_environment;
926 saved_environment = temp->next;
928 /* delete the current environment, totally */
929 env = GetEnvironmentStringsW ();
930 old = WCMD_dupenv (GetEnvironmentStringsW ());
933 n = lstrlenW(&old[len]) + 1;
934 p = WCMD_strchrW(&old[len], '=');
938 SetEnvironmentVariableW (&old[len], NULL);
943 FreeEnvironmentStringsW (env);
945 /* restore old environment */
949 n = lstrlenW(&env[len]) + 1;
950 p = WCMD_strchrW(&env[len], '=');
954 SetEnvironmentVariableW (&env[len], p);
962 /*****************************************************************************
963 * WCMD_setshow_attrib
965 * Display and optionally sets DOS attributes on a file or directory
967 * FIXME: Wine currently uses the Unix stat() function to get file attributes.
968 * As a result only the Readonly flag is correctly reported, the Archive bit
969 * is always set and the rest are not implemented. We do the Right Thing anyway.
971 * FIXME: No SET functionality.
975 void WCMD_setshow_attrib (void) {
980 char flags[9] = {" "};
982 if (param1[0] == '-') {
987 if (lstrlen(param1) == 0) {
988 GetCurrentDirectory (sizeof(param1), param1);
989 strcat (param1, "\\*");
992 hff = FindFirstFile (param1, &fd);
993 if (hff == INVALID_HANDLE_VALUE) {
994 WCMD_output ("%s: File Not Found\n",param1);
998 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
999 if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
1002 if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
1005 if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
1008 if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
1011 if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) {
1014 if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) {
1017 WCMD_output ("%s %s\n", flags, fd.cFileName);
1018 for (count=0; count < 8; count++) flags[count] = ' ';
1020 } while (FindNextFile(hff, &fd) != 0);
1025 /*****************************************************************************
1026 * WCMD_setshow_default
1028 * Set/Show the current default directory
1031 void WCMD_setshow_default (void) {
1036 if (strlen(param1) == 0) {
1037 GetCurrentDirectory (sizeof(string), string);
1038 strcat (string, "\n");
1039 WCMD_output (string);
1042 status = SetCurrentDirectory (param1);
1044 WCMD_print_error ();
1051 /****************************************************************************
1054 * Set/Show the system date
1055 * FIXME: Can't change date yet
1058 void WCMD_setshow_date (void) {
1060 char curdate[64], buffer[64];
1063 if (lstrlen(param1) == 0) {
1064 if (GetDateFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL,
1065 curdate, sizeof(curdate))) {
1066 WCMD_output ("Current Date is %s\nEnter new date: ", curdate);
1067 ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
1072 else WCMD_print_error ();
1079 /****************************************************************************
1082 static int WCMD_compare( const void *a, const void *b )
1085 const char * const *str_a = a, * const *str_b = b;
1086 r = CompareString( LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
1087 *str_a, -1, *str_b, -1 );
1088 if( r == CSTR_LESS_THAN ) return -1;
1089 if( r == CSTR_GREATER_THAN ) return 1;
1093 /****************************************************************************
1094 * WCMD_setshow_sortenv
1096 * sort variables into order for display
1097 * Optionally only display those who start with a stub
1098 * returns the count displayed
1100 static int WCMD_setshow_sortenv(const char *s, const char *stub)
1102 UINT count=0, len=0, i, displayedcount=0, stublen=0;
1105 if (stub) stublen = strlen(stub);
1107 /* count the number of strings, and the total length */
1109 len += (lstrlen(&s[len]) + 1);
1113 /* add the strings to an array */
1114 str = LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, count * sizeof (char*) );
1118 for( i=1; i<count; i++ )
1119 str[i] = str[i-1] + lstrlen(str[i-1]) + 1;
1121 /* sort the array */
1122 qsort( str, count, sizeof (char*), WCMD_compare );
1125 for( i=0; i<count; i++ ) {
1126 if (!stub || CompareString (LOCALE_USER_DEFAULT,
1127 NORM_IGNORECASE | SORT_STRINGSORT,
1128 str[i], stublen, stub, -1) == 2) {
1129 WCMD_output_asis(str[i]);
1130 WCMD_output_asis("\n");
1136 return displayedcount;
1139 /****************************************************************************
1142 * Set/Show the environment variables
1145 void WCMD_setshow_env (char *s) {
1151 if (strlen(param1) == 0) {
1152 env = GetEnvironmentStrings ();
1153 WCMD_setshow_sortenv( env, NULL );
1156 p = strchr (s, '=');
1158 env = GetEnvironmentStrings ();
1159 if (WCMD_setshow_sortenv( env, s ) == 0) {
1160 WCMD_output ("Environment variable %s not defined\n", s);
1166 if (strlen(p) == 0) p = NULL;
1167 status = SetEnvironmentVariable (s, p);
1168 if ((!status) & (GetLastError() != ERROR_ENVVAR_NOT_FOUND)) WCMD_print_error();
1172 /****************************************************************************
1175 * Set/Show the path environment variable
1178 void WCMD_setshow_path (char *command) {
1183 if (strlen(param1) == 0) {
1184 status = GetEnvironmentVariable ("PATH", string, sizeof(string));
1186 WCMD_output_asis ( "PATH=");
1187 WCMD_output_asis ( string);
1188 WCMD_output_asis ( "\n");
1191 WCMD_output ("PATH not found\n");
1195 if (*command == '=') command++; /* Skip leading '=' */
1196 status = SetEnvironmentVariable ("PATH", command);
1197 if (!status) WCMD_print_error();
1201 /****************************************************************************
1202 * WCMD_setshow_prompt
1204 * Set or show the command prompt.
1207 void WCMD_setshow_prompt (void) {
1211 if (strlen(param1) == 0) {
1212 SetEnvironmentVariable ("PROMPT", NULL);
1216 while ((*s == '=') || (*s == ' ')) s++;
1217 if (strlen(s) == 0) {
1218 SetEnvironmentVariable ("PROMPT", NULL);
1220 else SetEnvironmentVariable ("PROMPT", s);
1224 /****************************************************************************
1227 * Set/Show the system time
1228 * FIXME: Can't change time yet
1231 void WCMD_setshow_time (void) {
1233 char curtime[64], buffer[64];
1237 if (strlen(param1) == 0) {
1239 if (GetTimeFormat (LOCALE_USER_DEFAULT, 0, &st, NULL,
1240 curtime, sizeof(curtime))) {
1241 WCMD_output ("Current Time is %s\nEnter new time: ", curtime);
1242 ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
1247 else WCMD_print_error ();
1254 /****************************************************************************
1257 * Shift batch parameters.
1260 void WCMD_shift (void) {
1262 if (context != NULL) context -> shift_count++;
1266 /****************************************************************************
1269 * Set the console title
1271 void WCMD_title (char *command) {
1272 SetConsoleTitle(command);
1275 /****************************************************************************
1278 * Copy a file to standard output.
1281 void WCMD_type (void) {
1287 if (param1[0] == 0x00) {
1288 WCMD_output ("Argument missing\n");
1291 h = CreateFile (param1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
1292 FILE_ATTRIBUTE_NORMAL, NULL);
1293 if (h == INVALID_HANDLE_VALUE) {
1294 WCMD_print_error ();
1297 while (ReadFile (h, buffer, sizeof(buffer), &count, NULL)) {
1298 if (count == 0) break; /* ReadFile reports success on EOF! */
1300 WCMD_output_asis (buffer);
1305 /****************************************************************************
1308 * Display verify flag.
1309 * FIXME: We don't actually do anything with the verify flag other than toggle
1313 void WCMD_verify (char *command) {
1315 static const char von[] = "Verify is ON\n", voff[] = "Verify is OFF\n";
1318 count = strlen(command);
1320 if (verify_mode) WCMD_output (von);
1321 else WCMD_output (voff);
1324 if (lstrcmpi(command, "ON") == 0) {
1328 else if (lstrcmpi(command, "OFF") == 0) {
1332 else WCMD_output ("Verify must be ON or OFF\n");
1335 /****************************************************************************
1338 * Display version info.
1341 void WCMD_version (void) {
1343 WCMD_output (version_string);
1347 /****************************************************************************
1350 * Display volume info and/or set volume label. Returns 0 if error.
1353 int WCMD_volume (int mode, char *path) {
1355 DWORD count, serial;
1356 char string[MAX_PATH], label[MAX_PATH], curdir[MAX_PATH];
1359 if (lstrlen(path) == 0) {
1360 status = GetCurrentDirectory (sizeof(curdir), curdir);
1362 WCMD_print_error ();
1365 status = GetVolumeInformation (NULL, label, sizeof(label), &serial, NULL,
1369 if ((path[1] != ':') || (lstrlen(path) != 2)) {
1370 WCMD_output_asis("Syntax Error\n\n");
1373 wsprintf (curdir, "%s\\", path);
1374 status = GetVolumeInformation (curdir, label, sizeof(label), &serial, NULL,
1378 WCMD_print_error ();
1381 WCMD_output ("Volume in drive %c is %s\nVolume Serial Number is %04x-%04x\n\n",
1382 curdir[0], label, HIWORD(serial), LOWORD(serial));
1384 WCMD_output ("Volume label (11 characters, ENTER for none)?");
1385 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
1387 string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
1388 if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
1390 if (lstrlen(path) != 0) {
1391 if (!SetVolumeLabel (curdir, string)) WCMD_print_error ();
1394 if (!SetVolumeLabel (NULL, string)) WCMD_print_error ();
1400 /**************************************************************************
1403 * Exit either the process, or just this batch program
1407 void WCMD_exit (void) {
1409 int rc = atoi(param1); /* Note: atoi of empty parameter is 0 */
1411 if (context && lstrcmpi(quals, "/B") == 0) {
1413 context -> skip_rest = TRUE;
1419 /**************************************************************************
1422 * Issue a message and ask 'Are you sure (Y/N)', waiting on a valid
1425 * Returns True if Y answer is selected
1428 BOOL WCMD_ask_confirm (char *message, BOOL showSureText) {
1430 char msgbuffer[MAXSTRING];
1431 char Ybuffer[MAXSTRING];
1432 char Nbuffer[MAXSTRING];
1433 char answer[MAX_PATH] = "";
1436 /* Load the translated 'Are you sure', plus valid answers */
1437 LoadString (hinst, WCMD_CONFIRM, msgbuffer, sizeof(msgbuffer));
1438 LoadString (hinst, WCMD_YES, Ybuffer, sizeof(Ybuffer));
1439 LoadString (hinst, WCMD_NO, Nbuffer, sizeof(Nbuffer));
1441 /* Loop waiting on a Y or N */
1442 while (answer[0] != Ybuffer[0] && answer[0] != Nbuffer[0]) {
1443 WCMD_output_asis (message);
1445 WCMD_output_asis (msgbuffer);
1447 WCMD_output_asis (" (");
1448 WCMD_output_asis (Ybuffer);
1449 WCMD_output_asis ("/");
1450 WCMD_output_asis (Nbuffer);
1451 WCMD_output_asis (")?");
1452 ReadFile (GetStdHandle(STD_INPUT_HANDLE), answer, sizeof(answer),
1454 answer[0] = toupper(answer[0]);
1457 /* Return the answer */
1458 return (answer[0] == Ybuffer[0]);