2 * WCMD - Wine-compatible command line interface - built-in functions.
4 * Copyright (C) 1999 D A Pickles
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * On entry to each function, global variables quals, param1, param2 contain
24 * the qualifiers (uppercased and concatenated) and parameters entered, with
25 * environment-variable and batch parameter substitution already done.
30 * - No support for pipes, shell parameters
31 * - Lots of functionality missing from builtins
32 * - Messages etc need international support
37 void WCMD_execute (char *orig_command, char *parameter, char *substitution);
39 extern HINSTANCE hinst;
40 extern char *inbuilt[];
42 extern char newline[];
43 extern char version_string[];
45 extern int echo_mode, verify_mode;
46 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
47 extern BATCH_CONTEXT *context;
48 extern DWORD errorlevel;
52 /****************************************************************************
55 * Clear the terminal screen.
58 void WCMD_clear_screen (void) {
60 /* Emulate by filling the screen from the top left to bottom right with
61 spaces, then moving the cursor to the top left afterwards */
62 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
63 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
65 if (GetConsoleScreenBufferInfo(hStdOut, &consoleInfo))
70 screenSize = consoleInfo.dwSize.X * (consoleInfo.dwSize.Y + 1);
74 FillConsoleOutputCharacter(hStdOut, ' ', screenSize, topLeft, &screenSize);
75 SetConsoleCursorPosition(hStdOut, topLeft);
79 /****************************************************************************
82 * Change the default i/o device (ie redirect STDin/STDout).
85 void WCMD_change_tty (void) {
91 /****************************************************************************
94 * Copy a file or wildcarded set.
95 * FIXME: No wildcard support
98 void WCMD_copy (void) {
104 static const char *overwrite = "Overwrite file (Y/N)?";
105 char string[8], outpath[MAX_PATH], inpath[MAX_PATH], *infile;
107 if ((strchr(param1,'*') != NULL) && (strchr(param1,'%') != NULL)) {
108 WCMD_output ("Wildcards not yet supported\n");
112 /* If no destination supplied, assume current directory */
113 if (param2[0] == 0x00) {
117 GetFullPathName (param2, sizeof(outpath), outpath, NULL);
118 hff = FindFirstFile (outpath, &fd);
119 if (hff != INVALID_HANDLE_VALUE) {
120 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
121 GetFullPathName (param1, sizeof(inpath), inpath, &infile);
122 strcat (outpath, "\\");
123 strcat (outpath, infile);
128 force = (strstr (quals, "/Y") != NULL);
130 hff = FindFirstFile (outpath, &fd);
131 if (hff != INVALID_HANDLE_VALUE) {
133 WCMD_output (overwrite);
134 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
135 if (toupper(string[0]) == 'Y') force = TRUE;
140 status = CopyFile (param1, outpath, FALSE);
141 if (!status) WCMD_print_error ();
145 /****************************************************************************
148 * Create a directory.
151 void WCMD_create_dir (void) {
153 if (!CreateDirectory (param1, NULL)) WCMD_print_error ();
156 /****************************************************************************
159 * Delete a file or wildcarded set.
163 void WCMD_delete (int recurse) {
167 char fpath[MAX_PATH];
170 hff = FindFirstFile (param1, &fd);
171 if (hff == INVALID_HANDLE_VALUE) {
172 WCMD_output ("%s :File Not Found\n",param1);
175 if ((strchr(param1,'*') == NULL) && (strchr(param1,'?') == NULL)
176 && (!recurse) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
177 strcat (param1, "\\*");
182 if ((strchr(param1,'*') != NULL) || (strchr(param1,'?') != NULL)) {
183 strcpy (fpath, param1);
185 p = strrchr (fpath, '\\');
188 strcat (fpath, fd.cFileName);
190 else strcpy (fpath, fd.cFileName);
191 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
192 if (!DeleteFile (fpath)) WCMD_print_error ();
194 } while (FindNextFile(hff, &fd) != 0);
198 if (!DeleteFile (param1)) WCMD_print_error ();
203 /****************************************************************************
206 * Echo input to the screen (or not). We don't try to emulate the bugs
207 * in DOS (try typing "ECHO ON AGAIN" for an example).
210 void WCMD_echo (const char *command) {
212 static const char *eon = "Echo is ON\n", *eoff = "Echo is OFF\n";
215 count = strlen(command);
217 if (echo_mode) WCMD_output (eon);
218 else WCMD_output (eoff);
221 if ((count == 1) && (command[0] == '.')) {
222 WCMD_output (newline);
225 if (lstrcmpi(command, "ON") == 0) {
229 if (lstrcmpi(command, "OFF") == 0) {
233 WCMD_output_asis (command);
234 WCMD_output (newline);
238 /**************************************************************************
241 * Batch file loop processing.
242 * FIXME: We don't exhaustively check syntax. Any command which works in MessDOS
243 * will probably work here, but the reverse is not necessarily the case...
246 void WCMD_for (char *p) {
251 char set[MAX_PATH], param[MAX_PATH];
254 if (lstrcmpi (WCMD_parameter (p, 1, NULL), "in")
255 || lstrcmpi (WCMD_parameter (p, 3, NULL), "do")
256 || (param1[0] != '%')) {
257 WCMD_output ("Syntax error\n");
260 lstrcpyn (set, WCMD_parameter (p, 2, NULL), sizeof(set));
261 WCMD_parameter (p, 4, &cmd);
262 lstrcpy (param, param1);
265 * If the parameter within the set has a wildcard then search for matching files
266 * otherwise do a literal substitution.
270 while (*(item = WCMD_parameter (set, i, NULL))) {
271 if (strpbrk (item, "*?")) {
272 hff = FindFirstFile (item, &fd);
273 if (hff == INVALID_HANDLE_VALUE) {
277 WCMD_execute (cmd, param, fd.cFileName);
278 } while (FindNextFile(hff, &fd) != 0);
282 WCMD_execute (cmd, param, item);
288 /*****************************************************************************
291 * Execute a command after substituting variable text for the supplied parameter
294 void WCMD_execute (char *orig_cmd, char *param, char *subst) {
296 char *new_cmd, *p, *s, *dup;
299 size = lstrlen (orig_cmd);
300 new_cmd = (char *) LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, size);
301 dup = s = strdup (orig_cmd);
303 while ((p = strstr (s, param))) {
305 size += lstrlen (subst);
306 new_cmd = (char *) LocalReAlloc ((HANDLE)new_cmd, size, 0);
308 strcat (new_cmd, subst);
309 s = p + lstrlen (param);
312 WCMD_process_command (new_cmd);
314 LocalFree ((HANDLE)new_cmd);
318 /**************************************************************************
321 * Simple on-line help. Help text is stored in the resource file.
324 void WCMD_give_help (char *command) {
329 command = WCMD_strtrim_leading_spaces(command);
330 if (lstrlen(command) == 0) {
331 LoadString (hinst, 1000, buffer, sizeof(buffer));
332 WCMD_output_asis (buffer);
335 for (i=0; i<=WCMD_EXIT; i++) {
336 if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
337 param1, -1, inbuilt[i], -1) == 2) {
338 LoadString (hinst, i, buffer, sizeof(buffer));
339 WCMD_output (buffer);
343 WCMD_output ("No help available for %s\n", param1);
348 /****************************************************************************
351 * Batch file jump instruction. Not the most efficient algorithm ;-)
352 * Prints error message if the specified label cannot be found - the file pointer is
353 * then at EOF, effectively stopping the batch file.
354 * FIXME: DOS is supposed to allow labels with spaces - we don't.
357 void WCMD_goto (void) {
359 char string[MAX_PATH];
361 if (context != NULL) {
362 SetFilePointer (context -> h, 0, NULL, FILE_BEGIN);
363 while (WCMD_fgets (string, sizeof(string), context -> h)) {
364 if ((string[0] == ':') && (strcmp (&string[1], param1) == 0)) return;
366 WCMD_output ("Target to GOTO not found\n");
372 /****************************************************************************
375 * Batch file conditional.
376 * FIXME: Much more syntax checking needed!
379 void WCMD_if (char *p) {
382 int negate = 0, test = 0;
383 char condition[MAX_PATH], *command, *s;
385 if (!lstrcmpi (param1, "not")) {
387 lstrcpy (condition, param2);
390 lstrcpy (condition, param1);
392 if (!lstrcmpi (condition, "errorlevel")) {
393 if (errorlevel >= atoi(WCMD_parameter (p, 1+negate, NULL))) test = 1;
395 WCMD_parameter (p, 2+negate, &command);
397 else if (!lstrcmpi (condition, "exist")) {
398 if ((h = CreateFile (WCMD_parameter (p, 1+negate, NULL), GENERIC_READ,
399 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
400 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE) {
404 WCMD_parameter (p, 2+negate, &command);
406 else if ((s = strstr (p, "=="))) {
408 if (!lstrcmpi (condition, WCMD_parameter (s, 0, NULL))) test = 1;
409 WCMD_parameter (s, 1, &command);
412 WCMD_output ("Syntax error\n");
415 if (test != negate) {
416 command = strdup (command);
417 WCMD_process_command (command);
422 /****************************************************************************
425 * Move a file, directory tree or wildcarded set of files.
426 * FIXME: Needs input and output files to be fully specified.
429 void WCMD_move (void) {
432 char outpath[MAX_PATH], inpath[MAX_PATH], *infile;
436 if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
437 WCMD_output ("Wildcards not yet supported\n");
441 /* If no destination supplied, assume current directory */
442 if (param2[0] == 0x00) {
446 /* If 2nd parm is directory, then use original filename */
447 GetFullPathName (param2, sizeof(outpath), outpath, NULL);
448 hff = FindFirstFile (outpath, &fd);
449 if (hff != INVALID_HANDLE_VALUE) {
450 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
451 GetFullPathName (param1, sizeof(inpath), inpath, &infile);
452 strcat (outpath, "\\");
453 strcat (outpath, infile);
458 status = MoveFile (param1, outpath);
459 if (!status) WCMD_print_error ();
462 /****************************************************************************
465 * Wait for keyboard input.
468 void WCMD_pause (void) {
473 WCMD_output (anykey);
474 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
477 /****************************************************************************
480 * Delete a directory.
483 void WCMD_remove_dir (void) {
485 if (!RemoveDirectory (param1)) WCMD_print_error ();
488 /****************************************************************************
492 * FIXME: Needs input and output files to be fully specified.
495 void WCMD_rename (void) {
499 if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
500 WCMD_output ("Wildcards not yet supported\n");
503 status = MoveFile (param1, param2);
504 if (!status) WCMD_print_error ();
507 /*****************************************************************************
508 * WCMD_setshow_attrib
510 * Display and optionally sets DOS attributes on a file or directory
512 * FIXME: Wine currently uses the Unix stat() function to get file attributes.
513 * As a result only the Readonly flag is correctly reported, the Archive bit
514 * is always set and the rest are not implemented. We do the Right Thing anyway.
516 * FIXME: No SET functionality.
520 void WCMD_setshow_attrib (void) {
525 char flags[9] = {" "};
527 if (param1[0] == '-') {
532 if (lstrlen(param1) == 0) {
533 GetCurrentDirectory (sizeof(param1), param1);
534 strcat (param1, "\\*");
537 hff = FindFirstFile (param1, &fd);
538 if (hff == INVALID_HANDLE_VALUE) {
539 WCMD_output ("%s: File Not Found\n",param1);
543 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
544 if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
547 if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
550 if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
553 if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
556 if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) {
559 if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) {
562 WCMD_output ("%s %s\n", flags, fd.cFileName);
563 for (count=0; count < 8; count++) flags[count] = ' ';
565 } while (FindNextFile(hff, &fd) != 0);
570 /*****************************************************************************
571 * WCMD_setshow_default
573 * Set/Show the current default directory
576 void WCMD_setshow_default (void) {
581 if (strlen(param1) == 0) {
582 GetCurrentDirectory (sizeof(string), string);
583 strcat (string, "\n");
584 WCMD_output (string);
587 status = SetCurrentDirectory (param1);
596 /****************************************************************************
599 * Set/Show the system date
600 * FIXME: Can't change date yet
603 void WCMD_setshow_date (void) {
605 char curdate[64], buffer[64];
608 if (lstrlen(param1) == 0) {
609 if (GetDateFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL,
610 curdate, sizeof(curdate))) {
611 WCMD_output ("Current Date is %s\nEnter new date: ", curdate);
612 ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
617 else WCMD_print_error ();
624 /****************************************************************************
627 int WCMD_compare( const void *a, const void *b )
630 const char * const *str_a = a, * const *str_b = b;
631 r = CompareString( LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
632 *str_a, -1, *str_b, -1 );
633 if( r == CSTR_LESS_THAN ) return -1;
634 if( r == CSTR_GREATER_THAN ) return 1;
638 /****************************************************************************
639 * WCMD_setshow_sortenv
641 * sort variables into order for display
643 void WCMD_setshow_sortenv(const char *s)
645 UINT count=0, len=0, i;
648 /* count the number of strings, and the total length */
650 len += (lstrlen(&s[len]) + 1);
654 /* add the strings to an array */
655 str = LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, count * sizeof (char*) );
659 for( i=1; i<count; i++ )
660 str[i] = str[i-1] + lstrlen(str[i-1]) + 1;
663 qsort( str, count, sizeof (char*), WCMD_compare );
666 for( i=0; i<count; i++ )
667 WCMD_output("%s\n", str[i] );
672 /****************************************************************************
675 * Set/Show the environment variables
678 void WCMD_setshow_env (char *s) {
685 if (strlen(param1) == 0) {
686 env = GetEnvironmentStrings ();
687 WCMD_setshow_sortenv( env );
693 /* FIXME: Emulate Win98 for now, ie "SET C" looks ONLY for an
694 environment variable C, whereas on NT it shows ALL variables
697 status = GetEnvironmentVariable(s, buffer, sizeof(buffer));
699 WCMD_output("%s=%s\n", s, buffer);
701 WCMD_output ("Environment variable %s not defined\n", s);
707 if (strlen(p) == 0) p = 0x00;
708 status = SetEnvironmentVariable (s, p);
709 if (!status) WCMD_print_error();
711 /* WCMD_output (newline); @JED*/
714 /****************************************************************************
717 * Set/Show the path environment variable
720 void WCMD_setshow_path (char *command) {
725 if (strlen(param1) == 0) {
726 status = GetEnvironmentVariable ("PATH", string, sizeof(string));
728 WCMD_output ("PATH=%s\n", string);
731 WCMD_output ("PATH not found\n");
735 status = SetEnvironmentVariable ("PATH", command);
736 if (!status) WCMD_print_error();
740 /****************************************************************************
741 * WCMD_setshow_prompt
743 * Set or show the command prompt.
746 void WCMD_setshow_prompt (void) {
750 if (strlen(param1) == 0) {
751 SetEnvironmentVariable ("PROMPT", NULL);
755 while ((*s == '=') || (*s == ' ')) s++;
756 if (strlen(s) == 0) {
757 SetEnvironmentVariable ("PROMPT", NULL);
759 else SetEnvironmentVariable ("PROMPT", s);
763 /****************************************************************************
766 * Set/Show the system time
767 * FIXME: Can't change time yet
770 void WCMD_setshow_time (void) {
772 char curtime[64], buffer[64];
776 if (strlen(param1) == 0) {
778 if (GetTimeFormat (LOCALE_USER_DEFAULT, 0, &st, NULL,
779 curtime, sizeof(curtime))) {
780 WCMD_output ("Current Time is %s\nEnter new time: ", curtime);
781 ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
786 else WCMD_print_error ();
793 /****************************************************************************
796 * Shift batch parameters.
799 void WCMD_shift (void) {
801 if (context != NULL) context -> shift_count++;
805 /****************************************************************************
808 * Set the console title
810 void WCMD_title (char *command) {
811 SetConsoleTitle(command);
814 /****************************************************************************
817 * Copy a file to standard output.
820 void WCMD_type (void) {
826 h = CreateFile (param1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
827 FILE_ATTRIBUTE_NORMAL, NULL);
828 if (h == INVALID_HANDLE_VALUE) {
832 while (ReadFile (h, buffer, sizeof(buffer), &count, NULL)) {
833 if (count == 0) break; /* ReadFile reports success on EOF! */
835 WCMD_output_asis (buffer);
840 /****************************************************************************
843 * Display verify flag.
844 * FIXME: We don't actually do anything with the verify flag other than toggle
848 void WCMD_verify (char *command) {
850 static const char *von = "Verify is ON\n", *voff = "Verify is OFF\n";
853 count = strlen(command);
855 if (verify_mode) WCMD_output (von);
856 else WCMD_output (voff);
859 if (lstrcmpi(command, "ON") == 0) {
863 else if (lstrcmpi(command, "OFF") == 0) {
867 else WCMD_output ("Verify must be ON or OFF\n");
870 /****************************************************************************
873 * Display version info.
876 void WCMD_version (void) {
878 WCMD_output (version_string);
882 /****************************************************************************
885 * Display volume info and/or set volume label. Returns 0 if error.
888 int WCMD_volume (int mode, char *path) {
891 char string[MAX_PATH], label[MAX_PATH], curdir[MAX_PATH];
893 static char syntax[] = "Syntax Error\n\n";
895 if (lstrlen(path) == 0) {
896 status = GetCurrentDirectory (sizeof(curdir), curdir);
901 status = GetVolumeInformation (NULL, label, sizeof(label), &serial, NULL,
905 if ((path[1] != ':') || (lstrlen(path) != 2)) {
906 WCMD_output_asis(syntax);
909 wsprintf (curdir, "%s\\", path);
910 status = GetVolumeInformation (curdir, label, sizeof(label), &serial, NULL,
917 WCMD_output ("Volume in drive %c is %s\nVolume Serial Number is %04x-%04x\n\n",
918 curdir[0], label, HIWORD(serial), LOWORD(serial));
920 WCMD_output ("Volume label (11 characters, ENTER for none)?");
921 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
923 string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
924 if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
926 if (lstrlen(path) != 0) {
927 if (!SetVolumeLabel (curdir, string)) WCMD_print_error ();
930 if (!SetVolumeLabel (NULL, string)) WCMD_print_error ();