ole: ITypeInfo::Invoke rewrite.
[wine/hacks.git] / programs / wcmd / builtins.c
bloba47bc0a417e5a93fb186e97b8d34878e84d78ee9
1 /*
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
22 * NOTES:
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.
29 * FIXME:
30 * - No support for pipes, shell parameters
31 * - Lots of functionality missing from builtins
32 * - Messages etc need international support
35 #include "wcmd.h"
37 void WCMD_execute (char *orig_command, char *parameter, char *substitution);
39 struct env_stack
41 struct env_stack *next;
42 WCHAR *strings;
45 struct env_stack *saved_environment;
47 extern HINSTANCE hinst;
48 extern char *inbuilt[];
49 extern int echo_mode, verify_mode;
50 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
51 extern BATCH_CONTEXT *context;
52 extern DWORD errorlevel;
56 /****************************************************************************
57 * WCMD_clear_screen
59 * Clear the terminal screen.
62 void WCMD_clear_screen (void) {
64 /* Emulate by filling the screen from the top left to bottom right with
65 spaces, then moving the cursor to the top left afterwards */
66 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
67 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
69 if (GetConsoleScreenBufferInfo(hStdOut, &consoleInfo))
71 COORD topLeft;
72 DWORD screenSize;
74 screenSize = consoleInfo.dwSize.X * (consoleInfo.dwSize.Y + 1);
76 topLeft.X = 0;
77 topLeft.Y = 0;
78 FillConsoleOutputCharacter(hStdOut, ' ', screenSize, topLeft, &screenSize);
79 SetConsoleCursorPosition(hStdOut, topLeft);
83 /****************************************************************************
84 * WCMD_change_tty
86 * Change the default i/o device (ie redirect STDin/STDout).
89 void WCMD_change_tty (void) {
91 WCMD_output (nyi);
95 /****************************************************************************
96 * WCMD_copy
98 * Copy a file or wildcarded set.
99 * FIXME: No wildcard support
102 void WCMD_copy (void) {
104 DWORD count;
105 WIN32_FIND_DATA fd;
106 HANDLE hff;
107 BOOL force, status;
108 static const char overwrite[] = "Overwrite file (Y/N)?";
109 char string[8], outpath[MAX_PATH], inpath[MAX_PATH], *infile;
111 if ((strchr(param1,'*') != NULL) && (strchr(param1,'%') != NULL)) {
112 WCMD_output ("Wildcards not yet supported\n");
113 return;
116 /* If no destination supplied, assume current directory */
117 if (param2[0] == 0x00) {
118 strcpy(param2, ".");
121 GetFullPathName (param2, sizeof(outpath), outpath, NULL);
122 hff = FindFirstFile (outpath, &fd);
123 if (hff != INVALID_HANDLE_VALUE) {
124 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
125 GetFullPathName (param1, sizeof(inpath), inpath, &infile);
126 strcat (outpath, "\\");
127 strcat (outpath, infile);
129 FindClose (hff);
132 force = (strstr (quals, "/Y") != NULL);
133 if (!force) {
134 hff = FindFirstFile (outpath, &fd);
135 if (hff != INVALID_HANDLE_VALUE) {
136 FindClose (hff);
137 WCMD_output (overwrite);
138 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
139 if (toupper(string[0]) == 'Y') force = TRUE;
141 else force = TRUE;
143 if (force) {
144 status = CopyFile (param1, outpath, FALSE);
145 if (!status) WCMD_print_error ();
149 /****************************************************************************
150 * WCMD_create_dir
152 * Create a directory.
154 * this works recursivly. so mkdir dir1\dir2\dir3 will create dir1 and dir2 if
155 * they do not already exist.
158 BOOL create_full_path(CHAR* path)
160 int len;
161 CHAR *new_path;
162 BOOL ret = TRUE;
164 new_path = HeapAlloc(GetProcessHeap(),0,strlen(path)+1);
165 strcpy(new_path,path);
167 while ((len = strlen(new_path)) && new_path[len - 1] == '\\')
168 new_path[len - 1] = 0;
170 while (!CreateDirectory(new_path,NULL))
172 CHAR *slash;
173 DWORD last_error = GetLastError();
174 if (last_error == ERROR_ALREADY_EXISTS)
175 break;
177 if (last_error != ERROR_PATH_NOT_FOUND)
179 ret = FALSE;
180 break;
183 if (!(slash = strrchr(new_path,'\\')) && ! (slash = strrchr(new_path,'/')))
185 ret = FALSE;
186 break;
189 len = slash - new_path;
190 new_path[len] = 0;
191 if (!create_full_path(new_path))
193 ret = FALSE;
194 break;
196 new_path[len] = '\\';
198 HeapFree(GetProcessHeap(),0,new_path);
199 return ret;
202 void WCMD_create_dir (void) {
204 if (!create_full_path(param1)) WCMD_print_error ();
207 /****************************************************************************
208 * WCMD_delete
210 * Delete a file or wildcarded set.
214 void WCMD_delete (int recurse) {
216 WIN32_FIND_DATA fd;
217 HANDLE hff;
218 char fpath[MAX_PATH];
219 char *p;
221 hff = FindFirstFile (param1, &fd);
222 if (hff == INVALID_HANDLE_VALUE) {
223 WCMD_output ("%s :File Not Found\n",param1);
224 return;
226 if ((strchr(param1,'*') == NULL) && (strchr(param1,'?') == NULL)
227 && (!recurse) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
228 strcat (param1, "\\*");
229 FindClose(hff);
230 WCMD_delete (1);
231 return;
233 if ((strchr(param1,'*') != NULL) || (strchr(param1,'?') != NULL)) {
234 strcpy (fpath, param1);
235 do {
236 p = strrchr (fpath, '\\');
237 if (p != NULL) {
238 *++p = '\0';
239 strcat (fpath, fd.cFileName);
241 else strcpy (fpath, fd.cFileName);
242 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
243 if (!DeleteFile (fpath)) WCMD_print_error ();
245 } while (FindNextFile(hff, &fd) != 0);
246 FindClose (hff);
248 else {
249 if (!DeleteFile (param1)) WCMD_print_error ();
250 FindClose (hff);
254 /****************************************************************************
255 * WCMD_echo
257 * Echo input to the screen (or not). We don't try to emulate the bugs
258 * in DOS (try typing "ECHO ON AGAIN" for an example).
261 void WCMD_echo (const char *command) {
263 static const char eon[] = "Echo is ON\n", eoff[] = "Echo is OFF\n";
264 int count;
266 if ((command[0] == '.') && (command[1] == 0)) {
267 WCMD_output (newline);
268 return;
270 if (command[0]==' ')
271 command++;
272 count = strlen(command);
273 if (count == 0) {
274 if (echo_mode) WCMD_output (eon);
275 else WCMD_output (eoff);
276 return;
278 if (lstrcmpi(command, "ON") == 0) {
279 echo_mode = 1;
280 return;
282 if (lstrcmpi(command, "OFF") == 0) {
283 echo_mode = 0;
284 return;
286 WCMD_output_asis (command);
287 WCMD_output (newline);
291 /**************************************************************************
292 * WCMD_for
294 * Batch file loop processing.
295 * FIXME: We don't exhaustively check syntax. Any command which works in MessDOS
296 * will probably work here, but the reverse is not necessarily the case...
299 void WCMD_for (char *p) {
301 WIN32_FIND_DATA fd;
302 HANDLE hff;
303 char *cmd, *item;
304 char set[MAX_PATH], param[MAX_PATH];
305 int i;
307 if (lstrcmpi (WCMD_parameter (p, 1, NULL), "in")
308 || lstrcmpi (WCMD_parameter (p, 3, NULL), "do")
309 || (param1[0] != '%')) {
310 WCMD_output ("Syntax error\n");
311 return;
313 lstrcpyn (set, WCMD_parameter (p, 2, NULL), sizeof(set));
314 WCMD_parameter (p, 4, &cmd);
315 lstrcpy (param, param1);
318 * If the parameter within the set has a wildcard then search for matching files
319 * otherwise do a literal substitution.
322 i = 0;
323 while (*(item = WCMD_parameter (set, i, NULL))) {
324 if (strpbrk (item, "*?")) {
325 hff = FindFirstFile (item, &fd);
326 if (hff == INVALID_HANDLE_VALUE) {
327 return;
329 do {
330 WCMD_execute (cmd, param, fd.cFileName);
331 } while (FindNextFile(hff, &fd) != 0);
332 FindClose (hff);
334 else {
335 WCMD_execute (cmd, param, item);
337 i++;
341 /*****************************************************************************
342 * WCMD_Execute
344 * Execute a command after substituting variable text for the supplied parameter
347 void WCMD_execute (char *orig_cmd, char *param, char *subst) {
349 char *new_cmd, *p, *s, *dup;
350 int size;
352 size = lstrlen (orig_cmd);
353 new_cmd = (char *) LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, size);
354 dup = s = strdup (orig_cmd);
356 while ((p = strstr (s, param))) {
357 *p = '\0';
358 size += lstrlen (subst);
359 new_cmd = (char *) LocalReAlloc ((HANDLE)new_cmd, size, 0);
360 strcat (new_cmd, s);
361 strcat (new_cmd, subst);
362 s = p + lstrlen (param);
364 strcat (new_cmd, s);
365 WCMD_process_command (new_cmd);
366 free (dup);
367 LocalFree ((HANDLE)new_cmd);
371 /**************************************************************************
372 * WCMD_give_help
374 * Simple on-line help. Help text is stored in the resource file.
377 void WCMD_give_help (char *command) {
379 int i;
380 char buffer[2048];
382 command = WCMD_strtrim_leading_spaces(command);
383 if (lstrlen(command) == 0) {
384 LoadString (hinst, 1000, buffer, sizeof(buffer));
385 WCMD_output_asis (buffer);
387 else {
388 for (i=0; i<=WCMD_EXIT; i++) {
389 if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
390 param1, -1, inbuilt[i], -1) == 2) {
391 LoadString (hinst, i, buffer, sizeof(buffer));
392 WCMD_output_asis (buffer);
393 return;
396 WCMD_output ("No help available for %s\n", param1);
398 return;
401 /****************************************************************************
402 * WCMD_go_to
404 * Batch file jump instruction. Not the most efficient algorithm ;-)
405 * Prints error message if the specified label cannot be found - the file pointer is
406 * then at EOF, effectively stopping the batch file.
407 * FIXME: DOS is supposed to allow labels with spaces - we don't.
410 void WCMD_goto (void) {
412 char string[MAX_PATH];
414 if (context != NULL) {
415 SetFilePointer (context -> h, 0, NULL, FILE_BEGIN);
416 while (WCMD_fgets (string, sizeof(string), context -> h)) {
417 if ((string[0] == ':') && (strcmp (&string[1], param1) == 0)) return;
419 WCMD_output ("Target to GOTO not found\n");
421 return;
425 /****************************************************************************
426 * WCMD_if
428 * Batch file conditional.
429 * FIXME: Much more syntax checking needed!
432 void WCMD_if (char *p) {
434 int negate = 0, test = 0;
435 char condition[MAX_PATH], *command, *s;
437 if (!lstrcmpi (param1, "not")) {
438 negate = 1;
439 lstrcpy (condition, param2);
441 else {
442 lstrcpy (condition, param1);
444 if (!lstrcmpi (condition, "errorlevel")) {
445 if (errorlevel >= atoi(WCMD_parameter (p, 1+negate, NULL))) test = 1;
446 return;
447 WCMD_parameter (p, 2+negate, &command);
449 else if (!lstrcmpi (condition, "exist")) {
450 if (GetFileAttributesA(WCMD_parameter (p, 1+negate, NULL)) != INVALID_FILE_ATTRIBUTES) {
451 test = 1;
453 WCMD_parameter (p, 2+negate, &command);
455 else if ((s = strstr (p, "=="))) {
456 s += 2;
457 if (!lstrcmpi (condition, WCMD_parameter (s, 0, NULL))) test = 1;
458 WCMD_parameter (s, 1, &command);
460 else {
461 WCMD_output ("Syntax error\n");
462 return;
464 if (test != negate) {
465 command = strdup (command);
466 WCMD_process_command (command);
467 free (command);
471 /****************************************************************************
472 * WCMD_move
474 * Move a file, directory tree or wildcarded set of files.
475 * FIXME: Needs input and output files to be fully specified.
478 void WCMD_move (void) {
480 int status;
481 char outpath[MAX_PATH], inpath[MAX_PATH], *infile;
482 WIN32_FIND_DATA fd;
483 HANDLE hff;
485 if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
486 WCMD_output ("Wildcards not yet supported\n");
487 return;
490 /* If no destination supplied, assume current directory */
491 if (param2[0] == 0x00) {
492 strcpy(param2, ".");
495 /* If 2nd parm is directory, then use original filename */
496 GetFullPathName (param2, sizeof(outpath), outpath, NULL);
497 hff = FindFirstFile (outpath, &fd);
498 if (hff != INVALID_HANDLE_VALUE) {
499 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
500 GetFullPathName (param1, sizeof(inpath), inpath, &infile);
501 strcat (outpath, "\\");
502 strcat (outpath, infile);
504 FindClose (hff);
507 status = MoveFile (param1, outpath);
508 if (!status) WCMD_print_error ();
511 /****************************************************************************
512 * WCMD_pause
514 * Wait for keyboard input.
517 void WCMD_pause (void) {
519 DWORD count;
520 char string[32];
522 WCMD_output (anykey);
523 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
526 /****************************************************************************
527 * WCMD_remove_dir
529 * Delete a directory.
532 void WCMD_remove_dir (void) {
534 if (!RemoveDirectory (param1)) WCMD_print_error ();
537 /****************************************************************************
538 * WCMD_rename
540 * Rename a file.
541 * FIXME: Needs input and output files to be fully specified.
544 void WCMD_rename (void) {
546 int status;
548 if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
549 WCMD_output ("Wildcards not yet supported\n");
550 return;
552 status = MoveFile (param1, param2);
553 if (!status) WCMD_print_error ();
556 /*****************************************************************************
557 * WCMD_dupenv
559 * Make a copy of the environment.
561 static WCHAR *WCMD_dupenv( const WCHAR *env )
563 WCHAR *env_copy;
564 int len;
566 if( !env )
567 return NULL;
569 len = 0;
570 while ( env[len] )
571 len += (lstrlenW(&env[len]) + 1);
573 env_copy = LocalAlloc (LMEM_FIXED, (len+1) * sizeof (WCHAR) );
574 if (!env_copy)
576 WCMD_output ("out of memory\n");
577 return env_copy;
579 memcpy (env_copy, env, len*sizeof (WCHAR));
580 env_copy[len] = 0;
582 return env_copy;
585 /*****************************************************************************
586 * WCMD_setlocal
588 * setlocal pushes the environment onto a stack
589 * Save the environment as unicode so we don't screw anything up.
591 void WCMD_setlocal (const char *s) {
592 WCHAR *env;
593 struct env_stack *env_copy;
595 /* DISABLEEXTENSIONS ignored */
597 env_copy = LocalAlloc (LMEM_FIXED, sizeof (struct env_stack));
598 if( !env_copy )
600 WCMD_output ("out of memory\n");
601 return;
604 env = GetEnvironmentStringsW ();
606 env_copy->strings = WCMD_dupenv (env);
607 if (env_copy->strings)
609 env_copy->next = saved_environment;
610 saved_environment = env_copy;
612 else
613 LocalFree (env_copy);
615 FreeEnvironmentStringsW (env);
618 /*****************************************************************************
619 * WCMD_strchrW
621 static inline WCHAR *WCMD_strchrW(WCHAR *str, WCHAR ch)
623 while(*str)
625 if(*str == ch)
626 return str;
627 str++;
629 return NULL;
632 /*****************************************************************************
633 * WCMD_endlocal
635 * endlocal pops the environment off a stack
637 void WCMD_endlocal (void) {
638 WCHAR *env, *old, *p;
639 struct env_stack *temp;
640 int len, n;
642 if (!saved_environment)
643 return;
645 /* pop the old environment from the stack */
646 temp = saved_environment;
647 saved_environment = temp->next;
649 /* delete the current environment, totally */
650 env = GetEnvironmentStringsW ();
651 old = WCMD_dupenv (GetEnvironmentStringsW ());
652 len = 0;
653 while (old[len]) {
654 n = lstrlenW(&old[len]) + 1;
655 p = WCMD_strchrW(&old[len], '=');
656 if (p)
658 *p++ = 0;
659 SetEnvironmentVariableW (&old[len], NULL);
661 len += n;
663 LocalFree (old);
664 FreeEnvironmentStringsW (env);
666 /* restore old environment */
667 env = temp->strings;
668 len = 0;
669 while (env[len]) {
670 n = lstrlenW(&env[len]) + 1;
671 p = WCMD_strchrW(&env[len], '=');
672 if (p)
674 *p++ = 0;
675 SetEnvironmentVariableW (&env[len], p);
677 len += n;
679 LocalFree (env);
680 LocalFree (temp);
683 /*****************************************************************************
684 * WCMD_setshow_attrib
686 * Display and optionally sets DOS attributes on a file or directory
688 * FIXME: Wine currently uses the Unix stat() function to get file attributes.
689 * As a result only the Readonly flag is correctly reported, the Archive bit
690 * is always set and the rest are not implemented. We do the Right Thing anyway.
692 * FIXME: No SET functionality.
696 void WCMD_setshow_attrib (void) {
698 DWORD count;
699 HANDLE hff;
700 WIN32_FIND_DATA fd;
701 char flags[9] = {" "};
703 if (param1[0] == '-') {
704 WCMD_output (nyi);
705 return;
708 if (lstrlen(param1) == 0) {
709 GetCurrentDirectory (sizeof(param1), param1);
710 strcat (param1, "\\*");
713 hff = FindFirstFile (param1, &fd);
714 if (hff == INVALID_HANDLE_VALUE) {
715 WCMD_output ("%s: File Not Found\n",param1);
717 else {
718 do {
719 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
720 if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
721 flags[0] = 'H';
723 if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
724 flags[1] = 'S';
726 if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
727 flags[2] = 'A';
729 if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
730 flags[3] = 'R';
732 if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) {
733 flags[4] = 'T';
735 if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) {
736 flags[5] = 'C';
738 WCMD_output ("%s %s\n", flags, fd.cFileName);
739 for (count=0; count < 8; count++) flags[count] = ' ';
741 } while (FindNextFile(hff, &fd) != 0);
743 FindClose (hff);
746 /*****************************************************************************
747 * WCMD_setshow_default
749 * Set/Show the current default directory
752 void WCMD_setshow_default (void) {
754 BOOL status;
755 char string[1024];
757 if (strlen(param1) == 0) {
758 GetCurrentDirectory (sizeof(string), string);
759 strcat (string, "\n");
760 WCMD_output (string);
762 else {
763 status = SetCurrentDirectory (param1);
764 if (!status) {
765 WCMD_print_error ();
766 return;
769 return;
772 /****************************************************************************
773 * WCMD_setshow_date
775 * Set/Show the system date
776 * FIXME: Can't change date yet
779 void WCMD_setshow_date (void) {
781 char curdate[64], buffer[64];
782 DWORD count;
784 if (lstrlen(param1) == 0) {
785 if (GetDateFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL,
786 curdate, sizeof(curdate))) {
787 WCMD_output ("Current Date is %s\nEnter new date: ", curdate);
788 ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
789 if (count > 2) {
790 WCMD_output (nyi);
793 else WCMD_print_error ();
795 else {
796 WCMD_output (nyi);
800 /****************************************************************************
801 * WCMD_compare
803 static int WCMD_compare( const void *a, const void *b )
805 int r;
806 const char * const *str_a = a, * const *str_b = b;
807 r = CompareString( LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
808 *str_a, -1, *str_b, -1 );
809 if( r == CSTR_LESS_THAN ) return -1;
810 if( r == CSTR_GREATER_THAN ) return 1;
811 return 0;
814 /****************************************************************************
815 * WCMD_setshow_sortenv
817 * sort variables into order for display
819 static void WCMD_setshow_sortenv(const char *s)
821 UINT count=0, len=0, i;
822 const char **str;
824 /* count the number of strings, and the total length */
825 while ( s[len] ) {
826 len += (lstrlen(&s[len]) + 1);
827 count++;
830 /* add the strings to an array */
831 str = LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, count * sizeof (char*) );
832 if( !str )
833 return;
834 str[0] = s;
835 for( i=1; i<count; i++ )
836 str[i] = str[i-1] + lstrlen(str[i-1]) + 1;
838 /* sort the array */
839 qsort( str, count, sizeof (char*), WCMD_compare );
841 /* print it */
842 for( i=0; i<count; i++ ) {
843 WCMD_output_asis(str[i]);
844 WCMD_output_asis("\n");
847 LocalFree( str );
850 /****************************************************************************
851 * WCMD_setshow_env
853 * Set/Show the environment variables
856 void WCMD_setshow_env (char *s) {
858 LPVOID env;
859 char *p;
860 int status;
861 char buffer[1048];
863 if (strlen(param1) == 0) {
864 env = GetEnvironmentStrings ();
865 WCMD_setshow_sortenv( env );
867 else {
868 p = strchr (s, '=');
869 if (p == NULL) {
871 /* FIXME: Emulate Win98 for now, ie "SET C" looks ONLY for an
872 environment variable C, whereas on NT it shows ALL variables
873 starting with C.
875 status = GetEnvironmentVariable(s, buffer, sizeof(buffer));
876 if (status) {
877 WCMD_output_asis( s);
878 WCMD_output_asis( "=");
879 WCMD_output_asis( buffer);
880 WCMD_output_asis( "\n");
881 } else {
882 WCMD_output ("Environment variable %s not defined\n", s);
884 return;
886 *p++ = '\0';
888 if (strlen(p) == 0) p = NULL;
889 status = SetEnvironmentVariable (s, p);
890 if ((!status) & (GetLastError() != ERROR_ENVVAR_NOT_FOUND)) WCMD_print_error();
892 /* WCMD_output (newline); @JED*/
895 /****************************************************************************
896 * WCMD_setshow_path
898 * Set/Show the path environment variable
901 void WCMD_setshow_path (char *command) {
903 char string[1024];
904 DWORD status;
906 if (strlen(param1) == 0) {
907 status = GetEnvironmentVariable ("PATH", string, sizeof(string));
908 if (status != 0) {
909 WCMD_output_asis ( "PATH=");
910 WCMD_output_asis ( string);
911 WCMD_output_asis ( "\n");
913 else {
914 WCMD_output ("PATH not found\n");
917 else {
918 status = SetEnvironmentVariable ("PATH", command);
919 if (!status) WCMD_print_error();
923 /****************************************************************************
924 * WCMD_setshow_prompt
926 * Set or show the command prompt.
929 void WCMD_setshow_prompt (void) {
931 char *s;
933 if (strlen(param1) == 0) {
934 SetEnvironmentVariable ("PROMPT", NULL);
936 else {
937 s = param1;
938 while ((*s == '=') || (*s == ' ')) s++;
939 if (strlen(s) == 0) {
940 SetEnvironmentVariable ("PROMPT", NULL);
942 else SetEnvironmentVariable ("PROMPT", s);
946 /****************************************************************************
947 * WCMD_setshow_time
949 * Set/Show the system time
950 * FIXME: Can't change time yet
953 void WCMD_setshow_time (void) {
955 char curtime[64], buffer[64];
956 DWORD count;
957 SYSTEMTIME st;
959 if (strlen(param1) == 0) {
960 GetLocalTime(&st);
961 if (GetTimeFormat (LOCALE_USER_DEFAULT, 0, &st, NULL,
962 curtime, sizeof(curtime))) {
963 WCMD_output ("Current Time is %s\nEnter new time: ", curtime);
964 ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
965 if (count > 2) {
966 WCMD_output (nyi);
969 else WCMD_print_error ();
971 else {
972 WCMD_output (nyi);
976 /****************************************************************************
977 * WCMD_shift
979 * Shift batch parameters.
982 void WCMD_shift (void) {
984 if (context != NULL) context -> shift_count++;
988 /****************************************************************************
989 * WCMD_title
991 * Set the console title
993 void WCMD_title (char *command) {
994 SetConsoleTitle(command);
997 /****************************************************************************
998 * WCMD_type
1000 * Copy a file to standard output.
1003 void WCMD_type (void) {
1005 HANDLE h;
1006 char buffer[512];
1007 DWORD count;
1009 h = CreateFile (param1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
1010 FILE_ATTRIBUTE_NORMAL, NULL);
1011 if (h == INVALID_HANDLE_VALUE) {
1012 WCMD_print_error ();
1013 return;
1015 while (ReadFile (h, buffer, sizeof(buffer), &count, NULL)) {
1016 if (count == 0) break; /* ReadFile reports success on EOF! */
1017 buffer[count] = 0;
1018 WCMD_output_asis (buffer);
1020 CloseHandle (h);
1023 /****************************************************************************
1024 * WCMD_verify
1026 * Display verify flag.
1027 * FIXME: We don't actually do anything with the verify flag other than toggle
1028 * it...
1031 void WCMD_verify (char *command) {
1033 static const char von[] = "Verify is ON\n", voff[] = "Verify is OFF\n";
1034 int count;
1036 count = strlen(command);
1037 if (count == 0) {
1038 if (verify_mode) WCMD_output (von);
1039 else WCMD_output (voff);
1040 return;
1042 if (lstrcmpi(command, "ON") == 0) {
1043 verify_mode = 1;
1044 return;
1046 else if (lstrcmpi(command, "OFF") == 0) {
1047 verify_mode = 0;
1048 return;
1050 else WCMD_output ("Verify must be ON or OFF\n");
1053 /****************************************************************************
1054 * WCMD_version
1056 * Display version info.
1059 void WCMD_version (void) {
1061 WCMD_output (version_string);
1065 /****************************************************************************
1066 * WCMD_volume
1068 * Display volume info and/or set volume label. Returns 0 if error.
1071 int WCMD_volume (int mode, char *path) {
1073 DWORD count, serial;
1074 char string[MAX_PATH], label[MAX_PATH], curdir[MAX_PATH];
1075 BOOL status;
1077 if (lstrlen(path) == 0) {
1078 status = GetCurrentDirectory (sizeof(curdir), curdir);
1079 if (!status) {
1080 WCMD_print_error ();
1081 return 0;
1083 status = GetVolumeInformation (NULL, label, sizeof(label), &serial, NULL,
1084 NULL, NULL, 0);
1086 else {
1087 if ((path[1] != ':') || (lstrlen(path) != 2)) {
1088 WCMD_output_asis("Syntax Error\n\n");
1089 return 0;
1091 wsprintf (curdir, "%s\\", path);
1092 status = GetVolumeInformation (curdir, label, sizeof(label), &serial, NULL,
1093 NULL, NULL, 0);
1095 if (!status) {
1096 WCMD_print_error ();
1097 return 0;
1099 WCMD_output ("Volume in drive %c is %s\nVolume Serial Number is %04x-%04x\n\n",
1100 curdir[0], label, HIWORD(serial), LOWORD(serial));
1101 if (mode) {
1102 WCMD_output ("Volume label (11 characters, ENTER for none)?");
1103 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
1104 if (count > 1) {
1105 string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
1106 if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
1108 if (lstrlen(path) != 0) {
1109 if (!SetVolumeLabel (curdir, string)) WCMD_print_error ();
1111 else {
1112 if (!SetVolumeLabel (NULL, string)) WCMD_print_error ();
1115 return 1;