1999-06-06 17:24:04 +02:00
|
|
|
/*
|
2006-09-04 06:13:08 +02:00
|
|
|
* CMD - Wine-compatible command line interface.
|
1999-06-06 17:24:04 +02:00
|
|
|
*
|
2002-03-10 00:29:33 +01:00
|
|
|
* Copyright (C) 1999 - 2001 D A Pickles
|
2007-09-11 22:43:08 +02:00
|
|
|
* Copyright (C) 2007 J Edmeades
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* 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
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
1999-06-06 17:24:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FIXME:
|
|
|
|
* - Cannot handle parameters in quotes
|
|
|
|
* - Lots of functionality missing from builtins
|
|
|
|
*/
|
|
|
|
|
2006-03-18 17:10:36 +01:00
|
|
|
#include "config.h"
|
2012-01-23 12:16:36 +01:00
|
|
|
#include <time.h>
|
1999-06-06 17:24:04 +02:00
|
|
|
#include "wcmd.h"
|
2012-01-23 12:16:36 +01:00
|
|
|
#include "shellapi.h"
|
2007-03-04 23:34:45 +01:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(cmd);
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2011-11-29 23:30:49 +01:00
|
|
|
extern const WCHAR inbuilt[][10];
|
2011-11-29 23:30:52 +01:00
|
|
|
extern struct env_stack *pushd_directories;
|
|
|
|
|
|
|
|
BATCH_CONTEXT *context = NULL;
|
2000-08-02 00:02:18 +02:00
|
|
|
DWORD errorlevel;
|
2018-07-16 00:15:28 +02:00
|
|
|
WCHAR quals[MAXSTRING], param1[MAXSTRING], param2[MAXSTRING];
|
2012-10-01 23:37:17 +02:00
|
|
|
BOOL interactive;
|
2012-11-05 22:43:08 +01:00
|
|
|
FOR_CONTEXT forloopcontext; /* The 'for' loop context */
|
2013-01-07 00:06:28 +01:00
|
|
|
BOOL delayedsubst = FALSE; /* The current delayed substitution setting */
|
2011-11-29 23:30:52 +01:00
|
|
|
|
2011-09-16 08:44:07 +02:00
|
|
|
int defaultColor = 7;
|
2011-09-16 08:44:06 +02:00
|
|
|
BOOL echo_mode = TRUE;
|
2011-11-29 23:30:52 +01:00
|
|
|
|
|
|
|
WCHAR anykey[100], version_string[100];
|
2012-02-11 18:24:56 +01:00
|
|
|
const WCHAR newlineW[] = {'\r','\n','\0'};
|
|
|
|
const WCHAR spaceW[] = {' ','\0'};
|
2012-10-13 22:50:22 +02:00
|
|
|
static const WCHAR envPathExt[] = {'P','A','T','H','E','X','T','\0'};
|
|
|
|
static const WCHAR dfltPathExt[] = {'.','b','a','t',';',
|
|
|
|
'.','c','o','m',';',
|
|
|
|
'.','c','m','d',';',
|
|
|
|
'.','e','x','e','\0'};
|
2011-11-29 23:30:52 +01:00
|
|
|
|
|
|
|
static BOOL opt_c, opt_k, opt_s, unicodeOutput = FALSE;
|
|
|
|
|
|
|
|
/* Variables pertaining to paging */
|
|
|
|
static BOOL paged_mode;
|
2007-06-03 23:07:42 +02:00
|
|
|
static const WCHAR *pagedMessage = NULL;
|
2011-11-29 23:30:52 +01:00
|
|
|
static int line_count;
|
|
|
|
static int max_height;
|
|
|
|
static int max_width;
|
|
|
|
static int numChars;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2011-11-29 14:39:27 +01:00
|
|
|
#define MAX_WRITECONSOLE_SIZE 65535
|
|
|
|
|
2011-09-23 12:37:43 +02:00
|
|
|
/*
|
|
|
|
* Returns a buffer for reading from/writing to file
|
|
|
|
* Never freed
|
|
|
|
*/
|
|
|
|
static char *get_file_buffer(void)
|
|
|
|
{
|
2011-11-29 14:39:27 +01:00
|
|
|
static char *output_bufA = NULL;
|
2012-11-15 12:32:21 +01:00
|
|
|
if (!output_bufA)
|
|
|
|
output_bufA = heap_alloc(MAX_WRITECONSOLE_SIZE);
|
2011-09-23 12:37:43 +02:00
|
|
|
return output_bufA;
|
|
|
|
}
|
2007-02-23 23:22:57 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/*******************************************************************
|
|
|
|
* WCMD_output_asis_len - send output to current standard output
|
|
|
|
*
|
|
|
|
* 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
|
1999-06-06 17:24:04 +02:00
|
|
|
*/
|
2011-11-07 10:52:22 +01:00
|
|
|
static void WCMD_output_asis_len(const WCHAR *message, DWORD len, HANDLE device)
|
|
|
|
{
|
2008-12-31 00:56:53 +01:00
|
|
|
DWORD nOut= 0;
|
|
|
|
DWORD res = 0;
|
2007-06-03 23:07:42 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* If nothing to write, return (MORE does this sometimes) */
|
|
|
|
if (!len) return;
|
2008-01-09 18:45:15 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Try to write as unicode assuming it is to a console */
|
|
|
|
res = WriteConsoleW(device, message, len, &nOut, NULL);
|
2003-05-11 04:48:17 +02:00
|
|
|
|
2014-02-07 22:42:49 +01:00
|
|
|
/* If writing to console fails, assume it's file
|
2008-12-31 00:56:53 +01:00
|
|
|
i/o so convert to OEM codepage and output */
|
|
|
|
if (!res) {
|
|
|
|
BOOL usedDefaultChar = FALSE;
|
|
|
|
DWORD convertedChars;
|
2011-09-23 12:37:43 +02:00
|
|
|
char *buffer;
|
2006-07-01 04:02:31 +02:00
|
|
|
|
2011-11-29 14:39:28 +01:00
|
|
|
if (!unicodeOutput) {
|
2011-09-23 12:37:43 +02:00
|
|
|
|
|
|
|
if (!(buffer = get_file_buffer()))
|
|
|
|
return;
|
2006-07-01 04:02:31 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Convert to OEM, then output */
|
|
|
|
convertedChars = WideCharToMultiByte(GetConsoleOutputCP(), 0, message,
|
2011-09-23 12:37:43 +02:00
|
|
|
len, buffer, MAX_WRITECONSOLE_SIZE,
|
2008-12-31 00:56:53 +01:00
|
|
|
"?", &usedDefaultChar);
|
2011-09-23 12:37:43 +02:00
|
|
|
WriteFile(device, buffer, convertedChars,
|
2008-12-31 00:56:53 +01:00
|
|
|
&nOut, FALSE);
|
|
|
|
} else {
|
|
|
|
WriteFile(device, message, len*sizeof(WCHAR),
|
|
|
|
&nOut, FALSE);
|
2007-06-03 23:07:41 +02:00
|
|
|
}
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2006-07-01 04:02:31 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/*******************************************************************
|
|
|
|
* WCMD_output - send output to current standard output device.
|
|
|
|
*
|
|
|
|
*/
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2017-11-02 10:20:52 +01:00
|
|
|
void WINAPIV WCMD_output (const WCHAR *format, ...) {
|
2002-11-04 23:36:07 +01:00
|
|
|
|
2011-12-06 17:40:42 +01:00
|
|
|
__ms_va_list ap;
|
|
|
|
WCHAR* string;
|
|
|
|
DWORD len;
|
|
|
|
|
|
|
|
__ms_va_start(ap,format);
|
|
|
|
SetLastError(NO_ERROR);
|
|
|
|
string = NULL;
|
|
|
|
len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
|
|
|
|
format, 0, 0, (LPWSTR)&string, 0, &ap);
|
|
|
|
__ms_va_end(ap);
|
|
|
|
if (len == 0 && GetLastError() != NO_ERROR)
|
|
|
|
WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(format));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WCMD_output_asis_len(string, len, GetStdHandle(STD_OUTPUT_HANDLE));
|
|
|
|
LocalFree(string);
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
|
|
|
}
|
2006-07-25 02:20:31 +02:00
|
|
|
|
2011-09-24 12:50:55 +02:00
|
|
|
/*******************************************************************
|
|
|
|
* WCMD_output_stderr - send output to current standard error device.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-11-02 10:20:52 +01:00
|
|
|
void WINAPIV WCMD_output_stderr (const WCHAR *format, ...) {
|
2011-09-24 12:50:55 +02:00
|
|
|
|
2011-12-06 17:40:42 +01:00
|
|
|
__ms_va_list ap;
|
|
|
|
WCHAR* string;
|
|
|
|
DWORD len;
|
|
|
|
|
|
|
|
__ms_va_start(ap,format);
|
|
|
|
SetLastError(NO_ERROR);
|
|
|
|
string = NULL;
|
|
|
|
len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
|
|
|
|
format, 0, 0, (LPWSTR)&string, 0, &ap);
|
|
|
|
__ms_va_end(ap);
|
|
|
|
if (len == 0 && GetLastError() != NO_ERROR)
|
|
|
|
WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(format));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WCMD_output_asis_len(string, len, GetStdHandle(STD_ERROR_HANDLE));
|
|
|
|
LocalFree(string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************
|
|
|
|
* WCMD_format_string - allocate a buffer and format a string
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-11-02 10:20:52 +01:00
|
|
|
WCHAR* WINAPIV WCMD_format_string (const WCHAR *format, ...)
|
|
|
|
{
|
2011-12-06 17:40:42 +01:00
|
|
|
__ms_va_list ap;
|
|
|
|
WCHAR* string;
|
|
|
|
DWORD len;
|
|
|
|
|
|
|
|
__ms_va_start(ap,format);
|
|
|
|
SetLastError(NO_ERROR);
|
|
|
|
len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
|
|
|
|
format, 0, 0, (LPWSTR)&string, 0, &ap);
|
|
|
|
__ms_va_end(ap);
|
|
|
|
if (len == 0 && GetLastError() != NO_ERROR) {
|
|
|
|
WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(format));
|
|
|
|
string = (WCHAR*)LocalAlloc(LMEM_FIXED, 2);
|
|
|
|
*string = 0;
|
2011-09-24 12:50:55 +02:00
|
|
|
}
|
2011-12-06 17:40:42 +01:00
|
|
|
return string;
|
2011-09-24 12:50:55 +02:00
|
|
|
}
|
2003-05-11 04:48:17 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
void WCMD_enter_paged_mode(const WCHAR *msg)
|
|
|
|
{
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
|
2006-07-25 02:20:31 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo)) {
|
|
|
|
max_height = consoleInfo.dwSize.Y;
|
|
|
|
max_width = consoleInfo.dwSize.X;
|
|
|
|
} else {
|
|
|
|
max_height = 25;
|
|
|
|
max_width = 80;
|
|
|
|
}
|
|
|
|
paged_mode = TRUE;
|
|
|
|
line_count = 0;
|
|
|
|
numChars = 0;
|
|
|
|
pagedMessage = (msg==NULL)? anykey : msg;
|
|
|
|
}
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
void WCMD_leave_paged_mode(void)
|
|
|
|
{
|
|
|
|
paged_mode = FALSE;
|
|
|
|
pagedMessage = NULL;
|
|
|
|
}
|
2003-05-11 04:48:17 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/***************************************************************************
|
|
|
|
* WCMD_Readfile
|
|
|
|
*
|
|
|
|
* Read characters in from a console/file, returning result in Unicode
|
|
|
|
*/
|
2011-09-28 14:41:22 +02:00
|
|
|
BOOL WCMD_ReadFile(const HANDLE hIn, WCHAR *intoBuf, const DWORD maxChars, LPDWORD charsRead)
|
|
|
|
{
|
2011-10-05 14:02:43 +02:00
|
|
|
DWORD numRead;
|
|
|
|
char *buffer;
|
2003-05-11 04:48:17 +02:00
|
|
|
|
2011-10-06 18:50:31 +02:00
|
|
|
if (WCMD_is_console_handle(hIn))
|
2011-10-05 14:02:43 +02:00
|
|
|
/* Try to read from console as Unicode */
|
|
|
|
return ReadConsoleW(hIn, intoBuf, maxChars, charsRead, NULL);
|
2011-09-23 12:37:43 +02:00
|
|
|
|
2011-10-05 14:02:43 +02:00
|
|
|
/* We assume it's a file handle and read then convert from assumed OEM codepage */
|
|
|
|
if (!(buffer = get_file_buffer()))
|
|
|
|
return FALSE;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2011-10-05 14:02:43 +02:00
|
|
|
if (!ReadFile(hIn, buffer, maxChars, &numRead, NULL))
|
|
|
|
return FALSE;
|
2007-06-15 21:59:19 +02:00
|
|
|
|
2011-10-05 14:02:43 +02:00
|
|
|
*charsRead = MultiByteToWideChar(GetConsoleCP(), 0, buffer, numRead, intoBuf, maxChars);
|
2007-06-15 21:59:19 +02:00
|
|
|
|
2011-10-05 14:02:43 +02:00
|
|
|
return TRUE;
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/*******************************************************************
|
2011-08-29 21:45:14 +02:00
|
|
|
* WCMD_output_asis_handle
|
|
|
|
*
|
|
|
|
* Send output to specified handle without formatting e.g. when message contains '%'
|
2008-12-31 00:56:53 +01:00
|
|
|
*/
|
2011-08-29 21:45:14 +02:00
|
|
|
static void WCMD_output_asis_handle (DWORD std_handle, const WCHAR *message) {
|
2008-12-31 00:56:53 +01:00
|
|
|
DWORD count;
|
|
|
|
const WCHAR* ptr;
|
|
|
|
WCHAR string[1024];
|
2011-08-29 21:45:14 +02:00
|
|
|
HANDLE handle = GetStdHandle(std_handle);
|
2003-05-11 04:48:17 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
if (paged_mode) {
|
|
|
|
do {
|
|
|
|
ptr = message;
|
|
|
|
while (*ptr && *ptr!='\n' && (numChars < max_width)) {
|
|
|
|
numChars++;
|
|
|
|
ptr++;
|
|
|
|
};
|
|
|
|
if (*ptr == '\n') ptr++;
|
2011-10-29 01:08:49 +02:00
|
|
|
WCMD_output_asis_len(message, ptr - message, handle);
|
|
|
|
numChars = 0;
|
|
|
|
if (++line_count >= max_height - 1) {
|
|
|
|
line_count = 0;
|
|
|
|
WCMD_output_asis_len(pagedMessage, strlenW(pagedMessage), handle);
|
2018-08-20 21:15:42 +02:00
|
|
|
WCMD_ReadFile(GetStdHandle(STD_INPUT_HANDLE), string, ARRAY_SIZE(string), &count);
|
2007-03-08 01:38:38 +01:00
|
|
|
}
|
2008-12-31 00:56:53 +01:00
|
|
|
} while (((message = ptr) != NULL) && (*ptr));
|
2007-03-08 01:40:54 +01:00
|
|
|
} else {
|
2011-08-29 21:45:14 +02:00
|
|
|
WCMD_output_asis_len(message, lstrlenW(message), handle);
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
|
|
|
}
|
2007-03-08 01:40:54 +01:00
|
|
|
|
2011-08-29 21:45:14 +02:00
|
|
|
/*******************************************************************
|
|
|
|
* WCMD_output_asis
|
|
|
|
*
|
|
|
|
* Send output to current standard output device, without formatting
|
|
|
|
* e.g. when message contains '%'
|
|
|
|
*/
|
|
|
|
void WCMD_output_asis (const WCHAR *message) {
|
|
|
|
WCMD_output_asis_handle(STD_OUTPUT_HANDLE, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************
|
|
|
|
* WCMD_output_asis_stderr
|
|
|
|
*
|
|
|
|
* Send output to current standard error device, without formatting
|
|
|
|
* e.g. when message contains '%'
|
|
|
|
*/
|
|
|
|
void WCMD_output_asis_stderr (const WCHAR *message) {
|
|
|
|
WCMD_output_asis_handle(STD_ERROR_HANDLE, message);
|
|
|
|
}
|
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_print_error
|
|
|
|
*
|
|
|
|
* Print the message for GetLastError
|
|
|
|
*/
|
2007-03-08 01:40:54 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
void WCMD_print_error (void) {
|
|
|
|
LPVOID lpMsgBuf;
|
|
|
|
DWORD error_code;
|
|
|
|
int status;
|
2007-03-08 01:38:38 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
error_code = GetLastError ();
|
2009-12-09 18:52:40 +01:00
|
|
|
status = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
|
|
|
|
NULL, error_code, 0, (LPWSTR) &lpMsgBuf, 0, NULL);
|
2008-12-31 00:56:53 +01:00
|
|
|
if (!status) {
|
|
|
|
WINE_FIXME ("Cannot display message for error %d, status %d\n",
|
|
|
|
error_code, GetLastError());
|
|
|
|
return;
|
2007-03-13 01:08:00 +01:00
|
|
|
}
|
|
|
|
|
2009-12-09 18:52:40 +01:00
|
|
|
WCMD_output_asis_len(lpMsgBuf, lstrlenW(lpMsgBuf),
|
2008-12-31 00:56:53 +01:00
|
|
|
GetStdHandle(STD_ERROR_HANDLE));
|
|
|
|
LocalFree (lpMsgBuf);
|
2012-02-11 18:24:56 +01:00
|
|
|
WCMD_output_asis_len (newlineW, lstrlenW(newlineW),
|
2008-12-31 00:56:53 +01:00
|
|
|
GetStdHandle(STD_ERROR_HANDLE));
|
|
|
|
return;
|
|
|
|
}
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* WCMD_show_prompt
|
|
|
|
*
|
|
|
|
* Display the prompt on STDout
|
|
|
|
*
|
1999-06-06 17:24:04 +02:00
|
|
|
*/
|
|
|
|
|
2018-09-02 00:11:23 +02:00
|
|
|
static void WCMD_show_prompt (BOOL newLine) {
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
int status;
|
|
|
|
WCHAR out_string[MAX_PATH], curdir[MAX_PATH], prompt_string[MAX_PATH];
|
|
|
|
WCHAR *p, *q;
|
|
|
|
DWORD len;
|
|
|
|
static const WCHAR envPrompt[] = {'P','R','O','M','P','T','\0'};
|
2007-06-15 21:59:19 +02:00
|
|
|
|
2018-08-20 21:15:42 +02:00
|
|
|
len = GetEnvironmentVariableW(envPrompt, prompt_string, ARRAY_SIZE(prompt_string));
|
|
|
|
if ((len == 0) || (len >= ARRAY_SIZE(prompt_string))) {
|
2011-08-25 23:08:30 +02:00
|
|
|
static const WCHAR dfltPrompt[] = {'$','P','$','G','\0'};
|
2008-12-31 00:56:53 +01:00
|
|
|
strcpyW (prompt_string, dfltPrompt);
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
2008-12-31 00:56:53 +01:00
|
|
|
p = prompt_string;
|
|
|
|
q = out_string;
|
2018-09-02 00:11:23 +02:00
|
|
|
if (newLine) {
|
|
|
|
*q++ = '\r';
|
|
|
|
*q++ = '\n';
|
|
|
|
}
|
2008-12-31 00:56:53 +01:00
|
|
|
*q = '\0';
|
|
|
|
while (*p != '\0') {
|
|
|
|
if (*p != '$') {
|
|
|
|
*q++ = *p++;
|
|
|
|
*q = '\0';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p++;
|
|
|
|
switch (toupper(*p)) {
|
|
|
|
case '$':
|
|
|
|
*q++ = '$';
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
*q++ = '&';
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
*q++ = '|';
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
*q++ = '(';
|
|
|
|
break;
|
|
|
|
case 'D':
|
2016-06-18 09:50:59 +02:00
|
|
|
GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, NULL, q, MAX_PATH - (q - out_string));
|
2008-12-31 00:56:53 +01:00
|
|
|
while (*q) q++;
|
|
|
|
break;
|
|
|
|
case 'E':
|
2017-06-21 10:24:11 +02:00
|
|
|
*q++ = '\x1b';
|
2008-12-31 00:56:53 +01:00
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
*q++ = ')';
|
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
*q++ = '>';
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
*q++ = '\b';
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
*q++ = '<';
|
|
|
|
break;
|
|
|
|
case 'N':
|
2018-08-20 21:15:42 +02:00
|
|
|
status = GetCurrentDirectoryW(ARRAY_SIZE(curdir), curdir);
|
2008-12-31 00:56:53 +01:00
|
|
|
if (status) {
|
|
|
|
*q++ = curdir[0];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'P':
|
2018-08-20 21:15:42 +02:00
|
|
|
status = GetCurrentDirectoryW(ARRAY_SIZE(curdir), curdir);
|
2008-12-31 00:56:53 +01:00
|
|
|
if (status) {
|
|
|
|
strcatW (q, curdir);
|
|
|
|
while (*q) q++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'Q':
|
|
|
|
*q++ = '=';
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
*q++ = ' ';
|
|
|
|
break;
|
|
|
|
case 'T':
|
2009-12-09 18:52:40 +01:00
|
|
|
GetTimeFormatW(LOCALE_USER_DEFAULT, 0, NULL, NULL, q, MAX_PATH);
|
2008-12-31 00:56:53 +01:00
|
|
|
while (*q) q++;
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
strcatW (q, version_string);
|
|
|
|
while (*q) q++;
|
|
|
|
break;
|
|
|
|
case '_':
|
|
|
|
*q++ = '\n';
|
|
|
|
break;
|
|
|
|
case '+':
|
|
|
|
if (pushd_directories) {
|
|
|
|
memset(q, '+', pushd_directories->u.stackdepth);
|
|
|
|
q = q + pushd_directories->u.stackdepth;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
*q = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WCMD_output_asis (out_string);
|
|
|
|
}
|
2007-09-11 22:43:03 +02:00
|
|
|
|
2012-11-15 12:32:21 +01:00
|
|
|
void *heap_alloc(size_t size)
|
|
|
|
{
|
|
|
|
void *ret;
|
2007-09-11 22:43:03 +02:00
|
|
|
|
2012-11-15 12:32:21 +01:00
|
|
|
ret = HeapAlloc(GetProcessHeap(), 0, size);
|
|
|
|
if(!ret) {
|
|
|
|
ERR("Out of memory\n");
|
|
|
|
ExitProcess(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
2007-09-11 22:43:03 +02:00
|
|
|
|
2009-06-15 10:59:31 +02:00
|
|
|
/*************************************************************************
|
|
|
|
* WCMD_strsubstW
|
|
|
|
* Replaces a portion of a Unicode string with the specified string.
|
|
|
|
* It's up to the caller to ensure there is enough space in the
|
|
|
|
* destination buffer.
|
|
|
|
*/
|
2011-08-20 01:58:59 +02:00
|
|
|
void WCMD_strsubstW(WCHAR *start, const WCHAR *next, const WCHAR *insert, int len) {
|
2009-06-15 10:59:31 +02:00
|
|
|
|
|
|
|
if (len < 0)
|
|
|
|
len=insert ? lstrlenW(insert) : 0;
|
|
|
|
if (start+len != next)
|
|
|
|
memmove(start+len, next, (strlenW(next) + 1) * sizeof(*next));
|
|
|
|
if (insert)
|
|
|
|
memcpy(start, insert, len * sizeof(*insert));
|
|
|
|
}
|
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/***************************************************************************
|
2011-08-20 01:58:58 +02:00
|
|
|
* WCMD_skip_leading_spaces
|
2008-12-31 00:56:53 +01:00
|
|
|
*
|
2011-08-25 00:48:19 +02:00
|
|
|
* Return a pointer to the first non-whitespace character of string.
|
2011-08-20 01:58:58 +02:00
|
|
|
* Does not modify the input string.
|
2008-12-31 00:56:53 +01:00
|
|
|
*/
|
2011-08-20 01:58:58 +02:00
|
|
|
WCHAR *WCMD_skip_leading_spaces (WCHAR *string) {
|
2007-09-11 22:43:03 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
WCHAR *ptr;
|
2007-09-11 22:43:03 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
ptr = string;
|
2011-08-25 00:48:19 +02:00
|
|
|
while (*ptr == ' ' || *ptr == '\t') ptr++;
|
2008-12-31 00:56:53 +01:00
|
|
|
return ptr;
|
|
|
|
}
|
2007-09-11 22:43:03 +02:00
|
|
|
|
2011-08-25 00:48:18 +02:00
|
|
|
/***************************************************************************
|
|
|
|
* WCMD_keyword_ws_found
|
|
|
|
*
|
|
|
|
* Checks if the string located at ptr matches a keyword (of length len)
|
|
|
|
* followed by a whitespace character (space or tab)
|
|
|
|
*/
|
|
|
|
BOOL WCMD_keyword_ws_found(const WCHAR *keyword, int len, const WCHAR *ptr) {
|
|
|
|
return (CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
|
|
|
|
ptr, len, keyword, len) == CSTR_EQUAL)
|
|
|
|
&& ((*(ptr + len) == ' ') || (*(ptr + len) == '\t'));
|
|
|
|
}
|
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/*************************************************************************
|
2011-10-19 09:47:58 +02:00
|
|
|
* WCMD_strip_quotes
|
2008-12-31 00:56:53 +01:00
|
|
|
*
|
2013-06-03 23:38:15 +02:00
|
|
|
* Remove first and last quote WCHARacters, preserving all other text
|
|
|
|
* Returns the location of the final quote
|
2008-12-31 00:56:53 +01:00
|
|
|
*/
|
2013-06-03 23:38:15 +02:00
|
|
|
WCHAR *WCMD_strip_quotes(WCHAR *cmd) {
|
|
|
|
WCHAR *src = cmd + 1, *dest = cmd, *lastq = NULL, *lastquote;
|
2008-12-31 00:56:53 +01:00
|
|
|
while((*dest=*src) != '\0') {
|
|
|
|
if (*src=='\"')
|
|
|
|
lastq=dest;
|
|
|
|
dest++, src++;
|
|
|
|
}
|
2013-06-03 23:38:15 +02:00
|
|
|
lastquote = lastq;
|
2008-12-31 00:56:53 +01:00
|
|
|
if (lastq) {
|
|
|
|
dest=lastq++;
|
|
|
|
while ((*dest++=*lastq++) != 0)
|
|
|
|
;
|
|
|
|
}
|
2013-06-03 23:38:15 +02:00
|
|
|
return lastquote;
|
2007-09-11 22:43:03 +02:00
|
|
|
}
|
|
|
|
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2011-05-26 06:46:42 +02:00
|
|
|
/*************************************************************************
|
|
|
|
* WCMD_is_magic_envvar
|
|
|
|
* Return TRUE if s is '%'magicvar'%'
|
|
|
|
* and is not masked by a real environment variable.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline BOOL WCMD_is_magic_envvar(const WCHAR *s, const WCHAR *magicvar)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (s[0] != '%')
|
|
|
|
return FALSE; /* Didn't begin with % */
|
|
|
|
len = strlenW(s);
|
|
|
|
if (len < 2 || s[len-1] != '%')
|
|
|
|
return FALSE; /* Didn't end with another % */
|
|
|
|
|
|
|
|
if (CompareStringW(LOCALE_USER_DEFAULT,
|
|
|
|
NORM_IGNORECASE | SORT_STRINGSORT,
|
|
|
|
s+1, len-2, magicvar, -1) != CSTR_EQUAL) {
|
|
|
|
/* Name doesn't match. */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetEnvironmentVariableW(magicvar, NULL, 0) > 0) {
|
|
|
|
/* Masked by real environment variable. */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* WCMD_expand_envvar
|
|
|
|
*
|
|
|
|
* Expands environment variables, allowing for WCHARacter substitution
|
1999-06-06 17:24:04 +02:00
|
|
|
*/
|
2013-01-07 00:06:28 +01:00
|
|
|
static WCHAR *WCMD_expand_envvar(WCHAR *start, WCHAR startchar)
|
2012-11-05 22:43:08 +01:00
|
|
|
{
|
2008-12-31 00:56:53 +01:00
|
|
|
WCHAR *endOfVar = NULL, *s;
|
|
|
|
WCHAR *colonpos = NULL;
|
|
|
|
WCHAR thisVar[MAXSTRING];
|
|
|
|
WCHAR thisVarContents[MAXSTRING];
|
|
|
|
WCHAR savedchar = 0x00;
|
|
|
|
int len;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
static const WCHAR ErrorLvl[] = {'E','R','R','O','R','L','E','V','E','L','\0'};
|
|
|
|
static const WCHAR Date[] = {'D','A','T','E','\0'};
|
|
|
|
static const WCHAR Time[] = {'T','I','M','E','\0'};
|
|
|
|
static const WCHAR Cd[] = {'C','D','\0'};
|
|
|
|
static const WCHAR Random[] = {'R','A','N','D','O','M','\0'};
|
2013-01-07 00:06:28 +01:00
|
|
|
WCHAR Delims[] = {'%',':','\0'}; /* First char gets replaced appropriately */
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2013-01-07 00:06:28 +01:00
|
|
|
WINE_TRACE("Expanding: %s (%c)\n", wine_dbgstr_w(start), startchar);
|
2007-02-23 23:21:27 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Find the end of the environment variable, and extract name */
|
2013-01-07 00:06:28 +01:00
|
|
|
Delims[0] = startchar;
|
2008-12-31 00:56:53 +01:00
|
|
|
endOfVar = strpbrkW(start+1, Delims);
|
2007-09-11 22:43:03 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
if (endOfVar == NULL || *endOfVar==' ') {
|
2008-03-04 00:14:29 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* In batch program, missing terminator for % and no following
|
|
|
|
':' just removes the '%' */
|
|
|
|
if (context) {
|
2009-06-15 10:59:31 +02:00
|
|
|
WCMD_strsubstW(start, start + 1, NULL, 0);
|
2008-12-31 00:56:53 +01:00
|
|
|
return start;
|
|
|
|
} else {
|
2008-03-04 00:14:29 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* In command processing, just ignore it - allows command line
|
|
|
|
syntax like: for %i in (a.a) do echo %i */
|
|
|
|
return start+1;
|
|
|
|
}
|
2008-03-04 00:14:29 +01:00
|
|
|
}
|
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* If ':' found, process remaining up until '%' (or stop at ':' if
|
|
|
|
a missing '%' */
|
|
|
|
if (*endOfVar==':') {
|
2013-01-07 00:06:28 +01:00
|
|
|
WCHAR *endOfVar2 = strchrW(endOfVar+1, startchar);
|
2008-12-31 00:56:53 +01:00
|
|
|
if (endOfVar2 != NULL) endOfVar = endOfVar2;
|
2008-07-28 23:45:21 +02:00
|
|
|
}
|
2007-02-23 23:21:27 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
memcpy(thisVar, start, ((endOfVar - start) + 1) * sizeof(WCHAR));
|
|
|
|
thisVar[(endOfVar - start)+1] = 0x00;
|
|
|
|
colonpos = strchrW(thisVar+1, ':');
|
2008-03-04 00:14:29 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* If there's complex substitution, just need %var% for now
|
|
|
|
to get the expanded data to play with */
|
|
|
|
if (colonpos) {
|
2013-01-07 00:06:28 +01:00
|
|
|
*colonpos = startchar;
|
2008-12-31 00:56:53 +01:00
|
|
|
savedchar = *(colonpos+1);
|
|
|
|
*(colonpos+1) = 0x00;
|
2008-03-04 00:14:29 +01:00
|
|
|
}
|
|
|
|
|
2013-01-07 00:06:28 +01:00
|
|
|
/* By now, we know the variable we want to expand but it may be
|
|
|
|
surrounded by '!' if we are in delayed expansion - if so convert
|
|
|
|
to % signs. */
|
|
|
|
if (startchar=='!') {
|
|
|
|
thisVar[0] = '%';
|
|
|
|
thisVar[(endOfVar - start)] = '%';
|
|
|
|
}
|
2008-12-31 00:56:53 +01:00
|
|
|
WINE_TRACE("Retrieving contents of %s\n", wine_dbgstr_w(thisVar));
|
2007-02-23 23:21:27 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Expand to contents, if unchanged, return */
|
|
|
|
/* Handle DATE, TIME, ERRORLEVEL and CD replacements allowing */
|
|
|
|
/* override if existing env var called that name */
|
2011-05-26 06:46:42 +02:00
|
|
|
if (WCMD_is_magic_envvar(thisVar, ErrorLvl)) {
|
2008-12-31 00:56:53 +01:00
|
|
|
static const WCHAR fmt[] = {'%','d','\0'};
|
2009-12-09 18:52:40 +01:00
|
|
|
wsprintfW(thisVarContents, fmt, errorlevel);
|
2008-12-31 00:56:53 +01:00
|
|
|
len = strlenW(thisVarContents);
|
2011-05-26 06:46:42 +02:00
|
|
|
} else if (WCMD_is_magic_envvar(thisVar, Date)) {
|
2009-12-09 18:52:40 +01:00
|
|
|
GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL,
|
2008-12-31 00:56:53 +01:00
|
|
|
NULL, thisVarContents, MAXSTRING);
|
|
|
|
len = strlenW(thisVarContents);
|
2011-05-26 06:46:42 +02:00
|
|
|
} else if (WCMD_is_magic_envvar(thisVar, Time)) {
|
2009-12-09 18:52:40 +01:00
|
|
|
GetTimeFormatW(LOCALE_USER_DEFAULT, TIME_NOSECONDS, NULL,
|
2008-12-31 00:56:53 +01:00
|
|
|
NULL, thisVarContents, MAXSTRING);
|
|
|
|
len = strlenW(thisVarContents);
|
2011-05-26 06:46:42 +02:00
|
|
|
} else if (WCMD_is_magic_envvar(thisVar, Cd)) {
|
2009-12-09 18:52:40 +01:00
|
|
|
GetCurrentDirectoryW(MAXSTRING, thisVarContents);
|
2008-12-31 00:56:53 +01:00
|
|
|
len = strlenW(thisVarContents);
|
2011-05-26 06:46:42 +02:00
|
|
|
} else if (WCMD_is_magic_envvar(thisVar, Random)) {
|
2008-12-31 00:56:53 +01:00
|
|
|
static const WCHAR fmt[] = {'%','d','\0'};
|
2009-12-09 18:52:40 +01:00
|
|
|
wsprintfW(thisVarContents, fmt, rand() % 32768);
|
2008-12-31 00:56:53 +01:00
|
|
|
len = strlenW(thisVarContents);
|
|
|
|
} else {
|
2008-03-04 00:14:29 +01:00
|
|
|
|
2018-08-20 21:15:42 +02:00
|
|
|
len = ExpandEnvironmentStringsW(thisVar, thisVarContents, ARRAY_SIZE(thisVarContents));
|
1999-07-10 13:36:45 +02:00
|
|
|
}
|
2007-03-13 01:09:05 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
if (len == 0)
|
|
|
|
return endOfVar+1;
|
2007-03-13 01:09:05 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* 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 */
|
|
|
|
if (lstrcmpiW(thisVar, thisVarContents) == 0) {
|
2007-03-13 01:09:05 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Restore the complex part after the compare */
|
|
|
|
if (colonpos) {
|
|
|
|
*colonpos = ':';
|
|
|
|
*(colonpos+1) = savedchar;
|
2003-09-11 22:41:26 +02:00
|
|
|
}
|
2007-03-13 01:10:38 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Command line - just ignore this */
|
|
|
|
if (context == NULL) return endOfVar+1;
|
2007-03-13 01:10:38 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
|
|
|
|
/* Batch - replace unknown env var with nothing */
|
|
|
|
if (colonpos == NULL) {
|
2009-06-15 10:59:31 +02:00
|
|
|
WCMD_strsubstW(start, endOfVar + 1, NULL, 0);
|
2007-03-13 01:10:38 +01:00
|
|
|
} else {
|
2008-12-31 00:56:53 +01:00
|
|
|
len = strlenW(thisVar);
|
|
|
|
thisVar[len-1] = 0x00;
|
|
|
|
/* If %:...% supplied, : is retained */
|
|
|
|
if (colonpos == thisVar+1) {
|
2009-06-15 10:59:31 +02:00
|
|
|
WCMD_strsubstW(start, endOfVar + 1, colonpos, -1);
|
2008-12-31 00:56:53 +01:00
|
|
|
} else {
|
2009-06-15 10:59:31 +02:00
|
|
|
WCMD_strsubstW(start, endOfVar + 1, colonpos + 1, -1);
|
2007-03-13 01:10:38 +01:00
|
|
|
}
|
2003-09-11 22:41:26 +02:00
|
|
|
}
|
2008-12-31 00:56:53 +01:00
|
|
|
return start;
|
2007-03-13 01:10:38 +01:00
|
|
|
|
1999-07-10 13:36:45 +02:00
|
|
|
}
|
2007-03-13 01:09:05 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* See if we need to do complex substitution (any ':'s), if not
|
|
|
|
then our work here is done */
|
|
|
|
if (colonpos == NULL) {
|
2009-06-15 10:59:31 +02:00
|
|
|
WCMD_strsubstW(start, endOfVar + 1, thisVarContents, -1);
|
2008-12-31 00:56:53 +01:00
|
|
|
return start;
|
|
|
|
}
|
2000-08-01 04:14:33 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Restore complex bit */
|
|
|
|
*colonpos = ':';
|
|
|
|
*(colonpos+1) = savedchar;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/*
|
|
|
|
Handle complex substitutions:
|
|
|
|
xxx=yyy (replace xxx with yyy)
|
|
|
|
*xxx=yyy (replace up to and including xxx with yyy)
|
|
|
|
~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)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ~ is substring manipulation */
|
|
|
|
if (savedchar == '~') {
|
|
|
|
|
|
|
|
int substrposition, substrlength = 0;
|
|
|
|
WCHAR *commapos = strchrW(colonpos+2, ',');
|
|
|
|
WCHAR *startCopy;
|
|
|
|
|
|
|
|
substrposition = atolW(colonpos+2);
|
|
|
|
if (commapos) substrlength = atolW(commapos+1);
|
|
|
|
|
|
|
|
/* Check bounds */
|
|
|
|
if (substrposition >= 0) {
|
|
|
|
startCopy = &thisVarContents[min(substrposition, len)];
|
|
|
|
} else {
|
|
|
|
startCopy = &thisVarContents[max(0, len+substrposition-1)];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (commapos == NULL) {
|
2009-06-15 10:59:31 +02:00
|
|
|
/* Copy the lot */
|
|
|
|
WCMD_strsubstW(start, endOfVar + 1, startCopy, -1);
|
2008-12-31 00:56:53 +01:00
|
|
|
} else if (substrlength < 0) {
|
|
|
|
|
|
|
|
int copybytes = (len+substrlength-1)-(startCopy-thisVarContents);
|
|
|
|
if (copybytes > len) copybytes = len;
|
|
|
|
else if (copybytes < 0) copybytes = 0;
|
2009-06-15 10:59:31 +02:00
|
|
|
WCMD_strsubstW(start, endOfVar + 1, startCopy, copybytes);
|
2008-12-31 00:56:53 +01:00
|
|
|
} else {
|
2012-10-25 22:17:13 +02:00
|
|
|
substrlength = min(substrlength, len - (startCopy- thisVarContents + 1));
|
2009-06-15 10:59:31 +02:00
|
|
|
WCMD_strsubstW(start, endOfVar + 1, startCopy, substrlength);
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* search and replace manipulation */
|
|
|
|
} else {
|
2011-11-12 13:58:01 +01:00
|
|
|
WCHAR *equalspos = strstrW(colonpos, equalW);
|
2008-12-31 00:56:53 +01:00
|
|
|
WCHAR *replacewith = equalspos+1;
|
|
|
|
WCHAR *found = NULL;
|
|
|
|
WCHAR *searchIn;
|
|
|
|
WCHAR *searchFor;
|
|
|
|
|
|
|
|
if (equalspos == NULL) return start+1;
|
2012-11-15 12:32:21 +01:00
|
|
|
s = heap_strdupW(endOfVar + 1);
|
2008-12-31 00:56:53 +01:00
|
|
|
|
|
|
|
/* Null terminate both strings */
|
|
|
|
thisVar[strlenW(thisVar)-1] = 0x00;
|
|
|
|
*equalspos = 0x00;
|
|
|
|
|
|
|
|
/* Since we need to be case insensitive, copy the 2 buffers */
|
2012-11-15 12:32:21 +01:00
|
|
|
searchIn = heap_strdupW(thisVarContents);
|
2009-12-09 18:52:40 +01:00
|
|
|
CharUpperBuffW(searchIn, strlenW(thisVarContents));
|
2012-11-15 12:32:21 +01:00
|
|
|
searchFor = heap_strdupW(colonpos+1);
|
2009-12-09 18:52:40 +01:00
|
|
|
CharUpperBuffW(searchFor, strlenW(colonpos+1));
|
2008-12-31 00:56:53 +01:00
|
|
|
|
|
|
|
/* Handle wildcard case */
|
|
|
|
if (*(colonpos+1) == '*') {
|
|
|
|
/* Search for string to replace */
|
|
|
|
found = strstrW(searchIn, searchFor+1);
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
/* Do replacement */
|
|
|
|
strcpyW(start, replacewith);
|
|
|
|
strcatW(start, thisVarContents + (found-searchIn) + strlenW(searchFor+1));
|
|
|
|
strcatW(start, s);
|
|
|
|
} else {
|
2009-06-15 10:59:31 +02:00
|
|
|
/* Copy as is */
|
2008-12-31 00:56:53 +01:00
|
|
|
strcpyW(start, thisVarContents);
|
|
|
|
strcatW(start, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* Loop replacing all instances */
|
|
|
|
WCHAR *lastFound = searchIn;
|
|
|
|
WCHAR *outputposn = start;
|
|
|
|
|
|
|
|
*start = 0x00;
|
|
|
|
while ((found = strstrW(lastFound, searchFor))) {
|
|
|
|
lstrcpynW(outputposn,
|
|
|
|
thisVarContents + (lastFound-searchIn),
|
|
|
|
(found - lastFound)+1);
|
|
|
|
outputposn = outputposn + (found - lastFound);
|
|
|
|
strcatW(outputposn, replacewith);
|
|
|
|
outputposn = outputposn + strlenW(replacewith);
|
|
|
|
lastFound = found + strlenW(searchFor);
|
|
|
|
}
|
|
|
|
strcatW(outputposn,
|
|
|
|
thisVarContents + (lastFound-searchIn));
|
|
|
|
strcatW(outputposn, s);
|
|
|
|
}
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(s);
|
|
|
|
heap_free(searchIn);
|
|
|
|
heap_free(searchFor);
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
2012-01-03 18:29:40 +01:00
|
|
|
return start;
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Expand the command. Native expands lines from batch programs as they are
|
|
|
|
* read in and not again, except for 'for' variable substitution.
|
|
|
|
* eg. As evidence, "echo %1 && shift && echo %1" or "echo %%path%%"
|
2013-01-29 07:06:07 +01:00
|
|
|
* atExecute is TRUE when the expansion is occurring as the command is executed
|
|
|
|
* rather than at parse time, i.e. delayed expansion and for loops need to be
|
2013-01-07 00:06:28 +01:00
|
|
|
* processed
|
2008-12-31 00:56:53 +01:00
|
|
|
*/
|
2013-01-07 00:06:28 +01:00
|
|
|
static void handleExpansion(WCHAR *cmd, BOOL atExecute, BOOL delayed) {
|
2008-12-31 00:56:53 +01: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 */
|
|
|
|
|
|
|
|
WCHAR *p = cmd;
|
2009-06-15 10:59:31 +02:00
|
|
|
WCHAR *t;
|
2008-12-31 00:56:53 +01:00
|
|
|
int i;
|
2013-01-07 00:06:28 +01:00
|
|
|
WCHAR *delayedp = NULL;
|
|
|
|
WCHAR startchar = '%';
|
|
|
|
WCHAR *normalp;
|
2008-12-31 00:56:53 +01:00
|
|
|
|
2012-11-05 22:43:08 +01:00
|
|
|
/* Display the FOR variables in effect */
|
|
|
|
for (i=0;i<52;i++) {
|
|
|
|
if (forloopcontext.variable[i]) {
|
|
|
|
WINE_TRACE("FOR variable context: %c = '%s'\n",
|
|
|
|
i<26?i+'a':(i-26)+'A',
|
|
|
|
wine_dbgstr_w(forloopcontext.variable[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-07 00:06:28 +01:00
|
|
|
/* Find the next environment variable delimiter */
|
|
|
|
normalp = strchrW(p, '%');
|
|
|
|
if (delayed) delayedp = strchrW(p, '!');
|
|
|
|
if (!normalp) p = delayedp;
|
|
|
|
else if (!delayedp) p = normalp;
|
|
|
|
else p = min(p,delayedp);
|
|
|
|
if (p) startchar = *p;
|
|
|
|
|
|
|
|
while (p) {
|
2008-12-31 00:56:53 +01:00
|
|
|
|
|
|
|
WINE_TRACE("Translate command:%s %d (at: %s)\n",
|
2013-01-07 00:06:28 +01:00
|
|
|
wine_dbgstr_w(cmd), atExecute, wine_dbgstr_w(p));
|
2008-12-31 00:56:53 +01:00
|
|
|
i = *(p+1) - '0';
|
|
|
|
|
2014-02-07 22:42:49 +01:00
|
|
|
/* Don't touch %% unless it's in Batch */
|
2013-01-07 00:06:28 +01:00
|
|
|
if (!atExecute && *(p+1) == startchar) {
|
2008-12-31 00:56:53 +01:00
|
|
|
if (context) {
|
2009-06-15 10:59:31 +02:00
|
|
|
WCMD_strsubstW(p, p+1, NULL, 0);
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
|
|
|
p+=1;
|
|
|
|
|
|
|
|
/* Replace %~ modifications if in batch program */
|
|
|
|
} else if (*(p+1) == '~') {
|
2013-01-07 00:06:28 +01:00
|
|
|
WCMD_HandleTildaModifiers(&p, atExecute);
|
2008-12-31 00:56:53 +01:00
|
|
|
p++;
|
|
|
|
|
|
|
|
/* Replace use of %0...%9 if in batch program*/
|
2013-01-07 00:06:28 +01:00
|
|
|
} else if (!atExecute && context && (i >= 0) && (i <= 9) && startchar == '%') {
|
2012-10-17 00:35:33 +02:00
|
|
|
t = WCMD_parameter(context -> command, i + context -> shift_count[i],
|
2012-10-25 23:47:59 +02:00
|
|
|
NULL, TRUE, TRUE);
|
2009-06-15 10:59:31 +02:00
|
|
|
WCMD_strsubstW(p, p+2, t, -1);
|
2008-12-31 00:56:53 +01:00
|
|
|
|
|
|
|
/* Replace use of %* if in batch program*/
|
2013-01-07 00:06:28 +01:00
|
|
|
} else if (!atExecute && context && *(p+1)=='*' && startchar == '%') {
|
2008-12-31 00:56:53 +01:00
|
|
|
WCHAR *startOfParms = NULL;
|
2012-10-25 23:47:59 +02:00
|
|
|
WCHAR *thisParm = WCMD_parameter(context -> command, 0, &startOfParms, TRUE, TRUE);
|
2012-10-13 23:24:35 +02:00
|
|
|
if (startOfParms != NULL) {
|
2012-10-25 23:47:59 +02:00
|
|
|
startOfParms += strlenW(thisParm);
|
2012-10-13 23:24:35 +02:00
|
|
|
while (*startOfParms==' ' || *startOfParms == '\t') startOfParms++;
|
2009-06-15 10:59:31 +02:00
|
|
|
WCMD_strsubstW(p, p+2, startOfParms, -1);
|
2012-10-13 23:24:35 +02:00
|
|
|
} else
|
2009-06-15 10:59:31 +02:00
|
|
|
WCMD_strsubstW(p, p+2, NULL, 0);
|
2008-12-31 00:56:53 +01:00
|
|
|
|
2012-11-05 22:43:08 +01:00
|
|
|
} else {
|
|
|
|
int forvaridx = FOR_VAR_IDX(*(p+1));
|
2013-01-07 00:06:28 +01:00
|
|
|
if (startchar == '%' && forvaridx != -1 && forloopcontext.variable[forvaridx]) {
|
2012-11-05 22:43:08 +01:00
|
|
|
/* Replace the 2 characters, % and for variable character */
|
|
|
|
WCMD_strsubstW(p, p + 2, forloopcontext.variable[forvaridx], -1);
|
2015-10-14 01:58:38 +02:00
|
|
|
} else if (!atExecute || startchar == '!') {
|
2013-01-07 00:06:28 +01:00
|
|
|
p = WCMD_expand_envvar(p, startchar);
|
2012-11-05 22:43:08 +01:00
|
|
|
|
|
|
|
/* In a FOR loop, see if this is the variable to replace */
|
|
|
|
} else { /* Ignore %'s on second pass of batch program */
|
|
|
|
p++;
|
|
|
|
}
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
2013-01-07 00:06:28 +01:00
|
|
|
|
|
|
|
/* Find the next environment variable delimiter */
|
|
|
|
normalp = strchrW(p, '%');
|
|
|
|
if (delayed) delayedp = strchrW(p, '!');
|
|
|
|
if (!normalp) p = delayedp;
|
|
|
|
else if (!delayedp) p = normalp;
|
|
|
|
else p = min(p,delayedp);
|
|
|
|
if (p) startchar = *p;
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
2007-09-11 22:43:05 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
return;
|
|
|
|
}
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
|
|
|
|
/*******************************************************************
|
|
|
|
* 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.
|
|
|
|
*/
|
2011-08-20 01:58:59 +02:00
|
|
|
static void WCMD_parse (const WCHAR *s, WCHAR *q, WCHAR *p1, WCHAR *p2)
|
2008-12-31 00:56:53 +01:00
|
|
|
{
|
|
|
|
int p = 0;
|
|
|
|
|
|
|
|
*q = *p1 = *p2 = '\0';
|
|
|
|
while (TRUE) {
|
|
|
|
switch (*s) {
|
|
|
|
case '/':
|
|
|
|
*q++ = *s++;
|
|
|
|
while ((*s != '\0') && (*s != ' ') && *s != '/') {
|
|
|
|
*q++ = toupperW (*s++);
|
|
|
|
}
|
|
|
|
*q = '\0';
|
1999-06-06 17:24:04 +02:00
|
|
|
break;
|
2008-12-31 00:56:53 +01:00
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
s++;
|
1999-06-06 17:24:04 +02:00
|
|
|
break;
|
2008-12-31 00:56:53 +01:00
|
|
|
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++;
|
1999-06-06 17:24:04 +02:00
|
|
|
break;
|
2008-12-31 00:56:53 +01:00
|
|
|
case '\0':
|
|
|
|
return;
|
1999-06-06 17:24:04 +02:00
|
|
|
default:
|
2008-12-31 00:56:53 +01:00
|
|
|
while ((*s != '\0') && (*s != ' ') && (*s != '\t')
|
|
|
|
&& (*s != '=') && (*s != ',') ) {
|
|
|
|
if (p == 0) *p1++ = *s++;
|
|
|
|
else if (p == 1) *p2++ = *s++;
|
|
|
|
else s++;
|
|
|
|
}
|
|
|
|
/* Skip concurrent parms */
|
|
|
|
while ((*s == ' ') || (*s == '\t') || (*s == '=') || (*s == ',') ) s++;
|
2007-03-13 01:09:05 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
if (p == 0) *p1 = '\0';
|
|
|
|
if (p == 1) *p2 = '\0';
|
|
|
|
p++;
|
2005-07-15 11:55:52 +02:00
|
|
|
}
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
2005-07-15 11:55:52 +02:00
|
|
|
}
|
|
|
|
|
2009-12-09 18:52:40 +01:00
|
|
|
static void init_msvcrt_io_block(STARTUPINFOW* st)
|
2005-07-15 11:55:52 +02:00
|
|
|
{
|
2009-12-09 18:52:40 +01:00
|
|
|
STARTUPINFOW st_p;
|
2005-07-15 11:55:52 +02:00
|
|
|
/* fetch the parent MSVCRT info block if any, so that the child can use the
|
|
|
|
* same handles as its grand-father
|
|
|
|
*/
|
2009-12-09 18:52:40 +01:00
|
|
|
st_p.cb = sizeof(STARTUPINFOW);
|
|
|
|
GetStartupInfoW(&st_p);
|
2005-07-15 11:55:52 +02:00
|
|
|
st->cbReserved2 = st_p.cbReserved2;
|
|
|
|
st->lpReserved2 = st_p.lpReserved2;
|
2007-03-04 23:39:22 +01:00
|
|
|
if (st_p.cbReserved2 && st_p.lpReserved2)
|
2005-07-15 11:55:52 +02:00
|
|
|
{
|
2012-11-15 12:32:21 +01:00
|
|
|
unsigned num = *(unsigned*)st_p.lpReserved2;
|
|
|
|
char* flags;
|
|
|
|
HANDLE* handles;
|
|
|
|
BYTE *ptr;
|
|
|
|
size_t sz;
|
|
|
|
|
2005-07-15 11:55:52 +02:00
|
|
|
/* Override the entries for fd 0,1,2 if we happened
|
2011-07-27 02:21:01 +02:00
|
|
|
* to change those std handles (this depends on the way cmd sets
|
|
|
|
* its new input & output handles)
|
2005-07-15 11:55:52 +02:00
|
|
|
*/
|
2012-11-15 12:32:21 +01:00
|
|
|
sz = max(sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * 3, st_p.cbReserved2);
|
|
|
|
ptr = heap_alloc(sz);
|
|
|
|
flags = (char*)(ptr + sizeof(unsigned));
|
|
|
|
handles = (HANDLE*)(flags + num * sizeof(char));
|
2005-07-15 11:55:52 +02:00
|
|
|
|
2012-11-15 12:32:21 +01:00
|
|
|
memcpy(ptr, st_p.lpReserved2, st_p.cbReserved2);
|
|
|
|
st->cbReserved2 = sz;
|
|
|
|
st->lpReserved2 = ptr;
|
2005-08-19 12:04:03 +02:00
|
|
|
|
2005-07-15 11:55:52 +02:00
|
|
|
#define WX_OPEN 0x01 /* see dlls/msvcrt/file.c */
|
2012-11-15 12:32:21 +01:00
|
|
|
if (num <= 0 || (flags[0] & WX_OPEN))
|
|
|
|
{
|
|
|
|
handles[0] = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
flags[0] |= WX_OPEN;
|
2005-07-15 11:55:52 +02:00
|
|
|
}
|
2012-11-15 12:32:21 +01:00
|
|
|
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-07-10 13:36:45 +02:00
|
|
|
}
|
2003-07-10 00:21:04 +02:00
|
|
|
}
|
1999-06-06 17:24:04 +02:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* WCMD_run_program
|
|
|
|
*
|
2007-02-27 00:04:16 +01:00
|
|
|
* Execute a command line as an external program. Must allow recursion.
|
1999-06-06 17:24:04 +02:00
|
|
|
*
|
2007-02-27 00:04:16 +01: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
|
2008-01-16 12:20:50 +01:00
|
|
|
* findexecutable to achieve this which is left untouched.
|
2012-10-14 01:38:53 +02:00
|
|
|
* If an executable has not been found, and we were launched through
|
|
|
|
* a call, we need to check if the command is an internal command,
|
|
|
|
* so go back through wcmd_execute.
|
1999-06-06 17:24:04 +02:00
|
|
|
*/
|
|
|
|
|
2012-03-29 18:05:59 +02:00
|
|
|
void WCMD_run_program (WCHAR *command, BOOL called)
|
|
|
|
{
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR temp[MAX_PATH];
|
|
|
|
WCHAR pathtosearch[MAXSTRING];
|
|
|
|
WCHAR *pathposn;
|
2009-03-02 19:45:50 +01:00
|
|
|
WCHAR stemofsearch[MAX_PATH]; /* maximum allowed executable name is
|
|
|
|
MAX_PATH, including null character */
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR *lastSlash;
|
|
|
|
WCHAR pathext[MAXSTRING];
|
2012-10-13 23:11:03 +02:00
|
|
|
WCHAR *firstParam;
|
2007-02-27 00:04:16 +01:00
|
|
|
BOOL extensionsupplied = FALSE;
|
|
|
|
BOOL status;
|
2007-02-28 15:23:15 +01:00
|
|
|
DWORD len;
|
2007-06-03 23:07:42 +02:00
|
|
|
static const WCHAR envPath[] = {'P','A','T','H','\0'};
|
|
|
|
static const WCHAR delims[] = {'/','\\',':','\0'};
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2012-10-13 23:11:03 +02:00
|
|
|
/* Quick way to get the filename is to extract the first argument. */
|
|
|
|
WINE_TRACE("Running '%s' (%d)\n", wine_dbgstr_w(command), called);
|
2012-10-25 23:47:59 +02:00
|
|
|
firstParam = WCMD_parameter(command, 0, NULL, FALSE, TRUE);
|
2012-10-13 23:11:03 +02:00
|
|
|
if (!firstParam) return;
|
2007-02-27 00:04:16 +01:00
|
|
|
|
|
|
|
/* Calculate the search path and stem to search for */
|
2012-10-13 23:11:03 +02:00
|
|
|
if (strpbrkW (firstParam, delims) == NULL) { /* No explicit path given, search path */
|
2007-06-03 23:07:42 +02:00
|
|
|
static const WCHAR curDir[] = {'.',';','\0'};
|
|
|
|
strcpyW(pathtosearch, curDir);
|
2018-08-20 21:15:42 +02:00
|
|
|
len = GetEnvironmentVariableW(envPath, &pathtosearch[2], ARRAY_SIZE(pathtosearch)-2);
|
|
|
|
if ((len == 0) || (len >= ARRAY_SIZE(pathtosearch) - 2)) {
|
2007-06-03 23:07:42 +02:00
|
|
|
static const WCHAR curDir[] = {'.','\0'};
|
|
|
|
strcpyW (pathtosearch, curDir);
|
2003-03-25 01:33:56 +01:00
|
|
|
}
|
2012-10-13 23:11:03 +02:00
|
|
|
if (strchrW(firstParam, '.') != NULL) extensionsupplied = TRUE;
|
|
|
|
if (strlenW(firstParam) >= MAX_PATH)
|
2009-03-02 19:45:50 +01:00
|
|
|
{
|
2011-10-01 04:15:05 +02:00
|
|
|
WCMD_output_asis_stderr(WCMD_LoadMessage(WCMD_LINETOOLONG));
|
2009-03-02 19:45:50 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-13 23:11:03 +02:00
|
|
|
strcpyW(stemofsearch, firstParam);
|
2007-02-27 00:04:16 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* Convert eg. ..\fred to include a directory by removing file part */
|
2018-08-20 21:15:42 +02:00
|
|
|
GetFullPathNameW(firstParam, ARRAY_SIZE(pathtosearch), pathtosearch, NULL);
|
2007-06-03 23:07:42 +02:00
|
|
|
lastSlash = strrchrW(pathtosearch, '\\');
|
|
|
|
if (lastSlash && strchrW(lastSlash, '.') != NULL) extensionsupplied = TRUE;
|
|
|
|
strcpyW(stemofsearch, lastSlash+1);
|
2008-03-04 00:14:28 +01:00
|
|
|
|
|
|
|
/* Reduce pathtosearch to a path with trailing '\' to support c:\a.bat and
|
|
|
|
c:\windows\a.bat syntax */
|
|
|
|
if (lastSlash) *(lastSlash + 1) = 0x00;
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
2007-02-27 00:04:16 +01:00
|
|
|
|
|
|
|
/* Now extract PATHEXT */
|
2018-08-20 21:15:42 +02:00
|
|
|
len = GetEnvironmentVariableW(envPathExt, pathext, ARRAY_SIZE(pathext));
|
|
|
|
if ((len == 0) || (len >= ARRAY_SIZE(pathext))) {
|
2007-06-03 23:07:42 +02:00
|
|
|
strcpyW (pathext, dfltPathExt);
|
2007-02-27 00:04:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop through the search path, dir by dir */
|
|
|
|
pathposn = pathtosearch;
|
2007-06-03 23:07:42 +02:00
|
|
|
WINE_TRACE("Searching in '%s' for '%s'\n", wine_dbgstr_w(pathtosearch),
|
|
|
|
wine_dbgstr_w(stemofsearch));
|
2015-02-21 18:23:01 +01:00
|
|
|
while (pathposn) {
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR thisDir[MAX_PATH] = {'\0'};
|
2018-08-02 19:48:59 +02:00
|
|
|
int length = 0;
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR *pos = NULL;
|
2007-02-27 00:04:16 +01:00
|
|
|
BOOL found = FALSE;
|
2018-08-02 19:48:59 +02:00
|
|
|
BOOL inside_quotes = FALSE;
|
2007-02-27 00:04:16 +01:00
|
|
|
|
|
|
|
/* Work on the first directory on the search path */
|
2018-08-02 19:48:59 +02:00
|
|
|
pos = pathposn;
|
|
|
|
while ((inside_quotes || *pos != ';') && *pos != 0)
|
|
|
|
{
|
|
|
|
if (*pos == '"')
|
|
|
|
inside_quotes = !inside_quotes;
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*pos) { /* Reached semicolon */
|
2007-06-03 23:07:42 +02:00
|
|
|
memcpy(thisDir, pathposn, (pos-pathposn) * sizeof(WCHAR));
|
2007-02-27 00:04:16 +01:00
|
|
|
thisDir[(pos-pathposn)] = 0x00;
|
|
|
|
pathposn = pos+1;
|
2018-08-02 19:48:59 +02:00
|
|
|
} else { /* Reached string end */
|
2007-06-03 23:07:42 +02:00
|
|
|
strcpyW(thisDir, pathposn);
|
2007-02-27 00:04:16 +01:00
|
|
|
pathposn = NULL;
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
2003-08-22 07:01:59 +02:00
|
|
|
|
2018-08-02 19:48:59 +02:00
|
|
|
/* Remove quotes */
|
|
|
|
length = strlenW(thisDir);
|
|
|
|
if (thisDir[length - 1] == '"')
|
|
|
|
thisDir[length - 1] = 0;
|
|
|
|
|
|
|
|
if (*thisDir != '"')
|
|
|
|
strcpyW(temp, thisDir);
|
|
|
|
else
|
|
|
|
strcpyW(temp, thisDir + 1);
|
|
|
|
|
2007-02-27 00:04:16 +01:00
|
|
|
/* Since you can have eg. ..\.. on the path, need to expand
|
|
|
|
to full information */
|
2009-12-09 18:52:40 +01:00
|
|
|
GetFullPathNameW(temp, MAX_PATH, thisDir, NULL);
|
2007-02-27 00:04:16 +01:00
|
|
|
|
|
|
|
/* 1. If extension supplied, see if that file exists */
|
2007-06-03 23:07:42 +02:00
|
|
|
strcatW(thisDir, slashW);
|
|
|
|
strcatW(thisDir, stemofsearch);
|
|
|
|
pos = &thisDir[strlenW(thisDir)]; /* Pos = end of name */
|
2007-02-27 00:04:16 +01:00
|
|
|
|
2007-04-05 23:47:53 +02:00
|
|
|
/* 1. If extension supplied, see if that file exists */
|
|
|
|
if (extensionsupplied) {
|
2009-12-09 18:52:40 +01:00
|
|
|
if (GetFileAttributesW(thisDir) != INVALID_FILE_ATTRIBUTES) {
|
2007-04-05 23:47:53 +02:00
|
|
|
found = TRUE;
|
|
|
|
}
|
2007-02-27 00:04:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 2. Any .* matches? */
|
|
|
|
if (!found) {
|
|
|
|
HANDLE h;
|
2009-12-09 18:52:40 +01:00
|
|
|
WIN32_FIND_DATAW finddata;
|
2007-06-03 23:07:42 +02:00
|
|
|
static const WCHAR allFiles[] = {'.','*','\0'};
|
2007-02-27 00:04:16 +01:00
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
strcatW(thisDir,allFiles);
|
2009-12-09 18:52:40 +01:00
|
|
|
h = FindFirstFileW(thisDir, &finddata);
|
2007-02-27 00:04:16 +01:00
|
|
|
FindClose(h);
|
1999-06-06 17:24:04 +02:00
|
|
|
if (h != INVALID_HANDLE_VALUE) {
|
2007-02-27 00:04:16 +01:00
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR *thisExt = pathext;
|
2007-02-27 00:04:16 +01:00
|
|
|
|
|
|
|
/* 3. Yes - Try each path ext */
|
|
|
|
while (thisExt) {
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR *nextExt = strchrW(thisExt, ';');
|
2007-02-27 00:04:16 +01:00
|
|
|
|
|
|
|
if (nextExt) {
|
2007-06-03 23:07:42 +02:00
|
|
|
memcpy(pos, thisExt, (nextExt-thisExt) * sizeof(WCHAR));
|
2007-02-27 00:04:16 +01:00
|
|
|
pos[(nextExt-thisExt)] = 0x00;
|
|
|
|
thisExt = nextExt+1;
|
|
|
|
} else {
|
2007-06-03 23:07:42 +02:00
|
|
|
strcpyW(pos, thisExt);
|
2007-02-27 00:04:16 +01:00
|
|
|
thisExt = NULL;
|
|
|
|
}
|
|
|
|
|
2009-12-09 18:52:40 +01:00
|
|
|
if (GetFileAttributesW(thisDir) != INVALID_FILE_ATTRIBUTES) {
|
2007-02-27 00:04:16 +01:00
|
|
|
found = TRUE;
|
|
|
|
thisExt = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Once found, launch it */
|
2013-06-04 22:27:48 +02:00
|
|
|
if (found) {
|
2009-12-09 18:52:40 +01:00
|
|
|
STARTUPINFOW st;
|
2007-02-27 00:04:16 +01:00
|
|
|
PROCESS_INFORMATION pe;
|
2009-12-09 18:52:40 +01:00
|
|
|
SHFILEINFOW psfi;
|
2007-02-27 00:04:16 +01:00
|
|
|
DWORD console;
|
|
|
|
HINSTANCE hinst;
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR *ext = strrchrW( thisDir, '.' );
|
|
|
|
static const WCHAR batExt[] = {'.','b','a','t','\0'};
|
|
|
|
static const WCHAR cmdExt[] = {'.','c','m','d','\0'};
|
|
|
|
|
2013-06-04 22:27:48 +02:00
|
|
|
WINE_TRACE("Found as %s\n", wine_dbgstr_w(thisDir));
|
|
|
|
|
2007-02-27 00:04:16 +01:00
|
|
|
/* Special case BAT and CMD */
|
2011-12-20 14:48:24 +01:00
|
|
|
if (ext && (!strcmpiW(ext, batExt) || !strcmpiW(ext, cmdExt))) {
|
2012-10-04 10:57:33 +02:00
|
|
|
BOOL oldinteractive = interactive;
|
|
|
|
interactive = FALSE;
|
2007-02-27 00:04:16 +01:00
|
|
|
WCMD_batch (thisDir, command, called, NULL, INVALID_HANDLE_VALUE);
|
2012-10-04 10:57:33 +02:00
|
|
|
interactive = oldinteractive;
|
2007-02-27 00:04:16 +01:00
|
|
|
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 */
|
2009-12-09 18:52:40 +01:00
|
|
|
hinst = FindExecutableW (thisDir, NULL, temp);
|
2007-02-27 00:04:16 +01:00
|
|
|
if ((INT_PTR)hinst < 32)
|
|
|
|
console = 0;
|
|
|
|
else
|
2009-12-09 18:52:40 +01:00
|
|
|
console = SHGetFileInfoW(temp, 0, &psfi, sizeof(psfi), SHGFI_EXETYPE);
|
2007-02-27 00:04:16 +01:00
|
|
|
|
2009-12-09 18:52:40 +01:00
|
|
|
ZeroMemory (&st, sizeof(STARTUPINFOW));
|
|
|
|
st.cb = sizeof(STARTUPINFOW);
|
2007-02-27 00:04:16 +01:00
|
|
|
init_msvcrt_io_block(&st);
|
|
|
|
|
2007-04-05 23:47:54 +02:00
|
|
|
/* Launch the process and if a CUI wait on it to complete
|
|
|
|
Note: Launching internal wine processes cannot specify a full path to exe */
|
2013-06-04 22:27:48 +02:00
|
|
|
status = CreateProcessW(thisDir,
|
2007-04-05 23:47:54 +02:00
|
|
|
command, NULL, NULL, TRUE, 0, NULL, NULL, &st, &pe);
|
2013-03-01 14:31:13 +01:00
|
|
|
heap_free(st.lpReserved2);
|
2007-02-27 00:04:16 +01:00
|
|
|
if ((opt_c || opt_k) && !opt_s && !status
|
|
|
|
&& GetLastError()==ERROR_FILE_NOT_FOUND && command[0]=='\"') {
|
2007-06-03 23:07:42 +02:00
|
|
|
/* strip first and last quote WCHARacters and try again */
|
2011-10-19 09:47:58 +02:00
|
|
|
WCMD_strip_quotes(command);
|
2011-11-29 14:39:26 +01:00
|
|
|
opt_s = TRUE;
|
2007-02-27 00:04:16 +01:00
|
|
|
WCMD_run_program(command, called);
|
|
|
|
return;
|
|
|
|
}
|
2009-12-04 10:06:13 +01:00
|
|
|
|
|
|
|
if (!status)
|
|
|
|
break;
|
|
|
|
|
2013-01-16 14:40:31 +01:00
|
|
|
/* Always wait when non-interactive (cmd /c or in batch program),
|
|
|
|
or for console applications */
|
2013-06-04 22:27:48 +02:00
|
|
|
if (!interactive || (console && !HIWORD(console)))
|
2013-01-16 14:40:31 +01:00
|
|
|
WaitForSingleObject (pe.hProcess, INFINITE);
|
|
|
|
GetExitCodeProcess (pe.hProcess, &errorlevel);
|
|
|
|
if (errorlevel == STILL_ACTIVE) errorlevel = 0;
|
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
CloseHandle(pe.hProcess);
|
|
|
|
CloseHandle(pe.hThread);
|
|
|
|
return;
|
2007-06-15 00:06:37 +02:00
|
|
|
}
|
2007-06-03 23:07:42 +02:00
|
|
|
}
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
|
|
|
|
2012-10-14 01:38:53 +02:00
|
|
|
/* Not found anywhere - were we called? */
|
|
|
|
if (called) {
|
|
|
|
CMD_LIST *toExecute = NULL; /* Commands left to be executed */
|
|
|
|
|
|
|
|
/* Parse the command string, without reading any more input */
|
|
|
|
WCMD_ReadAndParseLine(command, &toExecute, INVALID_HANDLE_VALUE);
|
2012-11-05 22:43:08 +01:00
|
|
|
WCMD_process_commands(toExecute, FALSE, called);
|
2012-10-14 01:38:53 +02:00
|
|
|
WCMD_free_commands(toExecute);
|
|
|
|
toExecute = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Not found anywhere - give up */
|
2012-10-26 13:47:52 +02:00
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_NO_COMMAND_FOUND), command);
|
2008-12-31 00:56:53 +01:00
|
|
|
|
|
|
|
/* If a command fails to launch, it sets errorlevel 9009 - which
|
|
|
|
does not seem to have any associated constant definition */
|
|
|
|
errorlevel = 9009;
|
|
|
|
return;
|
|
|
|
|
2007-06-03 23:07:40 +02:00
|
|
|
}
|
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Process one command. If the command is EXIT this routine does not return.
|
|
|
|
* We will recurse through here executing batch files.
|
2012-10-14 01:38:53 +02:00
|
|
|
* Note: If call is used to a non-existing program, we reparse the line and
|
|
|
|
* try to run it as an internal command. 'retrycall' represents whether
|
|
|
|
* we are attempting this retry.
|
1999-06-06 17:24:04 +02:00
|
|
|
*/
|
2011-08-20 01:58:59 +02:00
|
|
|
void WCMD_execute (const WCHAR *command, const WCHAR *redirects,
|
2012-10-14 01:38:53 +02:00
|
|
|
CMD_LIST **cmdList, BOOL retrycall)
|
2008-12-31 00:56:53 +01:00
|
|
|
{
|
2018-08-27 21:41:50 +02:00
|
|
|
WCHAR *cmd, *parms_start, *redir;
|
|
|
|
WCHAR *pos;
|
|
|
|
int status, i, cmd_index;
|
2008-12-31 00:56:53 +01:00
|
|
|
DWORD count, creationDisposition;
|
|
|
|
HANDLE h;
|
|
|
|
WCHAR *whichcmd;
|
|
|
|
SECURITY_ATTRIBUTES sa;
|
|
|
|
WCHAR *new_cmd = NULL;
|
|
|
|
WCHAR *new_redir = NULL;
|
2009-02-09 21:47:54 +01:00
|
|
|
HANDLE old_stdhandles[3] = {GetStdHandle (STD_INPUT_HANDLE),
|
|
|
|
GetStdHandle (STD_OUTPUT_HANDLE),
|
|
|
|
GetStdHandle (STD_ERROR_HANDLE)};
|
2008-12-31 00:56:53 +01:00
|
|
|
DWORD idx_stdhandles[3] = {STD_INPUT_HANDLE,
|
|
|
|
STD_OUTPUT_HANDLE,
|
|
|
|
STD_ERROR_HANDLE};
|
2011-09-16 08:44:06 +02:00
|
|
|
BOOL prev_echo_mode, piped = FALSE;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2012-11-05 22:43:08 +01:00
|
|
|
WINE_TRACE("command on entry:%s (%p)\n",
|
|
|
|
wine_dbgstr_w(command), cmdList);
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2018-08-27 21:41:50 +02:00
|
|
|
/* Move copy of the command onto the heap so it can be expanded */
|
|
|
|
new_cmd = heap_alloc(MAXSTRING * sizeof(WCHAR));
|
|
|
|
strcpyW(new_cmd, command);
|
|
|
|
cmd = new_cmd;
|
|
|
|
|
|
|
|
/* Move copy of the redirects onto the heap so it can be expanded */
|
|
|
|
new_redir = heap_alloc(MAXSTRING * sizeof(WCHAR));
|
|
|
|
redir = new_redir;
|
|
|
|
|
|
|
|
/* Strip leading whitespaces, and a '@' if supplied */
|
|
|
|
whichcmd = WCMD_skip_leading_spaces(cmd);
|
|
|
|
WINE_TRACE("Command: '%s'\n", wine_dbgstr_w(cmd));
|
|
|
|
if (whichcmd[0] == '@') whichcmd++;
|
|
|
|
|
|
|
|
/* Check if the command entered is internal, and identify which one */
|
|
|
|
count = 0;
|
|
|
|
while (IsCharAlphaNumericW(whichcmd[count])) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
for (i=0; i<=WCMD_EXIT; i++) {
|
|
|
|
if (CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
|
|
|
|
whichcmd, count, inbuilt[i], -1) == CSTR_EQUAL) break;
|
|
|
|
}
|
|
|
|
cmd_index = i;
|
|
|
|
parms_start = WCMD_skip_leading_spaces (&whichcmd[count]);
|
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* If the next command is a pipe then we implement pipes by redirecting
|
|
|
|
the output from this command to a temp file and input into the
|
|
|
|
next command from that temp file.
|
2018-08-27 21:41:50 +02:00
|
|
|
Note: Do not do this for a for or if statement as the pipe is for
|
|
|
|
the individual statements, not the for or if itself.
|
2008-12-31 00:56:53 +01:00
|
|
|
FIXME: Use of named pipes would make more sense here as currently this
|
|
|
|
process has to finish before the next one can start but this requires
|
|
|
|
a change to not wait for the first app to finish but rather the pipe */
|
2018-08-27 21:41:50 +02:00
|
|
|
if (!(cmd_index == WCMD_FOR || cmd_index == WCMD_IF) &&
|
|
|
|
cmdList && (*cmdList)->nextcommand &&
|
2008-12-31 00:56:53 +01:00
|
|
|
(*cmdList)->nextcommand->prevDelim == CMD_PIPE) {
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
WCHAR temp_path[MAX_PATH];
|
|
|
|
static const WCHAR cmdW[] = {'C','M','D','\0'};
|
2003-02-25 04:58:42 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Remember piping is in action */
|
|
|
|
WINE_TRACE("Output needs to be piped\n");
|
|
|
|
piped = TRUE;
|
2003-02-25 04:58:42 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Generate a unique temporary filename */
|
2018-08-20 21:15:42 +02:00
|
|
|
GetTempPathW(ARRAY_SIZE(temp_path), temp_path);
|
2009-12-09 18:52:40 +01:00
|
|
|
GetTempFileNameW(temp_path, cmdW, 0, (*cmdList)->nextcommand->pipeFile);
|
2008-12-31 00:56:53 +01:00
|
|
|
WINE_TRACE("Using temporary file of %s\n",
|
|
|
|
wine_dbgstr_w((*cmdList)->nextcommand->pipeFile));
|
|
|
|
}
|
2003-02-25 04:58:42 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* If piped output, send stdout to the pipe by appending >filename to redirects */
|
|
|
|
if (piped) {
|
|
|
|
static const WCHAR redirOut[] = {'%','s',' ','>',' ','%','s','\0'};
|
2009-12-09 18:52:40 +01:00
|
|
|
wsprintfW (new_redir, redirOut, redirects, (*cmdList)->nextcommand->pipeFile);
|
2008-12-31 00:56:53 +01:00
|
|
|
WINE_TRACE("Redirects now %s\n", wine_dbgstr_w(new_redir));
|
|
|
|
} else {
|
|
|
|
strcpyW(new_redir, redirects);
|
|
|
|
}
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Expand variables in command line mode only (batch mode will
|
|
|
|
be expanded as the line is read in, except for 'for' loops) */
|
2013-01-07 00:06:28 +01:00
|
|
|
handleExpansion(new_cmd, (context != NULL), delayedsubst);
|
|
|
|
handleExpansion(new_redir, (context != NULL), delayedsubst);
|
2008-12-31 00:56:53 +01:00
|
|
|
|
|
|
|
/*
|
2018-07-04 23:08:38 +02:00
|
|
|
* Changing default drive has to be handled as a special case, anything
|
|
|
|
* else if it exists after whitespace is ignored
|
2000-08-01 04:14:33 +02:00
|
|
|
*/
|
|
|
|
|
2018-07-04 23:08:38 +02:00
|
|
|
if ((cmd[1] == ':') && IsCharAlphaW(cmd[0]) &&
|
|
|
|
(!cmd[2] || cmd[2] == ' ' || cmd[2] == '\t')) {
|
2008-12-31 00:56:53 +01:00
|
|
|
WCHAR envvar[5];
|
|
|
|
WCHAR dir[MAX_PATH];
|
2003-02-25 04:58:42 +01:00
|
|
|
|
2018-07-04 23:08:38 +02:00
|
|
|
/* Ignore potential garbage on the same line */
|
|
|
|
cmd[2]=0x00;
|
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* 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 */
|
2011-11-12 13:58:01 +01:00
|
|
|
strcpyW(envvar, equalW);
|
2008-12-31 00:56:53 +01:00
|
|
|
strcatW(envvar, cmd);
|
2009-12-09 18:52:40 +01:00
|
|
|
if (GetEnvironmentVariableW(envvar, dir, MAX_PATH) == 0) {
|
2008-12-31 00:56:53 +01:00
|
|
|
static const WCHAR fmt[] = {'%','s','\\','\0'};
|
2009-12-09 18:52:40 +01:00
|
|
|
wsprintfW(cmd, fmt, cmd);
|
2008-12-31 00:56:53 +01:00
|
|
|
WINE_TRACE("No special directory settings, using dir of %s\n", wine_dbgstr_w(cmd));
|
2003-02-25 04:58:42 +01:00
|
|
|
}
|
2008-12-31 00:56:53 +01:00
|
|
|
WINE_TRACE("Got directory %s as %s\n", wine_dbgstr_w(envvar), wine_dbgstr_w(cmd));
|
2009-12-09 18:52:40 +01:00
|
|
|
status = SetCurrentDirectoryW(cmd);
|
2008-12-31 00:56:53 +01:00
|
|
|
if (!status) WCMD_print_error ();
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(cmd );
|
|
|
|
heap_free(new_redir);
|
2008-12-31 00:56:53 +01:00
|
|
|
return;
|
|
|
|
}
|
2000-08-01 04:14:33 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
sa.nLength = sizeof(sa);
|
|
|
|
sa.lpSecurityDescriptor = NULL;
|
|
|
|
sa.bInheritHandle = TRUE;
|
2007-06-03 23:07:42 +02:00
|
|
|
|
2018-08-27 21:41:50 +02:00
|
|
|
/*
|
|
|
|
* Redirect stdin, stdout and/or stderr if required.
|
|
|
|
* Note: Do not do this for a for or if statement as the pipe is for
|
|
|
|
* the individual statements, not the for or if itself.
|
|
|
|
*/
|
|
|
|
if (!(cmd_index == WCMD_FOR || cmd_index == WCMD_IF)) {
|
|
|
|
/* STDIN could come from a preceding pipe, so delete on close if it does */
|
|
|
|
if (cmdList && (*cmdList)->pipeFile[0] != 0x00) {
|
|
|
|
WINE_TRACE("Input coming from %s\n", wine_dbgstr_w((*cmdList)->pipeFile));
|
|
|
|
h = CreateFileW((*cmdList)->pipeFile, GENERIC_READ,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, NULL);
|
|
|
|
if (h == INVALID_HANDLE_VALUE) {
|
|
|
|
WCMD_print_error ();
|
|
|
|
heap_free(cmd);
|
|
|
|
heap_free(new_redir);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SetStdHandle (STD_INPUT_HANDLE, h);
|
|
|
|
|
|
|
|
/* No need to remember the temporary name any longer once opened */
|
|
|
|
(*cmdList)->pipeFile[0] = 0x00;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2018-08-27 21:41:50 +02:00
|
|
|
/* Otherwise STDIN could come from a '<' redirect */
|
|
|
|
} else if ((pos = strchrW(new_redir,'<')) != NULL) {
|
|
|
|
h = CreateFileW(WCMD_parameter(++pos, 0, NULL, FALSE, FALSE), GENERIC_READ, FILE_SHARE_READ,
|
|
|
|
&sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
2008-12-31 00:56:53 +01:00
|
|
|
if (h == INVALID_HANDLE_VALUE) {
|
|
|
|
WCMD_print_error ();
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(cmd);
|
|
|
|
heap_free(new_redir);
|
2008-12-31 00:56:53 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
SetStdHandle (STD_INPUT_HANDLE, h);
|
|
|
|
}
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2018-08-27 21:41:50 +02:00
|
|
|
/* Scan the whole command looking for > and 2> */
|
|
|
|
while (redir != NULL && ((pos = strchrW(redir,'>')) != NULL)) {
|
|
|
|
int handle = 0;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2018-08-27 21:41:50 +02:00
|
|
|
if (pos > redir && (*(pos-1)=='2'))
|
|
|
|
handle = 2;
|
|
|
|
else
|
|
|
|
handle = 1;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2018-08-27 21:41:50 +02:00
|
|
|
pos++;
|
|
|
|
if ('>' == *pos) {
|
|
|
|
creationDisposition = OPEN_ALWAYS;
|
|
|
|
pos++;
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
2018-08-27 21:41:50 +02:00
|
|
|
else {
|
|
|
|
creationDisposition = CREATE_ALWAYS;
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
2001-06-04 04:55:38 +02:00
|
|
|
|
2018-08-27 21:41:50 +02:00
|
|
|
/* Add support for 2>&1 */
|
|
|
|
redir = pos;
|
|
|
|
if (*pos == '&') {
|
|
|
|
int idx = *(pos+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);
|
2006-07-25 02:20:31 +02:00
|
|
|
|
2018-08-27 21:41:50 +02:00
|
|
|
} else {
|
|
|
|
WCHAR *param = WCMD_parameter(pos, 0, NULL, FALSE, FALSE);
|
|
|
|
h = CreateFileW(param, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_DELETE,
|
|
|
|
&sa, creationDisposition, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (h == INVALID_HANDLE_VALUE) {
|
|
|
|
WCMD_print_error ();
|
|
|
|
heap_free(cmd);
|
|
|
|
heap_free(new_redir);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (SetFilePointer (h, 0, NULL, FILE_END) ==
|
|
|
|
INVALID_SET_FILE_POINTER) {
|
|
|
|
WCMD_print_error ();
|
|
|
|
}
|
|
|
|
WINE_TRACE("Redirect %d to '%s' (%p)\n", handle, wine_dbgstr_w(param), h);
|
|
|
|
}
|
2007-06-03 23:07:42 +02:00
|
|
|
|
2018-08-27 21:41:50 +02:00
|
|
|
SetStdHandle (idx_stdhandles[handle], h);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
WINE_TRACE("Not touching redirects for a FOR or IF command\n");
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_parse (parms_start, quals, param1, param2);
|
2008-12-31 00:56:53 +01:00
|
|
|
WINE_TRACE("param1: %s, param2: %s\n", wine_dbgstr_w(param1), wine_dbgstr_w(param2));
|
2007-09-11 22:43:03 +02:00
|
|
|
|
2018-08-27 21:41:50 +02:00
|
|
|
if (i <= WCMD_EXIT && (parms_start[0] == '/') && (parms_start[1] == '?')) {
|
2010-06-22 00:44:15 +02:00
|
|
|
/* this is a help request for a builtin program */
|
2010-03-02 22:26:09 +01:00
|
|
|
i = WCMD_HELP;
|
2018-08-27 21:41:50 +02:00
|
|
|
memcpy(parms_start, whichcmd, count * sizeof(WCHAR));
|
|
|
|
parms_start[count] = '\0';
|
2010-03-02 22:26:09 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
switch (i) {
|
2007-09-11 22:43:03 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
case WCMD_CALL:
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_call (parms_start);
|
2008-12-31 00:56:53 +01:00
|
|
|
break;
|
|
|
|
case WCMD_CD:
|
|
|
|
case WCMD_CHDIR:
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_setshow_default (parms_start);
|
2008-12-31 00:56:53 +01:00
|
|
|
break;
|
|
|
|
case WCMD_CLS:
|
|
|
|
WCMD_clear_screen ();
|
|
|
|
break;
|
|
|
|
case WCMD_COPY:
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_copy (parms_start);
|
2008-12-31 00:56:53 +01:00
|
|
|
break;
|
|
|
|
case WCMD_CTTY:
|
|
|
|
WCMD_change_tty ();
|
|
|
|
break;
|
|
|
|
case WCMD_DATE:
|
|
|
|
WCMD_setshow_date ();
|
|
|
|
break;
|
|
|
|
case WCMD_DEL:
|
|
|
|
case WCMD_ERASE:
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_delete (parms_start);
|
2008-12-31 00:56:53 +01:00
|
|
|
break;
|
|
|
|
case WCMD_DIR:
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_directory (parms_start);
|
2008-12-31 00:56:53 +01:00
|
|
|
break;
|
|
|
|
case WCMD_ECHO:
|
|
|
|
WCMD_echo(&whichcmd[count]);
|
|
|
|
break;
|
|
|
|
case WCMD_GOTO:
|
|
|
|
WCMD_goto (cmdList);
|
|
|
|
break;
|
|
|
|
case WCMD_HELP:
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_give_help (parms_start);
|
|
|
|
break;
|
2008-12-31 00:56:53 +01:00
|
|
|
case WCMD_LABEL:
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_volume (TRUE, parms_start);
|
2008-12-31 00:56:53 +01:00
|
|
|
break;
|
|
|
|
case WCMD_MD:
|
|
|
|
case WCMD_MKDIR:
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_create_dir (parms_start);
|
|
|
|
break;
|
2008-12-31 00:56:53 +01:00
|
|
|
case WCMD_MOVE:
|
|
|
|
WCMD_move ();
|
|
|
|
break;
|
|
|
|
case WCMD_PATH:
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_setshow_path (parms_start);
|
2008-12-31 00:56:53 +01: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:
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_remove_dir (parms_start);
|
2008-12-31 00:56:53 +01:00
|
|
|
break;
|
|
|
|
case WCMD_SETLOCAL:
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_setlocal(parms_start);
|
2008-12-31 00:56:53 +01:00
|
|
|
break;
|
|
|
|
case WCMD_ENDLOCAL:
|
|
|
|
WCMD_endlocal();
|
|
|
|
break;
|
|
|
|
case WCMD_SET:
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_setshow_env (parms_start);
|
|
|
|
break;
|
2008-12-31 00:56:53 +01:00
|
|
|
case WCMD_SHIFT:
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_shift (parms_start);
|
2008-12-31 00:56:53 +01:00
|
|
|
break;
|
2012-02-11 16:05:59 +01:00
|
|
|
case WCMD_START:
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_start (parms_start);
|
2012-02-11 16:05:59 +01:00
|
|
|
break;
|
2008-12-31 00:56:53 +01:00
|
|
|
case WCMD_TIME:
|
|
|
|
WCMD_setshow_time ();
|
|
|
|
break;
|
|
|
|
case WCMD_TITLE:
|
|
|
|
if (strlenW(&whichcmd[count]) > 0)
|
|
|
|
WCMD_title(&whichcmd[count+1]);
|
|
|
|
break;
|
|
|
|
case WCMD_TYPE:
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_type (parms_start);
|
|
|
|
break;
|
2008-12-31 00:56:53 +01:00
|
|
|
case WCMD_VER:
|
2012-02-11 18:24:56 +01:00
|
|
|
WCMD_output_asis(newlineW);
|
2008-12-31 00:56:53 +01:00
|
|
|
WCMD_version ();
|
|
|
|
break;
|
|
|
|
case WCMD_VERIFY:
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_verify (parms_start);
|
2008-12-31 00:56:53 +01:00
|
|
|
break;
|
|
|
|
case WCMD_VOL:
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_volume (FALSE, parms_start);
|
2008-12-31 00:56:53 +01:00
|
|
|
break;
|
|
|
|
case WCMD_PUSHD:
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_pushd(parms_start);
|
2008-12-31 00:56:53 +01:00
|
|
|
break;
|
|
|
|
case WCMD_POPD:
|
|
|
|
WCMD_popd();
|
|
|
|
break;
|
|
|
|
case WCMD_ASSOC:
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_assoc(parms_start, TRUE);
|
2008-12-31 00:56:53 +01:00
|
|
|
break;
|
|
|
|
case WCMD_COLOR:
|
|
|
|
WCMD_color();
|
|
|
|
break;
|
|
|
|
case WCMD_FTYPE:
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_assoc(parms_start, FALSE);
|
2008-12-31 00:56:53 +01:00
|
|
|
break;
|
|
|
|
case WCMD_MORE:
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_more(parms_start);
|
2008-12-31 00:56:53 +01:00
|
|
|
break;
|
2010-12-19 23:13:32 +01:00
|
|
|
case WCMD_CHOICE:
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_choice(parms_start);
|
2010-12-19 23:13:32 +01:00
|
|
|
break;
|
2017-11-08 00:26:42 +01:00
|
|
|
case WCMD_MKLINK:
|
2018-08-27 21:41:50 +02:00
|
|
|
WCMD_mklink(parms_start);
|
2017-11-08 00:26:42 +01:00
|
|
|
break;
|
2008-12-31 00:56:53 +01:00
|
|
|
case WCMD_EXIT:
|
|
|
|
WCMD_exit (cmdList);
|
|
|
|
break;
|
2012-10-14 01:38:53 +02:00
|
|
|
case WCMD_FOR:
|
|
|
|
case WCMD_IF:
|
|
|
|
/* Very oddly, probably because of all the special parsing required for
|
2014-06-29 13:12:33 +02:00
|
|
|
these two commands, neither 'for' nor 'if' is supported when called,
|
|
|
|
i.e. 'call if 1==1...' will fail. */
|
2012-10-14 01:38:53 +02:00
|
|
|
if (!retrycall) {
|
2018-08-27 21:41:50 +02:00
|
|
|
if (i==WCMD_FOR) WCMD_for (parms_start, cmdList);
|
|
|
|
else if (i==WCMD_IF) WCMD_if (parms_start, cmdList);
|
2012-10-14 01:38:53 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* else: drop through */
|
2008-12-31 00:56:53 +01:00
|
|
|
default:
|
2011-09-09 01:48:04 +02:00
|
|
|
prev_echo_mode = echo_mode;
|
2012-03-29 18:05:59 +02:00
|
|
|
WCMD_run_program (whichcmd, FALSE);
|
2011-09-09 01:48:04 +02:00
|
|
|
echo_mode = prev_echo_mode;
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(cmd);
|
|
|
|
heap_free(new_redir);
|
2007-06-03 23:07:44 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Restore old handles */
|
|
|
|
for (i=0; i<3; i++) {
|
2009-02-09 21:47:54 +01:00
|
|
|
if (old_stdhandles[i] != GetStdHandle(idx_stdhandles[i])) {
|
2008-12-31 00:56:53 +01:00
|
|
|
CloseHandle (GetStdHandle (idx_stdhandles[i]));
|
|
|
|
SetStdHandle (idx_stdhandles[i], old_stdhandles[i]);
|
2007-09-11 22:43:03 +02:00
|
|
|
}
|
2007-02-23 23:22:57 +01:00
|
|
|
}
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
2011-09-09 01:48:04 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* WCMD_LoadMessage
|
|
|
|
* Load a string from the resource file, handling any error
|
|
|
|
* Returns string retrieved from resource file
|
|
|
|
*/
|
|
|
|
WCHAR *WCMD_LoadMessage(UINT id) {
|
|
|
|
static WCHAR msg[2048];
|
|
|
|
static const WCHAR failedMsg[] = {'F','a','i','l','e','d','!','\0'};
|
2007-09-11 22:43:03 +02:00
|
|
|
|
2018-08-20 21:15:42 +02:00
|
|
|
if (!LoadStringW(GetModuleHandleW(NULL), id, msg, ARRAY_SIZE(msg))) {
|
2008-12-31 00:56:53 +01:00
|
|
|
WINE_FIXME("LoadString failed with %d\n", GetLastError());
|
|
|
|
strcpyW(msg, failedMsg);
|
2007-09-11 22:43:03 +02:00
|
|
|
}
|
2008-12-31 00:56:53 +01:00
|
|
|
return msg;
|
|
|
|
}
|
2007-09-11 22:43:03 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/***************************************************************************
|
|
|
|
* WCMD_DumpCommands
|
|
|
|
*
|
|
|
|
* Dumps out the parsed command line to ensure syntax is correct
|
|
|
|
*/
|
|
|
|
static void WCMD_DumpCommands(CMD_LIST *commands) {
|
|
|
|
CMD_LIST *thisCmd = commands;
|
2007-02-23 23:22:57 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
WINE_TRACE("Parsed line:\n");
|
|
|
|
while (thisCmd != NULL) {
|
2009-03-02 19:45:50 +01:00
|
|
|
WINE_TRACE("%p %d %2.2d %p %s Redir:%s\n",
|
2008-12-31 00:56:53 +01:00
|
|
|
thisCmd,
|
|
|
|
thisCmd->prevDelim,
|
|
|
|
thisCmd->bracketDepth,
|
|
|
|
thisCmd->nextcommand,
|
2009-03-02 19:45:50 +01:00
|
|
|
wine_dbgstr_w(thisCmd->command),
|
|
|
|
wine_dbgstr_w(thisCmd->redirects));
|
2008-12-31 00:56:53 +01:00
|
|
|
thisCmd = thisCmd->nextcommand;
|
2007-02-23 23:22:57 +01:00
|
|
|
}
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
2007-02-23 23:22:57 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/***************************************************************************
|
|
|
|
* WCMD_addCommand
|
|
|
|
*
|
|
|
|
* Adds a command to the current command list
|
|
|
|
*/
|
2009-01-05 19:58:16 +01:00
|
|
|
static void WCMD_addCommand(WCHAR *command, int *commandLen,
|
2008-12-31 00:56:53 +01:00
|
|
|
WCHAR *redirs, int *redirLen,
|
|
|
|
WCHAR **copyTo, int **copyToLen,
|
|
|
|
CMD_DELIMITERS prevDelim, int curDepth,
|
|
|
|
CMD_LIST **lastEntry, CMD_LIST **output) {
|
2007-02-27 00:03:34 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
CMD_LIST *thisEntry = NULL;
|
2007-02-27 00:03:34 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Allocate storage for command */
|
2012-11-15 12:32:21 +01:00
|
|
|
thisEntry = heap_alloc(sizeof(CMD_LIST));
|
2007-02-27 00:03:34 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Copy in the command */
|
|
|
|
if (command) {
|
2012-11-15 12:32:21 +01:00
|
|
|
thisEntry->command = heap_alloc((*commandLen+1) * sizeof(WCHAR));
|
2008-12-31 00:56:53 +01:00
|
|
|
memcpy(thisEntry->command, command, *commandLen * sizeof(WCHAR));
|
|
|
|
thisEntry->command[*commandLen] = 0x00;
|
2007-02-27 00:03:34 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Copy in the redirects */
|
2012-11-15 12:32:21 +01:00
|
|
|
thisEntry->redirects = heap_alloc((*redirLen+1) * sizeof(WCHAR));
|
2008-12-31 00:56:53 +01:00
|
|
|
memcpy(thisEntry->redirects, redirs, *redirLen * sizeof(WCHAR));
|
|
|
|
thisEntry->redirects[*redirLen] = 0x00;
|
|
|
|
thisEntry->pipeFile[0] = 0x00;
|
2007-02-27 00:03:34 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Reset the lengths */
|
|
|
|
*commandLen = 0;
|
|
|
|
*redirLen = 0;
|
|
|
|
*copyToLen = commandLen;
|
|
|
|
*copyTo = command;
|
2007-09-11 22:43:03 +02:00
|
|
|
|
2007-02-27 00:03:34 +01:00
|
|
|
} else {
|
2008-12-31 00:56:53 +01:00
|
|
|
thisEntry->command = NULL;
|
2010-03-07 22:37:23 +01:00
|
|
|
thisEntry->redirects = NULL;
|
|
|
|
thisEntry->pipeFile[0] = 0x00;
|
2007-02-27 00:03:34 +01:00
|
|
|
}
|
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Fill in other fields */
|
|
|
|
thisEntry->nextcommand = NULL;
|
|
|
|
thisEntry->prevDelim = prevDelim;
|
|
|
|
thisEntry->bracketDepth = curDepth;
|
|
|
|
if (*lastEntry) {
|
|
|
|
(*lastEntry)->nextcommand = thisEntry;
|
|
|
|
} else {
|
|
|
|
*output = thisEntry;
|
|
|
|
}
|
|
|
|
*lastEntry = thisEntry;
|
|
|
|
}
|
2007-07-12 09:35:00 +02:00
|
|
|
|
2009-10-04 04:28:26 +02:00
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
* WCMD_IsEndQuote
|
|
|
|
*
|
2011-07-27 02:21:01 +02:00
|
|
|
* Checks if the quote pointed to is the end-quote.
|
2009-10-04 04:28:26 +02:00
|
|
|
*
|
|
|
|
* Quotes end if:
|
|
|
|
*
|
|
|
|
* 1) The current parameter ends at EOL or at the beginning
|
|
|
|
* of a redirection or pipe and not in a quote section.
|
|
|
|
*
|
|
|
|
* 2) If the next character is a space and not in a quote section.
|
|
|
|
*
|
|
|
|
* Returns TRUE if this is an end quote, and FALSE if it is not.
|
|
|
|
*
|
|
|
|
*/
|
2011-08-20 01:58:59 +02:00
|
|
|
static BOOL WCMD_IsEndQuote(const WCHAR *quote, int quoteIndex)
|
2009-10-04 04:28:26 +02:00
|
|
|
{
|
|
|
|
int quoteCount = quoteIndex;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* If we are not in a quoted section, then we are not an end-quote */
|
|
|
|
if(quoteIndex == 0)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check how many quotes are left for this parameter */
|
|
|
|
for(i=0;quote[i];i++)
|
|
|
|
{
|
|
|
|
if(quote[i] == '"')
|
|
|
|
{
|
|
|
|
quoteCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Quote counting ends at EOL, redirection, space or pipe if current quote is complete */
|
|
|
|
else if(((quoteCount % 2) == 0)
|
|
|
|
&& ((quote[i] == '<') || (quote[i] == '>') || (quote[i] == '|') || (quote[i] == ' ')))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the quote is part of the last part of a series of quotes-on-quotes, then it must
|
|
|
|
be an end-quote */
|
|
|
|
if(quoteIndex >= (quoteCount / 2))
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No cigar */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/***************************************************************************
|
|
|
|
* WCMD_ReadAndParseLine
|
|
|
|
*
|
|
|
|
* Either uses supplied input or
|
|
|
|
* Reads a file from the handle, and then...
|
2011-07-27 02:21:01 +02:00
|
|
|
* Parse the text buffer, splitting into separate commands
|
2008-12-31 00:56:53 +01:00
|
|
|
* - 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)
|
|
|
|
*/
|
2011-10-06 18:50:31 +02:00
|
|
|
WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE readFrom)
|
2011-09-28 14:41:21 +02:00
|
|
|
{
|
2008-12-31 00:56:53 +01:00
|
|
|
WCHAR *curPos;
|
2009-01-11 23:47:53 +01:00
|
|
|
int inQuotes = 0;
|
2008-12-31 00:56:53 +01:00
|
|
|
WCHAR curString[MAXSTRING];
|
|
|
|
int curStringLen = 0;
|
|
|
|
WCHAR curRedirs[MAXSTRING];
|
|
|
|
int curRedirsLen = 0;
|
|
|
|
WCHAR *curCopyTo;
|
|
|
|
int *curLen;
|
|
|
|
int curDepth = 0;
|
|
|
|
CMD_LIST *lastEntry = NULL;
|
|
|
|
CMD_DELIMITERS prevDelim = CMD_NONE;
|
|
|
|
static WCHAR *extraSpace = NULL; /* Deliberately never freed */
|
2011-08-25 23:08:30 +02:00
|
|
|
static const WCHAR remCmd[] = {'r','e','m'};
|
|
|
|
static const WCHAR forCmd[] = {'f','o','r'};
|
|
|
|
static const WCHAR ifCmd[] = {'i','f'};
|
|
|
|
static const WCHAR ifElse[] = {'e','l','s','e'};
|
2013-06-02 01:17:30 +02:00
|
|
|
BOOL inOneLine = FALSE;
|
2008-12-31 00:56:53 +01:00
|
|
|
BOOL inFor = FALSE;
|
|
|
|
BOOL inIn = FALSE;
|
|
|
|
BOOL inIf = FALSE;
|
|
|
|
BOOL inElse= FALSE;
|
|
|
|
BOOL onlyWhiteSpace = FALSE;
|
|
|
|
BOOL lastWasWhiteSpace = FALSE;
|
|
|
|
BOOL lastWasDo = FALSE;
|
|
|
|
BOOL lastWasIn = FALSE;
|
|
|
|
BOOL lastWasElse = FALSE;
|
|
|
|
BOOL lastWasRedirect = TRUE;
|
2012-09-25 22:02:09 +02:00
|
|
|
BOOL lastWasCaret = FALSE;
|
2018-08-27 21:41:50 +02:00
|
|
|
int lineCurDepth; /* Bracket depth when line was read in */
|
|
|
|
BOOL resetAtEndOfLine = FALSE; /* Do we need to reset curdepth at EOL */
|
2007-02-23 23:22:57 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Allocate working space for a command read from keyboard, file etc */
|
|
|
|
if (!extraSpace)
|
2012-11-15 12:32:21 +01:00
|
|
|
extraSpace = heap_alloc((MAXSTRING+1) * sizeof(WCHAR));
|
2008-12-31 00:56:53 +01:00
|
|
|
if (!extraSpace)
|
|
|
|
{
|
|
|
|
WINE_ERR("Could not allocate memory for extraSpace\n");
|
|
|
|
return NULL;
|
2007-02-23 23:22:57 +01:00
|
|
|
}
|
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* 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 {
|
2011-10-06 18:50:31 +02:00
|
|
|
if (!WCMD_fgets(extraSpace, MAXSTRING, readFrom))
|
2011-09-28 14:41:21 +02:00
|
|
|
return NULL;
|
2007-02-23 23:22:57 +01:00
|
|
|
}
|
2008-12-31 00:56:53 +01:00
|
|
|
curPos = extraSpace;
|
2007-02-23 23:22:57 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Handle truncated input - issue warning */
|
|
|
|
if (strlenW(extraSpace) == MAXSTRING -1) {
|
2011-10-01 04:15:05 +02:00
|
|
|
WCMD_output_asis_stderr(WCMD_LoadMessage(WCMD_TRUNCATEDLINE));
|
|
|
|
WCMD_output_asis_stderr(extraSpace);
|
2012-02-11 18:24:56 +01:00
|
|
|
WCMD_output_asis_stderr(newlineW);
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
2007-02-23 23:22:57 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Replace env vars if in a batch context */
|
2013-01-07 00:06:28 +01:00
|
|
|
if (context) handleExpansion(extraSpace, FALSE, FALSE);
|
2012-09-25 23:05:03 +02:00
|
|
|
|
2012-10-01 03:30:06 +02:00
|
|
|
/* Skip preceding whitespace */
|
2012-09-25 23:05:03 +02:00
|
|
|
while (*curPos == ' ' || *curPos == '\t') curPos++;
|
|
|
|
|
2010-01-03 02:04:05 +01:00
|
|
|
/* Show prompt before batch line IF echo is on and in batch program */
|
2012-09-25 23:05:03 +02:00
|
|
|
if (context && echo_mode && *curPos && (*curPos != '@')) {
|
2011-08-25 23:08:30 +02:00
|
|
|
static const WCHAR echoDot[] = {'e','c','h','o','.'};
|
|
|
|
static const WCHAR echoCol[] = {'e','c','h','o',':'};
|
2015-08-18 15:54:43 +02:00
|
|
|
static const WCHAR echoSlash[] = {'e','c','h','o','/'};
|
2018-08-20 21:15:42 +02:00
|
|
|
const DWORD len = ARRAY_SIZE(echoDot);
|
2012-09-25 23:05:03 +02:00
|
|
|
DWORD curr_size = strlenW(curPos);
|
2011-08-05 16:29:21 +02:00
|
|
|
DWORD min_len = (curr_size < len ? curr_size : len);
|
2018-09-02 00:11:23 +02:00
|
|
|
WCMD_show_prompt(TRUE);
|
2012-09-25 23:05:03 +02:00
|
|
|
WCMD_output_asis(curPos);
|
2010-11-05 22:15:45 +01:00
|
|
|
/* I don't know why Windows puts a space here but it does */
|
2015-08-18 15:54:43 +02:00
|
|
|
/* Except for lines starting with 'echo.', 'echo:' or 'echo/'. Ask MS why */
|
2011-08-05 16:29:21 +02:00
|
|
|
if (CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
|
2012-09-25 23:05:03 +02:00
|
|
|
curPos, min_len, echoDot, len) != CSTR_EQUAL
|
2011-08-05 16:29:21 +02:00
|
|
|
&& CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
|
2015-08-18 15:54:43 +02:00
|
|
|
curPos, min_len, echoCol, len) != CSTR_EQUAL
|
|
|
|
&& CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
|
|
|
|
curPos, min_len, echoSlash, len) != CSTR_EQUAL)
|
2011-08-05 16:29:21 +02:00
|
|
|
{
|
2012-02-11 18:24:56 +01:00
|
|
|
WCMD_output_asis(spaceW);
|
2011-08-05 16:29:21 +02:00
|
|
|
}
|
2012-02-11 18:24:56 +01:00
|
|
|
WCMD_output_asis(newlineW);
|
2010-01-03 02:04:05 +01:00
|
|
|
}
|
2007-02-23 23:22:57 +01:00
|
|
|
|
2012-09-25 23:05:03 +02:00
|
|
|
/* Skip repeated 'no echo' characters */
|
|
|
|
while (*curPos == '@') curPos++;
|
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Start with an empty string, copying to the command string */
|
|
|
|
curStringLen = 0;
|
|
|
|
curRedirsLen = 0;
|
|
|
|
curCopyTo = curString;
|
|
|
|
curLen = &curStringLen;
|
2012-09-28 11:13:15 +02:00
|
|
|
lastWasRedirect = FALSE; /* Required e.g. for spaces between > and filename */
|
2018-08-27 21:41:50 +02:00
|
|
|
lineCurDepth = curDepth; /* What was the curdepth at the beginning of the line */
|
2007-02-23 23:22:57 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Parse every character on the line being processed */
|
|
|
|
while (*curPos != 0x00) {
|
2007-02-23 23:22:57 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
WCHAR thisChar;
|
2007-02-23 23:22:57 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Debugging AID:
|
|
|
|
WINE_TRACE("Looking at '%c' (len:%d, lws:%d, ows:%d)\n", *curPos, *curLen,
|
|
|
|
lastWasWhiteSpace, onlyWhiteSpace);
|
|
|
|
*/
|
2007-02-23 23:22:57 +01:00
|
|
|
|
2012-09-28 11:13:15 +02:00
|
|
|
/* Prevent overflow caused by the caret escape char */
|
2012-09-25 22:02:09 +02:00
|
|
|
if (*curLen >= MAXSTRING) {
|
|
|
|
WINE_ERR("Overflow detected in command\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Certain commands need special handling */
|
|
|
|
if (curStringLen == 0 && curCopyTo == curString) {
|
2011-08-25 23:08:30 +02:00
|
|
|
static const WCHAR forDO[] = {'d','o'};
|
2007-02-23 23:22:57 +01:00
|
|
|
|
2013-06-02 01:17:30 +02:00
|
|
|
/* If command starts with 'rem ' or identifies a label, ignore any &&, ( etc. */
|
2018-08-20 21:15:42 +02:00
|
|
|
if (WCMD_keyword_ws_found(remCmd, ARRAY_SIZE(remCmd), curPos) || *curPos == ':') {
|
2013-06-02 01:17:30 +02:00
|
|
|
inOneLine = TRUE;
|
2007-02-23 23:22:57 +01:00
|
|
|
|
2018-08-20 21:15:42 +02:00
|
|
|
} else if (WCMD_keyword_ws_found(forCmd, ARRAY_SIZE(forCmd), curPos)) {
|
2008-12-31 00:56:53 +01:00
|
|
|
inFor = TRUE;
|
2007-02-23 23:22:57 +01:00
|
|
|
|
2011-08-25 00:48:22 +02:00
|
|
|
/* If command starts with 'if ' or 'else ', handle ('s mid line. We should ensure this
|
2008-12-31 00:56:53 +01:00
|
|
|
is only true in the command portion of the IF statement, but this
|
|
|
|
should suffice for now
|
|
|
|
FIXME: Silly syntax like "if 1(==1( (
|
|
|
|
echo they equal
|
|
|
|
)" will be parsed wrong */
|
2018-08-20 21:15:42 +02:00
|
|
|
} else if (WCMD_keyword_ws_found(ifCmd, ARRAY_SIZE(ifCmd), curPos)) {
|
2008-12-31 00:56:53 +01:00
|
|
|
inIf = TRUE;
|
2007-02-23 23:22:57 +01:00
|
|
|
|
2018-08-20 21:15:42 +02:00
|
|
|
} else if (WCMD_keyword_ws_found(ifElse, ARRAY_SIZE(ifElse), curPos)) {
|
|
|
|
const int keyw_len = ARRAY_SIZE(ifElse) + 1;
|
2008-12-31 00:56:53 +01:00
|
|
|
inElse = TRUE;
|
|
|
|
lastWasElse = TRUE;
|
|
|
|
onlyWhiteSpace = TRUE;
|
2011-08-25 00:48:22 +02:00
|
|
|
memcpy(&curCopyTo[*curLen], curPos, keyw_len*sizeof(WCHAR));
|
|
|
|
(*curLen)+=keyw_len;
|
|
|
|
curPos+=keyw_len;
|
2018-08-27 21:41:50 +02:00
|
|
|
|
|
|
|
/* If we had a single line if XXX which reaches an else (needs odd
|
|
|
|
syntax like if 1=1 command && (command) else command we pretended
|
|
|
|
to add brackets for the if, so they are now over */
|
|
|
|
if (resetAtEndOfLine) {
|
|
|
|
WINE_TRACE("Resetting curdepth at end of line to %d\n", lineCurDepth);
|
|
|
|
resetAtEndOfLine = FALSE;
|
|
|
|
curDepth = lineCurDepth;
|
|
|
|
}
|
2008-12-31 00:56:53 +01:00
|
|
|
continue;
|
2007-02-23 23:22:57 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* In a for loop, the DO command will follow a close bracket followed by
|
|
|
|
whitespace, followed by DO, ie closeBracket inserts a NULL entry, curLen
|
|
|
|
is then 0, and all whitespace is skipped */
|
2018-08-20 21:15:42 +02:00
|
|
|
} else if (inFor && WCMD_keyword_ws_found(forDO, ARRAY_SIZE(forDO), curPos)) {
|
|
|
|
const int keyw_len = ARRAY_SIZE(forDO) + 1;
|
2011-08-25 00:48:18 +02:00
|
|
|
WINE_TRACE("Found 'DO '\n");
|
2008-12-31 00:56:53 +01:00
|
|
|
lastWasDo = TRUE;
|
|
|
|
onlyWhiteSpace = TRUE;
|
2011-08-25 00:48:18 +02:00
|
|
|
memcpy(&curCopyTo[*curLen], curPos, keyw_len*sizeof(WCHAR));
|
|
|
|
(*curLen)+=keyw_len;
|
|
|
|
curPos+=keyw_len;
|
2008-12-31 00:56:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if (curCopyTo == curString) {
|
2007-02-23 23:22:57 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Special handling for the 'FOR' command */
|
|
|
|
if (inFor && lastWasWhiteSpace) {
|
2011-08-25 23:08:30 +02:00
|
|
|
static const WCHAR forIN[] = {'i','n'};
|
2007-02-23 23:22:57 +01:00
|
|
|
|
2011-08-25 00:48:18 +02:00
|
|
|
WINE_TRACE("Found 'FOR ', comparing next parm: '%s'\n", wine_dbgstr_w(curPos));
|
2007-02-23 23:22:57 +01:00
|
|
|
|
2018-08-20 21:15:42 +02:00
|
|
|
if (WCMD_keyword_ws_found(forIN, ARRAY_SIZE(forIN), curPos)) {
|
|
|
|
const int keyw_len = ARRAY_SIZE(forIN) + 1;
|
2011-08-25 00:48:18 +02:00
|
|
|
WINE_TRACE("Found 'IN '\n");
|
2008-12-31 00:56:53 +01:00
|
|
|
lastWasIn = TRUE;
|
|
|
|
onlyWhiteSpace = TRUE;
|
2011-08-25 00:48:18 +02:00
|
|
|
memcpy(&curCopyTo[*curLen], curPos, keyw_len*sizeof(WCHAR));
|
|
|
|
(*curLen)+=keyw_len;
|
|
|
|
curPos+=keyw_len;
|
2008-12-31 00:56:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-02-23 23:22:57 +01:00
|
|
|
|
2013-06-02 01:17:30 +02:00
|
|
|
/* Nothing 'ends' a one line statement (e.g. REM or :labels mean
|
|
|
|
the &&, quotes and redirection etc are ineffective, so just force
|
|
|
|
the use of the default processing by skipping character specific
|
|
|
|
matching below) */
|
|
|
|
if (!inOneLine) thisChar = *curPos;
|
|
|
|
else thisChar = 'X'; /* Character with no special processing */
|
2007-02-23 23:22:57 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
lastWasWhiteSpace = FALSE; /* Will be reset below */
|
2012-09-25 22:02:09 +02:00
|
|
|
lastWasCaret = FALSE;
|
2007-02-23 23:22:57 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
switch (thisChar) {
|
2007-02-23 23:22:57 +01:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
case '=': /* drop through - ignore token delimiters at the start of a command */
|
|
|
|
case ',': /* drop through - ignore token delimiters at the start of a command */
|
|
|
|
case '\t':/* drop through - ignore token delimiters at the start of a command */
|
|
|
|
case ' ':
|
|
|
|
/* If a redirect in place, it ends here */
|
|
|
|
if (!inQuotes && !lastWasRedirect) {
|
2007-06-03 23:07:39 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* If finishing off a redirect, add a whitespace delimiter */
|
|
|
|
if (curCopyTo == curRedirs) {
|
|
|
|
curCopyTo[(*curLen)++] = ' ';
|
|
|
|
}
|
|
|
|
curCopyTo = curString;
|
|
|
|
curLen = &curStringLen;
|
|
|
|
}
|
|
|
|
if (*curLen > 0) {
|
|
|
|
curCopyTo[(*curLen)++] = *curPos;
|
|
|
|
}
|
2007-06-03 23:07:39 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Remember just processed whitespace */
|
|
|
|
lastWasWhiteSpace = TRUE;
|
2007-06-03 23:07:42 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
break;
|
2007-06-03 23:07:42 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
case '>': /* drop through - handle redirect chars the same */
|
|
|
|
case '<':
|
|
|
|
/* Make a redirect start here */
|
|
|
|
if (!inQuotes) {
|
|
|
|
curCopyTo = curRedirs;
|
|
|
|
curLen = &curRedirsLen;
|
|
|
|
lastWasRedirect = TRUE;
|
|
|
|
}
|
2007-06-03 23:07:42 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* See if 1>, 2> etc, in which case we have some patching up
|
2011-08-27 02:11:30 +02:00
|
|
|
to do (provided there's a preceding whitespace, and enough
|
|
|
|
chars read so far) */
|
|
|
|
if (curStringLen > 2
|
|
|
|
&& (*(curPos-1)>='1') && (*(curPos-1)<='9')
|
|
|
|
&& ((*(curPos-2)==' ') || (*(curPos-2)=='\t'))) {
|
2008-12-31 00:56:53 +01:00
|
|
|
curStringLen--;
|
|
|
|
curString[curStringLen] = 0x00;
|
|
|
|
curCopyTo[(*curLen)++] = *(curPos-1);
|
|
|
|
}
|
2007-06-03 23:07:42 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
curCopyTo[(*curLen)++] = *curPos;
|
2009-01-11 21:08:50 +01:00
|
|
|
|
|
|
|
/* If a redirect is immediately followed by '&' (ie. 2>&1) then
|
|
|
|
do not process that ampersand as an AND operator */
|
|
|
|
if (thisChar == '>' && *(curPos+1) == '&') {
|
|
|
|
curCopyTo[(*curLen)++] = *(curPos+1);
|
|
|
|
curPos++;
|
|
|
|
}
|
2008-12-31 00:56:53 +01:00
|
|
|
break;
|
2007-06-03 23:07:42 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
case '|': /* Pipe character only if not || */
|
|
|
|
if (!inQuotes) {
|
|
|
|
lastWasRedirect = FALSE;
|
2007-06-03 23:07:42 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Add an entry to the command list */
|
|
|
|
if (curStringLen > 0) {
|
2007-06-03 23:07:42 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Add the current command */
|
|
|
|
WCMD_addCommand(curString, &curStringLen,
|
|
|
|
curRedirs, &curRedirsLen,
|
|
|
|
&curCopyTo, &curLen,
|
|
|
|
prevDelim, curDepth,
|
|
|
|
&lastEntry, output);
|
2007-06-03 23:07:42 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
2007-06-15 21:59:19 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
if (*(curPos+1) == '|') {
|
|
|
|
curPos++; /* Skip other | */
|
|
|
|
prevDelim = CMD_ONFAILURE;
|
|
|
|
} else {
|
|
|
|
prevDelim = CMD_PIPE;
|
|
|
|
}
|
2018-08-27 21:41:50 +02:00
|
|
|
|
|
|
|
/* If in an IF or ELSE statement, put subsequent chained
|
|
|
|
commands at a higher depth as if brackets were supplied
|
|
|
|
but remember to reset to the original depth at EOL */
|
|
|
|
if ((inIf || inElse) && curDepth == lineCurDepth) {
|
|
|
|
curDepth++;
|
|
|
|
resetAtEndOfLine = TRUE;
|
|
|
|
}
|
2008-12-31 00:56:53 +01:00
|
|
|
} else {
|
|
|
|
curCopyTo[(*curLen)++] = *curPos;
|
|
|
|
}
|
|
|
|
break;
|
2007-06-15 21:59:19 +02:00
|
|
|
|
2009-10-04 04:28:26 +02:00
|
|
|
case '"': if (WCMD_IsEndQuote(curPos, inQuotes)) {
|
|
|
|
inQuotes--;
|
2009-01-11 23:47:53 +01:00
|
|
|
} else {
|
|
|
|
inQuotes++; /* Quotes within quotes are fun! */
|
|
|
|
}
|
2008-12-31 00:56:53 +01:00
|
|
|
curCopyTo[(*curLen)++] = *curPos;
|
|
|
|
lastWasRedirect = FALSE;
|
|
|
|
break;
|
2007-06-15 21:59:19 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
case '(': /* If a '(' is the first non whitespace in a command portion
|
|
|
|
ie start of line or just after &&, then we read until an
|
|
|
|
unquoted ) is found */
|
|
|
|
WINE_TRACE("Found '(' conditions: curLen(%d), inQ(%d), onlyWS(%d)"
|
|
|
|
", for(%d, In:%d, Do:%d)"
|
|
|
|
", if(%d, else:%d, lwe:%d)\n",
|
|
|
|
*curLen, inQuotes,
|
|
|
|
onlyWhiteSpace,
|
|
|
|
inFor, lastWasIn, lastWasDo,
|
|
|
|
inIf, inElse, lastWasElse);
|
|
|
|
lastWasRedirect = FALSE;
|
2007-09-11 22:43:08 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Ignore open brackets inside the for set */
|
|
|
|
if (*curLen == 0 && !inIn) {
|
|
|
|
curDepth++;
|
2007-09-11 22:43:08 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* If in quotes, ignore brackets */
|
|
|
|
} else if (inQuotes) {
|
|
|
|
curCopyTo[(*curLen)++] = *curPos;
|
2007-09-11 22:43:08 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* In a FOR loop, an unquoted '(' may occur straight after
|
|
|
|
IN or DO
|
|
|
|
In an IF statement just handle it regardless as we don't
|
|
|
|
parse the operands
|
|
|
|
In an ELSE statement, only allow it straight away after
|
|
|
|
the ELSE and whitespace
|
|
|
|
*/
|
|
|
|
} else if (inIf ||
|
|
|
|
(inElse && lastWasElse && onlyWhiteSpace) ||
|
|
|
|
(inFor && (lastWasIn || lastWasDo) && onlyWhiteSpace)) {
|
2007-09-11 22:43:08 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* If entering into an 'IN', set inIn */
|
|
|
|
if (inFor && lastWasIn && onlyWhiteSpace) {
|
|
|
|
WINE_TRACE("Inside an IN\n");
|
|
|
|
inIn = TRUE;
|
|
|
|
}
|
2007-09-11 22:43:08 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Add the current command */
|
|
|
|
WCMD_addCommand(curString, &curStringLen,
|
|
|
|
curRedirs, &curRedirsLen,
|
|
|
|
&curCopyTo, &curLen,
|
|
|
|
prevDelim, curDepth,
|
|
|
|
&lastEntry, output);
|
2007-09-11 22:43:08 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
curDepth++;
|
|
|
|
} else {
|
|
|
|
curCopyTo[(*curLen)++] = *curPos;
|
|
|
|
}
|
|
|
|
break;
|
2007-09-11 22:43:08 +02:00
|
|
|
|
2012-09-25 22:02:09 +02:00
|
|
|
case '^': if (!inQuotes) {
|
|
|
|
/* If we reach the end of the input, we need to wait for more */
|
|
|
|
if (*(curPos+1) == 0x00) {
|
|
|
|
lastWasCaret = TRUE;
|
|
|
|
WINE_TRACE("Caret found at end of line\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
curPos++;
|
|
|
|
}
|
2012-09-25 14:45:28 +02:00
|
|
|
curCopyTo[(*curLen)++] = *curPos;
|
|
|
|
break;
|
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
case '&': if (!inQuotes) {
|
|
|
|
lastWasRedirect = FALSE;
|
2007-09-11 22:43:08 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Add an entry to the command list */
|
|
|
|
if (curStringLen > 0) {
|
2007-06-15 21:59:19 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Add the current command */
|
|
|
|
WCMD_addCommand(curString, &curStringLen,
|
|
|
|
curRedirs, &curRedirsLen,
|
|
|
|
&curCopyTo, &curLen,
|
|
|
|
prevDelim, curDepth,
|
|
|
|
&lastEntry, output);
|
2007-06-15 21:59:19 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
2007-06-15 21:59:19 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
if (*(curPos+1) == '&') {
|
|
|
|
curPos++; /* Skip other & */
|
|
|
|
prevDelim = CMD_ONSUCCESS;
|
|
|
|
} else {
|
|
|
|
prevDelim = CMD_NONE;
|
|
|
|
}
|
2018-08-27 21:41:50 +02:00
|
|
|
/* If in an IF or ELSE statement, put subsequent chained
|
|
|
|
commands at a higher depth as if brackets were supplied
|
|
|
|
but remember to reset to the original depth at EOL */
|
|
|
|
if ((inIf || inElse) && curDepth == lineCurDepth) {
|
|
|
|
curDepth++;
|
|
|
|
resetAtEndOfLine = TRUE;
|
|
|
|
}
|
2008-12-31 00:56:53 +01:00
|
|
|
} else {
|
|
|
|
curCopyTo[(*curLen)++] = *curPos;
|
|
|
|
}
|
|
|
|
break;
|
2007-06-15 21:59:19 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
case ')': if (!inQuotes && curDepth > 0) {
|
|
|
|
lastWasRedirect = FALSE;
|
2007-06-15 21:59:19 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Add the current command if there is one */
|
|
|
|
if (curStringLen) {
|
2007-09-11 22:43:03 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Add the current command */
|
|
|
|
WCMD_addCommand(curString, &curStringLen,
|
|
|
|
curRedirs, &curRedirsLen,
|
|
|
|
&curCopyTo, &curLen,
|
|
|
|
prevDelim, curDepth,
|
|
|
|
&lastEntry, output);
|
|
|
|
}
|
2007-06-15 21:59:19 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Add an empty entry to the command list */
|
|
|
|
prevDelim = CMD_NONE;
|
|
|
|
WCMD_addCommand(NULL, &curStringLen,
|
|
|
|
curRedirs, &curRedirsLen,
|
|
|
|
&curCopyTo, &curLen,
|
|
|
|
prevDelim, curDepth,
|
|
|
|
&lastEntry, output);
|
|
|
|
curDepth--;
|
2007-06-15 21:59:23 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Leave inIn if necessary */
|
|
|
|
if (inIn) inIn = FALSE;
|
|
|
|
} else {
|
|
|
|
curCopyTo[(*curLen)++] = *curPos;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
lastWasRedirect = FALSE;
|
|
|
|
curCopyTo[(*curLen)++] = *curPos;
|
|
|
|
}
|
2007-06-15 21:59:23 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
curPos++;
|
2007-06-15 21:59:25 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* At various times we need to know if we have only skipped whitespace,
|
|
|
|
so reset this variable and then it will remain true until a non
|
|
|
|
whitespace is found */
|
2011-08-23 22:54:50 +02:00
|
|
|
if ((thisChar != ' ') && (thisChar != '\t') && (thisChar != '\n'))
|
|
|
|
onlyWhiteSpace = FALSE;
|
2007-06-15 21:59:25 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Flag end of interest in FOR DO and IN parms once something has been processed */
|
|
|
|
if (!lastWasWhiteSpace) {
|
|
|
|
lastWasIn = lastWasDo = FALSE;
|
|
|
|
}
|
2007-06-15 21:59:25 +02:00
|
|
|
|
2012-09-25 22:02:09 +02:00
|
|
|
/* If we have reached the end, add this command into the list
|
|
|
|
Do not add command to list if escape char ^ was last */
|
|
|
|
if (*curPos == 0x00 && !lastWasCaret && *curLen > 0) {
|
2007-06-15 21:59:25 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Add an entry to the command list */
|
|
|
|
WCMD_addCommand(curString, &curStringLen,
|
|
|
|
curRedirs, &curRedirsLen,
|
|
|
|
&curCopyTo, &curLen,
|
|
|
|
prevDelim, curDepth,
|
|
|
|
&lastEntry, output);
|
2018-08-27 21:41:50 +02:00
|
|
|
|
|
|
|
/* If we had a single line if or else, and we pretended to add
|
|
|
|
brackets, end them now */
|
|
|
|
if (resetAtEndOfLine) {
|
|
|
|
WINE_TRACE("Resetting curdepth at end of line to %d\n", lineCurDepth);
|
|
|
|
resetAtEndOfLine = FALSE;
|
|
|
|
curDepth = lineCurDepth;
|
|
|
|
}
|
|
|
|
}
|
2007-06-15 21:59:26 +02:00
|
|
|
|
2012-09-25 22:02:09 +02:00
|
|
|
/* If we have reached the end of the string, see if bracketing or
|
|
|
|
final caret is outstanding */
|
|
|
|
if (*curPos == 0x00 && (curDepth > 0 || lastWasCaret) &&
|
|
|
|
readFrom != INVALID_HANDLE_VALUE) {
|
|
|
|
WCHAR *extraData;
|
|
|
|
|
|
|
|
WINE_TRACE("Need to read more data as outstanding brackets or carets\n");
|
2013-06-02 01:17:30 +02:00
|
|
|
inOneLine = FALSE;
|
2008-12-31 00:56:53 +01:00
|
|
|
prevDelim = CMD_NONE;
|
2009-01-11 23:47:53 +01:00
|
|
|
inQuotes = 0;
|
2008-12-31 00:56:53 +01:00
|
|
|
memset(extraSpace, 0x00, (MAXSTRING+1) * sizeof(WCHAR));
|
2012-09-25 22:02:09 +02:00
|
|
|
extraData = extraSpace;
|
2007-06-15 21:59:26 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Read more, skipping any blank lines */
|
2012-09-25 22:02:09 +02:00
|
|
|
do {
|
|
|
|
WINE_TRACE("Read more input\n");
|
2008-12-31 00:56:53 +01:00
|
|
|
if (!context) WCMD_output_asis( WCMD_LoadMessage(WCMD_MOREPROMPT));
|
2012-09-25 22:02:09 +02:00
|
|
|
if (!WCMD_fgets(extraData, MAXSTRING, readFrom))
|
2011-09-28 14:41:21 +02:00
|
|
|
break;
|
2012-09-25 22:02:09 +02:00
|
|
|
|
2012-09-28 11:13:15 +02:00
|
|
|
/* Edge case for carets - a completely blank line (i.e. was just
|
2012-09-25 22:02:09 +02:00
|
|
|
CRLF) is oddly added as an LF but then more data is received (but
|
|
|
|
only once more!) */
|
|
|
|
if (lastWasCaret) {
|
|
|
|
if (*extraSpace == 0x00) {
|
|
|
|
WINE_TRACE("Read nothing, so appending LF char and will try again\n");
|
|
|
|
*extraData++ = '\r';
|
|
|
|
*extraData = 0x00;
|
|
|
|
} else break;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (*extraData == 0x00);
|
2008-12-31 00:56:53 +01:00
|
|
|
curPos = extraSpace;
|
2013-01-07 00:06:28 +01:00
|
|
|
if (context) handleExpansion(extraSpace, FALSE, FALSE);
|
2010-01-03 02:04:05 +01:00
|
|
|
/* Continue to echo commands IF echo is on and in batch program */
|
|
|
|
if (context && echo_mode && extraSpace[0] && (extraSpace[0] != '@')) {
|
|
|
|
WCMD_output_asis(extraSpace);
|
2012-02-11 18:24:56 +01:00
|
|
|
WCMD_output_asis(newlineW);
|
2010-01-03 02:04:05 +01:00
|
|
|
}
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
|
|
|
}
|
2007-06-15 21:59:25 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Dump out the parsed output */
|
|
|
|
WCMD_DumpCommands(*output);
|
2007-06-15 21:59:25 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
return extraSpace;
|
|
|
|
}
|
2007-06-15 21:59:25 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/***************************************************************************
|
|
|
|
* WCMD_process_commands
|
|
|
|
*
|
|
|
|
* Process all the commands read in so far
|
|
|
|
*/
|
|
|
|
CMD_LIST *WCMD_process_commands(CMD_LIST *thisCmd, BOOL oneBracket,
|
2012-10-14 01:38:53 +02:00
|
|
|
BOOL retrycall) {
|
2007-06-15 21:59:23 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
int bdepth = -1;
|
2007-06-15 21:59:23 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
if (thisCmd && oneBracket) bdepth = thisCmd->bracketDepth;
|
2007-06-15 21:59:25 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Loop through the commands, processing them one by one */
|
|
|
|
while (thisCmd) {
|
2007-06-15 21:59:19 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
CMD_LIST *origCmd = thisCmd;
|
2007-09-11 22:43:08 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* If processing one bracket only, and we find the end bracket
|
|
|
|
entry (or less), return */
|
|
|
|
if (oneBracket && !thisCmd->command &&
|
|
|
|
bdepth <= thisCmd->bracketDepth) {
|
|
|
|
WINE_TRACE("Finished bracket @ %p, next command is %p\n",
|
|
|
|
thisCmd, thisCmd->nextcommand);
|
|
|
|
return thisCmd->nextcommand;
|
|
|
|
}
|
2007-06-15 21:59:25 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* 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));
|
2012-11-05 22:43:08 +01:00
|
|
|
WCMD_execute (thisCmd->command, thisCmd->redirects, &thisCmd, retrycall);
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
2007-06-15 21:59:25 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Step on unless the command itself already stepped on */
|
|
|
|
if (thisCmd == origCmd) thisCmd = thisCmd->nextcommand;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-06-15 21:59:19 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/***************************************************************************
|
|
|
|
* 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) {
|
2007-09-11 22:43:08 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Loop through the commands, freeing them one by one */
|
|
|
|
while (cmds) {
|
|
|
|
CMD_LIST *thisCmd = cmds;
|
|
|
|
cmds = cmds->nextcommand;
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(thisCmd->command);
|
|
|
|
heap_free(thisCmd->redirects);
|
|
|
|
heap_free(thisCmd);
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Main entry point. This is a console application so we have a main() not a
|
|
|
|
* winmain().
|
|
|
|
*/
|
|
|
|
|
|
|
|
int wmain (int argc, WCHAR *argvW[])
|
|
|
|
{
|
|
|
|
int args;
|
2012-10-03 01:50:13 +02:00
|
|
|
WCHAR *cmdLine = NULL;
|
|
|
|
WCHAR *cmd = NULL;
|
|
|
|
WCHAR *argPos = NULL;
|
2008-12-31 00:56:53 +01:00
|
|
|
WCHAR string[1024];
|
|
|
|
WCHAR envvar[4];
|
2018-09-02 00:11:23 +02:00
|
|
|
BOOL promptNewLine = TRUE;
|
2011-11-29 14:39:26 +01:00
|
|
|
BOOL opt_q;
|
2008-12-31 00:56:53 +01:00
|
|
|
int opt_t = 0;
|
2013-01-07 00:06:28 +01:00
|
|
|
static const WCHAR offW[] = {'O','F','F','\0'};
|
2009-12-31 03:47:32 +01:00
|
|
|
static const WCHAR promptW[] = {'P','R','O','M','P','T','\0'};
|
|
|
|
static const WCHAR defaultpromptW[] = {'$','P','$','G','\0'};
|
2018-08-31 03:09:32 +02:00
|
|
|
static const WCHAR comspecW[] = {'C','O','M','S','P','E','C',0};
|
|
|
|
static const WCHAR cmdW[] = {'\\','c','m','d','.','e','x','e',0};
|
|
|
|
WCHAR comspec[MAX_PATH];
|
2008-12-31 00:56:53 +01:00
|
|
|
CMD_LIST *toExecute = NULL; /* Commands left to be executed */
|
2012-11-26 22:06:52 +01:00
|
|
|
OSVERSIONINFOW osv;
|
|
|
|
char osver[50];
|
2008-12-31 00:56:53 +01:00
|
|
|
|
2018-08-31 03:09:32 +02:00
|
|
|
if (!GetEnvironmentVariableW(comspecW, comspec, sizeof(comspec)/sizeof(WCHAR)))
|
|
|
|
{
|
|
|
|
GetSystemDirectoryW(comspec, (sizeof(comspec) - sizeof(cmdW))/sizeof(WCHAR));
|
|
|
|
strcatW(comspec, cmdW);
|
|
|
|
SetEnvironmentVariableW(comspecW, comspec);
|
|
|
|
}
|
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
srand(time(NULL));
|
2007-09-11 22:43:08 +02:00
|
|
|
|
2012-11-26 22:06:52 +01:00
|
|
|
/* Get the windows version being emulated */
|
|
|
|
osv.dwOSVersionInfoSize = sizeof(osv);
|
|
|
|
GetVersionExW(&osv);
|
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Pre initialize some messages */
|
|
|
|
strcpyW(anykey, WCMD_LoadMessage(WCMD_ANYKEY));
|
2012-11-26 22:06:52 +01:00
|
|
|
sprintf(osver, "%d.%d.%d (%s)", osv.dwMajorVersion, osv.dwMinorVersion,
|
|
|
|
osv.dwBuildNumber, PACKAGE_VERSION);
|
|
|
|
cmd = WCMD_format_string(WCMD_LoadMessage(WCMD_VERSION), osver);
|
2011-12-06 17:40:42 +01:00
|
|
|
strcpyW(version_string, cmd);
|
|
|
|
LocalFree(cmd);
|
|
|
|
cmd = NULL;
|
2007-09-11 22:43:08 +02:00
|
|
|
|
2012-10-03 01:50:13 +02:00
|
|
|
/* Can't use argc/argv as it will have stripped quotes from parameters
|
|
|
|
* meaning cmd.exe /C echo "quoted string" is impossible
|
|
|
|
*/
|
|
|
|
cmdLine = GetCommandLineW();
|
|
|
|
WINE_TRACE("Full commandline '%s'\n", wine_dbgstr_w(cmdLine));
|
2014-03-15 10:00:06 +01:00
|
|
|
args = 0;
|
2012-10-03 01:50:13 +02:00
|
|
|
|
2011-11-29 14:39:26 +01:00
|
|
|
opt_c = opt_k = opt_q = opt_s = FALSE;
|
2012-10-25 23:47:59 +02:00
|
|
|
WCMD_parameter(cmdLine, args, &argPos, TRUE, TRUE);
|
2012-10-03 01:50:13 +02:00
|
|
|
while (argPos && argPos[0] != 0x00)
|
2008-12-31 00:56:53 +01:00
|
|
|
{
|
|
|
|
WCHAR c;
|
2012-10-03 01:50:13 +02:00
|
|
|
WINE_TRACE("Command line parm: '%s'\n", wine_dbgstr_w(argPos));
|
|
|
|
if (argPos[0]!='/' || argPos[1]=='\0') {
|
|
|
|
args++;
|
2012-10-25 23:47:59 +02:00
|
|
|
WCMD_parameter(cmdLine, args, &argPos, TRUE, TRUE);
|
2008-12-31 00:56:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
2007-09-11 22:43:08 +02:00
|
|
|
|
2012-10-03 01:50:13 +02:00
|
|
|
c=argPos[1];
|
2008-12-31 00:56:53 +01:00
|
|
|
if (tolowerW(c)=='c') {
|
2011-11-29 14:39:26 +01:00
|
|
|
opt_c = TRUE;
|
2008-12-31 00:56:53 +01:00
|
|
|
} else if (tolowerW(c)=='q') {
|
2011-11-29 14:39:26 +01:00
|
|
|
opt_q = TRUE;
|
2008-12-31 00:56:53 +01:00
|
|
|
} else if (tolowerW(c)=='k') {
|
2011-11-29 14:39:26 +01:00
|
|
|
opt_k = TRUE;
|
2008-12-31 00:56:53 +01:00
|
|
|
} else if (tolowerW(c)=='s') {
|
2011-11-29 14:39:26 +01:00
|
|
|
opt_s = TRUE;
|
2008-12-31 00:56:53 +01:00
|
|
|
} else if (tolowerW(c)=='a') {
|
2011-11-29 14:39:28 +01:00
|
|
|
unicodeOutput = FALSE;
|
2008-12-31 00:56:53 +01:00
|
|
|
} else if (tolowerW(c)=='u') {
|
2011-11-29 14:39:28 +01:00
|
|
|
unicodeOutput = TRUE;
|
2013-01-07 00:06:28 +01:00
|
|
|
} else if (tolowerW(c)=='v' && argPos[2]==':') {
|
|
|
|
delayedsubst = strncmpiW(&argPos[3], offW, 3);
|
|
|
|
if (delayedsubst) WINE_TRACE("Delayed substitution is on\n");
|
2012-10-03 01:50:13 +02:00
|
|
|
} else if (tolowerW(c)=='t' && argPos[2]==':') {
|
|
|
|
opt_t=strtoulW(&argPos[3], NULL, 16);
|
2008-12-31 00:56:53 +01:00
|
|
|
} else if (tolowerW(c)=='x' || tolowerW(c)=='y') {
|
|
|
|
/* Ignored for compatibility with Windows */
|
|
|
|
}
|
2007-09-11 22:43:08 +02:00
|
|
|
|
2013-01-07 00:06:28 +01:00
|
|
|
if (argPos[2]==0 || argPos[2]==' ' || argPos[2]=='\t' ||
|
|
|
|
tolowerW(c)=='v') {
|
2012-10-03 01:50:13 +02:00
|
|
|
args++;
|
2012-10-25 23:47:59 +02:00
|
|
|
WCMD_parameter(cmdLine, args, &argPos, TRUE, TRUE);
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
|
|
|
else /* handle `cmd /cnotepad.exe` and `cmd /x/c ...` */
|
|
|
|
{
|
2012-10-22 12:20:23 +02:00
|
|
|
/* Do not step to next parameter, instead carry on parsing this one */
|
2012-10-03 01:50:13 +02:00
|
|
|
argPos+=2;
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
2007-09-11 22:43:08 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
if (opt_c || opt_k) /* break out of parsing immediately after c or k */
|
|
|
|
break;
|
|
|
|
}
|
2007-09-11 22:43:08 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
if (opt_q) {
|
2013-01-07 00:06:28 +01:00
|
|
|
WCMD_echo(offW);
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
2007-09-11 22:43:08 +02:00
|
|
|
|
2012-10-01 23:37:17 +02:00
|
|
|
/* Until we start to read from the keyboard, stay as non-interactive */
|
|
|
|
interactive = FALSE;
|
|
|
|
|
2015-12-08 09:53:41 +01:00
|
|
|
SetEnvironmentVariableW(promptW, defaultpromptW);
|
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
if (opt_c || opt_k) {
|
2012-10-03 01:50:13 +02:00
|
|
|
int len;
|
|
|
|
WCHAR *q1 = NULL,*q2 = NULL,*p;
|
|
|
|
|
2012-10-22 12:20:23 +02:00
|
|
|
/* Handle very edge case error scenario, "cmd.exe /c" ie when there are no
|
2012-10-03 01:50:13 +02:00
|
|
|
* parameters after the /C or /K by pretending there was a single space */
|
|
|
|
if (argPos == NULL) argPos = (WCHAR *)spaceW;
|
|
|
|
|
|
|
|
/* Take a copy */
|
2012-11-15 12:32:21 +01:00
|
|
|
cmd = heap_strdupW(argPos);
|
2007-09-11 22:43:08 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* 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: &<>()@^| */
|
2007-06-15 21:59:19 +02:00
|
|
|
|
2012-10-03 01:50:13 +02:00
|
|
|
if (!opt_s) {
|
|
|
|
/* 1. Confirm there is at least one quote */
|
|
|
|
q1 = strchrW(argPos, '"');
|
|
|
|
if (!q1) opt_s=1;
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
2007-06-15 21:59:25 +02:00
|
|
|
|
2012-10-03 01:50:13 +02:00
|
|
|
if (!opt_s) {
|
|
|
|
/* 2. Confirm there is a second quote */
|
|
|
|
q2 = strchrW(q1+1, '"');
|
|
|
|
if (!q2) opt_s=1;
|
|
|
|
}
|
2007-06-15 21:59:25 +02:00
|
|
|
|
2012-10-03 01:50:13 +02:00
|
|
|
if (!opt_s) {
|
|
|
|
/* 3. Ensure there are no more quotes */
|
|
|
|
if (strchrW(q2+1, '"')) opt_s=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check first parameter for a space and invalid characters. There must not be any
|
|
|
|
* invalid characters, but there must be one or more whitespace */
|
2008-12-31 00:56:53 +01:00
|
|
|
if (!opt_s) {
|
2011-11-29 14:39:26 +01:00
|
|
|
opt_s = TRUE;
|
2012-10-03 01:50:13 +02:00
|
|
|
p=q1;
|
|
|
|
while (p!=q2) {
|
2008-12-31 00:56:53 +01:00
|
|
|
if (*p=='&' || *p=='<' || *p=='>' || *p=='(' || *p==')'
|
|
|
|
|| *p=='@' || *p=='^' || *p=='|') {
|
2011-11-29 14:39:26 +01:00
|
|
|
opt_s = TRUE;
|
2008-12-31 00:56:53 +01:00
|
|
|
break;
|
|
|
|
}
|
2012-10-02 01:35:47 +02:00
|
|
|
if (*p==' ' || *p=='\t')
|
2011-11-29 14:39:26 +01:00
|
|
|
opt_s = FALSE;
|
2008-12-31 00:56:53 +01:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
2007-06-15 21:59:25 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
WINE_TRACE("/c command line: '%s'\n", wine_dbgstr_w(cmd));
|
2008-03-04 00:14:29 +01:00
|
|
|
|
2012-10-02 01:35:47 +02:00
|
|
|
/* Finally, we only stay in new mode IF the first parameter is quoted and
|
2012-10-22 12:20:23 +02:00
|
|
|
is a valid executable, i.e. must exist, otherwise drop back to old mode */
|
2012-10-02 01:35:47 +02:00
|
|
|
if (!opt_s) {
|
2012-10-25 23:47:59 +02:00
|
|
|
WCHAR *thisArg = WCMD_parameter(cmd, 0, NULL, FALSE, TRUE);
|
2012-10-13 22:50:22 +02:00
|
|
|
WCHAR pathext[MAXSTRING];
|
2012-10-02 01:35:47 +02:00
|
|
|
BOOL found = FALSE;
|
|
|
|
|
2012-10-13 22:50:22 +02:00
|
|
|
/* Now extract PATHEXT */
|
2018-08-20 21:15:42 +02:00
|
|
|
len = GetEnvironmentVariableW(envPathExt, pathext, ARRAY_SIZE(pathext));
|
|
|
|
if ((len == 0) || (len >= ARRAY_SIZE(pathext))) {
|
2012-10-13 22:50:22 +02:00
|
|
|
strcpyW (pathext, dfltPathExt);
|
|
|
|
}
|
|
|
|
|
2012-10-02 01:35:47 +02:00
|
|
|
/* If the supplied parameter has any directory information, look there */
|
|
|
|
WINE_TRACE("First parameter is '%s'\n", wine_dbgstr_w(thisArg));
|
|
|
|
if (strchrW(thisArg, '\\') != NULL) {
|
|
|
|
|
2018-08-20 21:15:42 +02:00
|
|
|
GetFullPathNameW(thisArg, ARRAY_SIZE(string), string, NULL);
|
2012-10-02 01:35:47 +02:00
|
|
|
WINE_TRACE("Full path name '%s'\n", wine_dbgstr_w(string));
|
|
|
|
p = string + strlenW(string);
|
|
|
|
|
|
|
|
/* Does file exist with this name? */
|
|
|
|
if (GetFileAttributesW(string) != INVALID_FILE_ATTRIBUTES) {
|
|
|
|
WINE_TRACE("Found file as '%s'\n", wine_dbgstr_w(string));
|
|
|
|
found = TRUE;
|
2012-10-13 22:50:22 +02:00
|
|
|
} else {
|
|
|
|
WCHAR *thisExt = pathext;
|
|
|
|
|
|
|
|
/* No - try with each of the PATHEXT extensions */
|
|
|
|
while (!found && thisExt) {
|
|
|
|
WCHAR *nextExt = strchrW(thisExt, ';');
|
|
|
|
|
|
|
|
if (nextExt) {
|
|
|
|
memcpy(p, thisExt, (nextExt-thisExt) * sizeof(WCHAR));
|
|
|
|
p[(nextExt-thisExt)] = 0x00;
|
|
|
|
thisExt = nextExt+1;
|
|
|
|
} else {
|
|
|
|
strcpyW(p, thisExt);
|
|
|
|
thisExt = NULL;
|
|
|
|
}
|
2012-10-02 01:35:47 +02:00
|
|
|
|
2012-10-13 22:50:22 +02:00
|
|
|
/* Does file exist with this extension appended? */
|
|
|
|
if (GetFileAttributesW(string) != INVALID_FILE_ATTRIBUTES) {
|
|
|
|
WINE_TRACE("Found file as '%s'\n", wine_dbgstr_w(string));
|
|
|
|
found = TRUE;
|
|
|
|
}
|
|
|
|
}
|
2012-10-02 01:35:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise we now need to look in the path to see if we can find it */
|
|
|
|
} else {
|
|
|
|
/* Does file exist with this name? */
|
2018-08-20 21:15:42 +02:00
|
|
|
if (SearchPathW(NULL, thisArg, NULL, ARRAY_SIZE(string), string, NULL) != 0) {
|
2012-10-02 01:35:47 +02:00
|
|
|
WINE_TRACE("Found on path as '%s'\n", wine_dbgstr_w(string));
|
|
|
|
found = TRUE;
|
2012-10-13 22:50:22 +02:00
|
|
|
} else {
|
|
|
|
WCHAR *thisExt = pathext;
|
2012-10-02 01:35:47 +02:00
|
|
|
|
2012-10-13 22:50:22 +02:00
|
|
|
/* No - try with each of the PATHEXT extensions */
|
|
|
|
while (!found && thisExt) {
|
|
|
|
WCHAR *nextExt = strchrW(thisExt, ';');
|
2012-10-02 01:35:47 +02:00
|
|
|
|
2012-10-13 22:50:22 +02:00
|
|
|
if (nextExt) {
|
|
|
|
*nextExt = 0;
|
|
|
|
nextExt = nextExt+1;
|
|
|
|
} else {
|
|
|
|
nextExt = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Does file exist with this extension? */
|
2018-08-20 21:15:42 +02:00
|
|
|
if (SearchPathW(NULL, thisArg, thisExt, ARRAY_SIZE(string), string, NULL) != 0) {
|
2012-10-13 22:50:22 +02:00
|
|
|
WINE_TRACE("Found on path as '%s' with extension '%s'\n", wine_dbgstr_w(string),
|
|
|
|
wine_dbgstr_w(thisExt));
|
|
|
|
found = TRUE;
|
|
|
|
}
|
|
|
|
thisExt = nextExt;
|
|
|
|
}
|
2012-10-02 01:35:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If not found, drop back to old behaviour */
|
|
|
|
if (!found) {
|
|
|
|
WINE_TRACE("Binary not found, dropping back to old behaviour\n");
|
|
|
|
opt_s = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* strip first and last quote characters if opt_s; check for invalid
|
|
|
|
* executable is done later */
|
|
|
|
if (opt_s && *cmd=='\"')
|
2011-10-19 09:47:58 +02:00
|
|
|
WCMD_strip_quotes(cmd);
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
2007-06-15 21:59:19 +02:00
|
|
|
|
2012-10-01 01:12:59 +02:00
|
|
|
/* Save cwd into appropriate env var (Must be before the /c processing */
|
2018-08-20 21:15:42 +02:00
|
|
|
GetCurrentDirectoryW(ARRAY_SIZE(string), string);
|
2012-10-01 01:12:59 +02:00
|
|
|
if (IsCharAlphaW(string[0]) && string[1] == ':') {
|
|
|
|
static const WCHAR fmt[] = {'=','%','c',':','\0'};
|
|
|
|
wsprintfW(envvar, fmt, string[0]);
|
|
|
|
SetEnvironmentVariableW(envvar, string);
|
|
|
|
WINE_TRACE("Set %s to %s\n", wine_dbgstr_w(envvar), wine_dbgstr_w(string));
|
|
|
|
}
|
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
if (opt_c) {
|
2011-07-27 02:21:01 +02:00
|
|
|
/* If we do a "cmd /c command", we don't want to allocate a new
|
2008-12-31 00:56:53 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
2007-06-15 21:59:19 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* Parse the command string, without reading any more input */
|
2011-10-06 18:50:31 +02:00
|
|
|
WCMD_ReadAndParseLine(cmd, &toExecute, INVALID_HANDLE_VALUE);
|
2012-11-05 22:43:08 +01:00
|
|
|
WCMD_process_commands(toExecute, FALSE, FALSE);
|
2008-12-31 00:56:53 +01:00
|
|
|
WCMD_free_commands(toExecute);
|
|
|
|
toExecute = NULL;
|
2007-09-11 22:43:08 +02:00
|
|
|
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(cmd);
|
2008-12-31 00:56:53 +01:00
|
|
|
return errorlevel;
|
|
|
|
}
|
2007-06-15 21:59:19 +02:00
|
|
|
|
2009-12-09 18:52:40 +01:00
|
|
|
SetConsoleTitleW(WCMD_LoadMessage(WCMD_CONSTITLE));
|
2007-06-15 21:59:28 +02:00
|
|
|
|
2008-12-31 00:56:53 +01: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;
|
|
|
|
param1[0] = 0x00;
|
|
|
|
WCMD_color();
|
2007-06-15 21:59:19 +02:00
|
|
|
}
|
2008-12-31 00:56:53 +01:00
|
|
|
} 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;
|
|
|
|
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-15 21:59:19 +02:00
|
|
|
|
2009-12-09 18:52:40 +01:00
|
|
|
if (RegOpenKeyExW(HKEY_CURRENT_USER, regKeyW,
|
2008-12-31 00:56:53 +01:00
|
|
|
0, KEY_READ, &key) == ERROR_SUCCESS) {
|
|
|
|
WCHAR strvalue[4];
|
2007-06-15 21:59:25 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* See if DWORD or REG_SZ */
|
2009-12-09 18:52:40 +01:00
|
|
|
if (RegQueryValueExW(key, dfltColorW, NULL, &type,
|
2008-12-31 00:56:53 +01:00
|
|
|
NULL, NULL) == ERROR_SUCCESS) {
|
|
|
|
if (type == REG_DWORD) {
|
|
|
|
size = sizeof(DWORD);
|
2009-12-09 18:52:40 +01:00
|
|
|
RegQueryValueExW(key, dfltColorW, NULL, NULL,
|
2008-12-31 00:56:53 +01:00
|
|
|
(LPBYTE)&value, &size);
|
|
|
|
} else if (type == REG_SZ) {
|
2018-08-20 21:15:42 +02:00
|
|
|
size = ARRAY_SIZE(strvalue);
|
2009-12-09 18:52:40 +01:00
|
|
|
RegQueryValueExW(key, dfltColorW, NULL, NULL,
|
2008-12-31 00:56:53 +01:00
|
|
|
(LPBYTE)strvalue, &size);
|
|
|
|
value = strtoulW(strvalue, NULL, 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RegCloseKey(key);
|
2007-06-15 21:59:25 +02:00
|
|
|
}
|
|
|
|
|
2009-12-09 18:52:40 +01:00
|
|
|
if (value == 0 && RegOpenKeyExW(HKEY_LOCAL_MACHINE, regKeyW,
|
2008-12-31 00:56:53 +01:00
|
|
|
0, KEY_READ, &key) == ERROR_SUCCESS) {
|
|
|
|
WCHAR strvalue[4];
|
2007-06-15 21:59:19 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* See if DWORD or REG_SZ */
|
2009-12-09 18:52:40 +01:00
|
|
|
if (RegQueryValueExW(key, dfltColorW, NULL, &type,
|
2008-12-31 00:56:53 +01:00
|
|
|
NULL, NULL) == ERROR_SUCCESS) {
|
|
|
|
if (type == REG_DWORD) {
|
|
|
|
size = sizeof(DWORD);
|
2009-12-09 18:52:40 +01:00
|
|
|
RegQueryValueExW(key, dfltColorW, NULL, NULL,
|
2008-12-31 00:56:53 +01:00
|
|
|
(LPBYTE)&value, &size);
|
|
|
|
} else if (type == REG_SZ) {
|
2018-08-20 21:15:42 +02:00
|
|
|
size = ARRAY_SIZE(strvalue);
|
2009-12-09 18:52:40 +01:00
|
|
|
RegQueryValueExW(key, dfltColorW, NULL, NULL,
|
2008-12-31 00:56:53 +01:00
|
|
|
(LPBYTE)strvalue, &size);
|
|
|
|
value = strtoulW(strvalue, NULL, 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RegCloseKey(key);
|
2007-06-15 21:59:19 +02:00
|
|
|
}
|
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/* If one found, set the screen to that colour */
|
|
|
|
if (!(((value & 0xF0) >> 4) == (value & 0x0F))) {
|
|
|
|
defaultColor = value & 0xFF;
|
|
|
|
param1[0] = 0x00;
|
|
|
|
WCMD_color();
|
2007-06-15 21:59:19 +02:00
|
|
|
}
|
2007-06-15 21:59:27 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
2007-06-15 21:59:19 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
if (opt_k) {
|
|
|
|
/* Parse the command string, without reading any more input */
|
2011-10-06 18:50:31 +02:00
|
|
|
WCMD_ReadAndParseLine(cmd, &toExecute, INVALID_HANDLE_VALUE);
|
2012-11-05 22:43:08 +01:00
|
|
|
WCMD_process_commands(toExecute, FALSE, FALSE);
|
2008-12-31 00:56:53 +01:00
|
|
|
WCMD_free_commands(toExecute);
|
|
|
|
toExecute = NULL;
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(cmd);
|
2008-12-31 00:56:53 +01:00
|
|
|
}
|
2007-06-15 21:59:27 +02:00
|
|
|
|
2008-12-31 00:56:53 +01:00
|
|
|
/*
|
|
|
|
* Loop forever getting commands and executing them.
|
2007-06-15 21:59:19 +02:00
|
|
|
*/
|
|
|
|
|
2012-10-01 23:37:17 +02:00
|
|
|
interactive = TRUE;
|
|
|
|
if (!opt_k) WCMD_version ();
|
2008-12-31 00:56:53 +01:00
|
|
|
while (TRUE) {
|
|
|
|
|
|
|
|
/* Read until EOF (which for std input is never, but if redirect
|
|
|
|
in place, may occur */
|
2018-09-02 00:11:23 +02:00
|
|
|
if (echo_mode) WCMD_show_prompt(promptNewLine);
|
2011-10-06 18:50:31 +02:00
|
|
|
if (!WCMD_ReadAndParseLine(NULL, &toExecute, GetStdHandle(STD_INPUT_HANDLE)))
|
2008-12-31 00:56:53 +01:00
|
|
|
break;
|
2012-11-05 22:43:08 +01:00
|
|
|
WCMD_process_commands(toExecute, FALSE, FALSE);
|
2008-12-31 00:56:53 +01:00
|
|
|
WCMD_free_commands(toExecute);
|
2018-09-02 00:11:23 +02:00
|
|
|
promptNewLine = !!toExecute;
|
2008-12-31 00:56:53 +01:00
|
|
|
toExecute = NULL;
|
|
|
|
}
|
|
|
|
return 0;
|
2007-06-15 21:59:19 +02:00
|
|
|
}
|