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