wine/programs/cmd/wcmdmain.c

2108 lines
65 KiB
C
Raw Normal View History

1999-06-06 15:24:04 +00:00
/*
2006-09-04 04:13:08 +00:00
* CMD - Wine-compatible command line interface.
1999-06-06 15:24:04 +00:00
*
* Copyright (C) 1999 - 2001 D A Pickles
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
1999-06-06 15:24:04 +00:00
*/
/*
* FIXME:
* - Cannot handle parameters in quotes
* - Lots of functionality missing from builtins
*/
#include "config.h"
1999-06-06 15:24:04 +00:00
#include "wcmd.h"
2007-03-04 22:34:45 +00:00
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(cmd);
1999-06-06 15:24:04 +00:00
2007-06-03 21:07:42 +00:00
const WCHAR inbuilt[][10] = {
{'A','T','T','R','I','B','\0'},
{'C','A','L','L','\0'},
{'C','D','\0'},
{'C','H','D','I','R','\0'},
{'C','L','S','\0'},
{'C','O','P','Y','\0'},
{'C','T','T','Y','\0'},
{'D','A','T','E','\0'},
{'D','E','L','\0'},
{'D','I','R','\0'},
{'E','C','H','O','\0'},
{'E','R','A','S','E','\0'},
{'F','O','R','\0'},
{'G','O','T','O','\0'},
{'H','E','L','P','\0'},
{'I','F','\0'},
{'L','A','B','E','L','\0'},
{'M','D','\0'},
{'M','K','D','I','R','\0'},
{'M','O','V','E','\0'},
{'P','A','T','H','\0'},
{'P','A','U','S','E','\0'},
{'P','R','O','M','P','T','\0'},
{'R','E','M','\0'},
{'R','E','N','\0'},
{'R','E','N','A','M','E','\0'},
{'R','D','\0'},
{'R','M','D','I','R','\0'},
{'S','E','T','\0'},
{'S','H','I','F','T','\0'},
{'T','I','M','E','\0'},
{'T','I','T','L','E','\0'},
{'T','Y','P','E','\0'},
{'V','E','R','I','F','Y','\0'},
{'V','E','R','\0'},
{'V','O','L','\0'},
{'E','N','D','L','O','C','A','L','\0'},
{'S','E','T','L','O','C','A','L','\0'},
{'P','U','S','H','D','\0'},
{'P','O','P','D','\0'},
{'A','S','S','O','C','\0'},
{'C','O','L','O','R','\0'},
{'F','T','Y','P','E','\0'},
{'M','O','R','E','\0'},
{'E','X','I','T','\0'}
};
1999-06-06 15:24:04 +00:00
HINSTANCE hinst;
DWORD errorlevel;
2007-03-08 00:37:44 +00:00
int echo_mode = 1, verify_mode = 0, defaultColor = 7;
static int opt_c, opt_k, opt_s;
2007-06-03 21:07:42 +00:00
const WCHAR newline[] = {'\n','\0'};
static const WCHAR equalsW[] = {'=','\0'};
static const WCHAR closeBW[] = {')','\0'};
2007-06-03 21:07:42 +00:00
WCHAR anykey[100];
WCHAR version_string[100];
WCHAR quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
BATCH_CONTEXT *context = NULL;
extern struct env_stack *pushd_directories;
2007-06-03 21:07:42 +00:00
static const WCHAR *pagedMessage = NULL;
static char *output_bufA = NULL;
#define MAX_WRITECONSOLE_SIZE 65535
BOOL unicodePipes = FALSE;
1999-06-06 15:24:04 +00:00
2007-06-03 21:07:42 +00:00
static WCHAR *WCMD_expand_envvar(WCHAR *start);
2007-02-23 22:22:57 +00:00
1999-06-06 15:24:04 +00:00
/*****************************************************************************
* Main entry point. This is a console application so we have a main() not a
* winmain().
*/
2007-06-03 21:07:45 +00:00
int wmain (int argc, WCHAR *argvW[])
{
int args;
2007-06-03 21:07:42 +00:00
WCHAR *cmd = NULL;
WCHAR string[1024];
WCHAR envvar[4];
HANDLE h;
int opt_q;
2007-03-08 00:38:38 +00:00
int opt_t = 0;
2007-06-03 21:07:42 +00:00
static const WCHAR autoexec[] = {'\\','a','u','t','o','e','x','e','c','.',
'b','a','t','\0'};
char ansiVersion[100];
CMD_LIST *toExecute = NULL; /* Commands left to be executed */
2007-06-03 21:07:42 +00:00
/* Pre initialize some messages */
strcpy(ansiVersion, PACKAGE_VERSION);
MultiByteToWideChar(CP_ACP, 0, ansiVersion, -1, string, 1024);
wsprintf(version_string, WCMD_LoadMessage(WCMD_VERSION), string);
strcpyW(anykey, WCMD_LoadMessage(WCMD_ANYKEY));
args = argc;
opt_c=opt_k=opt_q=opt_s=0;
while (args > 0)
{
WCHAR c;
WINE_TRACE("Command line parm: '%s'\n", wine_dbgstr_w(*argvW));
if ((*argvW)[0]!='/' || (*argvW)[1]=='\0') {
argvW++;
args--;
continue;
}
c=(*argvW)[1];
if (tolowerW(c)=='c') {
opt_c=1;
} else if (tolowerW(c)=='q') {
opt_q=1;
} else if (tolowerW(c)=='k') {
opt_k=1;
} else if (tolowerW(c)=='s') {
opt_s=1;
} else if (tolowerW(c)=='a') {
unicodePipes=FALSE;
} else if (tolowerW(c)=='u') {
unicodePipes=TRUE;
} else if (tolowerW(c)=='t' && (*argvW)[2]==':') {
opt_t=strtoulW(&(*argvW)[3], NULL, 16);
} else if (tolowerW(c)=='x' || tolowerW(c)=='y') {
/* Ignored for compatibility with Windows */
}
if ((*argvW)[2]==0) {
argvW++;
args--;
}
else /* handle `cmd /cnotepad.exe` and `cmd /x/c ...` */
{
*argvW+=2;
}
if (opt_c || opt_k) /* break out of parsing immediately after c or k */
break;
1999-06-06 15:24:04 +00:00
}
if (opt_q) {
2007-06-03 21:07:42 +00:00
const WCHAR eoff[] = {'O','F','F','\0'};
WCMD_echo(eoff);
}
if (opt_c || opt_k) {
int len,qcount;
WCHAR** arg;
int argsLeft;
WCHAR* p;
/* opt_s left unflagged if the command starts with and contains exactly
* one quoted string (exactly two quote characters). The quoted string
* must be an executable name that has whitespace and must not have the
* following characters: &<>()@^| */
/* Build the command to execute */
len = 0;
qcount = 0;
argsLeft = args;
for (arg = argvW; argsLeft>0; arg++,argsLeft--)
{
int has_space,bcount;
WCHAR* a;
has_space=0;
bcount=0;
a=*arg;
if( !*a ) has_space=1;
while (*a!='\0') {
if (*a=='\\') {
bcount++;
} else {
if (*a==' ' || *a=='\t') {
has_space=1;
} else if (*a=='"') {
2005-03-02 13:53:50 +00:00
/* doubling of '\' preceding a '"',
* plus escaping of said '"'
*/
len+=2*bcount+1;
qcount++;
}
bcount=0;
}
a++;
}
len+=(a-*arg) + 1; /* for the separating space */
if (has_space)
{
len+=2; /* for the quotes */
qcount+=2;
}
}
if (qcount!=2)
opt_s=1;
/* check argvW[0] for a space and invalid characters */
if (!opt_s) {
opt_s=1;
p=*argvW;
while (*p!='\0') {
if (*p=='&' || *p=='<' || *p=='>' || *p=='(' || *p==')'
|| *p=='@' || *p=='^' || *p=='|') {
opt_s=1;
break;
}
if (*p==' ')
opt_s=0;
p++;
}
}
1999-06-06 15:24:04 +00:00
2007-06-03 21:07:42 +00:00
cmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
if (!cmd)
exit(1);
2007-06-03 21:07:42 +00:00
p = cmd;
argsLeft = args;
for (arg = argvW; argsLeft>0; arg++,argsLeft--)
{
int has_space,has_quote;
WCHAR* a;
/* Check for quotes and spaces in this argument */
has_space=has_quote=0;
a=*arg;
if( !*a ) has_space=1;
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;
WCHAR* a;
bcount=0;
a=*arg;
while (*a!='\0') {
if (*a=='\\') {
*p++=*a;
bcount++;
} else {
if (*a=='"') {
int i;
2005-03-02 13:53:50 +00:00
/* Double all the '\\' preceding this '"', plus one */
for (i=0;i<=bcount;i++)
*p++='\\';
*p++='"';
} else {
*p++=*a;
}
bcount=0;
}
a++;
}
} else {
strcpyW(p,*arg);
p+=strlenW(*arg);
}
if (has_space)
*p++='"';
*p++=' ';
}
2007-06-03 21:07:42 +00:00
if (p > cmd)
p--; /* remove last space */
*p = '\0';
2007-06-03 21:07:42 +00:00
WINE_TRACE("/c command line: '%s'\n", wine_dbgstr_w(cmd));
/* strip first and last quote characters if opt_s; check for invalid
* executable is done later */
if (opt_s && *cmd=='\"')
WCMD_opt_s_strip_quotes(cmd);
}
1999-06-06 15:24:04 +00:00
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.
*/
/* Parse the command string, without reading any more input */
WCMD_ReadAndParseLine(cmd, &toExecute, INVALID_HANDLE_VALUE);
WCMD_process_commands(toExecute);
WCMD_free_commands(toExecute);
toExecute = NULL;
HeapFree(GetProcessHeap(), 0, cmd);
2007-02-20 17:49:08 +00:00
return errorlevel;
1999-06-06 15:24:04 +00:00
}
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_LINE_INPUT |
ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT);
2007-06-03 21:07:42 +00:00
SetConsoleTitle(WCMD_LoadMessage(WCMD_CONSTITLE));
2007-03-08 00:38:38 +00:00
/* Note: cmd.exe /c dir does not get a new color, /k dir does */
if (opt_t) {
if (!(((opt_t & 0xF0) >> 4) == (opt_t & 0x0F))) {
defaultColor = opt_t & 0xFF;
2007-03-08 00:38:38 +00:00
param1[0] = 0x00;
WCMD_color();
}
} else {
/* Check HKCU\Software\Microsoft\Command Processor
Then HKLM\Software\Microsoft\Command Processor
for defaultcolour value
Note Can be supplied as DWORD or REG_SZ
Note2 When supplied as REG_SZ it's in decimal!!! */
HKEY key;
DWORD type;
DWORD value=0, size=4;
2007-06-03 21:07:42 +00:00
static const WCHAR regKeyW[] = {'S','o','f','t','w','a','r','e','\\',
'M','i','c','r','o','s','o','f','t','\\',
'C','o','m','m','a','n','d',' ','P','r','o','c','e','s','s','o','r','\0'};
static const WCHAR dfltColorW[] = {'D','e','f','a','u','l','t','C','o','l','o','r','\0'};
2007-06-03 21:07:42 +00:00
if (RegOpenKeyEx(HKEY_CURRENT_USER, regKeyW,
0, KEY_READ, &key) == ERROR_SUCCESS) {
2007-06-03 21:07:42 +00:00
WCHAR strvalue[4];
/* See if DWORD or REG_SZ */
2007-06-03 21:07:42 +00:00
if (RegQueryValueEx(key, dfltColorW, NULL, &type,
NULL, NULL) == ERROR_SUCCESS) {
if (type == REG_DWORD) {
size = sizeof(DWORD);
2007-06-03 21:07:42 +00:00
RegQueryValueEx(key, dfltColorW, NULL, NULL,
(LPBYTE)&value, &size);
} else if (type == REG_SZ) {
2007-06-03 21:07:42 +00:00
size = sizeof(strvalue)/sizeof(WCHAR);
RegQueryValueEx(key, dfltColorW, NULL, NULL,
(LPBYTE)strvalue, &size);
2007-06-03 21:07:42 +00:00
value = strtoulW(strvalue, NULL, 10);
}
}
}
2007-06-03 21:07:42 +00:00
if (value == 0 && RegOpenKeyEx(HKEY_LOCAL_MACHINE, regKeyW,
0, KEY_READ, &key) == ERROR_SUCCESS) {
2007-06-03 21:07:42 +00:00
WCHAR strvalue[4];
/* See if DWORD or REG_SZ */
2007-06-03 21:07:42 +00:00
if (RegQueryValueEx(key, dfltColorW, NULL, &type,
NULL, NULL) == ERROR_SUCCESS) {
if (type == REG_DWORD) {
size = sizeof(DWORD);
2007-06-03 21:07:42 +00:00
RegQueryValueEx(key, dfltColorW, NULL, NULL,
(LPBYTE)&value, &size);
} else if (type == REG_SZ) {
2007-06-03 21:07:42 +00:00
size = sizeof(strvalue)/sizeof(WCHAR);
RegQueryValueEx(key, dfltColorW, NULL, NULL,
(LPBYTE)strvalue, &size);
2007-06-03 21:07:42 +00:00
value = strtoulW(strvalue, NULL, 10);
}
}
}
/* If one found, set the screen to that colour */
if (!(((value & 0xF0) >> 4) == (value & 0x0F))) {
defaultColor = value & 0xFF;
param1[0] = 0x00;
WCMD_color();
}
2007-03-08 00:38:38 +00:00
}
/* Save cwd into appropriate env var */
GetCurrentDirectory(1024, string);
if (IsCharAlpha(string[0]) && string[1] == ':') {
2007-06-03 21:07:42 +00:00
static const WCHAR fmt[] = {'=','%','c',':','\0'};
wsprintf(envvar, fmt, string[0]);
SetEnvironmentVariable(envvar, string);
}
if (opt_k) {
/* Parse the command string, without reading any more input */
WCMD_ReadAndParseLine(cmd, &toExecute, INVALID_HANDLE_VALUE);
WCMD_process_commands(toExecute);
WCMD_free_commands(toExecute);
toExecute = NULL;
HeapFree(GetProcessHeap(), 0, cmd);
1999-06-06 15:24:04 +00:00
}
/*
* If there is an AUTOEXEC.BAT file, try to execute it.
*/
2007-06-03 21:07:42 +00:00
GetFullPathName (autoexec, sizeof(string)/sizeof(WCHAR), string, NULL);
h = CreateFile (string, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1999-06-06 15:24:04 +00:00
if (h != INVALID_HANDLE_VALUE) {
CloseHandle (h);
2000-03-24 20:40:41 +00:00
#if 0
2007-06-03 21:07:42 +00:00
WCMD_batch (autoexec, autoexec, 0, NULL, INVALID_HANDLE_VALUE);
2000-03-24 20:40:41 +00:00
#endif
1999-06-06 15:24:04 +00:00
}
/*
* Loop forever getting commands and executing them.
*/
WCMD_version ();
while (TRUE) {
/* Read until EOF (which for std input is never, but if redirect
in place, may occur */
1999-06-06 15:24:04 +00:00
WCMD_show_prompt ();
if (WCMD_ReadAndParseLine(NULL, &toExecute,
GetStdHandle(STD_INPUT_HANDLE)) == NULL)
break;
WCMD_process_commands(toExecute);
WCMD_free_commands(toExecute);
toExecute = NULL;
1999-06-06 15:24:04 +00:00
}
return 0;
1999-06-06 15:24:04 +00:00
}
/*****************************************************************************
* Process one command. If the command is EXIT this routine does not return.
* We will recurse through here executing batch files.
*/
2007-06-03 21:07:42 +00:00
void WCMD_process_command (WCHAR *command)
{
2007-06-03 21:07:42 +00:00
WCHAR *cmd, *p, *s, *t, *redir;
2007-02-23 22:22:57 +00:00
int status, i;
DWORD count, creationDisposition;
HANDLE h;
2007-06-03 21:07:42 +00:00
WCHAR *whichcmd;
SECURITY_ATTRIBUTES sa;
2007-06-03 21:07:42 +00:00
WCHAR *new_cmd;
WCHAR *first_redir = NULL;
HANDLE old_stdhandles[3] = {INVALID_HANDLE_VALUE,
INVALID_HANDLE_VALUE,
INVALID_HANDLE_VALUE};
DWORD idx_stdhandles[3] = {STD_INPUT_HANDLE,
STD_OUTPUT_HANDLE,
STD_ERROR_HANDLE};
2007-02-23 22:21:27 +00:00
/* Move copy of the command onto the heap so it can be expanded */
2007-06-03 21:07:42 +00:00
new_cmd = HeapAlloc( GetProcessHeap(), 0, MAXSTRING * sizeof(WCHAR));
strcpyW(new_cmd, command);
2007-02-23 22:21:27 +00:00
/* For commands in a context (batch program): */
/* Expand environment variables in a batch file %{0-9} first */
/* including support for any ~ modifiers */
/* Additionally: */
/* Expand the DATE, TIME, CD, RANDOM and ERRORLEVEL special */
/* names allowing environment variable overrides */
/* NOTE: To support the %PATH:xxx% syntax, also perform */
/* manual expansion of environment variables here */
2007-02-23 22:21:27 +00:00
p = new_cmd;
2007-06-03 21:07:42 +00:00
while ((p = strchrW(p, '%'))) {
2007-02-23 22:21:27 +00:00
i = *(p+1) - '0';
/* Replace %~ modifications if in batch program */
if (context && *(p+1) == '~') {
WCMD_HandleTildaModifiers(&p, NULL);
p++;
/* Replace use of %0...%9 if in batch program*/
} else if (context && (i >= 0) && (i <= 9)) {
2007-06-03 21:07:42 +00:00
s = WCMD_strdupW(p+2);
2007-03-08 00:50:37 +00:00
t = WCMD_parameter (context -> command, i + context -> shift_count[i], NULL);
2007-06-03 21:07:42 +00:00
strcpyW (p, t);
strcatW (p, s);
2007-02-23 22:21:27 +00:00
free (s);
/* Replace use of %* if in batch program*/
} else if (context && *(p+1)=='*') {
2007-06-03 21:07:42 +00:00
WCHAR *startOfParms = NULL;
s = WCMD_strdupW(p+2);
2007-02-23 22:21:27 +00:00
t = WCMD_parameter (context -> command, 1, &startOfParms);
2007-06-03 21:07:42 +00:00
if (startOfParms != NULL) strcpyW (p, startOfParms);
2007-02-23 22:21:27 +00:00
else *p = 0x00;
2007-06-03 21:07:42 +00:00
strcatW (p, s);
2007-02-23 22:21:27 +00:00
free (s);
} else {
2007-02-23 22:22:57 +00:00
p = WCMD_expand_envvar(p);
}
}
2007-02-23 22:22:57 +00:00
cmd = new_cmd;
2007-02-23 22:21:27 +00:00
/* In a batch program, unknown variables are replace by nothing */
/* so remove any remaining %var% */
if (context) {
p = cmd;
2007-06-03 21:07:42 +00:00
while ((p = strchrW(p, '%'))) {
s = strchrW(p+1, '%');
2007-02-23 22:21:27 +00:00
if (!s) {
*p=0x00;
} else {
2007-06-03 21:07:42 +00:00
t = WCMD_strdupW(s+1);
strcpyW(p, t);
2007-02-23 22:21:27 +00:00
free(t);
}
}
/* Show prompt before batch line IF echo is on and in batch program */
if (echo_mode && (cmd[0] != '@')) {
WCMD_show_prompt();
WCMD_output_asis ( cmd);
WCMD_output_asis ( newline);
2007-02-23 22:21:27 +00:00
}
1999-06-06 15:24:04 +00:00
}
/*
* Changing default drive has to be handled as a special case.
*/
2007-06-03 21:07:42 +00:00
if ((cmd[1] == ':') && IsCharAlpha (cmd[0]) && (strlenW(cmd) == 2)) {
WCHAR envvar[5];
WCHAR dir[MAX_PATH];
/* According to MSDN CreateProcess docs, special env vars record
the current directory on each drive, in the form =C:
so see if one specified, and if so go back to it */
2007-06-03 21:07:42 +00:00
strcpyW(envvar, equalsW);
strcatW(envvar, cmd);
if (GetEnvironmentVariable(envvar, dir, MAX_PATH) == 0) {
2007-06-03 21:07:42 +00:00
static const WCHAR fmt[] = {'%','s','\\','\0'};
wsprintf(cmd, fmt, cmd);
}
1999-06-06 15:24:04 +00:00
status = SetCurrentDirectory (cmd);
if (!status) WCMD_print_error ();
HeapFree( GetProcessHeap(), 0, cmd );
1999-06-06 15:24:04 +00:00
return;
}
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
/*
* Redirect stdin, stdout and/or stderr if required.
*/
2007-06-03 21:07:42 +00:00
if ((p = strchrW(cmd,'<')) != NULL) {
if (first_redir == NULL) first_redir = p;
h = CreateFile (WCMD_parameter (++p, 0, NULL), GENERIC_READ, FILE_SHARE_READ, &sa, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE) {
WCMD_print_error ();
HeapFree( GetProcessHeap(), 0, cmd );
return;
}
old_stdhandles[0] = GetStdHandle (STD_INPUT_HANDLE);
SetStdHandle (STD_INPUT_HANDLE, h);
}
/* Scan the whole command looking for > and 2> */
redir = cmd;
2007-06-03 21:07:42 +00:00
while (redir != NULL && ((p = strchrW(redir,'>')) != NULL)) {
int handle = 0;
if (*(p-1)!='2') {
if (first_redir == NULL) first_redir = p;
handle = 1;
} else {
if (first_redir == NULL) first_redir = (p-1);
handle = 2;
}
p++;
if ('>' == *p) {
creationDisposition = OPEN_ALWAYS;
p++;
}
else {
creationDisposition = CREATE_ALWAYS;
}
2007-03-13 00:10:38 +00:00
/* Add support for 2>&1 */
redir = p;
2007-03-13 00:10:38 +00:00
if (*p == '&') {
int idx = *(p+1) - '0';
if (DuplicateHandle(GetCurrentProcess(),
GetStdHandle(idx_stdhandles[idx]),
GetCurrentProcess(),
&h,
0, TRUE, DUPLICATE_SAME_ACCESS) == 0) {
WINE_FIXME("Duplicating handle failed with gle %d\n", GetLastError());
}
WINE_TRACE("Redirect %d (%p) to %d (%p)\n", handle, GetStdHandle(idx_stdhandles[idx]), idx, h);
} else {
2007-06-03 21:07:42 +00:00
WCHAR *param = WCMD_parameter (p, 0, NULL);
2007-03-13 00:10:38 +00:00
h = CreateFile (param, GENERIC_WRITE, 0, &sa, creationDisposition,
FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE) {
WCMD_print_error ();
HeapFree( GetProcessHeap(), 0, cmd );
return;
}
if (SetFilePointer (h, 0, NULL, FILE_END) ==
INVALID_SET_FILE_POINTER) {
WCMD_print_error ();
}
2007-06-03 21:07:42 +00:00
WINE_TRACE("Redirect %d to '%s' (%p)\n", handle, wine_dbgstr_w(param), h);
}
2007-03-13 00:10:38 +00:00
old_stdhandles[handle] = GetStdHandle (idx_stdhandles[handle]);
SetStdHandle (idx_stdhandles[handle], h);
}
/* Terminate the command string at <, or first 2> or > */
if (first_redir != NULL) *first_redir = '\0';
2002-05-31 23:06:46 +00:00
/*
* Strip leading whitespaces, and a '@' if supplied
*/
whichcmd = WCMD_strtrim_leading_spaces(cmd);
2007-06-03 21:07:42 +00:00
WINE_TRACE("Command: '%s'\n", wine_dbgstr_w(cmd));
2002-05-31 23:06:46 +00:00
if (whichcmd[0] == '@') whichcmd++;
1999-06-06 15:24:04 +00:00
/*
* Check if the command entered is internal. If it is, pass the rest of the
* line down to the command. If not try to run a program.
*/
count = 0;
2002-05-31 23:06:46 +00:00
while (IsCharAlphaNumeric(whichcmd[count])) {
1999-06-06 15:24:04 +00:00
count++;
}
for (i=0; i<=WCMD_EXIT; i++) {
if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
2002-05-31 23:06:46 +00:00
whichcmd, count, inbuilt[i], -1) == 2) break;
1999-06-06 15:24:04 +00:00
}
2002-05-31 23:06:46 +00:00
p = WCMD_strtrim_leading_spaces (&whichcmd[count]);
1999-06-06 15:24:04 +00:00
WCMD_parse (p, quals, param1, param2);
switch (i) {
case WCMD_ATTRIB:
WCMD_setshow_attrib ();
break;
case WCMD_CALL:
WCMD_call (p);
1999-06-06 15:24:04 +00:00
break;
case WCMD_CD:
case WCMD_CHDIR:
WCMD_setshow_default (p);
1999-06-06 15:24:04 +00:00
break;
case WCMD_CLS:
WCMD_clear_screen ();
break;
case WCMD_COPY:
WCMD_copy ();
break;
case WCMD_CTTY:
WCMD_change_tty ();
break;
case WCMD_DATE:
WCMD_setshow_date ();
break;
case WCMD_DEL:
case WCMD_ERASE:
2007-04-23 22:24:38 +00:00
WCMD_delete (p, TRUE);
1999-06-06 15:24:04 +00:00
break;
case WCMD_DIR:
WCMD_directory (p);
1999-06-06 15:24:04 +00:00
break;
case WCMD_ECHO:
2004-03-18 04:01:32 +00:00
WCMD_echo(&whichcmd[count]);
2002-05-31 23:06:46 +00:00
break;
1999-06-06 15:24:04 +00:00
case WCMD_FOR:
WCMD_for (p);
1999-06-06 15:24:04 +00:00
break;
case WCMD_GOTO:
WCMD_goto ();
1999-06-06 15:24:04 +00:00
break;
case WCMD_HELP:
WCMD_give_help (p);
break;
case WCMD_IF:
WCMD_if (p);
1999-06-06 15:24:04 +00:00
break;
case WCMD_LABEL:
WCMD_volume (1, p);
break;
case WCMD_MD:
case WCMD_MKDIR:
WCMD_create_dir ();
break;
case WCMD_MOVE:
WCMD_move ();
break;
case WCMD_PATH:
WCMD_setshow_path (p);
1999-06-06 15:24:04 +00:00
break;
case WCMD_PAUSE:
WCMD_pause ();
break;
case WCMD_PROMPT:
WCMD_setshow_prompt ();
break;
case WCMD_REM:
break;
case WCMD_REN:
case WCMD_RENAME:
WCMD_rename ();
break;
case WCMD_RD:
case WCMD_RMDIR:
2007-03-04 22:33:27 +00:00
WCMD_remove_dir (p);
1999-06-06 15:24:04 +00:00
break;
2004-03-22 22:56:58 +00:00
case WCMD_SETLOCAL:
WCMD_setlocal(p);
break;
case WCMD_ENDLOCAL:
WCMD_endlocal();
break;
1999-06-06 15:24:04 +00:00
case WCMD_SET:
WCMD_setshow_env (p);
break;
case WCMD_SHIFT:
2007-03-08 00:50:37 +00:00
WCMD_shift (p);
1999-06-06 15:24:04 +00:00
break;
case WCMD_TIME:
WCMD_setshow_time ();
2002-05-04 18:29:31 +00:00
break;
case WCMD_TITLE:
2007-06-03 21:07:42 +00:00
if (strlenW(&whichcmd[count]) > 0)
2002-05-31 23:06:46 +00:00
WCMD_title(&whichcmd[count+1]);
2002-05-04 18:29:31 +00:00
break;
1999-06-06 15:24:04 +00:00
case WCMD_TYPE:
WCMD_type (p);
1999-06-06 15:24:04 +00:00
break;
case WCMD_VER:
WCMD_version ();
break;
case WCMD_VERIFY:
WCMD_verify (p);
1999-06-06 15:24:04 +00:00
break;
case WCMD_VOL:
WCMD_volume (0, p);
break;
2007-02-23 22:17:28 +00:00
case WCMD_PUSHD:
2007-03-08 00:44:09 +00:00
WCMD_pushd(p);
2007-02-23 22:17:28 +00:00
break;
case WCMD_POPD:
WCMD_popd();
break;
2007-03-04 22:34:45 +00:00
case WCMD_ASSOC:
2007-03-13 00:06:58 +00:00
WCMD_assoc(p, TRUE);
2007-03-04 22:34:45 +00:00
break;
2007-03-08 00:37:44 +00:00
case WCMD_COLOR:
WCMD_color();
break;
2007-03-13 00:06:58 +00:00
case WCMD_FTYPE:
WCMD_assoc(p, FALSE);
break;
case WCMD_MORE:
WCMD_more(p);
break;
1999-06-06 15:24:04 +00:00
case WCMD_EXIT:
2007-02-20 17:49:08 +00:00
WCMD_exit ();
break;
1999-06-06 15:24:04 +00:00
default:
WCMD_run_program (whichcmd, 0);
}
HeapFree( GetProcessHeap(), 0, cmd );
/* Restore old handles */
for (i=0; i<3; i++) {
if (old_stdhandles[i] != INVALID_HANDLE_VALUE) {
CloseHandle (GetStdHandle (idx_stdhandles[i]));
SetStdHandle (idx_stdhandles[i], old_stdhandles[i]);
}
}
}
static void init_msvcrt_io_block(STARTUPINFO* st)
{
STARTUPINFO st_p;
/* fetch the parent MSVCRT info block if any, so that the child can use the
* same handles as its grand-father
*/
st_p.cb = sizeof(STARTUPINFO);
GetStartupInfo(&st_p);
st->cbReserved2 = st_p.cbReserved2;
st->lpReserved2 = st_p.lpReserved2;
2007-03-04 22:39:22 +00:00
if (st_p.cbReserved2 && st_p.lpReserved2)
{
/* Override the entries for fd 0,1,2 if we happened
* to change those std handles (this depends on the way wcmd sets
* it's new input & output handles)
*/
2007-06-03 21:07:42 +00:00
size_t sz = max(sizeof(unsigned) + (sizeof(WCHAR) + sizeof(HANDLE)) * 3, st_p.cbReserved2);
2005-08-19 10:04:03 +00:00
BYTE* ptr = HeapAlloc(GetProcessHeap(), 0, sz);
if (ptr)
{
unsigned num = *(unsigned*)st_p.lpReserved2;
2007-06-03 21:07:42 +00:00
WCHAR* flags = (WCHAR*)(ptr + sizeof(unsigned));
HANDLE* handles = (HANDLE*)(flags + num * sizeof(WCHAR));
memcpy(ptr, st_p.lpReserved2, st_p.cbReserved2);
st->cbReserved2 = sz;
st->lpReserved2 = ptr;
2005-08-19 10:04:03 +00:00
#define WX_OPEN 0x01 /* see dlls/msvcrt/file.c */
if (num <= 0 || (flags[0] & WX_OPEN))
{
handles[0] = GetStdHandle(STD_INPUT_HANDLE);
flags[0] |= WX_OPEN;
}
if (num <= 1 || (flags[1] & WX_OPEN))
{
handles[1] = GetStdHandle(STD_OUTPUT_HANDLE);
flags[1] |= WX_OPEN;
}
if (num <= 2 || (flags[2] & WX_OPEN))
{
handles[2] = GetStdHandle(STD_ERROR_HANDLE);
flags[2] |= WX_OPEN;
}
#undef WX_OPEN
}
}
}
1999-06-06 15:24:04 +00:00
/******************************************************************************
* WCMD_run_program
*
* Execute a command line as an external program. Must allow recursion.
1999-06-06 15:24:04 +00:00
*
* Precedence:
* Manual testing under windows shows PATHEXT plays a key part in this,
* and the search algorithm and precedence appears to be as follows.
*
* Search locations:
* If directory supplied on command, just use that directory
* If extension supplied on command, look for that explicit name first
* Otherwise, search in each directory on the path
* Precedence:
* If extension supplied on command, look for that explicit name first
* Then look for supplied name .* (even if extension supplied, so
* 'garbage.exe' will match 'garbage.exe.cmd')
* If any found, cycle through PATHEXT looking for name.exe one by one
* Launching
* Once a match has been found, it is launched - Code currently uses
* findexecutable to acheive this which is left untouched.
1999-06-06 15:24:04 +00:00
*/
2007-06-03 21:07:42 +00:00
void WCMD_run_program (WCHAR *command, int called) {
1999-06-06 15:24:04 +00:00
2007-06-03 21:07:42 +00:00
WCHAR temp[MAX_PATH];
WCHAR pathtosearch[MAXSTRING];
WCHAR *pathposn;
WCHAR stemofsearch[MAX_PATH];
WCHAR *lastSlash;
WCHAR pathext[MAXSTRING];
BOOL extensionsupplied = FALSE;
BOOL launched = FALSE;
BOOL status;
BOOL assumeInternal = FALSE;
DWORD len;
2007-06-03 21:07:42 +00:00
static const WCHAR envPath[] = {'P','A','T','H','\0'};
static const WCHAR envPathExt[] = {'P','A','T','H','E','X','T','\0'};
static const WCHAR delims[] = {'/','\\',':','\0'};
1999-06-06 15:24:04 +00:00
WCMD_parse (command, quals, param1, param2); /* Quick way to get the filename */
if (!(*param1) && !(*param2))
return;
/* Calculate the search path and stem to search for */
2007-06-03 21:07:42 +00:00
if (strpbrkW (param1, delims) == NULL) { /* No explicit path given, search path */
static const WCHAR curDir[] = {'.',';','\0'};
strcpyW(pathtosearch, curDir);
len = GetEnvironmentVariable (envPath, &pathtosearch[2], (sizeof(pathtosearch)/sizeof(WCHAR))-2);
if ((len == 0) || (len >= (sizeof(pathtosearch)/sizeof(WCHAR)) - 2)) {
static const WCHAR curDir[] = {'.','\0'};
strcpyW (pathtosearch, curDir);
}
2007-06-03 21:07:42 +00:00
if (strchrW(param1, '.') != NULL) extensionsupplied = TRUE;
strcpyW(stemofsearch, param1);
} else {
/* Convert eg. ..\fred to include a directory by removing file part */
2007-06-03 21:07:42 +00:00
GetFullPathName(param1, sizeof(pathtosearch)/sizeof(WCHAR), pathtosearch, NULL);
lastSlash = strrchrW(pathtosearch, '\\');
if (lastSlash && strchrW(lastSlash, '.') != NULL) extensionsupplied = TRUE;
if (lastSlash) *lastSlash = 0x00;
2007-06-03 21:07:42 +00:00
strcpyW(stemofsearch, lastSlash+1);
1999-06-06 15:24:04 +00:00
}
/* Now extract PATHEXT */
2007-06-03 21:07:42 +00:00
len = GetEnvironmentVariable (envPathExt, pathext, sizeof(pathext)/sizeof(WCHAR));
if ((len == 0) || (len >= (sizeof(pathext)/sizeof(WCHAR)))) {
static const WCHAR dfltPathExt[] = {'.','b','a','t',';',
'.','c','o','m',';',
'.','c','m','d',';',
'.','e','x','e','\0'};
strcpyW (pathext, dfltPathExt);
}
/* Loop through the search path, dir by dir */
pathposn = pathtosearch;
2007-06-03 21:07:42 +00:00
WINE_TRACE("Searching in '%s' for '%s'\n", wine_dbgstr_w(pathtosearch),
wine_dbgstr_w(stemofsearch));
while (!launched && pathposn) {
2007-06-03 21:07:42 +00:00
WCHAR thisDir[MAX_PATH] = {'\0'};
WCHAR *pos = NULL;
BOOL found = FALSE;
2007-06-03 21:07:42 +00:00
const WCHAR slashW[] = {'\\','\0'};
/* Work on the first directory on the search path */
2007-06-03 21:07:42 +00:00
pos = strchrW(pathposn, ';');
if (pos) {
2007-06-03 21:07:42 +00:00
memcpy(thisDir, pathposn, (pos-pathposn) * sizeof(WCHAR));
thisDir[(pos-pathposn)] = 0x00;
pathposn = pos+1;
} else {
2007-06-03 21:07:42 +00:00
strcpyW(thisDir, pathposn);
pathposn = NULL;
1999-06-06 15:24:04 +00:00
}
/* Since you can have eg. ..\.. on the path, need to expand
to full information */
2007-06-03 21:07:42 +00:00
strcpyW(temp, thisDir);
GetFullPathName(temp, MAX_PATH, thisDir, NULL);
/* 1. If extension supplied, see if that file exists */
2007-06-03 21:07:42 +00:00
strcatW(thisDir, slashW);
strcatW(thisDir, stemofsearch);
pos = &thisDir[strlenW(thisDir)]; /* Pos = end of name */
/* 1. If extension supplied, see if that file exists */
if (extensionsupplied) {
if (GetFileAttributes(thisDir) != INVALID_FILE_ATTRIBUTES) {
found = TRUE;
}
}
/* 2. Any .* matches? */
if (!found) {
HANDLE h;
WIN32_FIND_DATA finddata;
2007-06-03 21:07:42 +00:00
static const WCHAR allFiles[] = {'.','*','\0'};
2007-06-03 21:07:42 +00:00
strcatW(thisDir,allFiles);
h = FindFirstFile(thisDir, &finddata);
FindClose(h);
1999-06-06 15:24:04 +00:00
if (h != INVALID_HANDLE_VALUE) {
2007-06-03 21:07:42 +00:00
WCHAR *thisExt = pathext;
/* 3. Yes - Try each path ext */
while (thisExt) {
2007-06-03 21:07:42 +00:00
WCHAR *nextExt = strchrW(thisExt, ';');
if (nextExt) {
2007-06-03 21:07:42 +00:00
memcpy(pos, thisExt, (nextExt-thisExt) * sizeof(WCHAR));
pos[(nextExt-thisExt)] = 0x00;
thisExt = nextExt+1;
} else {
2007-06-03 21:07:42 +00:00
strcpyW(pos, thisExt);
thisExt = NULL;
}
if (GetFileAttributes(thisDir) != INVALID_FILE_ATTRIBUTES) {
found = TRUE;
thisExt = NULL;
}
}
}
}
/* Internal programs won't be picked up by this search, so even
though not found, try one last createprocess and wait for it
to complete.
Note: Ideally we could tell between a console app (wait) and a
windows app, but the API's for it fail in this case */
if (!found && pathposn == NULL) {
WINE_TRACE("ASSUMING INTERNAL\n");
assumeInternal = TRUE;
} else {
2007-06-03 21:07:42 +00:00
WINE_TRACE("Found as %s\n", wine_dbgstr_w(thisDir));
}
/* Once found, launch it */
if (found || assumeInternal) {
STARTUPINFO st;
PROCESS_INFORMATION pe;
SHFILEINFO psfi;
DWORD console;
HINSTANCE hinst;
2007-06-03 21:07:42 +00:00
WCHAR *ext = strrchrW( thisDir, '.' );
static const WCHAR batExt[] = {'.','b','a','t','\0'};
static const WCHAR cmdExt[] = {'.','c','m','d','\0'};
launched = TRUE;
/* Special case BAT and CMD */
2007-06-03 21:07:42 +00:00
if (ext && !strcmpiW(ext, batExt)) {
WCMD_batch (thisDir, command, called, NULL, INVALID_HANDLE_VALUE);
return;
2007-06-03 21:07:42 +00:00
} else if (ext && !strcmpiW(ext, cmdExt)) {
WCMD_batch (thisDir, command, called, NULL, INVALID_HANDLE_VALUE);
return;
} else {
/* thisDir contains the file to be launched, but with what?
eg. a.exe will require a.exe to be launched, a.html may be iexplore */
hinst = FindExecutable (thisDir, NULL, temp);
if ((INT_PTR)hinst < 32)
console = 0;
else
console = SHGetFileInfo (temp, 0, &psfi, sizeof(psfi), SHGFI_EXETYPE);
ZeroMemory (&st, sizeof(STARTUPINFO));
st.cb = sizeof(STARTUPINFO);
init_msvcrt_io_block(&st);
/* Launch the process and if a CUI wait on it to complete
Note: Launching internal wine processes cannot specify a full path to exe */
status = CreateProcess (assumeInternal?NULL : thisDir,
command, NULL, NULL, TRUE, 0, NULL, NULL, &st, &pe);
if ((opt_c || opt_k) && !opt_s && !status
&& GetLastError()==ERROR_FILE_NOT_FOUND && command[0]=='\"') {
2007-06-03 21:07:42 +00:00
/* strip first and last quote WCHARacters and try again */
WCMD_opt_s_strip_quotes(command);
opt_s=1;
WCMD_run_program(command, called);
return;
}
if (!status) {
WCMD_print_error ();
/* If a command fails to launch, it sets errorlevel 9009 - which
does not seem to have any associated constant definition */
errorlevel = 9009;
return;
}
if (!assumeInternal && !console) errorlevel = 0;
else
{
if (assumeInternal || !HIWORD(console)) WaitForSingleObject (pe.hProcess, INFINITE);
GetExitCodeProcess (pe.hProcess, &errorlevel);
if (errorlevel == STILL_ACTIVE) errorlevel = 0;
}
CloseHandle(pe.hProcess);
CloseHandle(pe.hThread);
1999-06-06 15:24:04 +00:00
return;
}
}
}
/* Not found anywhere - give up */
SetLastError(ERROR_FILE_NOT_FOUND);
WCMD_print_error ();
/* If a command fails to launch, it sets errorlevel 9009 - which
does not seem to have any associated constant definition */
errorlevel = 9009;
return;
1999-06-06 15:24:04 +00:00
}
/******************************************************************************
* WCMD_show_prompt
*
* Display the prompt on STDout
*
*/
void WCMD_show_prompt (void) {
1999-06-06 15:24:04 +00:00
int status;
2007-06-03 21:07:42 +00:00
WCHAR out_string[MAX_PATH], curdir[MAX_PATH], prompt_string[MAX_PATH];
WCHAR *p, *q;
DWORD len;
2007-06-03 21:07:42 +00:00
static const WCHAR envPrompt[] = {'P','R','O','M','P','T','\0'};
1999-06-06 15:24:04 +00:00
2007-06-03 21:07:42 +00:00
len = GetEnvironmentVariable (envPrompt, prompt_string,
sizeof(prompt_string)/sizeof(WCHAR));
if ((len == 0) || (len >= (sizeof(prompt_string)/sizeof(WCHAR)))) {
const WCHAR dfltPrompt[] = {'$','P','$','G','\0'};
strcpyW (prompt_string, dfltPrompt);
1999-06-06 15:24:04 +00:00
}
p = prompt_string;
q = out_string;
*q = '\0';
while (*p != '\0') {
if (*p != '$') {
*q++ = *p++;
*q = '\0';
}
else {
p++;
switch (toupper(*p)) {
case '$':
*q++ = '$';
break;
2007-03-04 22:33:51 +00:00
case 'A':
*q++ = '&';
break;
1999-06-06 15:24:04 +00:00
case 'B':
*q++ = '|';
break;
2007-03-04 22:33:51 +00:00
case 'C':
*q++ = '(';
break;
1999-06-06 15:24:04 +00:00
case 'D':
GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, NULL, q, MAX_PATH);
while (*q) q++;
break;
case 'E':
*q++ = '\E';
break;
2007-03-04 22:33:51 +00:00
case 'F':
*q++ = ')';
break;
1999-06-06 15:24:04 +00:00
case 'G':
*q++ = '>';
break;
2007-03-04 22:33:51 +00:00
case 'H':
*q++ = '\b';
break;
1999-06-06 15:24:04 +00:00
case 'L':
*q++ = '<';
break;
case 'N':
2007-06-03 21:07:42 +00:00
status = GetCurrentDirectory (sizeof(curdir)/sizeof(WCHAR), curdir);
1999-06-06 15:24:04 +00:00
if (status) {
*q++ = curdir[0];
}
break;
case 'P':
2007-06-03 21:07:42 +00:00
status = GetCurrentDirectory (sizeof(curdir)/sizeof(WCHAR), curdir);
1999-06-06 15:24:04 +00:00
if (status) {
2007-06-03 21:07:42 +00:00
strcatW (q, curdir);
1999-06-06 15:24:04 +00:00
while (*q) q++;
}
break;
case 'Q':
*q++ = '=';
break;
2007-03-04 22:33:51 +00:00
case 'S':
*q++ = ' ';
break;
1999-06-06 15:24:04 +00:00
case 'T':
GetTimeFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL, q, MAX_PATH);
while (*q) q++;
break;
2007-06-03 21:07:42 +00:00
case 'V':
strcatW (q, version_string);
2007-03-04 22:33:51 +00:00
while (*q) q++;
break;
1999-06-06 15:24:04 +00:00
case '_':
*q++ = '\n';
break;
case '+':
if (pushd_directories) {
memset(q, '+', pushd_directories->u.stackdepth);
q = q + pushd_directories->u.stackdepth;
}
break;
1999-06-06 15:24:04 +00:00
}
p++;
*q = '\0';
}
}
WCMD_output_asis (out_string);
1999-06-06 15:24:04 +00:00
}
/****************************************************************************
* WCMD_print_error
*
* Print the message for GetLastError
1999-06-06 15:24:04 +00:00
*/
void WCMD_print_error (void) {
LPVOID lpMsgBuf;
DWORD error_code;
int status;
1999-06-06 15:24:04 +00:00
error_code = GetLastError ();
status = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, error_code, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
if (!status) {
WINE_FIXME ("Cannot display message for error %d, status %d\n",
error_code, GetLastError());
return;
1999-06-06 15:24:04 +00:00
}
WCMD_output_asis (lpMsgBuf);
LocalFree ((HLOCAL)lpMsgBuf);
WCMD_output_asis (newline);
1999-06-06 15:24:04 +00:00
return;
}
/*******************************************************************
* WCMD_parse - parse a command into parameters and qualifiers.
*
* On exit, all qualifiers are concatenated into q, the first string
* not beginning with "/" is in p1 and the
* second in p2. Any subsequent non-qualifier strings are lost.
* Parameters in quotes are handled.
*/
2007-06-03 21:07:42 +00:00
void WCMD_parse (WCHAR *s, WCHAR *q, WCHAR *p1, WCHAR *p2) {
1999-06-06 15:24:04 +00:00
int p = 0;
*q = *p1 = *p2 = '\0';
while (TRUE) {
switch (*s) {
case '/':
*q++ = *s++;
while ((*s != '\0') && (*s != ' ') && *s != '/') {
*q++ = toupper (*s++);
}
*q = '\0';
break;
case ' ':
2005-10-03 10:16:48 +00:00
case '\t':
1999-06-06 15:24:04 +00:00
s++;
break;
case '"':
s++;
while ((*s != '\0') && (*s != '"')) {
if (p == 0) *p1++ = *s++;
else if (p == 1) *p2++ = *s++;
else s++;
}
if (p == 0) *p1 = '\0';
if (p == 1) *p2 = '\0';
p++;
if (*s == '"') s++;
break;
case '\0':
return;
default:
2005-10-03 10:16:48 +00:00
while ((*s != '\0') && (*s != ' ') && (*s != '\t')) {
1999-06-06 15:24:04 +00:00
if (p == 0) *p1++ = *s++;
else if (p == 1) *p2++ = *s++;
else s++;
}
if (p == 0) *p1 = '\0';
if (p == 1) *p2 = '\0';
p++;
}
}
}
/*******************************************************************
* WCMD_output_asis_len - send output to current standard output
2007-06-03 21:07:42 +00:00
*
* Output a formatted unicode string. Ideally this will go to the console
* and hence required WriteConsoleW to output it, however if file i/o is
* redirected, it needs to be WriteFile'd using OEM (not ANSI) format
*/
2007-06-03 21:07:42 +00:00
static void WCMD_output_asis_len(const WCHAR *message, int len) {
DWORD nOut= 0;
DWORD res = 0;
/* If nothing to write, return (MORE does this sometimes) */
if (!len) return;
/* Try to write as unicode assuming it is to a console */
res = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),
message, len, &nOut, NULL);
/* If writing to console fails, assume its file
i/o so convert to OEM codepage and output */
if (!res) {
BOOL usedDefaultChar = FALSE;
DWORD convertedChars;
if (!unicodePipes) {
/*
* Allocate buffer to use when writing to file. (Not freed, as one off)
*/
if (!output_bufA) output_bufA = HeapAlloc(GetProcessHeap(), 0,
MAX_WRITECONSOLE_SIZE);
if (!output_bufA) {
WINE_FIXME("Out of memory - could not allocate ansi 64K buffer\n");
return;
}
2007-06-03 21:07:42 +00:00
/* Convert to OEM, then output */
convertedChars = WideCharToMultiByte(GetConsoleOutputCP(), 0, message,
len, output_bufA, MAX_WRITECONSOLE_SIZE,
"?", &usedDefaultChar);
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), output_bufA, convertedChars,
&nOut, FALSE);
} else {
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), message, len*sizeof(WCHAR),
&nOut, FALSE);
}
2007-06-03 21:07:42 +00:00
}
return;
}
1999-06-06 15:24:04 +00:00
/*******************************************************************
* WCMD_output - send output to current standard output device.
*
*/
2007-06-03 21:07:42 +00:00
void WCMD_output (const WCHAR *format, ...) {
1999-06-06 15:24:04 +00:00
va_list ap;
2007-06-03 21:07:42 +00:00
WCHAR string[1024];
int ret;
1999-06-06 15:24:04 +00:00
va_start(ap,format);
2007-06-03 21:07:42 +00:00
ret = wvsprintf (string, format, ap);
if( ret >= (sizeof(string)/sizeof(WCHAR))) {
WINE_ERR("Output truncated in WCMD_output\n" );
2007-06-03 21:07:42 +00:00
ret = (sizeof(string)/sizeof(WCHAR)) - 1;
string[ret] = '\0';
}
2007-06-03 21:07:42 +00:00
va_end(ap);
WCMD_output_asis_len(string, ret);
}
static int line_count;
static int max_height;
2007-06-03 21:07:42 +00:00
static int max_width;
static BOOL paged_mode;
2007-06-03 21:07:42 +00:00
static int numChars;
2007-06-03 21:07:42 +00:00
void WCMD_enter_paged_mode(const WCHAR *msg)
{
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
2007-06-03 21:07:42 +00:00
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo)) {
max_height = consoleInfo.dwSize.Y;
2007-06-03 21:07:42 +00:00
max_width = consoleInfo.dwSize.X;
} else {
max_height = 25;
2007-06-03 21:07:42 +00:00
max_width = 80;
}
paged_mode = TRUE;
line_count = 0;
2007-06-03 21:07:42 +00:00
numChars = 0;
pagedMessage = (msg==NULL)? anykey : msg;
}
void WCMD_leave_paged_mode(void)
{
paged_mode = FALSE;
pagedMessage = NULL;
1999-06-06 15:24:04 +00:00
}
2002-05-31 23:06:46 +00:00
/*******************************************************************
* WCMD_output_asis - send output to current standard output device.
* without formatting eg. when message contains '%'
*/
2007-06-03 21:07:42 +00:00
void WCMD_output_asis (const WCHAR *message) {
DWORD count;
2007-06-03 21:07:42 +00:00
const WCHAR* ptr;
WCHAR string[1024];
if (paged_mode) {
do {
2007-06-03 21:07:42 +00:00
ptr = message;
while (*ptr && *ptr!='\n' && (numChars < max_width)) {
numChars++;
ptr++;
};
if (*ptr == '\n') ptr++;
WCMD_output_asis_len(message, (ptr) ? ptr - message : strlenW(message));
if (ptr) {
2007-06-03 21:07:42 +00:00
numChars = 0;
if (++line_count >= max_height - 1) {
2007-06-03 21:07:42 +00:00
line_count = 0;
WCMD_output_asis_len(pagedMessage, strlenW(pagedMessage));
WCMD_ReadFile (GetStdHandle(STD_INPUT_HANDLE), string,
sizeof(string)/sizeof(WCHAR), &count, NULL);
}
}
2007-06-03 21:07:42 +00:00
} while (((message = ptr) != NULL) && (*ptr));
} else {
WCMD_output_asis_len(message, lstrlen(message));
}
}
2007-06-03 21:07:42 +00:00
/***************************************************************************
* WCMD_strtrim_leading_spaces
*
* Remove leading spaces from a string. Return a pointer to the first
1999-06-06 15:24:04 +00:00
* non-space character. Does not modify the input string
*/
2007-06-03 21:07:42 +00:00
WCHAR *WCMD_strtrim_leading_spaces (WCHAR *string) {
1999-06-06 15:24:04 +00:00
2007-06-03 21:07:42 +00:00
WCHAR *ptr;
1999-06-06 15:24:04 +00:00
ptr = string;
while (*ptr == ' ') ptr++;
return ptr;
}
/*************************************************************************
* WCMD_strtrim_trailing_spaces
*
* Remove trailing spaces from a string. This routine modifies the input
2007-06-03 21:07:42 +00:00
* string by placing a null after the last non-space WCHARacter
1999-06-06 15:24:04 +00:00
*/
2007-06-03 21:07:42 +00:00
void WCMD_strtrim_trailing_spaces (WCHAR *string) {
1999-06-06 15:24:04 +00:00
2007-06-03 21:07:42 +00:00
WCHAR *ptr;
1999-06-06 15:24:04 +00:00
2007-06-03 21:07:42 +00:00
ptr = string + strlenW (string) - 1;
1999-06-06 15:24:04 +00:00
while ((*ptr == ' ') && (ptr >= string)) {
*ptr = '\0';
ptr--;
}
}
/*************************************************************************
* WCMD_opt_s_strip_quotes
*
2007-06-03 21:07:42 +00:00
* Remove first and last quote WCHARacters, preserving all other text
*/
2007-06-03 21:07:42 +00:00
void WCMD_opt_s_strip_quotes(WCHAR *cmd) {
WCHAR *src = cmd + 1, *dest = cmd, *lastq = NULL;
while((*dest=*src) != '\0') {
if (*src=='\"')
lastq=dest;
dest++, src++;
}
if (lastq) {
dest=lastq++;
while ((*dest++=*lastq++) != 0)
;
}
}
/*************************************************************************
* WCMD_pipe
*
* Handle pipes within a command - the DOS way using temporary files.
*/
void WCMD_pipe (CMD_LIST **cmdEntry) {
2007-06-03 21:07:42 +00:00
WCHAR *p;
WCHAR *command = (*cmdEntry)->command;
2007-06-03 21:07:42 +00:00
WCHAR temp_path[MAX_PATH], temp_file[MAX_PATH], temp_file2[MAX_PATH], temp_cmd[1024];
static const WCHAR redirOut[] = {'%','s',' ','>',' ','%','s','\0'};
static const WCHAR redirIn[] = {'%','s',' ','<',' ','%','s','\0'};
static const WCHAR redirBoth[]= {'%','s',' ','<',' ','%','s',' ','>','%','s','\0'};
static const WCHAR cmdW[] = {'C','M','D','\0'};
2007-06-03 21:07:42 +00:00
GetTempPath (sizeof(temp_path)/sizeof(WCHAR), temp_path);
GetTempFileName (temp_path, cmdW, 0, temp_file);
p = strchrW(command, '|');
*p++ = '\0';
2007-06-03 21:07:42 +00:00
wsprintf (temp_cmd, redirOut, command, temp_file);
WCMD_process_command (temp_cmd);
command = p;
2007-06-03 21:07:42 +00:00
while ((p = strchrW(command, '|'))) {
*p++ = '\0';
2007-06-03 21:07:42 +00:00
GetTempFileName (temp_path, cmdW, 0, temp_file2);
wsprintf (temp_cmd, redirBoth, command, temp_file, temp_file2);
WCMD_process_command (temp_cmd);
DeleteFile (temp_file);
2007-06-03 21:07:42 +00:00
strcpyW (temp_file, temp_file2);
command = p;
}
2007-06-03 21:07:42 +00:00
wsprintf (temp_cmd, redirIn, command, temp_file);
WCMD_process_command (temp_cmd);
DeleteFile (temp_file);
}
2007-02-23 22:22:57 +00:00
/*************************************************************************
* WCMD_expand_envvar
*
2007-06-03 21:07:42 +00:00
* Expands environment variables, allowing for WCHARacter substitution
2007-02-23 22:22:57 +00:00
*/
2007-06-03 21:07:42 +00:00
static WCHAR *WCMD_expand_envvar(WCHAR *start) {
WCHAR *endOfVar = NULL, *s;
WCHAR *colonpos = NULL;
WCHAR thisVar[MAXSTRING];
WCHAR thisVarContents[MAXSTRING];
WCHAR savedchar = 0x00;
2007-02-23 22:22:57 +00:00
int len;
2007-06-03 21:07:42 +00:00
static const WCHAR ErrorLvl[] = {'E','R','R','O','R','L','E','V','E','L','\0'};
static const WCHAR ErrorLvlP[] = {'%','E','R','R','O','R','L','E','V','E','L','%','\0'};
static const WCHAR Date[] = {'D','A','T','E','\0'};
static const WCHAR DateP[] = {'%','D','A','T','E','%','\0'};
static const WCHAR Time[] = {'T','I','M','E','\0'};
static const WCHAR TimeP[] = {'%','T','I','M','E','%','\0'};
static const WCHAR Cd[] = {'C','D','\0'};
static const WCHAR CdP[] = {'%','C','D','%','\0'};
static const WCHAR Random[] = {'R','A','N','D','O','M','\0'};
static const WCHAR RandomP[] = {'%','R','A','N','D','O','M','%','\0'};
2007-02-23 22:22:57 +00:00
/* Find the end of the environment variable, and extract name */
2007-06-03 21:07:42 +00:00
endOfVar = strchrW(start+1, '%');
2007-02-23 22:22:57 +00:00
if (endOfVar == NULL) {
/* In batch program, missing terminator for % and no following
':' just removes the '%' */
s = WCMD_strdupW(start + 1);
strcpyW (start, s);
free(s);
/* FIXME: Some other special conditions here depending on whether
2007-02-23 22:22:57 +00:00
in batch, complex or not, and whether env var exists or not! */
return start;
2007-02-23 22:22:57 +00:00
}
2007-06-03 21:07:42 +00:00
memcpy(thisVar, start, ((endOfVar - start) + 1) * sizeof(WCHAR));
2007-02-23 22:22:57 +00:00
thisVar[(endOfVar - start)+1] = 0x00;
2007-06-03 21:07:42 +00:00
colonpos = strchrW(thisVar+1, ':');
2007-02-23 22:22:57 +00:00
/* If there's complex substitution, just need %var% for now
to get the expanded data to play with */
if (colonpos) {
*colonpos = '%';
savedchar = *(colonpos+1);
*(colonpos+1) = 0x00;
}
2007-06-03 21:07:42 +00:00
WINE_TRACE("Retrieving contents of %s\n", wine_dbgstr_w(thisVar));
2007-02-23 22:22:57 +00:00
/* Expand to contents, if unchanged, return */
/* Handle DATE, TIME, ERRORLEVEL and CD replacements allowing */
/* override if existing env var called that name */
if ((CompareString (LOCALE_USER_DEFAULT,
NORM_IGNORECASE | SORT_STRINGSORT,
2007-06-03 21:07:42 +00:00
thisVar, 12, ErrorLvlP, -1) == 2) &&
(GetEnvironmentVariable(ErrorLvl, thisVarContents, 1) == 0) &&
(GetLastError() == ERROR_ENVVAR_NOT_FOUND)) {
2007-06-03 21:07:42 +00:00
static const WCHAR fmt[] = {'%','d','\0'};
wsprintf(thisVarContents, fmt, errorlevel);
len = strlenW(thisVarContents);
} else if ((CompareString (LOCALE_USER_DEFAULT,
NORM_IGNORECASE | SORT_STRINGSORT,
2007-06-03 21:07:42 +00:00
thisVar, 6, DateP, -1) == 2) &&
(GetEnvironmentVariable(Date, thisVarContents, 1) == 0) &&
(GetLastError() == ERROR_ENVVAR_NOT_FOUND)) {
GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL,
NULL, thisVarContents, MAXSTRING);
2007-06-03 21:07:42 +00:00
len = strlenW(thisVarContents);
} else if ((CompareString (LOCALE_USER_DEFAULT,
NORM_IGNORECASE | SORT_STRINGSORT,
2007-06-03 21:07:42 +00:00
thisVar, 6, TimeP, -1) == 2) &&
(GetEnvironmentVariable(Time, thisVarContents, 1) == 0) &&
(GetLastError() == ERROR_ENVVAR_NOT_FOUND)) {
GetTimeFormat(LOCALE_USER_DEFAULT, TIME_NOSECONDS, NULL,
NULL, thisVarContents, MAXSTRING);
2007-06-03 21:07:42 +00:00
len = strlenW(thisVarContents);
} else if ((CompareString (LOCALE_USER_DEFAULT,
NORM_IGNORECASE | SORT_STRINGSORT,
2007-06-03 21:07:42 +00:00
thisVar, 4, CdP, -1) == 2) &&
(GetEnvironmentVariable(Cd, thisVarContents, 1) == 0) &&
(GetLastError() == ERROR_ENVVAR_NOT_FOUND)) {
GetCurrentDirectory (MAXSTRING, thisVarContents);
2007-06-03 21:07:42 +00:00
len = strlenW(thisVarContents);
} else if ((CompareString (LOCALE_USER_DEFAULT,
NORM_IGNORECASE | SORT_STRINGSORT,
2007-06-03 21:07:42 +00:00
thisVar, 8, RandomP, -1) == 2) &&
(GetEnvironmentVariable(Random, thisVarContents, 1) == 0) &&
(GetLastError() == ERROR_ENVVAR_NOT_FOUND)) {
2007-06-03 21:07:42 +00:00
static const WCHAR fmt[] = {'%','d','\0'};
wsprintf(thisVarContents, fmt, rand() % 32768);
len = strlenW(thisVarContents);
} else {
len = ExpandEnvironmentStrings(thisVar, thisVarContents,
2007-06-03 21:07:42 +00:00
sizeof(thisVarContents)/sizeof(WCHAR));
}
2007-02-23 22:22:57 +00:00
if (len == 0)
return endOfVar+1;
/* In a batch program, unknown env vars are replaced with nothing,
note syntax %garbage:1,3% results in anything after the ':'
except the %
From the command line, you just get back what you entered */
2007-06-03 21:07:42 +00:00
if (lstrcmpiW(thisVar, thisVarContents) == 0) {
2007-02-23 22:22:57 +00:00
/* Restore the complex part after the compare */
if (colonpos) {
*colonpos = ':';
*(colonpos+1) = savedchar;
}
2007-06-03 21:07:42 +00:00
s = WCMD_strdupW(endOfVar + 1);
2007-02-23 22:22:57 +00:00
/* Command line - just ignore this */
if (context == NULL) return endOfVar+1;
/* Batch - replace unknown env var with nothing */
if (colonpos == NULL) {
2007-06-03 21:07:42 +00:00
strcpyW (start, s);
2007-02-23 22:22:57 +00:00
} else {
2007-06-03 21:07:42 +00:00
len = strlenW(thisVar);
2007-02-23 22:22:57 +00:00
thisVar[len-1] = 0x00;
/* If %:...% supplied, : is retained */
if (colonpos == thisVar+1) {
2007-06-03 21:07:42 +00:00
strcpyW (start, colonpos);
2007-02-23 22:22:57 +00:00
} else {
2007-06-03 21:07:42 +00:00
strcpyW (start, colonpos+1);
2007-02-23 22:22:57 +00:00
}
2007-06-03 21:07:42 +00:00
strcatW (start, s);
2007-02-23 22:22:57 +00:00
}
free (s);
return start;
}
/* See if we need to do complex substitution (any ':'s), if not
then our work here is done */
if (colonpos == NULL) {
2007-06-03 21:07:42 +00:00
s = WCMD_strdupW(endOfVar + 1);
strcpyW (start, thisVarContents);
strcatW (start, s);
2007-02-23 22:22:57 +00:00
free(s);
return start;
}
/* Restore complex bit */
*colonpos = ':';
*(colonpos+1) = savedchar;
/*
Handle complex substitutions:
xxx=yyy (replace xxx with yyy)
*xxx=yyy (replace up to and including xxx with yyy)
2007-06-03 21:07:42 +00:00
~x (from x WCHARs in)
~-x (from x WCHARs from the end)
~x,y (from x WCHARs in for y WCHARacters)
~x,-y (from x WCHARs in until y WCHARacters from the end)
2007-02-23 22:22:57 +00:00
*/
/* ~ is substring manipulation */
if (savedchar == '~') {
2007-03-16 12:02:06 +00:00
int substrposition, substrlength = 0;
2007-06-03 21:07:42 +00:00
WCHAR *commapos = strchrW(colonpos+2, ',');
WCHAR *startCopy;
2007-02-23 22:22:57 +00:00
2007-06-03 21:07:42 +00:00
substrposition = atolW(colonpos+2);
if (commapos) substrlength = atolW(commapos+1);
2007-02-23 22:22:57 +00:00
2007-06-03 21:07:42 +00:00
s = WCMD_strdupW(endOfVar + 1);
2007-02-23 22:22:57 +00:00
/* Check bounds */
if (substrposition >= 0) {
startCopy = &thisVarContents[min(substrposition, len)];
} else {
startCopy = &thisVarContents[max(0, len+substrposition-1)];
}
if (commapos == NULL) {
2007-06-03 21:07:42 +00:00
strcpyW (start, startCopy); /* Copy the lot */
2007-02-23 22:22:57 +00:00
} else if (substrlength < 0) {
int copybytes = (len+substrlength-1)-(startCopy-thisVarContents);
if (copybytes > len) copybytes = len;
else if (copybytes < 0) copybytes = 0;
2007-06-03 21:07:42 +00:00
memcpy (start, startCopy, copybytes * sizeof(WCHAR)); /* Copy the lot */
2007-02-23 22:22:57 +00:00
start[copybytes] = 0x00;
} else {
2007-06-03 21:07:42 +00:00
memcpy (start, startCopy, substrlength * sizeof(WCHAR)); /* Copy the lot */
2007-02-23 22:22:57 +00:00
start[substrlength] = 0x00;
}
2007-06-03 21:07:42 +00:00
strcatW (start, s);
2007-02-23 22:22:57 +00:00
free(s);
return start;
/* search and replace manipulation */
} else {
2007-06-03 21:07:42 +00:00
WCHAR *equalspos = strstrW(colonpos, equalsW);
WCHAR *replacewith = equalspos+1;
WCHAR *found = NULL;
WCHAR *searchIn;
WCHAR *searchFor;
2007-02-23 22:22:57 +00:00
2007-06-03 21:07:42 +00:00
s = WCMD_strdupW(endOfVar + 1);
2007-02-23 22:22:57 +00:00
if (equalspos == NULL) return start+1;
/* Null terminate both strings */
2007-06-03 21:07:42 +00:00
thisVar[strlenW(thisVar)-1] = 0x00;
2007-02-23 22:22:57 +00:00
*equalspos = 0x00;
/* Since we need to be case insensitive, copy the 2 buffers */
2007-06-03 21:07:42 +00:00
searchIn = WCMD_strdupW(thisVarContents);
CharUpperBuff(searchIn, strlenW(thisVarContents));
searchFor = WCMD_strdupW(colonpos+1);
CharUpperBuff(searchFor, strlenW(colonpos+1));
2007-02-23 22:22:57 +00:00
/* Handle wildcard case */
if (*(colonpos+1) == '*') {
/* Search for string to replace */
2007-06-03 21:07:42 +00:00
found = strstrW(searchIn, searchFor+1);
2007-02-23 22:22:57 +00:00
if (found) {
/* Do replacement */
2007-06-03 21:07:42 +00:00
strcpyW(start, replacewith);
strcatW(start, thisVarContents + (found-searchIn) + strlenW(searchFor+1));
strcatW(start, s);
2007-02-23 22:22:57 +00:00
free(s);
} else {
/* Copy as it */
2007-06-03 21:07:42 +00:00
strcpyW(start, thisVarContents);
strcatW(start, s);
2007-02-23 22:22:57 +00:00
}
} else {
/* Loop replacing all instances */
2007-06-03 21:07:42 +00:00
WCHAR *lastFound = searchIn;
WCHAR *outputposn = start;
2007-02-23 22:22:57 +00:00
*start = 0x00;
2007-06-03 21:07:42 +00:00
while ((found = strstrW(lastFound, searchFor))) {
lstrcpynW(outputposn,
2007-02-23 22:22:57 +00:00
thisVarContents + (lastFound-searchIn),
2007-06-03 21:07:42 +00:00
(found - lastFound)+1);
2007-02-23 22:22:57 +00:00
outputposn = outputposn + (found - lastFound);
2007-06-03 21:07:42 +00:00
strcatW(outputposn, replacewith);
outputposn = outputposn + strlenW(replacewith);
lastFound = found + strlenW(searchFor);
2007-02-23 22:22:57 +00:00
}
2007-06-03 21:07:42 +00:00
strcatW(outputposn,
2007-02-23 22:22:57 +00:00
thisVarContents + (lastFound-searchIn));
2007-06-03 21:07:42 +00:00
strcatW(outputposn, s);
2007-02-23 22:22:57 +00:00
}
free(searchIn);
free(searchFor);
return start;
}
return start+1;
}
/*************************************************************************
* WCMD_LoadMessage
* Load a string from the resource file, handling any error
* Returns string retrieved from resource file
*/
2007-06-03 21:07:42 +00:00
WCHAR *WCMD_LoadMessage(UINT id) {
static WCHAR msg[2048];
static const WCHAR failedMsg[] = {'F','a','i','l','e','d','!','\0'};
2007-06-03 21:07:42 +00:00
if (!LoadString(GetModuleHandle(NULL), id, msg, sizeof(msg)/sizeof(WCHAR))) {
WINE_FIXME("LoadString failed with %d\n", GetLastError());
2007-06-03 21:07:42 +00:00
strcpyW(msg, failedMsg);
}
return msg;
}
2007-06-03 21:07:42 +00:00
/*************************************************************************
* WCMD_strdupW
* A wide version of strdup as its missing from unicode.h
*/
WCHAR *WCMD_strdupW(WCHAR *input) {
int len=strlenW(input)+1;
/* Note: Use malloc not HeapAlloc to emulate strdup */
WCHAR *result = malloc(len * sizeof(WCHAR));
memcpy(result, input, len * sizeof(WCHAR));
return result;
}
/***************************************************************************
* WCMD_Readfile
*
* Read characters in from a console/file, returning result in Unicode
* with signature identical to ReadFile
*/
BOOL WCMD_ReadFile(const HANDLE hIn, WCHAR *intoBuf, const DWORD maxChars,
LPDWORD charsRead, const LPOVERLAPPED unused) {
BOOL res;
/* Try to read from console as Unicode */
res = ReadConsoleW(hIn, intoBuf, maxChars, charsRead, NULL);
/* If reading from console has failed we assume its file
i/o so read in and convert from OEM codepage */
if (!res) {
DWORD numRead;
/*
* Allocate buffer to use when reading from file. Not freed
*/
if (!output_bufA) output_bufA = HeapAlloc(GetProcessHeap(), 0,
MAX_WRITECONSOLE_SIZE);
if (!output_bufA) {
WINE_FIXME("Out of memory - could not allocate ansi 64K buffer\n");
return 0;
}
/* Read from file (assume OEM codepage) */
res = ReadFile(hIn, output_bufA, maxChars, &numRead, unused);
/* Convert from OEM */
*charsRead = MultiByteToWideChar(GetConsoleCP(), 0, output_bufA, numRead,
intoBuf, maxChars);
}
return res;
}
/***************************************************************************
* WCMD_DumpCommands
*
* Domps out the parsed command line to ensure syntax is correct
*/
void WCMD_DumpCommands(CMD_LIST *commands) {
WCHAR buffer[MAXSTRING];
CMD_LIST *thisCmd = commands;
const WCHAR fmt[] = {'%','p',' ','%','c',' ','%','2','.','2','d',' ',
'%','p',' ','%','s','\0'};
WINE_TRACE("Parsed line:\n");
while (thisCmd != NULL) {
sprintfW(buffer, fmt,
thisCmd,
thisCmd->isAmphersand?'Y':'N',
thisCmd->bracketDepth,
thisCmd->nextcommand,
thisCmd->command);
WINE_TRACE("%s\n", wine_dbgstr_w(buffer));
thisCmd = thisCmd->nextcommand;
}
}
/***************************************************************************
* WCMD_ReadAndParseLine
*
* Either uses supplied input or
* Reads a file from the handle, and then...
* Parse the text buffer, spliting into seperate commands
* - unquoted && strings split 2 commands but the 2nd is flagged as
* following an &&
* - ( as the first character just ups the bracket depth
* - unquoted ) when bracket depth > 0 terminates a bracket and
* adds a CMD_LIST structure with null command
* - Anything else gets put into the command string (including
* redirects)
*/
WCHAR *WCMD_ReadAndParseLine(WCHAR *optionalcmd, CMD_LIST **output, HANDLE readFrom) {
WCHAR *curPos;
BOOL inQuotes = FALSE;
WCHAR curString[MAXSTRING];
int curLen = 0;
int curDepth = 0;
CMD_LIST *thisEntry = NULL;
CMD_LIST *lastEntry = NULL;
BOOL isAmphersand = FALSE;
static WCHAR *extraSpace = NULL; /* Deliberately never freed */
/* Allocate working space for a command read from keyboard, file etc */
if (!extraSpace)
extraSpace = HeapAlloc(GetProcessHeap(), 0, (MAXSTRING+1) * sizeof(WCHAR));
/* If initial command read in, use that, otherwise get input from handle */
if (optionalcmd != NULL) {
strcpyW(extraSpace, optionalcmd);
} else if (readFrom == INVALID_HANDLE_VALUE) {
WINE_FIXME("No command nor handle supplied\n");
} else {
if (WCMD_fgets(extraSpace, MAXSTRING, readFrom) == NULL) return NULL;
}
curPos = extraSpace;
/* Handle truncated input - issue warning */
if (strlenW(extraSpace) == MAXSTRING -1) {
WCMD_output_asis(WCMD_LoadMessage(WCMD_TRUNCATEDLINE));
WCMD_output_asis(extraSpace);
WCMD_output_asis(newline);
}
/* Start with an empty string */
curLen = 0;
/* Parse every character on the line being processed */
while (*curPos != 0x00) {
switch (*curPos) {
case '\t':/* drop through - ignore whitespace at the start of a command */
case ' ': if (curLen > 0)
curString[curLen++] = *curPos;
break;
case '"': inQuotes = !inQuotes;
break;
case '(': if (inQuotes || curLen > 0) {
curString[curLen++] = *curPos;
} else {
curDepth++;
}
break;
case '&': if (!inQuotes && *(curPos+1) == '&') {
curPos++; /* Skip other & */
/* Add an entry to the command list */
thisEntry = HeapAlloc(GetProcessHeap(), 0, sizeof(CMD_LIST));
thisEntry->command = HeapAlloc(GetProcessHeap(), 0,
(curLen+1) * sizeof(WCHAR));
memcpy(thisEntry->command, curString, curLen * sizeof(WCHAR));
thisEntry->command[curLen] = 0x00;
curLen = 0;
thisEntry->nextcommand = NULL;
thisEntry->isAmphersand = isAmphersand;
thisEntry->bracketDepth = curDepth;
if (lastEntry) {
lastEntry->nextcommand = thisEntry;
} else {
*output = thisEntry;
}
lastEntry = thisEntry;
isAmphersand = TRUE;
} else {
curString[curLen++] = *curPos;
}
break;
case ')': if (!inQuotes && curDepth > 0) {
/* Add the current command if there is one */
if (curLen) {
thisEntry = HeapAlloc(GetProcessHeap(), 0, sizeof(CMD_LIST));
thisEntry->command = HeapAlloc(GetProcessHeap(), 0,
(curLen+1) * sizeof(WCHAR));
memcpy(thisEntry->command, curString, curLen * sizeof(WCHAR));
thisEntry->command[curLen] = 0x00;
curLen = 0;
thisEntry->nextcommand = NULL;
thisEntry->isAmphersand = isAmphersand;
thisEntry->bracketDepth = curDepth;
if (lastEntry) {
lastEntry->nextcommand = thisEntry;
} else {
*output = thisEntry;
}
lastEntry = thisEntry;
}
/* Add an empty entry to the command list */
thisEntry = HeapAlloc(GetProcessHeap(), 0, sizeof(CMD_LIST));
thisEntry->command = NULL;
thisEntry->nextcommand = NULL;
thisEntry->isAmphersand = FALSE;
thisEntry->bracketDepth = curDepth;
curDepth--;
if (lastEntry) {
lastEntry->nextcommand = thisEntry;
} else {
*output = thisEntry;
}
lastEntry = thisEntry;
} else {
curString[curLen++] = *curPos;
}
break;
default:
curString[curLen++] = *curPos;
}
curPos++;
/* If we have reached the end, add this command into the list */
if (*curPos == 0x00 && curLen > 0) {
/* Add an entry to the command list */
thisEntry = HeapAlloc(GetProcessHeap(), 0, sizeof(CMD_LIST));
thisEntry->command = HeapAlloc(GetProcessHeap(), 0,
(curLen+1) * sizeof(WCHAR));
memcpy(thisEntry->command, curString, curLen * sizeof(WCHAR));
thisEntry->command[curLen] = 0x00;
curLen = 0;
thisEntry->nextcommand = NULL;
thisEntry->isAmphersand = isAmphersand;
thisEntry->bracketDepth = curDepth;
if (lastEntry) {
lastEntry->nextcommand = thisEntry;
} else {
*output = thisEntry;
}
lastEntry = thisEntry;
}
/* If we have reached the end of the string, see if bracketing outstanding */
if (*curPos == 0x00 && curDepth > 0 && readFrom != INVALID_HANDLE_VALUE) {
isAmphersand = FALSE;
inQuotes = FALSE;
memset(extraSpace, 0x00, (MAXSTRING+1) * sizeof(WCHAR));
/* Read more, skipping any blank lines */
while (*extraSpace == 0x00) {
if (!context) WCMD_output_asis( WCMD_LoadMessage(WCMD_MOREPROMPT));
if (WCMD_fgets(extraSpace, MAXSTRING, readFrom) == NULL) break;
}
curPos = extraSpace;
}
}
/* Dump out the parsed output */
WCMD_DumpCommands(*output);
return extraSpace;
}
/***************************************************************************
* WCMD_process_commands
*
* Process all the commands read in so far
*/
void WCMD_process_commands(CMD_LIST *thisCmd) {
/* Loop through the commands, processing them one by one */
while (thisCmd) {
/* Ignore the NULL entries a ')' inserts (Only 'if' cares
about them and it will be handled in there)
Also, skip over any batch labels (eg. :fred) */
if (thisCmd->command && thisCmd->command[0] != ':') {
WINE_TRACE("Executing command: '%s'\n", wine_dbgstr_w(thisCmd->command));
if (strchrW(thisCmd->command,'|') != NULL) {
WCMD_pipe (&thisCmd);
} else {
WCMD_process_command (thisCmd->command);
}
}
thisCmd = thisCmd->nextcommand;
}
}
/***************************************************************************
* WCMD_free_commands
*
* Frees the storage held for a parsed command line
* - This is not done in the process_commands, as eventually the current
* pointer will be modified within the commands, and hence a single free
* routine is simpler
*/
void WCMD_free_commands(CMD_LIST *cmds) {
/* Loop through the commands, freeing them one by one */
while (cmds) {
CMD_LIST *thisCmd = cmds;
cmds = cmds->nextcommand;
HeapFree(GetProcessHeap(), 0, thisCmd->command);
HeapFree(GetProcessHeap(), 0, thisCmd);
}
}