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 /* Support del <dirname> by just deleting all files dirname\* */
275 if ((strchr(param1,'*') == NULL) && (strchr(param1,'?') == NULL)
276 && (!recurse) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
277 strcat (param1, "\\*");
284 /* Build the filename to delete as <supplied directory>\<findfirst filename> */
285 strcpy (fpath, param1);
287 p = strrchr (fpath, '\\');
290 strcat (fpath, fd.cFileName);
292 else strcpy (fpath, fd.cFileName);
293 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
296 /* /P means prompt for each file */
297 if (strstr (quals, "/P") != NULL) {
298 char question[MAXSTRING];
300 /* Ask for confirmation */
301 sprintf(question, "%s, Delete", fpath);
302 ok = WCMD_ask_confirm(question, FALSE);
305 /* Only proceed if ok to */
308 /* If file is read only, and /F supplied, delete it */
309 if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY &&
310 strstr (quals, "/F") != NULL) {
311 SetFileAttributes(fpath, fd.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY);
314 /* Now do the delete */
315 if (!DeleteFile (fpath)) WCMD_print_error ();
319 } while (FindNextFile(hff, &fd) != 0);
324 /****************************************************************************
327 * Echo input to the screen (or not). We don't try to emulate the bugs
328 * in DOS (try typing "ECHO ON AGAIN" for an example).
331 void WCMD_echo (const char *command) {
333 static const char eon[] = "Echo is ON\n", eoff[] = "Echo is OFF\n";
336 if ((command[0] == '.') && (command[1] == 0)) {
337 WCMD_output (newline);
342 count = strlen(command);
344 if (echo_mode) WCMD_output (eon);
345 else WCMD_output (eoff);
348 if (lstrcmpi(command, "ON") == 0) {
352 if (lstrcmpi(command, "OFF") == 0) {
356 WCMD_output_asis (command);
357 WCMD_output (newline);
361 /**************************************************************************
364 * Batch file loop processing.
365 * FIXME: We don't exhaustively check syntax. Any command which works in MessDOS
366 * will probably work here, but the reverse is not necessarily the case...
369 void WCMD_for (char *p) {
374 char set[MAX_PATH], param[MAX_PATH];
377 if (lstrcmpi (WCMD_parameter (p, 1, NULL), "in")
378 || lstrcmpi (WCMD_parameter (p, 3, NULL), "do")
379 || (param1[0] != '%')) {
380 WCMD_output ("Syntax error\n");
383 lstrcpyn (set, WCMD_parameter (p, 2, NULL), sizeof(set));
384 WCMD_parameter (p, 4, &cmd);
385 lstrcpy (param, param1);
388 * If the parameter within the set has a wildcard then search for matching files
389 * otherwise do a literal substitution.
393 while (*(item = WCMD_parameter (set, i, NULL))) {
394 if (strpbrk (item, "*?")) {
395 hff = FindFirstFile (item, &fd);
396 if (hff == INVALID_HANDLE_VALUE) {
400 WCMD_execute (cmd, param, fd.cFileName);
401 } while (FindNextFile(hff, &fd) != 0);
405 WCMD_execute (cmd, param, item);
411 /*****************************************************************************
414 * Execute a command after substituting variable text for the supplied parameter
417 void WCMD_execute (char *orig_cmd, char *param, char *subst) {
419 char *new_cmd, *p, *s, *dup;
422 size = lstrlen (orig_cmd);
423 new_cmd = (char *) LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, size);
424 dup = s = strdup (orig_cmd);
426 while ((p = strstr (s, param))) {
428 size += lstrlen (subst);
429 new_cmd = (char *) LocalReAlloc ((HANDLE)new_cmd, size, 0);
431 strcat (new_cmd, subst);
432 s = p + lstrlen (param);
435 WCMD_process_command (new_cmd);
437 LocalFree ((HANDLE)new_cmd);
441 /**************************************************************************
444 * Simple on-line help. Help text is stored in the resource file.
447 void WCMD_give_help (char *command) {
452 command = WCMD_strtrim_leading_spaces(command);
453 if (lstrlen(command) == 0) {
454 LoadString (hinst, 1000, buffer, sizeof(buffer));
455 WCMD_output_asis (buffer);
458 for (i=0; i<=WCMD_EXIT; i++) {
459 if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
460 param1, -1, inbuilt[i], -1) == 2) {
461 LoadString (hinst, i, buffer, sizeof(buffer));
462 WCMD_output_asis (buffer);
466 WCMD_output ("No help available for %s\n", param1);
471 /****************************************************************************
474 * Batch file jump instruction. Not the most efficient algorithm ;-)
475 * Prints error message if the specified label cannot be found - the file pointer is
476 * then at EOF, effectively stopping the batch file.
477 * FIXME: DOS is supposed to allow labels with spaces - we don't.
480 void WCMD_goto (void) {
482 char string[MAX_PATH];
484 if (param1[0] == 0x00) {
485 WCMD_output ("Argument missing\n");
488 if (context != NULL) {
489 char *paramStart = param1;
491 /* Handle special :EOF label */
492 if (lstrcmpi (":eof", param1) == 0) {
493 context -> skip_rest = TRUE;
497 /* Support goto :label as well as goto label */
498 if (*paramStart == ':') paramStart++;
500 SetFilePointer (context -> h, 0, NULL, FILE_BEGIN);
501 while (WCMD_fgets (string, sizeof(string), context -> h)) {
502 if ((string[0] == ':') && (lstrcmpi (&string[1], paramStart) == 0)) return;
504 WCMD_output ("Target to GOTO not found\n");
509 /*****************************************************************************
512 * Push a directory onto the stack
515 void WCMD_pushd (void) {
516 struct env_stack *curdir;
520 curdir = LocalAlloc (LMEM_FIXED, sizeof (struct env_stack));
521 thisdir = LocalAlloc (LMEM_FIXED, 1024 * sizeof(WCHAR));
522 if( !curdir || !thisdir ) {
525 WCMD_output ("out of memory\n");
529 GetCurrentDirectoryW (1024, thisdir);
530 status = SetCurrentDirectoryA (param1);
537 curdir -> next = pushd_directories;
538 curdir -> strings = thisdir;
539 pushd_directories = curdir;
544 /*****************************************************************************
547 * Pop a directory from the stack
550 void WCMD_popd (void) {
551 struct env_stack *temp = pushd_directories;
553 if (!pushd_directories)
556 /* pop the old environment from the stack, and make it the current dir */
557 pushd_directories = temp->next;
558 SetCurrentDirectoryW(temp->strings);
559 LocalFree (temp->strings);
563 /****************************************************************************
566 * Batch file conditional.
567 * FIXME: Much more syntax checking needed!
570 void WCMD_if (char *p) {
572 int negate = 0, test = 0;
573 char condition[MAX_PATH], *command, *s;
575 if (!lstrcmpi (param1, "not")) {
577 lstrcpy (condition, param2);
580 lstrcpy (condition, param1);
582 if (!lstrcmpi (condition, "errorlevel")) {
583 if (errorlevel >= atoi(WCMD_parameter (p, 1+negate, NULL))) test = 1;
584 WCMD_parameter (p, 2+negate, &command);
586 else if (!lstrcmpi (condition, "exist")) {
587 if (GetFileAttributesA(WCMD_parameter (p, 1+negate, NULL)) != INVALID_FILE_ATTRIBUTES) {
590 WCMD_parameter (p, 2+negate, &command);
592 else if (!lstrcmpi (condition, "defined")) {
593 if (GetEnvironmentVariableA(WCMD_parameter (p, 1+negate, NULL), NULL, 0) > 0) {
596 WCMD_parameter (p, 2+negate, &command);
598 else if ((s = strstr (p, "=="))) {
600 if (!lstrcmpi (condition, WCMD_parameter (s, 0, NULL))) test = 1;
601 WCMD_parameter (s, 1, &command);
604 WCMD_output ("Syntax error\n");
607 if (test != negate) {
608 command = strdup (command);
609 WCMD_process_command (command);
614 /****************************************************************************
617 * Move a file, directory tree or wildcarded set of files.
618 * FIXME: Needs input and output files to be fully specified.
621 void WCMD_move (void) {
624 char outpath[MAX_PATH], inpath[MAX_PATH], *infile;
628 if (param1[0] == 0x00) {
629 WCMD_output ("Argument missing\n");
633 if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
634 WCMD_output ("Wildcards not yet supported\n");
638 /* If no destination supplied, assume current directory */
639 if (param2[0] == 0x00) {
643 /* If 2nd parm is directory, then use original filename */
644 GetFullPathName (param2, sizeof(outpath), outpath, NULL);
645 if (outpath[strlen(outpath) - 1] == '\\')
646 outpath[strlen(outpath) - 1] = '\0';
647 hff = FindFirstFile (outpath, &fd);
648 if (hff != INVALID_HANDLE_VALUE) {
649 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
650 GetFullPathName (param1, sizeof(inpath), inpath, &infile);
651 strcat (outpath, "\\");
652 strcat (outpath, infile);
657 status = MoveFile (param1, outpath);
658 if (!status) WCMD_print_error ();
661 /****************************************************************************
664 * Wait for keyboard input.
667 void WCMD_pause (void) {
672 WCMD_output (anykey);
673 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
676 /****************************************************************************
679 * Delete a directory.
682 void WCMD_remove_dir (void) {
684 if (param1[0] == 0x00) {
685 WCMD_output ("Argument missing\n");
689 /* If subdirectory search not supplied, just try to remove
690 and report error if it fails (eg if it contains a file) */
691 if (strstr (quals, "/S") == NULL) {
692 if (!RemoveDirectory (param1)) WCMD_print_error ();
694 /* Otherwise use ShFileOp to recursively remove a directory */
697 SHFILEOPSTRUCT lpDir;
700 if (strstr (quals, "/Q") == NULL) {
702 char question[MAXSTRING];
704 /* Ask for confirmation */
705 sprintf(question, "%s, ", param1);
706 ok = WCMD_ask_confirm(question, TRUE);
708 /* Abort if answer is 'N' */
715 lpDir.pFrom = param1;
716 lpDir.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI;
717 lpDir.wFunc = FO_DELETE;
718 if (SHFileOperationA(&lpDir)) WCMD_print_error ();
722 /****************************************************************************
726 * FIXME: Needs input and output files to be fully specified.
729 void WCMD_rename (void) {
733 if (param1[0] == 0x00 || param2[0] == 0x00) {
734 WCMD_output ("Argument missing\n");
737 if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
738 WCMD_output ("Wildcards not yet supported\n");
741 status = MoveFile (param1, param2);
742 if (!status) WCMD_print_error ();
745 /*****************************************************************************
748 * Make a copy of the environment.
750 static WCHAR *WCMD_dupenv( const WCHAR *env )
760 len += (lstrlenW(&env[len]) + 1);
762 env_copy = LocalAlloc (LMEM_FIXED, (len+1) * sizeof (WCHAR) );
765 WCMD_output ("out of memory\n");
768 memcpy (env_copy, env, len*sizeof (WCHAR));
774 /*****************************************************************************
777 * setlocal pushes the environment onto a stack
778 * Save the environment as unicode so we don't screw anything up.
780 void WCMD_setlocal (const char *s) {
782 struct env_stack *env_copy;
784 /* DISABLEEXTENSIONS ignored */
786 env_copy = LocalAlloc (LMEM_FIXED, sizeof (struct env_stack));
789 WCMD_output ("out of memory\n");
793 env = GetEnvironmentStringsW ();
795 env_copy->strings = WCMD_dupenv (env);
796 if (env_copy->strings)
798 env_copy->next = saved_environment;
799 saved_environment = env_copy;
802 LocalFree (env_copy);
804 FreeEnvironmentStringsW (env);
807 /*****************************************************************************
810 static inline WCHAR *WCMD_strchrW(WCHAR *str, WCHAR ch)
821 /*****************************************************************************
824 * endlocal pops the environment off a stack
826 void WCMD_endlocal (void) {
827 WCHAR *env, *old, *p;
828 struct env_stack *temp;
831 if (!saved_environment)
834 /* pop the old environment from the stack */
835 temp = saved_environment;
836 saved_environment = temp->next;
838 /* delete the current environment, totally */
839 env = GetEnvironmentStringsW ();
840 old = WCMD_dupenv (GetEnvironmentStringsW ());
843 n = lstrlenW(&old[len]) + 1;
844 p = WCMD_strchrW(&old[len], '=');
848 SetEnvironmentVariableW (&old[len], NULL);
853 FreeEnvironmentStringsW (env);
855 /* restore old environment */
859 n = lstrlenW(&env[len]) + 1;
860 p = WCMD_strchrW(&env[len], '=');
864 SetEnvironmentVariableW (&env[len], p);
872 /*****************************************************************************
873 * WCMD_setshow_attrib
875 * Display and optionally sets DOS attributes on a file or directory
877 * FIXME: Wine currently uses the Unix stat() function to get file attributes.
878 * As a result only the Readonly flag is correctly reported, the Archive bit
879 * is always set and the rest are not implemented. We do the Right Thing anyway.
881 * FIXME: No SET functionality.
885 void WCMD_setshow_attrib (void) {
890 char flags[9] = {" "};
892 if (param1[0] == '-') {
897 if (lstrlen(param1) == 0) {
898 GetCurrentDirectory (sizeof(param1), param1);
899 strcat (param1, "\\*");
902 hff = FindFirstFile (param1, &fd);
903 if (hff == INVALID_HANDLE_VALUE) {
904 WCMD_output ("%s: File Not Found\n",param1);
908 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
909 if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
912 if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
915 if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
918 if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
921 if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) {
924 if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) {
927 WCMD_output ("%s %s\n", flags, fd.cFileName);
928 for (count=0; count < 8; count++) flags[count] = ' ';
930 } while (FindNextFile(hff, &fd) != 0);
935 /*****************************************************************************
936 * WCMD_setshow_default
938 * Set/Show the current default directory
941 void WCMD_setshow_default (void) {
946 if (strlen(param1) == 0) {
947 GetCurrentDirectory (sizeof(string), string);
948 strcat (string, "\n");
949 WCMD_output (string);
952 status = SetCurrentDirectory (param1);
961 /****************************************************************************
964 * Set/Show the system date
965 * FIXME: Can't change date yet
968 void WCMD_setshow_date (void) {
970 char curdate[64], buffer[64];
973 if (lstrlen(param1) == 0) {
974 if (GetDateFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL,
975 curdate, sizeof(curdate))) {
976 WCMD_output ("Current Date is %s\nEnter new date: ", curdate);
977 ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
982 else WCMD_print_error ();
989 /****************************************************************************
992 static int WCMD_compare( const void *a, const void *b )
995 const char * const *str_a = a, * const *str_b = b;
996 r = CompareString( LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
997 *str_a, -1, *str_b, -1 );
998 if( r == CSTR_LESS_THAN ) return -1;
999 if( r == CSTR_GREATER_THAN ) return 1;
1003 /****************************************************************************
1004 * WCMD_setshow_sortenv
1006 * sort variables into order for display
1007 * Optionally only display those who start with a stub
1008 * returns the count displayed
1010 static int WCMD_setshow_sortenv(const char *s, const char *stub)
1012 UINT count=0, len=0, i, displayedcount=0, stublen=0;
1015 if (stub) stublen = strlen(stub);
1017 /* count the number of strings, and the total length */
1019 len += (lstrlen(&s[len]) + 1);
1023 /* add the strings to an array */
1024 str = LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, count * sizeof (char*) );
1028 for( i=1; i<count; i++ )
1029 str[i] = str[i-1] + lstrlen(str[i-1]) + 1;
1031 /* sort the array */
1032 qsort( str, count, sizeof (char*), WCMD_compare );
1035 for( i=0; i<count; i++ ) {
1036 if (!stub || CompareString (LOCALE_USER_DEFAULT,
1037 NORM_IGNORECASE | SORT_STRINGSORT,
1038 str[i], stublen, stub, -1) == 2) {
1039 WCMD_output_asis(str[i]);
1040 WCMD_output_asis("\n");
1046 return displayedcount;
1049 /****************************************************************************
1052 * Set/Show the environment variables
1055 void WCMD_setshow_env (char *s) {
1061 if (strlen(param1) == 0) {
1062 env = GetEnvironmentStrings ();
1063 WCMD_setshow_sortenv( env, NULL );
1066 p = strchr (s, '=');
1068 env = GetEnvironmentStrings ();
1069 if (WCMD_setshow_sortenv( env, s ) == 0) {
1070 WCMD_output ("Environment variable %s not defined\n", s);
1076 if (strlen(p) == 0) p = NULL;
1077 status = SetEnvironmentVariable (s, p);
1078 if ((!status) & (GetLastError() != ERROR_ENVVAR_NOT_FOUND)) WCMD_print_error();
1082 /****************************************************************************
1085 * Set/Show the path environment variable
1088 void WCMD_setshow_path (char *command) {
1093 if (strlen(param1) == 0) {
1094 status = GetEnvironmentVariable ("PATH", string, sizeof(string));
1096 WCMD_output_asis ( "PATH=");
1097 WCMD_output_asis ( string);
1098 WCMD_output_asis ( "\n");
1101 WCMD_output ("PATH not found\n");
1105 if (*command == '=') command++; /* Skip leading '=' */
1106 status = SetEnvironmentVariable ("PATH", command);
1107 if (!status) WCMD_print_error();
1111 /****************************************************************************
1112 * WCMD_setshow_prompt
1114 * Set or show the command prompt.
1117 void WCMD_setshow_prompt (void) {
1121 if (strlen(param1) == 0) {
1122 SetEnvironmentVariable ("PROMPT", NULL);
1126 while ((*s == '=') || (*s == ' ')) s++;
1127 if (strlen(s) == 0) {
1128 SetEnvironmentVariable ("PROMPT", NULL);
1130 else SetEnvironmentVariable ("PROMPT", s);
1134 /****************************************************************************
1137 * Set/Show the system time
1138 * FIXME: Can't change time yet
1141 void WCMD_setshow_time (void) {
1143 char curtime[64], buffer[64];
1147 if (strlen(param1) == 0) {
1149 if (GetTimeFormat (LOCALE_USER_DEFAULT, 0, &st, NULL,
1150 curtime, sizeof(curtime))) {
1151 WCMD_output ("Current Time is %s\nEnter new time: ", curtime);
1152 ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
1157 else WCMD_print_error ();
1164 /****************************************************************************
1167 * Shift batch parameters.
1170 void WCMD_shift (void) {
1172 if (context != NULL) context -> shift_count++;
1176 /****************************************************************************
1179 * Set the console title
1181 void WCMD_title (char *command) {
1182 SetConsoleTitle(command);
1185 /****************************************************************************
1188 * Copy a file to standard output.
1191 void WCMD_type (void) {
1197 if (param1[0] == 0x00) {
1198 WCMD_output ("Argument missing\n");
1201 h = CreateFile (param1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
1202 FILE_ATTRIBUTE_NORMAL, NULL);
1203 if (h == INVALID_HANDLE_VALUE) {
1204 WCMD_print_error ();
1207 while (ReadFile (h, buffer, sizeof(buffer), &count, NULL)) {
1208 if (count == 0) break; /* ReadFile reports success on EOF! */
1210 WCMD_output_asis (buffer);
1215 /****************************************************************************
1218 * Display verify flag.
1219 * FIXME: We don't actually do anything with the verify flag other than toggle
1223 void WCMD_verify (char *command) {
1225 static const char von[] = "Verify is ON\n", voff[] = "Verify is OFF\n";
1228 count = strlen(command);
1230 if (verify_mode) WCMD_output (von);
1231 else WCMD_output (voff);
1234 if (lstrcmpi(command, "ON") == 0) {
1238 else if (lstrcmpi(command, "OFF") == 0) {
1242 else WCMD_output ("Verify must be ON or OFF\n");
1245 /****************************************************************************
1248 * Display version info.
1251 void WCMD_version (void) {
1253 WCMD_output (version_string);
1257 /****************************************************************************
1260 * Display volume info and/or set volume label. Returns 0 if error.
1263 int WCMD_volume (int mode, char *path) {
1265 DWORD count, serial;
1266 char string[MAX_PATH], label[MAX_PATH], curdir[MAX_PATH];
1269 if (lstrlen(path) == 0) {
1270 status = GetCurrentDirectory (sizeof(curdir), curdir);
1272 WCMD_print_error ();
1275 status = GetVolumeInformation (NULL, label, sizeof(label), &serial, NULL,
1279 if ((path[1] != ':') || (lstrlen(path) != 2)) {
1280 WCMD_output_asis("Syntax Error\n\n");
1283 wsprintf (curdir, "%s\\", path);
1284 status = GetVolumeInformation (curdir, label, sizeof(label), &serial, NULL,
1288 WCMD_print_error ();
1291 WCMD_output ("Volume in drive %c is %s\nVolume Serial Number is %04x-%04x\n\n",
1292 curdir[0], label, HIWORD(serial), LOWORD(serial));
1294 WCMD_output ("Volume label (11 characters, ENTER for none)?");
1295 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
1297 string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
1298 if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
1300 if (lstrlen(path) != 0) {
1301 if (!SetVolumeLabel (curdir, string)) WCMD_print_error ();
1304 if (!SetVolumeLabel (NULL, string)) WCMD_print_error ();
1310 /**************************************************************************
1313 * Exit either the process, or just this batch program
1317 void WCMD_exit (void) {
1319 int rc = atoi(param1); /* Note: atoi of empty parameter is 0 */
1321 if (context && lstrcmpi(quals, "/B") == 0) {
1323 context -> skip_rest = TRUE;
1329 /**************************************************************************
1332 * Issue a message and ask 'Are you sure (Y/N)', waiting on a valid
1335 * Returns True if Y answer is selected
1338 BOOL WCMD_ask_confirm (char *message, BOOL showSureText) {
1340 char msgbuffer[MAXSTRING];
1341 char Ybuffer[MAXSTRING];
1342 char Nbuffer[MAXSTRING];
1343 char answer[MAX_PATH] = "";
1346 /* Load the translated 'Are you sure', plus valid answers */
1347 LoadString (hinst, WCMD_CONFIRM, msgbuffer, sizeof(msgbuffer));
1348 LoadString (hinst, WCMD_YES, Ybuffer, sizeof(Ybuffer));
1349 LoadString (hinst, WCMD_NO, Nbuffer, sizeof(Nbuffer));
1351 /* Loop waiting on a Y or N */
1352 while (answer[0] != Ybuffer[0] && answer[0] != Nbuffer[0]) {
1353 WCMD_output_asis (message);
1355 WCMD_output_asis (msgbuffer);
1357 WCMD_output_asis (" (");
1358 WCMD_output_asis (Ybuffer);
1359 WCMD_output_asis ("/");
1360 WCMD_output_asis (Nbuffer);
1361 WCMD_output_asis (")?");
1362 ReadFile (GetStdHandle(STD_INPUT_HANDLE), answer, sizeof(answer),
1364 answer[0] = toupper(answer[0]);
1367 /* Return the answer */
1368 return (answer[0] == Ybuffer[0]);