wined3d: Implement GetCreationParameters (from a patch by Al Tobey).
[wine/multimedia.git] / programs / wcmd / wcmdmain.c
blob644ebc10764d9aedd410d7a2f03b65e48f0a52ae
1 /*
2 * WCMD - Wine-compatible command line interface.
4 * Copyright (C) 1999 - 2001 D A Pickles
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * FIXME:
23 * - Cannot handle parameters in quotes
24 * - Lots of functionality missing from builtins
27 #include "wcmd.h"
29 const char * const 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",
34 "ENDLOCAL", "SETLOCAL", "EXIT" };
36 HINSTANCE hinst;
37 DWORD errorlevel;
38 int echo_mode = 1, verify_mode = 0;
39 const char nyi[] = "Not Yet Implemented\n\n";
40 const char newline[] = "\n";
41 const char version_string[] = "WCMD Version 0.17\n\n";
42 const char anykey[] = "Press Return key to continue: ";
43 char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
44 BATCH_CONTEXT *context = NULL;
45 static HANDLE old_stdin = INVALID_HANDLE_VALUE, old_stdout = INVALID_HANDLE_VALUE;
47 /*****************************************************************************
48 * Main entry point. This is a console application so we have a main() not a
49 * winmain().
52 int main (int argc, char *argv[])
54 char string[1024];
55 char* cmd=NULL;
56 DWORD count;
57 HANDLE h;
58 int opt_c, opt_k, opt_q;
60 opt_c=opt_k=opt_q=0;
61 while (*argv!=NULL)
63 if (lstrcmpi(*argv,"/c")==0) {
64 opt_c=1;
65 argv++;
66 break;
67 } else if (lstrcmpi(*argv,"/q")==0) {
68 opt_q=1;
69 } else if (lstrcmpi(*argv,"/k")==0) {
70 opt_k=1;
71 argv++;
72 break;
73 } else if (lstrcmpi(*argv,"/t")==0 || lstrcmpi(*argv,"/x")==0 ||
74 lstrcmpi(*argv,"/y")==0) {
75 /* Ignored for compatibility with Windows */
77 argv++;
80 if (opt_q) {
81 WCMD_echo("OFF");
84 if (opt_c || opt_k) {
85 int len;
86 char** arg;
87 char* p;
89 /* Build the command to execute */
90 len = 0;
91 for (arg = argv; *arg; arg++)
93 int has_space,bcount;
94 char* a;
96 has_space=0;
97 bcount=0;
98 a=*arg;
99 if( !*a ) has_space=1;
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 '\' preceding a '"',
108 * plus escaping of said '"'
110 len+=2*bcount+1;
112 bcount=0;
114 a++;
116 len+=(a-*arg)+1 /* for the separating space */;
117 if (has_space)
118 len+=2; /* for the quotes */
121 cmd = HeapAlloc(GetProcessHeap(), 0, len);
122 if (!cmd)
123 exit(1);
125 p = cmd;
126 for (arg = argv; *arg; arg++)
128 int has_space,has_quote;
129 char* a;
131 /* Check for quotes and spaces in this argument */
132 has_space=has_quote=0;
133 a=*arg;
134 if( !*a ) has_space=1;
135 while (*a!='\0') {
136 if (*a==' ' || *a=='\t') {
137 has_space=1;
138 if (has_quote)
139 break;
140 } else if (*a=='"') {
141 has_quote=1;
142 if (has_space)
143 break;
145 a++;
148 /* Now transfer it to the command line */
149 if (has_space)
150 *p++='"';
151 if (has_quote) {
152 int bcount;
153 char* a;
155 bcount=0;
156 a=*arg;
157 while (*a!='\0') {
158 if (*a=='\\') {
159 *p++=*a;
160 bcount++;
161 } else {
162 if (*a=='"') {
163 int i;
165 /* Double all the '\\' preceding this '"', plus one */
166 for (i=0;i<=bcount;i++)
167 *p++='\\';
168 *p++='"';
169 } else {
170 *p++=*a;
172 bcount=0;
174 a++;
176 } else {
177 strcpy(p,*arg);
178 p+=strlen(*arg);
180 if (has_space)
181 *p++='"';
182 *p++=' ';
184 if (p > cmd)
185 p--; /* remove last space */
186 *p = '\0';
189 if (opt_c) {
190 /* If we do a "wcmd /c command", we don't want to allocate a new
191 * console since the command returns immediately. Rather, we use
192 * the currently allocated input and output handles. This allows
193 * us to pipe to and read from the command interpreter.
195 if (strchr(cmd,'|') != NULL)
196 WCMD_pipe(cmd);
197 else
198 WCMD_process_command(cmd);
199 HeapFree(GetProcessHeap(), 0, cmd);
200 return 0;
203 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_LINE_INPUT |
204 ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT);
205 SetConsoleTitle("Wine Command Prompt");
207 if (opt_k) {
208 WCMD_process_command(cmd);
209 HeapFree(GetProcessHeap(), 0, cmd);
213 * If there is an AUTOEXEC.BAT file, try to execute it.
216 GetFullPathName ("\\autoexec.bat", sizeof(string), string, NULL);
217 h = CreateFile (string, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
218 if (h != INVALID_HANDLE_VALUE) {
219 CloseHandle (h);
220 #if 0
221 WCMD_batch_command (string);
222 #endif
226 * Loop forever getting commands and executing them.
229 WCMD_version ();
230 while (TRUE) {
231 WCMD_show_prompt ();
232 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
233 if (count > 1) {
234 string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
235 if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
236 if (lstrlen (string) != 0) {
237 if (strchr(string,'|') != NULL) {
238 WCMD_pipe (string);
240 else {
241 WCMD_process_command (string);
249 /*****************************************************************************
250 * Process one command. If the command is EXIT this routine does not return.
251 * We will recurse through here executing batch files.
255 void WCMD_process_command (char *command)
257 char *cmd, *p;
258 int status, i, len;
259 DWORD count, creationDisposition;
260 HANDLE h;
261 char *whichcmd;
262 SECURITY_ATTRIBUTES sa;
265 * Expand up environment variables.
267 len = ExpandEnvironmentStrings (command, NULL, 0);
268 cmd = HeapAlloc( GetProcessHeap(), 0, len );
269 status = ExpandEnvironmentStrings (command, cmd, len);
270 if (!status) {
271 WCMD_print_error ();
272 HeapFree( GetProcessHeap(), 0, cmd );
273 return;
277 * Changing default drive has to be handled as a special case.
280 if ((cmd[1] == ':') && IsCharAlpha (cmd[0]) && (strlen(cmd) == 2)) {
281 status = SetCurrentDirectory (cmd);
282 if (!status) WCMD_print_error ();
283 HeapFree( GetProcessHeap(), 0, cmd );
284 return;
287 /* Don't issue newline WCMD_output (newline); @JED*/
289 sa.nLength = sizeof(sa);
290 sa.lpSecurityDescriptor = NULL;
291 sa.bInheritHandle = TRUE;
293 * Redirect stdin and/or stdout if required.
296 if ((p = strchr(cmd,'<')) != NULL) {
297 h = CreateFile (WCMD_parameter (++p, 0, NULL), GENERIC_READ, FILE_SHARE_READ, &sa, OPEN_EXISTING,
298 FILE_ATTRIBUTE_NORMAL, NULL);
299 if (h == INVALID_HANDLE_VALUE) {
300 WCMD_print_error ();
301 HeapFree( GetProcessHeap(), 0, cmd );
302 return;
304 old_stdin = GetStdHandle (STD_INPUT_HANDLE);
305 SetStdHandle (STD_INPUT_HANDLE, h);
307 if ((p = strchr(cmd,'>')) != NULL) {
308 *p++ = '\0';
309 if ('>' == *p) {
310 creationDisposition = OPEN_ALWAYS;
311 p++;
313 else {
314 creationDisposition = CREATE_ALWAYS;
316 h = CreateFile (WCMD_parameter (p, 0, NULL), GENERIC_WRITE, 0, &sa, creationDisposition,
317 FILE_ATTRIBUTE_NORMAL, NULL);
318 if (h == INVALID_HANDLE_VALUE) {
319 WCMD_print_error ();
320 HeapFree( GetProcessHeap(), 0, cmd );
321 return;
323 if (SetFilePointer (h, 0, NULL, FILE_END) ==
324 INVALID_SET_FILE_POINTER) {
325 WCMD_print_error ();
327 old_stdout = GetStdHandle (STD_OUTPUT_HANDLE);
328 SetStdHandle (STD_OUTPUT_HANDLE, h);
330 if ((p = strchr(cmd,'<')) != NULL) *p = '\0';
333 * Strip leading whitespaces, and a '@' if supplied
335 whichcmd = WCMD_strtrim_leading_spaces(cmd);
336 if (whichcmd[0] == '@') whichcmd++;
339 * Check if the command entered is internal. If it is, pass the rest of the
340 * line down to the command. If not try to run a program.
343 count = 0;
344 while (IsCharAlphaNumeric(whichcmd[count])) {
345 count++;
347 for (i=0; i<=WCMD_EXIT; i++) {
348 if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
349 whichcmd, count, inbuilt[i], -1) == 2) break;
351 p = WCMD_strtrim_leading_spaces (&whichcmd[count]);
352 WCMD_parse (p, quals, param1, param2);
353 switch (i) {
355 case WCMD_ATTRIB:
356 WCMD_setshow_attrib ();
357 break;
358 case WCMD_CALL:
359 WCMD_run_program (p, 1);
360 break;
361 case WCMD_CD:
362 case WCMD_CHDIR:
363 WCMD_setshow_default ();
364 break;
365 case WCMD_CLS:
366 WCMD_clear_screen ();
367 break;
368 case WCMD_COPY:
369 WCMD_copy ();
370 break;
371 case WCMD_CTTY:
372 WCMD_change_tty ();
373 break;
374 case WCMD_DATE:
375 WCMD_setshow_date ();
376 break;
377 case WCMD_DEL:
378 case WCMD_ERASE:
379 WCMD_delete (0);
380 break;
381 case WCMD_DIR:
382 WCMD_directory ();
383 break;
384 case WCMD_ECHO:
385 WCMD_echo(&whichcmd[count]);
386 break;
387 case WCMD_FOR:
388 WCMD_for (p);
389 break;
390 case WCMD_GOTO:
391 WCMD_goto ();
392 break;
393 case WCMD_HELP:
394 WCMD_give_help (p);
395 break;
396 case WCMD_IF:
397 WCMD_if (p);
398 break;
399 case WCMD_LABEL:
400 WCMD_volume (1, p);
401 break;
402 case WCMD_MD:
403 case WCMD_MKDIR:
404 WCMD_create_dir ();
405 break;
406 case WCMD_MOVE:
407 WCMD_move ();
408 break;
409 case WCMD_PATH:
410 WCMD_setshow_path (p);
411 break;
412 case WCMD_PAUSE:
413 WCMD_pause ();
414 break;
415 case WCMD_PROMPT:
416 WCMD_setshow_prompt ();
417 break;
418 case WCMD_REM:
419 break;
420 case WCMD_REN:
421 case WCMD_RENAME:
422 WCMD_rename ();
423 break;
424 case WCMD_RD:
425 case WCMD_RMDIR:
426 WCMD_remove_dir ();
427 break;
428 case WCMD_SETLOCAL:
429 WCMD_setlocal(p);
430 break;
431 case WCMD_ENDLOCAL:
432 WCMD_endlocal();
433 break;
434 case WCMD_SET:
435 WCMD_setshow_env (p);
436 break;
437 case WCMD_SHIFT:
438 WCMD_shift ();
439 break;
440 case WCMD_TIME:
441 WCMD_setshow_time ();
442 break;
443 case WCMD_TITLE:
444 if (strlen(&whichcmd[count]) > 0)
445 WCMD_title(&whichcmd[count+1]);
446 break;
447 case WCMD_TYPE:
448 WCMD_type ();
449 break;
450 case WCMD_VER:
451 WCMD_version ();
452 break;
453 case WCMD_VERIFY:
454 WCMD_verify (p);
455 break;
456 case WCMD_VOL:
457 WCMD_volume (0, p);
458 break;
459 case WCMD_EXIT:
460 ExitProcess (0);
461 default:
462 WCMD_run_program (whichcmd, 0);
464 HeapFree( GetProcessHeap(), 0, cmd );
465 if (old_stdin != INVALID_HANDLE_VALUE) {
466 CloseHandle (GetStdHandle (STD_INPUT_HANDLE));
467 SetStdHandle (STD_INPUT_HANDLE, old_stdin);
468 old_stdin = INVALID_HANDLE_VALUE;
470 if (old_stdout != INVALID_HANDLE_VALUE) {
471 CloseHandle (GetStdHandle (STD_OUTPUT_HANDLE));
472 SetStdHandle (STD_OUTPUT_HANDLE, old_stdout);
473 old_stdout = INVALID_HANDLE_VALUE;
477 static void init_msvcrt_io_block(STARTUPINFO* st)
479 STARTUPINFO st_p;
480 /* fetch the parent MSVCRT info block if any, so that the child can use the
481 * same handles as its grand-father
483 st_p.cb = sizeof(STARTUPINFO);
484 GetStartupInfo(&st_p);
485 st->cbReserved2 = st_p.cbReserved2;
486 st->lpReserved2 = st_p.lpReserved2;
487 if (st_p.cbReserved2 && st_p.lpReserved2 &&
488 (old_stdin != INVALID_HANDLE_VALUE || old_stdout != INVALID_HANDLE_VALUE))
490 /* Override the entries for fd 0,1,2 if we happened
491 * to change those std handles (this depends on the way wcmd sets
492 * it's new input & output handles)
494 size_t sz = max(sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * 3, st_p.cbReserved2);
495 BYTE* ptr = HeapAlloc(GetProcessHeap(), 0, sz);
496 if (ptr)
498 unsigned num = *(unsigned*)st_p.lpReserved2;
499 char* flags = (char*)(ptr + sizeof(unsigned));
500 HANDLE* handles = (HANDLE*)(flags + num * sizeof(char));
502 memcpy(ptr, st_p.lpReserved2, st_p.cbReserved2);
503 st->cbReserved2 = sz;
504 st->lpReserved2 = ptr;
506 #define WX_OPEN 0x01 /* see dlls/msvcrt/file.c */
507 if (num <= 0 || (flags[0] & WX_OPEN))
509 handles[0] = GetStdHandle(STD_INPUT_HANDLE);
510 flags[0] |= WX_OPEN;
512 if (num <= 1 || (flags[1] & WX_OPEN))
514 handles[1] = GetStdHandle(STD_OUTPUT_HANDLE);
515 flags[1] |= WX_OPEN;
517 if (num <= 2 || (flags[2] & WX_OPEN))
519 handles[2] = GetStdHandle(STD_ERROR_HANDLE);
520 flags[2] |= WX_OPEN;
522 #undef WX_OPEN
527 /******************************************************************************
528 * WCMD_run_program
530 * Execute a command line as an external program. If no extension given then
531 * precedence is given to .BAT files. Must allow recursion.
533 * called is 1 if the program was invoked with a CALL command - removed
534 * from command -. It is only used for batch programs.
536 * FIXME: Case sensitivity in suffixes!
539 void WCMD_run_program (char *command, int called) {
541 STARTUPINFO st;
542 PROCESS_INFORMATION pe;
543 SHFILEINFO psfi;
544 DWORD console;
545 BOOL status;
546 HANDLE h;
547 HINSTANCE hinst;
548 char filetorun[MAX_PATH];
550 WCMD_parse (command, quals, param1, param2); /* Quick way to get the filename */
551 if (!(*param1) && !(*param2))
552 return;
553 if (strpbrk (param1, "/\\:") == NULL) { /* No explicit path given */
554 char *ext = strrchr( param1, '.' );
555 if (!ext || !strcasecmp( ext, ".bat"))
557 if (SearchPath (NULL, param1, ".bat", sizeof(filetorun), filetorun, NULL)) {
558 WCMD_batch (filetorun, command, called);
559 return;
562 if (!ext || !strcasecmp( ext, ".cmd"))
564 if (SearchPath (NULL, param1, ".cmd", sizeof(filetorun), filetorun, NULL)) {
565 WCMD_batch (filetorun, command, called);
566 return;
570 else { /* Explicit path given */
571 char *ext = strrchr( param1, '.' );
572 if (ext && (!strcasecmp( ext, ".bat" ) || !strcasecmp( ext, ".cmd" )))
574 WCMD_batch (param1, command, called);
575 return;
578 if (ext && strpbrk( ext, "/\\:" )) ext = NULL;
579 if (!ext)
581 strcpy (filetorun, param1);
582 strcat (filetorun, ".bat");
583 h = CreateFile (filetorun, GENERIC_READ, FILE_SHARE_READ,
584 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
585 if (h != INVALID_HANDLE_VALUE) {
586 CloseHandle (h);
587 WCMD_batch (param1, command, called);
588 return;
593 /* No batch file found, assume executable */
595 hinst = FindExecutable (param1, NULL, filetorun);
596 if ((int)hinst < 32) {
597 WCMD_print_error ();
598 return;
600 console = SHGetFileInfo (filetorun, 0, &psfi, sizeof(psfi), SHGFI_EXETYPE);
601 ZeroMemory (&st, sizeof(STARTUPINFO));
602 st.cb = sizeof(STARTUPINFO);
603 init_msvcrt_io_block(&st);
605 status = CreateProcess (NULL, command, NULL, NULL, TRUE,
606 0, NULL, NULL, &st, &pe);
607 if (!status) {
608 WCMD_print_error ();
609 return;
611 if (!console) errorlevel = 0;
612 else
614 if (!HIWORD(console)) WaitForSingleObject (pe.hProcess, INFINITE);
615 GetExitCodeProcess (pe.hProcess, &errorlevel);
616 if (errorlevel == STILL_ACTIVE) errorlevel = 0;
618 CloseHandle(pe.hProcess);
619 CloseHandle(pe.hThread);
622 /******************************************************************************
623 * WCMD_show_prompt
625 * Display the prompt on STDout
629 void WCMD_show_prompt (void) {
631 int status;
632 char out_string[MAX_PATH], curdir[MAX_PATH], prompt_string[MAX_PATH];
633 char *p, *q;
635 status = GetEnvironmentVariable ("PROMPT", prompt_string, sizeof(prompt_string));
636 if ((status == 0) || (status > sizeof(prompt_string))) {
637 lstrcpy (prompt_string, "$P$G");
639 p = prompt_string;
640 q = out_string;
641 *q = '\0';
642 while (*p != '\0') {
643 if (*p != '$') {
644 *q++ = *p++;
645 *q = '\0';
647 else {
648 p++;
649 switch (toupper(*p)) {
650 case '$':
651 *q++ = '$';
652 break;
653 case 'B':
654 *q++ = '|';
655 break;
656 case 'D':
657 GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, NULL, q, MAX_PATH);
658 while (*q) q++;
659 break;
660 case 'E':
661 *q++ = '\E';
662 break;
663 case 'G':
664 *q++ = '>';
665 break;
666 case 'L':
667 *q++ = '<';
668 break;
669 case 'N':
670 status = GetCurrentDirectory (sizeof(curdir), curdir);
671 if (status) {
672 *q++ = curdir[0];
674 break;
675 case 'P':
676 status = GetCurrentDirectory (sizeof(curdir), curdir);
677 if (status) {
678 lstrcat (q, curdir);
679 while (*q) q++;
681 break;
682 case 'Q':
683 *q++ = '=';
684 break;
685 case 'T':
686 GetTimeFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL, q, MAX_PATH);
687 while (*q) q++;
688 break;
689 case 'V':
690 lstrcat (q, version_string);
691 while (*q) q++;
692 break;
693 case '_':
694 *q++ = '\n';
695 break;
697 p++;
698 *q = '\0';
701 WCMD_output_asis (out_string);
704 /****************************************************************************
705 * WCMD_print_error
707 * Print the message for GetLastError
710 void WCMD_print_error (void) {
711 LPVOID lpMsgBuf;
712 DWORD error_code;
713 int status;
715 error_code = GetLastError ();
716 status = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
717 NULL, error_code, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
718 if (!status) {
719 WCMD_output ("FIXME: Cannot display message for error %d, status %d\n",
720 error_code, GetLastError());
721 return;
723 WCMD_output_asis (lpMsgBuf);
724 LocalFree ((HLOCAL)lpMsgBuf);
725 WCMD_output_asis (newline);
726 return;
729 /*******************************************************************
730 * WCMD_parse - parse a command into parameters and qualifiers.
732 * On exit, all qualifiers are concatenated into q, the first string
733 * not beginning with "/" is in p1 and the
734 * second in p2. Any subsequent non-qualifier strings are lost.
735 * Parameters in quotes are handled.
738 void WCMD_parse (char *s, char *q, char *p1, char *p2) {
740 int p = 0;
742 *q = *p1 = *p2 = '\0';
743 while (TRUE) {
744 switch (*s) {
745 case '/':
746 *q++ = *s++;
747 while ((*s != '\0') && (*s != ' ') && *s != '/') {
748 *q++ = toupper (*s++);
750 *q = '\0';
751 break;
752 case ' ':
753 case '\t':
754 s++;
755 break;
756 case '"':
757 s++;
758 while ((*s != '\0') && (*s != '"')) {
759 if (p == 0) *p1++ = *s++;
760 else if (p == 1) *p2++ = *s++;
761 else s++;
763 if (p == 0) *p1 = '\0';
764 if (p == 1) *p2 = '\0';
765 p++;
766 if (*s == '"') s++;
767 break;
768 case '\0':
769 return;
770 default:
771 while ((*s != '\0') && (*s != ' ') && (*s != '\t')) {
772 if (p == 0) *p1++ = *s++;
773 else if (p == 1) *p2++ = *s++;
774 else s++;
776 if (p == 0) *p1 = '\0';
777 if (p == 1) *p2 = '\0';
778 p++;
783 /*******************************************************************
784 * WCMD_output - send output to current standard output device.
788 void WCMD_output (const char *format, ...) {
790 va_list ap;
791 char string[1024];
792 int ret;
794 va_start(ap,format);
795 ret = vsnprintf (string, sizeof( string), format, ap);
796 va_end(ap);
797 if( ret >= sizeof( string)) {
798 WCMD_output_asis("ERR: output truncated in WCMD_output\n" );
799 string[sizeof( string) -1] = '\0';
801 WCMD_output_asis(string);
805 static int line_count;
806 static int max_height;
807 static BOOL paged_mode;
809 void WCMD_enter_paged_mode(void)
811 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
813 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo))
814 max_height = consoleInfo.dwSize.Y;
815 else
816 max_height = 25;
817 paged_mode = TRUE;
818 line_count = 5; /* keep 5 lines from previous output */
821 void WCMD_leave_paged_mode(void)
823 paged_mode = FALSE;
826 /*******************************************************************
827 * WCMD_output_asis - send output to current standard output device.
828 * without formatting eg. when message contains '%'
831 void WCMD_output_asis (const char *message) {
832 DWORD count;
833 char* ptr;
834 char string[1024];
836 if (paged_mode) {
837 do {
838 if ((ptr = strchr(message, '\n')) != NULL) ptr++;
839 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), message,
840 (ptr) ? ptr - message : lstrlen(message), &count, NULL);
841 if (ptr) {
842 if (++line_count >= max_height - 1) {
843 line_count = 0;
844 WCMD_output_asis (anykey);
845 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
848 } while ((message = ptr) != NULL);
849 } else {
850 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), message, lstrlen(message), &count, NULL);
855 /***************************************************************************
856 * WCMD_strtrim_leading_spaces
858 * Remove leading spaces from a string. Return a pointer to the first
859 * non-space character. Does not modify the input string
862 char *WCMD_strtrim_leading_spaces (char *string) {
864 char *ptr;
866 ptr = string;
867 while (*ptr == ' ') ptr++;
868 return ptr;
871 /*************************************************************************
872 * WCMD_strtrim_trailing_spaces
874 * Remove trailing spaces from a string. This routine modifies the input
875 * string by placing a null after the last non-space character
878 void WCMD_strtrim_trailing_spaces (char *string) {
880 char *ptr;
882 ptr = string + lstrlen (string) - 1;
883 while ((*ptr == ' ') && (ptr >= string)) {
884 *ptr = '\0';
885 ptr--;
889 /*************************************************************************
890 * WCMD_pipe
892 * Handle pipes within a command - the DOS way using temporary files.
895 void WCMD_pipe (char *command) {
897 char *p;
898 char temp_path[MAX_PATH], temp_file[MAX_PATH], temp_file2[MAX_PATH], temp_cmd[1024];
900 GetTempPath (sizeof(temp_path), temp_path);
901 GetTempFileName (temp_path, "WCMD", 0, temp_file);
902 p = strchr(command, '|');
903 *p++ = '\0';
904 wsprintf (temp_cmd, "%s > %s", command, temp_file);
905 WCMD_process_command (temp_cmd);
906 command = p;
907 while ((p = strchr(command, '|'))) {
908 *p++ = '\0';
909 GetTempFileName (temp_path, "WCMD", 0, temp_file2);
910 wsprintf (temp_cmd, "%s < %s > %s", command, temp_file, temp_file2);
911 WCMD_process_command (temp_cmd);
912 DeleteFile (temp_file);
913 lstrcpy (temp_file, temp_file2);
914 command = p;
916 wsprintf (temp_cmd, "%s < %s", command, temp_file);
917 WCMD_process_command (temp_cmd);
918 DeleteFile (temp_file);