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 () {
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 */
64 CONSOLE_SCREEN_BUFFER_INFO consoleInfo
;
65 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
67 GetConsoleScreenBufferInfo(hStdOut
, &consoleInfo
);
68 screenSize
= consoleInfo
.dwSize
.X
* (consoleInfo
.dwSize
.Y
+ 1);
72 FillConsoleOutputCharacter(hStdOut
, ' ', screenSize
, topLeft
, &screenSize
);
73 SetConsoleCursorPosition(hStdOut
, topLeft
);
76 /****************************************************************************
79 * Change the default i/o device (ie redirect STDin/STDout).
82 void WCMD_change_tty () {
88 /****************************************************************************
91 * Copy a file or wildcarded set.
92 * FIXME: No wildcard support
101 static char *overwrite
= "Overwrite file (Y/N)?";
102 char string
[8], outpath
[MAX_PATH
], inpath
[MAX_PATH
], *infile
;
104 if ((strchr(param1
,'*') != NULL
) && (strchr(param1
,'%') != NULL
)) {
105 WCMD_output ("Wildcards not yet supported\n");
109 /* If no destination supplied, assume current directory */
110 if (param2
[0] == 0x00) {
114 GetFullPathName (param2
, sizeof(outpath
), outpath
, NULL
);
115 hff
= FindFirstFile (outpath
, &fd
);
116 if (hff
!= INVALID_HANDLE_VALUE
) {
117 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) {
118 GetFullPathName (param1
, sizeof(inpath
), inpath
, &infile
);
119 strcat (outpath
, "\\");
120 strcat (outpath
, infile
);
125 force
= (strstr (quals
, "/Y") != NULL
);
127 hff
= FindFirstFile (outpath
, &fd
);
128 if (hff
!= INVALID_HANDLE_VALUE
) {
130 WCMD_output (overwrite
);
131 ReadFile (GetStdHandle(STD_INPUT_HANDLE
), string
, sizeof(string
), &count
, NULL
);
132 if (toupper(string
[0]) == 'Y') force
= TRUE
;
137 status
= CopyFile (param1
, outpath
, FALSE
);
138 if (!status
) WCMD_print_error ();
142 /****************************************************************************
145 * Create a directory.
148 void WCMD_create_dir () {
150 if (!CreateDirectory (param1
, NULL
)) WCMD_print_error ();
153 /****************************************************************************
156 * Delete a file or wildcarded set.
160 void WCMD_delete (int recurse
) {
164 char fpath
[MAX_PATH
];
167 hff
= FindFirstFile (param1
, &fd
);
168 if (hff
== INVALID_HANDLE_VALUE
) {
169 WCMD_output ("%s :File Not Found\n",param1
);
172 if ((strchr(param1
,'*') == NULL
) && (strchr(param1
,'?') == NULL
)
173 && (!recurse
) && (fd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)) {
174 strcat (param1
, "\\*");
179 if ((strchr(param1
,'*') != NULL
) || (strchr(param1
,'?') != NULL
)) {
180 strcpy (fpath
, param1
);
182 p
= strrchr (fpath
, '\\');
185 strcat (fpath
, fd
.cFileName
);
187 else strcpy (fpath
, fd
.cFileName
);
188 if (!(fd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)) {
189 if (!DeleteFile (fpath
)) WCMD_print_error ();
191 } while (FindNextFile(hff
, &fd
) != 0);
195 if (!DeleteFile (param1
)) WCMD_print_error ();
200 /****************************************************************************
203 * Echo input to the screen (or not). We don't try to emulate the bugs
204 * in DOS (try typing "ECHO ON AGAIN" for an example).
207 void WCMD_echo (char *command
) {
209 static char *eon
= "Echo is ON\n", *eoff
= "Echo is OFF\n";
212 count
= strlen(command
);
214 if (echo_mode
) WCMD_output (eon
);
215 else WCMD_output (eoff
);
218 if ((count
== 1) && (command
[0] == '.')) {
219 WCMD_output (newline
);
222 if (lstrcmpi(command
, "ON") == 0) {
226 if (lstrcmpi(command
, "OFF") == 0) {
230 WCMD_output_asis (command
);
231 WCMD_output (newline
);
235 /**************************************************************************
238 * Batch file loop processing.
239 * FIXME: We don't exhaustively check syntax. Any command which works in MessDOS
240 * will probably work here, but the reverse is not necessarily the case...
243 void WCMD_for (char *p
) {
248 char set
[MAX_PATH
], param
[MAX_PATH
];
251 if (lstrcmpi (WCMD_parameter (p
, 1, NULL
), "in")
252 || lstrcmpi (WCMD_parameter (p
, 3, NULL
), "do")
253 || (param1
[0] != '%')) {
254 WCMD_output ("Syntax error\n");
257 lstrcpyn (set
, WCMD_parameter (p
, 2, NULL
), sizeof(set
));
258 WCMD_parameter (p
, 4, &cmd
);
259 lstrcpy (param
, param1
);
262 * If the parameter within the set has a wildcard then search for matching files
263 * otherwise do a literal substitution.
267 while (*(item
= WCMD_parameter (set
, i
, NULL
))) {
268 if (strpbrk (item
, "*?")) {
269 hff
= FindFirstFile (item
, &fd
);
270 if (hff
== INVALID_HANDLE_VALUE
) {
274 WCMD_execute (cmd
, param
, fd
.cFileName
);
275 } while (FindNextFile(hff
, &fd
) != 0);
279 WCMD_execute (cmd
, param
, item
);
285 /*****************************************************************************
288 * Execute a command after substituting variable text for the supplied parameter
291 void WCMD_execute (char *orig_cmd
, char *param
, char *subst
) {
293 char *new_cmd
, *p
, *s
, *dup
;
296 size
= lstrlen (orig_cmd
);
297 new_cmd
= (char *) LocalAlloc (LMEM_FIXED
| LMEM_ZEROINIT
, size
);
298 dup
= s
= strdup (orig_cmd
);
300 while ((p
= strstr (s
, param
))) {
302 size
+= lstrlen (subst
);
303 new_cmd
= (char *) LocalReAlloc ((HANDLE
)new_cmd
, size
, 0);
305 strcat (new_cmd
, subst
);
306 s
= p
+ lstrlen (param
);
309 WCMD_process_command (new_cmd
);
311 LocalFree ((HANDLE
)new_cmd
);
315 /**************************************************************************
318 * Simple on-line help. Help text is stored in the resource file.
321 void WCMD_give_help (char *command
) {
326 command
= WCMD_strtrim_leading_spaces(command
);
327 if (lstrlen(command
) == 0) {
328 LoadString (0, 1000, buffer
, sizeof(buffer
));
329 WCMD_output (buffer
);
332 for (i
=0; i
<=WCMD_EXIT
; i
++) {
333 if (CompareString (LOCALE_USER_DEFAULT
, NORM_IGNORECASE
| SORT_STRINGSORT
,
334 param1
, -1, inbuilt
[i
], -1) == 2) {
335 LoadString (hinst
, i
, buffer
, sizeof(buffer
));
336 WCMD_output (buffer
);
340 WCMD_output ("No help available for %s\n", param1
);
345 /****************************************************************************
348 * Batch file jump instruction. Not the most efficient algorithm ;-)
349 * Prints error message if the specified label cannot be found - the file pointer is
350 * then at EOF, effectively stopping the batch file.
351 * FIXME: DOS is supposed to allow labels with spaces - we don't.
356 char string
[MAX_PATH
];
358 if (context
!= NULL
) {
359 SetFilePointer (context
-> h
, 0, NULL
, FILE_BEGIN
);
360 while (WCMD_fgets (string
, sizeof(string
), context
-> h
)) {
361 if ((string
[0] == ':') && (strcmp (&string
[1], param1
) == 0)) return;
363 WCMD_output ("Target to GOTO not found\n");
369 /****************************************************************************
372 * Batch file conditional.
373 * FIXME: Much more syntax checking needed!
376 void WCMD_if (char *p
) {
379 int negate
= 0, test
= 0;
380 char condition
[MAX_PATH
], *command
, *s
;
382 if (!lstrcmpi (param1
, "not")) {
384 lstrcpy (condition
, param2
);
387 lstrcpy (condition
, param1
);
389 if (!lstrcmpi (condition
, "errorlevel")) {
390 if (errorlevel
>= atoi(WCMD_parameter (p
, 1+negate
, NULL
))) test
= 1;
393 else if (!lstrcmpi (condition
, "exist")) {
394 if ((h
= CreateFile (WCMD_parameter (p
, 1+negate
, NULL
), GENERIC_READ
, 0, NULL
,
395 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
)) != INVALID_HANDLE_VALUE
) {
400 else if ((s
= strstr (p
, "=="))) {
402 if (!lstrcmpi (condition
, WCMD_parameter (s
, 0, NULL
))) test
= 1;
405 WCMD_output ("Syntax error\n");
408 if (test
!= negate
) {
409 WCMD_parameter (p
, 2+negate
, &s
);
410 command
= strdup (s
);
411 WCMD_process_command (command
);
416 /****************************************************************************
419 * Move a file, directory tree or wildcarded set of files.
420 * FIXME: Needs input and output files to be fully specified.
426 char outpath
[MAX_PATH
], inpath
[MAX_PATH
], *infile
;
430 if ((strchr(param1
,'*') != NULL
) || (strchr(param1
,'%') != NULL
)) {
431 WCMD_output ("Wildcards not yet supported\n");
435 /* If no destination supplied, assume current directory */
436 if (param2
[0] == 0x00) {
440 /* If 2nd parm is directory, then use original filename */
441 GetFullPathName (param2
, sizeof(outpath
), outpath
, NULL
);
442 hff
= FindFirstFile (outpath
, &fd
);
443 if (hff
!= INVALID_HANDLE_VALUE
) {
444 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) {
445 GetFullPathName (param1
, sizeof(inpath
), inpath
, &infile
);
446 strcat (outpath
, "\\");
447 strcat (outpath
, infile
);
452 status
= MoveFile (param1
, outpath
);
453 if (!status
) WCMD_print_error ();
456 /****************************************************************************
459 * Wait for keyboard input.
467 WCMD_output (anykey
);
468 ReadFile (GetStdHandle(STD_INPUT_HANDLE
), string
, sizeof(string
), &count
, NULL
);
471 /****************************************************************************
474 * Delete a directory.
477 void WCMD_remove_dir () {
479 if (!RemoveDirectory (param1
)) WCMD_print_error ();
482 /****************************************************************************
486 * FIXME: Needs input and output files to be fully specified.
489 void WCMD_rename () {
493 if ((strchr(param1
,'*') != NULL
) || (strchr(param1
,'%') != NULL
)) {
494 WCMD_output ("Wildcards not yet supported\n");
497 status
= MoveFile (param1
, param2
);
498 if (!status
) WCMD_print_error ();
501 /*****************************************************************************
502 * WCMD_setshow_attrib
504 * Display and optionally sets DOS attributes on a file or directory
506 * FIXME: Wine currently uses the Unix stat() function to get file attributes.
507 * As a result only the Readonly flag is correctly reported, the Archive bit
508 * is always set and the rest are not implemented. We do the Right Thing anyway.
510 * FIXME: No SET functionality.
514 void WCMD_setshow_attrib () {
519 char flags
[9] = {" "};
521 if (param1
[0] == '-') {
526 if (lstrlen(param1
) == 0) {
527 GetCurrentDirectory (sizeof(param1
), param1
);
528 strcat (param1
, "\\*");
531 hff
= FindFirstFile (param1
, &fd
);
532 if (hff
== INVALID_HANDLE_VALUE
) {
533 WCMD_output ("%s: File Not Found\n",param1
);
537 if (!(fd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)) {
538 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_HIDDEN
) {
541 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_SYSTEM
) {
544 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_ARCHIVE
) {
547 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) {
550 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_TEMPORARY
) {
553 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_COMPRESSED
) {
556 WCMD_output ("%s %s\n", flags
, fd
.cFileName
);
557 for (count
=0; count
< 8; count
++) flags
[count
] = ' ';
559 } while (FindNextFile(hff
, &fd
) != 0);
564 /*****************************************************************************
565 * WCMD_setshow_default
567 * Set/Show the current default directory
570 void WCMD_setshow_default () {
575 if (strlen(param1
) == 0) {
576 GetCurrentDirectory (sizeof(string
), string
);
577 strcat (string
, "\n");
578 WCMD_output (string
);
581 status
= SetCurrentDirectory (param1
);
590 /****************************************************************************
593 * Set/Show the system date
594 * FIXME: Can't change date yet
597 void WCMD_setshow_date () {
599 char curdate
[64], buffer
[64];
602 if (lstrlen(param1
) == 0) {
603 if (GetDateFormat (LOCALE_USER_DEFAULT
, 0, NULL
, NULL
,
604 curdate
, sizeof(curdate
))) {
605 WCMD_output ("Current Date is %s\nEnter new date: ", curdate
);
606 ReadFile (GetStdHandle(STD_INPUT_HANDLE
), buffer
, sizeof(buffer
), &count
, NULL
);
611 else WCMD_print_error ();
618 /****************************************************************************
621 * Set/Show the environment variables
623 * FIXME: need to sort variables into order for display?
626 void WCMD_setshow_env (char *s
) {
633 if (strlen(param1
) == 0) {
634 env
= GetEnvironmentStrings ();
637 WCMD_output ("%s\n", p
);
645 /* FIXME: Emulate Win98 for now, ie "SET C" looks ONLY for an
646 environment variable C, whereas on NT it shows ALL variables
649 status
= GetEnvironmentVariable(s
, buffer
, sizeof(buffer
));
651 WCMD_output("%s=%s\n", s
, buffer
);
653 WCMD_output ("Environment variable %s not defined\n", s
);
659 if (strlen(p
) == 0) p
= 0x00;
660 status
= SetEnvironmentVariable (s
, p
);
661 if (!status
) WCMD_print_error();
663 /* WCMD_output (newline); @JED*/
666 /****************************************************************************
669 * Set/Show the path environment variable
672 void WCMD_setshow_path () {
677 if (strlen(param1
) == 0) {
678 status
= GetEnvironmentVariable ("PATH", string
, sizeof(string
));
680 WCMD_output ("PATH=%s\n", string
);
683 WCMD_output ("PATH not found\n");
687 status
= SetEnvironmentVariable ("PATH", param1
);
688 if (!status
) WCMD_print_error();
692 /****************************************************************************
693 * WCMD_setshow_prompt
695 * Set or show the command prompt.
698 void WCMD_setshow_prompt () {
702 if (strlen(param1
) == 0) {
703 SetEnvironmentVariable ("PROMPT", NULL
);
707 while ((*s
== '=') || (*s
== ' ')) s
++;
708 if (strlen(s
) == 0) {
709 SetEnvironmentVariable ("PROMPT", NULL
);
711 else SetEnvironmentVariable ("PROMPT", s
);
715 /****************************************************************************
718 * Set/Show the system time
719 * FIXME: Can't change time yet
722 void WCMD_setshow_time () {
724 char curtime
[64], buffer
[64];
728 if (strlen(param1
) == 0) {
730 if (GetTimeFormat (LOCALE_USER_DEFAULT
, 0, &st
, NULL
,
731 curtime
, sizeof(curtime
))) {
732 WCMD_output ("Current Time is %s\nEnter new time: ", curtime
);
733 ReadFile (GetStdHandle(STD_INPUT_HANDLE
), buffer
, sizeof(buffer
), &count
, NULL
);
738 else WCMD_print_error ();
745 /****************************************************************************
748 * Shift batch parameters.
753 if (context
!= NULL
) context
-> shift_count
++;
757 /****************************************************************************
760 * Set the console title
762 void WCMD_title (char *command
) {
763 SetConsoleTitle(command
);
766 /****************************************************************************
769 * Copy a file to standard output.
778 h
= CreateFile (param1
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
,
779 FILE_ATTRIBUTE_NORMAL
, NULL
);
780 if (h
== INVALID_HANDLE_VALUE
) {
784 while (ReadFile (h
, buffer
, sizeof(buffer
), &count
, NULL
)) {
785 if (count
== 0) break; /* ReadFile reports success on EOF! */
786 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE
), buffer
, count
, &count
, NULL
);
791 /****************************************************************************
794 * Display verify flag.
795 * FIXME: We don't actually do anything with the verify flag other than toggle
799 void WCMD_verify (char *command
) {
801 static char *von
= "Verify is ON\n", *voff
= "Verify is OFF\n";
804 count
= strlen(command
);
806 if (verify_mode
) WCMD_output (von
);
807 else WCMD_output (voff
);
810 if (lstrcmpi(command
, "ON") == 0) {
814 else if (lstrcmpi(command
, "OFF") == 0) {
818 else WCMD_output ("Verify must be ON or OFF\n");
821 /****************************************************************************
824 * Display version info.
827 void WCMD_version () {
829 WCMD_output (version_string
);
833 /****************************************************************************
836 * Display volume info and/or set volume label. Returns 0 if error.
839 int WCMD_volume (int mode
, char *path
) {
842 char string
[MAX_PATH
], label
[MAX_PATH
], curdir
[MAX_PATH
];
844 static char syntax
[] = "Syntax Error\n\n";
846 if (lstrlen(path
) == 0) {
847 status
= GetCurrentDirectory (sizeof(curdir
), curdir
);
852 status
= GetVolumeInformation (NULL
, label
, sizeof(label
), &serial
, NULL
,
856 if ((path
[1] != ':') || (lstrlen(path
) != 2)) {
857 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE
), syntax
, strlen(syntax
), &count
, NULL
);
860 wsprintf (curdir
, "%s\\", path
);
861 status
= GetVolumeInformation (curdir
, label
, sizeof(label
), &serial
, NULL
,
868 WCMD_output ("Volume in drive %c is %s\nVolume Serial Number is %04x-%04x\n\n",
869 curdir
[0], label
, HIWORD(serial
), LOWORD(serial
));
871 WCMD_output ("Volume label (11 characters, ENTER for none)?");
872 ReadFile (GetStdHandle(STD_INPUT_HANDLE
), string
, sizeof(string
), &count
, NULL
);
874 string
[count
-1] = '\0'; /* ReadFile output is not null-terminated! */
875 if (string
[count
-2] == '\r') string
[count
-2] = '\0'; /* Under Windoze we get CRLF! */
877 if (lstrlen(path
) != 0) {
878 if (!SetVolumeLabel (curdir
, string
)) WCMD_print_error ();
881 if (!SetVolumeLabel (NULL
, string
)) WCMD_print_error ();