Fix handling of wcmd /c "c:\Program Files\hello.bat".

Make /c and /k effectively exclusive, like the real cmd does.
Fix handling of /q: it's compatible with /c and /k.
Added compatibility with /t /x and /y, just ignore them.
This commit is contained in:
Francois Gouget 2003-05-11 02:48:17 +00:00 committed by Alexandre Julliard
parent bf022eecb9
commit d98f4a5e43
1 changed files with 152 additions and 40 deletions

View File

@ -47,50 +47,162 @@ BATCH_CONTEXT *context = NULL;
* winmain(). * winmain().
*/ */
int main (int argc, char *argv[]) { int main (int argc, char *argv[])
{
char string[1024];
char* cmd=NULL;
DWORD count;
HANDLE h;
int opt_c, opt_k, opt_q;
char string[1024], args[MAX_PATH], param[MAX_PATH]; argv++;
int i; opt_c=opt_k=opt_q=0;
DWORD count; while (*argv!=NULL)
HANDLE h; {
if (lstrcmpi(*argv,"/c")==0) {
args[0] = param[0] = '\0'; opt_c=1;
if (argc > 1) { argv++;
/* interpreter options must come before the command to execute. break;
* Any options after the command are command options, not interpreter options. } else if (lstrcmpi(*argv,"/q")==0) {
*/ opt_q=1;
for (i=1; i<argc && argv[i][0] == '/'; i++) } else if (lstrcmpi(*argv,"/k")==0) {
strcat (args, argv[i]); opt_k=1;
for (; i<argc; i++) { argv++;
strcat (param, argv[i]); break;
strcat (param, " "); } else if (lstrcmpi(*argv,"/t")==0 || lstrcmpi(*argv,"/x")==0 ||
} lstrcmpi(*argv,"/y")==0) {
/* Ignored for compatibility with Windows */
} else {
break;
}
argv++;
} }
/* If we do a "wcmd /c command", we don't want to allocate a new if (opt_q) {
* console since the command returns immediately. Rather, we use WCMD_echo("OFF");
* the surrently allocated input and output handles. This allows
* us to pipe to and read from the command interpreter.
*/
if (strstr(args, "/c") != NULL) {
WCMD_process_command (param);
return 0;
} }
SetConsoleMode (GetStdHandle(STD_INPUT_HANDLE), ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | if (opt_c || opt_k) {
ENABLE_PROCESSED_INPUT); int len;
char** arg;
char* p;
/* Build the command to execute */
len = 0;
for (arg = argv; *arg; arg++)
{
int has_space,bcount;
char* a;
has_space=0;
bcount=0;
a=*arg;
while (*a!='\0') {
if (*a=='\\') {
bcount++;
} else {
if (*a==' ' || *a=='\t') {
has_space=1;
} else if (*a=='"') {
/* doubling of '\' preceeding a '"',
* plus escaping of said '"'
*/
len+=2*bcount+1;
}
bcount=0;
}
a++;
}
len+=(a-*arg)+1 /* for the separating space */;
if (has_space)
len+=2; /* for the quotes */
}
cmd = HeapAlloc(GetProcessHeap(), 0, len);
if (!cmd)
exit(1);
p = cmd;
for (arg = argv; *arg; arg++)
{
int has_space,has_quote;
char* a;
/* Check for quotes and spaces in this argument */
has_space=has_quote=0;
a=*arg;
while (*a!='\0') {
if (*a==' ' || *a=='\t') {
has_space=1;
if (has_quote)
break;
} else if (*a=='"') {
has_quote=1;
if (has_space)
break;
}
a++;
}
/* Now transfer it to the command line */
if (has_space)
*p++='"';
if (has_quote) {
int bcount;
char* a;
bcount=0;
a=*arg;
while (*a!='\0') {
if (*a=='\\') {
*p++=*a;
bcount++;
} else {
if (*a=='"') {
int i;
/* Double all the '\\' preceeding this '"', plus one */
for (i=0;i<=bcount;i++)
*p++='\\';
*p++='"';
} else {
*p++=*a;
}
bcount=0;
}
a++;
}
} else {
strcpy(p,*arg);
p+=strlen(*arg);
}
if (has_space)
*p++='"';
*p++=' ';
}
if (p > cmd)
p--; /* remove last space */
*p = '\0';
}
if (opt_c) {
/* If we do a "wcmd /c command", we don't want to allocate a new
* console since the command returns immediately. Rather, we use
* the currently allocated input and output handles. This allows
* us to pipe to and read from the command interpreter.
*/
WCMD_process_command(cmd);
HeapFree(GetProcessHeap(), 0, cmd);
return 0;
}
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_LINE_INPUT |
ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT);
SetConsoleTitle("Wine Command Prompt"); SetConsoleTitle("Wine Command Prompt");
/* if (opt_k) {
* Execute any command-line options. WCMD_process_command(cmd);
*/ HeapFree(GetProcessHeap(), 0, cmd);
if (strstr(args, "/q") != NULL) {
WCMD_echo ("OFF");
}
if (strstr(args, "/k") != NULL) {
WCMD_process_command (param);
} }
/* /*
@ -115,15 +227,15 @@ HANDLE h;
WCMD_show_prompt (); WCMD_show_prompt ();
ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL); ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
if (count > 1) { if (count > 1) {
string[count-1] = '\0'; /* ReadFile output is not null-terminated! */ string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */ if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
if (lstrlen (string) != 0) { if (lstrlen (string) != 0) {
if (strchr(string,'|') != NULL) { if (strchr(string,'|') != NULL) {
WCMD_pipe (string); WCMD_pipe (string);
} }
else { else {
WCMD_process_command (string); WCMD_process_command (string);
} }
} }
} }
} }