Added Japanese translation for msrle32, wininet, wcmd.
[wine] / programs / wcmd / builtins.c
1 /*
2  * WCMD - Wine-compatible command line interface - built-in functions.
3  *
4  * Copyright (C) 1999 D A Pickles
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /*
22  * NOTES:
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.
26  */
27
28 /*
29  * FIXME:
30  * - No support for pipes, shell parameters
31  * - Lots of functionality missing from builtins
32  * - Messages etc need international support
33  */
34
35 #include "wcmd.h"
36
37 void WCMD_execute (char *orig_command, char *parameter, char *substitution);
38
39 struct env_stack
40 {
41   struct env_stack *next;
42   WCHAR *strings;
43 };
44
45 struct env_stack *saved_environment;
46
47 extern HINSTANCE hinst;
48 extern char *inbuilt[];
49 extern int echo_mode, verify_mode;
50 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
51 extern BATCH_CONTEXT *context;
52 extern DWORD errorlevel;
53
54
55
56 /****************************************************************************
57  * WCMD_clear_screen
58  *
59  * Clear the terminal screen.
60  */
61
62 void WCMD_clear_screen (void) {
63
64   /* Emulate by filling the screen from the top left to bottom right with
65         spaces, then moving the cursor to the top left afterwards */
66   CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
67   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
68
69   if (GetConsoleScreenBufferInfo(hStdOut, &consoleInfo))
70   {
71       COORD topLeft;
72       long screenSize;
73       
74       screenSize = consoleInfo.dwSize.X * (consoleInfo.dwSize.Y + 1);
75
76       topLeft.X = 0;
77       topLeft.Y = 0;
78       FillConsoleOutputCharacter(hStdOut, ' ', screenSize, topLeft, &screenSize);
79       SetConsoleCursorPosition(hStdOut, topLeft);
80   }
81 }
82
83 /****************************************************************************
84  * WCMD_change_tty
85  *
86  * Change the default i/o device (ie redirect STDin/STDout).
87  */
88
89 void WCMD_change_tty (void) {
90
91   WCMD_output (nyi);
92
93 }
94
95 /****************************************************************************
96  * WCMD_copy
97  *
98  * Copy a file or wildcarded set.
99  * FIXME: No wildcard support
100  */
101
102 void WCMD_copy (void) {
103
104 DWORD count;
105 WIN32_FIND_DATA fd;
106 HANDLE hff;
107 BOOL force, status;
108 static const char *overwrite = "Overwrite file (Y/N)?";
109 char string[8], outpath[MAX_PATH], inpath[MAX_PATH], *infile;
110
111   if ((strchr(param1,'*') != NULL) && (strchr(param1,'%') != NULL)) {
112     WCMD_output ("Wildcards not yet supported\n");
113     return;
114   }
115
116   /* If no destination supplied, assume current directory */
117   if (param2[0] == 0x00) {
118       strcpy(param2, ".");
119   }
120
121   GetFullPathName (param2, sizeof(outpath), outpath, NULL);
122   hff = FindFirstFile (outpath, &fd);
123   if (hff != INVALID_HANDLE_VALUE) {
124     if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
125       GetFullPathName (param1, sizeof(inpath), inpath, &infile);
126       strcat (outpath, "\\");
127       strcat (outpath, infile);
128     }
129     FindClose (hff);
130   }
131
132   force = (strstr (quals, "/Y") != NULL);
133   if (!force) {
134     hff = FindFirstFile (outpath, &fd);
135     if (hff != INVALID_HANDLE_VALUE) {
136       FindClose (hff);
137       WCMD_output (overwrite);
138       ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
139       if (toupper(string[0]) == 'Y') force = TRUE;
140     }
141     else force = TRUE;
142   }
143   if (force) {
144     status = CopyFile (param1, outpath, FALSE);
145     if (!status) WCMD_print_error ();
146   }
147 }
148
149 /****************************************************************************
150  * WCMD_create_dir
151  *
152  * Create a directory.
153  */
154
155 void WCMD_create_dir (void) {
156
157   if (!CreateDirectory (param1, NULL)) WCMD_print_error ();
158 }
159
160 /****************************************************************************
161  * WCMD_delete
162  *
163  * Delete a file or wildcarded set.
164  *
165  */
166
167 void WCMD_delete (int recurse) {
168
169 WIN32_FIND_DATA fd;
170 HANDLE hff;
171 char fpath[MAX_PATH];
172 char *p;
173
174   hff = FindFirstFile (param1, &fd);
175   if (hff == INVALID_HANDLE_VALUE) {
176     WCMD_output ("%s :File Not Found\n",param1);
177     return;
178   }
179   if ((strchr(param1,'*') == NULL) && (strchr(param1,'?') == NULL)
180         && (!recurse) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
181     strcat (param1, "\\*");
182     FindClose(hff);
183     WCMD_delete (1);
184     return;
185   }
186   if ((strchr(param1,'*') != NULL) || (strchr(param1,'?') != NULL)) {
187     strcpy (fpath, param1);
188     do {
189       p = strrchr (fpath, '\\');
190       if (p != NULL) {
191         *++p = '\0';
192         strcat (fpath, fd.cFileName);
193       }
194       else strcpy (fpath, fd.cFileName);
195       if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
196         if (!DeleteFile (fpath)) WCMD_print_error ();
197       }
198     } while (FindNextFile(hff, &fd) != 0);
199     FindClose (hff);
200   }
201   else {
202     if (!DeleteFile (param1)) WCMD_print_error ();
203     FindClose (hff);
204   }
205 }
206
207 /****************************************************************************
208  * WCMD_echo
209  *
210  * Echo input to the screen (or not). We don't try to emulate the bugs
211  * in DOS (try typing "ECHO ON AGAIN" for an example).
212  */
213
214 void WCMD_echo (const char *command) {
215
216 static const char *eon = "Echo is ON\n", *eoff = "Echo is OFF\n";
217 int count;
218
219   if ((command[0] == '.') && (command[1] == 0)) {
220     WCMD_output (newline);
221     return;
222   }
223   if (command[0]==' ')
224     command++;
225   count = strlen(command);
226   if (count == 0) {
227     if (echo_mode) WCMD_output (eon);
228     else WCMD_output (eoff);
229     return;
230   }
231   if (lstrcmpi(command, "ON") == 0) {
232     echo_mode = 1;
233     return;
234   }
235   if (lstrcmpi(command, "OFF") == 0) {
236     echo_mode = 0;
237     return;
238   }
239   WCMD_output_asis (command);
240   WCMD_output (newline);
241
242 }
243
244 /**************************************************************************
245  * WCMD_for
246  *
247  * Batch file loop processing.
248  * FIXME: We don't exhaustively check syntax. Any command which works in MessDOS
249  * will probably work here, but the reverse is not necessarily the case...
250  */
251
252 void WCMD_for (char *p) {
253
254 WIN32_FIND_DATA fd;
255 HANDLE hff;
256 char *cmd, *item;
257 char set[MAX_PATH], param[MAX_PATH];
258 int i;
259
260   if (lstrcmpi (WCMD_parameter (p, 1, NULL), "in")
261         || lstrcmpi (WCMD_parameter (p, 3, NULL), "do")
262         || (param1[0] != '%')) {
263     WCMD_output ("Syntax error\n");
264     return;
265   }
266   lstrcpyn (set, WCMD_parameter (p, 2, NULL), sizeof(set));
267   WCMD_parameter (p, 4, &cmd);
268   lstrcpy (param, param1);
269
270 /*
271  *      If the parameter within the set has a wildcard then search for matching files
272  *      otherwise do a literal substitution.
273  */
274
275   i = 0;
276   while (*(item = WCMD_parameter (set, i, NULL))) {
277     if (strpbrk (item, "*?")) {
278       hff = FindFirstFile (item, &fd);
279       if (hff == INVALID_HANDLE_VALUE) {
280         return;
281       }
282       do {
283         WCMD_execute (cmd, param, fd.cFileName);
284       } while (FindNextFile(hff, &fd) != 0);
285       FindClose (hff);
286 }
287     else {
288       WCMD_execute (cmd, param, item);
289     }
290     i++;
291   }
292 }
293
294 /*****************************************************************************
295  * WCMD_Execute
296  *
297  *      Execute a command after substituting variable text for the supplied parameter
298  */
299
300 void WCMD_execute (char *orig_cmd, char *param, char *subst) {
301
302 char *new_cmd, *p, *s, *dup;
303 int size;
304
305   size = lstrlen (orig_cmd);
306   new_cmd = (char *) LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, size);
307   dup = s = strdup (orig_cmd);
308
309   while ((p = strstr (s, param))) {
310     *p = '\0';
311     size += lstrlen (subst);
312     new_cmd = (char *) LocalReAlloc ((HANDLE)new_cmd, size, 0);
313     strcat (new_cmd, s);
314     strcat (new_cmd, subst);
315     s = p + lstrlen (param);
316   }
317   strcat (new_cmd, s);
318   WCMD_process_command (new_cmd);
319   free (dup);
320   LocalFree ((HANDLE)new_cmd);
321 }
322
323
324 /**************************************************************************
325  * WCMD_give_help
326  *
327  *      Simple on-line help. Help text is stored in the resource file.
328  */
329
330 void WCMD_give_help (char *command) {
331
332 int i;
333 char buffer[2048];
334
335   command = WCMD_strtrim_leading_spaces(command);
336   if (lstrlen(command) == 0) {
337     LoadString (hinst, 1000, buffer, sizeof(buffer));
338     WCMD_output_asis (buffer);
339   }
340   else {
341     for (i=0; i<=WCMD_EXIT; i++) {
342       if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
343           param1, -1, inbuilt[i], -1) == 2) {
344         LoadString (hinst, i, buffer, sizeof(buffer));
345         WCMD_output (buffer);
346         return;
347       }
348     }
349     WCMD_output ("No help available for %s\n", param1);
350   }
351   return;
352 }
353
354 /****************************************************************************
355  * WCMD_go_to
356  *
357  * Batch file jump instruction. Not the most efficient algorithm ;-)
358  * Prints error message if the specified label cannot be found - the file pointer is
359  * then at EOF, effectively stopping the batch file.
360  * FIXME: DOS is supposed to allow labels with spaces - we don't.
361  */
362
363 void WCMD_goto (void) {
364
365 char string[MAX_PATH];
366
367   if (context != NULL) {
368     SetFilePointer (context -> h, 0, NULL, FILE_BEGIN);
369     while (WCMD_fgets (string, sizeof(string), context -> h)) {
370       if ((string[0] == ':') && (strcmp (&string[1], param1) == 0)) return;
371     }
372     WCMD_output ("Target to GOTO not found\n");
373   }
374   return;
375 }
376
377
378 /****************************************************************************
379  * WCMD_if
380  *
381  * Batch file conditional.
382  * FIXME: Much more syntax checking needed!
383  */
384
385 void WCMD_if (char *p) {
386
387 HANDLE h;
388 int negate = 0, test = 0;
389 char condition[MAX_PATH], *command, *s;
390
391   if (!lstrcmpi (param1, "not")) {
392     negate = 1;
393     lstrcpy (condition, param2);
394 }
395   else {
396     lstrcpy (condition, param1);
397   }
398   if (!lstrcmpi (condition, "errorlevel")) {
399     if (errorlevel >= atoi(WCMD_parameter (p, 1+negate, NULL))) test = 1;
400     return;
401     WCMD_parameter (p, 2+negate, &command);
402   }
403   else if (!lstrcmpi (condition, "exist")) {
404     if ((h = CreateFile (WCMD_parameter (p, 1+negate, NULL), GENERIC_READ,
405                          FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
406         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE) {
407       CloseHandle (h);
408       test = 1;
409     }
410     WCMD_parameter (p, 2+negate, &command);
411   }
412   else if ((s = strstr (p, "=="))) {
413     s += 2;
414     if (!lstrcmpi (condition, WCMD_parameter (s, 0, NULL))) test = 1;
415     WCMD_parameter (s, 1, &command);
416   }
417   else {
418     WCMD_output ("Syntax error\n");
419     return;
420   }
421   if (test != negate) {
422     command = strdup (command);
423     WCMD_process_command (command);
424     free (command);
425   }
426 }
427
428 /****************************************************************************
429  * WCMD_move
430  *
431  * Move a file, directory tree or wildcarded set of files.
432  * FIXME: Needs input and output files to be fully specified.
433  */
434
435 void WCMD_move (void) {
436
437 int status;
438 char outpath[MAX_PATH], inpath[MAX_PATH], *infile;
439 WIN32_FIND_DATA fd;
440 HANDLE hff;
441
442   if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
443     WCMD_output ("Wildcards not yet supported\n");
444     return;
445   }
446
447   /* If no destination supplied, assume current directory */
448   if (param2[0] == 0x00) {
449       strcpy(param2, ".");
450   }
451
452   /* If 2nd parm is directory, then use original filename */
453   GetFullPathName (param2, sizeof(outpath), outpath, NULL);
454   hff = FindFirstFile (outpath, &fd);
455   if (hff != INVALID_HANDLE_VALUE) {
456     if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
457       GetFullPathName (param1, sizeof(inpath), inpath, &infile);
458       strcat (outpath, "\\");
459       strcat (outpath, infile);
460     }
461     FindClose (hff);
462   }
463
464   status = MoveFile (param1, outpath);
465   if (!status) WCMD_print_error ();
466 }
467
468 /****************************************************************************
469  * WCMD_pause
470  *
471  * Wait for keyboard input.
472  */
473
474 void WCMD_pause (void) {
475
476 DWORD count;
477 char string[32];
478
479   WCMD_output (anykey);
480   ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
481 }
482
483 /****************************************************************************
484  * WCMD_remove_dir
485  *
486  * Delete a directory.
487  */
488
489 void WCMD_remove_dir (void) {
490
491   if (!RemoveDirectory (param1)) WCMD_print_error ();
492 }
493
494 /****************************************************************************
495  * WCMD_rename
496  *
497  * Rename a file.
498  * FIXME: Needs input and output files to be fully specified.
499  */
500
501 void WCMD_rename (void) {
502
503 int status;
504
505   if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) {
506     WCMD_output ("Wildcards not yet supported\n");
507     return;
508   }
509   status = MoveFile (param1, param2);
510   if (!status) WCMD_print_error ();
511 }
512
513 /*****************************************************************************
514  * WCMD_dupenv
515  *
516  * Make a copy of the environment.
517  */
518 WCHAR *WCMD_dupenv( const WCHAR *env )
519 {
520   WCHAR *env_copy;
521   int len;
522
523   if( !env )
524     return NULL;
525
526   len = 0;
527   while ( env[len] )
528     len += (lstrlenW(&env[len]) + 1);
529
530   env_copy = LocalAlloc (LMEM_FIXED, (len+1) * sizeof (WCHAR) );
531   if (!env_copy)
532   {
533     WCMD_output ("out of memory\n");
534     return env_copy;
535   }
536   memcpy (env_copy, env, len*sizeof (WCHAR));
537   env_copy[len] = 0;
538
539   return env_copy;
540 }
541
542 /*****************************************************************************
543  * WCMD_setlocal
544  *
545  *  setlocal pushes the environment onto a stack
546  *  Save the environment as unicode so we don't screw anything up.
547  */
548 void WCMD_setlocal (const char *s) {
549   WCHAR *env;
550   struct env_stack *env_copy;
551
552   /* DISABLEEXTENSIONS ignored */
553
554   env_copy = LocalAlloc (LMEM_FIXED, sizeof (struct env_stack));
555   if( !env_copy )
556   {
557     WCMD_output ("out of memory\n");
558     return;
559   }
560
561   env = GetEnvironmentStringsW ();
562
563   env_copy->strings = WCMD_dupenv (env);
564   if (env_copy->strings)
565   {
566     env_copy->next = saved_environment;
567     saved_environment = env_copy;
568   }
569   else
570     LocalFree (env_copy);
571
572   FreeEnvironmentStringsW (env);
573 }
574
575 /*****************************************************************************
576  * WCMD_strchrW
577  */
578 inline WCHAR *WCMD_strchrW(WCHAR *str, WCHAR ch)
579 {
580    while(*str)
581    {
582      if(*str == ch)
583        return str;
584      str++;
585    }
586    return NULL;
587 }
588
589 /*****************************************************************************
590  * WCMD_endlocal
591  *
592  *  endlocal pops the environment off a stack
593  */
594 void WCMD_endlocal (void) {
595   WCHAR *env, *old, *p;
596   struct env_stack *temp;
597   int len, n;
598
599   if (!saved_environment)
600     return;
601
602   /* pop the old environment from the stack */
603   temp = saved_environment;
604   saved_environment = temp->next;
605
606   /* delete the current environment, totally */
607   env = GetEnvironmentStringsW ();
608   old = WCMD_dupenv (GetEnvironmentStringsW ());
609   len = 0;
610   while (old[len]) {
611     n = lstrlenW(&old[len]) + 1;
612     p = WCMD_strchrW(&old[len], '=');
613     if (p)
614     {
615       *p++ = 0;
616       SetEnvironmentVariableW (&old[len], NULL);
617     }
618     len += n;
619   }
620   LocalFree (old);
621   FreeEnvironmentStringsW (env);
622   
623   /* restore old environment */
624   env = temp->strings;
625   len = 0;
626   while (env[len]) {
627     n = lstrlenW(&env[len]) + 1;
628     p = WCMD_strchrW(&env[len], '=');
629     if (p)
630     {
631       *p++ = 0;
632       SetEnvironmentVariableW (&env[len], p);
633     }
634     len += n;
635   }
636   LocalFree (env);
637   LocalFree (temp);
638 }
639
640 /*****************************************************************************
641  * WCMD_setshow_attrib
642  *
643  * Display and optionally sets DOS attributes on a file or directory
644  *
645  * FIXME: Wine currently uses the Unix stat() function to get file attributes.
646  * As a result only the Readonly flag is correctly reported, the Archive bit
647  * is always set and the rest are not implemented. We do the Right Thing anyway.
648  *
649  * FIXME: No SET functionality.
650  *
651  */
652
653 void WCMD_setshow_attrib (void) {
654
655 DWORD count;
656 HANDLE hff;
657 WIN32_FIND_DATA fd;
658 char flags[9] = {"        "};
659
660   if (param1[0] == '-') {
661     WCMD_output (nyi);
662     return;
663   }
664
665   if (lstrlen(param1) == 0) {
666     GetCurrentDirectory (sizeof(param1), param1);
667     strcat (param1, "\\*");
668   }
669
670   hff = FindFirstFile (param1, &fd);
671   if (hff == INVALID_HANDLE_VALUE) {
672     WCMD_output ("%s: File Not Found\n",param1);
673   }
674   else {
675     do {
676       if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
677         if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
678           flags[0] = 'H';
679         }
680         if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
681           flags[1] = 'S';
682         }
683         if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
684           flags[2] = 'A';
685         }
686         if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
687           flags[3] = 'R';
688         }
689         if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) {
690           flags[4] = 'T';
691         }
692         if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) {
693           flags[5] = 'C';
694         }
695         WCMD_output ("%s   %s\n", flags, fd.cFileName);
696         for (count=0; count < 8; count++) flags[count] = ' ';
697       }
698     } while (FindNextFile(hff, &fd) != 0);
699   }
700   FindClose (hff);
701 }
702
703 /*****************************************************************************
704  * WCMD_setshow_default
705  *
706  *      Set/Show the current default directory
707  */
708
709 void WCMD_setshow_default (void) {
710
711 BOOL status;
712 char string[1024];
713
714   if (strlen(param1) == 0) {
715     GetCurrentDirectory (sizeof(string), string);
716     strcat (string, "\n");
717     WCMD_output (string);
718   }
719   else {
720     status = SetCurrentDirectory (param1);
721     if (!status) {
722       WCMD_print_error ();
723       return;
724     }
725    }
726   return;
727 }
728
729 /****************************************************************************
730  * WCMD_setshow_date
731  *
732  * Set/Show the system date
733  * FIXME: Can't change date yet
734  */
735
736 void WCMD_setshow_date (void) {
737
738 char curdate[64], buffer[64];
739 DWORD count;
740
741   if (lstrlen(param1) == 0) {
742     if (GetDateFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL,
743                 curdate, sizeof(curdate))) {
744       WCMD_output ("Current Date is %s\nEnter new date: ", curdate);
745       ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
746       if (count > 2) {
747         WCMD_output (nyi);
748       }
749     }
750     else WCMD_print_error ();
751   }
752   else {
753     WCMD_output (nyi);
754   }
755 }
756
757 /****************************************************************************
758  * WCMD_compare
759  */
760 int WCMD_compare( const void *a, const void *b )
761 {
762     int r;
763     const char * const *str_a = a, * const *str_b = b;
764     r = CompareString( LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
765           *str_a, -1, *str_b, -1 );
766     if( r == CSTR_LESS_THAN ) return -1;
767     if( r == CSTR_GREATER_THAN ) return 1;
768     return 0;
769 }
770
771 /****************************************************************************
772  * WCMD_setshow_sortenv
773  *
774  * sort variables into order for display
775  */
776 void WCMD_setshow_sortenv(const char *s)
777 {
778   UINT count=0, len=0, i;
779   const char **str;
780
781   /* count the number of strings, and the total length */
782   while ( s[len] ) {
783     len += (lstrlen(&s[len]) + 1);
784     count++;
785   }
786
787   /* add the strings to an array */
788   str = LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, count * sizeof (char*) );
789   if( !str )
790     return;
791   str[0] = s;
792   for( i=1; i<count; i++ )
793     str[i] = str[i-1] + lstrlen(str[i-1]) + 1;
794
795   /* sort the array */
796   qsort( str, count, sizeof (char*), WCMD_compare );
797
798   /* print it */
799   for( i=0; i<count; i++ )
800     WCMD_output("%s\n", str[i] );
801
802   LocalFree( str );
803 }
804
805 /****************************************************************************
806  * WCMD_setshow_env
807  *
808  * Set/Show the environment variables
809  */
810
811 void WCMD_setshow_env (char *s) {
812
813 LPVOID env;
814 char *p;
815 int status;
816 char buffer[1048];
817
818   if (strlen(param1) == 0) {
819     env = GetEnvironmentStrings ();
820     WCMD_setshow_sortenv( env );
821   }
822   else {
823     p = strchr (s, '=');
824     if (p == NULL) {
825
826       /* FIXME: Emulate Win98 for now, ie "SET C" looks ONLY for an
827          environment variable C, whereas on NT it shows ALL variables
828          starting with C.
829        */
830       status = GetEnvironmentVariable(s, buffer, sizeof(buffer));
831       if (status) {
832         WCMD_output("%s=%s\n", s, buffer);
833       } else {
834         WCMD_output ("Environment variable %s not defined\n", s);
835       }
836       return;
837     }
838     *p++ = '\0';
839
840     if (strlen(p) == 0) p = 0x00;
841     status = SetEnvironmentVariable (s, p);
842     if (!status) WCMD_print_error();
843   }
844   /* WCMD_output (newline);   @JED*/
845 }
846
847 /****************************************************************************
848  * WCMD_setshow_path
849  *
850  * Set/Show the path environment variable
851  */
852
853 void WCMD_setshow_path (char *command) {
854
855 char string[1024];
856 DWORD status;
857
858   if (strlen(param1) == 0) {
859     status = GetEnvironmentVariable ("PATH", string, sizeof(string));
860     if (status != 0) {
861       WCMD_output ("PATH=%s\n", string);
862     }
863     else {
864       WCMD_output ("PATH not found\n");
865     }
866   }
867   else {
868     status = SetEnvironmentVariable ("PATH", command);
869     if (!status) WCMD_print_error();
870   }
871 }
872
873 /****************************************************************************
874  * WCMD_setshow_prompt
875  *
876  * Set or show the command prompt.
877  */
878
879 void WCMD_setshow_prompt (void) {
880
881 char *s;
882
883   if (strlen(param1) == 0) {
884     SetEnvironmentVariable ("PROMPT", NULL);
885   }
886   else {
887     s = param1;
888     while ((*s == '=') || (*s == ' ')) s++;
889     if (strlen(s) == 0) {
890       SetEnvironmentVariable ("PROMPT", NULL);
891     }
892     else SetEnvironmentVariable ("PROMPT", s);
893   }
894 }
895
896 /****************************************************************************
897  * WCMD_setshow_time
898  *
899  * Set/Show the system time
900  * FIXME: Can't change time yet
901  */
902
903 void WCMD_setshow_time (void) {
904
905 char curtime[64], buffer[64];
906 DWORD count;
907 SYSTEMTIME st;
908
909   if (strlen(param1) == 0) {
910     GetLocalTime(&st);
911     if (GetTimeFormat (LOCALE_USER_DEFAULT, 0, &st, NULL,
912                 curtime, sizeof(curtime))) {
913       WCMD_output ("Current Time is %s\nEnter new time: ", curtime);
914       ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL);
915       if (count > 2) {
916         WCMD_output (nyi);
917       }
918     }
919     else WCMD_print_error ();
920   }
921   else {
922     WCMD_output (nyi);
923   }
924 }
925
926 /****************************************************************************
927  * WCMD_shift
928  *
929  * Shift batch parameters.
930  */
931
932 void WCMD_shift (void) {
933
934   if (context != NULL) context -> shift_count++;
935
936 }
937
938 /****************************************************************************
939  * WCMD_title
940  *
941  * Set the console title
942  */
943 void WCMD_title (char *command) {
944   SetConsoleTitle(command);
945 }
946
947 /****************************************************************************
948  * WCMD_type
949  *
950  * Copy a file to standard output.
951  */
952
953 void WCMD_type (void) {
954
955 HANDLE h;
956 char buffer[512];
957 DWORD count;
958
959   h = CreateFile (param1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
960                 FILE_ATTRIBUTE_NORMAL, NULL);
961   if (h == INVALID_HANDLE_VALUE) {
962     WCMD_print_error ();
963     return;
964   }
965   while (ReadFile (h, buffer, sizeof(buffer), &count, NULL)) {
966     if (count == 0) break;      /* ReadFile reports success on EOF! */
967     buffer[count] = 0;
968     WCMD_output_asis (buffer);
969   }
970   CloseHandle (h);
971 }
972
973 /****************************************************************************
974  * WCMD_verify
975  *
976  * Display verify flag.
977  * FIXME: We don't actually do anything with the verify flag other than toggle
978  * it...
979  */
980
981 void WCMD_verify (char *command) {
982
983 static const char *von = "Verify is ON\n", *voff = "Verify is OFF\n";
984 int count;
985
986   count = strlen(command);
987   if (count == 0) {
988     if (verify_mode) WCMD_output (von);
989     else WCMD_output (voff);
990     return;
991   }
992   if (lstrcmpi(command, "ON") == 0) {
993     verify_mode = 1;
994     return;
995   }
996   else if (lstrcmpi(command, "OFF") == 0) {
997     verify_mode = 0;
998     return;
999   }
1000   else WCMD_output ("Verify must be ON or OFF\n");
1001 }
1002
1003 /****************************************************************************
1004  * WCMD_version
1005  *
1006  * Display version info.
1007  */
1008
1009 void WCMD_version (void) {
1010
1011   WCMD_output (version_string);
1012
1013 }
1014
1015 /****************************************************************************
1016  * WCMD_volume
1017  *
1018  * Display volume info and/or set volume label. Returns 0 if error.
1019  */
1020
1021 int WCMD_volume (int mode, char *path) {
1022
1023 DWORD count, serial;
1024 char string[MAX_PATH], label[MAX_PATH], curdir[MAX_PATH];
1025 BOOL status;
1026
1027   if (lstrlen(path) == 0) {
1028     status = GetCurrentDirectory (sizeof(curdir), curdir);
1029     if (!status) {
1030       WCMD_print_error ();
1031       return 0;
1032     }
1033     status = GetVolumeInformation (NULL, label, sizeof(label), &serial, NULL,
1034         NULL, NULL, 0);
1035   }
1036   else {
1037     if ((path[1] != ':') || (lstrlen(path) != 2)) {
1038       WCMD_output_asis("Syntax Error\n\n");
1039       return 0;
1040     }
1041     wsprintf (curdir, "%s\\", path);
1042     status = GetVolumeInformation (curdir, label, sizeof(label), &serial, NULL,
1043         NULL, NULL, 0);
1044   }
1045   if (!status) {
1046     WCMD_print_error ();
1047     return 0;
1048   }
1049   WCMD_output ("Volume in drive %c is %s\nVolume Serial Number is %04x-%04x\n\n",
1050         curdir[0], label, HIWORD(serial), LOWORD(serial));
1051   if (mode) {
1052     WCMD_output ("Volume label (11 characters, ENTER for none)?");
1053     ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
1054     if (count > 1) {
1055       string[count-1] = '\0';           /* ReadFile output is not null-terminated! */
1056       if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
1057     }
1058     if (lstrlen(path) != 0) {
1059       if (!SetVolumeLabel (curdir, string)) WCMD_print_error ();
1060     }
1061     else {
1062       if (!SetVolumeLabel (NULL, string)) WCMD_print_error ();
1063     }
1064   }
1065   return 1;
1066 }