Removed redundant LANGUAGE statements.
[wine] / programs / wcmd / wcmdmain.c
1 /*
2  * WCMD - Wine-compatible command line interface.
3  *
4  * Copyright (C) 1999 - 2001 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  * FIXME:
23  * - Cannot handle parameters in quotes
24  * - Lots of functionality missing from builtins
25  */
26
27 #include "wcmd.h"
28
29 char *inbuilt[] = {"ATTRIB", "CALL", "CD", "CHDIR", "CLS", "COPY", "CTTY",
30                 "DATE", "DEL", "DIR", "ECHO", "ERASE", "FOR", "GOTO",
31                 "HELP", "IF", "LABEL", "MD", "MKDIR", "MOVE", "PATH", "PAUSE",
32                 "PROMPT", "REM", "REN", "RENAME", "RD", "RMDIR", "SET", "SHIFT",
33                 "TIME", "TITLE", "TYPE", "VERIFY", "VER", "VOL", "EXIT"  };
34
35 HINSTANCE hinst;
36 DWORD errorlevel;
37 int echo_mode = 1, verify_mode = 0;
38 char nyi[] = "Not Yet Implemented\n\n";
39 char newline[] = "\n";
40 char version_string[] = "WCMD Version 0.17\n\n";
41 char anykey[] = "Press any key to continue: ";
42 char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
43 BATCH_CONTEXT *context = NULL;
44
45 /*****************************************************************************
46  * Main entry point. This is a console application so we have a main() not a
47  * winmain().
48  */
49
50 int main (int argc, char *argv[])
51 {
52   char string[1024];
53   char* cmd=NULL;
54   DWORD count;
55   HANDLE h;
56   int opt_c, opt_k, opt_q;
57
58   argv++;
59   opt_c=opt_k=opt_q=0;
60   while (*argv!=NULL)
61   {
62       if (lstrcmpi(*argv,"/c")==0) {
63           opt_c=1;
64           argv++;
65           break;
66       } else if (lstrcmpi(*argv,"/q")==0) {
67           opt_q=1;
68       } else if (lstrcmpi(*argv,"/k")==0) {
69           opt_k=1;
70           argv++;
71           break;
72       } else if (lstrcmpi(*argv,"/t")==0 || lstrcmpi(*argv,"/x")==0 ||
73                  lstrcmpi(*argv,"/y")==0) {
74           /* Ignored for compatibility with Windows */
75       } else {
76           break;
77       }
78       argv++;
79   }
80
81   if (opt_q) {
82     WCMD_echo("OFF");
83   }
84
85   if (opt_c || opt_k) {
86       int len;
87       char** arg;
88       char* p;
89
90       /* Build the command to execute */
91       len = 0;
92       for (arg = argv; *arg; arg++)
93       {
94           int has_space,bcount;
95           char* a;
96
97           has_space=0;
98           bcount=0;
99           a=*arg;
100           while (*a!='\0') {
101               if (*a=='\\') {
102                   bcount++;
103               } else {
104                   if (*a==' ' || *a=='\t') {
105                       has_space=1;
106                   } else if (*a=='"') {
107                       /* doubling of '\' preceeding a '"',
108                        * plus escaping of said '"'
109                        */
110                       len+=2*bcount+1;
111                   }
112                   bcount=0;
113               }
114               a++;
115           }
116           len+=(a-*arg)+1 /* for the separating space */;
117           if (has_space)
118               len+=2; /* for the quotes */
119       }
120
121       cmd = HeapAlloc(GetProcessHeap(), 0, len);
122       if (!cmd)
123           exit(1);
124
125       p = cmd;
126       for (arg = argv; *arg; arg++)
127       {
128           int has_space,has_quote;
129           char* a;
130
131           /* Check for quotes and spaces in this argument */
132           has_space=has_quote=0;
133           a=*arg;
134           while (*a!='\0') {
135               if (*a==' ' || *a=='\t') {
136                   has_space=1;
137                   if (has_quote)
138                       break;
139               } else if (*a=='"') {
140                   has_quote=1;
141                   if (has_space)
142                       break;
143               }
144               a++;
145           }
146
147           /* Now transfer it to the command line */
148           if (has_space)
149               *p++='"';
150           if (has_quote) {
151               int bcount;
152               char* a;
153
154               bcount=0;
155               a=*arg;
156               while (*a!='\0') {
157                   if (*a=='\\') {
158                       *p++=*a;
159                       bcount++;
160                   } else {
161                       if (*a=='"') {
162                           int i;
163
164                           /* Double all the '\\' preceeding this '"', plus one */
165                           for (i=0;i<=bcount;i++)
166                               *p++='\\';
167                           *p++='"';
168                       } else {
169                           *p++=*a;
170                       }
171                       bcount=0;
172                   }
173                   a++;
174               }
175           } else {
176               strcpy(p,*arg);
177               p+=strlen(*arg);
178           }
179           if (has_space)
180               *p++='"';
181           *p++=' ';
182       }
183       if (p > cmd)
184           p--;  /* remove last space */
185       *p = '\0';
186   }
187
188   if (opt_c) {
189       /* If we do a "wcmd /c command", we don't want to allocate a new
190        * console since the command returns immediately. Rather, we use
191        * the currently allocated input and output handles. This allows
192        * us to pipe to and read from the command interpreter.
193        */
194       WCMD_process_command(cmd);
195       HeapFree(GetProcessHeap(), 0, cmd);
196       return 0;
197   }
198
199   SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_LINE_INPUT |
200                  ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT);
201   SetConsoleTitle("Wine Command Prompt");
202
203   if (opt_k) {
204       WCMD_process_command(cmd);
205       HeapFree(GetProcessHeap(), 0, cmd);
206   }
207
208 /*
209  *      If there is an AUTOEXEC.BAT file, try to execute it.
210  */
211
212   GetFullPathName ("\\autoexec.bat", sizeof(string), string, NULL);
213   h = CreateFile (string, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
214   if (h != INVALID_HANDLE_VALUE) {
215     CloseHandle (h);
216 #if 0
217     WCMD_batch_command (string);
218 #endif
219   }
220
221 /*
222  *      Loop forever getting commands and executing them.
223  */
224
225   WCMD_version ();
226   while (TRUE) {
227     WCMD_show_prompt ();
228     ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
229     if (count > 1) {
230       string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
231       if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
232       if (lstrlen (string) != 0) {
233         if (strchr(string,'|') != NULL) {
234           WCMD_pipe (string);
235         }
236         else {
237           WCMD_process_command (string);
238         }
239       }
240     }
241   }
242 }
243
244
245 /*****************************************************************************
246  * Process one command. If the command is EXIT this routine does not return.
247  * We will recurse through here executing batch files.
248  */
249
250
251 void WCMD_process_command (char *command) {
252
253 char cmd[1024];
254 char *p;
255 int status, i;
256 DWORD count;
257 HANDLE old_stdin = 0, old_stdout = 0, h;
258 char *whichcmd;
259
260
261 /*
262  *      Expand up environment variables.
263  */
264
265     status = ExpandEnvironmentStrings (command, cmd, sizeof(cmd));
266     if (!status) {
267       WCMD_print_error ();
268       return;
269     }
270
271 /*
272  *      Changing default drive has to be handled as a special case.
273  */
274
275     if ((cmd[1] == ':') && IsCharAlpha (cmd[0]) && (strlen(cmd) == 2)) {
276       status = SetCurrentDirectory (cmd);
277       if (!status) WCMD_print_error ();
278       return;
279     }
280
281     /* Dont issue newline WCMD_output (newline);           @JED*/
282
283 /*
284  *      Redirect stdin and/or stdout if required.
285  */
286
287     if ((p = strchr(cmd,'<')) != NULL) {
288       h = CreateFile (WCMD_parameter (++p, 0, NULL), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
289                 FILE_ATTRIBUTE_NORMAL, NULL);
290       if (h == INVALID_HANDLE_VALUE) {
291         WCMD_print_error ();
292         return;
293       }
294       old_stdin = GetStdHandle (STD_INPUT_HANDLE);
295       SetStdHandle (STD_INPUT_HANDLE, h);
296     }
297     if ((p = strchr(cmd,'>')) != NULL) {
298       h = CreateFile (WCMD_parameter (++p, 0, NULL), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
299                 FILE_ATTRIBUTE_NORMAL, NULL);
300       if (h == INVALID_HANDLE_VALUE) {
301         WCMD_print_error ();
302         return;
303       }
304       old_stdout = GetStdHandle (STD_OUTPUT_HANDLE);
305       SetStdHandle (STD_OUTPUT_HANDLE, h);
306       *--p = '\0';
307     }
308     if ((p = strchr(cmd,'<')) != NULL) *p = '\0';
309
310 /*
311  * Strip leading whitespaces, and a '@' if supplied
312  */
313     whichcmd = WCMD_strtrim_leading_spaces(cmd);
314     if (whichcmd[0] == '@') whichcmd++;
315
316 /*
317  *      Check if the command entered is internal. If it is, pass the rest of the
318  *      line down to the command. If not try to run a program.
319  */
320
321     count = 0;
322     while (IsCharAlphaNumeric(whichcmd[count])) {
323       count++;
324     }
325     for (i=0; i<=WCMD_EXIT; i++) {
326       if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
327           whichcmd, count, inbuilt[i], -1) == 2) break;
328     }
329     p = WCMD_strtrim_leading_spaces (&whichcmd[count]);
330     WCMD_parse (p, quals, param1, param2);
331     switch (i) {
332
333       case WCMD_ATTRIB:
334         WCMD_setshow_attrib ();
335         break;
336       case WCMD_CALL:
337         WCMD_batch (param1, p, 1);
338         break;
339       case WCMD_CD:
340       case WCMD_CHDIR:
341         WCMD_setshow_default ();
342         break;
343       case WCMD_CLS:
344         WCMD_clear_screen ();
345         break;
346       case WCMD_COPY:
347         WCMD_copy ();
348         break;
349       case WCMD_CTTY:
350         WCMD_change_tty ();
351         break;
352       case WCMD_DATE:
353         WCMD_setshow_date ();
354         break;
355       case WCMD_DEL:
356       case WCMD_ERASE:
357         WCMD_delete (0);
358         break;
359       case WCMD_DIR:
360         WCMD_directory ();
361         break;
362       case WCMD_ECHO:
363         /* Use the unstripped version of the following data - step over the space */
364         /* but only if a parameter follows                                        */
365         if (strlen(&whichcmd[count]) > 0)
366           WCMD_echo(&whichcmd[count+1]);
367         else
368           WCMD_echo(&whichcmd[count]);
369         break;
370       case WCMD_FOR:
371         WCMD_for (p);
372         break;
373       case WCMD_GOTO:
374         WCMD_goto ();
375         break;
376       case WCMD_HELP:
377         WCMD_give_help (p);
378         break;
379       case WCMD_IF:
380         WCMD_if (p);
381         break;
382       case WCMD_LABEL:
383         WCMD_volume (1, p);
384         break;
385       case WCMD_MD:
386       case WCMD_MKDIR:
387         WCMD_create_dir ();
388         break;
389       case WCMD_MOVE:
390         WCMD_move ();
391         break;
392       case WCMD_PATH:
393         WCMD_setshow_path (p);
394         break;
395       case WCMD_PAUSE:
396         WCMD_pause ();
397         break;
398       case WCMD_PROMPT:
399         WCMD_setshow_prompt ();
400         break;
401       case WCMD_REM:
402         break;
403       case WCMD_REN:
404       case WCMD_RENAME:
405         WCMD_rename ();
406         break;
407       case WCMD_RD:
408       case WCMD_RMDIR:
409         WCMD_remove_dir ();
410         break;
411       case WCMD_SET:
412         WCMD_setshow_env (p);
413         break;
414       case WCMD_SHIFT:
415         WCMD_shift ();
416         break;
417       case WCMD_TIME:
418         WCMD_setshow_time ();
419         break;
420       case WCMD_TITLE:
421         if (strlen(&whichcmd[count]) > 0)
422           WCMD_title(&whichcmd[count+1]);
423         break;
424       case WCMD_TYPE:
425         WCMD_type ();
426         break;
427       case WCMD_VER:
428         WCMD_version ();
429         break;
430       case WCMD_VERIFY:
431         WCMD_verify (p);
432         break;
433       case WCMD_VOL:
434         WCMD_volume (0, p);
435         break;
436       case WCMD_EXIT:
437         ExitProcess (0);
438       default:
439         WCMD_run_program (whichcmd);
440     };
441     if (old_stdin) {
442       CloseHandle (GetStdHandle (STD_INPUT_HANDLE));
443       SetStdHandle (STD_INPUT_HANDLE, old_stdin);
444     }
445     if (old_stdout) {
446       CloseHandle (GetStdHandle (STD_OUTPUT_HANDLE));
447       SetStdHandle (STD_OUTPUT_HANDLE, old_stdout);
448     }
449   }
450
451 /******************************************************************************
452  * WCMD_run_program
453  *
454  *      Execute a command line as an external program. If no extension given then
455  *      precedence is given to .BAT files. Must allow recursion.
456  *
457  *      FIXME: Case sensitivity in suffixes!
458  */
459
460 void WCMD_run_program (char *command) {
461
462 STARTUPINFO st;
463 PROCESS_INFORMATION pe;
464 SHFILEINFO psfi;
465 DWORD console;
466 BOOL status;
467 HANDLE h;
468 HINSTANCE hinst;
469 char filetorun[MAX_PATH];
470
471   WCMD_parse (command, quals, param1, param2);  /* Quick way to get the filename */
472   if (!(*param1) && !(*param2))
473     return;
474   if (strpbrk (param1, "\\:") == NULL) {        /* No explicit path given */
475     if ((strchr (param1, '.') == NULL) || (strstr (param1, ".bat") != NULL)) {
476       if (SearchPath (NULL, param1, ".bat", sizeof(filetorun), filetorun, NULL)) {
477         WCMD_batch (filetorun, command, 0);
478         return;
479       }
480     }
481     if ((strchr (param1, '.') == NULL) || (strstr (param1, ".cmd") != NULL)) {
482       if (SearchPath (NULL, param1, ".cmd", sizeof(filetorun), filetorun, NULL)) {
483         WCMD_batch (filetorun, command, 0);
484         return;
485       }
486     }
487   }
488   else {                                        /* Explicit path given */
489     if ((strstr (param1, ".bat") != NULL) || 
490         (strstr (param1, ".cmd") != NULL)) {
491       WCMD_batch (param1, command, 0);
492       return;
493     }
494     if (strchr (param1, '.') == NULL) {
495       strcpy (filetorun, param1);
496       strcat (filetorun, ".bat");
497       h = CreateFile (filetorun, GENERIC_READ, FILE_SHARE_READ,
498                       NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
499       if (h != INVALID_HANDLE_VALUE) {
500         CloseHandle (h);
501         WCMD_batch (param1, command, 0);
502         return;
503       }
504     }
505   }
506
507         /* No batch file found, assume executable */
508
509   hinst = FindExecutable (param1, NULL, filetorun);
510   if ((int)hinst < 32) {
511     WCMD_print_error ();
512     return;
513   }
514   console = SHGetFileInfo (filetorun, 0, &psfi, sizeof(psfi), SHGFI_EXETYPE);
515   ZeroMemory (&st, sizeof(STARTUPINFO));
516   st.cb = sizeof(STARTUPINFO);
517   status = CreateProcess (NULL, command, NULL, NULL, FALSE,
518                  0, NULL, NULL, &st, &pe);
519   if (!status) {
520     WCMD_print_error ();
521     return;
522   }
523   if (!console) errorlevel = 0;
524   else
525   {
526       if (!HIWORD(console)) WaitForSingleObject (pe.hProcess, INFINITE);
527       GetExitCodeProcess (pe.hProcess, &errorlevel);
528       if (errorlevel == STILL_ACTIVE) errorlevel = 0;
529   }
530 }
531
532 /******************************************************************************
533  * WCMD_show_prompt
534  *
535  *      Display the prompt on STDout
536  *
537  */
538
539 void WCMD_show_prompt (void) {
540
541 int status;
542 char out_string[MAX_PATH], curdir[MAX_PATH], prompt_string[MAX_PATH];
543 char *p, *q;
544
545   status = GetEnvironmentVariable ("PROMPT", prompt_string, sizeof(prompt_string));
546   if ((status == 0) || (status > sizeof(prompt_string))) {
547     lstrcpy (prompt_string, "$P$G");
548   }
549   p = prompt_string;
550   q = out_string;
551   *q = '\0';
552   while (*p != '\0') {
553     if (*p != '$') {
554       *q++ = *p++;
555       *q = '\0';
556     }
557     else {
558       p++;
559       switch (toupper(*p)) {
560         case '$':
561           *q++ = '$';
562           break;
563         case 'B':
564           *q++ = '|';
565           break;
566         case 'D':
567           GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, NULL, q, MAX_PATH);
568           while (*q) q++;
569           break;
570         case 'E':
571           *q++ = '\E';
572           break;
573         case 'G':
574           *q++ = '>';
575           break;
576         case 'L':
577           *q++ = '<';
578           break;
579         case 'N':
580           status = GetCurrentDirectory (sizeof(curdir), curdir);
581           if (status) {
582             *q++ = curdir[0];
583           }
584           break;
585         case 'P':
586           status = GetCurrentDirectory (sizeof(curdir), curdir);
587           if (status) {
588             lstrcat (q, curdir);
589             while (*q) q++;
590           }
591           break;
592         case 'Q':
593           *q++ = '=';
594           break;
595         case 'T':
596           GetTimeFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL, q, MAX_PATH);
597           while (*q) q++;
598           break;
599        case 'V':
600            lstrcat (q, version_string);
601            while (*q) q++;
602          break;
603         case '_':
604           *q++ = '\n';
605           break;
606       }
607       p++;
608       *q = '\0';
609     }
610   }
611   WCMD_output (out_string);
612 }
613
614 /****************************************************************************
615  * WCMD_print_error
616  *
617  * Print the message for GetLastError
618  */
619
620 void WCMD_print_error (void) {
621 LPVOID lpMsgBuf;
622 DWORD error_code;
623 int status;
624
625   error_code = GetLastError ();
626   status = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
627                         NULL, error_code, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
628   if (!status) {
629     WCMD_output ("FIXME: Cannot display message for error %d, status %d\n",
630                         error_code, GetLastError());
631     return;
632   }
633   WCMD_output (lpMsgBuf);
634   LocalFree ((HLOCAL)lpMsgBuf);
635   WCMD_output (newline);
636   return;
637 }
638
639 /*******************************************************************
640  * WCMD_parse - parse a command into parameters and qualifiers.
641  *
642  *      On exit, all qualifiers are concatenated into q, the first string
643  *      not beginning with "/" is in p1 and the
644  *      second in p2. Any subsequent non-qualifier strings are lost.
645  *      Parameters in quotes are handled.
646  */
647
648 void WCMD_parse (char *s, char *q, char *p1, char *p2) {
649
650 int p = 0;
651
652   *q = *p1 = *p2 = '\0';
653   while (TRUE) {
654     switch (*s) {
655       case '/':
656         *q++ = *s++;
657         while ((*s != '\0') && (*s != ' ') && *s != '/') {
658           *q++ = toupper (*s++);
659         }
660         *q = '\0';
661         break;
662       case ' ':
663         s++;
664         break;
665       case '"':
666         s++;
667         while ((*s != '\0') && (*s != '"')) {
668           if (p == 0) *p1++ = *s++;
669           else if (p == 1) *p2++ = *s++;
670           else s++;
671         }
672         if (p == 0) *p1 = '\0';
673         if (p == 1) *p2 = '\0';
674         p++;
675         if (*s == '"') s++;
676         break;
677       case '\0':
678         return;
679       default:
680         while ((*s != '\0') && (*s != ' ')) {
681           if (p == 0) *p1++ = *s++;
682           else if (p == 1) *p2++ = *s++;
683           else s++;
684         }
685         if (p == 0) *p1 = '\0';
686         if (p == 1) *p2 = '\0';
687         p++;
688     }
689   }
690 }
691
692 /*******************************************************************
693  * WCMD_output - send output to current standard output device.
694  *
695  */
696
697 void WCMD_output (char *format, ...) {
698
699 va_list ap;
700 char string[1024];
701
702   va_start(ap,format);
703   vsprintf (string, format, ap);
704   va_end(ap);
705   WCMD_output_asis(string);
706 }
707
708
709 static int line_count;
710 static int max_height;
711 static BOOL paged_mode;
712
713 void WCMD_enter_paged_mode(void)
714 {
715 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
716
717   if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo))
718     max_height = consoleInfo.dwSize.Y;
719   else
720     max_height = 25;
721   paged_mode = TRUE;
722   line_count = 5; /* keep 5 lines from previous output */
723 }
724
725 void WCMD_leave_paged_mode(void)
726 {
727   paged_mode = FALSE;
728 }
729
730 /*******************************************************************
731  * WCMD_output_asis - send output to current standard output device.
732  *        without formatting eg. when message contains '%'
733  */
734
735 void WCMD_output_asis (char *message) {
736   DWORD count;
737   char* ptr;
738   char string[1024];
739
740   if (paged_mode) {
741     do {
742       if ((ptr = strchr(message, '\n')) != NULL) ptr++;
743       WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), message, 
744                  (ptr) ? ptr - message : lstrlen(message), &count, NULL);
745       if (ptr) {
746         if (++line_count >= max_height - 1) {
747           line_count = 0;
748           WCMD_output_asis (anykey);
749           ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
750         }
751       }
752     } while ((message = ptr) != NULL);
753   } else {
754       WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), message, lstrlen(message), &count, NULL);
755   }
756 }
757
758
759 /***************************************************************************
760  * WCMD_strtrim_leading_spaces
761  *
762  *      Remove leading spaces from a string. Return a pointer to the first
763  *      non-space character. Does not modify the input string
764  */
765
766 char *WCMD_strtrim_leading_spaces (char *string) {
767
768 char *ptr;
769
770   ptr = string;
771   while (*ptr == ' ') ptr++;
772   return ptr;
773 }
774
775 /*************************************************************************
776  * WCMD_strtrim_trailing_spaces
777  *
778  *      Remove trailing spaces from a string. This routine modifies the input
779  *      string by placing a null after the last non-space character
780  */
781
782 void WCMD_strtrim_trailing_spaces (char *string) {
783
784 char *ptr;
785
786   ptr = string + lstrlen (string) - 1;
787   while ((*ptr == ' ') && (ptr >= string)) {
788     *ptr = '\0';
789     ptr--;
790   }
791 }
792
793 /*************************************************************************
794  * WCMD_pipe
795  *
796  *      Handle pipes within a command - the DOS way using temporary files.
797  */
798
799 void WCMD_pipe (char *command) {
800
801 char *p;
802 char temp_path[MAX_PATH], temp_file[MAX_PATH], temp_file2[MAX_PATH], temp_cmd[1024];
803
804   GetTempPath (sizeof(temp_path), temp_path);
805   GetTempFileName (temp_path, "WCMD", 0, temp_file);
806   p = strchr(command, '|');
807   *p++ = '\0';
808   wsprintf (temp_cmd, "%s > %s", command, temp_file);
809   WCMD_process_command (temp_cmd);
810   command = p;
811   while ((p = strchr(command, '|'))) {
812     *p++ = '\0';
813     GetTempFileName (temp_path, "WCMD", 0, temp_file2);
814     wsprintf (temp_cmd, "%s < %s > %s", command, temp_file, temp_file2);
815     WCMD_process_command (temp_cmd);
816     DeleteFile (temp_file);
817     lstrcpy (temp_file, temp_file2);
818     command = p;
819   }
820   wsprintf (temp_cmd, "%s < %s", command, temp_file);
821   WCMD_process_command (temp_cmd);
822   DeleteFile (temp_file);
823 }