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 * - 32-bit limit on file sizes in DIR command
15 * - Lots of functionality missing from builtins
16 * - Messages etc need international support
21 void WCMD_execute (char *orig_command
, char *parameter
, char *substitution
);
23 extern HINSTANCE hinst
;
24 extern char *inbuilt
[];
26 extern char newline
[];
27 extern char version_string
[];
29 extern int echo_mode
, verify_mode
;
30 extern char quals
[MAX_PATH
], param1
[MAX_PATH
], param2
[MAX_PATH
];
31 extern BATCH_CONTEXT
*context
;
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
64 * FIXME: Needs output file to be fully specified (can't just enter directory)
73 static char *overwrite
= "Overwrite file (Y/N)?";
74 char string
[8], outpath
[MAX_PATH
];
76 if ((strchr(param1
,'*') != NULL
) && (strchr(param1
,'%') != NULL
)) {
77 WCMD_output ("Wildcards not yet supported\n");
80 GetFullPathName (param2
, sizeof(outpath
), outpath
, NULL
);
81 force
= (strstr (quals
, "/Y") != NULL
);
83 hff
= FindFirstFile (outpath
, &fd
);
84 if (hff
!= INVALID_HANDLE_VALUE
) {
86 WCMD_output (overwrite
);
87 ReadFile (GetStdHandle(STD_INPUT_HANDLE
), string
, sizeof(string
), &count
, NULL
);
88 if (toupper(string
[0]) == 'Y') force
= TRUE
;
93 status
= CopyFile (param1
, outpath
, FALSE
);
94 if (!status
) WCMD_print_error ();
98 /****************************************************************************
101 * Create a directory.
104 void WCMD_create_dir () {
106 if (!CreateDirectory (param1
, NULL
)) WCMD_print_error ();
109 /****************************************************************************
112 * Delete a file or wildcarded set.
116 void WCMD_delete (int recurse
) {
120 char fpath
[MAX_PATH
];
123 hff
= FindFirstFile (param1
, &fd
);
124 if (hff
== INVALID_HANDLE_VALUE
) {
125 WCMD_output ("File Not Found\n");
128 if ((strchr(param1
,'*') == NULL
) && (strchr(param1
,'?') == NULL
)
129 && (!recurse
) && (fd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)) {
130 strcat (param1
, "\\*");
134 if ((strchr(param1
,'*') != NULL
) || (strchr(param1
,'?') != NULL
)) {
135 strcpy (fpath
, param1
);
137 p
= strrchr (fpath
, '\\');
140 strcat (fpath
, fd
.cFileName
);
142 else strcpy (fpath
, fd
.cFileName
);
143 if (!(fd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)) {
144 if (!DeleteFile (fpath
)) WCMD_print_error ();
146 } while (FindNextFile(hff
, &fd
) != 0);
150 if (!DeleteFile (param1
)) WCMD_print_error ();
154 /****************************************************************************
157 * Echo input to the screen (or not). We don't try to emulate the bugs
158 * in DOS (try typing "ECHO ON AGAIN" for an example).
161 void WCMD_echo (char *command
) {
163 static char *eon
= "Echo is ON\n", *eoff
= "Echo is OFF\n";
166 count
= strlen(command
);
168 if (echo_mode
) WCMD_output (eon
);
169 else WCMD_output (eoff
);
172 if ((count
== 1) && (command
[0] == '.')) {
173 WCMD_output (newline
);
176 if (lstrcmpi(command
, "ON") == 0) {
180 if (lstrcmpi(command
, "OFF") == 0) {
184 WCMD_output (command
);
185 WCMD_output (newline
);
189 /**************************************************************************
192 * Batch file loop processing.
193 * FIXME: We don't exhaustively check syntax. Any command which works in MessDOS
194 * will probably work here, but the reverse is not necessarily the case...
197 void WCMD_for (char *p
) {
202 char set
[MAX_PATH
], param
[MAX_PATH
];
205 if (lstrcmpi (WCMD_parameter (p
, 1, NULL
), "in")
206 || lstrcmpi (WCMD_parameter (p
, 3, NULL
), "do")
207 || (param1
[0] != '%')) {
208 WCMD_output ("Syntax error\n");
211 lstrcpyn (set
, WCMD_parameter (p
, 2, NULL
), sizeof(set
));
212 WCMD_parameter (p
, 4, &cmd
);
213 lstrcpy (param
, param1
);
216 * If the parameter within the set has a wildcard then search for matching files
217 * otherwise do a literal substitution.
221 while (*(item
= WCMD_parameter (set
, i
, NULL
))) {
222 if (strpbrk (item
, "*?")) {
223 hff
= FindFirstFile (item
, &fd
);
224 if (hff
== INVALID_HANDLE_VALUE
) {
228 WCMD_execute (cmd
, param
, fd
.cFileName
);
229 } while (FindNextFile(hff
, &fd
) != 0);
233 WCMD_execute (cmd
, param
, item
);
240 * Execute a command after substituting variable text for the supplied parameter
243 void WCMD_execute (char *orig_cmd
, char *param
, char *subst
) {
245 char *new_cmd
, *p
, *s
, *dup
;
248 size
= lstrlen (orig_cmd
);
249 new_cmd
= (char *) LocalAlloc (LMEM_FIXED
| LMEM_ZEROINIT
, size
);
250 dup
= s
= strdup (orig_cmd
);
252 while ((p
= strstr (s
, param
))) {
254 size
+= lstrlen (subst
);
255 new_cmd
= (char *) LocalReAlloc ((HANDLE
)new_cmd
, size
, 0);
257 strcat (new_cmd
, subst
);
258 s
= p
+ lstrlen (param
);
261 WCMD_process_command (new_cmd
);
263 LocalFree ((HANDLE
)new_cmd
);
267 /**************************************************************************
270 * Simple on-line help. Help text is stored in the resource file.
273 void WCMD_give_help (char *command
) {
278 command
= WCMD_strtrim_leading_spaces(command
);
279 if (lstrlen(command
) == 0) {
280 LoadString (0, 1000, buffer
, sizeof(buffer
));
281 WCMD_output (buffer
);
284 for (i
=0; i
<=WCMD_EXIT
; i
++) {
285 if (CompareString (LOCALE_USER_DEFAULT
, NORM_IGNORECASE
| SORT_STRINGSORT
,
286 param1
, -1, inbuilt
[i
], -1) == 2) {
287 LoadString (0, i
, buffer
, sizeof(buffer
));
288 WCMD_output (buffer
);
292 WCMD_output ("No help available for %s\n", param1
);
297 /****************************************************************************
300 * Batch file jump instruction. Not the most efficient algorithm ;-)
301 * Prints error message if the specified label cannot be found - the file pointer is
302 * then at EOF, effectively stopping the batch file.
303 * FIXME: DOS is supposed to allow labels with spaces - we don't.
308 char string
[MAX_PATH
];
310 if (context
!= NULL
) {
311 SetFilePointer (context
-> h
, 0, NULL
, FILE_BEGIN
);
312 while (WCMD_fgets (string
, sizeof(string
), context
-> h
)) {
313 if ((string
[0] == ':') && (strcmp (&string
[1], param1
) == 0)) return;
315 WCMD_output ("Target to GOTO not found\n");
321 /****************************************************************************
324 * Batch file conditional.
325 * FIXME: The "errorlevel" version is not supported.
326 * FIXME: Much more syntax checking needed!
329 void WCMD_if (char *p
) {
332 int negate
= 0, test
= 0;
333 char condition
[MAX_PATH
], *command
, *s
;
335 if (!lstrcmpi (param1
, "not")) {
337 lstrcpy (condition
, param2
);
340 lstrcpy (condition
, param1
);
342 if (!lstrcmpi (condition
, "errorlevel")) {
346 else if (!lstrcmpi (condition
, "exist")) {
347 if ((h
= CreateFile (WCMD_parameter (p
, 1+negate
, NULL
), GENERIC_READ
, 0, NULL
,
348 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0)) != INVALID_HANDLE_VALUE
) {
353 else if ((s
= strstr (p
, "=="))) {
355 if (!lstrcmpi (condition
, WCMD_parameter (s
, 0, NULL
))) test
= 1;
358 WCMD_output ("Syntax error\n");
361 if (test
!= negate
) {
362 WCMD_parameter (p
, 2+negate
, &s
);
363 command
= strdup (s
);
364 WCMD_process_command (command
);
369 /****************************************************************************
372 * Move a file, directory tree or wildcarded set of files.
373 * FIXME: Needs input and output files to be fully specified.
380 if ((strchr(param1
,'*') != NULL
) || (strchr(param1
,'%') != NULL
)) {
381 WCMD_output ("Wildcards not yet supported\n");
384 status
= MoveFile (param1
, param2
);
385 if (!status
) WCMD_print_error ();
388 /****************************************************************************
391 * Wait for keyboard input.
399 WCMD_output (anykey
);
400 ReadFile (GetStdHandle(STD_INPUT_HANDLE
), string
, sizeof(string
), &count
, NULL
);
403 /****************************************************************************
406 * Delete a directory.
409 void WCMD_remove_dir () {
411 if (!RemoveDirectory (param1
)) WCMD_print_error ();
414 /****************************************************************************
418 * FIXME: Needs input and output files to be fully specified.
421 void WCMD_rename () {
424 static char *dirmsg
= "Input file is a directory. Use the MOVE command\n\n";
426 if ((strchr(param1
,'*') != NULL
) || (strchr(param1
,'%') != NULL
)) {
427 WCMD_output ("Wildcards not yet supported\n");
430 status
= GetFileAttributes (param1
);
431 if ((status
!= -1) && (status
& FILE_ATTRIBUTE_DIRECTORY
)) {
432 WCMD_output (dirmsg
);
435 status
= MoveFile (param1
, param2
);
436 if (!status
) WCMD_print_error ();
439 /*****************************************************************************
440 * WCMD_setshow_attrib
442 * Display and optionally sets DOS attributes on a file or directory
444 * FIXME: Wine currently uses the Unix stat() function to get file attributes.
445 * As a result only the Readonly flag is correctly reported, the Archive bit
446 * is always set and the rest are not implemented. We do the Right Thing anyway.
448 * FIXME: No SET functionality.
452 void WCMD_setshow_attrib () {
457 char flags
[9] = {" "};
459 if (param1
[0] == '-') {
464 if (lstrlen(param1
) == 0) {
465 GetCurrentDirectory (sizeof(param1
), param1
);
466 strcat (param1
, "\\*");
469 hff
= FindFirstFile (param1
, &fd
);
470 if (hff
== INVALID_HANDLE_VALUE
) {
471 WCMD_output ("File Not Found\n");
475 if (!(fd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)) {
476 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_HIDDEN
) {
479 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_SYSTEM
) {
482 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_ARCHIVE
) {
485 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) {
488 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_TEMPORARY
) {
491 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_COMPRESSED
) {
494 WCMD_output ("%s %s\n", flags
, fd
.cFileName
);
495 for (count
=0; count
< 8; count
++) flags
[count
] = ' ';
497 } while (FindNextFile(hff
, &fd
) != 0);
502 /*****************************************************************************
503 * WCMD_setshow_default
505 * Set/Show the current default directory
508 void WCMD_setshow_default () {
513 if (strlen(param1
) == 0) {
514 GetCurrentDirectory (sizeof(string
), string
);
515 strcat (string
, "\n");
516 WCMD_output (string
);
519 status
= SetCurrentDirectory (param1
);
528 /****************************************************************************
531 * Set/Show the system date
532 * FIXME: Can't change date yet
535 void WCMD_setshow_date () {
537 char curdate
[64], buffer
[64];
540 if (lstrlen(param1
) == 0) {
541 if (GetDateFormat (LOCALE_USER_DEFAULT
, 0, NULL
, NULL
,
542 curdate
, sizeof(curdate
))) {
543 WCMD_output ("Current Date is %s\nEnter new date: ", curdate
);
544 ReadFile (GetStdHandle(STD_INPUT_HANDLE
), buffer
, sizeof(buffer
), &count
, NULL
);
549 else WCMD_print_error ();
556 /****************************************************************************
559 * Set/Show the environment variables
561 * FIXME: need to sort variables into order for display?
564 void WCMD_setshow_env (char *s
) {
570 if (strlen(param1
) == 0) {
571 env
= GetEnvironmentStrings ();
574 WCMD_output ("%s\n", p
);
581 WCMD_output ("Command Syntax: SET variable=value\n");
585 status
= SetEnvironmentVariable (s
, p
);
586 if (!status
) WCMD_print_error();
588 WCMD_output (newline
);
591 /****************************************************************************
594 * Set/Show the path environment variable
597 void WCMD_setshow_path () {
602 if (strlen(param1
) == 0) {
603 status
= GetEnvironmentVariable ("PATH", string
, sizeof(string
));
605 WCMD_output ("PATH=%s\n", string
);
608 WCMD_output ("PATH not found\n");
612 status
= SetEnvironmentVariable ("PATH", param1
);
613 if (!status
) WCMD_print_error();
617 /****************************************************************************
618 * WCMD_setshow_prompt
620 * Set or show the command prompt.
623 void WCMD_setshow_prompt () {
627 if (strlen(param1
) == 0) {
628 SetEnvironmentVariable ("PROMPT", NULL
);
632 while ((*s
== '=') || (*s
== ' ')) s
++;
633 if (strlen(s
) == 0) {
634 SetEnvironmentVariable ("PROMPT", NULL
);
636 else SetEnvironmentVariable ("PROMPT", s
);
640 /****************************************************************************
643 * Set/Show the system time
644 * FIXME: Can't change time yet
647 void WCMD_setshow_time () {
649 char curtime
[64], buffer
[64];
653 if (strlen(param1
) == 0) {
655 if (GetTimeFormat (LOCALE_USER_DEFAULT
, 0, &st
, NULL
,
656 curtime
, sizeof(curtime
))) {
657 WCMD_output ("Current Time is %s\nEnter new time: ", curtime
);
658 ReadFile (GetStdHandle(STD_INPUT_HANDLE
), buffer
, sizeof(buffer
), &count
, NULL
);
663 else WCMD_print_error ();
670 /****************************************************************************
673 * Shift batch parameters.
678 if (context
!= NULL
) context
-> shift_count
++;
682 /****************************************************************************
685 * Copy a file to standard output.
694 h
= CreateFile (param1
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
,
695 FILE_ATTRIBUTE_NORMAL
, 0);
696 if (h
== INVALID_HANDLE_VALUE
) {
700 while (ReadFile (h
, buffer
, sizeof(buffer
), &count
, NULL
)) {
701 if (count
== 0) break; /* ReadFile reports success on EOF! */
702 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE
), buffer
, count
, &count
, NULL
);
707 /****************************************************************************
710 * Display verify flag.
711 * FIXME: We don't actually do anything with the verify flag other than toggle
715 void WCMD_verify (char *command
) {
717 static char *von
= "Verify is ON\n", *voff
= "Verify is OFF\n";
720 count
= strlen(command
);
722 if (verify_mode
) WCMD_output (von
);
723 else WCMD_output (voff
);
726 if (lstrcmpi(command
, "ON") == 0) {
730 else if (lstrcmpi(command
, "OFF") == 0) {
734 else WCMD_output ("Verify must be ON or OFF\n");
737 /****************************************************************************
740 * Display version info.
743 void WCMD_version () {
745 WCMD_output (version_string
);
749 /****************************************************************************
752 * Display volume info and/or set volume label. Returns 0 if error.
755 int WCMD_volume (int mode
, char *path
) {
758 char string
[MAX_PATH
], label
[MAX_PATH
], curdir
[MAX_PATH
];
760 static char syntax
[] = "Syntax Error\n\n";
762 if (lstrlen(path
) == 0) {
763 status
= GetCurrentDirectory (sizeof(curdir
), curdir
);
768 status
= GetVolumeInformation (NULL
, label
, sizeof(label
), &serial
, NULL
,
772 if ((path
[1] != ':') || (lstrlen(path
) != 2)) {
773 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE
), syntax
, strlen(syntax
), &count
, NULL
);
776 wsprintf (curdir
, "%s\\", path
);
777 status
= GetVolumeInformation (curdir
, label
, sizeof(label
), &serial
, NULL
,
784 WCMD_output ("Volume in drive %c is %s\nVolume Serial Number is %04x-%04x\n\n",
785 curdir
[0], label
, HIWORD(serial
), LOWORD(serial
));
787 WCMD_output ("Volume label (11 characters, ENTER for none)?");
788 ReadFile (GetStdHandle(STD_INPUT_HANDLE
), string
, sizeof(string
), &count
, NULL
);
790 string
[count
-1] = '\0'; /* ReadFile output is not null-terminated! */
791 if (string
[count
-2] == '\r') string
[count
-2] = '\0'; /* Under Windoze we get CRLF! */
793 if (lstrlen(path
) != 0) {
794 if (!SetVolumeLabel (curdir
, string
)) WCMD_print_error ();
797 if (!SetVolumeLabel (NULL
, string
)) WCMD_print_error ();