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;
32 extern DWORD errorlevel;
36 /****************************************************************************
39 * Clear the terminal screen.
42 void WCMD_clear_screen () {
48 /****************************************************************************
51 * Change the default i/o device (ie redirect STDin/STDout).
54 void WCMD_change_tty () {
60 /****************************************************************************
63 * Copy a file or wildcarded set.
64 * FIXME: No wildcard support
73 static char *overwrite = "Overwrite file (Y/N)?";
74 char string[8], outpath[MAX_PATH], inpath[MAX_PATH], *infile;
76 if ((strchr(param1,'*') != NULL) && (strchr(param1,'%') != NULL)) {
77 WCMD_output ("Wildcards not yet supported\n");
80 GetFullPathName (param2, sizeof(outpath), outpath, NULL);
81 hff = FindFirstFile (outpath, &fd);
82 if (hff != INVALID_HANDLE_VALUE) {
83 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
84 GetFullPathName (param1, sizeof(inpath), inpath, &infile);
85 strcat (outpath, "\\");
86 strcat (outpath, infile);
90 force = (strstr (quals, "/Y") != NULL);
92 hff = FindFirstFile (outpath, &fd);
93 if (hff != INVALID_HANDLE_VALUE) {
95 WCMD_output (overwrite);
96 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
97 if (toupper(string[0]) == 'Y') force = TRUE;
102 status = CopyFile (param1, outpath, FALSE);
103 if (!status) WCMD_print_error ();
107 /****************************************************************************
110 * Create a directory.
113 void WCMD_create_dir () {
115 if (!CreateDirectory (param1, NULL)) WCMD_print_error ();
118 /****************************************************************************
121 * Delete a file or wildcarded set.
125 void WCMD_delete (int recurse) {
129 char fpath[MAX_PATH];
132 hff = FindFirstFile (param1, &fd);
133 if (hff == INVALID_HANDLE_VALUE) {
134 WCMD_output ("File Not Found\n");
137 if ((strchr(param1,'*') == NULL) && (strchr(param1,'?') == NULL)
138 && (!recurse) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
139 strcat (param1, "\\*");
143 if ((strchr(param1,'*') != NULL) || (strchr(param1,'?') != NULL)) {
144 strcpy (fpath, param1);
146 p = strrchr (fpath, '\\');
149 strcat (fpath, fd.cFileName);
151 else strcpy (fpath, fd.cFileName);
152 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
153 if (!DeleteFile (fpath)) WCMD_print_error ();
155 } while (FindNextFile(hff, &fd) != 0);
159 if (!DeleteFile (param1)) WCMD_print_error ();
163 /****************************************************************************
166 * Echo input to the screen (or not). We don't try to emulate the bugs
167 * in DOS (try typing "ECHO ON AGAIN" for an example).
170 void WCMD_echo (char *command) {
172 static char *eon = "Echo is ON\n", *eoff = "Echo is OFF\n";
175 count = strlen(command);
177 if (echo_mode) WCMD_output (eon);
178 else WCMD_output (eoff);
181 if ((count == 1) && (command[0] == '.')) {
182 WCMD_output (newline);
185 if (lstrcmpi(command, "ON") == 0) {
189 if (lstrcmpi(command, "OFF") == 0) {
193 WCMD_output_asis (command);
194 WCMD_output (newline);
198 /**************************************************************************
201 * Batch file loop processing.
202 * FIXME: We don't exhaustively check syntax. Any command which works in MessDOS
203 * will probably work here, but the reverse is not necessarily the case...
206 void WCMD_for (char *p) {
211 char set[MAX_PATH], param[MAX_PATH];
214 if (lstrcmpi (WCMD_parameter (p, 1, NULL), "in")
215 || lstrcmpi (WCMD_parameter (p, 3, NULL), "do")
216 || (param1[0] != '%')) {
217 WCMD_output ("Syntax error\n");
220 lstrcpyn (set, WCMD_parameter (p, 2, NULL), sizeof(set));
221 WCMD_parameter (p, 4, &cmd);
222 lstrcpy (param, param1);
225 * If the parameter within the set has a wildcard then search for matching files
226 * otherwise do a literal substitution.
230 while (*(item = WCMD_parameter (set, i, NULL))) {
231 if (strpbrk (item, "*?")) {
232 hff = FindFirstFile (item, &fd);
233 if (hff == INVALID_HANDLE_VALUE) {
237 WCMD_execute (cmd, param, fd.cFileName);
238 } while (FindNextFile(hff, &fd) != 0);
242 WCMD_execute (cmd, param, item);
248 /*****************************************************************************
251 * Execute a command after substituting variable text for the supplied parameter
254 void WCMD_execute (char *orig_cmd, char *param, char *subst) {
256 char *new_cmd, *p, *s, *dup;
259 size = lstrlen (orig_cmd);
260 new_cmd = (char *) LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, size);
261 dup = s = strdup (orig_cmd);
263 while ((p = strstr (s, param))) {
265 size += lstrlen (subst);
266 new_cmd = (char *) LocalReAlloc ((HANDLE)new_cmd, size, 0);
268 strcat (new_cmd, subst);
269 s = p + lstrlen (param);
272 WCMD_process_command (new_cmd);
274 LocalFree ((HANDLE)new_cmd);
278 /**************************************************************************
281 * Simple on-line help. Help text is stored in the resource file.
284 void WCMD_give_help (char *command) {
289 command = WCMD_strtrim_leading_spaces(command);
290 if (lstrlen(command) == 0) {
291 LoadString (0, 1000, buffer, sizeof(buffer));
292 WCMD_output (buffer);
295 for (i=0; i<=WCMD_EXIT; i++) {
296 if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
297 param1, -1, inbuilt[i], -1) == 2) {
298 LoadString (hinst, i, buffer, sizeof(buffer));
299 WCMD_output (buffer);
303 WCMD_output ("No help available for %s\n", param1);
308 /****************************************************************************
311 * Batch file jump instruction. Not the most efficient algorithm ;-)
312 * Prints error message if the specified label cannot be found - the file pointer is
313 * then at EOF, effectively stopping the batch file.
314 * FIXME: DOS is supposed to allow labels with spaces - we don't.
319 char string[MAX_PATH];
321 if (context != NULL) {
322 SetFilePointer (context -> h, 0, NULL, FILE_BEGIN);
323 while (WCMD_fgets (string, sizeof(string), context -> h)) {
324 if ((string[0] == ':') && (strcmp (&string[1], param1) == 0)) return;
326 WCMD_output ("Target to GOTO not found\n");
332 /****************************************************************************
335 * Batch file conditional.
336 * FIXME: Much more syntax checking needed!
339 void WCMD_if (char *p) {
342 int negate = 0, test = 0;
343 char condition[MAX_PATH], *command, *s;
345 if (!lstrcmpi (param1, "not")) {
347 lstrcpy (condition, param2);
350 lstrcpy (condition, param1);
352 if (!lstrcmpi (condition, "errorlevel")) {
353 if (errorlevel >= atoi(WCMD_parameter (p, 1+negate, NULL))) test = 1;
356 else if (!lstrcmpi (condition, "exist")) {
357 if ((h = CreateFile (WCMD_parameter (p, 1+negate, NULL), GENERIC_READ, 0, NULL,
358 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)) != INVALID_HANDLE_VALUE) {
363 else if ((s = strstr (p, "=="))) {
365 if (!lstrcmpi (condition, WCMD_parameter (s, 0, NULL))) test = 1;
368 WCMD_output ("Syntax error\n");
371 if (test != negate) {
372 WCMD_parameter (p, 2+negate, &s);
373 command = strdup (s);
374 WCMD_process_command (command);
379 /****************************************************************************
382 * Move a file, directory tree or wildcarded set of files.
383 * FIXME: Needs input and output files to be fully specified.
390 if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
391 WCMD_output ("Wildcards not yet supported\n");
394 status = MoveFile (param1, param2);
395 if (!status) WCMD_print_error ();
398 /****************************************************************************
401 * Wait for keyboard input.
409 WCMD_output (anykey);
410 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
413 /****************************************************************************
416 * Delete a directory.
419 void WCMD_remove_dir () {
421 if (!RemoveDirectory (param1)) WCMD_print_error ();
424 /****************************************************************************
428 * FIXME: Needs input and output files to be fully specified.
431 void WCMD_rename () {
435 if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
436 WCMD_output ("Wildcards not yet supported\n");
439 status = MoveFile (param1, param2);
440 if (!status) WCMD_print_error ();
443 /*****************************************************************************
444 * WCMD_setshow_attrib
446 * Display and optionally sets DOS attributes on a file or directory
448 * FIXME: Wine currently uses the Unix stat() function to get file attributes.
449 * As a result only the Readonly flag is correctly reported, the Archive bit
450 * is always set and the rest are not implemented. We do the Right Thing anyway.
452 * FIXME: No SET functionality.
456 void WCMD_setshow_attrib () {
461 char flags[9] = {" "};
463 if (param1[0] == '-') {
468 if (lstrlen(param1) == 0) {
469 GetCurrentDirectory (sizeof(param1), param1);
470 strcat (param1, "\\*");
473 hff = FindFirstFile (param1, &fd);
474 if (hff == INVALID_HANDLE_VALUE) {
475 WCMD_output ("File Not Found\n");
479 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
480 if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
483 if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
486 if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
489 if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
492 if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) {
495 if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) {
498 WCMD_output ("%s %s\n", flags, fd.cFileName);
499 for (count=0; count < 8; count++) flags[count] = ' ';
501 } while (FindNextFile(hff, &fd) != 0);
506 /*****************************************************************************
507 * WCMD_setshow_default
509 * Set/Show the current default directory
512 void WCMD_setshow_default () {
517 if (strlen(param1) == 0) {
518 GetCurrentDirectory (sizeof(string), string);
519 strcat (string, "\n");
520 WCMD_output (string);
523 status = SetCurrentDirectory (param1);
532 /****************************************************************************
535 * Set/Show the system date
536 * FIXME: Can't change date yet
539 void WCMD_setshow_date () {
541 char curdate[64], buffer[64];
544 if (lstrlen(param1) == 0) {
545 if (GetDateFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL,
546 curdate, sizeof(curdate))) {
547 WCMD_output ("Current Date is %s\nEnter new date: ", curdate);
548 ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
553 else WCMD_print_error ();
560 /****************************************************************************
563 * Set/Show the environment variables
565 * FIXME: need to sort variables into order for display?
568 void WCMD_setshow_env (char *s) {
575 if (strlen(param1) == 0) {
576 env = GetEnvironmentStrings ();
579 WCMD_output ("%s\n", p);
587 /* FIXME: Emulate Win98 for now, ie "SET C" looks ONLY for an
588 environment variable C, whereas on NT it shows ALL variables
591 status = GetEnvironmentVariable(s, buffer, sizeof(buffer));
593 WCMD_output("%s=%s\n", s, buffer);
595 WCMD_output ("Environment variable %s not defined\n", s);
601 if (strlen(p) == 0) p = 0x00;
602 status = SetEnvironmentVariable (s, p);
603 if (!status) WCMD_print_error();
605 /* WCMD_output (newline); @JED*/
608 /****************************************************************************
611 * Set/Show the path environment variable
614 void WCMD_setshow_path () {
619 if (strlen(param1) == 0) {
620 status = GetEnvironmentVariable ("PATH", string, sizeof(string));
622 WCMD_output ("PATH=%s\n", string);
625 WCMD_output ("PATH not found\n");
629 status = SetEnvironmentVariable ("PATH", param1);
630 if (!status) WCMD_print_error();
634 /****************************************************************************
635 * WCMD_setshow_prompt
637 * Set or show the command prompt.
640 void WCMD_setshow_prompt () {
644 if (strlen(param1) == 0) {
645 SetEnvironmentVariable ("PROMPT", NULL);
649 while ((*s == '=') || (*s == ' ')) s++;
650 if (strlen(s) == 0) {
651 SetEnvironmentVariable ("PROMPT", NULL);
653 else SetEnvironmentVariable ("PROMPT", s);
657 /****************************************************************************
660 * Set/Show the system time
661 * FIXME: Can't change time yet
664 void WCMD_setshow_time () {
666 char curtime[64], buffer[64];
670 if (strlen(param1) == 0) {
672 if (GetTimeFormat (LOCALE_USER_DEFAULT, 0, &st, NULL,
673 curtime, sizeof(curtime))) {
674 WCMD_output ("Current Time is %s\nEnter new time: ", curtime);
675 ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
680 else WCMD_print_error ();
687 /****************************************************************************
690 * Shift batch parameters.
695 if (context != NULL) context -> shift_count++;
699 /****************************************************************************
702 * Copy a file to standard output.
711 h = CreateFile (param1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
712 FILE_ATTRIBUTE_NORMAL, 0);
713 if (h == INVALID_HANDLE_VALUE) {
717 while (ReadFile (h, buffer, sizeof(buffer), &count, NULL)) {
718 if (count == 0) break; /* ReadFile reports success on EOF! */
719 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), buffer, count, &count, NULL);
724 /****************************************************************************
727 * Display verify flag.
728 * FIXME: We don't actually do anything with the verify flag other than toggle
732 void WCMD_verify (char *command) {
734 static char *von = "Verify is ON\n", *voff = "Verify is OFF\n";
737 count = strlen(command);
739 if (verify_mode) WCMD_output (von);
740 else WCMD_output (voff);
743 if (lstrcmpi(command, "ON") == 0) {
747 else if (lstrcmpi(command, "OFF") == 0) {
751 else WCMD_output ("Verify must be ON or OFF\n");
754 /****************************************************************************
757 * Display version info.
760 void WCMD_version () {
762 WCMD_output (version_string);
766 /****************************************************************************
769 * Display volume info and/or set volume label. Returns 0 if error.
772 int WCMD_volume (int mode, char *path) {
775 char string[MAX_PATH], label[MAX_PATH], curdir[MAX_PATH];
777 static char syntax[] = "Syntax Error\n\n";
779 if (lstrlen(path) == 0) {
780 status = GetCurrentDirectory (sizeof(curdir), curdir);
785 status = GetVolumeInformation (NULL, label, sizeof(label), &serial, NULL,
789 if ((path[1] != ':') || (lstrlen(path) != 2)) {
790 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), syntax, strlen(syntax), &count, NULL);
793 wsprintf (curdir, "%s\\", path);
794 status = GetVolumeInformation (curdir, label, sizeof(label), &serial, NULL,
801 WCMD_output ("Volume in drive %c is %s\nVolume Serial Number is %04x-%04x\n\n",
802 curdir[0], label, HIWORD(serial), LOWORD(serial));
804 WCMD_output ("Volume label (11 characters, ENTER for none)?");
805 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
807 string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
808 if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
810 if (lstrlen(path) != 0) {
811 if (!SetVolumeLabel (curdir, string)) WCMD_print_error ();
814 if (!SetVolumeLabel (NULL, string)) WCMD_print_error ();