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
40 void WCMD_execute (char *orig_command, char *parameter, char *substitution);
44 struct env_stack *next;
48 struct env_stack *saved_environment;
49 struct env_stack *pushd_directories;
51 extern HINSTANCE hinst;
52 extern char *inbuilt[];
53 extern int echo_mode, verify_mode;
54 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
55 extern BATCH_CONTEXT *context;
56 extern DWORD errorlevel;
60 /****************************************************************************
63 * Clear the terminal screen.
66 void WCMD_clear_screen (void) {
68 /* Emulate by filling the screen from the top left to bottom right with
69 spaces, then moving the cursor to the top left afterwards */
70 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
71 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
73 if (GetConsoleScreenBufferInfo(hStdOut, &consoleInfo))
78 screenSize = consoleInfo.dwSize.X * (consoleInfo.dwSize.Y + 1);
82 FillConsoleOutputCharacter(hStdOut, ' ', screenSize, topLeft, &screenSize);
83 SetConsoleCursorPosition(hStdOut, topLeft);
87 /****************************************************************************
90 * Change the default i/o device (ie redirect STDin/STDout).
93 void WCMD_change_tty (void) {
99 /****************************************************************************
102 * Copy a file or wildcarded set.
103 * FIXME: No wildcard support
106 void WCMD_copy (void) {
112 static const char overwrite[] = "Overwrite file (Y/N)?";
113 char string[8], outpath[MAX_PATH], inpath[MAX_PATH], *infile;
115 if (param1[0] == 0x00) {
116 WCMD_output ("Argument missing\n");
120 if ((strchr(param1,'*') != NULL) && (strchr(param1,'%') != NULL)) {
121 WCMD_output ("Wildcards not yet supported\n");
125 /* If no destination supplied, assume current directory */
126 if (param2[0] == 0x00) {
130 GetFullPathName (param2, sizeof(outpath), outpath, NULL);
131 if (outpath[strlen(outpath) - 1] == '\\')
132 outpath[strlen(outpath) - 1] = '\0';
133 hff = FindFirstFile (outpath, &fd);
134 if (hff != INVALID_HANDLE_VALUE) {
135 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
136 GetFullPathName (param1, sizeof(inpath), inpath, &infile);
137 strcat (outpath, "\\");
138 strcat (outpath, infile);
143 force = (strstr (quals, "/Y") != NULL);
145 hff = FindFirstFile (outpath, &fd);
146 if (hff != INVALID_HANDLE_VALUE) {
148 WCMD_output (overwrite);
149 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
150 if (toupper(string[0]) == 'Y') force = TRUE;
155 status = CopyFile (param1, outpath, FALSE);
156 if (!status) WCMD_print_error ();
160 /****************************************************************************
163 * Create a directory.
165 * this works recursivly. so mkdir dir1\dir2\dir3 will create dir1 and dir2 if
166 * they do not already exist.
169 BOOL create_full_path(CHAR* path)
175 new_path = HeapAlloc(GetProcessHeap(),0,strlen(path)+1);
176 strcpy(new_path,path);
178 while ((len = strlen(new_path)) && new_path[len - 1] == '\\')
179 new_path[len - 1] = 0;
181 while (!CreateDirectory(new_path,NULL))
184 DWORD last_error = GetLastError();
185 if (last_error == ERROR_ALREADY_EXISTS)
188 if (last_error != ERROR_PATH_NOT_FOUND)
194 if (!(slash = strrchr(new_path,'\\')) && ! (slash = strrchr(new_path,'/')))
200 len = slash - new_path;
202 if (!create_full_path(new_path))
207 new_path[len] = '\\';
209 HeapFree(GetProcessHeap(),0,new_path);
213 void WCMD_create_dir (void) {
215 if (param1[0] == 0x00) {
216 WCMD_output ("Argument missing\n");
219 if (!create_full_path(param1)) WCMD_print_error ();
222 /****************************************************************************
225 * Delete a file or wildcarded set.
229 void WCMD_delete (int recurse) {
233 char fpath[MAX_PATH];
236 if (param1[0] == 0x00) {
237 WCMD_output ("Argument missing\n");
241 /* If filename part of parameter is * or *.*, prompt unless
243 if ((strstr (quals, "/Q") == NULL) && (strstr (quals, "/P") == NULL)) {
247 char fname[MAX_PATH];
250 /* Convert path into actual directory spec */
251 GetFullPathName (param1, sizeof(fpath), fpath, NULL);
252 WCMD_splitpath(fpath, drive, dir, fname, ext);
254 /* Only prompt for * and *.*, not *a, a*, *.a* etc */
255 if ((strcmp(fname, "*") == 0) &&
256 (*ext == 0x00 || (strcmp(ext, ".*") == 0))) {
258 char question[MAXSTRING];
260 /* Ask for confirmation */
261 sprintf(question, "%s, ", fpath);
262 ok = WCMD_ask_confirm(question, TRUE);
264 /* Abort if answer is 'N' */
269 hff = FindFirstFile (param1, &fd);
270 if (hff == INVALID_HANDLE_VALUE) {
271 WCMD_output ("%s :File Not Found\n",param1);
274 if ((strchr(param1,'*') == NULL) && (strchr(param1,'?') == NULL)
275 && (!recurse) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
276 strcat (param1, "\\*");
281 if ((strchr(param1,'*') != NULL) || (strchr(param1,'?') != NULL)) {
282 strcpy (fpath, param1);
284 p = strrchr (fpath, '\\');
287 strcat (fpath, fd.cFileName);
289 else strcpy (fpath, fd.cFileName);
290 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
291 /* /P means prompt for each file */
292 if (strstr (quals, "/P") != NULL) {
294 char question[MAXSTRING];
296 /* Ask for confirmation */
297 sprintf(question, "%s, Delete", fpath);
298 ok = WCMD_ask_confirm(question, FALSE);
300 /* Only delete if answer is 'Y' */
301 if (ok && !DeleteFile (fpath)) WCMD_print_error ();
303 if (!DeleteFile (fpath)) WCMD_print_error ();
306 } while (FindNextFile(hff, &fd) != 0);
310 if (!DeleteFile (param1)) WCMD_print_error ();
315 /****************************************************************************
318 * Echo input to the screen (or not). We don't try to emulate the bugs
319 * in DOS (try typing "ECHO ON AGAIN" for an example).
322 void WCMD_echo (const char *command) {
324 static const char eon[] = "Echo is ON\n", eoff[] = "Echo is OFF\n";
327 if ((command[0] == '.') && (command[1] == 0)) {
328 WCMD_output (newline);
333 count = strlen(command);
335 if (echo_mode) WCMD_output (eon);
336 else WCMD_output (eoff);
339 if (lstrcmpi(command, "ON") == 0) {
343 if (lstrcmpi(command, "OFF") == 0) {
347 WCMD_output_asis (command);
348 WCMD_output (newline);
352 /**************************************************************************
355 * Batch file loop processing.
356 * FIXME: We don't exhaustively check syntax. Any command which works in MessDOS
357 * will probably work here, but the reverse is not necessarily the case...
360 void WCMD_for (char *p) {
365 char set[MAX_PATH], param[MAX_PATH];
368 if (lstrcmpi (WCMD_parameter (p, 1, NULL), "in")
369 || lstrcmpi (WCMD_parameter (p, 3, NULL), "do")
370 || (param1[0] != '%')) {
371 WCMD_output ("Syntax error\n");
374 lstrcpyn (set, WCMD_parameter (p, 2, NULL), sizeof(set));
375 WCMD_parameter (p, 4, &cmd);
376 lstrcpy (param, param1);
379 * If the parameter within the set has a wildcard then search for matching files
380 * otherwise do a literal substitution.
384 while (*(item = WCMD_parameter (set, i, NULL))) {
385 if (strpbrk (item, "*?")) {
386 hff = FindFirstFile (item, &fd);
387 if (hff == INVALID_HANDLE_VALUE) {
391 WCMD_execute (cmd, param, fd.cFileName);
392 } while (FindNextFile(hff, &fd) != 0);
396 WCMD_execute (cmd, param, item);
402 /*****************************************************************************
405 * Execute a command after substituting variable text for the supplied parameter
408 void WCMD_execute (char *orig_cmd, char *param, char *subst) {
410 char *new_cmd, *p, *s, *dup;
413 size = lstrlen (orig_cmd);
414 new_cmd = (char *) LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, size);
415 dup = s = strdup (orig_cmd);
417 while ((p = strstr (s, param))) {
419 size += lstrlen (subst);
420 new_cmd = (char *) LocalReAlloc ((HANDLE)new_cmd, size, 0);
422 strcat (new_cmd, subst);
423 s = p + lstrlen (param);
426 WCMD_process_command (new_cmd);
428 LocalFree ((HANDLE)new_cmd);
432 /**************************************************************************
435 * Simple on-line help. Help text is stored in the resource file.
438 void WCMD_give_help (char *command) {
443 command = WCMD_strtrim_leading_spaces(command);
444 if (lstrlen(command) == 0) {
445 LoadString (hinst, 1000, buffer, sizeof(buffer));
446 WCMD_output_asis (buffer);
449 for (i=0; i<=WCMD_EXIT; i++) {
450 if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
451 param1, -1, inbuilt[i], -1) == 2) {
452 LoadString (hinst, i, buffer, sizeof(buffer));
453 WCMD_output_asis (buffer);
457 WCMD_output ("No help available for %s\n", param1);
462 /****************************************************************************
465 * Batch file jump instruction. Not the most efficient algorithm ;-)
466 * Prints error message if the specified label cannot be found - the file pointer is
467 * then at EOF, effectively stopping the batch file.
468 * FIXME: DOS is supposed to allow labels with spaces - we don't.
471 void WCMD_goto (void) {
473 char string[MAX_PATH];
475 if (param1[0] == 0x00) {
476 WCMD_output ("Argument missing\n");
479 if (context != NULL) {
480 char *paramStart = param1;
482 /* Handle special :EOF label */
483 if (lstrcmpi (":eof", param1) == 0) {
484 context -> skip_rest = TRUE;
488 /* Support goto :label as well as goto label */
489 if (*paramStart == ':') paramStart++;
491 SetFilePointer (context -> h, 0, NULL, FILE_BEGIN);
492 while (WCMD_fgets (string, sizeof(string), context -> h)) {
493 if ((string[0] == ':') && (lstrcmpi (&string[1], paramStart) == 0)) return;
495 WCMD_output ("Target to GOTO not found\n");
500 /*****************************************************************************
503 * Push a directory onto the stack
506 void WCMD_pushd (void) {
507 struct env_stack *curdir;
511 curdir = LocalAlloc (LMEM_FIXED, sizeof (struct env_stack));
512 thisdir = LocalAlloc (LMEM_FIXED, 1024 * sizeof(WCHAR));
513 if( !curdir || !thisdir ) {
516 WCMD_output ("out of memory\n");
520 GetCurrentDirectoryW (1024, thisdir);
521 status = SetCurrentDirectoryA (param1);
528 curdir -> next = pushd_directories;
529 curdir -> strings = thisdir;
530 pushd_directories = curdir;
535 /*****************************************************************************
538 * Pop a directory from the stack
541 void WCMD_popd (void) {
542 struct env_stack *temp = pushd_directories;
544 if (!pushd_directories)
547 /* pop the old environment from the stack, and make it the current dir */
548 pushd_directories = temp->next;
549 SetCurrentDirectoryW(temp->strings);
550 LocalFree (temp->strings);
554 /****************************************************************************
557 * Batch file conditional.
558 * FIXME: Much more syntax checking needed!
561 void WCMD_if (char *p) {
563 int negate = 0, test = 0;
564 char condition[MAX_PATH], *command, *s;
566 if (!lstrcmpi (param1, "not")) {
568 lstrcpy (condition, param2);
571 lstrcpy (condition, param1);
573 if (!lstrcmpi (condition, "errorlevel")) {
574 if (errorlevel >= atoi(WCMD_parameter (p, 1+negate, NULL))) test = 1;
575 WCMD_parameter (p, 2+negate, &command);
577 else if (!lstrcmpi (condition, "exist")) {
578 if (GetFileAttributesA(WCMD_parameter (p, 1+negate, NULL)) != INVALID_FILE_ATTRIBUTES) {
581 WCMD_parameter (p, 2+negate, &command);
583 else if (!lstrcmpi (condition, "defined")) {
584 if (GetEnvironmentVariableA(WCMD_parameter (p, 1+negate, NULL), NULL, 0) > 0) {
587 WCMD_parameter (p, 2+negate, &command);
589 else if ((s = strstr (p, "=="))) {
591 if (!lstrcmpi (condition, WCMD_parameter (s, 0, NULL))) test = 1;
592 WCMD_parameter (s, 1, &command);
595 WCMD_output ("Syntax error\n");
598 if (test != negate) {
599 command = strdup (command);
600 WCMD_process_command (command);
605 /****************************************************************************
608 * Move a file, directory tree or wildcarded set of files.
609 * FIXME: Needs input and output files to be fully specified.
612 void WCMD_move (void) {
615 char outpath[MAX_PATH], inpath[MAX_PATH], *infile;
619 if (param1[0] == 0x00) {
620 WCMD_output ("Argument missing\n");
624 if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
625 WCMD_output ("Wildcards not yet supported\n");
629 /* If no destination supplied, assume current directory */
630 if (param2[0] == 0x00) {
634 /* If 2nd parm is directory, then use original filename */
635 GetFullPathName (param2, sizeof(outpath), outpath, NULL);
636 if (outpath[strlen(outpath) - 1] == '\\')
637 outpath[strlen(outpath) - 1] = '\0';
638 hff = FindFirstFile (outpath, &fd);
639 if (hff != INVALID_HANDLE_VALUE) {
640 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
641 GetFullPathName (param1, sizeof(inpath), inpath, &infile);
642 strcat (outpath, "\\");
643 strcat (outpath, infile);
648 status = MoveFile (param1, outpath);
649 if (!status) WCMD_print_error ();
652 /****************************************************************************
655 * Wait for keyboard input.
658 void WCMD_pause (void) {
663 WCMD_output (anykey);
664 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
667 /****************************************************************************
670 * Delete a directory.
673 void WCMD_remove_dir (void) {
675 if (param1[0] == 0x00) {
676 WCMD_output ("Argument missing\n");
680 /* If subdirectory search not supplied, just try to remove
681 and report error if it fails (eg if it contains a file) */
682 if (strstr (quals, "/S") == NULL) {
683 if (!RemoveDirectory (param1)) WCMD_print_error ();
685 /* Otherwise use ShFileOp to recursively remove a directory */
688 SHFILEOPSTRUCT lpDir;
691 if (strstr (quals, "/Q") == NULL) {
693 char question[MAXSTRING];
695 /* Ask for confirmation */
696 sprintf(question, "%s, ", param1);
697 ok = WCMD_ask_confirm(question, TRUE);
699 /* Abort if answer is 'N' */
706 lpDir.pFrom = param1;
707 lpDir.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI;
708 lpDir.wFunc = FO_DELETE;
709 if (SHFileOperationA(&lpDir)) WCMD_print_error ();
713 /****************************************************************************
717 * FIXME: Needs input and output files to be fully specified.
720 void WCMD_rename (void) {
724 if (param1[0] == 0x00 || param2[0] == 0x00) {
725 WCMD_output ("Argument missing\n");
728 if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
729 WCMD_output ("Wildcards not yet supported\n");
732 status = MoveFile (param1, param2);
733 if (!status) WCMD_print_error ();
736 /*****************************************************************************
739 * Make a copy of the environment.
741 static WCHAR *WCMD_dupenv( const WCHAR *env )
751 len += (lstrlenW(&env[len]) + 1);
753 env_copy = LocalAlloc (LMEM_FIXED, (len+1) * sizeof (WCHAR) );
756 WCMD_output ("out of memory\n");
759 memcpy (env_copy, env, len*sizeof (WCHAR));
765 /*****************************************************************************
768 * setlocal pushes the environment onto a stack
769 * Save the environment as unicode so we don't screw anything up.
771 void WCMD_setlocal (const char *s) {
773 struct env_stack *env_copy;
775 /* DISABLEEXTENSIONS ignored */
777 env_copy = LocalAlloc (LMEM_FIXED, sizeof (struct env_stack));
780 WCMD_output ("out of memory\n");
784 env = GetEnvironmentStringsW ();
786 env_copy->strings = WCMD_dupenv (env);
787 if (env_copy->strings)
789 env_copy->next = saved_environment;
790 saved_environment = env_copy;
793 LocalFree (env_copy);
795 FreeEnvironmentStringsW (env);
798 /*****************************************************************************
801 static inline WCHAR *WCMD_strchrW(WCHAR *str, WCHAR ch)
812 /*****************************************************************************
815 * endlocal pops the environment off a stack
817 void WCMD_endlocal (void) {
818 WCHAR *env, *old, *p;
819 struct env_stack *temp;
822 if (!saved_environment)
825 /* pop the old environment from the stack */
826 temp = saved_environment;
827 saved_environment = temp->next;
829 /* delete the current environment, totally */
830 env = GetEnvironmentStringsW ();
831 old = WCMD_dupenv (GetEnvironmentStringsW ());
834 n = lstrlenW(&old[len]) + 1;
835 p = WCMD_strchrW(&old[len], '=');
839 SetEnvironmentVariableW (&old[len], NULL);
844 FreeEnvironmentStringsW (env);
846 /* restore old environment */
850 n = lstrlenW(&env[len]) + 1;
851 p = WCMD_strchrW(&env[len], '=');
855 SetEnvironmentVariableW (&env[len], p);
863 /*****************************************************************************
864 * WCMD_setshow_attrib
866 * Display and optionally sets DOS attributes on a file or directory
868 * FIXME: Wine currently uses the Unix stat() function to get file attributes.
869 * As a result only the Readonly flag is correctly reported, the Archive bit
870 * is always set and the rest are not implemented. We do the Right Thing anyway.
872 * FIXME: No SET functionality.
876 void WCMD_setshow_attrib (void) {
881 char flags[9] = {" "};
883 if (param1[0] == '-') {
888 if (lstrlen(param1) == 0) {
889 GetCurrentDirectory (sizeof(param1), param1);
890 strcat (param1, "\\*");
893 hff = FindFirstFile (param1, &fd);
894 if (hff == INVALID_HANDLE_VALUE) {
895 WCMD_output ("%s: File Not Found\n",param1);
899 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
900 if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
903 if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
906 if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
909 if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
912 if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) {
915 if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) {
918 WCMD_output ("%s %s\n", flags, fd.cFileName);
919 for (count=0; count < 8; count++) flags[count] = ' ';
921 } while (FindNextFile(hff, &fd) != 0);
926 /*****************************************************************************
927 * WCMD_setshow_default
929 * Set/Show the current default directory
932 void WCMD_setshow_default (void) {
937 if (strlen(param1) == 0) {
938 GetCurrentDirectory (sizeof(string), string);
939 strcat (string, "\n");
940 WCMD_output (string);
943 status = SetCurrentDirectory (param1);
952 /****************************************************************************
955 * Set/Show the system date
956 * FIXME: Can't change date yet
959 void WCMD_setshow_date (void) {
961 char curdate[64], buffer[64];
964 if (lstrlen(param1) == 0) {
965 if (GetDateFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL,
966 curdate, sizeof(curdate))) {
967 WCMD_output ("Current Date is %s\nEnter new date: ", curdate);
968 ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
973 else WCMD_print_error ();
980 /****************************************************************************
983 static int WCMD_compare( const void *a, const void *b )
986 const char * const *str_a = a, * const *str_b = b;
987 r = CompareString( LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
988 *str_a, -1, *str_b, -1 );
989 if( r == CSTR_LESS_THAN ) return -1;
990 if( r == CSTR_GREATER_THAN ) return 1;
994 /****************************************************************************
995 * WCMD_setshow_sortenv
997 * sort variables into order for display
998 * Optionally only display those who start with a stub
999 * returns the count displayed
1001 static int WCMD_setshow_sortenv(const char *s, const char *stub)
1003 UINT count=0, len=0, i, displayedcount=0, stublen=0;
1006 if (stub) stublen = strlen(stub);
1008 /* count the number of strings, and the total length */
1010 len += (lstrlen(&s[len]) + 1);
1014 /* add the strings to an array */
1015 str = LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, count * sizeof (char*) );
1019 for( i=1; i<count; i++ )
1020 str[i] = str[i-1] + lstrlen(str[i-1]) + 1;
1022 /* sort the array */
1023 qsort( str, count, sizeof (char*), WCMD_compare );
1026 for( i=0; i<count; i++ ) {
1027 if (!stub || CompareString (LOCALE_USER_DEFAULT,
1028 NORM_IGNORECASE | SORT_STRINGSORT,
1029 str[i], stublen, stub, -1) == 2) {
1030 WCMD_output_asis(str[i]);
1031 WCMD_output_asis("\n");
1037 return displayedcount;
1040 /****************************************************************************
1043 * Set/Show the environment variables
1046 void WCMD_setshow_env (char *s) {
1052 if (strlen(param1) == 0) {
1053 env = GetEnvironmentStrings ();
1054 WCMD_setshow_sortenv( env, NULL );
1057 p = strchr (s, '=');
1059 env = GetEnvironmentStrings ();
1060 if (WCMD_setshow_sortenv( env, s ) == 0) {
1061 WCMD_output ("Environment variable %s not defined\n", s);
1067 if (strlen(p) == 0) p = NULL;
1068 status = SetEnvironmentVariable (s, p);
1069 if ((!status) & (GetLastError() != ERROR_ENVVAR_NOT_FOUND)) WCMD_print_error();
1073 /****************************************************************************
1076 * Set/Show the path environment variable
1079 void WCMD_setshow_path (char *command) {
1084 if (strlen(param1) == 0) {
1085 status = GetEnvironmentVariable ("PATH", string, sizeof(string));
1087 WCMD_output_asis ( "PATH=");
1088 WCMD_output_asis ( string);
1089 WCMD_output_asis ( "\n");
1092 WCMD_output ("PATH not found\n");
1096 if (*command == '=') command++; /* Skip leading '=' */
1097 status = SetEnvironmentVariable ("PATH", command);
1098 if (!status) WCMD_print_error();
1102 /****************************************************************************
1103 * WCMD_setshow_prompt
1105 * Set or show the command prompt.
1108 void WCMD_setshow_prompt (void) {
1112 if (strlen(param1) == 0) {
1113 SetEnvironmentVariable ("PROMPT", NULL);
1117 while ((*s == '=') || (*s == ' ')) s++;
1118 if (strlen(s) == 0) {
1119 SetEnvironmentVariable ("PROMPT", NULL);
1121 else SetEnvironmentVariable ("PROMPT", s);
1125 /****************************************************************************
1128 * Set/Show the system time
1129 * FIXME: Can't change time yet
1132 void WCMD_setshow_time (void) {
1134 char curtime[64], buffer[64];
1138 if (strlen(param1) == 0) {
1140 if (GetTimeFormat (LOCALE_USER_DEFAULT, 0, &st, NULL,
1141 curtime, sizeof(curtime))) {
1142 WCMD_output ("Current Time is %s\nEnter new time: ", curtime);
1143 ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
1148 else WCMD_print_error ();
1155 /****************************************************************************
1158 * Shift batch parameters.
1161 void WCMD_shift (void) {
1163 if (context != NULL) context -> shift_count++;
1167 /****************************************************************************
1170 * Set the console title
1172 void WCMD_title (char *command) {
1173 SetConsoleTitle(command);
1176 /****************************************************************************
1179 * Copy a file to standard output.
1182 void WCMD_type (void) {
1188 if (param1[0] == 0x00) {
1189 WCMD_output ("Argument missing\n");
1192 h = CreateFile (param1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
1193 FILE_ATTRIBUTE_NORMAL, NULL);
1194 if (h == INVALID_HANDLE_VALUE) {
1195 WCMD_print_error ();
1198 while (ReadFile (h, buffer, sizeof(buffer), &count, NULL)) {
1199 if (count == 0) break; /* ReadFile reports success on EOF! */
1201 WCMD_output_asis (buffer);
1206 /****************************************************************************
1209 * Display verify flag.
1210 * FIXME: We don't actually do anything with the verify flag other than toggle
1214 void WCMD_verify (char *command) {
1216 static const char von[] = "Verify is ON\n", voff[] = "Verify is OFF\n";
1219 count = strlen(command);
1221 if (verify_mode) WCMD_output (von);
1222 else WCMD_output (voff);
1225 if (lstrcmpi(command, "ON") == 0) {
1229 else if (lstrcmpi(command, "OFF") == 0) {
1233 else WCMD_output ("Verify must be ON or OFF\n");
1236 /****************************************************************************
1239 * Display version info.
1242 void WCMD_version (void) {
1244 WCMD_output (version_string);
1248 /****************************************************************************
1251 * Display volume info and/or set volume label. Returns 0 if error.
1254 int WCMD_volume (int mode, char *path) {
1256 DWORD count, serial;
1257 char string[MAX_PATH], label[MAX_PATH], curdir[MAX_PATH];
1260 if (lstrlen(path) == 0) {
1261 status = GetCurrentDirectory (sizeof(curdir), curdir);
1263 WCMD_print_error ();
1266 status = GetVolumeInformation (NULL, label, sizeof(label), &serial, NULL,
1270 if ((path[1] != ':') || (lstrlen(path) != 2)) {
1271 WCMD_output_asis("Syntax Error\n\n");
1274 wsprintf (curdir, "%s\\", path);
1275 status = GetVolumeInformation (curdir, label, sizeof(label), &serial, NULL,
1279 WCMD_print_error ();
1282 WCMD_output ("Volume in drive %c is %s\nVolume Serial Number is %04x-%04x\n\n",
1283 curdir[0], label, HIWORD(serial), LOWORD(serial));
1285 WCMD_output ("Volume label (11 characters, ENTER for none)?");
1286 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
1288 string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
1289 if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
1291 if (lstrlen(path) != 0) {
1292 if (!SetVolumeLabel (curdir, string)) WCMD_print_error ();
1295 if (!SetVolumeLabel (NULL, string)) WCMD_print_error ();
1301 /**************************************************************************
1304 * Exit either the process, or just this batch program
1308 void WCMD_exit (void) {
1310 int rc = atoi(param1); /* Note: atoi of empty parameter is 0 */
1312 if (context && lstrcmpi(quals, "/B") == 0) {
1314 context -> skip_rest = TRUE;
1320 /**************************************************************************
1323 * Issue a message and ask 'Are you sure (Y/N)', waiting on a valid
1326 * Returns True if Y answer is selected
1329 BOOL WCMD_ask_confirm (char *message, BOOL showSureText) {
1331 char msgbuffer[MAXSTRING];
1332 char Ybuffer[MAXSTRING];
1333 char Nbuffer[MAXSTRING];
1334 char answer[MAX_PATH] = "";
1337 /* Load the translated 'Are you sure', plus valid answers */
1338 LoadString (hinst, WCMD_CONFIRM, msgbuffer, sizeof(msgbuffer));
1339 LoadString (hinst, WCMD_YES, Ybuffer, sizeof(Ybuffer));
1340 LoadString (hinst, WCMD_NO, Nbuffer, sizeof(Nbuffer));
1342 /* Loop waiting on a Y or N */
1343 while (answer[0] != Ybuffer[0] && answer[0] != Nbuffer[0]) {
1344 WCMD_output_asis (message);
1346 WCMD_output_asis (msgbuffer);
1348 WCMD_output_asis (" (");
1349 WCMD_output_asis (Ybuffer);
1350 WCMD_output_asis ("/");
1351 WCMD_output_asis (Nbuffer);
1352 WCMD_output_asis (")?");
1353 ReadFile (GetStdHandle(STD_INPUT_HANDLE), answer, sizeof(answer),
1355 answer[0] = toupper(answer[0]);
1358 /* Return the answer */
1359 return (answer[0] == Ybuffer[0]);