Pause command interpreter while executing a console-mode app.
[wine.git] / programs / wcmd / wcmdmain.c
blob9e9eba5efd635ad4842727593abe48c92d238af0
1 /*
2 * WCMD - Wine-compatible command line interface.
4 * (C) 1999 D A Pickles
5 */
7 /*
8 * FIXME:
9 * - No support for pipes
10 * - 32-bit limit on file sizes in DIR command
11 * - Cannot handle parameters in quotes
12 * - Lots of functionality missing from builtins
15 #include "wcmd.h"
17 char *inbuilt[] = {"ATTRIB", "CALL", "CD", "CHDIR", "CLS", "COPY", "CTTY",
18 "DATE", "DEL", "DIR", "ECHO", "ERASE", "FOR", "GOTO",
19 "HELP", "IF", "LABEL", "MD", "MKDIR", "MOVE", "PATH", "PAUSE",
20 "PROMPT", "REM", "REN", "RENAME", "RD", "RMDIR", "SET", "SHIFT",
21 "TIME", "TYPE", "VERIFY", "VER", "VOL", "EXIT"};
23 HINSTANCE hinst;
24 DWORD errorlevel;
25 int echo_mode = 1, verify_mode = 0;
26 char nyi[] = "Not Yet Implemented\n\n";
27 char newline[] = "\n";
28 char version_string[] = "WCMD Version 0.15\n\n";
29 char anykey[] = "Press any key to continue: ";
30 char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
31 BATCH_CONTEXT *context = NULL;
33 /*****************************************************************************
34 * Main entry point. This is a console application so we have a main() not a
35 * winmain().
39 int wine_main (int argc, char *argv[]) {
41 char string[1024], args[MAX_PATH], param[MAX_PATH];
42 int status, i;
43 DWORD count;
44 HANDLE h;
46 args[0] = param[0] = '\0';
47 if (argc > 1) {
48 for (i=1; i<argc; i++) {
49 if (argv[i][0] == '/') {
50 strcat (args, argv[i]);
52 else {
53 strcat (param, argv[i]);
54 strcat (param, " ");
60 * Allocate a console and set it up.
63 status = FreeConsole ();
64 if (!status) WCMD_print_error();
65 status = AllocConsole();
66 if (!status) WCMD_print_error();
67 SetConsoleMode (GetStdHandle(STD_INPUT_HANDLE), ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT |
68 ENABLE_PROCESSED_INPUT);
71 * Execute any command-line options.
74 if (strstr(args, "/q") != NULL) {
75 WCMD_echo ("OFF");
78 if (strstr(args, "/c") != NULL) {
79 WCMD_process_command (param);
80 return 0;
83 if (strstr(args, "/k") != NULL) {
84 WCMD_process_command (param);
88 * If there is an AUTOEXEC.BAT file, try to execute it.
91 GetFullPathName ("\\autoexec.bat", sizeof(string), string, NULL);
92 h = CreateFile (string, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
93 if (h != INVALID_HANDLE_VALUE) {
94 CloseHandle (h);
95 #if 0
96 WCMD_batch (string, " ");
97 #endif
101 * Loop forever getting commands and executing them.
104 WCMD_version ();
105 while (TRUE) {
106 WCMD_show_prompt ();
107 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
108 if (count > 1) {
109 string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
110 if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
111 if (lstrlen (string) != 0) {
112 WCMD_process_command (string);
119 /*****************************************************************************
120 * Process one command. If the command is EXIT this routine does not return.
121 * We will recurse through here executing batch files.
125 void WCMD_process_command (char *command) {
127 char cmd[1024];
128 char *p;
129 int status, i;
130 DWORD count;
131 HANDLE old_stdin = 0, old_stdout = 0, h;
132 char *whichcmd;
135 * Throw away constructs we don't support yet
138 if (strchr(command,'|') != NULL) {
139 WCMD_output ("Pipes not yet implemented\n");
140 return;
144 * Expand up environment variables.
147 status = ExpandEnvironmentStrings (command, cmd, sizeof(cmd));
148 if (!status) {
149 WCMD_print_error ();
150 return;
154 * Changing default drive has to be handled as a special case.
157 if ((cmd[1] == ':') && IsCharAlpha (cmd[0]) && (strlen(cmd) == 2)) {
158 status = SetCurrentDirectory (cmd);
159 if (!status) WCMD_print_error ();
160 return;
163 /* Dont issue newline WCMD_output (newline); @JED*/
166 * Redirect stdin and/or stdout if required.
169 if ((p = strchr(cmd,'<')) != NULL) {
170 h = CreateFile (WCMD_parameter (++p, 0, NULL), GENERIC_READ, 0, NULL, OPEN_EXISTING,
171 FILE_ATTRIBUTE_NORMAL, 0);
172 if (h == INVALID_HANDLE_VALUE) {
173 WCMD_print_error ();
174 return;
176 old_stdin = GetStdHandle (STD_INPUT_HANDLE);
177 SetStdHandle (STD_INPUT_HANDLE, h);
179 if ((p = strchr(cmd,'>')) != NULL) {
180 h = CreateFile (WCMD_parameter (++p, 0, NULL), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
181 FILE_ATTRIBUTE_NORMAL, 0);
182 if (h == INVALID_HANDLE_VALUE) {
183 WCMD_print_error ();
184 return;
186 old_stdout = GetStdHandle (STD_OUTPUT_HANDLE);
187 SetStdHandle (STD_OUTPUT_HANDLE, h);
188 *--p = '\0';
190 if ((p = strchr(cmd,'<')) != NULL) *p = '\0';
193 * Strip leading whitespaces, and a '@' if supplied
195 whichcmd = WCMD_strtrim_leading_spaces(cmd);
196 if (whichcmd[0] == '@') whichcmd++;
199 * Check if the command entered is internal. If it is, pass the rest of the
200 * line down to the command. If not try to run a program.
203 count = 0;
204 while (IsCharAlphaNumeric(whichcmd[count])) {
205 count++;
207 for (i=0; i<=WCMD_EXIT; i++) {
208 if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
209 whichcmd, count, inbuilt[i], -1) == 2) break;
211 p = WCMD_strtrim_leading_spaces (&whichcmd[count]);
212 WCMD_parse (p, quals, param1, param2);
213 switch (i) {
215 case WCMD_ATTRIB:
216 WCMD_setshow_attrib ();
217 break;
218 case WCMD_CALL:
219 WCMD_batch (param1, p, 1);
220 break;
221 case WCMD_CD:
222 case WCMD_CHDIR:
223 WCMD_setshow_default ();
224 break;
225 case WCMD_CLS:
226 WCMD_clear_screen ();
227 break;
228 case WCMD_COPY:
229 WCMD_copy ();
230 break;
231 case WCMD_CTTY:
232 WCMD_change_tty ();
233 break;
234 case WCMD_DATE:
235 WCMD_setshow_date ();
236 break;
237 case WCMD_DEL:
238 case WCMD_ERASE:
239 WCMD_delete (0);
240 break;
241 case WCMD_DIR:
242 WCMD_directory ();
243 break;
244 case WCMD_ECHO:
245 /* Use the unstripped version of the following data - step over the space */
246 /* but only if a parameter follows */
247 if (strlen(&whichcmd[count]) > 0)
248 WCMD_echo(&whichcmd[count+1]);
249 else
250 WCMD_echo(&whichcmd[count]);
251 break;
252 case WCMD_FOR:
253 WCMD_for (p);
254 break;
255 case WCMD_GOTO:
256 WCMD_goto ();
257 break;
258 case WCMD_HELP:
259 WCMD_give_help (p);
260 break;
261 case WCMD_IF:
262 WCMD_if (p);
263 break;
264 case WCMD_LABEL:
265 WCMD_volume (1, p);
266 break;
267 case WCMD_MD:
268 case WCMD_MKDIR:
269 WCMD_create_dir ();
270 break;
271 case WCMD_MOVE:
272 WCMD_move ();
273 break;
274 case WCMD_PATH:
275 WCMD_setshow_path ();
276 break;
277 case WCMD_PAUSE:
278 WCMD_pause ();
279 break;
280 case WCMD_PROMPT:
281 WCMD_setshow_prompt ();
282 break;
283 case WCMD_REM:
284 break;
285 case WCMD_REN:
286 case WCMD_RENAME:
287 WCMD_rename ();
288 break;
289 case WCMD_RD:
290 case WCMD_RMDIR:
291 WCMD_remove_dir ();
292 break;
293 case WCMD_SET:
294 WCMD_setshow_env (p);
295 break;
296 case WCMD_SHIFT:
297 WCMD_shift ();
298 break;
299 case WCMD_TIME:
300 WCMD_setshow_time ();
301 break;
302 case WCMD_TYPE:
303 WCMD_type ();
304 break;
305 case WCMD_VER:
306 WCMD_version ();
307 break;
308 case WCMD_VERIFY:
309 WCMD_verify (p);
310 break;
311 case WCMD_VOL:
312 WCMD_volume (0, p);
313 break;
314 case WCMD_EXIT:
315 ExitProcess (0);
316 default:
317 WCMD_run_program (whichcmd);
319 if (old_stdin) {
320 CloseHandle (GetStdHandle (STD_INPUT_HANDLE));
321 SetStdHandle (STD_INPUT_HANDLE, old_stdin);
323 if (old_stdout) {
324 CloseHandle (GetStdHandle (STD_OUTPUT_HANDLE));
325 SetStdHandle (STD_OUTPUT_HANDLE, old_stdout);
329 /******************************************************************************
330 * WCMD_run_program
332 * Execute a command line as an external program. If no extension given then
333 * precedence is given to .BAT files. Must allow recursion.
335 * FIXME: Case sensitivity in suffixes!
338 void WCMD_run_program (char *command) {
340 STARTUPINFO st;
341 PROCESS_INFORMATION pe;
342 SHFILEINFO psfi;
343 DWORD console;
344 BOOL status;
345 HANDLE h;
346 HINSTANCE hinst;
347 char filetorun[MAX_PATH];
349 WCMD_parse (command, quals, param1, param2); /* Quick way to get the filename */
350 if (strpbrk (param1, "\\:") == NULL) { /* No explicit path given */
351 if ((strchr (param1, '.') == NULL) || (strstr (param1, ".bat") != NULL)) {
352 if (SearchPath (NULL, param1, ".bat", sizeof(filetorun), filetorun, NULL)) {
353 WCMD_batch (filetorun, command, 0);
354 return;
358 else { /* Explicit path given */
359 if (strstr (param1, ".bat") != NULL) {
360 WCMD_batch (param1, command, 0);
361 return;
363 if (strchr (param1, '.') == NULL) {
364 strcpy (filetorun, param1);
365 strcat (filetorun, ".bat");
366 h = CreateFile (filetorun, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
367 if (h != INVALID_HANDLE_VALUE) {
368 CloseHandle (h);
369 WCMD_batch (param1, command, 0);
370 return;
375 /* No batch file found, assume executable */
377 hinst = FindExecutable (param1, NULL, filetorun);
378 if ((int)hinst < 32) {
379 WCMD_print_error ();
380 return;
382 console = SHGetFileInfo (filetorun, 0, &psfi, sizeof(psfi), SHGFI_EXETYPE);
383 if (!console) {
384 WCMD_print_error ();
385 return;
387 ZeroMemory (&st, sizeof(STARTUPINFO));
388 st.cb = sizeof(STARTUPINFO);
389 status = CreateProcess (NULL, command, NULL, NULL, FALSE,
390 0, NULL, NULL, &st, &pe);
391 if (!status) {
392 WCMD_print_error ();
394 if (!HIWORD(console)) WaitForSingleObject (pe.hProcess, INFINITE);
395 GetExitCodeProcess (pe.hProcess, &errorlevel);
396 if (errorlevel == STILL_ACTIVE) errorlevel = 0;
399 /******************************************************************************
400 * WCMD_show_prompt
402 * Display the prompt on STDout
406 void WCMD_show_prompt () {
408 int status;
409 char out_string[MAX_PATH], curdir[MAX_PATH], prompt_string[MAX_PATH];
410 char *p, *q;
412 status = GetEnvironmentVariable ("PROMPT", prompt_string, sizeof(prompt_string));
413 if ((status == 0) || (status > sizeof(prompt_string))) {
414 lstrcpy (prompt_string, "$N$G");
416 p = prompt_string;
417 q = out_string;
418 *q = '\0';
419 while (*p != '\0') {
420 if (*p != '$') {
421 *q++ = *p++;
422 *q = '\0';
424 else {
425 p++;
426 switch (toupper(*p)) {
427 case '$':
428 *q++ = '$';
429 break;
430 case 'B':
431 *q++ = '|';
432 break;
433 case 'D':
434 GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, NULL, q, MAX_PATH);
435 while (*q) q++;
436 break;
437 case 'E':
438 *q++ = '\E';
439 break;
440 case 'G':
441 *q++ = '>';
442 break;
443 case 'L':
444 *q++ = '<';
445 break;
446 case 'N':
447 status = GetCurrentDirectory (sizeof(curdir), curdir);
448 if (status) {
449 *q++ = curdir[0];
451 break;
452 case 'P':
453 status = GetCurrentDirectory (sizeof(curdir), curdir);
454 if (status) {
455 lstrcat (q, curdir);
456 while (*q) q++;
458 break;
459 case 'Q':
460 *q++ = '=';
461 break;
462 case 'T':
463 GetTimeFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL, q, MAX_PATH);
464 while (*q) q++;
465 break;
466 case '_':
467 *q++ = '\n';
468 break;
470 p++;
471 *q = '\0';
474 WCMD_output (out_string);
477 /****************************************************************************
478 * WCMD_print_error
480 * Print the message for GetLastError
483 void WCMD_print_error () {
484 LPVOID lpMsgBuf;
485 DWORD error_code;
486 int status;
488 error_code = GetLastError ();
489 status = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
490 NULL, error_code, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
491 if (!status) {
492 WCMD_output ("FIXME: Cannot display message for error %d, status %d\n",
493 error_code, GetLastError());
494 return;
496 WCMD_output (lpMsgBuf);
497 LocalFree ((HLOCAL)lpMsgBuf);
498 WCMD_output (newline);
499 return;
502 /*******************************************************************
503 * WCMD_parse - parse a command into parameters and qualifiers.
505 * On exit, all qualifiers are concatenated into q, the first string
506 * not beginning with "/" is in p1 and the
507 * second in p2. Any subsequent non-qualifier strings are lost.
508 * Parameters in quotes are handled.
511 void WCMD_parse (char *s, char *q, char *p1, char *p2) {
513 int p = 0;
515 *q = *p1 = *p2 = '\0';
516 while (TRUE) {
517 switch (*s) {
518 case '/':
519 *q++ = *s++;
520 while ((*s != '\0') && (*s != ' ') && *s != '/') {
521 *q++ = toupper (*s++);
523 *q = '\0';
524 break;
525 case ' ':
526 s++;
527 break;
528 case '"':
529 s++;
530 while ((*s != '\0') && (*s != '"')) {
531 if (p == 0) *p1++ = *s++;
532 else if (p == 1) *p2++ = *s++;
533 else s++;
535 if (p == 0) *p1 = '\0';
536 if (p == 1) *p2 = '\0';
537 p++;
538 if (*s == '"') s++;
539 break;
540 case '\0':
541 return;
542 default:
543 while ((*s != '\0') && (*s != ' ') && (*s != '/')) {
544 if (p == 0) *p1++ = *s++;
545 else if (p == 1) *p2++ = *s++;
546 else s++;
548 if (p == 0) *p1 = '\0';
549 if (p == 1) *p2 = '\0';
550 p++;
555 /*******************************************************************
556 * WCMD_output - send output to current standard output device.
560 void WCMD_output (char *format, ...) {
562 va_list ap;
563 char string[1024];
564 DWORD count;
566 va_start(ap,format);
567 vsprintf (string, format, ap);
568 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), string, lstrlen(string), &count, NULL);
569 va_end(ap);
572 /*******************************************************************
573 * WCMD_output_asis - send output to current standard output device.
574 * without formatting eg. when message contains '%'
577 void WCMD_output_asis (char *message) {
578 DWORD count;
579 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), message, lstrlen(message), &count, NULL);
584 /***************************************************************************
585 * WCMD_strtrim_leading_spaces
587 * Remove leading spaces from a string. Return a pointer to the first
588 * non-space character. Does not modify the input string
591 char *WCMD_strtrim_leading_spaces (char *string) {
593 char *ptr;
595 ptr = string;
596 while (*ptr == ' ') ptr++;
597 return ptr;
600 /*************************************************************************
601 * WCMD_strtrim_trailing_spaces
603 * Remove trailing spaces from a string. This routine modifies the input
604 * string by placing a null after the last non-space character
607 void WCMD_strtrim_trailing_spaces (char *string) {
609 char *ptr;
611 ptr = string + lstrlen (string) - 1;
612 while ((*ptr == ' ') && (ptr >= string)) {
613 *ptr = '\0';
614 ptr--;