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 () {
64 /****************************************************************************
67 * Change the default i/o device (ie redirect STDin/STDout).
70 void WCMD_change_tty () {
76 /****************************************************************************
79 * Copy a file or wildcarded set.
80 * FIXME: No wildcard support
89 static char *overwrite = "Overwrite file (Y/N)?";
90 char string[8], outpath[MAX_PATH], inpath[MAX_PATH], *infile;
92 if ((strchr(param1,'*') != NULL) && (strchr(param1,'%') != NULL)) {
93 WCMD_output ("Wildcards not yet supported\n");
96 GetFullPathName (param2, sizeof(outpath), outpath, NULL);
97 hff = FindFirstFile (outpath, &fd);
98 if (hff != INVALID_HANDLE_VALUE) {
99 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
100 GetFullPathName (param1, sizeof(inpath), inpath, &infile);
101 strcat (outpath, "\\");
102 strcat (outpath, infile);
106 force = (strstr (quals, "/Y") != NULL);
108 hff = FindFirstFile (outpath, &fd);
109 if (hff != INVALID_HANDLE_VALUE) {
111 WCMD_output (overwrite);
112 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
113 if (toupper(string[0]) == 'Y') force = TRUE;
118 status = CopyFile (param1, outpath, FALSE);
119 if (!status) WCMD_print_error ();
123 /****************************************************************************
126 * Create a directory.
129 void WCMD_create_dir () {
131 if (!CreateDirectory (param1, NULL)) WCMD_print_error ();
134 /****************************************************************************
137 * Delete a file or wildcarded set.
141 void WCMD_delete (int recurse) {
145 char fpath[MAX_PATH];
148 hff = FindFirstFile (param1, &fd);
149 if (hff == INVALID_HANDLE_VALUE) {
150 WCMD_output ("File Not Found\n");
153 if ((strchr(param1,'*') == NULL) && (strchr(param1,'?') == NULL)
154 && (!recurse) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
155 strcat (param1, "\\*");
159 if ((strchr(param1,'*') != NULL) || (strchr(param1,'?') != NULL)) {
160 strcpy (fpath, param1);
162 p = strrchr (fpath, '\\');
165 strcat (fpath, fd.cFileName);
167 else strcpy (fpath, fd.cFileName);
168 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
169 if (!DeleteFile (fpath)) WCMD_print_error ();
171 } while (FindNextFile(hff, &fd) != 0);
175 if (!DeleteFile (param1)) WCMD_print_error ();
179 /****************************************************************************
182 * Echo input to the screen (or not). We don't try to emulate the bugs
183 * in DOS (try typing "ECHO ON AGAIN" for an example).
186 void WCMD_echo (char *command) {
188 static char *eon = "Echo is ON\n", *eoff = "Echo is OFF\n";
191 count = strlen(command);
193 if (echo_mode) WCMD_output (eon);
194 else WCMD_output (eoff);
197 if ((count == 1) && (command[0] == '.')) {
198 WCMD_output (newline);
201 if (lstrcmpi(command, "ON") == 0) {
205 if (lstrcmpi(command, "OFF") == 0) {
209 WCMD_output_asis (command);
210 WCMD_output (newline);
214 /**************************************************************************
217 * Batch file loop processing.
218 * FIXME: We don't exhaustively check syntax. Any command which works in MessDOS
219 * will probably work here, but the reverse is not necessarily the case...
222 void WCMD_for (char *p) {
227 char set[MAX_PATH], param[MAX_PATH];
230 if (lstrcmpi (WCMD_parameter (p, 1, NULL), "in")
231 || lstrcmpi (WCMD_parameter (p, 3, NULL), "do")
232 || (param1[0] != '%')) {
233 WCMD_output ("Syntax error\n");
236 lstrcpyn (set, WCMD_parameter (p, 2, NULL), sizeof(set));
237 WCMD_parameter (p, 4, &cmd);
238 lstrcpy (param, param1);
241 * If the parameter within the set has a wildcard then search for matching files
242 * otherwise do a literal substitution.
246 while (*(item = WCMD_parameter (set, i, NULL))) {
247 if (strpbrk (item, "*?")) {
248 hff = FindFirstFile (item, &fd);
249 if (hff == INVALID_HANDLE_VALUE) {
253 WCMD_execute (cmd, param, fd.cFileName);
254 } while (FindNextFile(hff, &fd) != 0);
258 WCMD_execute (cmd, param, item);
264 /*****************************************************************************
267 * Execute a command after substituting variable text for the supplied parameter
270 void WCMD_execute (char *orig_cmd, char *param, char *subst) {
272 char *new_cmd, *p, *s, *dup;
275 size = lstrlen (orig_cmd);
276 new_cmd = (char *) LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, size);
277 dup = s = strdup (orig_cmd);
279 while ((p = strstr (s, param))) {
281 size += lstrlen (subst);
282 new_cmd = (char *) LocalReAlloc ((HANDLE)new_cmd, size, 0);
284 strcat (new_cmd, subst);
285 s = p + lstrlen (param);
288 WCMD_process_command (new_cmd);
290 LocalFree ((HANDLE)new_cmd);
294 /**************************************************************************
297 * Simple on-line help. Help text is stored in the resource file.
300 void WCMD_give_help (char *command) {
305 command = WCMD_strtrim_leading_spaces(command);
306 if (lstrlen(command) == 0) {
307 LoadString (0, 1000, buffer, sizeof(buffer));
308 WCMD_output (buffer);
311 for (i=0; i<=WCMD_EXIT; i++) {
312 if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
313 param1, -1, inbuilt[i], -1) == 2) {
314 LoadString (hinst, i, buffer, sizeof(buffer));
315 WCMD_output (buffer);
319 WCMD_output ("No help available for %s\n", param1);
324 /****************************************************************************
327 * Batch file jump instruction. Not the most efficient algorithm ;-)
328 * Prints error message if the specified label cannot be found - the file pointer is
329 * then at EOF, effectively stopping the batch file.
330 * FIXME: DOS is supposed to allow labels with spaces - we don't.
335 char string[MAX_PATH];
337 if (context != NULL) {
338 SetFilePointer (context -> h, 0, NULL, FILE_BEGIN);
339 while (WCMD_fgets (string, sizeof(string), context -> h)) {
340 if ((string[0] == ':') && (strcmp (&string[1], param1) == 0)) return;
342 WCMD_output ("Target to GOTO not found\n");
348 /****************************************************************************
351 * Batch file conditional.
352 * FIXME: Much more syntax checking needed!
355 void WCMD_if (char *p) {
358 int negate = 0, test = 0;
359 char condition[MAX_PATH], *command, *s;
361 if (!lstrcmpi (param1, "not")) {
363 lstrcpy (condition, param2);
366 lstrcpy (condition, param1);
368 if (!lstrcmpi (condition, "errorlevel")) {
369 if (errorlevel >= atoi(WCMD_parameter (p, 1+negate, NULL))) test = 1;
372 else if (!lstrcmpi (condition, "exist")) {
373 if ((h = CreateFile (WCMD_parameter (p, 1+negate, NULL), GENERIC_READ, 0, NULL,
374 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE) {
379 else if ((s = strstr (p, "=="))) {
381 if (!lstrcmpi (condition, WCMD_parameter (s, 0, NULL))) test = 1;
384 WCMD_output ("Syntax error\n");
387 if (test != negate) {
388 WCMD_parameter (p, 2+negate, &s);
389 command = strdup (s);
390 WCMD_process_command (command);
395 /****************************************************************************
398 * Move a file, directory tree or wildcarded set of files.
399 * FIXME: Needs input and output files to be fully specified.
406 if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
407 WCMD_output ("Wildcards not yet supported\n");
410 status = MoveFile (param1, param2);
411 if (!status) WCMD_print_error ();
414 /****************************************************************************
417 * Wait for keyboard input.
425 WCMD_output (anykey);
426 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
429 /****************************************************************************
432 * Delete a directory.
435 void WCMD_remove_dir () {
437 if (!RemoveDirectory (param1)) WCMD_print_error ();
440 /****************************************************************************
444 * FIXME: Needs input and output files to be fully specified.
447 void WCMD_rename () {
451 if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
452 WCMD_output ("Wildcards not yet supported\n");
455 status = MoveFile (param1, param2);
456 if (!status) WCMD_print_error ();
459 /*****************************************************************************
460 * WCMD_setshow_attrib
462 * Display and optionally sets DOS attributes on a file or directory
464 * FIXME: Wine currently uses the Unix stat() function to get file attributes.
465 * As a result only the Readonly flag is correctly reported, the Archive bit
466 * is always set and the rest are not implemented. We do the Right Thing anyway.
468 * FIXME: No SET functionality.
472 void WCMD_setshow_attrib () {
477 char flags[9] = {" "};
479 if (param1[0] == '-') {
484 if (lstrlen(param1) == 0) {
485 GetCurrentDirectory (sizeof(param1), param1);
486 strcat (param1, "\\*");
489 hff = FindFirstFile (param1, &fd);
490 if (hff == INVALID_HANDLE_VALUE) {
491 WCMD_output ("File Not Found\n");
495 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
496 if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
499 if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
502 if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
505 if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
508 if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) {
511 if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) {
514 WCMD_output ("%s %s\n", flags, fd.cFileName);
515 for (count=0; count < 8; count++) flags[count] = ' ';
517 } while (FindNextFile(hff, &fd) != 0);
522 /*****************************************************************************
523 * WCMD_setshow_default
525 * Set/Show the current default directory
528 void WCMD_setshow_default () {
533 if (strlen(param1) == 0) {
534 GetCurrentDirectory (sizeof(string), string);
535 strcat (string, "\n");
536 WCMD_output (string);
539 status = SetCurrentDirectory (param1);
548 /****************************************************************************
551 * Set/Show the system date
552 * FIXME: Can't change date yet
555 void WCMD_setshow_date () {
557 char curdate[64], buffer[64];
560 if (lstrlen(param1) == 0) {
561 if (GetDateFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL,
562 curdate, sizeof(curdate))) {
563 WCMD_output ("Current Date is %s\nEnter new date: ", curdate);
564 ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
569 else WCMD_print_error ();
576 /****************************************************************************
579 * Set/Show the environment variables
581 * FIXME: need to sort variables into order for display?
584 void WCMD_setshow_env (char *s) {
591 if (strlen(param1) == 0) {
592 env = GetEnvironmentStrings ();
595 WCMD_output ("%s\n", p);
603 /* FIXME: Emulate Win98 for now, ie "SET C" looks ONLY for an
604 environment variable C, whereas on NT it shows ALL variables
607 status = GetEnvironmentVariable(s, buffer, sizeof(buffer));
609 WCMD_output("%s=%s\n", s, buffer);
611 WCMD_output ("Environment variable %s not defined\n", s);
617 if (strlen(p) == 0) p = 0x00;
618 status = SetEnvironmentVariable (s, p);
619 if (!status) WCMD_print_error();
621 /* WCMD_output (newline); @JED*/
624 /****************************************************************************
627 * Set/Show the path environment variable
630 void WCMD_setshow_path () {
635 if (strlen(param1) == 0) {
636 status = GetEnvironmentVariable ("PATH", string, sizeof(string));
638 WCMD_output ("PATH=%s\n", string);
641 WCMD_output ("PATH not found\n");
645 status = SetEnvironmentVariable ("PATH", param1);
646 if (!status) WCMD_print_error();
650 /****************************************************************************
651 * WCMD_setshow_prompt
653 * Set or show the command prompt.
656 void WCMD_setshow_prompt () {
660 if (strlen(param1) == 0) {
661 SetEnvironmentVariable ("PROMPT", NULL);
665 while ((*s == '=') || (*s == ' ')) s++;
666 if (strlen(s) == 0) {
667 SetEnvironmentVariable ("PROMPT", NULL);
669 else SetEnvironmentVariable ("PROMPT", s);
673 /****************************************************************************
676 * Set/Show the system time
677 * FIXME: Can't change time yet
680 void WCMD_setshow_time () {
682 char curtime[64], buffer[64];
686 if (strlen(param1) == 0) {
688 if (GetTimeFormat (LOCALE_USER_DEFAULT, 0, &st, NULL,
689 curtime, sizeof(curtime))) {
690 WCMD_output ("Current Time is %s\nEnter new time: ", curtime);
691 ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
696 else WCMD_print_error ();
703 /****************************************************************************
706 * Shift batch parameters.
711 if (context != NULL) context -> shift_count++;
715 /****************************************************************************
718 * Copy a file to standard output.
727 h = CreateFile (param1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
728 FILE_ATTRIBUTE_NORMAL, NULL);
729 if (h == INVALID_HANDLE_VALUE) {
733 while (ReadFile (h, buffer, sizeof(buffer), &count, NULL)) {
734 if (count == 0) break; /* ReadFile reports success on EOF! */
735 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), buffer, count, &count, NULL);
740 /****************************************************************************
743 * Display verify flag.
744 * FIXME: We don't actually do anything with the verify flag other than toggle
748 void WCMD_verify (char *command) {
750 static char *von = "Verify is ON\n", *voff = "Verify is OFF\n";
753 count = strlen(command);
755 if (verify_mode) WCMD_output (von);
756 else WCMD_output (voff);
759 if (lstrcmpi(command, "ON") == 0) {
763 else if (lstrcmpi(command, "OFF") == 0) {
767 else WCMD_output ("Verify must be ON or OFF\n");
770 /****************************************************************************
773 * Display version info.
776 void WCMD_version () {
778 WCMD_output (version_string);
782 /****************************************************************************
785 * Display volume info and/or set volume label. Returns 0 if error.
788 int WCMD_volume (int mode, char *path) {
791 char string[MAX_PATH], label[MAX_PATH], curdir[MAX_PATH];
793 static char syntax[] = "Syntax Error\n\n";
795 if (lstrlen(path) == 0) {
796 status = GetCurrentDirectory (sizeof(curdir), curdir);
801 status = GetVolumeInformation (NULL, label, sizeof(label), &serial, NULL,
805 if ((path[1] != ':') || (lstrlen(path) != 2)) {
806 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), syntax, strlen(syntax), &count, NULL);
809 wsprintf (curdir, "%s\\", path);
810 status = GetVolumeInformation (curdir, label, sizeof(label), &serial, NULL,
817 WCMD_output ("Volume in drive %c is %s\nVolume Serial Number is %04x-%04x\n\n",
818 curdir[0], label, HIWORD(serial), LOWORD(serial));
820 WCMD_output ("Volume label (11 characters, ENTER for none)?");
821 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
823 string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
824 if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
826 if (lstrlen(path) != 0) {
827 if (!SetVolumeLabel (curdir, string)) WCMD_print_error ();
830 if (!SetVolumeLabel (NULL, string)) WCMD_print_error ();