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 redirection, 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 extern HANDLE STDin, STDout;
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;
34 /****************************************************************************
37 * Clear the terminal screen.
40 void WCMD_clear_screen () {
46 /****************************************************************************
49 * Change the default i/o device (ie redirect STDin/STDout).
52 void WCMD_change_tty () {
58 /****************************************************************************
61 * Copy a file or wildcarded set.
62 * FIXME: No wildcard support
63 * FIXME: Needs output file to be fully specified (can't just enter directory)
72 static char *overwrite = "Overwrite file (Y/N)?";
73 char string[8], outpath[MAX_PATH];
75 if ((strchr(param1,'*') != NULL) && (strchr(param1,'%') != NULL)) {
76 WCMD_output ("Wildcards not yet supported\n");
79 GetFullPathName (param2, sizeof(outpath), outpath, NULL);
80 force = (strstr (quals, "/Y") != NULL);
82 hff = FindFirstFile (outpath, &fd);
83 if (hff != INVALID_HANDLE_VALUE) {
85 WCMD_output (overwrite);
86 ReadFile (STDin, string, sizeof(string), &count, NULL);
87 if (toupper(string[0]) == 'Y') force = TRUE;
92 status = CopyFile (param1, outpath, FALSE);
93 if (!status) WCMD_print_error ();
97 /****************************************************************************
100 * Create a directory.
103 void WCMD_create_dir () {
105 if (!CreateDirectory (param1, NULL)) WCMD_print_error ();
108 /****************************************************************************
111 * Delete a file or wildcarded set.
115 void WCMD_delete (int recurse) {
119 char fpath[MAX_PATH];
122 hff = FindFirstFile (param1, &fd);
123 if (hff == INVALID_HANDLE_VALUE) {
124 WCMD_output ("File Not Found\n");
127 if ((strchr(param1,'*') == NULL) && (strchr(param1,'?') == NULL)
128 && (!recurse) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
129 strcat (param1, "\\*");
133 if ((strchr(param1,'*') != NULL) || (strchr(param1,'?') != NULL)) {
134 strcpy (fpath, param1);
136 p = strrchr (fpath, '\\');
139 strcat (fpath, fd.cFileName);
141 else strcpy (fpath, fd.cFileName);
142 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
143 if (!DeleteFile (fpath)) WCMD_print_error ();
145 } while (FindNextFile(hff, &fd) != 0);
149 if (!DeleteFile (param1)) WCMD_print_error ();
153 /****************************************************************************
156 * Echo input to the screen (or not). We don't try to emulate the bugs
157 * in DOS (try typing "ECHO ON AGAIN" for an example).
160 void WCMD_echo (char *command) {
162 static char *eon = "Echo is ON\n", *eoff = "Echo is OFF\n";
165 count = strlen(command);
167 if (echo_mode) WCMD_output (eon);
168 else WCMD_output (eoff);
171 if ((count == 1) && (command[0] == '.')) {
172 WCMD_output (newline);
175 if (lstrcmpi(command, "ON") == 0) {
179 if (lstrcmpi(command, "OFF") == 0) {
183 WCMD_output (command);
184 WCMD_output (newline);
188 /****************************************************************************
191 * Batch file loop processing.
194 void WCMD_for (char *p) {
196 if (lstrcmpi (WCMD_parameter (p, 1), "in") || lstrcmpi (WCMD_parameter (p, 3), "do")) {
197 WCMD_output ("Syntax error\n");
204 /**************************************************************************
207 * Simple on-line help. Help text is stored in the resource file.
210 void WCMD_give_help (char *command) {
215 command = WCMD_strtrim_leading_spaces(command);
216 if (lstrlen(command) == 0) {
217 LoadString (hinst, 1000, buffer, sizeof(buffer));
218 WCMD_output (buffer);
221 for (i=0; i<=WCMD_EXIT; i++) {
222 if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
223 param1, -1, inbuilt[i], -1) == 2) {
224 LoadString (hinst, i, buffer, sizeof(buffer));
225 WCMD_output (buffer);
229 WCMD_output ("No help available for %s\n", param1);
234 /****************************************************************************
237 * Batch file jump instruction. Not the most efficient algorithm ;-)
238 * Prints error message if the specified label cannot be found - the file pointer is
239 * then at EOF, effectively stopping the batch file.
240 * FIXME: DOS is supposed to allow labels with spaces - we don't.
245 char string[MAX_PATH];
247 if (context != NULL) {
248 SetFilePointer (context -> h, 0, NULL, FILE_BEGIN);
249 while (WCMD_fgets (string, sizeof(string), context -> h)) {
250 if ((string[0] == ':') && (strcmp (&string[1], param1) == 0)) return;
252 WCMD_output ("Target to GOTO not found\n");
258 /****************************************************************************
261 * Batch file conditional.
270 /****************************************************************************
273 * Move a file, directory tree or wildcarded set of files.
281 /****************************************************************************
284 * Wait for keyboard input.
292 WCMD_output (anykey);
293 ReadFile (STDin, string, sizeof(string), &count, NULL);
296 /****************************************************************************
299 * Delete a directory.
302 void WCMD_remove_dir () {
304 if (!RemoveDirectory (param1)) WCMD_print_error ();
307 /****************************************************************************
311 * FIXME: Needs input and output files to be fully specified.
314 void WCMD_rename () {
317 static char *dirmsg = "Input file is a directory. Use the MOVE command\n\n";
319 if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
320 WCMD_output ("Wildcards not yet supported\n");
323 status = GetFileAttributes (param1);
324 if ((status != -1) && (status & FILE_ATTRIBUTE_DIRECTORY)) {
325 WCMD_output (dirmsg);
328 status = MoveFile (param1, param2);
329 if (!status) WCMD_print_error ();
332 /*****************************************************************************
333 * WCMD_setshow_attrib
335 * Display and optionally sets DOS attributes on a file or directory
337 * FIXME: Wine currently uses the Unix stat() function to get file attributes.
338 * As a result only the Readonly flag is correctly reported, the Archive bit
339 * is always set and the rest are not implemented. We do the Right Thing anyway.
343 void WCMD_setshow_attrib () {
348 char flags[9] = {" "};
350 if (lstrlen(param1) == 0) {
351 GetCurrentDirectory (sizeof(param1), param1);
352 strcat (param1, "\\*");
355 hff = FindFirstFile (param1, &fd);
356 if (hff == INVALID_HANDLE_VALUE) {
357 WCMD_output ("File Not Found\n");
361 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
362 if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
365 if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
368 if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
371 if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
374 if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) {
377 if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) {
380 WCMD_output ("%s %s\n", flags, fd.cFileName);
381 for (count=0; count < 8; count++) flags[count] = ' ';
383 } while (FindNextFile(hff, &fd) != 0);
388 /*****************************************************************************
389 * WCMD_setshow_default
391 * Set/Show the current default directory
394 void WCMD_setshow_default () {
399 if (strlen(param1) == 0) {
400 GetCurrentDirectory (sizeof(string), string);
401 strcat (string, "\n");
402 WCMD_output (string);
405 status = SetCurrentDirectory (param1);
414 /****************************************************************************
417 * Set/Show the system date
418 * FIXME: Can't change date yet
421 void WCMD_setshow_date () {
423 char curdate[64], buffer[64];
426 if (lstrlen(param1) == 0) {
427 if (GetDateFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL,
428 curdate, sizeof(curdate))) {
429 WCMD_output ("Current Date is %s\nEnter new date: ", curdate);
430 ReadFile (STDin, buffer, sizeof(buffer), &count, NULL);
435 else WCMD_print_error ();
442 /****************************************************************************
445 * Set/Show the environment variables
447 * FIXME: need to sort variables into order for display?
450 void WCMD_setshow_env (char *s) {
456 if (strlen(param1) == 0) {
457 env = GetEnvironmentStrings ();
460 WCMD_output ("%s\n", p);
467 WCMD_output ("Command Syntax: SET variable=value\n");
471 status = SetEnvironmentVariable (s, p);
472 if (!status) WCMD_print_error();
474 WCMD_output (newline);
477 /****************************************************************************
480 * Set/Show the path environment variable
483 void WCMD_setshow_path () {
488 if (strlen(param1) == 0) {
489 status = GetEnvironmentVariable ("PATH", string, sizeof(string));
491 WCMD_output ("PATH=%s\n", string);
494 WCMD_output ("PATH not found\n");
498 status = SetEnvironmentVariable ("PATH", param1);
499 if (!status) WCMD_print_error();
503 /****************************************************************************
504 * WCMD_setshow_prompt
506 * Set or show the command prompt.
509 void WCMD_setshow_prompt () {
513 if (strlen(param1) == 0) {
514 SetEnvironmentVariable ("PROMPT", NULL);
518 while ((*s == '=') || (*s == ' ')) s++;
519 if (strlen(s) == 0) {
520 SetEnvironmentVariable ("PROMPT", NULL);
522 else SetEnvironmentVariable ("PROMPT", s);
526 /****************************************************************************
529 * Set/Show the system time
530 * FIXME: Can't change time yet
533 void WCMD_setshow_time () {
535 char curtime[64], buffer[64];
538 if (strlen(param1) == 0) {
539 if (GetTimeFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL,
540 curtime, sizeof(curtime))) {
541 WCMD_output ("Current Time is %s\nEnter new time: ", curtime);
542 ReadFile (STDin, buffer, sizeof(buffer), &count, NULL);
547 else WCMD_print_error ();
554 /****************************************************************************
557 * Shift batch parameters.
562 if (context != NULL) context -> shift_count++;
566 /****************************************************************************
569 * Copy a file to standard output.
578 h = CreateFile (param1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
579 FILE_ATTRIBUTE_NORMAL, 0);
580 if (h == INVALID_HANDLE_VALUE) {
584 while (ReadFile (h, buffer, sizeof(buffer), &count, NULL)) {
585 if (count == 0) break; /* ReadFile reports success on EOF! */
586 WriteFile (STDout, buffer, count, &count, NULL);
591 /****************************************************************************
594 * Display verify flag.
595 * FIXME: We don't actually do anything with the verify flag other than toggle
599 void WCMD_verify (char *command) {
601 static char *von = "Verify is ON\n", *voff = "Verify is OFF\n";
604 count = strlen(command);
606 if (verify_mode) WCMD_output (von);
607 else WCMD_output (voff);
610 if (lstrcmpi(command, "ON") == 0) {
614 else if (lstrcmpi(command, "OFF") == 0) {
618 else WCMD_output ("Verify must be ON or OFF\n");
621 /****************************************************************************
624 * Display version info.
627 void WCMD_version () {
629 WCMD_output (version_string);
633 /****************************************************************************
636 * Display volume info and/or set volume label. Returns 0 if error.
639 int WCMD_volume (int mode, char *path) {
642 char string[MAX_PATH], label[MAX_PATH], curdir[MAX_PATH];
644 static char syntax[] = "Syntax Error\n\n";
646 if (lstrlen(path) == 0) {
647 status = GetCurrentDirectory (sizeof(curdir), curdir);
652 status = GetVolumeInformation (NULL, label, sizeof(label), &serial, NULL,
656 if ((path[1] != ':') || (lstrlen(path) != 2)) {
657 WriteFile (STDout, syntax, strlen(syntax), &count, NULL);
660 wsprintf (curdir, "%s\\", path);
661 status = GetVolumeInformation (curdir, label, sizeof(label), &serial, NULL,
668 WCMD_output ("Volume in drive %c is %s\nVolume Serial Number is %04x-%04x\n\n",
669 curdir[0], label, HIWORD(serial), LOWORD(serial));
671 WCMD_output ("Volume label (11 characters, ENTER for none)?");
672 ReadFile (STDin, string, sizeof(string), &count, NULL);
673 if (lstrlen(path) != 0) {
674 if (!SetVolumeLabel (curdir, string)) WCMD_print_error ();
677 if (!SetVolumeLabel (NULL, string)) WCMD_print_error ();