Changes wine_main to main to properly build on all platforms.
[wine/wine64.git] / programs / wcmd / wcmdmain.c
blob45efc7f39c97f5c465d365d6cc0937214914b013
1 /*
2 * WCMD - Wine-compatible command line interface.
4 * (C) 1999 - 2001 D A Pickles
5 */
7 /*
8 * FIXME:
9 * - Cannot handle parameters in quotes
10 * - Lots of functionality missing from builtins
13 #include "wcmd.h"
15 char *inbuilt[] = {"ATTRIB", "CALL", "CD", "CHDIR", "CLS", "COPY", "CTTY",
16 "DATE", "DEL", "DIR", "ECHO", "ERASE", "FOR", "GOTO",
17 "HELP", "IF", "LABEL", "MD", "MKDIR", "MOVE", "PATH", "PAUSE",
18 "PROMPT", "REM", "REN", "RENAME", "RD", "RMDIR", "SET", "SHIFT",
19 "TIME", "TYPE", "VERIFY", "VER", "VOL", "EXIT"};
21 HINSTANCE hinst;
22 DWORD errorlevel;
23 int echo_mode = 1, verify_mode = 0;
24 char nyi[] = "Not Yet Implemented\n\n";
25 char newline[] = "\n";
26 char version_string[] = "WCMD Version 0.17\n\n";
27 char anykey[] = "Press any key to continue: ";
28 char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
29 BATCH_CONTEXT *context = NULL;
31 /*****************************************************************************
32 * Main entry point. This is a console application so we have a main() not a
33 * winmain().
36 int main (int argc, char *argv[]) {
38 char string[1024], args[MAX_PATH], param[MAX_PATH];
39 int status, i;
40 DWORD count;
41 HANDLE h;
43 args[0] = param[0] = '\0';
44 if (argc > 1) {
45 for (i=1; i<argc; i++) {
46 if (argv[i][0] == '/') {
47 strcat (args, argv[i]);
49 else {
50 strcat (param, argv[i]);
51 strcat (param, " ");
57 * Allocate a console and set it up.
60 status = FreeConsole ();
61 if (!status) WCMD_print_error();
62 status = AllocConsole();
63 if (!status) WCMD_print_error();
64 SetConsoleMode (GetStdHandle(STD_INPUT_HANDLE), ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT |
65 ENABLE_PROCESSED_INPUT);
68 * Execute any command-line options.
71 if (strstr(args, "/q") != NULL) {
72 WCMD_echo ("OFF");
75 if (strstr(args, "/c") != NULL) {
76 WCMD_process_command (param);
77 return 0;
80 if (strstr(args, "/k") != NULL) {
81 WCMD_process_command (param);
85 * If there is an AUTOEXEC.BAT file, try to execute it.
88 GetFullPathName ("\\autoexec.bat", sizeof(string), string, NULL);
89 h = CreateFile (string, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
90 if (h != INVALID_HANDLE_VALUE) {
91 CloseHandle (h);
92 #if 0
93 WCMD_batch (string, " ");
94 #endif
98 * Loop forever getting commands and executing them.
101 WCMD_version ();
102 while (TRUE) {
103 WCMD_show_prompt ();
104 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
105 if (count > 1) {
106 string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
107 if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
108 if (lstrlen (string) != 0) {
109 if (strchr(string,'|') != NULL) {
110 WCMD_pipe (string);
112 else {
113 WCMD_process_command (string);
121 /*****************************************************************************
122 * Process one command. If the command is EXIT this routine does not return.
123 * We will recurse through here executing batch files.
127 void WCMD_process_command (char *command) {
129 char cmd[1024];
130 char *p;
131 int status, i;
132 DWORD count;
133 HANDLE old_stdin = 0, old_stdout = 0, h;
134 char *whichcmd;
138 * Expand up environment variables.
141 status = ExpandEnvironmentStrings (command, cmd, sizeof(cmd));
142 if (!status) {
143 WCMD_print_error ();
144 return;
148 * Changing default drive has to be handled as a special case.
151 if ((cmd[1] == ':') && IsCharAlpha (cmd[0]) && (strlen(cmd) == 2)) {
152 status = SetCurrentDirectory (cmd);
153 if (!status) WCMD_print_error ();
154 return;
157 /* Dont issue newline WCMD_output (newline); @JED*/
160 * Redirect stdin and/or stdout if required.
163 if ((p = strchr(cmd,'<')) != NULL) {
164 h = CreateFile (WCMD_parameter (++p, 0, NULL), GENERIC_READ, 0, NULL, OPEN_EXISTING,
165 FILE_ATTRIBUTE_NORMAL, NULL);
166 if (h == INVALID_HANDLE_VALUE) {
167 WCMD_print_error ();
168 return;
170 old_stdin = GetStdHandle (STD_INPUT_HANDLE);
171 SetStdHandle (STD_INPUT_HANDLE, h);
173 if ((p = strchr(cmd,'>')) != NULL) {
174 h = CreateFile (WCMD_parameter (++p, 0, NULL), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
175 FILE_ATTRIBUTE_NORMAL, NULL);
176 if (h == INVALID_HANDLE_VALUE) {
177 WCMD_print_error ();
178 return;
180 old_stdout = GetStdHandle (STD_OUTPUT_HANDLE);
181 SetStdHandle (STD_OUTPUT_HANDLE, h);
182 *--p = '\0';
184 if ((p = strchr(cmd,'<')) != NULL) *p = '\0';
187 * Strip leading whitespaces, and a '@' if supplied
189 whichcmd = WCMD_strtrim_leading_spaces(cmd);
190 if (whichcmd[0] == '@') whichcmd++;
193 * Check if the command entered is internal. If it is, pass the rest of the
194 * line down to the command. If not try to run a program.
197 count = 0;
198 while (IsCharAlphaNumeric(whichcmd[count])) {
199 count++;
201 for (i=0; i<=WCMD_EXIT; i++) {
202 if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
203 whichcmd, count, inbuilt[i], -1) == 2) break;
205 p = WCMD_strtrim_leading_spaces (&whichcmd[count]);
206 WCMD_parse (p, quals, param1, param2);
207 switch (i) {
209 case WCMD_ATTRIB:
210 WCMD_setshow_attrib ();
211 break;
212 case WCMD_CALL:
213 WCMD_batch (param1, p, 1);
214 break;
215 case WCMD_CD:
216 case WCMD_CHDIR:
217 WCMD_setshow_default ();
218 break;
219 case WCMD_CLS:
220 WCMD_clear_screen ();
221 break;
222 case WCMD_COPY:
223 WCMD_copy ();
224 break;
225 case WCMD_CTTY:
226 WCMD_change_tty ();
227 break;
228 case WCMD_DATE:
229 WCMD_setshow_date ();
230 break;
231 case WCMD_DEL:
232 case WCMD_ERASE:
233 WCMD_delete (0);
234 break;
235 case WCMD_DIR:
236 WCMD_directory ();
237 break;
238 case WCMD_ECHO:
239 /* Use the unstripped version of the following data - step over the space */
240 /* but only if a parameter follows */
241 if (strlen(&whichcmd[count]) > 0)
242 WCMD_echo(&whichcmd[count+1]);
243 else
244 WCMD_echo(&whichcmd[count]);
245 break;
246 case WCMD_FOR:
247 WCMD_for (p);
248 break;
249 case WCMD_GOTO:
250 WCMD_goto ();
251 break;
252 case WCMD_HELP:
253 WCMD_give_help (p);
254 break;
255 case WCMD_IF:
256 WCMD_if (p);
257 break;
258 case WCMD_LABEL:
259 WCMD_volume (1, p);
260 break;
261 case WCMD_MD:
262 case WCMD_MKDIR:
263 WCMD_create_dir ();
264 break;
265 case WCMD_MOVE:
266 WCMD_move ();
267 break;
268 case WCMD_PATH:
269 WCMD_setshow_path ();
270 break;
271 case WCMD_PAUSE:
272 WCMD_pause ();
273 break;
274 case WCMD_PROMPT:
275 WCMD_setshow_prompt ();
276 break;
277 case WCMD_REM:
278 break;
279 case WCMD_REN:
280 case WCMD_RENAME:
281 WCMD_rename ();
282 break;
283 case WCMD_RD:
284 case WCMD_RMDIR:
285 WCMD_remove_dir ();
286 break;
287 case WCMD_SET:
288 WCMD_setshow_env (p);
289 break;
290 case WCMD_SHIFT:
291 WCMD_shift ();
292 break;
293 case WCMD_TIME:
294 WCMD_setshow_time ();
295 break;
296 case WCMD_TYPE:
297 WCMD_type ();
298 break;
299 case WCMD_VER:
300 WCMD_version ();
301 break;
302 case WCMD_VERIFY:
303 WCMD_verify (p);
304 break;
305 case WCMD_VOL:
306 WCMD_volume (0, p);
307 break;
308 case WCMD_EXIT:
309 ExitProcess (0);
310 default:
311 WCMD_run_program (whichcmd);
313 if (old_stdin) {
314 CloseHandle (GetStdHandle (STD_INPUT_HANDLE));
315 SetStdHandle (STD_INPUT_HANDLE, old_stdin);
317 if (old_stdout) {
318 CloseHandle (GetStdHandle (STD_OUTPUT_HANDLE));
319 SetStdHandle (STD_OUTPUT_HANDLE, old_stdout);
323 /******************************************************************************
324 * WCMD_run_program
326 * Execute a command line as an external program. If no extension given then
327 * precedence is given to .BAT files. Must allow recursion.
329 * FIXME: Case sensitivity in suffixes!
332 void WCMD_run_program (char *command) {
334 STARTUPINFO st;
335 PROCESS_INFORMATION pe;
336 SHFILEINFO psfi;
337 DWORD console;
338 BOOL status;
339 HANDLE h;
340 HINSTANCE hinst;
341 char filetorun[MAX_PATH];
343 WCMD_parse (command, quals, param1, param2); /* Quick way to get the filename */
344 if (strpbrk (param1, "\\:") == NULL) { /* No explicit path given */
345 if ((strchr (param1, '.') == NULL) || (strstr (param1, ".bat") != NULL)) {
346 if (SearchPath (NULL, param1, ".bat", sizeof(filetorun), filetorun, NULL)) {
347 WCMD_batch (filetorun, command, 0);
348 return;
352 else { /* Explicit path given */
353 if (strstr (param1, ".bat") != NULL) {
354 WCMD_batch (param1, command, 0);
355 return;
357 if (strchr (param1, '.') == NULL) {
358 strcpy (filetorun, param1);
359 strcat (filetorun, ".bat");
360 h = CreateFile (filetorun, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
361 if (h != INVALID_HANDLE_VALUE) {
362 CloseHandle (h);
363 WCMD_batch (param1, command, 0);
364 return;
369 /* No batch file found, assume executable */
371 hinst = FindExecutable (param1, NULL, filetorun);
372 if ((int)hinst < 32) {
373 WCMD_print_error ();
374 return;
376 console = SHGetFileInfo (filetorun, 0, &psfi, sizeof(psfi), SHGFI_EXETYPE);
377 if (!console) {
378 WCMD_print_error ();
379 return;
381 ZeroMemory (&st, sizeof(STARTUPINFO));
382 st.cb = sizeof(STARTUPINFO);
383 status = CreateProcess (NULL, command, NULL, NULL, FALSE,
384 0, NULL, NULL, &st, &pe);
385 if (!status) {
386 WCMD_print_error ();
388 if (!HIWORD(console)) WaitForSingleObject (pe.hProcess, INFINITE);
389 GetExitCodeProcess (pe.hProcess, &errorlevel);
390 if (errorlevel == STILL_ACTIVE) errorlevel = 0;
393 /******************************************************************************
394 * WCMD_show_prompt
396 * Display the prompt on STDout
400 void WCMD_show_prompt () {
402 int status;
403 char out_string[MAX_PATH], curdir[MAX_PATH], prompt_string[MAX_PATH];
404 char *p, *q;
406 status = GetEnvironmentVariable ("PROMPT", prompt_string, sizeof(prompt_string));
407 if ((status == 0) || (status > sizeof(prompt_string))) {
408 lstrcpy (prompt_string, "$N$G");
410 p = prompt_string;
411 q = out_string;
412 *q = '\0';
413 while (*p != '\0') {
414 if (*p != '$') {
415 *q++ = *p++;
416 *q = '\0';
418 else {
419 p++;
420 switch (toupper(*p)) {
421 case '$':
422 *q++ = '$';
423 break;
424 case 'B':
425 *q++ = '|';
426 break;
427 case 'D':
428 GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, NULL, q, MAX_PATH);
429 while (*q) q++;
430 break;
431 case 'E':
432 *q++ = '\E';
433 break;
434 case 'G':
435 *q++ = '>';
436 break;
437 case 'L':
438 *q++ = '<';
439 break;
440 case 'N':
441 status = GetCurrentDirectory (sizeof(curdir), curdir);
442 if (status) {
443 *q++ = curdir[0];
445 break;
446 case 'P':
447 status = GetCurrentDirectory (sizeof(curdir), curdir);
448 if (status) {
449 lstrcat (q, curdir);
450 while (*q) q++;
452 break;
453 case 'Q':
454 *q++ = '=';
455 break;
456 case 'T':
457 GetTimeFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL, q, MAX_PATH);
458 while (*q) q++;
459 break;
460 case '_':
461 *q++ = '\n';
462 break;
464 p++;
465 *q = '\0';
468 WCMD_output (out_string);
471 /****************************************************************************
472 * WCMD_print_error
474 * Print the message for GetLastError
477 void WCMD_print_error () {
478 LPVOID lpMsgBuf;
479 DWORD error_code;
480 int status;
482 error_code = GetLastError ();
483 status = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
484 NULL, error_code, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
485 if (!status) {
486 WCMD_output ("FIXME: Cannot display message for error %d, status %d\n",
487 error_code, GetLastError());
488 return;
490 WCMD_output (lpMsgBuf);
491 LocalFree ((HLOCAL)lpMsgBuf);
492 WCMD_output (newline);
493 return;
496 /*******************************************************************
497 * WCMD_parse - parse a command into parameters and qualifiers.
499 * On exit, all qualifiers are concatenated into q, the first string
500 * not beginning with "/" is in p1 and the
501 * second in p2. Any subsequent non-qualifier strings are lost.
502 * Parameters in quotes are handled.
505 void WCMD_parse (char *s, char *q, char *p1, char *p2) {
507 int p = 0;
509 *q = *p1 = *p2 = '\0';
510 while (TRUE) {
511 switch (*s) {
512 case '/':
513 *q++ = *s++;
514 while ((*s != '\0') && (*s != ' ') && *s != '/') {
515 *q++ = toupper (*s++);
517 *q = '\0';
518 break;
519 case ' ':
520 s++;
521 break;
522 case '"':
523 s++;
524 while ((*s != '\0') && (*s != '"')) {
525 if (p == 0) *p1++ = *s++;
526 else if (p == 1) *p2++ = *s++;
527 else s++;
529 if (p == 0) *p1 = '\0';
530 if (p == 1) *p2 = '\0';
531 p++;
532 if (*s == '"') s++;
533 break;
534 case '\0':
535 return;
536 default:
537 while ((*s != '\0') && (*s != ' ') && (*s != '/')) {
538 if (p == 0) *p1++ = *s++;
539 else if (p == 1) *p2++ = *s++;
540 else s++;
542 if (p == 0) *p1 = '\0';
543 if (p == 1) *p2 = '\0';
544 p++;
549 /*******************************************************************
550 * WCMD_output - send output to current standard output device.
554 void WCMD_output (char *format, ...) {
556 va_list ap;
557 char string[1024];
558 DWORD count;
560 va_start(ap,format);
561 vsprintf (string, format, ap);
562 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), string, lstrlen(string), &count, NULL);
563 va_end(ap);
566 /*******************************************************************
567 * WCMD_output_asis - send output to current standard output device.
568 * without formatting eg. when message contains '%'
571 void WCMD_output_asis (char *message) {
572 DWORD count;
573 WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), message, lstrlen(message), &count, NULL);
578 /***************************************************************************
579 * WCMD_strtrim_leading_spaces
581 * Remove leading spaces from a string. Return a pointer to the first
582 * non-space character. Does not modify the input string
585 char *WCMD_strtrim_leading_spaces (char *string) {
587 char *ptr;
589 ptr = string;
590 while (*ptr == ' ') ptr++;
591 return ptr;
594 /*************************************************************************
595 * WCMD_strtrim_trailing_spaces
597 * Remove trailing spaces from a string. This routine modifies the input
598 * string by placing a null after the last non-space character
601 void WCMD_strtrim_trailing_spaces (char *string) {
603 char *ptr;
605 ptr = string + lstrlen (string) - 1;
606 while ((*ptr == ' ') && (ptr >= string)) {
607 *ptr = '\0';
608 ptr--;
612 /*************************************************************************
613 * WCMD_pipe
615 * Handle pipes within a command - the DOS way using temporary files.
618 void WCMD_pipe (char *command) {
620 char *p;
621 char temp_path[MAX_PATH], temp_file[MAX_PATH], temp_file2[MAX_PATH], temp_cmd[1024];
623 GetTempPath (sizeof(temp_path), temp_path);
624 GetTempFileName (temp_path, "WCMD", 0, temp_file);
625 p = strchr(command, '|');
626 *p++ = '\0';
627 wsprintf (temp_cmd, "%s > %s", command, temp_file);
628 WCMD_process_command (temp_cmd);
629 command = p;
630 while ((p = strchr(command, '|'))) {
631 *p++ = '\0';
632 GetTempFileName (temp_path, "WCMD", 0, temp_file2);
633 wsprintf (temp_cmd, "%s < %s > %s", command, temp_file, temp_file2);
634 WCMD_process_command (temp_cmd);
635 DeleteFile (temp_file);
636 lstrcpy (temp_file, temp_file2);
637 command = p;
639 wsprintf (temp_cmd, "%s < %s", command, temp_file);
640 WCMD_process_command (temp_cmd);
641 DeleteFile (temp_file);