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;
392 WCMD_parameter (p, 2+negate, &command);
394 else if (!lstrcmpi (condition, "exist")) {
395 if ((h = CreateFile (WCMD_parameter (p, 1+negate, NULL), GENERIC_READ,
396 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
397 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE) {
401 WCMD_parameter (p, 2+negate, &command);
403 else if ((s = strstr (p, "=="))) {
405 if (!lstrcmpi (condition, WCMD_parameter (s, 0, NULL))) test = 1;
406 WCMD_parameter (s, 1, &command);
409 WCMD_output ("Syntax error\n");
412 if (test != negate) {
413 command = strdup (command);
414 WCMD_process_command (command);
419 /****************************************************************************
422 * Move a file, directory tree or wildcarded set of files.
423 * FIXME: Needs input and output files to be fully specified.
429 char outpath[MAX_PATH], inpath[MAX_PATH], *infile;
433 if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
434 WCMD_output ("Wildcards not yet supported\n");
438 /* If no destination supplied, assume current directory */
439 if (param2[0] == 0x00) {
443 /* If 2nd parm is directory, then use original filename */
444 GetFullPathName (param2, sizeof(outpath), outpath, NULL);
445 hff = FindFirstFile (outpath, &fd);
446 if (hff != INVALID_HANDLE_VALUE) {
447 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
448 GetFullPathName (param1, sizeof(inpath), inpath, &infile);
449 strcat (outpath, "\\");
450 strcat (outpath, infile);
455 status = MoveFile (param1, outpath);
456 if (!status) WCMD_print_error ();
459 /****************************************************************************
462 * Wait for keyboard input.
470 WCMD_output (anykey);
471 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
474 /****************************************************************************
477 * Delete a directory.
480 void WCMD_remove_dir () {
482 if (!RemoveDirectory (param1)) WCMD_print_error ();
485 /****************************************************************************
489 * FIXME: Needs input and output files to be fully specified.
492 void WCMD_rename () {
496 if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
497 WCMD_output ("Wildcards not yet supported\n");
500 status = MoveFile (param1, param2);
501 if (!status) WCMD_print_error ();
504 /*****************************************************************************
505 * WCMD_setshow_attrib
507 * Display and optionally sets DOS attributes on a file or directory
509 * FIXME: Wine currently uses the Unix stat() function to get file attributes.
510 * As a result only the Readonly flag is correctly reported, the Archive bit
511 * is always set and the rest are not implemented. We do the Right Thing anyway.
513 * FIXME: No SET functionality.
517 void WCMD_setshow_attrib () {
522 char flags[9] = {" "};
524 if (param1[0] == '-') {
529 if (lstrlen(param1) == 0) {
530 GetCurrentDirectory (sizeof(param1), param1);
531 strcat (param1, "\\*");
534 hff = FindFirstFile (param1, &fd);
535 if (hff == INVALID_HANDLE_VALUE) {
536 WCMD_output ("%s: File Not Found\n",param1);
540 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
541 if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
544 if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
547 if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
550 if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
553 if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) {
556 if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) {
559 WCMD_output ("%s %s\n", flags, fd.cFileName);
560 for (count=0; count < 8; count++) flags[count] = ' ';
562 } while (FindNextFile(hff, &fd) != 0);
567 /*****************************************************************************
568 * WCMD_setshow_default
570 * Set/Show the current default directory
573 void WCMD_setshow_default () {
578 if (strlen(param1) == 0) {
579 GetCurrentDirectory (sizeof(string), string);
580 strcat (string, "\n");
581 WCMD_output (string);
584 status = SetCurrentDirectory (param1);
593 /****************************************************************************
596 * Set/Show the system date
597 * FIXME: Can't change date yet
600 void WCMD_setshow_date () {
602 char curdate[64], buffer[64];
605 if (lstrlen(param1) == 0) {
606 if (GetDateFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL,
607 curdate, sizeof(curdate))) {
608 WCMD_output ("Current Date is %s\nEnter new date: ", curdate);
609 ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
614 else WCMD_print_error ();
621 /****************************************************************************
624 * Set/Show the environment variables
626 * FIXME: need to sort variables into order for display?
629 void WCMD_setshow_env (char *s) {
636 if (strlen(param1) == 0) {
637 env = GetEnvironmentStrings ();
640 WCMD_output ("%s\n", p);
648 /* FIXME: Emulate Win98 for now, ie "SET C" looks ONLY for an
649 environment variable C, whereas on NT it shows ALL variables
652 status = GetEnvironmentVariable(s, buffer, sizeof(buffer));
654 WCMD_output("%s=%s\n", s, buffer);
656 WCMD_output ("Environment variable %s not defined\n", s);
662 if (strlen(p) == 0) p = 0x00;
663 status = SetEnvironmentVariable (s, p);
664 if (!status) WCMD_print_error();
666 /* WCMD_output (newline); @JED*/
669 /****************************************************************************
672 * Set/Show the path environment variable
675 void WCMD_setshow_path () {
680 if (strlen(param1) == 0) {
681 status = GetEnvironmentVariable ("PATH", string, sizeof(string));
683 WCMD_output ("PATH=%s\n", string);
686 WCMD_output ("PATH not found\n");
690 status = SetEnvironmentVariable ("PATH", param1);
691 if (!status) WCMD_print_error();
695 /****************************************************************************
696 * WCMD_setshow_prompt
698 * Set or show the command prompt.
701 void WCMD_setshow_prompt () {
705 if (strlen(param1) == 0) {
706 SetEnvironmentVariable ("PROMPT", NULL);
710 while ((*s == '=') || (*s == ' ')) s++;
711 if (strlen(s) == 0) {
712 SetEnvironmentVariable ("PROMPT", NULL);
714 else SetEnvironmentVariable ("PROMPT", s);
718 /****************************************************************************
721 * Set/Show the system time
722 * FIXME: Can't change time yet
725 void WCMD_setshow_time () {
727 char curtime[64], buffer[64];
731 if (strlen(param1) == 0) {
733 if (GetTimeFormat (LOCALE_USER_DEFAULT, 0, &st, NULL,
734 curtime, sizeof(curtime))) {
735 WCMD_output ("Current Time is %s\nEnter new time: ", curtime);
736 ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
741 else WCMD_print_error ();
748 /****************************************************************************
751 * Shift batch parameters.
756 if (context != NULL) context -> shift_count++;
760 /****************************************************************************
763 * Set the console title
765 void WCMD_title (char *command) {
766 SetConsoleTitle(command);
769 /****************************************************************************
772 * Copy a file to standard output.
781 h = CreateFile (param1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
782 FILE_ATTRIBUTE_NORMAL, NULL);
783 if (h == INVALID_HANDLE_VALUE) {
787 while (ReadFile (h, buffer, sizeof(buffer), &count, NULL)) {
788 if (count == 0) break; /* ReadFile reports success on EOF! */
789 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), buffer, count, &count, NULL);
794 /****************************************************************************
797 * Display verify flag.
798 * FIXME: We don't actually do anything with the verify flag other than toggle
802 void WCMD_verify (char *command) {
804 static char *von = "Verify is ON\n", *voff = "Verify is OFF\n";
807 count = strlen(command);
809 if (verify_mode) WCMD_output (von);
810 else WCMD_output (voff);
813 if (lstrcmpi(command, "ON") == 0) {
817 else if (lstrcmpi(command, "OFF") == 0) {
821 else WCMD_output ("Verify must be ON or OFF\n");
824 /****************************************************************************
827 * Display version info.
830 void WCMD_version () {
832 WCMD_output (version_string);
836 /****************************************************************************
839 * Display volume info and/or set volume label. Returns 0 if error.
842 int WCMD_volume (int mode, char *path) {
845 char string[MAX_PATH], label[MAX_PATH], curdir[MAX_PATH];
847 static char syntax[] = "Syntax Error\n\n";
849 if (lstrlen(path) == 0) {
850 status = GetCurrentDirectory (sizeof(curdir), curdir);
855 status = GetVolumeInformation (NULL, label, sizeof(label), &serial, NULL,
859 if ((path[1] != ':') || (lstrlen(path) != 2)) {
860 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), syntax, strlen(syntax), &count, NULL);
863 wsprintf (curdir, "%s\\", path);
864 status = GetVolumeInformation (curdir, label, sizeof(label), &serial, NULL,
871 WCMD_output ("Volume in drive %c is %s\nVolume Serial Number is %04x-%04x\n\n",
872 curdir[0], label, HIWORD(serial), LOWORD(serial));
874 WCMD_output ("Volume label (11 characters, ENTER for none)?");
875 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
877 string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
878 if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
880 if (lstrlen(path) != 0) {
881 if (!SetVolumeLabel (curdir, string)) WCMD_print_error ();
884 if (!SetVolumeLabel (NULL, string)) WCMD_print_error ();