2 * WCMD - Wine-compatible command line interface - built-in functions.
6 * On entry to each function, global variables quals, param1, param2 contain
7 * the qualifiers (uppercased and concatenated) and parameters entered, with
8 * environment-variable and batch parameter substitution already done.
13 * - No support for pipes, shell parameters
14 * - Lots of functionality missing from builtins
15 * - Messages etc need international support
20 void WCMD_execute (char *orig_command
, char *parameter
, char *substitution
);
22 extern HINSTANCE hinst
;
23 extern char *inbuilt
[];
25 extern char newline
[];
26 extern char version_string
[];
28 extern int echo_mode
, verify_mode
;
29 extern char quals
[MAX_PATH
], param1
[MAX_PATH
], param2
[MAX_PATH
];
30 extern BATCH_CONTEXT
*context
;
31 extern DWORD errorlevel
;
35 /****************************************************************************
38 * Clear the terminal screen.
41 void WCMD_clear_screen () {
47 /****************************************************************************
50 * Change the default i/o device (ie redirect STDin/STDout).
53 void WCMD_change_tty () {
59 /****************************************************************************
62 * Copy a file or wildcarded set.
63 * FIXME: No wildcard support
72 static char *overwrite
= "Overwrite file (Y/N)?";
73 char string
[8], outpath
[MAX_PATH
], inpath
[MAX_PATH
], *infile
;
75 if ((strchr(param1
,'*') != NULL
) && (strchr(param1
,'%') != NULL
)) {
76 WCMD_output ("Wildcards not yet supported\n");
79 GetFullPathName (param2
, sizeof(outpath
), outpath
, NULL
);
80 hff
= FindFirstFile (outpath
, &fd
);
81 if (hff
!= INVALID_HANDLE_VALUE
) {
82 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) {
83 GetFullPathName (param1
, sizeof(inpath
), inpath
, &infile
);
84 strcat (outpath
, "\\");
85 strcat (outpath
, infile
);
89 force
= (strstr (quals
, "/Y") != NULL
);
91 hff
= FindFirstFile (outpath
, &fd
);
92 if (hff
!= INVALID_HANDLE_VALUE
) {
94 WCMD_output (overwrite
);
95 ReadFile (GetStdHandle(STD_INPUT_HANDLE
), string
, sizeof(string
), &count
, NULL
);
96 if (toupper(string
[0]) == 'Y') force
= TRUE
;
101 status
= CopyFile (param1
, outpath
, FALSE
);
102 if (!status
) WCMD_print_error ();
106 /****************************************************************************
109 * Create a directory.
112 void WCMD_create_dir () {
114 if (!CreateDirectory (param1
, NULL
)) WCMD_print_error ();
117 /****************************************************************************
120 * Delete a file or wildcarded set.
124 void WCMD_delete (int recurse
) {
128 char fpath
[MAX_PATH
];
131 hff
= FindFirstFile (param1
, &fd
);
132 if (hff
== INVALID_HANDLE_VALUE
) {
133 WCMD_output ("File Not Found\n");
136 if ((strchr(param1
,'*') == NULL
) && (strchr(param1
,'?') == NULL
)
137 && (!recurse
) && (fd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)) {
138 strcat (param1
, "\\*");
142 if ((strchr(param1
,'*') != NULL
) || (strchr(param1
,'?') != NULL
)) {
143 strcpy (fpath
, param1
);
145 p
= strrchr (fpath
, '\\');
148 strcat (fpath
, fd
.cFileName
);
150 else strcpy (fpath
, fd
.cFileName
);
151 if (!(fd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)) {
152 if (!DeleteFile (fpath
)) WCMD_print_error ();
154 } while (FindNextFile(hff
, &fd
) != 0);
158 if (!DeleteFile (param1
)) WCMD_print_error ();
162 /****************************************************************************
165 * Echo input to the screen (or not). We don't try to emulate the bugs
166 * in DOS (try typing "ECHO ON AGAIN" for an example).
169 void WCMD_echo (char *command
) {
171 static char *eon
= "Echo is ON\n", *eoff
= "Echo is OFF\n";
174 count
= strlen(command
);
176 if (echo_mode
) WCMD_output (eon
);
177 else WCMD_output (eoff
);
180 if ((count
== 1) && (command
[0] == '.')) {
181 WCMD_output (newline
);
184 if (lstrcmpi(command
, "ON") == 0) {
188 if (lstrcmpi(command
, "OFF") == 0) {
192 WCMD_output_asis (command
);
193 WCMD_output (newline
);
197 /**************************************************************************
200 * Batch file loop processing.
201 * FIXME: We don't exhaustively check syntax. Any command which works in MessDOS
202 * will probably work here, but the reverse is not necessarily the case...
205 void WCMD_for (char *p
) {
210 char set
[MAX_PATH
], param
[MAX_PATH
];
213 if (lstrcmpi (WCMD_parameter (p
, 1, NULL
), "in")
214 || lstrcmpi (WCMD_parameter (p
, 3, NULL
), "do")
215 || (param1
[0] != '%')) {
216 WCMD_output ("Syntax error\n");
219 lstrcpyn (set
, WCMD_parameter (p
, 2, NULL
), sizeof(set
));
220 WCMD_parameter (p
, 4, &cmd
);
221 lstrcpy (param
, param1
);
224 * If the parameter within the set has a wildcard then search for matching files
225 * otherwise do a literal substitution.
229 while (*(item
= WCMD_parameter (set
, i
, NULL
))) {
230 if (strpbrk (item
, "*?")) {
231 hff
= FindFirstFile (item
, &fd
);
232 if (hff
== INVALID_HANDLE_VALUE
) {
236 WCMD_execute (cmd
, param
, fd
.cFileName
);
237 } while (FindNextFile(hff
, &fd
) != 0);
241 WCMD_execute (cmd
, param
, item
);
247 /*****************************************************************************
250 * Execute a command after substituting variable text for the supplied parameter
253 void WCMD_execute (char *orig_cmd
, char *param
, char *subst
) {
255 char *new_cmd
, *p
, *s
, *dup
;
258 size
= lstrlen (orig_cmd
);
259 new_cmd
= (char *) LocalAlloc (LMEM_FIXED
| LMEM_ZEROINIT
, size
);
260 dup
= s
= strdup (orig_cmd
);
262 while ((p
= strstr (s
, param
))) {
264 size
+= lstrlen (subst
);
265 new_cmd
= (char *) LocalReAlloc ((HANDLE
)new_cmd
, size
, 0);
267 strcat (new_cmd
, subst
);
268 s
= p
+ lstrlen (param
);
271 WCMD_process_command (new_cmd
);
273 LocalFree ((HANDLE
)new_cmd
);
277 /**************************************************************************
280 * Simple on-line help. Help text is stored in the resource file.
283 void WCMD_give_help (char *command
) {
288 command
= WCMD_strtrim_leading_spaces(command
);
289 if (lstrlen(command
) == 0) {
290 LoadString (0, 1000, buffer
, sizeof(buffer
));
291 WCMD_output (buffer
);
294 for (i
=0; i
<=WCMD_EXIT
; i
++) {
295 if (CompareString (LOCALE_USER_DEFAULT
, NORM_IGNORECASE
| SORT_STRINGSORT
,
296 param1
, -1, inbuilt
[i
], -1) == 2) {
297 LoadString (hinst
, i
, buffer
, sizeof(buffer
));
298 WCMD_output (buffer
);
302 WCMD_output ("No help available for %s\n", param1
);
307 /****************************************************************************
310 * Batch file jump instruction. Not the most efficient algorithm ;-)
311 * Prints error message if the specified label cannot be found - the file pointer is
312 * then at EOF, effectively stopping the batch file.
313 * FIXME: DOS is supposed to allow labels with spaces - we don't.
318 char string
[MAX_PATH
];
320 if (context
!= NULL
) {
321 SetFilePointer (context
-> h
, 0, NULL
, FILE_BEGIN
);
322 while (WCMD_fgets (string
, sizeof(string
), context
-> h
)) {
323 if ((string
[0] == ':') && (strcmp (&string
[1], param1
) == 0)) return;
325 WCMD_output ("Target to GOTO not found\n");
331 /****************************************************************************
334 * Batch file conditional.
335 * FIXME: Much more syntax checking needed!
338 void WCMD_if (char *p
) {
341 int negate
= 0, test
= 0;
342 char condition
[MAX_PATH
], *command
, *s
;
344 if (!lstrcmpi (param1
, "not")) {
346 lstrcpy (condition
, param2
);
349 lstrcpy (condition
, param1
);
351 if (!lstrcmpi (condition
, "errorlevel")) {
352 if (errorlevel
>= atoi(WCMD_parameter (p
, 1+negate
, NULL
))) test
= 1;
355 else if (!lstrcmpi (condition
, "exist")) {
356 if ((h
= CreateFile (WCMD_parameter (p
, 1+negate
, NULL
), GENERIC_READ
, 0, NULL
,
357 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
)) != INVALID_HANDLE_VALUE
) {
362 else if ((s
= strstr (p
, "=="))) {
364 if (!lstrcmpi (condition
, WCMD_parameter (s
, 0, NULL
))) test
= 1;
367 WCMD_output ("Syntax error\n");
370 if (test
!= negate
) {
371 WCMD_parameter (p
, 2+negate
, &s
);
372 command
= strdup (s
);
373 WCMD_process_command (command
);
378 /****************************************************************************
381 * Move a file, directory tree or wildcarded set of files.
382 * FIXME: Needs input and output files to be fully specified.
389 if ((strchr(param1
,'*') != NULL
) || (strchr(param1
,'%') != NULL
)) {
390 WCMD_output ("Wildcards not yet supported\n");
393 status
= MoveFile (param1
, param2
);
394 if (!status
) WCMD_print_error ();
397 /****************************************************************************
400 * Wait for keyboard input.
408 WCMD_output (anykey
);
409 ReadFile (GetStdHandle(STD_INPUT_HANDLE
), string
, sizeof(string
), &count
, NULL
);
412 /****************************************************************************
415 * Delete a directory.
418 void WCMD_remove_dir () {
420 if (!RemoveDirectory (param1
)) WCMD_print_error ();
423 /****************************************************************************
427 * FIXME: Needs input and output files to be fully specified.
430 void WCMD_rename () {
434 if ((strchr(param1
,'*') != NULL
) || (strchr(param1
,'%') != NULL
)) {
435 WCMD_output ("Wildcards not yet supported\n");
438 status
= MoveFile (param1
, param2
);
439 if (!status
) WCMD_print_error ();
442 /*****************************************************************************
443 * WCMD_setshow_attrib
445 * Display and optionally sets DOS attributes on a file or directory
447 * FIXME: Wine currently uses the Unix stat() function to get file attributes.
448 * As a result only the Readonly flag is correctly reported, the Archive bit
449 * is always set and the rest are not implemented. We do the Right Thing anyway.
451 * FIXME: No SET functionality.
455 void WCMD_setshow_attrib () {
460 char flags
[9] = {" "};
462 if (param1
[0] == '-') {
467 if (lstrlen(param1
) == 0) {
468 GetCurrentDirectory (sizeof(param1
), param1
);
469 strcat (param1
, "\\*");
472 hff
= FindFirstFile (param1
, &fd
);
473 if (hff
== INVALID_HANDLE_VALUE
) {
474 WCMD_output ("File Not Found\n");
478 if (!(fd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)) {
479 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_HIDDEN
) {
482 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_SYSTEM
) {
485 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_ARCHIVE
) {
488 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) {
491 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_TEMPORARY
) {
494 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_COMPRESSED
) {
497 WCMD_output ("%s %s\n", flags
, fd
.cFileName
);
498 for (count
=0; count
< 8; count
++) flags
[count
] = ' ';
500 } while (FindNextFile(hff
, &fd
) != 0);
505 /*****************************************************************************
506 * WCMD_setshow_default
508 * Set/Show the current default directory
511 void WCMD_setshow_default () {
516 if (strlen(param1
) == 0) {
517 GetCurrentDirectory (sizeof(string
), string
);
518 strcat (string
, "\n");
519 WCMD_output (string
);
522 status
= SetCurrentDirectory (param1
);
531 /****************************************************************************
534 * Set/Show the system date
535 * FIXME: Can't change date yet
538 void WCMD_setshow_date () {
540 char curdate
[64], buffer
[64];
543 if (lstrlen(param1
) == 0) {
544 if (GetDateFormat (LOCALE_USER_DEFAULT
, 0, NULL
, NULL
,
545 curdate
, sizeof(curdate
))) {
546 WCMD_output ("Current Date is %s\nEnter new date: ", curdate
);
547 ReadFile (GetStdHandle(STD_INPUT_HANDLE
), buffer
, sizeof(buffer
), &count
, NULL
);
552 else WCMD_print_error ();
559 /****************************************************************************
562 * Set/Show the environment variables
564 * FIXME: need to sort variables into order for display?
567 void WCMD_setshow_env (char *s
) {
574 if (strlen(param1
) == 0) {
575 env
= GetEnvironmentStrings ();
578 WCMD_output ("%s\n", p
);
586 /* FIXME: Emulate Win98 for now, ie "SET C" looks ONLY for an
587 environment variable C, whereas on NT it shows ALL variables
590 status
= GetEnvironmentVariable(s
, buffer
, sizeof(buffer
));
592 WCMD_output("%s=%s\n", s
, buffer
);
594 WCMD_output ("Environment variable %s not defined\n", s
);
600 if (strlen(p
) == 0) p
= 0x00;
601 status
= SetEnvironmentVariable (s
, p
);
602 if (!status
) WCMD_print_error();
604 /* WCMD_output (newline); @JED*/
607 /****************************************************************************
610 * Set/Show the path environment variable
613 void WCMD_setshow_path () {
618 if (strlen(param1
) == 0) {
619 status
= GetEnvironmentVariable ("PATH", string
, sizeof(string
));
621 WCMD_output ("PATH=%s\n", string
);
624 WCMD_output ("PATH not found\n");
628 status
= SetEnvironmentVariable ("PATH", param1
);
629 if (!status
) WCMD_print_error();
633 /****************************************************************************
634 * WCMD_setshow_prompt
636 * Set or show the command prompt.
639 void WCMD_setshow_prompt () {
643 if (strlen(param1
) == 0) {
644 SetEnvironmentVariable ("PROMPT", NULL
);
648 while ((*s
== '=') || (*s
== ' ')) s
++;
649 if (strlen(s
) == 0) {
650 SetEnvironmentVariable ("PROMPT", NULL
);
652 else SetEnvironmentVariable ("PROMPT", s
);
656 /****************************************************************************
659 * Set/Show the system time
660 * FIXME: Can't change time yet
663 void WCMD_setshow_time () {
665 char curtime
[64], buffer
[64];
669 if (strlen(param1
) == 0) {
671 if (GetTimeFormat (LOCALE_USER_DEFAULT
, 0, &st
, NULL
,
672 curtime
, sizeof(curtime
))) {
673 WCMD_output ("Current Time is %s\nEnter new time: ", curtime
);
674 ReadFile (GetStdHandle(STD_INPUT_HANDLE
), buffer
, sizeof(buffer
), &count
, NULL
);
679 else WCMD_print_error ();
686 /****************************************************************************
689 * Shift batch parameters.
694 if (context
!= NULL
) context
-> shift_count
++;
698 /****************************************************************************
701 * Copy a file to standard output.
710 h
= CreateFile (param1
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
,
711 FILE_ATTRIBUTE_NORMAL
, NULL
);
712 if (h
== INVALID_HANDLE_VALUE
) {
716 while (ReadFile (h
, buffer
, sizeof(buffer
), &count
, NULL
)) {
717 if (count
== 0) break; /* ReadFile reports success on EOF! */
718 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE
), buffer
, count
, &count
, NULL
);
723 /****************************************************************************
726 * Display verify flag.
727 * FIXME: We don't actually do anything with the verify flag other than toggle
731 void WCMD_verify (char *command
) {
733 static char *von
= "Verify is ON\n", *voff
= "Verify is OFF\n";
736 count
= strlen(command
);
738 if (verify_mode
) WCMD_output (von
);
739 else WCMD_output (voff
);
742 if (lstrcmpi(command
, "ON") == 0) {
746 else if (lstrcmpi(command
, "OFF") == 0) {
750 else WCMD_output ("Verify must be ON or OFF\n");
753 /****************************************************************************
756 * Display version info.
759 void WCMD_version () {
761 WCMD_output (version_string
);
765 /****************************************************************************
768 * Display volume info and/or set volume label. Returns 0 if error.
771 int WCMD_volume (int mode
, char *path
) {
774 char string
[MAX_PATH
], label
[MAX_PATH
], curdir
[MAX_PATH
];
776 static char syntax
[] = "Syntax Error\n\n";
778 if (lstrlen(path
) == 0) {
779 status
= GetCurrentDirectory (sizeof(curdir
), curdir
);
784 status
= GetVolumeInformation (NULL
, label
, sizeof(label
), &serial
, NULL
,
788 if ((path
[1] != ':') || (lstrlen(path
) != 2)) {
789 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE
), syntax
, strlen(syntax
), &count
, NULL
);
792 wsprintf (curdir
, "%s\\", path
);
793 status
= GetVolumeInformation (curdir
, label
, sizeof(label
), &serial
, NULL
,
800 WCMD_output ("Volume in drive %c is %s\nVolume Serial Number is %04x-%04x\n\n",
801 curdir
[0], label
, HIWORD(serial
), LOWORD(serial
));
803 WCMD_output ("Volume label (11 characters, ENTER for none)?");
804 ReadFile (GetStdHandle(STD_INPUT_HANDLE
), string
, sizeof(string
), &count
, NULL
);
806 string
[count
-1] = '\0'; /* ReadFile output is not null-terminated! */
807 if (string
[count
-2] == '\r') string
[count
-2] = '\0'; /* Under Windoze we get CRLF! */
809 if (lstrlen(path
) != 0) {
810 if (!SetVolumeLabel (curdir
, string
)) WCMD_print_error ();
813 if (!SetVolumeLabel (NULL
, string
)) WCMD_print_error ();