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 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 (char *command
) {
212 static 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 (0, 1000, buffer
, sizeof(buffer
));
332 WCMD_output (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 * Set/Show the environment variables
629 * FIXME: need to sort variables into order for display?
632 void WCMD_setshow_env (char *s
) {
639 if (strlen(param1
) == 0) {
640 env
= GetEnvironmentStrings ();
643 WCMD_output ("%s\n", p
);
651 /* FIXME: Emulate Win98 for now, ie "SET C" looks ONLY for an
652 environment variable C, whereas on NT it shows ALL variables
655 status
= GetEnvironmentVariable(s
, buffer
, sizeof(buffer
));
657 WCMD_output("%s=%s\n", s
, buffer
);
659 WCMD_output ("Environment variable %s not defined\n", s
);
665 if (strlen(p
) == 0) p
= 0x00;
666 status
= SetEnvironmentVariable (s
, p
);
667 if (!status
) WCMD_print_error();
669 /* WCMD_output (newline); @JED*/
672 /****************************************************************************
675 * Set/Show the path environment variable
678 void WCMD_setshow_path (char *command
) {
683 if (strlen(param1
) == 0) {
684 status
= GetEnvironmentVariable ("PATH", string
, sizeof(string
));
686 WCMD_output ("PATH=%s\n", string
);
689 WCMD_output ("PATH not found\n");
693 status
= SetEnvironmentVariable ("PATH", command
);
694 if (!status
) WCMD_print_error();
698 /****************************************************************************
699 * WCMD_setshow_prompt
701 * Set or show the command prompt.
704 void WCMD_setshow_prompt (void) {
708 if (strlen(param1
) == 0) {
709 SetEnvironmentVariable ("PROMPT", NULL
);
713 while ((*s
== '=') || (*s
== ' ')) s
++;
714 if (strlen(s
) == 0) {
715 SetEnvironmentVariable ("PROMPT", NULL
);
717 else SetEnvironmentVariable ("PROMPT", s
);
721 /****************************************************************************
724 * Set/Show the system time
725 * FIXME: Can't change time yet
728 void WCMD_setshow_time (void) {
730 char curtime
[64], buffer
[64];
734 if (strlen(param1
) == 0) {
736 if (GetTimeFormat (LOCALE_USER_DEFAULT
, 0, &st
, NULL
,
737 curtime
, sizeof(curtime
))) {
738 WCMD_output ("Current Time is %s\nEnter new time: ", curtime
);
739 ReadFile (GetStdHandle(STD_INPUT_HANDLE
), buffer
, sizeof(buffer
), &count
, NULL
);
744 else WCMD_print_error ();
751 /****************************************************************************
754 * Shift batch parameters.
757 void WCMD_shift (void) {
759 if (context
!= NULL
) context
-> shift_count
++;
763 /****************************************************************************
766 * Set the console title
768 void WCMD_title (char *command
) {
769 SetConsoleTitle(command
);
772 /****************************************************************************
775 * Copy a file to standard output.
778 void WCMD_type (void) {
784 h
= CreateFile (param1
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
,
785 FILE_ATTRIBUTE_NORMAL
, NULL
);
786 if (h
== INVALID_HANDLE_VALUE
) {
790 while (ReadFile (h
, buffer
, sizeof(buffer
), &count
, NULL
)) {
791 if (count
== 0) break; /* ReadFile reports success on EOF! */
793 WCMD_output_asis (buffer
);
798 /****************************************************************************
801 * Display verify flag.
802 * FIXME: We don't actually do anything with the verify flag other than toggle
806 void WCMD_verify (char *command
) {
808 static char *von
= "Verify is ON\n", *voff
= "Verify is OFF\n";
811 count
= strlen(command
);
813 if (verify_mode
) WCMD_output (von
);
814 else WCMD_output (voff
);
817 if (lstrcmpi(command
, "ON") == 0) {
821 else if (lstrcmpi(command
, "OFF") == 0) {
825 else WCMD_output ("Verify must be ON or OFF\n");
828 /****************************************************************************
831 * Display version info.
834 void WCMD_version (void) {
836 WCMD_output (version_string
);
840 /****************************************************************************
843 * Display volume info and/or set volume label. Returns 0 if error.
846 int WCMD_volume (int mode
, char *path
) {
849 char string
[MAX_PATH
], label
[MAX_PATH
], curdir
[MAX_PATH
];
851 static char syntax
[] = "Syntax Error\n\n";
853 if (lstrlen(path
) == 0) {
854 status
= GetCurrentDirectory (sizeof(curdir
), curdir
);
859 status
= GetVolumeInformation (NULL
, label
, sizeof(label
), &serial
, NULL
,
863 if ((path
[1] != ':') || (lstrlen(path
) != 2)) {
864 WCMD_output_asis(syntax
);
867 wsprintf (curdir
, "%s\\", path
);
868 status
= GetVolumeInformation (curdir
, label
, sizeof(label
), &serial
, NULL
,
875 WCMD_output ("Volume in drive %c is %s\nVolume Serial Number is %04x-%04x\n\n",
876 curdir
[0], label
, HIWORD(serial
), LOWORD(serial
));
878 WCMD_output ("Volume label (11 characters, ENTER for none)?");
879 ReadFile (GetStdHandle(STD_INPUT_HANDLE
), string
, sizeof(string
), &count
, NULL
);
881 string
[count
-1] = '\0'; /* ReadFile output is not null-terminated! */
882 if (string
[count
-2] == '\r') string
[count
-2] = '\0'; /* Under Windoze we get CRLF! */
884 if (lstrlen(path
) != 0) {
885 if (!SetVolumeLabel (curdir
, string
)) WCMD_print_error ();
888 if (!SetVolumeLabel (NULL
, string
)) WCMD_print_error ();