1999-06-06 17:24:04 +02:00
|
|
|
/*
|
2006-09-04 06:13:08 +02:00
|
|
|
* CMD - Wine-compatible command line interface - built-in functions.
|
1999-06-06 17:24:04 +02:00
|
|
|
*
|
2002-03-10 00:29:33 +01:00
|
|
|
* Copyright (C) 1999 D A Pickles
|
2007-09-11 22:43:08 +02:00
|
|
|
* Copyright (C) 2007 J Edmeades
|
1999-06-06 17:24:04 +02:00
|
|
|
*
|
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
|
2002-03-10 00:29:33 +01:00
|
|
|
*/
|
|
|
|
|
1999-06-06 17:24:04 +02:00
|
|
|
/*
|
|
|
|
* FIXME:
|
1999-07-10 13:36:45 +02:00
|
|
|
* - No support for pipes, shell parameters
|
1999-06-06 17:24:04 +02:00
|
|
|
* - Lots of functionality missing from builtins
|
|
|
|
* - Messages etc need international support
|
|
|
|
*/
|
|
|
|
|
2006-01-16 20:41:34 +01:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
|
1999-06-06 17:24:04 +02:00
|
|
|
#include "wcmd.h"
|
2007-02-27 00:04:40 +01:00
|
|
|
#include <shellapi.h>
|
2007-03-04 23:33:27 +01:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(cmd);
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2011-09-16 08:44:07 +02:00
|
|
|
extern int defaultColor;
|
2011-09-16 08:44:06 +02:00
|
|
|
extern BOOL echo_mode;
|
2012-10-01 23:37:17 +02:00
|
|
|
extern BOOL interactive;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2011-11-21 09:08:03 +01:00
|
|
|
struct env_stack *pushd_directories;
|
|
|
|
const WCHAR inbuilt[][10] = {
|
2020-11-29 23:30:29 +01:00
|
|
|
L"CALL",
|
|
|
|
L"CD",
|
|
|
|
L"CHDIR",
|
|
|
|
L"CLS",
|
|
|
|
L"COPY",
|
|
|
|
L"CTTY",
|
|
|
|
L"DATE",
|
|
|
|
L"DEL",
|
|
|
|
L"DIR",
|
|
|
|
L"ECHO",
|
|
|
|
L"ERASE",
|
|
|
|
L"FOR",
|
|
|
|
L"GOTO",
|
|
|
|
L"HELP",
|
|
|
|
L"IF",
|
|
|
|
L"LABEL",
|
|
|
|
L"MD",
|
|
|
|
L"MKDIR",
|
|
|
|
L"MOVE",
|
|
|
|
L"PATH",
|
|
|
|
L"PAUSE",
|
|
|
|
L"PROMPT",
|
|
|
|
L"REM",
|
|
|
|
L"REN",
|
|
|
|
L"RENAME",
|
|
|
|
L"RD",
|
|
|
|
L"RMDIR",
|
|
|
|
L"SET",
|
|
|
|
L"SHIFT",
|
|
|
|
L"START",
|
|
|
|
L"TIME",
|
|
|
|
L"TITLE",
|
|
|
|
L"TYPE",
|
|
|
|
L"VERIFY",
|
|
|
|
L"VER",
|
|
|
|
L"VOL",
|
|
|
|
L"ENDLOCAL",
|
|
|
|
L"SETLOCAL",
|
|
|
|
L"PUSHD",
|
|
|
|
L"POPD",
|
|
|
|
L"ASSOC",
|
|
|
|
L"COLOR",
|
|
|
|
L"FTYPE",
|
|
|
|
L"MORE",
|
|
|
|
L"CHOICE",
|
|
|
|
L"MKLINK",
|
|
|
|
L"EXIT"
|
2011-11-21 09:08:03 +01:00
|
|
|
};
|
|
|
|
static const WCHAR externals[][10] = {
|
2020-11-29 23:30:29 +01:00
|
|
|
L"ATTRIB",
|
|
|
|
L"XCOPY"
|
2011-11-21 09:08:03 +01:00
|
|
|
};
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2011-11-29 23:30:52 +01:00
|
|
|
static HINSTANCE hinst;
|
2012-10-01 00:07:01 +02:00
|
|
|
struct env_stack *saved_environment;
|
2011-11-29 23:30:52 +01:00
|
|
|
static BOOL verify_mode = FALSE;
|
|
|
|
|
2012-12-21 00:38:14 +01:00
|
|
|
/* set /a routines work from single character operators, but some of the
|
|
|
|
operators are multiple character ones, especially the assignment ones.
|
|
|
|
Temporarily represent these using the values below on the operator stack */
|
|
|
|
#define OP_POSITIVE 'P'
|
|
|
|
#define OP_NEGATIVE 'N'
|
|
|
|
#define OP_ASSSIGNMUL 'a'
|
|
|
|
#define OP_ASSSIGNDIV 'b'
|
|
|
|
#define OP_ASSSIGNMOD 'c'
|
|
|
|
#define OP_ASSSIGNADD 'd'
|
|
|
|
#define OP_ASSSIGNSUB 'e'
|
|
|
|
#define OP_ASSSIGNAND 'f'
|
|
|
|
#define OP_ASSSIGNNOT 'g'
|
|
|
|
#define OP_ASSSIGNOR 'h'
|
|
|
|
#define OP_ASSSIGNSHL 'i'
|
|
|
|
#define OP_ASSSIGNSHR 'j'
|
|
|
|
|
|
|
|
/* This maintains a stack of operators, holding both the operator precedence
|
|
|
|
and the single character representation of the operator in question */
|
|
|
|
typedef struct _OPSTACK
|
|
|
|
{
|
|
|
|
int precedence;
|
|
|
|
WCHAR op;
|
|
|
|
struct _OPSTACK *next;
|
|
|
|
} OPSTACK;
|
|
|
|
|
|
|
|
/* This maintains a stack of values, where each value can either be a
|
2015-08-17 15:11:55 +02:00
|
|
|
numeric value, or a string representing an environment variable */
|
2012-12-21 00:38:14 +01:00
|
|
|
typedef struct _VARSTACK
|
|
|
|
{
|
|
|
|
BOOL isnum;
|
|
|
|
WCHAR *variable;
|
|
|
|
int value;
|
|
|
|
struct _VARSTACK *next;
|
|
|
|
} VARSTACK;
|
|
|
|
|
|
|
|
/* This maintains a mapping between the calculated operator and the
|
|
|
|
single character representation for the assignment operators. */
|
|
|
|
static struct
|
|
|
|
{
|
|
|
|
WCHAR op;
|
|
|
|
WCHAR calculatedop;
|
|
|
|
} calcassignments[] =
|
|
|
|
{
|
|
|
|
{'*', OP_ASSSIGNMUL},
|
|
|
|
{'/', OP_ASSSIGNDIV},
|
|
|
|
{'%', OP_ASSSIGNMOD},
|
|
|
|
{'+', OP_ASSSIGNADD},
|
|
|
|
{'-', OP_ASSSIGNSUB},
|
|
|
|
{'&', OP_ASSSIGNAND},
|
|
|
|
{'^', OP_ASSSIGNNOT},
|
|
|
|
{'|', OP_ASSSIGNOR},
|
|
|
|
{'<', OP_ASSSIGNSHL},
|
|
|
|
{'>', OP_ASSSIGNSHR},
|
|
|
|
{' ',' '}
|
|
|
|
};
|
|
|
|
|
2009-01-05 19:57:31 +01:00
|
|
|
/**************************************************************************
|
|
|
|
* WCMD_ask_confirm
|
|
|
|
*
|
2012-01-23 17:37:18 +01:00
|
|
|
* Issue a message and ask for confirmation, waiting on a valid answer.
|
2009-01-05 19:57:31 +01:00
|
|
|
*
|
|
|
|
* Returns True if Y (or A) answer is selected
|
|
|
|
* If optionAll contains a pointer, ALL is allowed, and if answered
|
|
|
|
* set to TRUE
|
|
|
|
*
|
|
|
|
*/
|
2011-08-20 01:58:59 +02:00
|
|
|
static BOOL WCMD_ask_confirm (const WCHAR *message, BOOL showSureText,
|
2012-01-23 17:37:18 +01:00
|
|
|
BOOL *optionAll) {
|
2009-01-05 19:57:31 +01:00
|
|
|
|
2012-01-23 17:37:47 +01:00
|
|
|
UINT msgid;
|
|
|
|
WCHAR confirm[MAXSTRING];
|
|
|
|
WCHAR options[MAXSTRING];
|
|
|
|
WCHAR Ybuffer[MAXSTRING];
|
|
|
|
WCHAR Nbuffer[MAXSTRING];
|
|
|
|
WCHAR Abuffer[MAXSTRING];
|
|
|
|
WCHAR answer[MAX_PATH] = {'\0'};
|
2009-01-05 19:57:31 +01:00
|
|
|
DWORD count = 0;
|
|
|
|
|
2012-01-23 17:37:47 +01:00
|
|
|
/* Load the translated valid answers */
|
|
|
|
if (showSureText)
|
2018-08-20 21:15:42 +02:00
|
|
|
LoadStringW(hinst, WCMD_CONFIRM, confirm, ARRAY_SIZE(confirm));
|
2012-01-23 17:37:47 +01:00
|
|
|
msgid = optionAll ? WCMD_YESNOALL : WCMD_YESNO;
|
2018-08-20 21:15:42 +02:00
|
|
|
LoadStringW(hinst, msgid, options, ARRAY_SIZE(options));
|
|
|
|
LoadStringW(hinst, WCMD_YES, Ybuffer, ARRAY_SIZE(Ybuffer));
|
|
|
|
LoadStringW(hinst, WCMD_NO, Nbuffer, ARRAY_SIZE(Nbuffer));
|
|
|
|
LoadStringW(hinst, WCMD_ALL, Abuffer, ARRAY_SIZE(Abuffer));
|
2009-01-05 19:57:31 +01:00
|
|
|
|
2012-01-23 17:37:18 +01:00
|
|
|
/* Loop waiting on a valid answer */
|
|
|
|
if (optionAll)
|
|
|
|
*optionAll = FALSE;
|
2012-01-23 17:37:47 +01:00
|
|
|
while (1)
|
|
|
|
{
|
2009-01-05 19:57:31 +01:00
|
|
|
WCMD_output_asis (message);
|
2012-01-23 17:37:47 +01:00
|
|
|
if (showSureText)
|
|
|
|
WCMD_output_asis (confirm);
|
|
|
|
WCMD_output_asis (options);
|
2018-08-20 21:15:42 +02:00
|
|
|
WCMD_ReadFile(GetStdHandle(STD_INPUT_HANDLE), answer, ARRAY_SIZE(answer), &count);
|
2019-05-03 12:28:33 +02:00
|
|
|
answer[0] = towupper(answer[0]);
|
2012-01-23 17:37:18 +01:00
|
|
|
if (answer[0] == Ybuffer[0])
|
|
|
|
return TRUE;
|
|
|
|
if (answer[0] == Nbuffer[0])
|
|
|
|
return FALSE;
|
|
|
|
if (optionAll && answer[0] == Abuffer[0])
|
|
|
|
{
|
|
|
|
*optionAll = TRUE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2009-01-05 19:57:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-06-06 17:24:04 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_clear_screen
|
|
|
|
*
|
|
|
|
* Clear the terminal screen.
|
|
|
|
*/
|
|
|
|
|
2003-02-27 02:41:21 +01:00
|
|
|
void WCMD_clear_screen (void) {
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2002-04-29 19:12:57 +02:00
|
|
|
/* Emulate by filling the screen from the top left to bottom right with
|
|
|
|
spaces, then moving the cursor to the top left afterwards */
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
|
|
|
|
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2003-02-27 02:41:21 +01:00
|
|
|
if (GetConsoleScreenBufferInfo(hStdOut, &consoleInfo))
|
|
|
|
{
|
|
|
|
COORD topLeft;
|
2015-11-16 01:53:06 +01:00
|
|
|
DWORD screenSize, written;
|
2005-08-19 12:04:03 +02:00
|
|
|
|
2003-02-27 02:41:21 +01:00
|
|
|
screenSize = consoleInfo.dwSize.X * (consoleInfo.dwSize.Y + 1);
|
2002-04-29 19:12:57 +02:00
|
|
|
|
2003-02-27 02:41:21 +01:00
|
|
|
topLeft.X = 0;
|
|
|
|
topLeft.Y = 0;
|
2015-11-16 01:53:06 +01:00
|
|
|
FillConsoleOutputCharacterW(hStdOut, ' ', screenSize, topLeft, &written);
|
|
|
|
FillConsoleOutputAttribute(hStdOut, consoleInfo.wAttributes, screenSize, topLeft, &written);
|
2003-02-27 02:41:21 +01:00
|
|
|
SetConsoleCursorPosition(hStdOut, topLeft);
|
|
|
|
}
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_change_tty
|
|
|
|
*
|
|
|
|
* Change the default i/o device (ie redirect STDin/STDout).
|
|
|
|
*/
|
|
|
|
|
2003-02-27 02:41:21 +01:00
|
|
|
void WCMD_change_tty (void) {
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2011-12-02 10:07:27 +01:00
|
|
|
WCMD_output_stderr (WCMD_LoadMessage(WCMD_NYI));
|
1999-06-06 17:24:04 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-12-19 23:13:32 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_choice
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-10-17 00:41:58 +02:00
|
|
|
void WCMD_choice (const WCHAR * args) {
|
2010-12-19 23:13:32 +01:00
|
|
|
WCHAR answer[16];
|
|
|
|
WCHAR buffer[16];
|
|
|
|
WCHAR *ptr = NULL;
|
|
|
|
WCHAR *opt_c = NULL;
|
|
|
|
WCHAR *my_command = NULL;
|
|
|
|
WCHAR opt_default = 0;
|
|
|
|
DWORD opt_timeout = 0;
|
|
|
|
DWORD count;
|
|
|
|
DWORD oldmode;
|
2013-10-16 23:38:31 +02:00
|
|
|
BOOL have_console;
|
2010-12-19 23:13:32 +01:00
|
|
|
BOOL opt_n = FALSE;
|
|
|
|
BOOL opt_s = FALSE;
|
|
|
|
|
|
|
|
have_console = GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &oldmode);
|
|
|
|
errorlevel = 0;
|
|
|
|
|
2012-11-15 12:32:21 +01:00
|
|
|
my_command = heap_strdupW(WCMD_skip_leading_spaces((WCHAR*) args));
|
2010-12-19 23:13:32 +01:00
|
|
|
|
2011-08-20 01:58:58 +02:00
|
|
|
ptr = WCMD_skip_leading_spaces(my_command);
|
2010-12-19 23:13:32 +01:00
|
|
|
while (*ptr == '/') {
|
2019-05-03 12:28:33 +02:00
|
|
|
switch (towupper(ptr[1])) {
|
2010-12-19 23:13:32 +01:00
|
|
|
case 'C':
|
|
|
|
ptr += 2;
|
|
|
|
/* the colon is optional */
|
|
|
|
if (*ptr == ':')
|
|
|
|
ptr++;
|
|
|
|
|
2019-05-03 12:28:33 +02:00
|
|
|
if (!*ptr || iswspace(*ptr)) {
|
2010-12-19 23:13:32 +01:00
|
|
|
WINE_FIXME("bad parameter %s for /C\n", wine_dbgstr_w(ptr));
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(my_command);
|
2010-12-19 23:13:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remember the allowed keys (overwrite previous /C option) */
|
|
|
|
opt_c = ptr;
|
2019-05-03 12:28:33 +02:00
|
|
|
while (*ptr && (!iswspace(*ptr)))
|
2010-12-19 23:13:32 +01:00
|
|
|
ptr++;
|
|
|
|
|
|
|
|
if (*ptr) {
|
|
|
|
/* terminate allowed chars */
|
|
|
|
*ptr = 0;
|
2011-08-20 01:58:58 +02:00
|
|
|
ptr = WCMD_skip_leading_spaces(&ptr[1]);
|
2010-12-19 23:13:32 +01:00
|
|
|
}
|
|
|
|
WINE_TRACE("answer-list: %s\n", wine_dbgstr_w(opt_c));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'N':
|
|
|
|
opt_n = TRUE;
|
2011-08-20 01:58:58 +02:00
|
|
|
ptr = WCMD_skip_leading_spaces(&ptr[2]);
|
2010-12-19 23:13:32 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'S':
|
|
|
|
opt_s = TRUE;
|
2011-08-20 01:58:58 +02:00
|
|
|
ptr = WCMD_skip_leading_spaces(&ptr[2]);
|
2010-12-19 23:13:32 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'T':
|
|
|
|
ptr = &ptr[2];
|
|
|
|
/* the colon is optional */
|
|
|
|
if (*ptr == ':')
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
opt_default = *ptr++;
|
|
|
|
|
|
|
|
if (!opt_default || (*ptr != ',')) {
|
|
|
|
WINE_FIXME("bad option %s for /T\n", opt_default ? wine_dbgstr_w(ptr) : "");
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(my_command);
|
2010-12-19 23:13:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
count = 0;
|
2019-05-03 12:28:33 +02:00
|
|
|
while (((answer[count] = *ptr)) && iswdigit(*ptr) && (count < 15)) {
|
2010-12-19 23:13:32 +01:00
|
|
|
count++;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
answer[count] = 0;
|
2019-05-03 12:28:33 +02:00
|
|
|
opt_timeout = wcstol(answer, NULL, 10);
|
2010-12-19 23:13:32 +01:00
|
|
|
|
2011-08-20 01:58:58 +02:00
|
|
|
ptr = WCMD_skip_leading_spaces(ptr);
|
2010-12-19 23:13:32 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
WINE_FIXME("bad parameter: %s\n", wine_dbgstr_w(ptr));
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(my_command);
|
2010-12-19 23:13:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opt_timeout)
|
|
|
|
WINE_FIXME("timeout not supported: %c,%d\n", opt_default, opt_timeout);
|
|
|
|
|
|
|
|
if (have_console)
|
|
|
|
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), 0);
|
|
|
|
|
|
|
|
/* use default keys, when needed: localized versions of "Y"es and "No" */
|
|
|
|
if (!opt_c) {
|
2018-08-20 21:15:42 +02:00
|
|
|
LoadStringW(hinst, WCMD_YES, buffer, ARRAY_SIZE(buffer));
|
|
|
|
LoadStringW(hinst, WCMD_NO, buffer + 1, ARRAY_SIZE(buffer) - 1);
|
2010-12-19 23:13:32 +01:00
|
|
|
opt_c = buffer;
|
|
|
|
buffer[2] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print the question, when needed */
|
|
|
|
if (*ptr)
|
|
|
|
WCMD_output_asis(ptr);
|
|
|
|
|
|
|
|
if (!opt_s) {
|
2019-05-03 12:28:33 +02:00
|
|
|
wcsupr(opt_c);
|
2010-12-19 23:13:32 +01:00
|
|
|
WINE_TRACE("case insensitive answer-list: %s\n", wine_dbgstr_w(opt_c));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!opt_n) {
|
|
|
|
/* print a list of all allowed answers inside brackets */
|
2020-11-29 23:30:29 +01:00
|
|
|
WCMD_output_asis(L"[");
|
2010-12-19 23:13:32 +01:00
|
|
|
ptr = opt_c;
|
|
|
|
answer[1] = 0;
|
|
|
|
while ((answer[0] = *ptr++)) {
|
|
|
|
WCMD_output_asis(answer);
|
|
|
|
if (*ptr)
|
2020-11-29 23:30:29 +01:00
|
|
|
WCMD_output_asis(L",");
|
2010-12-19 23:13:32 +01:00
|
|
|
}
|
2020-11-29 23:30:29 +01:00
|
|
|
WCMD_output_asis(L"]?");
|
2010-12-19 23:13:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
while (TRUE) {
|
|
|
|
|
|
|
|
/* FIXME: Add support for option /T */
|
2013-07-10 08:26:14 +02:00
|
|
|
answer[1] = 0; /* terminate single character string */
|
2011-09-28 14:41:22 +02:00
|
|
|
WCMD_ReadFile(GetStdHandle(STD_INPUT_HANDLE), answer, 1, &count);
|
2010-12-19 23:13:32 +01:00
|
|
|
|
|
|
|
if (!opt_s)
|
2019-05-03 12:28:33 +02:00
|
|
|
answer[0] = towupper(answer[0]);
|
2010-12-19 23:13:32 +01:00
|
|
|
|
2019-05-03 12:28:33 +02:00
|
|
|
ptr = wcschr(opt_c, answer[0]);
|
2010-12-19 23:13:32 +01:00
|
|
|
if (ptr) {
|
|
|
|
WCMD_output_asis(answer);
|
2020-11-29 23:30:28 +01:00
|
|
|
WCMD_output_asis(L"\r\n");
|
2010-12-19 23:13:32 +01:00
|
|
|
if (have_console)
|
|
|
|
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), oldmode);
|
|
|
|
|
|
|
|
errorlevel = (ptr - opt_c) + 1;
|
|
|
|
WINE_TRACE("answer: %d\n", errorlevel);
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(my_command);
|
2010-12-19 23:13:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* key not allowed: play the bell */
|
|
|
|
WINE_TRACE("key not allowed: %s\n", wine_dbgstr_w(answer));
|
2020-11-29 23:30:29 +01:00
|
|
|
WCMD_output_asis(L"\a");
|
2010-12-19 23:13:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-12 01:37:24 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_AppendEOF
|
|
|
|
*
|
|
|
|
* Adds an EOF onto the end of a file
|
|
|
|
* Returns TRUE on success
|
|
|
|
*/
|
|
|
|
static BOOL WCMD_AppendEOF(WCHAR *filename)
|
|
|
|
{
|
|
|
|
HANDLE h;
|
2014-07-12 10:39:13 +02:00
|
|
|
DWORD bytes_written;
|
2012-10-12 01:37:24 +02:00
|
|
|
|
|
|
|
char eof = '\x1a';
|
|
|
|
|
|
|
|
WINE_TRACE("Appending EOF to %s\n", wine_dbgstr_w(filename));
|
|
|
|
h = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
|
|
|
|
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
|
2016-12-05 18:07:07 +01:00
|
|
|
if (h == INVALID_HANDLE_VALUE) {
|
2012-10-12 01:37:24 +02:00
|
|
|
WINE_ERR("Failed to open %s (%d)\n", wine_dbgstr_w(filename), GetLastError());
|
|
|
|
return FALSE;
|
|
|
|
} else {
|
|
|
|
SetFilePointer (h, 0, NULL, FILE_END);
|
2014-07-12 10:39:13 +02:00
|
|
|
if (!WriteFile(h, &eof, 1, &bytes_written, NULL)) {
|
2012-10-12 01:37:24 +02:00
|
|
|
WINE_ERR("Failed to append EOF to %s (%d)\n", wine_dbgstr_w(filename), GetLastError());
|
2012-10-30 17:31:28 +01:00
|
|
|
CloseHandle(h);
|
2012-10-12 01:37:24 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
CloseHandle(h);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-12-05 17:53:23 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_IsSameFile
|
|
|
|
*
|
|
|
|
* Checks if the two paths reference to the same file
|
|
|
|
*/
|
|
|
|
static BOOL WCMD_IsSameFile(const WCHAR *name1, const WCHAR *name2)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
HANDLE file1 = INVALID_HANDLE_VALUE, file2 = INVALID_HANDLE_VALUE;
|
|
|
|
BY_HANDLE_FILE_INFORMATION info1, info2;
|
|
|
|
|
|
|
|
file1 = CreateFileW(name1, 0, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
|
|
|
|
if (file1 == INVALID_HANDLE_VALUE || !GetFileInformationByHandle(file1, &info1))
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
file2 = CreateFileW(name2, 0, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
|
|
|
|
if (file2 == INVALID_HANDLE_VALUE || !GetFileInformationByHandle(file2, &info2))
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
ret = info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
|
|
|
|
&& info1.nFileIndexHigh == info2.nFileIndexHigh
|
|
|
|
&& info1.nFileIndexLow == info2.nFileIndexLow;
|
|
|
|
end:
|
|
|
|
if (file1 != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle(file1);
|
|
|
|
if (file2 != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle(file2);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-10-12 11:23:24 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_ManualCopy
|
|
|
|
*
|
|
|
|
* Copies from a file
|
|
|
|
* optionally reading only until EOF (ascii copy)
|
|
|
|
* optionally appending onto an existing file (append)
|
|
|
|
* Returns TRUE on success
|
|
|
|
*/
|
|
|
|
static BOOL WCMD_ManualCopy(WCHAR *srcname, WCHAR *dstname, BOOL ascii, BOOL append)
|
|
|
|
{
|
|
|
|
HANDLE in,out;
|
|
|
|
BOOL ok;
|
|
|
|
DWORD bytesread, byteswritten;
|
|
|
|
|
2012-11-09 23:36:30 +01:00
|
|
|
WINE_TRACE("Manual Copying %s to %s (append?%d)\n",
|
2012-10-12 11:23:24 +02:00
|
|
|
wine_dbgstr_w(srcname), wine_dbgstr_w(dstname), append);
|
|
|
|
|
|
|
|
in = CreateFileW(srcname, GENERIC_READ, 0, NULL,
|
|
|
|
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
2016-12-05 18:07:07 +01:00
|
|
|
if (in == INVALID_HANDLE_VALUE) {
|
2012-10-12 11:23:24 +02:00
|
|
|
WINE_ERR("Failed to open %s (%d)\n", wine_dbgstr_w(srcname), GetLastError());
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the output file, overwriting if not appending */
|
|
|
|
out = CreateFileW(dstname, GENERIC_WRITE, 0, NULL,
|
|
|
|
append?OPEN_EXISTING:CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
2016-12-05 18:07:07 +01:00
|
|
|
if (out == INVALID_HANDLE_VALUE) {
|
2012-10-12 11:23:24 +02:00
|
|
|
WINE_ERR("Failed to open %s (%d)\n", wine_dbgstr_w(dstname), GetLastError());
|
2012-10-30 17:31:28 +01:00
|
|
|
CloseHandle(in);
|
2012-10-12 11:23:24 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move to end of destination if we are going to append to it */
|
|
|
|
if (append) {
|
|
|
|
SetFilePointer(out, 0, NULL, FILE_END);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop copying data from source to destination until EOF read */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
char buffer[MAXSTRING];
|
|
|
|
|
|
|
|
ok = ReadFile(in, buffer, MAXSTRING, &bytesread, NULL);
|
|
|
|
if (ok) {
|
|
|
|
|
|
|
|
/* Stop at first EOF */
|
|
|
|
if (ascii) {
|
|
|
|
char *ptr = (char *)memchr((void *)buffer, '\x1a', bytesread);
|
|
|
|
if (ptr) bytesread = (ptr - buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytesread) {
|
|
|
|
ok = WriteFile(out, buffer, bytesread, &byteswritten, NULL);
|
|
|
|
if (!ok || byteswritten != bytesread) {
|
|
|
|
WINE_ERR("Unexpected failure writing to %s, rc=%d\n",
|
|
|
|
wine_dbgstr_w(dstname), GetLastError());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
WINE_ERR("Unexpected failure reading from %s, rc=%d\n",
|
|
|
|
wine_dbgstr_w(srcname), GetLastError());
|
|
|
|
}
|
|
|
|
} while (ok && bytesread > 0);
|
|
|
|
|
|
|
|
CloseHandle(out);
|
|
|
|
CloseHandle(in);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
1999-06-06 17:24:04 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_copy
|
|
|
|
*
|
|
|
|
* Copy a file or wildcarded set.
|
2012-10-09 21:11:50 +02:00
|
|
|
* For ascii/binary type copies, it gets complex:
|
|
|
|
* Syntax on command line is
|
|
|
|
* ... /a | /b filename /a /b {[ + filename /a /b]} [dest /a /b]
|
|
|
|
* Where first /a or /b sets 'mode in operation' until another is found
|
2012-10-22 12:20:23 +02:00
|
|
|
* once another is found, it applies to the file preceding the /a or /b
|
2012-10-09 21:11:50 +02:00
|
|
|
* In addition each filename can contain wildcards
|
2012-10-22 12:20:23 +02:00
|
|
|
* To make matters worse, the + may be in the same parameter (i.e. no
|
|
|
|
* whitespace) or with whitespace separating it
|
2012-10-09 21:11:50 +02:00
|
|
|
*
|
|
|
|
* ASCII mode on read == read and stop at first EOF
|
|
|
|
* ASCII mode on write == append EOF to destination
|
|
|
|
* Binary == copy as-is
|
|
|
|
*
|
|
|
|
* Design of this is to build up a list of files which will be copied into a
|
|
|
|
* list, then work through the list file by file.
|
|
|
|
* If no destination is specified, it defaults to the name of the first file in
|
|
|
|
* the list, but the current directory.
|
|
|
|
*
|
1999-06-06 17:24:04 +02:00
|
|
|
*/
|
|
|
|
|
2012-10-17 00:41:58 +02:00
|
|
|
void WCMD_copy(WCHAR * args) {
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2012-10-09 21:11:50 +02:00
|
|
|
BOOL opt_d, opt_v, opt_n, opt_z, opt_y, opt_noty;
|
2012-10-08 22:25:58 +02:00
|
|
|
WCHAR *thisparam;
|
|
|
|
int argno = 0;
|
|
|
|
WCHAR *rawarg;
|
2009-12-09 18:52:40 +01:00
|
|
|
WIN32_FIND_DATAW fd;
|
2012-11-09 23:36:30 +01:00
|
|
|
HANDLE hff = INVALID_HANDLE_VALUE;
|
2012-10-08 22:25:58 +02:00
|
|
|
int binarymode = -1; /* -1 means use the default, 1 is binary, 0 ascii */
|
|
|
|
BOOL concatnextfilename = FALSE; /* True if we have just processed a + */
|
|
|
|
BOOL anyconcats = FALSE; /* Have we found any + options */
|
2012-10-09 21:11:50 +02:00
|
|
|
BOOL appendfirstsource = FALSE; /* Use first found filename as destination */
|
|
|
|
BOOL writtenoneconcat = FALSE; /* Remember when the first concatenated file done */
|
|
|
|
BOOL prompt; /* Prompt before overwriting */
|
|
|
|
WCHAR destname[MAX_PATH]; /* Used in calculating the destination name */
|
|
|
|
BOOL destisdirectory = FALSE; /* Is the destination a directory? */
|
|
|
|
BOOL status;
|
|
|
|
WCHAR copycmd[4];
|
|
|
|
DWORD len;
|
2012-11-09 23:36:30 +01:00
|
|
|
BOOL dstisdevice = FALSE;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2012-10-08 22:25:58 +02:00
|
|
|
typedef struct _COPY_FILES
|
|
|
|
{
|
|
|
|
struct _COPY_FILES *next;
|
|
|
|
BOOL concatenate;
|
|
|
|
WCHAR *name;
|
|
|
|
int binarycopy;
|
|
|
|
} COPY_FILES;
|
|
|
|
COPY_FILES *sourcelist = NULL;
|
|
|
|
COPY_FILES *lastcopyentry = NULL;
|
|
|
|
COPY_FILES *destination = NULL;
|
|
|
|
COPY_FILES *thiscopy = NULL;
|
|
|
|
COPY_FILES *prevcopy = NULL;
|
|
|
|
|
2012-10-09 21:11:50 +02:00
|
|
|
/* Assume we were successful! */
|
|
|
|
errorlevel = 0;
|
|
|
|
|
2012-10-08 22:25:58 +02:00
|
|
|
/* If no args supplied at all, report an error */
|
2006-10-31 21:11:28 +01:00
|
|
|
if (param1[0] == 0x00) {
|
2011-12-02 10:07:27 +01:00
|
|
|
WCMD_output_stderr (WCMD_LoadMessage(WCMD_NOARG));
|
2012-10-09 21:11:50 +02:00
|
|
|
errorlevel = 1;
|
2006-10-31 21:11:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-09 21:11:50 +02:00
|
|
|
opt_d = opt_v = opt_n = opt_z = opt_y = opt_noty = FALSE;
|
2012-10-08 22:25:58 +02:00
|
|
|
|
|
|
|
/* Walk through all args, building up a list of files to process */
|
2012-10-25 23:47:59 +02:00
|
|
|
thisparam = WCMD_parameter(args, argno++, &rawarg, TRUE, FALSE);
|
2012-10-08 22:25:58 +02:00
|
|
|
while (*(thisparam)) {
|
|
|
|
WCHAR *pos1, *pos2;
|
|
|
|
BOOL inquotes;
|
|
|
|
|
|
|
|
WINE_TRACE("Working on parameter '%s'\n", wine_dbgstr_w(thisparam));
|
|
|
|
|
|
|
|
/* Handle switches */
|
|
|
|
if (*thisparam == '/') {
|
|
|
|
while (*thisparam == '/') {
|
|
|
|
thisparam++;
|
2019-05-03 12:28:33 +02:00
|
|
|
if (towupper(*thisparam) == 'D') {
|
2012-10-08 22:25:58 +02:00
|
|
|
opt_d = TRUE;
|
|
|
|
if (opt_d) WINE_FIXME("copy /D support not implemented yet\n");
|
2019-05-03 12:28:33 +02:00
|
|
|
} else if (towupper(*thisparam) == 'Y') {
|
2012-10-09 21:11:50 +02:00
|
|
|
opt_y = TRUE;
|
2019-05-03 12:28:33 +02:00
|
|
|
} else if (towupper(*thisparam) == '-' && towupper(*(thisparam+1)) == 'Y') {
|
2012-10-09 21:11:50 +02:00
|
|
|
opt_noty = TRUE;
|
2019-05-03 12:28:33 +02:00
|
|
|
} else if (towupper(*thisparam) == 'V') {
|
2012-10-08 22:25:58 +02:00
|
|
|
opt_v = TRUE;
|
|
|
|
if (opt_v) WINE_FIXME("copy /V support not implemented yet\n");
|
2019-05-03 12:28:33 +02:00
|
|
|
} else if (towupper(*thisparam) == 'N') {
|
2012-10-08 22:25:58 +02:00
|
|
|
opt_n = TRUE;
|
|
|
|
if (opt_n) WINE_FIXME("copy /N support not implemented yet\n");
|
2019-05-03 12:28:33 +02:00
|
|
|
} else if (towupper(*thisparam) == 'Z') {
|
2012-10-08 22:25:58 +02:00
|
|
|
opt_z = TRUE;
|
|
|
|
if (opt_z) WINE_FIXME("copy /Z support not implemented yet\n");
|
2019-05-03 12:28:33 +02:00
|
|
|
} else if (towupper(*thisparam) == 'A') {
|
2012-10-08 22:25:58 +02:00
|
|
|
if (binarymode != 0) {
|
|
|
|
binarymode = 0;
|
|
|
|
WINE_TRACE("Subsequent files will be handled as ASCII\n");
|
|
|
|
if (destination != NULL) {
|
|
|
|
WINE_TRACE("file %s will be written as ASCII\n", wine_dbgstr_w(destination->name));
|
|
|
|
destination->binarycopy = binarymode;
|
|
|
|
} else if (lastcopyentry != NULL) {
|
|
|
|
WINE_TRACE("file %s will be read as ASCII\n", wine_dbgstr_w(lastcopyentry->name));
|
|
|
|
lastcopyentry->binarycopy = binarymode;
|
|
|
|
}
|
|
|
|
}
|
2019-05-03 12:28:33 +02:00
|
|
|
} else if (towupper(*thisparam) == 'B') {
|
2012-10-08 22:25:58 +02:00
|
|
|
if (binarymode != 1) {
|
|
|
|
binarymode = 1;
|
|
|
|
WINE_TRACE("Subsequent files will be handled as binary\n");
|
|
|
|
if (destination != NULL) {
|
|
|
|
WINE_TRACE("file %s will be written as binary\n", wine_dbgstr_w(destination->name));
|
|
|
|
destination->binarycopy = binarymode;
|
|
|
|
} else if (lastcopyentry != NULL) {
|
|
|
|
WINE_TRACE("file %s will be read as binary\n", wine_dbgstr_w(lastcopyentry->name));
|
|
|
|
lastcopyentry->binarycopy = binarymode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
WINE_FIXME("Unexpected copy switch %s\n", wine_dbgstr_w(thisparam));
|
|
|
|
}
|
|
|
|
thisparam++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This parameter was purely switches, get the next one */
|
2012-10-25 23:47:59 +02:00
|
|
|
thisparam = WCMD_parameter(args, argno++, &rawarg, TRUE, FALSE);
|
2012-10-08 22:25:58 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have found something which is not a switch. If could be anything of the form
|
|
|
|
sourcefilename (which could be destination too)
|
|
|
|
+ (when filename + filename syntex used)
|
|
|
|
sourcefilename+sourcefilename
|
|
|
|
+sourcefilename
|
|
|
|
+/b[tests show windows then ignores to end of parameter]
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (*thisparam=='+') {
|
|
|
|
if (lastcopyentry == NULL) {
|
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_SYNTAXERR));
|
|
|
|
errorlevel = 1;
|
|
|
|
goto exitreturn;
|
|
|
|
} else {
|
|
|
|
concatnextfilename = TRUE;
|
|
|
|
anyconcats = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move to next thing to process */
|
|
|
|
thisparam++;
|
2012-10-17 00:35:33 +02:00
|
|
|
if (*thisparam == 0x00)
|
2012-10-25 23:47:59 +02:00
|
|
|
thisparam = WCMD_parameter(args, argno++, &rawarg, TRUE, FALSE);
|
2012-10-08 22:25:58 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have found something to process - build a COPY_FILE block to store it */
|
2018-11-25 21:56:00 +01:00
|
|
|
thiscopy = heap_xalloc(sizeof(COPY_FILES));
|
2012-10-08 22:25:58 +02:00
|
|
|
|
|
|
|
WINE_TRACE("Not a switch, but probably a filename/list %s\n", wine_dbgstr_w(thisparam));
|
|
|
|
thiscopy->concatenate = concatnextfilename;
|
|
|
|
thiscopy->binarycopy = binarymode;
|
|
|
|
thiscopy->next = NULL;
|
|
|
|
|
|
|
|
/* Time to work out the name. Allocate at least enough space (deliberately too much to
|
|
|
|
leave space to append \* to the end) , then copy in character by character. Strip off
|
|
|
|
quotes if we find them. */
|
2019-05-03 12:28:33 +02:00
|
|
|
len = lstrlenW(thisparam) + (sizeof(WCHAR) * 5); /* 5 spare characters, null + \*.* */
|
2018-11-25 21:56:00 +01:00
|
|
|
thiscopy->name = heap_xalloc(len*sizeof(WCHAR));
|
2012-10-08 22:25:58 +02:00
|
|
|
memset(thiscopy->name, 0x00, len);
|
|
|
|
|
|
|
|
pos1 = thisparam;
|
|
|
|
pos2 = thiscopy->name;
|
|
|
|
inquotes = FALSE;
|
|
|
|
while (*pos1 && (inquotes || (*pos1 != '+' && *pos1 != '/'))) {
|
|
|
|
if (*pos1 == '"') {
|
|
|
|
inquotes = !inquotes;
|
|
|
|
pos1++;
|
|
|
|
} else *pos2++ = *pos1++;
|
|
|
|
}
|
|
|
|
*pos2 = 0;
|
|
|
|
WINE_TRACE("Calculated file name %s\n", wine_dbgstr_w(thiscopy->name));
|
|
|
|
|
|
|
|
/* This is either the first source, concatenated subsequent source or destination */
|
|
|
|
if (sourcelist == NULL) {
|
|
|
|
WINE_TRACE("Adding as first source part\n");
|
|
|
|
sourcelist = thiscopy;
|
|
|
|
lastcopyentry = thiscopy;
|
|
|
|
} else if (concatnextfilename) {
|
|
|
|
WINE_TRACE("Adding to source file list to be concatenated\n");
|
|
|
|
lastcopyentry->next = thiscopy;
|
|
|
|
lastcopyentry = thiscopy;
|
|
|
|
} else if (destination == NULL) {
|
|
|
|
destination = thiscopy;
|
|
|
|
} else {
|
|
|
|
/* We have processed sources and destinations and still found more to do - invalid */
|
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_SYNTAXERR));
|
|
|
|
errorlevel = 1;
|
|
|
|
goto exitreturn;
|
|
|
|
}
|
|
|
|
concatnextfilename = FALSE;
|
|
|
|
|
|
|
|
/* We either need to process the rest of the parameter or move to the next */
|
|
|
|
if (*pos1 == '/' || *pos1 == '+') {
|
|
|
|
thisparam = pos1;
|
|
|
|
continue;
|
|
|
|
} else {
|
2012-10-25 23:47:59 +02:00
|
|
|
thisparam = WCMD_parameter(args, argno++, &rawarg, TRUE, FALSE);
|
2012-10-08 22:25:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure we have at least one source file */
|
|
|
|
if (!sourcelist) {
|
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_SYNTAXERR));
|
|
|
|
errorlevel = 1;
|
|
|
|
goto exitreturn;
|
|
|
|
}
|
|
|
|
|
2012-10-09 21:11:50 +02:00
|
|
|
/* Default whether automatic overwriting is on. If we are interactive then
|
|
|
|
we prompt by default, otherwise we overwrite by default
|
|
|
|
/-Y has the highest priority, then /Y and finally the COPYCMD env. variable */
|
|
|
|
if (opt_noty) prompt = TRUE;
|
|
|
|
else if (opt_y) prompt = FALSE;
|
2007-02-28 14:49:46 +01:00
|
|
|
else {
|
2009-10-04 00:00:59 +02:00
|
|
|
/* By default, we will force the overwrite in batch mode and ask for
|
|
|
|
* confirmation in interactive mode. */
|
2012-10-09 21:11:50 +02:00
|
|
|
prompt = interactive;
|
2009-10-04 00:00:59 +02:00
|
|
|
/* If COPYCMD is set, then we force the overwrite with /Y and ask for
|
|
|
|
* confirmation with /-Y. If COPYCMD is neither of those, then we use the
|
|
|
|
* default behavior. */
|
2020-11-29 23:30:29 +01:00
|
|
|
len = GetEnvironmentVariableW(L"COPYCMD", copycmd, ARRAY_SIZE(copycmd));
|
2018-08-20 21:15:42 +02:00
|
|
|
if (len && len < ARRAY_SIZE(copycmd)) {
|
2020-11-29 23:30:29 +01:00
|
|
|
if (!lstrcmpiW(copycmd, L"/Y"))
|
2012-10-09 21:11:50 +02:00
|
|
|
prompt = FALSE;
|
2020-11-29 23:30:29 +01:00
|
|
|
else if (!lstrcmpiW(copycmd, L"/-Y"))
|
2012-10-09 21:11:50 +02:00
|
|
|
prompt = TRUE;
|
2009-10-04 00:00:59 +02:00
|
|
|
}
|
2007-02-28 14:49:46 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 22:42:49 +01:00
|
|
|
/* Calculate the destination now - if none supplied, it's current dir +
|
2012-10-09 21:11:50 +02:00
|
|
|
filename of first file in list*/
|
|
|
|
if (destination == NULL) {
|
|
|
|
|
|
|
|
WINE_TRACE("No destination supplied, so need to calculate it\n");
|
2020-11-29 23:30:28 +01:00
|
|
|
lstrcpyW(destname, L".");
|
|
|
|
lstrcatW(destname, L"\\");
|
2012-10-09 21:11:50 +02:00
|
|
|
|
2018-11-25 21:56:00 +01:00
|
|
|
destination = heap_xalloc(sizeof(COPY_FILES));
|
2012-10-09 21:11:50 +02:00
|
|
|
if (destination == NULL) goto exitreturn;
|
|
|
|
destination->concatenate = FALSE; /* Not used for destination */
|
|
|
|
destination->binarycopy = binarymode;
|
|
|
|
destination->next = NULL; /* Not used for destination */
|
|
|
|
destination->name = NULL; /* To be filled in */
|
|
|
|
destisdirectory = TRUE;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
WCHAR *filenamepart;
|
|
|
|
DWORD attributes;
|
|
|
|
|
|
|
|
WINE_TRACE("Destination supplied, processing to see if file or directory\n");
|
|
|
|
|
|
|
|
/* Convert to fully qualified path/filename */
|
2018-08-20 21:15:42 +02:00
|
|
|
GetFullPathNameW(destination->name, ARRAY_SIZE(destname), destname, &filenamepart);
|
2012-10-09 21:11:50 +02:00
|
|
|
WINE_TRACE("Full dest name is '%s'\n", wine_dbgstr_w(destname));
|
|
|
|
|
|
|
|
/* If parameter is a directory, ensure it ends in \ */
|
|
|
|
attributes = GetFileAttributesW(destname);
|
2014-10-30 05:11:27 +01:00
|
|
|
if (ends_with_backslash( destname ) ||
|
2012-10-09 21:11:50 +02:00
|
|
|
((attributes != INVALID_FILE_ATTRIBUTES) &&
|
|
|
|
(attributes & FILE_ATTRIBUTE_DIRECTORY))) {
|
|
|
|
|
|
|
|
destisdirectory = TRUE;
|
2020-11-29 23:30:28 +01:00
|
|
|
if (!ends_with_backslash(destname)) lstrcatW(destname, L"\\");
|
2012-10-09 21:11:50 +02:00
|
|
|
WINE_TRACE("Directory, so full name is now '%s'\n", wine_dbgstr_w(destname));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Normally, the destination is the current directory unless we are
|
2014-02-07 22:42:49 +01:00
|
|
|
concatenating, in which case it's current directory plus first filename.
|
2012-10-09 21:11:50 +02:00
|
|
|
Note that if the
|
|
|
|
In addition by default it is a binary copy unless concatenating, when
|
|
|
|
the copy defaults to an ascii copy (stop at EOF). We do not know the
|
|
|
|
first source part yet (until we search) so flag as needing filling in. */
|
|
|
|
|
|
|
|
if (anyconcats) {
|
|
|
|
/* We have found an a+b type syntax, so destination has to be a filename
|
|
|
|
and we need to default to ascii copying. If we have been supplied a
|
|
|
|
directory as the destination, we need to defer calculating the name */
|
|
|
|
if (destisdirectory) appendfirstsource = TRUE;
|
|
|
|
if (destination->binarycopy == -1) destination->binarycopy = 0;
|
|
|
|
|
|
|
|
} else if (!destisdirectory) {
|
|
|
|
/* We have been asked to copy to a filename. Default to ascii IF the
|
|
|
|
source contains wildcards (true even if only one match) */
|
2020-11-29 23:30:29 +01:00
|
|
|
if (wcspbrk(sourcelist->name, L"*?") != NULL) {
|
2012-10-12 11:23:24 +02:00
|
|
|
anyconcats = TRUE; /* We really are concatenating to a single file */
|
|
|
|
if (destination->binarycopy == -1) {
|
2012-10-09 21:11:50 +02:00
|
|
|
destination->binarycopy = 0;
|
2012-10-12 11:23:24 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (destination->binarycopy == -1) {
|
2012-10-09 21:11:50 +02:00
|
|
|
destination->binarycopy = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-07-27 22:43:37 +02:00
|
|
|
|
2012-10-09 21:11:50 +02:00
|
|
|
/* Save away the destination name*/
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(destination->name);
|
2012-11-15 12:32:21 +01:00
|
|
|
destination->name = heap_strdupW(destname);
|
2012-10-09 21:11:50 +02:00
|
|
|
WINE_TRACE("Resolved destination is '%s' (calc later %d)\n",
|
|
|
|
wine_dbgstr_w(destname), appendfirstsource);
|
|
|
|
|
2012-11-09 23:36:30 +01:00
|
|
|
/* Remember if the destination is a device */
|
2020-11-29 23:30:29 +01:00
|
|
|
if (wcsncmp(destination->name, L"\\\\.\\", lstrlenW(L"\\\\.\\")) == 0) {
|
2012-11-09 23:36:30 +01:00
|
|
|
WINE_TRACE("Destination is a device\n");
|
|
|
|
dstisdevice = TRUE;
|
|
|
|
}
|
|
|
|
|
2012-10-09 21:11:50 +02:00
|
|
|
/* Now we need to walk the set of sources, and process each name we come to.
|
|
|
|
If anyconcats is true, we are writing to one file, otherwise we are using
|
|
|
|
the source name each time.
|
|
|
|
If destination exists, prompt for overwrite the first time (if concatenating
|
|
|
|
we ask each time until yes is answered)
|
|
|
|
The first source file we come across must exist (when wildcards expanded)
|
|
|
|
and if concatenating with overwrite prompts, each source file must exist
|
|
|
|
until a yes is answered. */
|
2007-07-27 22:43:37 +02:00
|
|
|
|
2012-10-09 21:11:50 +02:00
|
|
|
thiscopy = sourcelist;
|
|
|
|
prevcopy = NULL;
|
|
|
|
|
|
|
|
while (thiscopy != NULL) {
|
|
|
|
|
|
|
|
WCHAR srcpath[MAX_PATH];
|
2012-11-09 23:36:30 +01:00
|
|
|
const WCHAR *srcname;
|
2012-10-09 21:11:50 +02:00
|
|
|
WCHAR *filenamepart;
|
|
|
|
DWORD attributes;
|
2012-11-09 23:36:30 +01:00
|
|
|
BOOL srcisdevice = FALSE;
|
2012-10-09 21:11:50 +02:00
|
|
|
|
2012-10-12 11:23:24 +02:00
|
|
|
/* If it was not explicit, we now know whether we are concatenating or not and
|
2012-10-09 21:11:50 +02:00
|
|
|
hence whether to copy as binary or ascii */
|
|
|
|
if (thiscopy->binarycopy == -1) thiscopy->binarycopy = !anyconcats;
|
|
|
|
|
|
|
|
/* Convert to fully qualified path/filename in srcpath, file filenamepart pointing
|
2014-06-29 13:12:33 +02:00
|
|
|
to where the filename portion begins (used for wildcard expansion). */
|
2018-08-20 21:15:42 +02:00
|
|
|
GetFullPathNameW(thiscopy->name, ARRAY_SIZE(srcpath), srcpath, &filenamepart);
|
2012-10-09 21:11:50 +02:00
|
|
|
WINE_TRACE("Full src name is '%s'\n", wine_dbgstr_w(srcpath));
|
|
|
|
|
|
|
|
/* If parameter is a directory, ensure it ends in \* */
|
|
|
|
attributes = GetFileAttributesW(srcpath);
|
2014-10-30 05:11:27 +01:00
|
|
|
if (ends_with_backslash( srcpath )) {
|
2012-10-09 21:11:50 +02:00
|
|
|
|
|
|
|
/* We need to know where the filename part starts, so append * and
|
|
|
|
recalculate the full resulting path */
|
2020-11-29 23:30:28 +01:00
|
|
|
lstrcatW(thiscopy->name, L"*");
|
2018-08-20 21:15:42 +02:00
|
|
|
GetFullPathNameW(thiscopy->name, ARRAY_SIZE(srcpath), srcpath, &filenamepart);
|
2012-10-09 21:11:50 +02:00
|
|
|
WINE_TRACE("Directory, so full name is now '%s'\n", wine_dbgstr_w(srcpath));
|
|
|
|
|
2020-11-29 23:30:29 +01:00
|
|
|
} else if ((wcspbrk(srcpath, L"*?") == NULL) &&
|
2012-10-09 21:11:50 +02:00
|
|
|
(attributes != INVALID_FILE_ATTRIBUTES) &&
|
|
|
|
(attributes & FILE_ATTRIBUTE_DIRECTORY)) {
|
|
|
|
|
|
|
|
/* We need to know where the filename part starts, so append \* and
|
|
|
|
recalculate the full resulting path */
|
2020-11-29 23:30:29 +01:00
|
|
|
lstrcatW(thiscopy->name, L"\\*");
|
2018-08-20 21:15:42 +02:00
|
|
|
GetFullPathNameW(thiscopy->name, ARRAY_SIZE(srcpath), srcpath, &filenamepart);
|
2012-10-09 21:11:50 +02:00
|
|
|
WINE_TRACE("Directory, so full name is now '%s'\n", wine_dbgstr_w(srcpath));
|
|
|
|
}
|
|
|
|
|
|
|
|
WINE_TRACE("Copy source (calculated): path: '%s' (Concats: %d)\n",
|
|
|
|
wine_dbgstr_w(srcpath), anyconcats);
|
|
|
|
|
2012-11-09 23:36:30 +01:00
|
|
|
/* If the source is a device, just use it, otherwise search */
|
2020-11-29 23:30:29 +01:00
|
|
|
if (wcsncmp(srcpath, L"\\\\.\\", lstrlenW(L"\\\\.\\")) == 0) {
|
2012-11-09 23:36:30 +01:00
|
|
|
WINE_TRACE("Source is a device\n");
|
|
|
|
srcisdevice = TRUE;
|
|
|
|
srcname = &srcpath[4]; /* After the \\.\ prefix */
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* Loop through all source files */
|
|
|
|
WINE_TRACE("Searching for: '%s'\n", wine_dbgstr_w(srcpath));
|
|
|
|
hff = FindFirstFileW(srcpath, &fd);
|
|
|
|
if (hff != INVALID_HANDLE_VALUE) {
|
|
|
|
srcname = fd.cFileName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (srcisdevice || hff != INVALID_HANDLE_VALUE) {
|
2012-10-09 21:11:50 +02:00
|
|
|
do {
|
|
|
|
WCHAR outname[MAX_PATH];
|
|
|
|
BOOL overwrite;
|
2016-12-05 17:53:23 +01:00
|
|
|
BOOL appendtofirstfile = FALSE;
|
2007-07-27 22:43:37 +02:00
|
|
|
|
|
|
|
/* Skip . and .., and directories */
|
2012-11-09 23:36:30 +01:00
|
|
|
if (!srcisdevice && fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
2007-07-27 22:43:37 +02:00
|
|
|
WINE_TRACE("Skipping directories\n");
|
2012-10-09 21:11:50 +02:00
|
|
|
} else {
|
2007-07-27 22:43:37 +02:00
|
|
|
|
2012-10-09 21:11:50 +02:00
|
|
|
/* Build final destination name */
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(outname, destination->name);
|
|
|
|
if (destisdirectory || appendfirstsource) lstrcatW(outname, srcname);
|
2012-10-09 21:11:50 +02:00
|
|
|
|
|
|
|
/* Build source name */
|
2019-05-03 12:28:33 +02:00
|
|
|
if (!srcisdevice) lstrcpyW(filenamepart, srcname);
|
2012-10-09 21:11:50 +02:00
|
|
|
|
2012-11-09 23:36:30 +01:00
|
|
|
/* Do we just overwrite (we do if we are writing to a device) */
|
2012-10-09 21:11:50 +02:00
|
|
|
overwrite = !prompt;
|
2012-11-09 23:36:30 +01:00
|
|
|
if (dstisdevice || (anyconcats && writtenoneconcat)) {
|
2012-10-09 21:11:50 +02:00
|
|
|
overwrite = TRUE;
|
2007-07-27 22:43:37 +02:00
|
|
|
}
|
|
|
|
|
2012-10-09 21:11:50 +02:00
|
|
|
WINE_TRACE("Copying from : '%s'\n", wine_dbgstr_w(srcpath));
|
|
|
|
WINE_TRACE("Copying to : '%s'\n", wine_dbgstr_w(outname));
|
|
|
|
WINE_TRACE("Flags: srcbinary(%d), dstbinary(%d), over(%d), prompt(%d)\n",
|
|
|
|
thiscopy->binarycopy, destination->binarycopy, overwrite, prompt);
|
|
|
|
|
2016-12-05 17:53:23 +01:00
|
|
|
if (!writtenoneconcat) {
|
|
|
|
appendtofirstfile = anyconcats && WCMD_IsSameFile(srcpath, outname);
|
|
|
|
}
|
|
|
|
|
2012-10-09 21:11:50 +02:00
|
|
|
/* Prompt before overwriting */
|
2016-12-05 17:53:23 +01:00
|
|
|
if (appendtofirstfile) {
|
|
|
|
overwrite = TRUE;
|
|
|
|
} else if (!overwrite) {
|
2012-10-09 21:11:50 +02:00
|
|
|
DWORD attributes = GetFileAttributesW(outname);
|
|
|
|
if (attributes != INVALID_FILE_ATTRIBUTES) {
|
|
|
|
WCHAR* question;
|
|
|
|
question = WCMD_format_string(WCMD_LoadMessage(WCMD_OVERWRITE), outname);
|
|
|
|
overwrite = WCMD_ask_confirm(question, FALSE, NULL);
|
|
|
|
LocalFree(question);
|
|
|
|
}
|
|
|
|
else overwrite = TRUE;
|
|
|
|
}
|
|
|
|
|
2012-11-09 23:36:30 +01:00
|
|
|
/* If we needed to save away the first filename, do it */
|
2012-10-09 21:11:50 +02:00
|
|
|
if (appendfirstsource && overwrite) {
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(destination->name);
|
2012-11-15 12:32:21 +01:00
|
|
|
destination->name = heap_strdupW(outname);
|
2012-10-09 21:11:50 +02:00
|
|
|
WINE_TRACE("Final resolved destination name : '%s'\n", wine_dbgstr_w(outname));
|
|
|
|
appendfirstsource = FALSE;
|
|
|
|
destisdirectory = FALSE;
|
2012-10-08 22:25:58 +02:00
|
|
|
}
|
2007-07-27 22:43:37 +02:00
|
|
|
|
2012-10-09 21:11:50 +02:00
|
|
|
/* Do the copy as appropriate */
|
|
|
|
if (overwrite) {
|
2016-12-05 17:53:23 +01:00
|
|
|
if (anyconcats && WCMD_IsSameFile(srcpath, outname)) {
|
|
|
|
/* Silently skip if the destination file is also a source file */
|
|
|
|
status = TRUE;
|
|
|
|
} else if (anyconcats && writtenoneconcat) {
|
2012-10-09 21:11:50 +02:00
|
|
|
if (thiscopy->binarycopy) {
|
2012-10-12 11:23:24 +02:00
|
|
|
status = WCMD_ManualCopy(srcpath, outname, FALSE, TRUE);
|
2012-10-09 21:11:50 +02:00
|
|
|
} else {
|
2012-10-12 11:23:24 +02:00
|
|
|
status = WCMD_ManualCopy(srcpath, outname, TRUE, TRUE);
|
2012-10-09 21:11:50 +02:00
|
|
|
}
|
|
|
|
} else if (!thiscopy->binarycopy) {
|
2012-10-12 11:23:24 +02:00
|
|
|
status = WCMD_ManualCopy(srcpath, outname, TRUE, FALSE);
|
2012-11-09 23:36:30 +01:00
|
|
|
} else if (srcisdevice) {
|
|
|
|
status = WCMD_ManualCopy(srcpath, outname, FALSE, FALSE);
|
2012-10-12 11:23:24 +02:00
|
|
|
} else {
|
|
|
|
status = CopyFileW(srcpath, outname, FALSE);
|
2012-10-09 21:11:50 +02:00
|
|
|
}
|
|
|
|
if (!status) {
|
|
|
|
WCMD_print_error ();
|
|
|
|
errorlevel = 1;
|
|
|
|
} else {
|
|
|
|
WINE_TRACE("Copied successfully\n");
|
|
|
|
if (anyconcats) writtenoneconcat = TRUE;
|
2012-10-12 01:37:24 +02:00
|
|
|
|
2012-10-12 11:23:24 +02:00
|
|
|
/* Append EOF if ascii destination and we are not going to add more onto the end
|
|
|
|
Note: Testing shows windows has an optimization whereas if you have a binary
|
|
|
|
copy of a file to a single destination (ie concatenation) then it does not add
|
|
|
|
the EOF, hence the check on the source copy type below. */
|
|
|
|
if (!destination->binarycopy && !anyconcats && !thiscopy->binarycopy) {
|
2012-10-12 01:37:24 +02:00
|
|
|
if (!WCMD_AppendEOF(outname)) {
|
|
|
|
WCMD_print_error ();
|
|
|
|
errorlevel = 1;
|
|
|
|
}
|
|
|
|
}
|
2012-10-09 21:11:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-09 23:36:30 +01:00
|
|
|
} while (!srcisdevice && FindNextFileW(hff, &fd) != 0);
|
|
|
|
if (!srcisdevice) FindClose (hff);
|
2012-10-09 21:11:50 +02:00
|
|
|
} else {
|
|
|
|
/* Error if the first file was not found */
|
2015-10-14 01:58:38 +02:00
|
|
|
if (!anyconcats || !writtenoneconcat) {
|
2012-10-09 21:11:50 +02:00
|
|
|
WCMD_print_error ();
|
|
|
|
errorlevel = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Step on to the next supplied source */
|
|
|
|
thiscopy = thiscopy -> next;
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
2012-10-08 22:25:58 +02:00
|
|
|
|
2012-10-09 21:11:50 +02:00
|
|
|
/* Append EOF if ascii destination and we were concatenating */
|
2012-10-12 01:37:24 +02:00
|
|
|
if (!errorlevel && !destination->binarycopy && anyconcats && writtenoneconcat) {
|
|
|
|
if (!WCMD_AppendEOF(destination->name)) {
|
|
|
|
WCMD_print_error ();
|
|
|
|
errorlevel = 1;
|
|
|
|
}
|
2012-10-09 21:11:50 +02:00
|
|
|
}
|
2012-10-08 22:25:58 +02:00
|
|
|
|
2012-10-22 12:20:23 +02:00
|
|
|
/* Exit out of the routine, freeing any remaining allocated memory */
|
2012-10-08 22:25:58 +02:00
|
|
|
exitreturn:
|
|
|
|
|
|
|
|
thiscopy = sourcelist;
|
|
|
|
while (thiscopy != NULL) {
|
|
|
|
prevcopy = thiscopy;
|
|
|
|
/* Free up this block*/
|
|
|
|
thiscopy = thiscopy -> next;
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(prevcopy->name);
|
|
|
|
heap_free(prevcopy);
|
2012-10-08 22:25:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free up the destination memory */
|
|
|
|
if (destination) {
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(destination->name);
|
|
|
|
heap_free(destination);
|
2012-10-08 22:25:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_create_dir
|
|
|
|
*
|
2011-08-02 21:35:36 +02:00
|
|
|
* Create a directory (and, if needed, any intermediate directories).
|
2005-12-03 18:02:15 +01:00
|
|
|
*
|
2011-08-02 21:35:36 +02:00
|
|
|
* Modifies its argument by replacing slashes temporarily with nulls.
|
1999-06-06 17:24:04 +02:00
|
|
|
*/
|
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
static BOOL create_full_path(WCHAR* path)
|
2005-12-03 18:02:15 +01:00
|
|
|
{
|
2011-08-02 21:35:36 +02:00
|
|
|
WCHAR *p, *start;
|
|
|
|
|
|
|
|
/* don't mess with drive letter portion of path, if any */
|
|
|
|
start = path;
|
|
|
|
if (path[1] == ':')
|
|
|
|
start = path+2;
|
|
|
|
|
|
|
|
/* Strip trailing slashes. */
|
2019-05-03 12:28:33 +02:00
|
|
|
for (p = path + lstrlenW(path) - 1; p != start && *p == '\\'; p--)
|
2011-08-02 21:35:36 +02:00
|
|
|
*p = 0;
|
|
|
|
|
|
|
|
/* Step through path, creating intermediate directories as needed. */
|
|
|
|
/* First component includes drive letter, if any. */
|
|
|
|
p = start;
|
|
|
|
for (;;) {
|
|
|
|
DWORD rv;
|
|
|
|
/* Skip to end of component */
|
|
|
|
while (*p == '\\') p++;
|
|
|
|
while (*p && *p != '\\') p++;
|
|
|
|
if (!*p) {
|
|
|
|
/* path is now the original full path */
|
|
|
|
return CreateDirectoryW(path, NULL);
|
2005-12-03 18:02:15 +01:00
|
|
|
}
|
2011-08-02 21:35:36 +02:00
|
|
|
/* Truncate path, create intermediate directory, and restore path */
|
|
|
|
*p = 0;
|
|
|
|
rv = CreateDirectoryW(path, NULL);
|
|
|
|
*p = '\\';
|
|
|
|
if (!rv && GetLastError() != ERROR_ALREADY_EXISTS)
|
|
|
|
return FALSE;
|
2005-12-03 18:02:15 +01:00
|
|
|
}
|
2011-08-02 21:35:36 +02:00
|
|
|
/* notreached */
|
|
|
|
return FALSE;
|
2005-12-03 18:02:15 +01:00
|
|
|
}
|
|
|
|
|
2012-10-17 00:41:58 +02:00
|
|
|
void WCMD_create_dir (WCHAR *args) {
|
2011-08-02 22:02:19 +02:00
|
|
|
int argno = 0;
|
2012-10-17 00:41:58 +02:00
|
|
|
WCHAR *argN = args;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2006-10-31 21:11:28 +01:00
|
|
|
if (param1[0] == 0x00) {
|
2011-09-24 12:50:56 +02:00
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_NOARG));
|
2006-10-31 21:11:28 +01:00
|
|
|
return;
|
|
|
|
}
|
2011-08-02 22:02:19 +02:00
|
|
|
/* Loop through all args */
|
|
|
|
while (TRUE) {
|
2012-10-25 23:47:59 +02:00
|
|
|
WCHAR *thisArg = WCMD_parameter(args, argno++, &argN, FALSE, FALSE);
|
2011-08-02 22:02:19 +02:00
|
|
|
if (!argN) break;
|
|
|
|
if (!create_full_path(thisArg)) {
|
|
|
|
WCMD_print_error ();
|
2011-08-02 21:21:31 +02:00
|
|
|
errorlevel = 1;
|
2011-08-02 22:02:19 +02:00
|
|
|
}
|
|
|
|
}
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
|
2011-05-22 16:59:22 +02:00
|
|
|
/* Parse the /A options given by the user on the commandline
|
|
|
|
* into a bitmask of wanted attributes (*wantSet),
|
|
|
|
* and a bitmask of unwanted attributes (*wantClear).
|
|
|
|
*/
|
|
|
|
static void WCMD_delete_parse_attributes(DWORD *wantSet, DWORD *wantClear) {
|
|
|
|
WCHAR *p;
|
|
|
|
|
|
|
|
/* both are strictly 'out' parameters */
|
|
|
|
*wantSet=0;
|
|
|
|
*wantClear=0;
|
|
|
|
|
|
|
|
/* For each /A argument */
|
2020-11-29 23:30:29 +01:00
|
|
|
for (p=wcsstr(quals, L"/A"); p != NULL; p=wcsstr(p, L"/A")) {
|
2011-05-22 16:59:22 +02:00
|
|
|
/* Skip /A itself */
|
|
|
|
p += 2;
|
|
|
|
|
|
|
|
/* Skip optional : */
|
|
|
|
if (*p == ':') p++;
|
|
|
|
|
|
|
|
/* For each of the attribute specifier chars to this /A option */
|
|
|
|
for (; *p != 0 && *p != '/'; p++) {
|
|
|
|
BOOL negate = FALSE;
|
|
|
|
DWORD mask = 0;
|
|
|
|
|
|
|
|
if (*p == '-') {
|
|
|
|
negate=TRUE;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the attribute specifier to a bit in one of the masks */
|
|
|
|
switch (*p) {
|
|
|
|
case 'R': mask = FILE_ATTRIBUTE_READONLY; break;
|
|
|
|
case 'H': mask = FILE_ATTRIBUTE_HIDDEN; break;
|
|
|
|
case 'S': mask = FILE_ATTRIBUTE_SYSTEM; break;
|
|
|
|
case 'A': mask = FILE_ATTRIBUTE_ARCHIVE; break;
|
|
|
|
default:
|
2011-09-24 12:50:56 +02:00
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_SYNTAXERR));
|
2011-05-22 16:59:22 +02:00
|
|
|
}
|
|
|
|
if (negate)
|
|
|
|
*wantClear |= mask;
|
|
|
|
else
|
|
|
|
*wantSet |= mask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-30 05:19:00 +02:00
|
|
|
/* If filename part of parameter is * or *.*,
|
|
|
|
* and neither /Q nor /P options were given,
|
|
|
|
* prompt the user whether to proceed.
|
|
|
|
* Returns FALSE if user says no, TRUE otherwise.
|
|
|
|
* *pPrompted is set to TRUE if the user is prompted.
|
|
|
|
* (If /P supplied, del will prompt for individual files later.)
|
|
|
|
*/
|
2011-08-20 01:58:59 +02:00
|
|
|
static BOOL WCMD_delete_confirm_wildcard(const WCHAR *filename, BOOL *pPrompted) {
|
2020-11-29 23:30:29 +01:00
|
|
|
if ((wcsstr(quals, L"/Q") == NULL) && (wcsstr(quals, L"/P") == NULL)) {
|
2011-05-30 05:19:00 +02:00
|
|
|
WCHAR drive[10];
|
|
|
|
WCHAR dir[MAX_PATH];
|
|
|
|
WCHAR fname[MAX_PATH];
|
|
|
|
WCHAR ext[MAX_PATH];
|
|
|
|
WCHAR fpath[MAX_PATH];
|
|
|
|
|
|
|
|
/* Convert path into actual directory spec */
|
2018-08-20 21:15:42 +02:00
|
|
|
GetFullPathNameW(filename, ARRAY_SIZE(fpath), fpath, NULL);
|
2020-04-26 05:53:36 +02:00
|
|
|
_wsplitpath(fpath, drive, dir, fname, ext);
|
2011-05-30 05:19:00 +02:00
|
|
|
|
|
|
|
/* Only prompt for * and *.*, not *a, a*, *.a* etc */
|
2020-11-29 23:30:28 +01:00
|
|
|
if ((lstrcmpW(fname, L"*") == 0) && (*ext == 0x00 || (lstrcmpW(ext, L".*") == 0))) {
|
2011-05-30 05:19:00 +02:00
|
|
|
|
|
|
|
WCHAR question[MAXSTRING];
|
|
|
|
|
|
|
|
/* Caller uses this to suppress "file not found" warning later */
|
|
|
|
*pPrompted = TRUE;
|
|
|
|
|
|
|
|
/* Ask for confirmation */
|
2020-11-29 23:30:29 +01:00
|
|
|
wsprintfW(question, L"%s ", fpath);
|
2011-05-30 05:19:00 +02:00
|
|
|
return WCMD_ask_confirm(question, TRUE, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* No scary wildcard, or question suppressed, so it's ok to delete the file(s) */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-06-02 04:12:56 +02:00
|
|
|
/* Helper function for WCMD_delete().
|
|
|
|
* Deletes a single file, directory, or wildcard.
|
|
|
|
* If /S was given, does it recursively.
|
|
|
|
* Returns TRUE if a file was deleted.
|
1999-06-06 17:24:04 +02:00
|
|
|
*/
|
2011-08-20 01:58:59 +02:00
|
|
|
static BOOL WCMD_delete_one (const WCHAR *thisArg) {
|
2011-05-22 16:59:22 +02:00
|
|
|
DWORD wanted_attrs;
|
|
|
|
DWORD unwanted_attrs;
|
2011-06-02 04:12:56 +02:00
|
|
|
BOOL found = FALSE;
|
|
|
|
WCHAR argCopy[MAX_PATH];
|
|
|
|
WIN32_FIND_DATAW fd;
|
|
|
|
HANDLE hff;
|
|
|
|
WCHAR fpath[MAX_PATH];
|
|
|
|
WCHAR *p;
|
|
|
|
BOOL handleParm = TRUE;
|
2011-05-22 16:59:22 +02:00
|
|
|
|
|
|
|
WCMD_delete_parse_attributes(&wanted_attrs, &unwanted_attrs);
|
2007-04-24 00:24:38 +02:00
|
|
|
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(argCopy, thisArg);
|
2011-06-02 04:12:56 +02:00
|
|
|
WINE_TRACE("del: Processing arg %s (quals:%s)\n",
|
|
|
|
wine_dbgstr_w(argCopy), wine_dbgstr_w(quals));
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2011-06-02 04:12:56 +02:00
|
|
|
if (!WCMD_delete_confirm_wildcard(argCopy, &found)) {
|
|
|
|
/* Skip this arg if user declines to delete *.* */
|
|
|
|
return FALSE;
|
|
|
|
}
|
2007-04-24 00:24:38 +02:00
|
|
|
|
2011-06-02 04:12:56 +02:00
|
|
|
/* First, try to delete in the current directory */
|
|
|
|
hff = FindFirstFileW(argCopy, &fd);
|
|
|
|
if (hff == INVALID_HANDLE_VALUE) {
|
|
|
|
handleParm = FALSE;
|
|
|
|
} else {
|
|
|
|
found = TRUE;
|
|
|
|
}
|
2007-02-28 00:19:24 +01:00
|
|
|
|
2011-06-02 04:12:56 +02:00
|
|
|
/* Support del <dirname> by just deleting all files dirname\* */
|
|
|
|
if (handleParm
|
2019-05-03 12:28:33 +02:00
|
|
|
&& (wcschr(argCopy,'*') == NULL)
|
|
|
|
&& (wcschr(argCopy,'?') == NULL)
|
2011-06-02 04:12:56 +02:00
|
|
|
&& (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
|
|
|
|
{
|
|
|
|
WCHAR modifiedParm[MAX_PATH];
|
|
|
|
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(modifiedParm, argCopy);
|
2020-11-29 23:30:29 +01:00
|
|
|
lstrcatW(modifiedParm, L"\\*");
|
2011-06-02 04:12:56 +02:00
|
|
|
FindClose(hff);
|
|
|
|
found = TRUE;
|
|
|
|
WCMD_delete_one(modifiedParm);
|
|
|
|
|
|
|
|
} else if (handleParm) {
|
2007-02-28 00:20:55 +01:00
|
|
|
|
2011-06-02 04:12:56 +02:00
|
|
|
/* Build the filename to delete as <supplied directory>\<findfirst filename> */
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW (fpath, argCopy);
|
2011-06-02 04:12:56 +02:00
|
|
|
do {
|
2019-05-03 12:28:33 +02:00
|
|
|
p = wcsrchr (fpath, '\\');
|
2011-06-02 04:12:56 +02:00
|
|
|
if (p != NULL) {
|
|
|
|
*++p = '\0';
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW (fpath, fd.cFileName);
|
2007-03-04 23:35:09 +01:00
|
|
|
}
|
2019-05-03 12:28:33 +02:00
|
|
|
else lstrcpyW (fpath, fd.cFileName);
|
2011-06-02 04:12:56 +02:00
|
|
|
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
|
|
|
|
BOOL ok;
|
2007-04-24 00:24:38 +02:00
|
|
|
|
2011-06-02 04:12:56 +02:00
|
|
|
/* Handle attribute matching (/A) */
|
|
|
|
ok = ((fd.dwFileAttributes & wanted_attrs) == wanted_attrs)
|
|
|
|
&& ((fd.dwFileAttributes & unwanted_attrs) == 0);
|
|
|
|
|
|
|
|
/* /P means prompt for each file */
|
2020-11-29 23:30:29 +01:00
|
|
|
if (ok && wcsstr(quals, L"/P") != NULL) {
|
2011-12-06 17:40:42 +01:00
|
|
|
WCHAR* question;
|
2007-02-28 00:20:25 +01:00
|
|
|
|
2011-06-02 04:12:56 +02:00
|
|
|
/* Ask for confirmation */
|
2011-12-06 17:40:42 +01:00
|
|
|
question = WCMD_format_string(WCMD_LoadMessage(WCMD_DELPROMPT), fpath);
|
2011-06-02 04:12:56 +02:00
|
|
|
ok = WCMD_ask_confirm(question, FALSE, NULL);
|
2011-12-06 17:40:42 +01:00
|
|
|
LocalFree(question);
|
2011-06-02 04:12:56 +02:00
|
|
|
}
|
2007-02-28 00:20:25 +01:00
|
|
|
|
2011-06-02 04:12:56 +02:00
|
|
|
/* Only proceed if ok to */
|
|
|
|
if (ok) {
|
|
|
|
|
|
|
|
/* If file is read only, and /A:r or /F supplied, delete it */
|
|
|
|
if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY &&
|
|
|
|
((wanted_attrs & FILE_ATTRIBUTE_READONLY) ||
|
2020-11-29 23:30:29 +01:00
|
|
|
wcsstr(quals, L"/F") != NULL)) {
|
2011-06-02 04:12:56 +02:00
|
|
|
SetFileAttributesW(fpath, fd.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY);
|
2007-03-04 23:35:09 +01:00
|
|
|
}
|
2011-06-02 04:12:56 +02:00
|
|
|
|
|
|
|
/* Now do the delete */
|
|
|
|
if (!DeleteFileW(fpath)) WCMD_print_error ();
|
|
|
|
}
|
|
|
|
|
2007-02-28 00:20:05 +01:00
|
|
|
}
|
2011-06-02 04:12:56 +02:00
|
|
|
} while (FindNextFileW(hff, &fd) != 0);
|
|
|
|
FindClose (hff);
|
|
|
|
}
|
2007-04-24 00:24:38 +02:00
|
|
|
|
2011-06-02 04:12:56 +02:00
|
|
|
/* Now recurse into all subdirectories handling the parameter in the same way */
|
2020-11-29 23:30:29 +01:00
|
|
|
if (wcsstr(quals, L"/S") != NULL) {
|
2011-06-02 04:12:56 +02:00
|
|
|
|
|
|
|
WCHAR thisDir[MAX_PATH];
|
|
|
|
int cPos;
|
|
|
|
|
|
|
|
WCHAR drive[10];
|
|
|
|
WCHAR dir[MAX_PATH];
|
|
|
|
WCHAR fname[MAX_PATH];
|
|
|
|
WCHAR ext[MAX_PATH];
|
|
|
|
|
|
|
|
/* Convert path into actual directory spec */
|
2018-08-20 21:15:42 +02:00
|
|
|
GetFullPathNameW(argCopy, ARRAY_SIZE(thisDir), thisDir, NULL);
|
2020-04-26 05:53:36 +02:00
|
|
|
_wsplitpath(thisDir, drive, dir, fname, ext);
|
2011-06-02 04:12:56 +02:00
|
|
|
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(thisDir, drive);
|
|
|
|
lstrcatW(thisDir, dir);
|
|
|
|
cPos = lstrlenW(thisDir);
|
2011-06-02 04:12:56 +02:00
|
|
|
|
|
|
|
WINE_TRACE("Searching recursively in '%s'\n", wine_dbgstr_w(thisDir));
|
|
|
|
|
|
|
|
/* Append '*' to the directory */
|
|
|
|
thisDir[cPos] = '*';
|
|
|
|
thisDir[cPos+1] = 0x00;
|
|
|
|
|
|
|
|
hff = FindFirstFileW(thisDir, &fd);
|
|
|
|
|
|
|
|
/* Remove residual '*' */
|
|
|
|
thisDir[cPos] = 0x00;
|
|
|
|
|
|
|
|
if (hff != INVALID_HANDLE_VALUE) {
|
|
|
|
DIRECTORY_STACK *allDirs = NULL;
|
|
|
|
DIRECTORY_STACK *lastEntry = NULL;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
|
2020-11-29 23:30:28 +01:00
|
|
|
(lstrcmpW(fd.cFileName, L"..") != 0) && (lstrcmpW(fd.cFileName, L".") != 0)) {
|
2011-06-02 04:12:56 +02:00
|
|
|
|
|
|
|
DIRECTORY_STACK *nextDir;
|
|
|
|
WCHAR subParm[MAX_PATH];
|
|
|
|
|
|
|
|
/* Work out search parameter in sub dir */
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW (subParm, thisDir);
|
|
|
|
lstrcatW (subParm, fd.cFileName);
|
2020-11-29 23:30:28 +01:00
|
|
|
lstrcatW (subParm, L"\\");
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW (subParm, fname);
|
|
|
|
lstrcatW (subParm, ext);
|
2011-06-02 04:12:56 +02:00
|
|
|
WINE_TRACE("Recursive, Adding to search list '%s'\n", wine_dbgstr_w(subParm));
|
|
|
|
|
|
|
|
/* Allocate memory, add to list */
|
2018-11-25 21:56:00 +01:00
|
|
|
nextDir = heap_xalloc(sizeof(DIRECTORY_STACK));
|
2011-06-02 04:12:56 +02:00
|
|
|
if (allDirs == NULL) allDirs = nextDir;
|
|
|
|
if (lastEntry != NULL) lastEntry->next = nextDir;
|
|
|
|
lastEntry = nextDir;
|
|
|
|
nextDir->next = NULL;
|
2012-11-15 12:32:21 +01:00
|
|
|
nextDir->dirName = heap_strdupW(subParm);
|
2007-04-24 00:24:38 +02:00
|
|
|
}
|
2011-06-02 04:12:56 +02:00
|
|
|
} while (FindNextFileW(hff, &fd) != 0);
|
|
|
|
FindClose (hff);
|
|
|
|
|
|
|
|
/* Go through each subdir doing the delete */
|
|
|
|
while (allDirs != NULL) {
|
|
|
|
DIRECTORY_STACK *tempDir;
|
|
|
|
|
|
|
|
tempDir = allDirs->next;
|
|
|
|
found |= WCMD_delete_one (allDirs->dirName);
|
|
|
|
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(allDirs->dirName);
|
|
|
|
heap_free(allDirs);
|
2011-06-02 04:12:56 +02:00
|
|
|
allDirs = tempDir;
|
2007-04-24 00:24:38 +02:00
|
|
|
}
|
2011-06-02 04:12:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_delete
|
|
|
|
*
|
|
|
|
* Delete a file or wildcarded set.
|
|
|
|
*
|
|
|
|
* Note on /A:
|
|
|
|
* - Testing shows /A is repeatable, eg. /a-r /ar matches all files
|
|
|
|
* - Each set is a pattern, eg /ahr /as-r means
|
|
|
|
* readonly+hidden OR nonreadonly system files
|
|
|
|
* - The '-' applies to a single field, ie /a:-hr means read only
|
|
|
|
* non-hidden files
|
|
|
|
*/
|
|
|
|
|
2012-10-17 00:41:58 +02:00
|
|
|
BOOL WCMD_delete (WCHAR *args) {
|
2011-06-02 04:12:56 +02:00
|
|
|
int argno;
|
|
|
|
WCHAR *argN;
|
|
|
|
BOOL argsProcessed = FALSE;
|
|
|
|
BOOL foundAny = FALSE;
|
|
|
|
|
|
|
|
errorlevel = 0;
|
|
|
|
|
|
|
|
for (argno=0; ; argno++) {
|
|
|
|
BOOL found;
|
|
|
|
WCHAR *thisArg;
|
|
|
|
|
|
|
|
argN = NULL;
|
2012-10-25 23:47:59 +02:00
|
|
|
thisArg = WCMD_parameter (args, argno, &argN, FALSE, FALSE);
|
2011-06-02 04:12:56 +02:00
|
|
|
if (!argN)
|
|
|
|
break; /* no more parameters */
|
|
|
|
if (argN[0] == '/')
|
|
|
|
continue; /* skip options */
|
|
|
|
|
|
|
|
argsProcessed = TRUE;
|
|
|
|
found = WCMD_delete_one(thisArg);
|
2015-11-17 05:21:39 +01:00
|
|
|
if (!found)
|
2011-09-24 12:50:56 +02:00
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_FILENOTFOUND), thisArg);
|
2007-04-24 00:24:38 +02:00
|
|
|
foundAny |= found;
|
2007-03-04 23:35:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle no valid args */
|
2011-06-02 04:12:56 +02:00
|
|
|
if (!argsProcessed)
|
2011-09-24 12:50:56 +02:00
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_NOARG));
|
2007-04-24 00:24:38 +02:00
|
|
|
|
|
|
|
return foundAny;
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
|
2011-11-11 19:27:43 +01:00
|
|
|
/*
|
|
|
|
* WCMD_strtrim
|
|
|
|
*
|
|
|
|
* Returns a trimmed version of s with all leading and trailing whitespace removed
|
|
|
|
* Pre: s non NULL
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static WCHAR *WCMD_strtrim(const WCHAR *s)
|
|
|
|
{
|
2019-05-03 12:28:33 +02:00
|
|
|
DWORD len = lstrlenW(s);
|
2011-11-11 19:27:43 +01:00
|
|
|
const WCHAR *start = s;
|
|
|
|
WCHAR* result;
|
|
|
|
|
2018-11-25 21:56:00 +01:00
|
|
|
result = heap_xalloc((len + 1) * sizeof(WCHAR));
|
2011-11-11 19:27:43 +01:00
|
|
|
|
2019-05-03 12:28:33 +02:00
|
|
|
while (iswspace(*start)) start++;
|
2011-11-11 19:27:43 +01:00
|
|
|
if (*start) {
|
|
|
|
const WCHAR *end = s + len - 1;
|
2019-05-03 12:28:33 +02:00
|
|
|
while (end > start && iswspace(*end)) end--;
|
2011-11-11 19:27:43 +01:00
|
|
|
memcpy(result, start, (end - start + 2) * sizeof(WCHAR));
|
|
|
|
result[end - start + 1] = '\0';
|
|
|
|
} else {
|
|
|
|
result[0] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-06-06 17:24:04 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_echo
|
|
|
|
*
|
|
|
|
* Echo input to the screen (or not). We don't try to emulate the bugs
|
|
|
|
* in DOS (try typing "ECHO ON AGAIN" for an example).
|
|
|
|
*/
|
|
|
|
|
2012-10-17 00:41:58 +02:00
|
|
|
void WCMD_echo (const WCHAR *args)
|
2011-11-11 19:27:43 +01:00
|
|
|
{
|
2007-03-01 12:43:19 +01:00
|
|
|
int count;
|
2012-10-17 00:41:58 +02:00
|
|
|
const WCHAR *origcommand = args;
|
2011-11-11 19:27:43 +01:00
|
|
|
WCHAR *trimmed;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2012-10-17 00:41:58 +02:00
|
|
|
if ( args[0]==' ' || args[0]=='\t' || args[0]=='.'
|
2015-08-18 15:54:43 +02:00
|
|
|
|| args[0]==':' || args[0]==';' || args[0]=='/')
|
2012-10-17 00:41:58 +02:00
|
|
|
args++;
|
2011-08-31 22:10:57 +02:00
|
|
|
|
2012-10-17 00:41:58 +02:00
|
|
|
trimmed = WCMD_strtrim(args);
|
2011-11-11 19:27:43 +01:00
|
|
|
if (!trimmed) return;
|
|
|
|
|
2019-05-03 12:28:33 +02:00
|
|
|
count = lstrlenW(trimmed);
|
2011-08-31 22:10:57 +02:00
|
|
|
if (count == 0 && origcommand[0]!='.' && origcommand[0]!=':'
|
2015-08-18 15:54:43 +02:00
|
|
|
&& origcommand[0]!=';' && origcommand[0]!='/') {
|
2020-11-29 23:30:29 +01:00
|
|
|
if (echo_mode) WCMD_output(WCMD_LoadMessage(WCMD_ECHOPROMPT), L"ON");
|
|
|
|
else WCMD_output (WCMD_LoadMessage(WCMD_ECHOPROMPT), L"OFF");
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(trimmed);
|
1999-06-06 17:24:04 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-11-11 19:27:43 +01:00
|
|
|
|
2020-11-29 23:30:29 +01:00
|
|
|
if (lstrcmpiW(trimmed, L"ON") == 0)
|
2011-09-16 08:44:06 +02:00
|
|
|
echo_mode = TRUE;
|
2020-11-29 23:30:29 +01:00
|
|
|
else if (lstrcmpiW(trimmed, L"OFF") == 0)
|
2011-09-16 08:44:06 +02:00
|
|
|
echo_mode = FALSE;
|
2011-11-11 19:27:43 +01:00
|
|
|
else {
|
2012-10-17 00:41:58 +02:00
|
|
|
WCMD_output_asis (args);
|
2020-11-29 23:30:28 +01:00
|
|
|
WCMD_output_asis(L"\r\n");
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(trimmed);
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
|
2011-11-18 01:25:45 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* WCMD_part_execute
|
|
|
|
*
|
|
|
|
* Execute a command, and any && or bracketed follow on to the command. The
|
|
|
|
* first command to be executed may not be at the front of the
|
|
|
|
* commands->thiscommand string (eg. it may point after a DO or ELSE)
|
|
|
|
*/
|
|
|
|
static void WCMD_part_execute(CMD_LIST **cmdList, const WCHAR *firstcmd,
|
2012-09-03 00:25:21 +02:00
|
|
|
BOOL isIF, BOOL executecmds)
|
2011-11-18 01:25:45 +01:00
|
|
|
{
|
|
|
|
CMD_LIST *curPosition = *cmdList;
|
|
|
|
int myDepth = (*cmdList)->bracketDepth;
|
|
|
|
|
2018-08-27 21:41:50 +02:00
|
|
|
WINE_TRACE("cmdList(%p), firstCmd(%s), doIt(%d), isIF(%d)\n", cmdList,
|
|
|
|
wine_dbgstr_w(firstcmd), executecmds, isIF);
|
2011-11-18 01:25:45 +01:00
|
|
|
|
|
|
|
/* Skip leading whitespace between condition and the command */
|
|
|
|
while (firstcmd && *firstcmd && (*firstcmd==' ' || *firstcmd=='\t')) firstcmd++;
|
|
|
|
|
|
|
|
/* Process the first command, if there is one */
|
2012-09-03 00:25:21 +02:00
|
|
|
if (executecmds && firstcmd && *firstcmd) {
|
2012-11-15 12:32:21 +01:00
|
|
|
WCHAR *command = heap_strdupW(firstcmd);
|
2012-11-05 22:43:08 +01:00
|
|
|
WCMD_execute (firstcmd, (*cmdList)->redirects, cmdList, FALSE);
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(command);
|
2011-11-18 01:25:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* If it didn't move the position, step to next command */
|
|
|
|
if (curPosition == *cmdList) *cmdList = (*cmdList)->nextcommand;
|
|
|
|
|
|
|
|
/* Process any other parts of the command */
|
|
|
|
if (*cmdList) {
|
2012-09-03 00:25:21 +02:00
|
|
|
BOOL processThese = executecmds;
|
2011-11-18 01:25:45 +01:00
|
|
|
|
|
|
|
while (*cmdList) {
|
|
|
|
static const WCHAR ifElse[] = {'e','l','s','e'};
|
|
|
|
|
|
|
|
/* execute all appropriate commands */
|
|
|
|
curPosition = *cmdList;
|
|
|
|
|
2018-07-16 00:15:29 +02:00
|
|
|
WINE_TRACE("Processing cmdList(%p) - delim(%d) bd(%d / %d) processThese(%d)\n",
|
2011-11-18 01:25:45 +01:00
|
|
|
*cmdList,
|
|
|
|
(*cmdList)->prevDelim,
|
2018-07-16 00:15:29 +02:00
|
|
|
(*cmdList)->bracketDepth,
|
|
|
|
myDepth,
|
|
|
|
processThese);
|
2011-11-18 01:25:45 +01:00
|
|
|
|
|
|
|
/* Execute any statements appended to the line */
|
|
|
|
/* FIXME: Only if previous call worked for && or failed for || */
|
|
|
|
if ((*cmdList)->prevDelim == CMD_ONFAILURE ||
|
|
|
|
(*cmdList)->prevDelim == CMD_ONSUCCESS) {
|
|
|
|
if (processThese && (*cmdList)->command) {
|
2012-11-05 22:43:08 +01:00
|
|
|
WCMD_execute ((*cmdList)->command, (*cmdList)->redirects,
|
|
|
|
cmdList, FALSE);
|
2011-11-18 01:25:45 +01:00
|
|
|
}
|
|
|
|
if (curPosition == *cmdList) *cmdList = (*cmdList)->nextcommand;
|
|
|
|
|
|
|
|
/* Execute any appended to the statement with (...) */
|
|
|
|
} else if ((*cmdList)->bracketDepth > myDepth) {
|
|
|
|
if (processThese) {
|
2012-11-05 22:43:08 +01:00
|
|
|
*cmdList = WCMD_process_commands(*cmdList, TRUE, FALSE);
|
2018-08-27 21:41:50 +02:00
|
|
|
} else {
|
|
|
|
WINE_TRACE("Skipping command %p due to stack depth\n", *cmdList);
|
2011-11-18 01:25:45 +01:00
|
|
|
}
|
|
|
|
if (curPosition == *cmdList) *cmdList = (*cmdList)->nextcommand;
|
|
|
|
|
|
|
|
/* End of the command - does 'ELSE ' follow as the next command? */
|
|
|
|
} else {
|
2018-08-20 21:15:42 +02:00
|
|
|
if (isIF && WCMD_keyword_ws_found(ifElse, ARRAY_SIZE(ifElse), (*cmdList)->command)) {
|
2011-11-18 01:25:45 +01:00
|
|
|
/* Swap between if and else processing */
|
2015-06-24 20:10:04 +02:00
|
|
|
processThese = !executecmds;
|
2011-11-18 01:25:45 +01:00
|
|
|
|
|
|
|
/* Process the ELSE part */
|
|
|
|
if (processThese) {
|
2018-08-20 21:15:42 +02:00
|
|
|
const int keyw_len = ARRAY_SIZE(ifElse) + 1;
|
2011-11-18 01:25:45 +01:00
|
|
|
WCHAR *cmd = ((*cmdList)->command) + keyw_len;
|
|
|
|
|
|
|
|
/* Skip leading whitespace between condition and the command */
|
|
|
|
while (*cmd && (*cmd==' ' || *cmd=='\t')) cmd++;
|
|
|
|
if (*cmd) {
|
2012-11-05 22:43:08 +01:00
|
|
|
WCMD_execute (cmd, (*cmdList)->redirects, cmdList, FALSE);
|
2011-11-18 01:25:45 +01:00
|
|
|
}
|
2018-07-16 00:15:29 +02:00
|
|
|
} else {
|
|
|
|
/* Loop skipping all commands until we get back to the current
|
|
|
|
depth, including skipping commands and their subsequent
|
|
|
|
pipes (eg cmd | prog) */
|
|
|
|
do {
|
|
|
|
*cmdList = (*cmdList)->nextcommand;
|
|
|
|
} while (*cmdList &&
|
|
|
|
((*cmdList)->bracketDepth > myDepth ||
|
|
|
|
(*cmdList)->prevDelim));
|
|
|
|
|
|
|
|
/* After the else is complete, we need to now process subsequent commands */
|
|
|
|
processThese = TRUE;
|
2011-11-18 01:25:45 +01:00
|
|
|
}
|
|
|
|
if (curPosition == *cmdList) *cmdList = (*cmdList)->nextcommand;
|
2018-08-27 21:41:50 +02:00
|
|
|
|
2018-09-14 16:47:32 +02:00
|
|
|
/* If we were in an IF statement and we didn't find an else and yet we get back to
|
2018-08-27 21:41:50 +02:00
|
|
|
the same bracket depth as the IF, then the IF statement is over. This is required
|
|
|
|
to handle nested ifs properly */
|
|
|
|
} else if (isIF && (*cmdList)->bracketDepth == myDepth) {
|
2021-02-08 19:06:43 +01:00
|
|
|
static const WCHAR doW[] = {'d','o'};
|
|
|
|
if (WCMD_keyword_ws_found(doW, ARRAY_SIZE(doW), (*cmdList)->command)) {
|
|
|
|
WINE_TRACE("Still inside FOR-loop, not an end of IF statement\n");
|
|
|
|
*cmdList = (*cmdList)->nextcommand;
|
|
|
|
} else {
|
|
|
|
WINE_TRACE("Found end of this nested IF statement, ending this if\n");
|
|
|
|
break;
|
|
|
|
}
|
2016-11-01 00:57:33 +01:00
|
|
|
} else if (!processThese) {
|
|
|
|
if (curPosition == *cmdList) *cmdList = (*cmdList)->nextcommand;
|
2018-08-27 21:41:50 +02:00
|
|
|
WINE_TRACE("Skipping this command, as in not process mode (next = %p)\n", *cmdList);
|
2011-11-18 01:25:45 +01:00
|
|
|
} else {
|
|
|
|
WINE_TRACE("Found end of this IF statement (next = %p)\n", *cmdList);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-22 00:46:46 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* WCMD_parse_forf_options
|
|
|
|
*
|
|
|
|
* Parses the for /f 'options', extracting the values and validating the
|
|
|
|
* keywords. Note all keywords are optional.
|
|
|
|
* Parameters:
|
|
|
|
* options [I] The unparsed parameter string
|
|
|
|
* eol [O] Set to the comment character (eol=x)
|
|
|
|
* skip [O] Set to the number of lines to skip (skip=xx)
|
|
|
|
* delims [O] Set to the token delimiters (delims=)
|
|
|
|
* tokens [O] Set to the requested tokens, as provided (tokens=)
|
|
|
|
* usebackq [O] Set to TRUE if usebackq found
|
|
|
|
*
|
|
|
|
* Returns TRUE on success, FALSE on syntax error
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static BOOL WCMD_parse_forf_options(WCHAR *options, WCHAR *eol, int *skip,
|
|
|
|
WCHAR *delims, WCHAR *tokens, BOOL *usebackq)
|
|
|
|
{
|
|
|
|
|
|
|
|
WCHAR *pos = options;
|
2019-05-03 12:28:33 +02:00
|
|
|
int len = lstrlenW(pos);
|
2012-10-22 00:46:46 +02:00
|
|
|
static const WCHAR eolW[] = {'e','o','l','='};
|
|
|
|
static const WCHAR skipW[] = {'s','k','i','p','='};
|
|
|
|
static const WCHAR tokensW[] = {'t','o','k','e','n','s','='};
|
|
|
|
static const WCHAR delimsW[] = {'d','e','l','i','m','s','='};
|
|
|
|
static const WCHAR usebackqW[] = {'u','s','e','b','a','c','k','q'};
|
|
|
|
|
|
|
|
/* Initialize to defaults */
|
2020-11-29 23:30:29 +01:00
|
|
|
lstrcpyW(delims, L" \t");
|
|
|
|
lstrcpyW(tokens, L"1");
|
2012-10-22 00:46:46 +02:00
|
|
|
*eol = 0;
|
|
|
|
*skip = 0;
|
|
|
|
*usebackq = FALSE;
|
|
|
|
|
|
|
|
/* Strip (optional) leading and trailing quotes */
|
|
|
|
if ((*pos == '"') && (pos[len-1] == '"')) {
|
|
|
|
pos[len-1] = 0;
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process each keyword */
|
|
|
|
while (pos && *pos) {
|
|
|
|
if (*pos == ' ' || *pos == '\t') {
|
|
|
|
pos++;
|
|
|
|
|
|
|
|
/* Save End of line character (Ignore line if first token (based on delims) starts with it) */
|
|
|
|
} else if (CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
|
2018-08-20 21:15:42 +02:00
|
|
|
pos, ARRAY_SIZE(eolW), eolW, ARRAY_SIZE(eolW)) == CSTR_EQUAL) {
|
|
|
|
*eol = *(pos + ARRAY_SIZE(eolW));
|
|
|
|
pos = pos + ARRAY_SIZE(eolW) + 1;
|
2012-10-22 02:00:11 +02:00
|
|
|
WINE_TRACE("Found eol as %c(%x)\n", *eol, *eol);
|
2012-10-22 00:46:46 +02:00
|
|
|
|
|
|
|
/* Save number of lines to skip (Can be in base 10, hex (0x...) or octal (0xx) */
|
|
|
|
} else if (CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
|
2018-08-20 21:15:42 +02:00
|
|
|
pos, ARRAY_SIZE(skipW), skipW, ARRAY_SIZE(skipW)) == CSTR_EQUAL) {
|
2012-10-22 00:46:46 +02:00
|
|
|
WCHAR *nextchar = NULL;
|
2018-08-20 21:15:42 +02:00
|
|
|
pos = pos + ARRAY_SIZE(skipW);
|
2019-05-03 12:28:33 +02:00
|
|
|
*skip = wcstoul(pos, &nextchar, 0);
|
2012-10-22 00:46:46 +02:00
|
|
|
WINE_TRACE("Found skip as %d lines\n", *skip);
|
|
|
|
pos = nextchar;
|
|
|
|
|
|
|
|
/* Save if usebackq semantics are in effect */
|
2018-08-20 21:15:42 +02:00
|
|
|
} else if (CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT, pos,
|
|
|
|
ARRAY_SIZE(usebackqW), usebackqW, ARRAY_SIZE(usebackqW)) == CSTR_EQUAL) {
|
2012-10-22 00:46:46 +02:00
|
|
|
*usebackq = TRUE;
|
2018-08-20 21:15:42 +02:00
|
|
|
pos = pos + ARRAY_SIZE(usebackqW);
|
2012-10-22 23:22:28 +02:00
|
|
|
WINE_TRACE("Found usebackq\n");
|
2012-10-22 00:46:46 +02:00
|
|
|
|
|
|
|
/* Save the supplied delims. Slightly odd as space can be a delimiter but only
|
|
|
|
if you finish the optionsroot string with delims= otherwise the space is
|
|
|
|
just a token delimiter! */
|
|
|
|
} else if (CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
|
2018-08-20 21:15:42 +02:00
|
|
|
pos, ARRAY_SIZE(delimsW), delimsW, ARRAY_SIZE(delimsW)) == CSTR_EQUAL) {
|
2012-10-22 00:46:46 +02:00
|
|
|
int i=0;
|
|
|
|
|
2018-08-20 21:15:42 +02:00
|
|
|
pos = pos + ARRAY_SIZE(delimsW);
|
2012-10-22 00:46:46 +02:00
|
|
|
while (*pos && *pos != ' ') {
|
|
|
|
delims[i++] = *pos;
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
if (*pos==' ' && *(pos+1)==0) delims[i++] = *pos;
|
|
|
|
delims[i++] = 0; /* Null terminate the delims */
|
2012-10-23 23:05:06 +02:00
|
|
|
WINE_TRACE("Found delims as '%s'\n", wine_dbgstr_w(delims));
|
2012-10-22 00:46:46 +02:00
|
|
|
|
|
|
|
/* Save the tokens being requested */
|
|
|
|
} else if (CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
|
2018-08-20 21:15:42 +02:00
|
|
|
pos, ARRAY_SIZE(tokensW), tokensW, ARRAY_SIZE(tokensW)) == CSTR_EQUAL) {
|
2012-10-22 00:46:46 +02:00
|
|
|
int i=0;
|
|
|
|
|
2018-08-20 21:15:42 +02:00
|
|
|
pos = pos + ARRAY_SIZE(tokensW);
|
2012-10-22 00:46:46 +02:00
|
|
|
while (*pos && *pos != ' ') {
|
|
|
|
tokens[i++] = *pos;
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
tokens[i++] = 0; /* Null terminate the tokens */
|
2012-11-08 22:53:08 +01:00
|
|
|
WINE_TRACE("Found tokens as '%s'\n", wine_dbgstr_w(tokens));
|
2012-10-22 00:46:46 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
WINE_WARN("Unexpected data in optionsroot: '%s'\n", wine_dbgstr_w(pos));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-10-22 01:10:10 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* WCMD_add_dirstowalk
|
|
|
|
*
|
|
|
|
* When recursing through directories (for /r), we need to add to the list of
|
|
|
|
* directories still to walk, any subdirectories of the one we are processing.
|
|
|
|
*
|
|
|
|
* Parameters
|
|
|
|
* options [I] The remaining list of directories still to process
|
|
|
|
*
|
|
|
|
* Note this routine inserts the subdirectories found between the entry being
|
2015-03-18 08:44:56 +01:00
|
|
|
* processed, and any other directory still to be processed, mimicking what
|
2012-10-22 01:10:10 +02:00
|
|
|
* Windows does
|
|
|
|
*/
|
|
|
|
static void WCMD_add_dirstowalk(DIRECTORY_STACK *dirsToWalk) {
|
|
|
|
DIRECTORY_STACK *remainingDirs = dirsToWalk;
|
|
|
|
WCHAR fullitem[MAX_PATH];
|
|
|
|
WIN32_FIND_DATAW fd;
|
|
|
|
HANDLE hff;
|
|
|
|
|
|
|
|
/* Build a generic search and add all directories on the list of directories
|
|
|
|
still to walk */
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(fullitem, dirsToWalk->dirName);
|
2020-11-29 23:30:29 +01:00
|
|
|
lstrcatW(fullitem, L"\\*");
|
2012-10-22 01:10:10 +02:00
|
|
|
hff = FindFirstFileW(fullitem, &fd);
|
|
|
|
if (hff != INVALID_HANDLE_VALUE) {
|
|
|
|
do {
|
|
|
|
WINE_TRACE("Looking for subdirectories\n");
|
|
|
|
if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
|
2020-11-29 23:30:28 +01:00
|
|
|
(lstrcmpW(fd.cFileName, L"..") != 0) && (lstrcmpW(fd.cFileName, L".") != 0))
|
2012-10-22 01:10:10 +02:00
|
|
|
{
|
|
|
|
/* Allocate memory, add to list */
|
2018-11-25 21:56:00 +01:00
|
|
|
DIRECTORY_STACK *toWalk = heap_xalloc(sizeof(DIRECTORY_STACK));
|
2012-10-22 01:10:10 +02:00
|
|
|
WINE_TRACE("(%p->%p)\n", remainingDirs, remainingDirs->next);
|
|
|
|
toWalk->next = remainingDirs->next;
|
|
|
|
remainingDirs->next = toWalk;
|
|
|
|
remainingDirs = toWalk;
|
2019-05-03 12:28:33 +02:00
|
|
|
toWalk->dirName = heap_xalloc(sizeof(WCHAR) * (lstrlenW(dirsToWalk->dirName) + 2 + lstrlenW(fd.cFileName)));
|
|
|
|
lstrcpyW(toWalk->dirName, dirsToWalk->dirName);
|
2020-11-29 23:30:28 +01:00
|
|
|
lstrcatW(toWalk->dirName, L"\\");
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW(toWalk->dirName, fd.cFileName);
|
2012-10-22 01:10:10 +02:00
|
|
|
WINE_TRACE("Added to stack %s (%p->%p)\n", wine_dbgstr_w(toWalk->dirName),
|
|
|
|
toWalk, toWalk->next);
|
|
|
|
}
|
|
|
|
} while (FindNextFileW(hff, &fd) != 0);
|
|
|
|
WINE_TRACE("Finished adding all subdirectories\n");
|
|
|
|
FindClose (hff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-08 22:53:08 +01:00
|
|
|
/**************************************************************************
|
|
|
|
* WCMD_for_nexttoken
|
|
|
|
*
|
|
|
|
* Parse the token= line, identifying the next highest number not processed
|
|
|
|
* so far. Count how many tokens are referred (including duplicates) and
|
|
|
|
* optionally return that, plus optionally indicate if the tokens= line
|
|
|
|
* ends in a star.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* lasttoken [I] - Identifies the token index of the last one
|
|
|
|
* returned so far (-1 used for first loop)
|
|
|
|
* tokenstr [I] - The specified tokens= line
|
|
|
|
* firstCmd [O] - Optionally indicate how many tokens are listed
|
|
|
|
* doAll [O] - Optionally indicate if line ends with *
|
|
|
|
* duplicates [O] - Optionally indicate if there is any evidence of
|
|
|
|
* overlaying tokens in the string
|
|
|
|
* Note the caller should keep a running track of duplicates as the tokens
|
|
|
|
* are recursively passed. If any have duplicates, then the * token should
|
|
|
|
* not be honoured.
|
|
|
|
*/
|
|
|
|
static int WCMD_for_nexttoken(int lasttoken, WCHAR *tokenstr,
|
|
|
|
int *totalfound, BOOL *doall,
|
|
|
|
BOOL *duplicates)
|
|
|
|
{
|
|
|
|
WCHAR *pos = tokenstr;
|
|
|
|
int nexttoken = -1;
|
|
|
|
|
|
|
|
if (totalfound) *totalfound = 0;
|
|
|
|
if (doall) *doall = FALSE;
|
|
|
|
if (duplicates) *duplicates = FALSE;
|
|
|
|
|
2018-09-11 00:30:19 +02:00
|
|
|
WINE_TRACE("Find next token after %d in %s\n", lasttoken,
|
|
|
|
wine_dbgstr_w(tokenstr));
|
2012-11-08 22:53:08 +01:00
|
|
|
|
|
|
|
/* Loop through the token string, parsing it. Valid syntax is:
|
|
|
|
token=m or x-y with comma delimiter and optionally * to finish*/
|
|
|
|
while (*pos) {
|
|
|
|
int nextnumber1, nextnumber2 = -1;
|
|
|
|
WCHAR *nextchar;
|
|
|
|
|
2018-09-11 00:30:19 +02:00
|
|
|
/* Remember if the next character is a star, it indicates a need to
|
|
|
|
show all remaining tokens and should be the last character */
|
2018-07-16 00:15:27 +02:00
|
|
|
if (*pos == '*') {
|
|
|
|
if (doall) *doall = TRUE;
|
|
|
|
if (totalfound) (*totalfound)++;
|
2018-09-11 00:30:19 +02:00
|
|
|
/* If we have not found a next token to return, then indicate
|
|
|
|
time to process the star */
|
|
|
|
if (nexttoken == -1) {
|
|
|
|
if (lasttoken == -1) {
|
|
|
|
/* Special case the syntax of tokens=* which just means get whole line */
|
|
|
|
nexttoken = 0;
|
|
|
|
} else {
|
|
|
|
nexttoken = lasttoken;
|
|
|
|
}
|
|
|
|
}
|
2018-07-16 00:15:27 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-11-08 22:53:08 +01:00
|
|
|
/* Get the next number */
|
2019-05-03 12:28:33 +02:00
|
|
|
nextnumber1 = wcstoul(pos, &nextchar, 10);
|
2012-11-08 22:53:08 +01:00
|
|
|
|
2014-02-07 22:42:49 +01:00
|
|
|
/* If it is followed by a minus, it's a range, so get the next one as well */
|
2012-11-08 22:53:08 +01:00
|
|
|
if (*nextchar == '-') {
|
2019-05-03 12:28:33 +02:00
|
|
|
nextnumber2 = wcstoul(nextchar+1, &nextchar, 10);
|
2012-11-08 22:53:08 +01:00
|
|
|
|
|
|
|
/* We want to return the lowest number that is higher than lasttoken
|
|
|
|
but only if range is positive */
|
|
|
|
if (nextnumber2 >= nextnumber1 &&
|
|
|
|
lasttoken < nextnumber2) {
|
|
|
|
|
|
|
|
int nextvalue;
|
|
|
|
if (nexttoken == -1) {
|
|
|
|
nextvalue = max(nextnumber1, (lasttoken+1));
|
|
|
|
} else {
|
|
|
|
nextvalue = min(nexttoken, max(nextnumber1, (lasttoken+1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Flag if duplicates identified */
|
|
|
|
if (nexttoken == nextvalue && duplicates) *duplicates = TRUE;
|
|
|
|
|
|
|
|
nexttoken = nextvalue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the running total for the whole range */
|
|
|
|
if (nextnumber2 >= nextnumber1 && totalfound) {
|
|
|
|
*totalfound = *totalfound + 1 + (nextnumber2 - nextnumber1);
|
|
|
|
}
|
2018-09-11 00:30:19 +02:00
|
|
|
pos = nextchar;
|
2012-11-08 22:53:08 +01:00
|
|
|
|
2018-09-11 00:30:19 +02:00
|
|
|
} else if (pos != nextchar) {
|
2012-11-08 22:53:08 +01:00
|
|
|
if (totalfound) (*totalfound)++;
|
|
|
|
|
|
|
|
/* See if the number found is one we have already seen */
|
|
|
|
if (nextnumber1 == nexttoken && duplicates) *duplicates = TRUE;
|
|
|
|
|
|
|
|
/* We want to return the lowest number that is higher than lasttoken */
|
|
|
|
if (lasttoken < nextnumber1 &&
|
|
|
|
((nexttoken == -1) || (nextnumber1 < nexttoken))) {
|
|
|
|
nexttoken = nextnumber1;
|
|
|
|
}
|
2018-09-11 00:30:19 +02:00
|
|
|
pos = nextchar;
|
2012-11-08 22:53:08 +01:00
|
|
|
|
2018-09-11 00:30:19 +02:00
|
|
|
} else {
|
|
|
|
/* Step on to the next character, usually over comma */
|
|
|
|
if (*pos) pos++;
|
2012-11-08 22:53:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return result */
|
2018-09-11 00:30:19 +02:00
|
|
|
if (nexttoken == -1) {
|
|
|
|
WINE_TRACE("No next token found, previous was %d\n", lasttoken);
|
|
|
|
nexttoken = lasttoken;
|
|
|
|
} else if (nexttoken==lasttoken && doall && *doall) {
|
|
|
|
WINE_TRACE("Request for all remaining tokens now\n");
|
|
|
|
} else {
|
|
|
|
WINE_TRACE("Found next token after %d was %d\n", lasttoken, nexttoken);
|
|
|
|
}
|
|
|
|
if (totalfound) WINE_TRACE("Found total tokens to be %d\n", *totalfound);
|
2012-11-08 22:53:08 +01:00
|
|
|
if (duplicates && *duplicates) WINE_TRACE("Duplicate numbers found\n");
|
|
|
|
return nexttoken;
|
|
|
|
}
|
|
|
|
|
2012-10-22 01:53:30 +02:00
|
|
|
/**************************************************************************
|
|
|
|
* WCMD_parse_line
|
|
|
|
*
|
|
|
|
* When parsing file or string contents (for /f), once the string to parse
|
2012-10-22 02:00:11 +02:00
|
|
|
* has been identified, handle the various options and call the do part
|
2012-10-22 01:53:30 +02:00
|
|
|
* if appropriate.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* cmdStart [I] - Identifies the list of commands making up the
|
|
|
|
* for loop body (especially if brackets in use)
|
|
|
|
* firstCmd [I] - The textual start of the command after the DO
|
|
|
|
* which is within the first item of cmdStart
|
|
|
|
* cmdEnd [O] - Identifies where to continue after the DO
|
|
|
|
* variable [I] - The variable identified on the for line
|
|
|
|
* buffer [I] - The string to parse
|
|
|
|
* doExecuted [O] - Set to TRUE if the DO is ever executed once
|
|
|
|
* forf_skip [I/O] - How many lines to skip first
|
2012-10-22 02:00:11 +02:00
|
|
|
* forf_eol [I] - The 'end of line' (comment) character
|
2012-10-23 23:05:06 +02:00
|
|
|
* forf_delims [I] - The delimiters to use when breaking the string apart
|
2012-11-08 22:53:08 +01:00
|
|
|
* forf_tokens [I] - The tokens to use when breaking the string apart
|
2012-10-22 01:53:30 +02:00
|
|
|
*/
|
2012-10-22 02:00:11 +02:00
|
|
|
static void WCMD_parse_line(CMD_LIST *cmdStart,
|
2012-10-22 01:53:30 +02:00
|
|
|
const WCHAR *firstCmd,
|
2012-10-22 02:00:11 +02:00
|
|
|
CMD_LIST **cmdEnd,
|
2012-11-05 22:43:08 +01:00
|
|
|
const WCHAR variable,
|
2012-10-22 02:00:11 +02:00
|
|
|
WCHAR *buffer,
|
|
|
|
BOOL *doExecuted,
|
|
|
|
int *forf_skip,
|
2012-10-23 23:05:06 +02:00
|
|
|
WCHAR forf_eol,
|
2012-11-08 22:53:08 +01:00
|
|
|
WCHAR *forf_delims,
|
|
|
|
WCHAR *forf_tokens) {
|
2012-10-22 01:53:30 +02:00
|
|
|
|
2012-11-08 22:53:08 +01:00
|
|
|
WCHAR *parm;
|
2012-11-05 22:43:08 +01:00
|
|
|
FOR_CONTEXT oldcontext;
|
2012-11-08 22:53:08 +01:00
|
|
|
int varidx, varoffset;
|
|
|
|
int nexttoken, lasttoken = -1;
|
|
|
|
BOOL starfound = FALSE;
|
|
|
|
BOOL thisduplicate = FALSE;
|
|
|
|
BOOL anyduplicates = FALSE;
|
|
|
|
int totalfound;
|
2020-11-29 23:30:27 +01:00
|
|
|
static WCHAR emptyW[] = L"";
|
2012-10-22 01:53:30 +02:00
|
|
|
|
|
|
|
/* Skip lines if requested */
|
|
|
|
if (*forf_skip) {
|
|
|
|
(*forf_skip)--;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-05 22:43:08 +01:00
|
|
|
/* Save away any existing for variable context (e.g. nested for loops) */
|
|
|
|
oldcontext = forloopcontext;
|
|
|
|
|
2012-11-08 22:53:08 +01:00
|
|
|
/* Extract the parameters based on the tokens= value (There will always
|
|
|
|
be some value, as if it is not supplied, it defaults to tokens=1).
|
|
|
|
Rough logic:
|
|
|
|
Count how many tokens are named in the line, identify the lowest
|
|
|
|
Empty (set to null terminated string) that number of named variables
|
|
|
|
While lasttoken != nextlowest
|
|
|
|
%letter = parameter number 'nextlowest'
|
|
|
|
letter++ (if >26 or >52 abort)
|
|
|
|
Go through token= string finding next lowest number
|
|
|
|
If token ends in * set %letter = raw position of token(nextnumber+1)
|
|
|
|
*/
|
|
|
|
lasttoken = -1;
|
|
|
|
nexttoken = WCMD_for_nexttoken(lasttoken, forf_tokens, &totalfound,
|
2018-07-16 00:15:27 +02:00
|
|
|
&starfound, &thisduplicate);
|
2012-11-05 22:43:08 +01:00
|
|
|
varidx = FOR_VAR_IDX(variable);
|
|
|
|
|
2012-11-08 22:53:08 +01:00
|
|
|
/* Empty out variables */
|
|
|
|
for (varoffset=0;
|
2018-07-16 00:15:27 +02:00
|
|
|
varidx >= 0 && varoffset<totalfound && (((varidx%26) + varoffset) < 26);
|
2012-11-08 22:53:08 +01:00
|
|
|
varoffset++) {
|
2020-11-29 23:30:27 +01:00
|
|
|
forloopcontext.variable[varidx + varoffset] = emptyW;
|
2012-11-08 22:53:08 +01:00
|
|
|
}
|
|
|
|
|
2018-07-16 00:15:27 +02:00
|
|
|
/* Loop extracting the tokens
|
|
|
|
Note: nexttoken of 0 means there were no tokens requested, to handle
|
|
|
|
the special case of tokens=* */
|
2012-11-08 22:53:08 +01:00
|
|
|
varoffset = 0;
|
|
|
|
WINE_TRACE("Parsing buffer into tokens: '%s'\n", wine_dbgstr_w(buffer));
|
2018-07-16 00:15:27 +02:00
|
|
|
while (varidx >= 0 && (nexttoken > 0 && (nexttoken > lasttoken))) {
|
2012-11-08 22:53:08 +01:00
|
|
|
anyduplicates |= thisduplicate;
|
|
|
|
|
|
|
|
/* Extract the token number requested and set into the next variable context */
|
2018-02-03 17:13:11 +01:00
|
|
|
parm = WCMD_parameter_with_delims(buffer, (nexttoken-1), NULL, TRUE, FALSE, forf_delims);
|
2012-11-08 22:53:08 +01:00
|
|
|
WINE_TRACE("Parsed token %d(%d) as parameter %s\n", nexttoken,
|
|
|
|
varidx + varoffset, wine_dbgstr_w(parm));
|
|
|
|
if (varidx >=0) {
|
2018-07-16 00:15:27 +02:00
|
|
|
if (parm) forloopcontext.variable[varidx + varoffset] = heap_strdupW(parm);
|
2012-11-08 22:53:08 +01:00
|
|
|
varoffset++;
|
2018-07-16 00:15:27 +02:00
|
|
|
if (((varidx%26)+varoffset) >= 26) break;
|
2012-11-08 22:53:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the next token */
|
|
|
|
lasttoken = nexttoken;
|
|
|
|
nexttoken = WCMD_for_nexttoken(lasttoken, forf_tokens, NULL,
|
|
|
|
&starfound, &thisduplicate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If all the rest of the tokens were requested, and there is still space in
|
|
|
|
the variable range, write them now */
|
2018-07-16 00:15:27 +02:00
|
|
|
if (!anyduplicates && starfound && varidx >= 0 && (((varidx%26) + varoffset) < 26)) {
|
2012-11-08 22:53:08 +01:00
|
|
|
nexttoken++;
|
|
|
|
WCMD_parameter_with_delims(buffer, (nexttoken-1), &parm, FALSE, FALSE, forf_delims);
|
|
|
|
WINE_TRACE("Parsed allremaining tokens (%d) as parameter %s\n",
|
|
|
|
varidx + varoffset, wine_dbgstr_w(parm));
|
2018-07-16 00:15:27 +02:00
|
|
|
if (parm) forloopcontext.variable[varidx + varoffset] = heap_strdupW(parm);
|
2012-11-08 22:53:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Execute the body of the foor loop with these values */
|
|
|
|
if (forloopcontext.variable[varidx] && forloopcontext.variable[varidx][0] != forf_eol) {
|
2012-10-22 01:53:30 +02:00
|
|
|
CMD_LIST *thisCmdStart = cmdStart;
|
|
|
|
*doExecuted = TRUE;
|
2012-11-05 22:43:08 +01:00
|
|
|
WCMD_part_execute(&thisCmdStart, firstCmd, FALSE, TRUE);
|
2012-10-22 01:53:30 +02:00
|
|
|
*cmdEnd = thisCmdStart;
|
|
|
|
}
|
|
|
|
|
2012-11-08 22:53:08 +01:00
|
|
|
/* Free the duplicated strings, and restore the context */
|
|
|
|
if (varidx >=0) {
|
|
|
|
int i;
|
|
|
|
for (i=varidx; i<MAX_FOR_VARIABLES; i++) {
|
|
|
|
if ((forloopcontext.variable[i] != oldcontext.variable[i]) &&
|
2020-11-29 23:30:27 +01:00
|
|
|
(forloopcontext.variable[i] != emptyW)) {
|
2012-11-08 22:53:08 +01:00
|
|
|
heap_free(forloopcontext.variable[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-05 22:43:08 +01:00
|
|
|
|
|
|
|
/* Restore the original for variable contextx */
|
|
|
|
forloopcontext = oldcontext;
|
2012-10-22 01:53:30 +02:00
|
|
|
}
|
|
|
|
|
2012-10-22 23:22:28 +02:00
|
|
|
/**************************************************************************
|
|
|
|
* WCMD_forf_getinputhandle
|
|
|
|
*
|
|
|
|
* Return a file handle which can be used for reading the input lines,
|
|
|
|
* either to a specific file (which may be quote delimited as we have to
|
|
|
|
* read the parameters in raw mode) or to a command which we need to
|
|
|
|
* execute. The command being executed runs in its own shell and stores
|
|
|
|
* its data in a temporary file.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* usebackq [I] - Indicates whether usebackq is in effect or not
|
|
|
|
* itemStr [I] - The item to be handled, either a filename or
|
|
|
|
* whole command string to execute
|
|
|
|
* iscmd [I] - Identifies whether this is a command or not
|
|
|
|
*
|
|
|
|
* Returns a file handle which can be used to read the input lines from.
|
|
|
|
*/
|
2012-10-28 19:50:10 +01:00
|
|
|
static HANDLE WCMD_forf_getinputhandle(BOOL usebackq, WCHAR *itemstr, BOOL iscmd) {
|
2012-10-22 23:22:28 +02:00
|
|
|
WCHAR temp_str[MAX_PATH];
|
|
|
|
WCHAR temp_file[MAX_PATH];
|
|
|
|
WCHAR temp_cmd[MAXSTRING];
|
2018-09-11 00:30:18 +02:00
|
|
|
WCHAR *trimmed = NULL;
|
2012-10-22 23:22:28 +02:00
|
|
|
HANDLE hinput = INVALID_HANDLE_VALUE;
|
|
|
|
|
2018-09-11 00:30:18 +02:00
|
|
|
/* Remove leading and trailing character (but there may be trailing whitespace too) */
|
2012-10-22 23:22:28 +02:00
|
|
|
if ((iscmd && (itemstr[0] == '`' && usebackq)) ||
|
|
|
|
(iscmd && (itemstr[0] == '\'' && !usebackq)) ||
|
|
|
|
(!iscmd && (itemstr[0] == '"' && usebackq)))
|
|
|
|
{
|
2018-09-11 00:30:18 +02:00
|
|
|
trimmed = WCMD_strtrim(itemstr);
|
|
|
|
if (trimmed) {
|
|
|
|
itemstr = trimmed;
|
|
|
|
}
|
2019-05-03 12:28:33 +02:00
|
|
|
itemstr[lstrlenW(itemstr)-1] = 0x00;
|
2012-10-22 23:22:28 +02:00
|
|
|
itemstr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iscmd) {
|
|
|
|
/* Get temp filename */
|
2018-08-20 21:15:42 +02:00
|
|
|
GetTempPathW(ARRAY_SIZE(temp_str), temp_str);
|
2020-11-29 23:30:29 +01:00
|
|
|
GetTempFileNameW(temp_str, L"CMD", 0, temp_file);
|
2012-10-22 23:22:28 +02:00
|
|
|
|
|
|
|
/* Redirect output to the temporary file */
|
2020-11-29 23:30:29 +01:00
|
|
|
wsprintfW(temp_str, L">%s", temp_file);
|
|
|
|
wsprintfW(temp_cmd, L"CMD.EXE /C %s", itemstr);
|
2012-10-22 23:22:28 +02:00
|
|
|
WINE_TRACE("Issuing '%s' with redirs '%s'\n",
|
|
|
|
wine_dbgstr_w(temp_cmd), wine_dbgstr_w(temp_str));
|
2012-11-05 22:43:08 +01:00
|
|
|
WCMD_execute (temp_cmd, temp_str, NULL, FALSE);
|
2012-10-22 23:22:28 +02:00
|
|
|
|
|
|
|
/* Open the file, read line by line and process */
|
|
|
|
hinput = CreateFileW(temp_file, GENERIC_READ, FILE_SHARE_READ,
|
|
|
|
NULL, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, NULL);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* Open the file, read line by line and process */
|
|
|
|
WINE_TRACE("Reading input to parse from '%s'\n", wine_dbgstr_w(itemstr));
|
|
|
|
hinput = CreateFileW(itemstr, GENERIC_READ, FILE_SHARE_READ,
|
|
|
|
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
}
|
2018-09-11 00:30:18 +02:00
|
|
|
heap_free(trimmed);
|
2012-10-22 23:22:28 +02:00
|
|
|
return hinput;
|
|
|
|
}
|
|
|
|
|
1999-07-10 13:36:45 +02:00
|
|
|
/**************************************************************************
|
1999-06-06 17:24:04 +02:00
|
|
|
* WCMD_for
|
|
|
|
*
|
|
|
|
* Batch file loop processing.
|
2007-06-15 21:59:28 +02:00
|
|
|
*
|
|
|
|
* On entry: cmdList contains the syntax up to the set
|
|
|
|
* next cmdList and all in that bracket contain the set data
|
|
|
|
* next cmdlist contains the DO cmd
|
|
|
|
* following that is either brackets or && entries (as per if)
|
|
|
|
*
|
1999-06-06 17:24:04 +02:00
|
|
|
*/
|
|
|
|
|
2007-06-15 21:59:21 +02:00
|
|
|
void WCMD_for (WCHAR *p, CMD_LIST **cmdList) {
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2009-12-09 18:52:40 +01:00
|
|
|
WIN32_FIND_DATAW fd;
|
2007-03-01 12:43:19 +01:00
|
|
|
HANDLE hff;
|
|
|
|
int i;
|
2011-08-25 23:08:30 +02:00
|
|
|
static const WCHAR inW[] = {'i','n'};
|
|
|
|
static const WCHAR doW[] = {'d','o'};
|
2007-06-15 21:59:28 +02:00
|
|
|
CMD_LIST *setStart, *thisSet, *cmdStart, *cmdEnd;
|
|
|
|
WCHAR variable[4];
|
2012-11-05 22:43:08 +01:00
|
|
|
int varidx = -1;
|
|
|
|
WCHAR *oldvariablevalue;
|
2007-06-15 21:59:28 +02:00
|
|
|
WCHAR *firstCmd;
|
|
|
|
int thisDepth;
|
2012-09-27 20:58:56 +02:00
|
|
|
WCHAR optionsRoot[MAX_PATH];
|
|
|
|
DIRECTORY_STACK *dirsToWalk = NULL;
|
2007-09-11 22:43:02 +02:00
|
|
|
BOOL expandDirs = FALSE;
|
|
|
|
BOOL useNumbers = FALSE;
|
|
|
|
BOOL doFileset = FALSE;
|
2012-09-27 20:58:56 +02:00
|
|
|
BOOL doRecurse = FALSE;
|
2012-09-04 22:11:51 +02:00
|
|
|
BOOL doExecuted = FALSE; /* Has the 'do' part been executed */
|
2007-09-11 22:43:02 +02:00
|
|
|
LONG numbers[3] = {0,0,0}; /* Defaults to 0 in native */
|
|
|
|
int itemNum;
|
|
|
|
CMD_LIST *thisCmdStart;
|
2012-09-26 15:37:56 +02:00
|
|
|
int parameterNo = 0;
|
2012-10-22 00:46:46 +02:00
|
|
|
WCHAR forf_eol = 0;
|
|
|
|
int forf_skip = 0;
|
|
|
|
WCHAR forf_delims[256];
|
|
|
|
WCHAR forf_tokens[MAXSTRING];
|
2012-10-22 23:22:28 +02:00
|
|
|
BOOL forf_usebackq = FALSE;
|
2007-09-11 22:43:02 +02:00
|
|
|
|
|
|
|
/* Handle optional qualifiers (multiple are allowed) */
|
2012-10-25 23:47:59 +02:00
|
|
|
WCHAR *thisArg = WCMD_parameter(p, parameterNo++, NULL, FALSE, FALSE);
|
2012-09-27 20:58:56 +02:00
|
|
|
|
|
|
|
optionsRoot[0] = 0;
|
2012-09-26 15:37:56 +02:00
|
|
|
while (thisArg && *thisArg == '/') {
|
|
|
|
WINE_TRACE("Processing qualifier at %s\n", wine_dbgstr_w(thisArg));
|
|
|
|
thisArg++;
|
2019-05-03 12:28:33 +02:00
|
|
|
switch (towupper(*thisArg)) {
|
2012-09-26 15:37:56 +02:00
|
|
|
case 'D': expandDirs = TRUE; break;
|
|
|
|
case 'L': useNumbers = TRUE; break;
|
2007-09-11 22:43:02 +02:00
|
|
|
|
|
|
|
/* Recursive is special case - /R can have an optional path following it */
|
|
|
|
/* filenamesets are another special case - /F can have an optional options following it */
|
|
|
|
case 'R':
|
|
|
|
case 'F':
|
|
|
|
{
|
2012-09-27 20:58:56 +02:00
|
|
|
/* When recursing directories, use current directory as the starting point unless
|
|
|
|
subsequently overridden */
|
2019-05-03 12:28:33 +02:00
|
|
|
doRecurse = (towupper(*thisArg) == 'R');
|
2018-08-20 21:15:42 +02:00
|
|
|
if (doRecurse) GetCurrentDirectoryW(ARRAY_SIZE(optionsRoot), optionsRoot);
|
2007-09-11 22:43:02 +02:00
|
|
|
|
2019-05-03 12:28:33 +02:00
|
|
|
doFileset = (towupper(*thisArg) == 'F');
|
2007-09-11 22:43:02 +02:00
|
|
|
|
2012-09-27 20:58:56 +02:00
|
|
|
/* Retrieve next parameter to see if is root/options (raw form required
|
|
|
|
with for /f, or unquoted in for /r) */
|
2012-10-25 23:47:59 +02:00
|
|
|
thisArg = WCMD_parameter(p, parameterNo, NULL, doFileset, FALSE);
|
2007-09-11 22:43:02 +02:00
|
|
|
|
|
|
|
/* Next parm is either qualifier, path/options or variable -
|
|
|
|
only care about it if it is the path/options */
|
2012-09-26 15:37:56 +02:00
|
|
|
if (thisArg && *thisArg != '/' && *thisArg != '%') {
|
|
|
|
parameterNo++;
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(optionsRoot, thisArg);
|
2007-09-11 22:43:02 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2012-09-26 15:37:56 +02:00
|
|
|
WINE_FIXME("for qualifier '%c' unhandled\n", *thisArg);
|
2007-09-11 22:43:02 +02:00
|
|
|
}
|
|
|
|
|
2012-09-26 15:37:56 +02:00
|
|
|
/* Step to next token */
|
2012-10-25 23:47:59 +02:00
|
|
|
thisArg = WCMD_parameter(p, parameterNo++, NULL, FALSE, FALSE);
|
2007-09-11 22:43:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure line continues with variable */
|
2018-01-09 12:25:09 +01:00
|
|
|
if (*thisArg != '%') {
|
2011-12-02 10:07:27 +01:00
|
|
|
WCMD_output_stderr (WCMD_LoadMessage(WCMD_SYNTAXERR));
|
2007-09-11 22:43:02 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-22 00:46:46 +02:00
|
|
|
/* With for /f parse the options if provided */
|
|
|
|
if (doFileset) {
|
|
|
|
if (!WCMD_parse_forf_options(optionsRoot, &forf_eol, &forf_skip,
|
|
|
|
forf_delims, forf_tokens, &forf_usebackq))
|
|
|
|
{
|
|
|
|
WCMD_output_stderr (WCMD_LoadMessage(WCMD_SYNTAXERR));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-27 20:58:56 +02:00
|
|
|
/* Set up the list of directories to recurse if we are going to */
|
2012-10-22 00:46:46 +02:00
|
|
|
} else if (doRecurse) {
|
2012-09-27 20:58:56 +02:00
|
|
|
/* Allocate memory, add to list */
|
2018-11-25 21:56:00 +01:00
|
|
|
dirsToWalk = heap_xalloc(sizeof(DIRECTORY_STACK));
|
2012-09-27 20:58:56 +02:00
|
|
|
dirsToWalk->next = NULL;
|
2012-11-15 12:32:21 +01:00
|
|
|
dirsToWalk->dirName = heap_strdupW(optionsRoot);
|
2012-09-27 20:58:56 +02:00
|
|
|
WINE_TRACE("Starting with root directory %s\n", wine_dbgstr_w(dirsToWalk->dirName));
|
|
|
|
}
|
|
|
|
|
2007-09-11 22:43:02 +02:00
|
|
|
/* Variable should follow */
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(variable, thisArg);
|
2007-09-11 22:43:02 +02:00
|
|
|
WINE_TRACE("Variable identified as %s\n", wine_dbgstr_w(variable));
|
2012-11-05 22:43:08 +01:00
|
|
|
varidx = FOR_VAR_IDX(variable[1]);
|
2007-09-11 22:43:02 +02:00
|
|
|
|
|
|
|
/* Ensure line continues with IN */
|
2012-10-25 23:47:59 +02:00
|
|
|
thisArg = WCMD_parameter(p, parameterNo++, NULL, FALSE, FALSE);
|
2012-09-26 15:37:56 +02:00
|
|
|
if (!thisArg
|
|
|
|
|| !(CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
|
2018-08-20 21:15:42 +02:00
|
|
|
thisArg, ARRAY_SIZE(inW), inW, ARRAY_SIZE(inW)) == CSTR_EQUAL)) {
|
2011-12-02 10:07:27 +01:00
|
|
|
WCMD_output_stderr (WCMD_LoadMessage(WCMD_SYNTAXERR));
|
2007-09-11 22:43:02 +02:00
|
|
|
return;
|
1999-06-26 12:24:08 +02:00
|
|
|
}
|
1999-07-10 13:36:45 +02:00
|
|
|
|
2007-06-15 21:59:28 +02:00
|
|
|
/* Save away where the set of data starts and the variable */
|
|
|
|
thisDepth = (*cmdList)->bracketDepth;
|
|
|
|
*cmdList = (*cmdList)->nextcommand;
|
|
|
|
setStart = (*cmdList);
|
|
|
|
|
|
|
|
/* Skip until the close bracket */
|
|
|
|
WINE_TRACE("Searching %p as the set\n", *cmdList);
|
|
|
|
while (*cmdList &&
|
|
|
|
(*cmdList)->command != NULL &&
|
|
|
|
(*cmdList)->bracketDepth > thisDepth) {
|
|
|
|
WINE_TRACE("Skipping %p which is part of the set\n", *cmdList);
|
|
|
|
*cmdList = (*cmdList)->nextcommand;
|
|
|
|
}
|
1999-07-10 13:36:45 +02:00
|
|
|
|
2007-06-15 21:59:28 +02:00
|
|
|
/* Skip the close bracket, if there is one */
|
|
|
|
if (*cmdList) *cmdList = (*cmdList)->nextcommand;
|
|
|
|
|
|
|
|
/* Syntax error if missing close bracket, or nothing following it
|
|
|
|
and once we have the complete set, we expect a DO */
|
2011-08-25 00:48:18 +02:00
|
|
|
WINE_TRACE("Looking for 'do ' in %p\n", *cmdList);
|
2018-08-20 21:15:42 +02:00
|
|
|
if ((*cmdList == NULL) || !WCMD_keyword_ws_found(doW, ARRAY_SIZE(doW), (*cmdList)->command)) {
|
2011-12-02 10:07:27 +01:00
|
|
|
WCMD_output_stderr (WCMD_LoadMessage(WCMD_SYNTAXERR));
|
2007-06-15 21:59:28 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdEnd = *cmdList;
|
|
|
|
|
2012-09-27 20:58:56 +02:00
|
|
|
/* Loop repeatedly per-directory we are potentially walking, when in for /r
|
|
|
|
mode, or once for the rest of the time. */
|
|
|
|
do {
|
|
|
|
|
|
|
|
/* Save away the starting position for the commands (and offset for the
|
|
|
|
first one) */
|
|
|
|
cmdStart = *cmdList;
|
|
|
|
firstCmd = (*cmdList)->command + 3; /* Skip 'do ' */
|
|
|
|
itemNum = 0;
|
|
|
|
|
|
|
|
/* If we are recursing directories (ie /R), add all sub directories now, then
|
|
|
|
prefix the root when searching for the item */
|
2012-10-22 01:10:10 +02:00
|
|
|
if (dirsToWalk) WCMD_add_dirstowalk(dirsToWalk);
|
2007-06-15 21:59:28 +02:00
|
|
|
|
2012-09-27 20:58:56 +02:00
|
|
|
thisSet = setStart;
|
|
|
|
/* Loop through all set entries */
|
|
|
|
while (thisSet &&
|
|
|
|
thisSet->command != NULL &&
|
|
|
|
thisSet->bracketDepth >= thisDepth) {
|
|
|
|
|
|
|
|
/* Loop through all entries on the same line */
|
2018-06-24 22:44:11 +02:00
|
|
|
WCHAR *staticitem;
|
2012-09-27 20:58:56 +02:00
|
|
|
WCHAR *itemStart;
|
2012-10-22 01:53:30 +02:00
|
|
|
WCHAR buffer[MAXSTRING];
|
2012-09-27 20:58:56 +02:00
|
|
|
|
|
|
|
WINE_TRACE("Processing for set %p\n", thisSet);
|
|
|
|
i = 0;
|
2018-06-24 22:44:11 +02:00
|
|
|
while (*(staticitem = WCMD_parameter (thisSet->command, i, &itemStart, TRUE, FALSE))) {
|
2012-09-27 20:58:56 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the parameter within the set has a wildcard then search for matching files
|
|
|
|
* otherwise do a literal substitution.
|
|
|
|
*/
|
2018-06-24 22:44:11 +02:00
|
|
|
|
|
|
|
/* Take a copy of the item returned from WCMD_parameter as it is held in a
|
|
|
|
static buffer which can be overwritten during parsing of the for body */
|
|
|
|
WCHAR item[MAXSTRING];
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(item, staticitem);
|
2018-06-24 22:44:11 +02:00
|
|
|
|
2012-09-27 20:58:56 +02:00
|
|
|
thisCmdStart = cmdStart;
|
|
|
|
|
|
|
|
itemNum++;
|
|
|
|
WINE_TRACE("Processing for item %d '%s'\n", itemNum, wine_dbgstr_w(item));
|
|
|
|
|
|
|
|
if (!useNumbers && !doFileset) {
|
|
|
|
WCHAR fullitem[MAX_PATH];
|
2013-06-04 01:29:51 +02:00
|
|
|
int prefixlen = 0;
|
2012-09-27 20:58:56 +02:00
|
|
|
|
|
|
|
/* Now build the item to use / search for in the specified directory,
|
|
|
|
as it is fully qualified in the /R case */
|
|
|
|
if (dirsToWalk) {
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(fullitem, dirsToWalk->dirName);
|
2020-11-29 23:30:28 +01:00
|
|
|
lstrcatW(fullitem, L"\\");
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW(fullitem, item);
|
2012-09-27 20:58:56 +02:00
|
|
|
} else {
|
2019-05-03 12:28:33 +02:00
|
|
|
WCHAR *prefix = wcsrchr(item, '\\');
|
2013-06-04 01:29:51 +02:00
|
|
|
if (prefix) prefixlen = (prefix - item) + 1;
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(fullitem, item);
|
2012-09-27 20:58:56 +02:00
|
|
|
}
|
2007-09-11 22:43:02 +02:00
|
|
|
|
2020-11-29 23:30:29 +01:00
|
|
|
if (wcspbrk(fullitem, L"*?")) {
|
2012-09-27 20:58:56 +02:00
|
|
|
hff = FindFirstFileW(fullitem, &fd);
|
|
|
|
if (hff != INVALID_HANDLE_VALUE) {
|
|
|
|
do {
|
|
|
|
BOOL isDirectory = FALSE;
|
|
|
|
|
|
|
|
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) isDirectory = TRUE;
|
|
|
|
|
|
|
|
/* Handle as files or dirs appropriately, but ignore . and .. */
|
|
|
|
if (isDirectory == expandDirs &&
|
2020-11-29 23:30:28 +01:00
|
|
|
(lstrcmpW(fd.cFileName, L"..") != 0) && (lstrcmpW(fd.cFileName, L".") != 0))
|
2012-09-27 20:58:56 +02:00
|
|
|
{
|
|
|
|
thisCmdStart = cmdStart;
|
|
|
|
WINE_TRACE("Processing FOR filename %s\n", wine_dbgstr_w(fd.cFileName));
|
|
|
|
|
|
|
|
if (doRecurse) {
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(fullitem, dirsToWalk->dirName);
|
2020-11-29 23:30:28 +01:00
|
|
|
lstrcatW(fullitem, L"\\");
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW(fullitem, fd.cFileName);
|
2012-09-27 20:58:56 +02:00
|
|
|
} else {
|
2013-06-04 01:29:51 +02:00
|
|
|
if (prefixlen) lstrcpynW(fullitem, item, prefixlen + 1);
|
|
|
|
fullitem[prefixlen] = 0x00;
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW(fullitem, fd.cFileName);
|
2012-09-27 20:58:56 +02:00
|
|
|
}
|
|
|
|
doExecuted = TRUE;
|
2012-11-05 22:43:08 +01:00
|
|
|
|
|
|
|
/* Save away any existing for variable context (e.g. nested for loops)
|
|
|
|
and restore it after executing the body of this for loop */
|
|
|
|
if (varidx >= 0) {
|
|
|
|
oldvariablevalue = forloopcontext.variable[varidx];
|
|
|
|
forloopcontext.variable[varidx] = fullitem;
|
|
|
|
}
|
|
|
|
WCMD_part_execute (&thisCmdStart, firstCmd, FALSE, TRUE);
|
|
|
|
if (varidx >= 0) forloopcontext.variable[varidx] = oldvariablevalue;
|
|
|
|
|
2012-09-27 20:58:56 +02:00
|
|
|
cmdEnd = thisCmdStart;
|
|
|
|
}
|
|
|
|
} while (FindNextFileW(hff, &fd) != 0);
|
|
|
|
FindClose (hff);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
doExecuted = TRUE;
|
2012-11-05 22:43:08 +01:00
|
|
|
|
|
|
|
/* Save away any existing for variable context (e.g. nested for loops)
|
|
|
|
and restore it after executing the body of this for loop */
|
|
|
|
if (varidx >= 0) {
|
|
|
|
oldvariablevalue = forloopcontext.variable[varidx];
|
|
|
|
forloopcontext.variable[varidx] = fullitem;
|
|
|
|
}
|
|
|
|
WCMD_part_execute (&thisCmdStart, firstCmd, FALSE, TRUE);
|
|
|
|
if (varidx >= 0) forloopcontext.variable[varidx] = oldvariablevalue;
|
|
|
|
|
2012-09-27 20:58:56 +02:00
|
|
|
cmdEnd = thisCmdStart;
|
|
|
|
}
|
2007-09-11 22:43:02 +02:00
|
|
|
|
2012-09-27 20:58:56 +02:00
|
|
|
} else if (useNumbers) {
|
|
|
|
/* Convert the first 3 numbers to signed longs and save */
|
2019-05-03 12:28:33 +02:00
|
|
|
if (itemNum <=3) numbers[itemNum-1] = wcstol(item, NULL, 10);
|
2012-09-27 20:58:56 +02:00
|
|
|
/* else ignore them! */
|
2007-09-11 22:43:02 +02:00
|
|
|
|
2012-09-27 20:58:56 +02:00
|
|
|
/* Filesets - either a list of files, or a command to run and parse the output */
|
2012-10-22 23:22:28 +02:00
|
|
|
} else if (doFileset && ((!forf_usebackq && *itemStart != '"') ||
|
|
|
|
(forf_usebackq && *itemStart != '\''))) {
|
2007-09-11 22:43:02 +02:00
|
|
|
|
2012-09-27 20:58:56 +02:00
|
|
|
HANDLE input;
|
2012-10-23 01:28:18 +02:00
|
|
|
WCHAR *itemparm;
|
2007-09-11 22:43:02 +02:00
|
|
|
|
2012-09-27 20:58:56 +02:00
|
|
|
WINE_TRACE("Processing for filespec from item %d '%s'\n", itemNum,
|
|
|
|
wine_dbgstr_w(item));
|
2007-09-11 22:43:07 +02:00
|
|
|
|
2012-09-27 20:58:56 +02:00
|
|
|
/* If backquote or single quote, we need to launch that command
|
|
|
|
and parse the results - use a temporary file */
|
2012-10-22 23:22:28 +02:00
|
|
|
if ((forf_usebackq && *itemStart == '`') ||
|
|
|
|
(!forf_usebackq && *itemStart == '\'')) {
|
2007-09-11 22:43:07 +02:00
|
|
|
|
2012-10-22 23:22:28 +02:00
|
|
|
/* Use itemstart because the command is the whole set, not just the first token */
|
2012-10-23 01:28:18 +02:00
|
|
|
itemparm = itemStart;
|
2012-09-27 20:58:56 +02:00
|
|
|
} else {
|
2007-09-11 22:43:02 +02:00
|
|
|
|
2012-10-22 23:22:28 +02:00
|
|
|
/* Use item because the file to process is just the first item in the set */
|
2012-10-23 01:28:18 +02:00
|
|
|
itemparm = item;
|
2012-09-27 20:58:56 +02:00
|
|
|
}
|
2012-10-23 01:28:18 +02:00
|
|
|
input = WCMD_forf_getinputhandle(forf_usebackq, itemparm, (itemparm==itemStart));
|
2007-09-11 22:43:02 +02:00
|
|
|
|
2012-09-27 20:58:56 +02:00
|
|
|
/* Process the input file */
|
|
|
|
if (input == INVALID_HANDLE_VALUE) {
|
|
|
|
WCMD_print_error ();
|
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_READFAIL), item);
|
|
|
|
errorlevel = 1;
|
|
|
|
return; /* FOR loop aborts at first failure here */
|
2007-09-11 22:43:02 +02:00
|
|
|
|
2012-09-27 20:58:56 +02:00
|
|
|
} else {
|
2007-09-11 22:43:02 +02:00
|
|
|
|
2012-10-22 23:22:28 +02:00
|
|
|
/* Read line by line until end of file */
|
2018-08-20 21:15:42 +02:00
|
|
|
while (WCMD_fgets(buffer, ARRAY_SIZE(buffer), input)) {
|
2012-11-05 22:43:08 +01:00
|
|
|
WCMD_parse_line(cmdStart, firstCmd, &cmdEnd, variable[1], buffer, &doExecuted,
|
2012-11-08 22:53:08 +01:00
|
|
|
&forf_skip, forf_eol, forf_delims, forf_tokens);
|
2012-09-27 20:58:56 +02:00
|
|
|
buffer[0] = 0;
|
|
|
|
}
|
|
|
|
CloseHandle (input);
|
2007-09-11 22:43:02 +02:00
|
|
|
}
|
2007-09-11 22:43:06 +02:00
|
|
|
|
2012-10-23 01:28:18 +02:00
|
|
|
/* When we have processed the item as a whole command, abort future set processing */
|
|
|
|
if (itemparm==itemStart) {
|
|
|
|
thisSet = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-09-27 20:58:56 +02:00
|
|
|
/* Filesets - A string literal */
|
2012-10-22 23:22:28 +02:00
|
|
|
} else if (doFileset && ((!forf_usebackq && *itemStart == '"') ||
|
|
|
|
(forf_usebackq && *itemStart == '\''))) {
|
2012-10-22 01:53:30 +02:00
|
|
|
|
2012-10-23 22:35:05 +02:00
|
|
|
/* Remove leading and trailing character, ready to parse with delims= delimiters
|
|
|
|
Note that the last quote is removed from the set and the string terminates
|
|
|
|
there to mimic windows */
|
2019-05-03 12:28:33 +02:00
|
|
|
WCHAR *strend = wcsrchr(itemStart, forf_usebackq?'\'':'"');
|
2012-10-23 22:35:05 +02:00
|
|
|
if (strend) {
|
|
|
|
*strend = 0x00;
|
|
|
|
itemStart++;
|
|
|
|
}
|
|
|
|
|
2012-10-22 01:53:30 +02:00
|
|
|
/* Copy the item away from the global buffer used by WCMD_parameter */
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(buffer, itemStart);
|
2012-11-05 22:43:08 +01:00
|
|
|
WCMD_parse_line(cmdStart, firstCmd, &cmdEnd, variable[1], buffer, &doExecuted,
|
2012-11-08 22:53:08 +01:00
|
|
|
&forf_skip, forf_eol, forf_delims, forf_tokens);
|
2012-10-23 01:28:18 +02:00
|
|
|
|
|
|
|
/* Only one string can be supplied in the whole set, abort future set processing */
|
|
|
|
thisSet = NULL;
|
|
|
|
break;
|
2012-09-27 20:58:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
WINE_TRACE("Post-command, cmdEnd = %p\n", cmdEnd);
|
|
|
|
i++;
|
1999-07-10 13:36:45 +02:00
|
|
|
}
|
2007-06-15 21:59:28 +02:00
|
|
|
|
2012-09-27 20:58:56 +02:00
|
|
|
/* Move onto the next set line */
|
2012-10-23 01:28:18 +02:00
|
|
|
if (thisSet) thisSet = thisSet->nextcommand;
|
2007-06-15 21:59:21 +02:00
|
|
|
}
|
2007-06-15 21:59:28 +02:00
|
|
|
|
2012-09-27 20:58:56 +02:00
|
|
|
/* If /L is provided, now run the for loop */
|
|
|
|
if (useNumbers) {
|
|
|
|
WCHAR thisNum[20];
|
2007-06-15 21:59:28 +02:00
|
|
|
|
2012-09-27 20:58:56 +02:00
|
|
|
WINE_TRACE("FOR /L provided range from %d to %d step %d\n",
|
|
|
|
numbers[0], numbers[2], numbers[1]);
|
|
|
|
for (i=numbers[0];
|
|
|
|
(numbers[1]<0)? i>=numbers[2] : i<=numbers[2];
|
|
|
|
i=i + numbers[1]) {
|
2007-09-11 22:43:02 +02:00
|
|
|
|
2020-11-29 23:30:29 +01:00
|
|
|
swprintf(thisNum, ARRAY_SIZE(thisNum), L"%d", i);
|
2012-09-27 20:58:56 +02:00
|
|
|
WINE_TRACE("Processing FOR number %s\n", wine_dbgstr_w(thisNum));
|
2007-09-11 22:43:02 +02:00
|
|
|
|
2012-09-27 20:58:56 +02:00
|
|
|
thisCmdStart = cmdStart;
|
|
|
|
doExecuted = TRUE;
|
2012-11-05 22:43:08 +01:00
|
|
|
|
|
|
|
/* Save away any existing for variable context (e.g. nested for loops)
|
|
|
|
and restore it after executing the body of this for loop */
|
|
|
|
if (varidx >= 0) {
|
|
|
|
oldvariablevalue = forloopcontext.variable[varidx];
|
|
|
|
forloopcontext.variable[varidx] = thisNum;
|
|
|
|
}
|
|
|
|
WCMD_part_execute (&thisCmdStart, firstCmd, FALSE, TRUE);
|
|
|
|
if (varidx >= 0) forloopcontext.variable[varidx] = oldvariablevalue;
|
2012-09-27 20:58:56 +02:00
|
|
|
}
|
|
|
|
cmdEnd = thisCmdStart;
|
|
|
|
}
|
2007-09-11 22:43:02 +02:00
|
|
|
|
2012-09-27 20:58:56 +02:00
|
|
|
/* If we are walking directories, move on to any which remain */
|
|
|
|
if (dirsToWalk != NULL) {
|
|
|
|
DIRECTORY_STACK *nextDir = dirsToWalk->next;
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(dirsToWalk->dirName);
|
|
|
|
heap_free(dirsToWalk);
|
2012-09-27 20:58:56 +02:00
|
|
|
dirsToWalk = nextDir;
|
2019-12-09 09:43:23 +01:00
|
|
|
if (dirsToWalk) WINE_TRACE("Moving to next directory to iterate: %s\n",
|
2012-09-27 20:58:56 +02:00
|
|
|
wine_dbgstr_w(dirsToWalk->dirName));
|
|
|
|
else WINE_TRACE("Finished all directories.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (dirsToWalk != NULL);
|
2007-09-11 22:43:02 +02:00
|
|
|
|
2012-09-04 22:11:51 +02:00
|
|
|
/* Now skip over the do part if we did not perform the for loop so far.
|
|
|
|
We store in cmdEnd the next command after the do block, but we only
|
|
|
|
know this if something was run. If it has not been, we need to calculate
|
|
|
|
it. */
|
|
|
|
if (!doExecuted) {
|
|
|
|
thisCmdStart = cmdStart;
|
|
|
|
WINE_TRACE("Skipping for loop commands due to no valid iterations\n");
|
2012-11-05 22:43:08 +01:00
|
|
|
WCMD_part_execute(&thisCmdStart, firstCmd, FALSE, FALSE);
|
2012-09-04 22:11:51 +02:00
|
|
|
cmdEnd = thisCmdStart;
|
|
|
|
}
|
|
|
|
|
2007-06-15 21:59:28 +02:00
|
|
|
/* When the loop ends, either something like a GOTO or EXIT /b has terminated
|
|
|
|
all processing, OR it should be pointing to the end of && processing OR
|
|
|
|
it should be pointing at the NULL end of bracket for the DO. The return
|
|
|
|
value needs to be the NEXT command to execute, which it either is, or
|
|
|
|
we need to step over the closing bracket */
|
|
|
|
*cmdList = cmdEnd;
|
|
|
|
if (cmdEnd && cmdEnd->command == NULL) *cmdList = cmdEnd->nextcommand;
|
|
|
|
}
|
|
|
|
|
1999-06-06 17:24:04 +02:00
|
|
|
/**************************************************************************
|
|
|
|
* WCMD_give_help
|
|
|
|
*
|
|
|
|
* Simple on-line help. Help text is stored in the resource file.
|
|
|
|
*/
|
|
|
|
|
2012-10-17 00:41:58 +02:00
|
|
|
void WCMD_give_help (const WCHAR *args)
|
2011-11-07 10:52:22 +01:00
|
|
|
{
|
|
|
|
size_t i;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2012-10-17 00:41:58 +02:00
|
|
|
args = WCMD_skip_leading_spaces((WCHAR*) args);
|
2019-12-09 09:48:16 +01:00
|
|
|
if (!*args) {
|
2007-06-03 23:07:43 +02:00
|
|
|
WCMD_output_asis (WCMD_LoadMessage(WCMD_ALLHELP));
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
else {
|
2011-09-12 09:24:25 +02:00
|
|
|
/* Display help message for builtin commands */
|
1999-06-06 17:24:04 +02:00
|
|
|
for (i=0; i<=WCMD_EXIT; i++) {
|
2009-12-09 18:52:40 +01:00
|
|
|
if (CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
|
2012-10-17 00:41:58 +02:00
|
|
|
args, -1, inbuilt[i], -1) == CSTR_EQUAL) {
|
2007-06-03 23:07:43 +02:00
|
|
|
WCMD_output_asis (WCMD_LoadMessage(i));
|
2000-08-02 00:02:18 +02:00
|
|
|
return;
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
}
|
2011-09-07 09:10:06 +02:00
|
|
|
/* Launch the command with the /? option for external commands shipped with cmd.exe */
|
2018-08-20 21:15:42 +02:00
|
|
|
for (i = 0; i <= ARRAY_SIZE(externals); i++) {
|
2011-09-07 09:10:06 +02:00
|
|
|
if (CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
|
2012-10-17 00:41:58 +02:00
|
|
|
args, -1, externals[i], -1) == CSTR_EQUAL) {
|
2011-09-07 09:10:06 +02:00
|
|
|
WCHAR cmd[128];
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(cmd, args);
|
2020-11-29 23:30:29 +01:00
|
|
|
lstrcatW(cmd, L" /?");
|
2012-03-29 18:05:59 +02:00
|
|
|
WCMD_run_program(cmd, FALSE);
|
2011-09-07 09:10:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-10-17 00:41:58 +02:00
|
|
|
WCMD_output (WCMD_LoadMessage(WCMD_NOCMDHELP), args);
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-06-26 12:24:08 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_go_to
|
|
|
|
*
|
|
|
|
* Batch file jump instruction. Not the most efficient algorithm ;-)
|
|
|
|
* Prints error message if the specified label cannot be found - the file pointer is
|
|
|
|
* then at EOF, effectively stopping the batch file.
|
|
|
|
* FIXME: DOS is supposed to allow labels with spaces - we don't.
|
|
|
|
*/
|
|
|
|
|
2007-06-15 21:59:22 +02:00
|
|
|
void WCMD_goto (CMD_LIST **cmdList) {
|
1999-06-26 12:24:08 +02:00
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR string[MAX_PATH];
|
2013-06-02 01:17:30 +02:00
|
|
|
WCHAR *labelend = NULL;
|
2020-11-29 23:30:29 +01:00
|
|
|
const WCHAR labelEndsW[] = L"><|& :\t";
|
1999-06-26 12:24:08 +02:00
|
|
|
|
2007-06-15 21:59:22 +02:00
|
|
|
/* Do not process any more parts of a processed multipart or multilines command */
|
2007-09-11 22:43:03 +02:00
|
|
|
if (cmdList) *cmdList = NULL;
|
2007-06-15 21:59:22 +02:00
|
|
|
|
1999-06-26 12:24:08 +02:00
|
|
|
if (context != NULL) {
|
2010-03-24 04:45:25 +01:00
|
|
|
WCHAR *paramStart = param1, *str;
|
2007-02-20 19:00:37 +01:00
|
|
|
|
2011-09-09 21:54:41 +02:00
|
|
|
if (param1[0] == 0x00) {
|
2011-09-24 12:50:56 +02:00
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_NOARG));
|
2011-09-09 21:54:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-20 19:00:37 +01:00
|
|
|
/* Handle special :EOF label */
|
2020-11-29 23:30:29 +01:00
|
|
|
if (lstrcmpiW(L":eof", param1) == 0) {
|
2007-02-20 19:00:37 +01:00
|
|
|
context -> skip_rest = TRUE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-02 01:17:30 +02:00
|
|
|
/* Support goto :label as well as goto label plus remove trailing chars */
|
2007-02-23 23:19:25 +01:00
|
|
|
if (*paramStart == ':') paramStart++;
|
2019-05-03 12:28:33 +02:00
|
|
|
labelend = wcspbrk(paramStart, labelEndsW);
|
2013-06-02 01:17:30 +02:00
|
|
|
if (labelend) *labelend = 0x00;
|
|
|
|
WINE_TRACE("goto label: '%s'\n", wine_dbgstr_w(paramStart));
|
2007-02-23 23:19:25 +01:00
|
|
|
|
2018-07-04 23:08:39 +02:00
|
|
|
/* Loop through potentially twice - once from current file position
|
|
|
|
through to the end, and second time from start to current file
|
|
|
|
position */
|
|
|
|
if (*paramStart) {
|
|
|
|
int loop;
|
|
|
|
LARGE_INTEGER startli;
|
|
|
|
for (loop=0; loop<2; loop++) {
|
|
|
|
if (loop==0) {
|
|
|
|
/* On first loop, save the file size */
|
|
|
|
startli.QuadPart = 0;
|
|
|
|
startli.u.LowPart = SetFilePointer(context -> h, startli.u.LowPart,
|
|
|
|
&startli.u.HighPart, FILE_CURRENT);
|
|
|
|
} else {
|
|
|
|
/* On second loop, start at the beginning of the file */
|
|
|
|
WINE_TRACE("Label not found, trying from beginning of file\n");
|
|
|
|
if (loop==1) SetFilePointer (context -> h, 0, NULL, FILE_BEGIN);
|
|
|
|
}
|
2013-06-02 01:17:30 +02:00
|
|
|
|
2018-08-20 21:15:42 +02:00
|
|
|
while (WCMD_fgets (string, ARRAY_SIZE(string), context -> h)) {
|
2018-07-04 23:08:39 +02:00
|
|
|
str = string;
|
2013-06-02 01:17:30 +02:00
|
|
|
|
2018-07-04 23:08:39 +02:00
|
|
|
/* Ignore leading whitespace or no-echo character */
|
2019-05-03 12:28:33 +02:00
|
|
|
while (*str=='@' || iswspace (*str)) str++;
|
2010-12-13 00:04:19 +01:00
|
|
|
|
2018-07-04 23:08:39 +02:00
|
|
|
/* If the first real character is a : then this is a label */
|
|
|
|
if (*str == ':') {
|
|
|
|
str++;
|
2013-06-02 01:17:30 +02:00
|
|
|
|
2018-07-04 23:08:39 +02:00
|
|
|
/* Skip spaces between : and label */
|
2019-05-03 12:28:33 +02:00
|
|
|
while (iswspace (*str)) str++;
|
2018-07-04 23:08:39 +02:00
|
|
|
WINE_TRACE("str before brk %s\n", wine_dbgstr_w(str));
|
2013-06-02 01:17:30 +02:00
|
|
|
|
2018-07-04 23:08:39 +02:00
|
|
|
/* Label ends at whitespace or redirection characters */
|
2019-05-03 12:28:33 +02:00
|
|
|
labelend = wcspbrk(str, labelEndsW);
|
2018-07-04 23:08:39 +02:00
|
|
|
if (labelend) *labelend = 0x00;
|
|
|
|
WINE_TRACE("comparing found label %s\n", wine_dbgstr_w(str));
|
|
|
|
|
|
|
|
if (lstrcmpiW (str, paramStart) == 0) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See if we have gone beyond the end point if second time through */
|
|
|
|
if (loop==1) {
|
|
|
|
LARGE_INTEGER curli;
|
|
|
|
curli.QuadPart = 0;
|
|
|
|
curli.u.LowPart = SetFilePointer(context -> h, curli.u.LowPart,
|
|
|
|
&curli.u.HighPart, FILE_CURRENT);
|
|
|
|
if (curli.QuadPart > startli.QuadPart) {
|
|
|
|
WINE_TRACE("Reached wrap point, label not found\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-06-26 12:24:08 +02:00
|
|
|
}
|
2018-07-04 23:08:39 +02:00
|
|
|
|
2011-09-24 12:50:56 +02:00
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_NOTARGET));
|
2013-06-02 01:17:30 +02:00
|
|
|
context -> skip_rest = TRUE;
|
1999-06-26 12:24:08 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-23 23:17:28 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* WCMD_pushd
|
|
|
|
*
|
|
|
|
* Push a directory onto the stack
|
|
|
|
*/
|
|
|
|
|
2012-10-17 00:41:58 +02:00
|
|
|
void WCMD_pushd (const WCHAR *args)
|
2011-10-21 00:56:32 +02:00
|
|
|
{
|
2007-02-23 23:17:28 +01:00
|
|
|
struct env_stack *curdir;
|
|
|
|
WCHAR *thisdir;
|
|
|
|
|
2019-05-03 12:28:33 +02:00
|
|
|
if (wcschr(args, '/') != NULL) {
|
2007-03-08 01:44:46 +01:00
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
WCMD_print_error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-23 23:17:28 +01:00
|
|
|
curdir = LocalAlloc (LMEM_FIXED, sizeof (struct env_stack));
|
|
|
|
thisdir = LocalAlloc (LMEM_FIXED, 1024 * sizeof(WCHAR));
|
|
|
|
if( !curdir || !thisdir ) {
|
|
|
|
LocalFree(curdir);
|
|
|
|
LocalFree(thisdir);
|
2007-06-03 23:07:39 +02:00
|
|
|
WINE_ERR ("out of memory\n");
|
2007-02-23 23:17:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-08 01:50:14 +01:00
|
|
|
/* Change directory using CD code with /D parameter */
|
2020-11-29 23:30:29 +01:00
|
|
|
lstrcpyW(quals, L"/D");
|
2007-02-23 23:17:28 +01:00
|
|
|
GetCurrentDirectoryW (1024, thisdir);
|
2007-03-08 01:44:09 +01:00
|
|
|
errorlevel = 0;
|
2012-10-17 00:41:58 +02:00
|
|
|
WCMD_setshow_default(args);
|
2007-03-08 01:44:09 +01:00
|
|
|
if (errorlevel) {
|
2007-02-23 23:17:28 +01:00
|
|
|
LocalFree(curdir);
|
|
|
|
LocalFree(thisdir);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
curdir -> next = pushd_directories;
|
|
|
|
curdir -> strings = thisdir;
|
2007-03-04 23:34:20 +01:00
|
|
|
if (pushd_directories == NULL) {
|
2007-03-12 10:35:13 +01:00
|
|
|
curdir -> u.stackdepth = 1;
|
2007-03-04 23:34:20 +01:00
|
|
|
} else {
|
2007-03-12 10:35:13 +01:00
|
|
|
curdir -> u.stackdepth = pushd_directories -> u.stackdepth + 1;
|
2007-03-04 23:34:20 +01:00
|
|
|
}
|
2007-02-23 23:17:28 +01:00
|
|
|
pushd_directories = curdir;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* WCMD_popd
|
|
|
|
*
|
|
|
|
* Pop a directory from the stack
|
|
|
|
*/
|
|
|
|
|
|
|
|
void WCMD_popd (void) {
|
|
|
|
struct env_stack *temp = pushd_directories;
|
|
|
|
|
|
|
|
if (!pushd_directories)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* pop the old environment from the stack, and make it the current dir */
|
|
|
|
pushd_directories = temp->next;
|
|
|
|
SetCurrentDirectoryW(temp->strings);
|
|
|
|
LocalFree (temp->strings);
|
|
|
|
LocalFree (temp);
|
|
|
|
}
|
1999-06-26 12:24:08 +02:00
|
|
|
|
2012-10-25 23:48:02 +02:00
|
|
|
/*******************************************************************
|
|
|
|
* evaluate_if_comparison
|
|
|
|
*
|
|
|
|
* Evaluates an "if" comparison operation
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* leftOperand [I] left operand, non NULL
|
|
|
|
* operator [I] "if" binary comparison operator, non NULL
|
|
|
|
* rightOperand [I] right operand, non NULL
|
|
|
|
* caseInsensitive [I] 0 for case sensitive comparison, anything else for insensitive
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Success: 1 if operator applied to the operands evaluates to TRUE
|
|
|
|
* 0 if operator applied to the operands evaluates to FALSE
|
|
|
|
* Failure: -1 if operator is not recognized
|
|
|
|
*/
|
|
|
|
static int evaluate_if_comparison(const WCHAR *leftOperand, const WCHAR *operator,
|
|
|
|
const WCHAR *rightOperand, int caseInsensitive)
|
|
|
|
{
|
|
|
|
WCHAR *endptr_leftOp, *endptr_rightOp;
|
|
|
|
long int leftOperand_int, rightOperand_int;
|
|
|
|
BOOL int_operands;
|
|
|
|
|
|
|
|
/* == is a special case, as it always compares strings */
|
2020-11-29 23:30:29 +01:00
|
|
|
if (!lstrcmpiW(operator, L"=="))
|
2012-10-25 23:48:02 +02:00
|
|
|
return caseInsensitive ? lstrcmpiW(leftOperand, rightOperand) == 0
|
|
|
|
: lstrcmpW (leftOperand, rightOperand) == 0;
|
|
|
|
|
|
|
|
/* Check if we have plain integers (in decimal, octal or hexadecimal notation) */
|
2019-05-03 12:28:33 +02:00
|
|
|
leftOperand_int = wcstol(leftOperand, &endptr_leftOp, 0);
|
|
|
|
rightOperand_int = wcstol(rightOperand, &endptr_rightOp, 0);
|
2012-10-25 23:48:02 +02:00
|
|
|
int_operands = (!*endptr_leftOp) && (!*endptr_rightOp);
|
|
|
|
|
|
|
|
/* Perform actual (integer or string) comparison */
|
2020-11-29 23:30:29 +01:00
|
|
|
if (!lstrcmpiW(operator, L"lss")) {
|
2012-10-25 23:48:02 +02:00
|
|
|
if (int_operands)
|
|
|
|
return leftOperand_int < rightOperand_int;
|
|
|
|
else
|
|
|
|
return caseInsensitive ? lstrcmpiW(leftOperand, rightOperand) < 0
|
|
|
|
: lstrcmpW (leftOperand, rightOperand) < 0;
|
|
|
|
}
|
|
|
|
|
2020-11-29 23:30:29 +01:00
|
|
|
if (!lstrcmpiW(operator, L"leq")) {
|
2012-10-25 23:48:03 +02:00
|
|
|
if (int_operands)
|
|
|
|
return leftOperand_int <= rightOperand_int;
|
|
|
|
else
|
|
|
|
return caseInsensitive ? lstrcmpiW(leftOperand, rightOperand) <= 0
|
|
|
|
: lstrcmpW (leftOperand, rightOperand) <= 0;
|
|
|
|
}
|
|
|
|
|
2020-11-29 23:30:29 +01:00
|
|
|
if (!lstrcmpiW(operator, L"equ")) {
|
2012-10-25 23:48:04 +02:00
|
|
|
if (int_operands)
|
|
|
|
return leftOperand_int == rightOperand_int;
|
|
|
|
else
|
|
|
|
return caseInsensitive ? lstrcmpiW(leftOperand, rightOperand) == 0
|
|
|
|
: lstrcmpW (leftOperand, rightOperand) == 0;
|
|
|
|
}
|
|
|
|
|
2020-11-29 23:30:29 +01:00
|
|
|
if (!lstrcmpiW(operator, L"neq")) {
|
2012-10-25 23:48:05 +02:00
|
|
|
if (int_operands)
|
|
|
|
return leftOperand_int != rightOperand_int;
|
|
|
|
else
|
|
|
|
return caseInsensitive ? lstrcmpiW(leftOperand, rightOperand) != 0
|
|
|
|
: lstrcmpW (leftOperand, rightOperand) != 0;
|
|
|
|
}
|
|
|
|
|
2020-11-29 23:30:29 +01:00
|
|
|
if (!lstrcmpiW(operator, L"geq")) {
|
2012-10-25 23:48:06 +02:00
|
|
|
if (int_operands)
|
|
|
|
return leftOperand_int >= rightOperand_int;
|
|
|
|
else
|
|
|
|
return caseInsensitive ? lstrcmpiW(leftOperand, rightOperand) >= 0
|
|
|
|
: lstrcmpW (leftOperand, rightOperand) >= 0;
|
|
|
|
}
|
|
|
|
|
2020-11-29 23:30:29 +01:00
|
|
|
if (!lstrcmpiW(operator, L"gtr")) {
|
2012-10-25 23:48:07 +02:00
|
|
|
if (int_operands)
|
|
|
|
return leftOperand_int > rightOperand_int;
|
|
|
|
else
|
|
|
|
return caseInsensitive ? lstrcmpiW(leftOperand, rightOperand) > 0
|
|
|
|
: lstrcmpW (leftOperand, rightOperand) > 0;
|
|
|
|
}
|
|
|
|
|
2012-10-25 23:48:02 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-08-20 17:04:58 +02:00
|
|
|
int evaluate_if_condition(WCHAR *p, WCHAR **command, int *test, int *negate)
|
2012-10-25 23:48:00 +02:00
|
|
|
{
|
2019-08-20 17:04:58 +02:00
|
|
|
WCHAR condition[MAX_PATH];
|
2020-11-29 23:30:29 +01:00
|
|
|
int caseInsensitive = (wcsstr(quals, L"/I") != NULL);
|
|
|
|
|
|
|
|
*negate = !lstrcmpiW(param1,L"not");
|
2019-08-20 17:04:58 +02:00
|
|
|
lstrcpyW(condition, (*negate ? param2 : param1));
|
2007-09-11 22:43:05 +02:00
|
|
|
WINE_TRACE("Condition: %s\n", wine_dbgstr_w(condition));
|
|
|
|
|
2020-11-29 23:30:29 +01:00
|
|
|
if (!lstrcmpiW(condition, L"errorlevel")) {
|
2019-08-20 17:04:58 +02:00
|
|
|
WCHAR *param = WCMD_parameter(p, 1+(*negate), NULL, FALSE, FALSE);
|
2011-10-26 16:57:52 +02:00
|
|
|
WCHAR *endptr;
|
2019-05-03 12:28:33 +02:00
|
|
|
long int param_int = wcstol(param, &endptr, 10);
|
2012-10-25 23:48:00 +02:00
|
|
|
if (*endptr) goto syntax_err;
|
2019-08-20 17:04:58 +02:00
|
|
|
*test = ((long int)errorlevel >= param_int);
|
|
|
|
WCMD_parameter(p, 2+(*negate), command, FALSE, FALSE);
|
1999-07-10 13:36:45 +02:00
|
|
|
}
|
2020-11-29 23:30:29 +01:00
|
|
|
else if (!lstrcmpiW(condition, L"exist")) {
|
2018-06-24 22:44:10 +02:00
|
|
|
WIN32_FIND_DATAW fd;
|
2018-08-27 21:41:52 +02:00
|
|
|
HANDLE hff;
|
2019-08-20 17:04:58 +02:00
|
|
|
WCHAR *param = WCMD_parameter(p, 1+(*negate), NULL, FALSE, FALSE);
|
2019-05-03 12:28:33 +02:00
|
|
|
int len = lstrlenW(param);
|
2018-08-27 21:41:52 +02:00
|
|
|
|
|
|
|
/* FindFirstFile does not like a directory path ending in '\', append a '.' */
|
2020-11-29 23:30:28 +01:00
|
|
|
if (len && param[len-1] == '\\') lstrcatW(param, L".");
|
2018-08-27 21:41:52 +02:00
|
|
|
|
|
|
|
hff = FindFirstFileW(param, &fd);
|
2019-08-20 17:04:58 +02:00
|
|
|
*test = (hff != INVALID_HANDLE_VALUE );
|
|
|
|
if (*test) FindClose(hff);
|
2018-06-24 22:44:10 +02:00
|
|
|
|
2019-08-20 17:04:58 +02:00
|
|
|
WCMD_parameter(p, 2+(*negate), command, FALSE, FALSE);
|
1999-07-10 13:36:45 +02:00
|
|
|
}
|
2020-11-29 23:30:29 +01:00
|
|
|
else if (!lstrcmpiW(condition, L"defined")) {
|
2019-08-20 17:04:58 +02:00
|
|
|
*test = (GetEnvironmentVariableW(WCMD_parameter(p, 1+(*negate), NULL, FALSE, FALSE),
|
2012-09-26 01:07:20 +02:00
|
|
|
NULL, 0) > 0);
|
2019-08-20 17:04:58 +02:00
|
|
|
WCMD_parameter(p, 2+(*negate), command, FALSE, FALSE);
|
2007-02-20 18:47:35 +01:00
|
|
|
}
|
2012-10-25 23:48:01 +02:00
|
|
|
else { /* comparison operation */
|
|
|
|
WCHAR leftOperand[MAXSTRING], rightOperand[MAXSTRING], operator[MAXSTRING];
|
|
|
|
WCHAR *paramStart;
|
|
|
|
|
2019-08-20 17:04:58 +02:00
|
|
|
lstrcpyW(leftOperand, WCMD_parameter(p, (*negate)+caseInsensitive, ¶mStart, TRUE, FALSE));
|
2012-10-25 23:48:01 +02:00
|
|
|
if (!*leftOperand)
|
|
|
|
goto syntax_err;
|
|
|
|
|
|
|
|
/* Note: '==' can't be returned by WCMD_parameter since '=' is a separator */
|
2019-05-03 12:28:33 +02:00
|
|
|
p = paramStart + lstrlenW(leftOperand);
|
2012-10-25 23:48:01 +02:00
|
|
|
while (*p == ' ' || *p == '\t')
|
|
|
|
p++;
|
|
|
|
|
2020-11-29 23:30:29 +01:00
|
|
|
if (!wcsncmp(p, L"==", lstrlenW(L"==")))
|
|
|
|
lstrcpyW(operator, L"==");
|
2012-10-25 23:48:02 +02:00
|
|
|
else {
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(operator, WCMD_parameter(p, 0, ¶mStart, FALSE, FALSE));
|
2012-10-25 23:48:02 +02:00
|
|
|
if (!*operator) goto syntax_err;
|
|
|
|
}
|
2019-05-03 12:28:33 +02:00
|
|
|
p += lstrlenW(operator);
|
2012-10-25 23:48:01 +02:00
|
|
|
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(rightOperand, WCMD_parameter(p, 0, ¶mStart, TRUE, FALSE));
|
2012-10-25 23:48:01 +02:00
|
|
|
if (!*rightOperand)
|
|
|
|
goto syntax_err;
|
|
|
|
|
2019-08-20 17:04:58 +02:00
|
|
|
*test = evaluate_if_comparison(leftOperand, operator, rightOperand, caseInsensitive);
|
|
|
|
if (*test == -1)
|
2012-10-25 23:48:02 +02:00
|
|
|
goto syntax_err;
|
2012-10-25 23:48:01 +02:00
|
|
|
|
2019-05-03 12:28:33 +02:00
|
|
|
p = paramStart + lstrlenW(rightOperand);
|
2019-08-20 17:04:58 +02:00
|
|
|
WCMD_parameter(p, 0, command, FALSE, FALSE);
|
1999-07-10 13:36:45 +02:00
|
|
|
}
|
2007-06-15 21:59:27 +02:00
|
|
|
|
2019-08-20 17:04:58 +02:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
syntax_err:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_if
|
|
|
|
*
|
|
|
|
* Batch file conditional.
|
|
|
|
*
|
|
|
|
* On entry, cmdlist will point to command containing the IF, and optionally
|
|
|
|
* the first command to execute (if brackets not found)
|
|
|
|
* If &&'s were found, this may be followed by a record flagged as isAmpersand
|
|
|
|
* If ('s were found, execute all within that bracket
|
|
|
|
* Command may optionally be followed by an ELSE - need to skip instructions
|
|
|
|
* in the else using the same logic
|
|
|
|
*
|
|
|
|
* FIXME: Much more syntax checking needed!
|
|
|
|
*/
|
|
|
|
void WCMD_if (WCHAR *p, CMD_LIST **cmdList)
|
|
|
|
{
|
|
|
|
int negate; /* Negate condition */
|
|
|
|
int test; /* Condition evaluation result */
|
|
|
|
WCHAR *command;
|
|
|
|
|
2020-05-03 17:05:24 +02:00
|
|
|
/* Function evaluate_if_condition relies on the global variables quals, param1 and param2
|
|
|
|
set in a call to WCMD_parse before */
|
2019-08-20 17:04:58 +02:00
|
|
|
if (evaluate_if_condition(p, &command, &test, &negate) == -1)
|
|
|
|
goto syntax_err;
|
|
|
|
|
2020-05-03 17:05:24 +02:00
|
|
|
WINE_TRACE("p: %s, quals: %s, param1: %s, param2: %s, command: %s\n",
|
|
|
|
wine_dbgstr_w(p), wine_dbgstr_w(quals), wine_dbgstr_w(param1),
|
|
|
|
wine_dbgstr_w(param2), wine_dbgstr_w(command));
|
|
|
|
|
2007-06-15 21:59:27 +02:00
|
|
|
/* Process rest of IF statement which is on the same line
|
|
|
|
Note: This may process all or some of the cmdList (eg a GOTO) */
|
2012-11-05 22:43:08 +01:00
|
|
|
WCMD_part_execute(cmdList, command, TRUE, (test != negate));
|
2012-10-25 23:48:00 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
syntax_err:
|
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_SYNTAXERR));
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_move
|
|
|
|
*
|
|
|
|
* Move a file, directory tree or wildcarded set of files.
|
|
|
|
*/
|
|
|
|
|
2011-10-13 17:30:17 +02:00
|
|
|
void WCMD_move (void)
|
|
|
|
{
|
2013-10-16 23:38:31 +02:00
|
|
|
BOOL status;
|
2009-12-09 18:52:40 +01:00
|
|
|
WIN32_FIND_DATAW fd;
|
2007-04-11 23:25:52 +02:00
|
|
|
HANDLE hff;
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR input[MAX_PATH];
|
|
|
|
WCHAR output[MAX_PATH];
|
|
|
|
WCHAR drive[10];
|
|
|
|
WCHAR dir[MAX_PATH];
|
|
|
|
WCHAR fname[MAX_PATH];
|
|
|
|
WCHAR ext[MAX_PATH];
|
1999-07-10 13:36:45 +02:00
|
|
|
|
2006-10-31 21:11:28 +01:00
|
|
|
if (param1[0] == 0x00) {
|
2011-09-24 12:50:56 +02:00
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_NOARG));
|
2006-10-31 21:11:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-04-20 22:54:38 +02:00
|
|
|
/* If no destination supplied, assume current directory */
|
|
|
|
if (param2[0] == 0x00) {
|
2020-11-29 23:30:28 +01:00
|
|
|
lstrcpyW(param2, L".");
|
2002-04-20 22:54:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If 2nd parm is directory, then use original filename */
|
2007-04-11 23:25:52 +02:00
|
|
|
/* Convert partial path to full path */
|
2018-08-20 21:15:42 +02:00
|
|
|
GetFullPathNameW(param1, ARRAY_SIZE(input), input, NULL);
|
|
|
|
GetFullPathNameW(param2, ARRAY_SIZE(output), output, NULL);
|
2007-06-03 23:07:42 +02:00
|
|
|
WINE_TRACE("Move from '%s'('%s') to '%s'\n", wine_dbgstr_w(input),
|
|
|
|
wine_dbgstr_w(param1), wine_dbgstr_w(output));
|
2007-04-11 23:25:52 +02:00
|
|
|
|
|
|
|
/* Split into components */
|
2020-04-26 05:53:36 +02:00
|
|
|
_wsplitpath(input, drive, dir, fname, ext);
|
2007-04-11 23:25:52 +02:00
|
|
|
|
2009-12-09 18:52:40 +01:00
|
|
|
hff = FindFirstFileW(input, &fd);
|
2011-10-05 10:16:30 +02:00
|
|
|
if (hff == INVALID_HANDLE_VALUE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
do {
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR dest[MAX_PATH];
|
|
|
|
WCHAR src[MAX_PATH];
|
2007-04-11 23:25:52 +02:00
|
|
|
DWORD attribs;
|
2011-10-13 17:30:17 +02:00
|
|
|
BOOL ok = TRUE;
|
2020-09-10 21:10:55 +02:00
|
|
|
DWORD flags = 0;
|
2007-04-11 23:25:52 +02:00
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
WINE_TRACE("Processing file '%s'\n", wine_dbgstr_w(fd.cFileName));
|
2007-04-11 23:25:52 +02:00
|
|
|
|
|
|
|
/* Build src & dest name */
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(src, drive);
|
|
|
|
lstrcatW(src, dir);
|
2007-04-11 23:25:52 +02:00
|
|
|
|
|
|
|
/* See if dest is an existing directory */
|
2009-12-09 18:52:40 +01:00
|
|
|
attribs = GetFileAttributesW(output);
|
2007-04-11 23:25:52 +02:00
|
|
|
if (attribs != INVALID_FILE_ATTRIBUTES &&
|
|
|
|
(attribs & FILE_ATTRIBUTE_DIRECTORY)) {
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(dest, output);
|
2020-11-29 23:30:28 +01:00
|
|
|
lstrcatW(dest, L"\\");
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW(dest, fd.cFileName);
|
2007-04-11 23:25:52 +02:00
|
|
|
} else {
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(dest, output);
|
2002-04-20 22:54:38 +02:00
|
|
|
}
|
|
|
|
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW(src, fd.cFileName);
|
2007-04-11 23:25:52 +02:00
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
WINE_TRACE("Source '%s'\n", wine_dbgstr_w(src));
|
|
|
|
WINE_TRACE("Dest '%s'\n", wine_dbgstr_w(dest));
|
2007-04-11 23:25:52 +02:00
|
|
|
|
2011-10-13 17:30:17 +02:00
|
|
|
/* If destination exists, prompt unless /Y supplied */
|
|
|
|
if (GetFileAttributesW(dest) != INVALID_FILE_ATTRIBUTES) {
|
|
|
|
BOOL force = FALSE;
|
|
|
|
WCHAR copycmd[MAXSTRING];
|
2011-11-07 10:52:22 +01:00
|
|
|
DWORD len;
|
2007-04-11 23:25:53 +02:00
|
|
|
|
2020-09-10 21:17:36 +02:00
|
|
|
/* Default whether automatic overwriting is on. If we are interactive then
|
|
|
|
we prompt by default, otherwise we overwrite by default
|
|
|
|
/-Y has the highest priority, then /Y and finally the COPYCMD env. variable */
|
2020-11-29 23:30:29 +01:00
|
|
|
if (wcsstr(quals, L"/-Y"))
|
2011-10-13 17:30:17 +02:00
|
|
|
force = FALSE;
|
2020-11-29 23:30:29 +01:00
|
|
|
else if (wcsstr(quals, L"/Y"))
|
2011-10-13 17:30:17 +02:00
|
|
|
force = TRUE;
|
|
|
|
else {
|
2020-09-10 21:17:36 +02:00
|
|
|
/* By default, we will force the overwrite in batch mode and ask for
|
|
|
|
* confirmation in interactive mode. */
|
|
|
|
force = !interactive;
|
|
|
|
/* If COPYCMD is set, then we force the overwrite with /Y and ask for
|
|
|
|
* confirmation with /-Y. If COPYCMD is neither of those, then we use the
|
|
|
|
* default behavior. */
|
2020-11-29 23:30:29 +01:00
|
|
|
len = GetEnvironmentVariableW(L"COPYCMD", copycmd, ARRAY_SIZE(copycmd));
|
2020-09-10 21:17:36 +02:00
|
|
|
if (len && len < ARRAY_SIZE(copycmd)) {
|
2020-11-29 23:30:29 +01:00
|
|
|
if (!lstrcmpiW(copycmd, L"/Y"))
|
2020-09-10 21:17:36 +02:00
|
|
|
force = TRUE;
|
2020-11-29 23:30:29 +01:00
|
|
|
else if (!lstrcmpiW(copycmd, L"/-Y"))
|
2020-09-10 21:17:36 +02:00
|
|
|
force = FALSE;
|
|
|
|
}
|
2011-10-13 17:30:17 +02:00
|
|
|
}
|
2007-04-11 23:25:53 +02:00
|
|
|
|
2011-10-13 17:30:17 +02:00
|
|
|
/* Prompt if overwriting */
|
|
|
|
if (!force) {
|
2011-12-06 17:40:42 +01:00
|
|
|
WCHAR* question;
|
2007-04-11 23:25:53 +02:00
|
|
|
|
2011-10-13 17:30:17 +02:00
|
|
|
/* Ask for confirmation */
|
2011-12-06 17:40:42 +01:00
|
|
|
question = WCMD_format_string(WCMD_LoadMessage(WCMD_OVERWRITE), dest);
|
2011-10-13 17:30:17 +02:00
|
|
|
ok = WCMD_ask_confirm(question, FALSE, NULL);
|
2011-12-06 17:40:42 +01:00
|
|
|
LocalFree(question);
|
2007-04-11 23:25:53 +02:00
|
|
|
}
|
2020-09-10 21:10:55 +02:00
|
|
|
|
|
|
|
if (ok)
|
|
|
|
flags |= MOVEFILE_REPLACE_EXISTING;
|
2011-10-13 17:30:17 +02:00
|
|
|
}
|
2007-04-11 23:25:53 +02:00
|
|
|
|
2011-10-13 17:30:17 +02:00
|
|
|
if (ok) {
|
2020-09-10 21:10:55 +02:00
|
|
|
status = MoveFileExW(src, dest, flags);
|
2011-10-13 17:30:17 +02:00
|
|
|
} else {
|
2013-10-16 23:38:31 +02:00
|
|
|
status = TRUE;
|
2007-04-11 23:25:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!status) {
|
|
|
|
WCMD_print_error ();
|
|
|
|
errorlevel = 1;
|
|
|
|
}
|
2011-10-05 10:16:30 +02:00
|
|
|
} while (FindNextFileW(hff, &fd) != 0);
|
2007-04-11 23:25:52 +02:00
|
|
|
|
2011-10-05 10:16:30 +02:00
|
|
|
FindClose(hff);
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_pause
|
|
|
|
*
|
2011-10-07 01:02:08 +02:00
|
|
|
* Suspend execution of a batch script until a key is typed
|
1999-06-06 17:24:04 +02:00
|
|
|
*/
|
|
|
|
|
2011-10-07 01:02:08 +02:00
|
|
|
void WCMD_pause (void)
|
|
|
|
{
|
|
|
|
DWORD oldmode;
|
|
|
|
BOOL have_console;
|
2007-03-01 12:43:19 +01:00
|
|
|
DWORD count;
|
2011-10-07 01:02:08 +02:00
|
|
|
WCHAR key;
|
|
|
|
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
|
|
|
|
have_console = GetConsoleMode(hIn, &oldmode);
|
|
|
|
if (have_console)
|
|
|
|
SetConsoleMode(hIn, 0);
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2011-11-24 00:30:23 +01:00
|
|
|
WCMD_output_asis(anykey);
|
2011-10-07 01:02:08 +02:00
|
|
|
WCMD_ReadFile(hIn, &key, 1, &count);
|
|
|
|
if (have_console)
|
|
|
|
SetConsoleMode(hIn, oldmode);
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_remove_dir
|
|
|
|
*
|
|
|
|
* Delete a directory.
|
|
|
|
*/
|
|
|
|
|
2012-10-17 00:41:58 +02:00
|
|
|
void WCMD_remove_dir (WCHAR *args) {
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2007-03-04 23:33:27 +01:00
|
|
|
int argno = 0;
|
|
|
|
int argsProcessed = 0;
|
2012-10-17 00:41:58 +02:00
|
|
|
WCHAR *argN = args;
|
2007-02-27 00:04:40 +01:00
|
|
|
|
2007-03-04 23:33:27 +01:00
|
|
|
/* Loop through all args */
|
|
|
|
while (argN) {
|
2012-10-25 23:47:59 +02:00
|
|
|
WCHAR *thisArg = WCMD_parameter (args, argno++, &argN, FALSE, FALSE);
|
2007-03-04 23:33:27 +01:00
|
|
|
if (argN && argN[0] != '/') {
|
2007-06-03 23:07:42 +02:00
|
|
|
WINE_TRACE("rd: Processing arg %s (quals:%s)\n", wine_dbgstr_w(thisArg),
|
|
|
|
wine_dbgstr_w(quals));
|
2007-03-04 23:33:27 +01:00
|
|
|
argsProcessed++;
|
2007-02-27 00:04:40 +01:00
|
|
|
|
2007-03-04 23:33:27 +01:00
|
|
|
/* If subdirectory search not supplied, just try to remove
|
|
|
|
and report error if it fails (eg if it contains a file) */
|
2020-11-29 23:30:29 +01:00
|
|
|
if (wcsstr(quals, L"/S") == NULL) {
|
2009-12-09 18:52:40 +01:00
|
|
|
if (!RemoveDirectoryW(thisArg)) WCMD_print_error ();
|
2007-02-27 00:04:40 +01:00
|
|
|
|
2007-03-04 23:33:27 +01:00
|
|
|
/* Otherwise use ShFileOp to recursively remove a directory */
|
|
|
|
} else {
|
2007-02-28 00:18:57 +01:00
|
|
|
|
2009-12-09 18:52:40 +01:00
|
|
|
SHFILEOPSTRUCTW lpDir;
|
2007-02-28 00:18:57 +01:00
|
|
|
|
2007-03-04 23:33:27 +01:00
|
|
|
/* Ask first */
|
2020-11-29 23:30:29 +01:00
|
|
|
if (wcsstr(quals, L"/Q") == NULL) {
|
2007-03-04 23:33:27 +01:00
|
|
|
BOOL ok;
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR question[MAXSTRING];
|
2007-02-28 00:18:57 +01:00
|
|
|
|
2007-03-04 23:33:27 +01:00
|
|
|
/* Ask for confirmation */
|
2020-11-29 23:30:29 +01:00
|
|
|
wsprintfW(question, L"%s ", thisArg);
|
2007-06-03 23:07:39 +02:00
|
|
|
ok = WCMD_ask_confirm(question, TRUE, NULL);
|
2007-03-04 23:33:27 +01:00
|
|
|
|
|
|
|
/* Abort if answer is 'N' */
|
|
|
|
if (!ok) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the delete */
|
|
|
|
lpDir.hwnd = NULL;
|
|
|
|
lpDir.pTo = NULL;
|
|
|
|
lpDir.pFrom = thisArg;
|
|
|
|
lpDir.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI;
|
|
|
|
lpDir.wFunc = FO_DELETE;
|
2012-09-03 23:03:58 +02:00
|
|
|
|
|
|
|
/* SHFileOperationW needs file list with a double null termination */
|
|
|
|
thisArg[lstrlenW(thisArg) + 1] = 0x00;
|
|
|
|
|
2009-12-09 18:52:40 +01:00
|
|
|
if (SHFileOperationW(&lpDir)) WCMD_print_error ();
|
2007-03-04 23:33:27 +01:00
|
|
|
}
|
2007-02-28 00:18:57 +01:00
|
|
|
}
|
2007-03-04 23:33:27 +01:00
|
|
|
}
|
2007-02-28 00:18:57 +01:00
|
|
|
|
2007-03-04 23:33:27 +01:00
|
|
|
/* Handle no valid args */
|
|
|
|
if (argsProcessed == 0) {
|
2011-09-24 12:50:56 +02:00
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_NOARG));
|
2007-03-04 23:33:27 +01:00
|
|
|
return;
|
2007-02-27 00:04:40 +01:00
|
|
|
}
|
2007-03-04 23:33:27 +01:00
|
|
|
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_rename
|
|
|
|
*
|
|
|
|
* Rename a file.
|
|
|
|
*/
|
|
|
|
|
2011-10-14 15:37:59 +02:00
|
|
|
void WCMD_rename (void)
|
|
|
|
{
|
2013-10-16 23:38:31 +02:00
|
|
|
BOOL status;
|
2007-04-11 23:25:51 +02:00
|
|
|
HANDLE hff;
|
2009-12-09 18:52:40 +01:00
|
|
|
WIN32_FIND_DATAW fd;
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR input[MAX_PATH];
|
|
|
|
WCHAR *dotDst = NULL;
|
|
|
|
WCHAR drive[10];
|
|
|
|
WCHAR dir[MAX_PATH];
|
|
|
|
WCHAR fname[MAX_PATH];
|
|
|
|
WCHAR ext[MAX_PATH];
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2007-04-11 23:25:51 +02:00
|
|
|
errorlevel = 0;
|
|
|
|
|
|
|
|
/* Must be at least two args */
|
2006-10-31 21:11:28 +01:00
|
|
|
if (param1[0] == 0x00 || param2[0] == 0x00) {
|
2011-09-24 12:50:56 +02:00
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_NOARG));
|
2007-04-11 23:25:51 +02:00
|
|
|
errorlevel = 1;
|
2006-10-31 21:11:28 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-04-11 23:25:51 +02:00
|
|
|
|
|
|
|
/* Destination cannot contain a drive letter or directory separator */
|
2019-05-03 12:28:33 +02:00
|
|
|
if ((wcschr(param2,':') != NULL) || (wcschr(param2,'\\') != NULL)) {
|
2007-04-11 23:25:51 +02:00
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
WCMD_print_error();
|
|
|
|
errorlevel = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert partial path to full path */
|
2018-08-20 21:15:42 +02:00
|
|
|
GetFullPathNameW(param1, ARRAY_SIZE(input), input, NULL);
|
2007-06-03 23:07:42 +02:00
|
|
|
WINE_TRACE("Rename from '%s'('%s') to '%s'\n", wine_dbgstr_w(input),
|
|
|
|
wine_dbgstr_w(param1), wine_dbgstr_w(param2));
|
2019-05-03 12:28:33 +02:00
|
|
|
dotDst = wcschr(param2, '.');
|
2007-04-11 23:25:51 +02:00
|
|
|
|
|
|
|
/* Split into components */
|
2020-04-26 05:53:36 +02:00
|
|
|
_wsplitpath(input, drive, dir, fname, ext);
|
2007-04-11 23:25:51 +02:00
|
|
|
|
2009-12-09 18:52:40 +01:00
|
|
|
hff = FindFirstFileW(input, &fd);
|
2011-10-05 10:16:30 +02:00
|
|
|
if (hff == INVALID_HANDLE_VALUE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
do {
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR dest[MAX_PATH];
|
|
|
|
WCHAR src[MAX_PATH];
|
|
|
|
WCHAR *dotSrc = NULL;
|
2007-04-11 23:25:51 +02:00
|
|
|
int dirLen;
|
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
WINE_TRACE("Processing file '%s'\n", wine_dbgstr_w(fd.cFileName));
|
2007-04-11 23:25:51 +02:00
|
|
|
|
|
|
|
/* FIXME: If dest name or extension is *, replace with filename/ext
|
|
|
|
part otherwise use supplied name. This supports:
|
|
|
|
ren *.fred *.jim
|
|
|
|
ren jim.* fred.* etc
|
2008-02-13 22:11:01 +01:00
|
|
|
However, windows has a more complex algorithm supporting eg
|
2007-04-11 23:25:51 +02:00
|
|
|
?'s and *'s mid name */
|
2019-05-03 12:28:33 +02:00
|
|
|
dotSrc = wcschr(fd.cFileName, '.');
|
2007-04-11 23:25:51 +02:00
|
|
|
|
|
|
|
/* Build src & dest name */
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(src, drive);
|
|
|
|
lstrcatW(src, dir);
|
|
|
|
lstrcpyW(dest, src);
|
|
|
|
dirLen = lstrlenW(src);
|
|
|
|
lstrcatW(src, fd.cFileName);
|
2007-04-11 23:25:51 +02:00
|
|
|
|
|
|
|
/* Build name */
|
|
|
|
if (param2[0] == '*') {
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW(dest, fd.cFileName);
|
2007-04-11 23:25:51 +02:00
|
|
|
if (dotSrc) dest[dirLen + (dotSrc - fd.cFileName)] = 0x00;
|
|
|
|
} else {
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW(dest, param2);
|
2007-04-11 23:25:51 +02:00
|
|
|
if (dotDst) dest[dirLen + (dotDst - param2)] = 0x00;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Build Extension */
|
|
|
|
if (dotDst && (*(dotDst+1)=='*')) {
|
2019-05-03 12:28:33 +02:00
|
|
|
if (dotSrc) lstrcatW(dest, dotSrc);
|
2007-04-11 23:25:51 +02:00
|
|
|
} else if (dotDst) {
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW(dest, dotDst);
|
2007-04-11 23:25:51 +02:00
|
|
|
}
|
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
WINE_TRACE("Source '%s'\n", wine_dbgstr_w(src));
|
|
|
|
WINE_TRACE("Dest '%s'\n", wine_dbgstr_w(dest));
|
2007-04-11 23:25:51 +02:00
|
|
|
|
2011-10-14 15:37:59 +02:00
|
|
|
status = MoveFileW(src, dest);
|
2007-04-11 23:25:51 +02:00
|
|
|
|
|
|
|
if (!status) {
|
|
|
|
WCMD_print_error ();
|
|
|
|
errorlevel = 1;
|
|
|
|
}
|
2011-10-05 10:16:30 +02:00
|
|
|
} while (FindNextFileW(hff, &fd) != 0);
|
2007-04-11 23:25:51 +02:00
|
|
|
|
2011-10-05 10:16:30 +02:00
|
|
|
FindClose(hff);
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
|
2004-03-22 23:56:58 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* WCMD_dupenv
|
|
|
|
*
|
|
|
|
* Make a copy of the environment.
|
|
|
|
*/
|
2004-11-29 19:00:10 +01:00
|
|
|
static WCHAR *WCMD_dupenv( const WCHAR *env )
|
2004-03-22 23:56:58 +01:00
|
|
|
{
|
|
|
|
WCHAR *env_copy;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if( !env )
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
while ( env[len] )
|
2019-05-03 12:28:33 +02:00
|
|
|
len += (lstrlenW(&env[len]) + 1);
|
2004-03-22 23:56:58 +01:00
|
|
|
|
|
|
|
env_copy = LocalAlloc (LMEM_FIXED, (len+1) * sizeof (WCHAR) );
|
|
|
|
if (!env_copy)
|
|
|
|
{
|
2007-06-03 23:07:39 +02:00
|
|
|
WINE_ERR("out of memory\n");
|
2004-03-22 23:56:58 +01:00
|
|
|
return env_copy;
|
|
|
|
}
|
|
|
|
memcpy (env_copy, env, len*sizeof (WCHAR));
|
|
|
|
env_copy[len] = 0;
|
|
|
|
|
|
|
|
return env_copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* WCMD_setlocal
|
|
|
|
*
|
|
|
|
* setlocal pushes the environment onto a stack
|
|
|
|
* Save the environment as unicode so we don't screw anything up.
|
|
|
|
*/
|
2007-06-03 23:07:42 +02:00
|
|
|
void WCMD_setlocal (const WCHAR *s) {
|
2004-03-22 23:56:58 +01:00
|
|
|
WCHAR *env;
|
|
|
|
struct env_stack *env_copy;
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR cwd[MAX_PATH];
|
2013-01-07 00:06:28 +01:00
|
|
|
BOOL newdelay;
|
2004-03-22 23:56:58 +01:00
|
|
|
|
2012-10-01 00:07:01 +02:00
|
|
|
/* setlocal does nothing outside of batch programs */
|
|
|
|
if (!context) return;
|
|
|
|
|
2004-03-22 23:56:58 +01:00
|
|
|
/* DISABLEEXTENSIONS ignored */
|
|
|
|
|
2013-01-07 00:06:28 +01:00
|
|
|
/* ENABLEDELAYEDEXPANSION / DISABLEDELAYEDEXPANSION could be parm1 or parm2
|
|
|
|
(if both ENABLEEXTENSIONS and ENABLEDELAYEDEXPANSION supplied for example) */
|
2020-11-29 23:30:29 +01:00
|
|
|
if (!wcsicmp(param1, L"ENABLEDELAYEDEXPANSION") || !wcsicmp(param2, L"ENABLEDELAYEDEXPANSION")) {
|
2013-01-07 00:06:28 +01:00
|
|
|
newdelay = TRUE;
|
2020-11-29 23:30:29 +01:00
|
|
|
} else if (!wcsicmp(param1, L"DISABLEDELAYEDEXPANSION") || !wcsicmp(param2, L"DISABLEDELAYEDEXPANSION")) {
|
2013-01-07 00:06:28 +01:00
|
|
|
newdelay = FALSE;
|
|
|
|
} else {
|
|
|
|
newdelay = delayedsubst;
|
|
|
|
}
|
|
|
|
WINE_TRACE("Setting delayed expansion to %d\n", newdelay);
|
|
|
|
|
2004-03-22 23:56:58 +01:00
|
|
|
env_copy = LocalAlloc (LMEM_FIXED, sizeof (struct env_stack));
|
|
|
|
if( !env_copy )
|
|
|
|
{
|
2007-06-03 23:07:39 +02:00
|
|
|
WINE_ERR ("out of memory\n");
|
2004-03-22 23:56:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
env = GetEnvironmentStringsW ();
|
|
|
|
env_copy->strings = WCMD_dupenv (env);
|
|
|
|
if (env_copy->strings)
|
|
|
|
{
|
2012-10-01 00:07:01 +02:00
|
|
|
env_copy->batchhandle = context->h;
|
2004-03-22 23:56:58 +01:00
|
|
|
env_copy->next = saved_environment;
|
2013-01-07 00:06:28 +01:00
|
|
|
env_copy->delayedsubst = delayedsubst;
|
|
|
|
delayedsubst = newdelay;
|
2004-03-22 23:56:58 +01:00
|
|
|
saved_environment = env_copy;
|
2007-03-08 01:48:49 +01:00
|
|
|
|
|
|
|
/* Save the current drive letter */
|
2009-12-09 18:52:40 +01:00
|
|
|
GetCurrentDirectoryW(MAX_PATH, cwd);
|
2007-03-12 10:35:13 +01:00
|
|
|
env_copy->u.cwd = cwd[0];
|
2004-03-22 23:56:58 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
LocalFree (env_copy);
|
|
|
|
|
|
|
|
FreeEnvironmentStringsW (env);
|
2007-03-08 01:48:49 +01:00
|
|
|
|
2004-03-22 23:56:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* WCMD_endlocal
|
|
|
|
*
|
|
|
|
* endlocal pops the environment off a stack
|
2007-06-03 23:07:42 +02:00
|
|
|
* Note: When searching for '=', search from WCHAR position 1, to handle
|
2007-03-08 01:48:49 +01:00
|
|
|
* special internal environment variables =C:, =D: etc
|
2004-03-22 23:56:58 +01:00
|
|
|
*/
|
|
|
|
void WCMD_endlocal (void) {
|
|
|
|
WCHAR *env, *old, *p;
|
|
|
|
struct env_stack *temp;
|
|
|
|
int len, n;
|
|
|
|
|
2012-10-01 00:07:01 +02:00
|
|
|
/* setlocal does nothing outside of batch programs */
|
|
|
|
if (!context) return;
|
|
|
|
|
|
|
|
/* setlocal needs a saved environment from within the same context (batch
|
|
|
|
program) as it was saved in */
|
|
|
|
if (!saved_environment || saved_environment->batchhandle != context->h)
|
2004-03-22 23:56:58 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* pop the old environment from the stack */
|
|
|
|
temp = saved_environment;
|
|
|
|
saved_environment = temp->next;
|
|
|
|
|
|
|
|
/* delete the current environment, totally */
|
|
|
|
env = GetEnvironmentStringsW ();
|
2012-11-01 04:10:01 +01:00
|
|
|
old = WCMD_dupenv (env);
|
2004-03-22 23:56:58 +01:00
|
|
|
len = 0;
|
|
|
|
while (old[len]) {
|
2019-05-03 12:28:33 +02:00
|
|
|
n = lstrlenW(&old[len]) + 1;
|
|
|
|
p = wcschr(&old[len] + 1, '=');
|
2004-03-22 23:56:58 +01:00
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
*p++ = 0;
|
|
|
|
SetEnvironmentVariableW (&old[len], NULL);
|
|
|
|
}
|
|
|
|
len += n;
|
|
|
|
}
|
|
|
|
LocalFree (old);
|
|
|
|
FreeEnvironmentStringsW (env);
|
2007-02-28 00:18:57 +01:00
|
|
|
|
2004-03-22 23:56:58 +01:00
|
|
|
/* restore old environment */
|
|
|
|
env = temp->strings;
|
|
|
|
len = 0;
|
2013-01-07 00:06:28 +01:00
|
|
|
delayedsubst = temp->delayedsubst;
|
|
|
|
WINE_TRACE("Delayed expansion now %d\n", delayedsubst);
|
2004-03-22 23:56:58 +01:00
|
|
|
while (env[len]) {
|
2019-05-03 12:28:33 +02:00
|
|
|
n = lstrlenW(&env[len]) + 1;
|
|
|
|
p = wcschr(&env[len] + 1, '=');
|
2004-03-22 23:56:58 +01:00
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
*p++ = 0;
|
|
|
|
SetEnvironmentVariableW (&env[len], p);
|
|
|
|
}
|
|
|
|
len += n;
|
|
|
|
}
|
2007-03-08 01:48:49 +01:00
|
|
|
|
|
|
|
/* Restore current drive letter */
|
2009-12-09 18:52:40 +01:00
|
|
|
if (IsCharAlphaW(temp->u.cwd)) {
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR envvar[4];
|
|
|
|
WCHAR cwd[MAX_PATH];
|
|
|
|
|
2020-11-29 23:30:29 +01:00
|
|
|
wsprintfW(envvar, L"=%c:", temp->u.cwd);
|
2009-12-09 18:52:40 +01:00
|
|
|
if (GetEnvironmentVariableW(envvar, cwd, MAX_PATH)) {
|
2007-06-03 23:07:42 +02:00
|
|
|
WINE_TRACE("Resetting cwd to %s\n", wine_dbgstr_w(cwd));
|
2009-12-09 18:52:40 +01:00
|
|
|
SetCurrentDirectoryW(cwd);
|
2007-03-08 01:48:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-22 23:56:58 +01:00
|
|
|
LocalFree (env);
|
|
|
|
LocalFree (temp);
|
|
|
|
}
|
|
|
|
|
1999-06-06 17:24:04 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* WCMD_setshow_default
|
|
|
|
*
|
|
|
|
* Set/Show the current default directory
|
|
|
|
*/
|
|
|
|
|
2012-10-17 00:41:58 +02:00
|
|
|
void WCMD_setshow_default (const WCHAR *args) {
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2007-03-01 12:43:19 +01:00
|
|
|
BOOL status;
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR string[1024];
|
|
|
|
WCHAR cwd[1024];
|
|
|
|
WCHAR *pos;
|
2009-12-09 18:52:40 +01:00
|
|
|
WIN32_FIND_DATAW fd;
|
2007-03-08 01:43:09 +01:00
|
|
|
HANDLE hff;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2012-10-17 00:41:58 +02:00
|
|
|
WINE_TRACE("Request change to directory '%s'\n", wine_dbgstr_w(args));
|
2007-03-08 01:50:14 +01:00
|
|
|
|
|
|
|
/* Skip /D and trailing whitespace if on the front of the command line */
|
2019-05-03 12:28:33 +02:00
|
|
|
if (lstrlenW(args) >= 2 &&
|
2015-06-11 22:14:11 +02:00
|
|
|
CompareStringW(LOCALE_USER_DEFAULT,
|
2007-03-08 01:50:14 +01:00
|
|
|
NORM_IGNORECASE | SORT_STRINGSORT,
|
2020-11-29 23:30:29 +01:00
|
|
|
args, 2, L"/D", -1) == CSTR_EQUAL) {
|
2012-10-17 00:41:58 +02:00
|
|
|
args += 2;
|
|
|
|
while (*args && (*args==' ' || *args=='\t'))
|
|
|
|
args++;
|
2007-03-08 01:50:14 +01:00
|
|
|
}
|
|
|
|
|
2018-08-20 21:15:42 +02:00
|
|
|
GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
|
2019-12-09 09:48:16 +01:00
|
|
|
|
|
|
|
if (!*args) {
|
2020-11-29 23:30:28 +01:00
|
|
|
lstrcatW(cwd, L"\r\n");
|
2011-11-24 00:30:23 +01:00
|
|
|
WCMD_output_asis (cwd);
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
else {
|
2007-03-08 01:42:33 +01:00
|
|
|
/* Remove any double quotes, which may be in the
|
|
|
|
middle, eg. cd "C:\Program Files"\Microsoft is ok */
|
|
|
|
pos = string;
|
2012-10-17 00:41:58 +02:00
|
|
|
while (*args) {
|
|
|
|
if (*args != '"') *pos++ = *args;
|
|
|
|
args++;
|
2007-03-08 01:42:33 +01:00
|
|
|
}
|
2012-09-05 00:40:43 +02:00
|
|
|
while (pos > string && (*(pos-1) == ' ' || *(pos-1) == '\t'))
|
2011-08-27 02:11:28 +02:00
|
|
|
pos--;
|
2007-03-08 01:42:33 +01:00
|
|
|
*pos = 0x00;
|
|
|
|
|
2008-04-07 13:01:02 +02:00
|
|
|
/* Search for appropriate directory */
|
2007-06-03 23:07:42 +02:00
|
|
|
WINE_TRACE("Looking for directory '%s'\n", wine_dbgstr_w(string));
|
2009-12-09 18:52:40 +01:00
|
|
|
hff = FindFirstFileW(string, &fd);
|
2011-10-05 10:16:30 +02:00
|
|
|
if (hff != INVALID_HANDLE_VALUE) {
|
|
|
|
do {
|
|
|
|
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
|
|
|
WCHAR fpath[MAX_PATH];
|
|
|
|
WCHAR drive[10];
|
|
|
|
WCHAR dir[MAX_PATH];
|
|
|
|
WCHAR fname[MAX_PATH];
|
|
|
|
WCHAR ext[MAX_PATH];
|
|
|
|
|
|
|
|
/* Convert path into actual directory spec */
|
2018-08-20 21:15:42 +02:00
|
|
|
GetFullPathNameW(string, ARRAY_SIZE(fpath), fpath, NULL);
|
2020-04-26 05:53:36 +02:00
|
|
|
_wsplitpath(fpath, drive, dir, fname, ext);
|
2011-10-05 10:16:30 +02:00
|
|
|
|
|
|
|
/* Rebuild path */
|
2020-11-29 23:30:29 +01:00
|
|
|
wsprintfW(string, L"%s%s%s", drive, dir, fd.cFileName);
|
2011-10-05 10:16:30 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (FindNextFileW(hff, &fd) != 0);
|
|
|
|
FindClose(hff);
|
2007-03-08 01:43:09 +01:00
|
|
|
}
|
|
|
|
|
2007-03-08 01:42:33 +01:00
|
|
|
/* Change to that directory */
|
2007-06-03 23:07:42 +02:00
|
|
|
WINE_TRACE("Really changing to directory '%s'\n", wine_dbgstr_w(string));
|
2007-03-08 01:47:45 +01:00
|
|
|
|
2009-12-09 18:52:40 +01:00
|
|
|
status = SetCurrentDirectoryW(string);
|
1999-06-06 17:24:04 +02:00
|
|
|
if (!status) {
|
2007-03-08 01:43:32 +01:00
|
|
|
errorlevel = 1;
|
1999-06-06 17:24:04 +02:00
|
|
|
WCMD_print_error ();
|
|
|
|
return;
|
2007-03-08 01:50:14 +01:00
|
|
|
} else {
|
|
|
|
|
2008-03-04 00:14:30 +01:00
|
|
|
/* Save away the actual new directory, to store as current location */
|
2018-08-20 21:15:42 +02:00
|
|
|
GetCurrentDirectoryW(ARRAY_SIZE(string), string);
|
2008-03-04 00:14:30 +01:00
|
|
|
|
2007-03-08 01:50:14 +01:00
|
|
|
/* Restore old directory if drive letter would change, and
|
|
|
|
CD x:\directory /D (or pushd c:\directory) not supplied */
|
2020-11-29 23:30:29 +01:00
|
|
|
if ((wcsstr(quals, L"/D") == NULL) &&
|
2007-03-08 01:50:14 +01:00
|
|
|
(param1[1] == ':') && (toupper(param1[0]) != toupper(cwd[0]))) {
|
2009-12-09 18:52:40 +01:00
|
|
|
SetCurrentDirectoryW(cwd);
|
2007-03-08 01:50:14 +01:00
|
|
|
}
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
2007-03-08 01:47:45 +01:00
|
|
|
|
2007-03-08 01:50:14 +01:00
|
|
|
/* Set special =C: type environment variable, for drive letter of
|
|
|
|
change of directory, even if path was restored due to missing
|
|
|
|
/D (allows changing drive letter when not resident on that
|
|
|
|
drive */
|
2009-12-09 18:52:40 +01:00
|
|
|
if ((string[1] == ':') && IsCharAlphaW(string[0])) {
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR env[4];
|
2020-11-29 23:30:28 +01:00
|
|
|
lstrcpyW(env, L"=");
|
2007-06-03 23:07:42 +02:00
|
|
|
memcpy(env+1, string, 2 * sizeof(WCHAR));
|
2007-03-08 01:47:45 +01:00
|
|
|
env[3] = 0x00;
|
2007-06-08 12:54:21 +02:00
|
|
|
WINE_TRACE("Setting '%s' to '%s'\n", wine_dbgstr_w(env), wine_dbgstr_w(string));
|
2009-12-09 18:52:40 +01:00
|
|
|
SetEnvironmentVariableW(env, string);
|
2007-03-08 01:47:45 +01:00
|
|
|
}
|
|
|
|
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_setshow_date
|
|
|
|
*
|
|
|
|
* Set/Show the system date
|
|
|
|
* FIXME: Can't change date yet
|
|
|
|
*/
|
|
|
|
|
2003-02-27 02:41:21 +01:00
|
|
|
void WCMD_setshow_date (void) {
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR curdate[64], buffer[64];
|
2007-03-01 12:43:19 +01:00
|
|
|
DWORD count;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2019-12-09 09:48:16 +01:00
|
|
|
if (!*param1) {
|
2018-08-20 21:15:42 +02:00
|
|
|
if (GetDateFormatW(LOCALE_USER_DEFAULT, 0, NULL, NULL, curdate, ARRAY_SIZE(curdate))) {
|
2007-06-03 23:07:39 +02:00
|
|
|
WCMD_output (WCMD_LoadMessage(WCMD_CURRENTDATE), curdate);
|
2020-11-29 23:30:29 +01:00
|
|
|
if (wcsstr(quals, L"/T") == NULL) {
|
2007-06-03 23:07:39 +02:00
|
|
|
WCMD_output (WCMD_LoadMessage(WCMD_NEWDATE));
|
2018-08-20 21:15:42 +02:00
|
|
|
WCMD_ReadFile(GetStdHandle(STD_INPUT_HANDLE), buffer, ARRAY_SIZE(buffer), &count);
|
2007-03-08 01:45:05 +01:00
|
|
|
if (count > 2) {
|
2011-12-02 10:07:27 +01:00
|
|
|
WCMD_output_stderr (WCMD_LoadMessage(WCMD_NYI));
|
2007-03-08 01:45:05 +01:00
|
|
|
}
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else WCMD_print_error ();
|
|
|
|
}
|
|
|
|
else {
|
2011-12-02 10:07:27 +01:00
|
|
|
WCMD_output_stderr (WCMD_LoadMessage(WCMD_NYI));
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-30 20:17:31 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_compare
|
2012-09-26 00:16:54 +02:00
|
|
|
* Note: Native displays 'fred' before 'fred ', so need to only compare up to
|
|
|
|
* the equals sign.
|
2003-12-30 20:17:31 +01:00
|
|
|
*/
|
2019-05-06 10:40:25 +02:00
|
|
|
static int __cdecl WCMD_compare( const void *a, const void *b )
|
2003-12-30 20:17:31 +01:00
|
|
|
{
|
|
|
|
int r;
|
2007-06-03 23:07:42 +02:00
|
|
|
const WCHAR * const *str_a = a, * const *str_b = b;
|
2009-12-09 18:52:40 +01:00
|
|
|
r = CompareStringW( LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
|
2020-11-29 23:30:28 +01:00
|
|
|
*str_a, wcscspn(*str_a, L"="), *str_b, wcscspn(*str_b, L"=") );
|
2003-12-30 20:17:31 +01:00
|
|
|
if( r == CSTR_LESS_THAN ) return -1;
|
|
|
|
if( r == CSTR_GREATER_THAN ) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_setshow_sortenv
|
|
|
|
*
|
|
|
|
* sort variables into order for display
|
2007-02-27 00:06:11 +01:00
|
|
|
* Optionally only display those who start with a stub
|
|
|
|
* returns the count displayed
|
2003-12-30 20:17:31 +01:00
|
|
|
*/
|
2007-06-03 23:07:42 +02:00
|
|
|
static int WCMD_setshow_sortenv(const WCHAR *s, const WCHAR *stub)
|
2003-12-30 20:17:31 +01:00
|
|
|
{
|
2007-02-27 00:06:11 +01:00
|
|
|
UINT count=0, len=0, i, displayedcount=0, stublen=0;
|
2007-06-03 23:07:42 +02:00
|
|
|
const WCHAR **str;
|
2003-12-30 20:17:31 +01:00
|
|
|
|
2019-05-03 12:28:33 +02:00
|
|
|
if (stub) stublen = lstrlenW(stub);
|
2007-02-27 00:06:11 +01:00
|
|
|
|
2003-12-30 20:17:31 +01:00
|
|
|
/* count the number of strings, and the total length */
|
|
|
|
while ( s[len] ) {
|
2019-05-03 12:28:33 +02:00
|
|
|
len += (lstrlenW(&s[len]) + 1);
|
2003-12-30 20:17:31 +01:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add the strings to an array */
|
2007-06-03 23:07:42 +02:00
|
|
|
str = LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, count * sizeof (WCHAR*) );
|
2003-12-30 20:17:31 +01:00
|
|
|
if( !str )
|
2007-02-27 00:06:11 +01:00
|
|
|
return 0;
|
2003-12-30 20:17:31 +01:00
|
|
|
str[0] = s;
|
|
|
|
for( i=1; i<count; i++ )
|
2019-05-03 12:28:33 +02:00
|
|
|
str[i] = str[i-1] + lstrlenW(str[i-1]) + 1;
|
2003-12-30 20:17:31 +01:00
|
|
|
|
|
|
|
/* sort the array */
|
2007-06-03 23:07:42 +02:00
|
|
|
qsort( str, count, sizeof (WCHAR*), WCMD_compare );
|
2003-12-30 20:17:31 +01:00
|
|
|
|
|
|
|
/* print it */
|
2005-12-01 15:58:16 +01:00
|
|
|
for( i=0; i<count; i++ ) {
|
2009-12-09 18:52:40 +01:00
|
|
|
if (!stub || CompareStringW(LOCALE_USER_DEFAULT,
|
2007-02-27 00:06:11 +01:00
|
|
|
NORM_IGNORECASE | SORT_STRINGSORT,
|
2011-08-20 01:58:53 +02:00
|
|
|
str[i], stublen, stub, -1) == CSTR_EQUAL) {
|
2007-03-08 01:48:17 +01:00
|
|
|
/* Don't display special internal variables */
|
|
|
|
if (str[i][0] != '=') {
|
|
|
|
WCMD_output_asis(str[i]);
|
2020-11-29 23:30:28 +01:00
|
|
|
WCMD_output_asis(L"\r\n");
|
2007-03-08 01:48:17 +01:00
|
|
|
displayedcount++;
|
|
|
|
}
|
2007-02-27 00:06:11 +01:00
|
|
|
}
|
2005-12-01 15:58:16 +01:00
|
|
|
}
|
2003-12-30 20:17:31 +01:00
|
|
|
|
|
|
|
LocalFree( str );
|
2007-02-27 00:06:11 +01:00
|
|
|
return displayedcount;
|
2003-12-30 20:17:31 +01:00
|
|
|
}
|
|
|
|
|
2012-12-21 00:38:14 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_getprecedence
|
|
|
|
* Return the precedence of a particular operator
|
|
|
|
*/
|
|
|
|
static int WCMD_getprecedence(const WCHAR in)
|
|
|
|
{
|
|
|
|
switch (in) {
|
|
|
|
case '!':
|
|
|
|
case '~':
|
|
|
|
case OP_POSITIVE:
|
|
|
|
case OP_NEGATIVE:
|
|
|
|
return 8;
|
|
|
|
case '*':
|
|
|
|
case '/':
|
|
|
|
case '%':
|
|
|
|
return 7;
|
|
|
|
case '+':
|
|
|
|
case '-':
|
|
|
|
return 6;
|
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
return 5;
|
|
|
|
case '&':
|
|
|
|
return 4;
|
|
|
|
case '^':
|
|
|
|
return 3;
|
|
|
|
case '|':
|
|
|
|
return 2;
|
|
|
|
case '=':
|
|
|
|
case OP_ASSSIGNMUL:
|
|
|
|
case OP_ASSSIGNDIV:
|
|
|
|
case OP_ASSSIGNMOD:
|
|
|
|
case OP_ASSSIGNADD:
|
|
|
|
case OP_ASSSIGNSUB:
|
|
|
|
case OP_ASSSIGNAND:
|
|
|
|
case OP_ASSSIGNNOT:
|
|
|
|
case OP_ASSSIGNOR:
|
|
|
|
case OP_ASSSIGNSHL:
|
|
|
|
case OP_ASSSIGNSHR:
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_pushnumber
|
|
|
|
* Push either a number or name (environment variable) onto the supplied
|
|
|
|
* stack
|
|
|
|
*/
|
|
|
|
static void WCMD_pushnumber(WCHAR *var, int num, VARSTACK **varstack) {
|
2018-11-25 21:56:00 +01:00
|
|
|
VARSTACK *thisstack = heap_xalloc(sizeof(VARSTACK));
|
2012-12-21 00:38:14 +01:00
|
|
|
thisstack->isnum = (var == NULL);
|
|
|
|
if (var) {
|
|
|
|
thisstack->variable = var;
|
|
|
|
WINE_TRACE("Pushed variable %s\n", wine_dbgstr_w(var));
|
|
|
|
} else {
|
|
|
|
thisstack->value = num;
|
|
|
|
WINE_TRACE("Pushed number %d\n", num);
|
|
|
|
}
|
|
|
|
thisstack->next = *varstack;
|
|
|
|
*varstack = thisstack;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_peeknumber
|
|
|
|
* Returns the value of the top number or environment variable on the stack
|
|
|
|
* and leaves the item on the stack.
|
|
|
|
*/
|
|
|
|
static int WCMD_peeknumber(VARSTACK **varstack) {
|
|
|
|
int result = 0;
|
|
|
|
VARSTACK *thisvar;
|
|
|
|
|
|
|
|
if (varstack) {
|
|
|
|
thisvar = *varstack;
|
|
|
|
if (!thisvar->isnum) {
|
|
|
|
WCHAR tmpstr[MAXSTRING];
|
|
|
|
if (GetEnvironmentVariableW(thisvar->variable, tmpstr, MAXSTRING)) {
|
2019-11-25 20:47:23 +01:00
|
|
|
result = wcstol(tmpstr,NULL,0);
|
2012-12-21 00:38:14 +01:00
|
|
|
}
|
|
|
|
WINE_TRACE("Envvar %s converted to %d\n", wine_dbgstr_w(thisvar->variable), result);
|
|
|
|
} else {
|
|
|
|
result = thisvar->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WINE_TRACE("Peeked number %d\n", result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_popnumber
|
|
|
|
* Returns the value of the top number or environment variable on the stack
|
|
|
|
* and removes the item from the stack.
|
|
|
|
*/
|
|
|
|
static int WCMD_popnumber(VARSTACK **varstack) {
|
|
|
|
int result = 0;
|
|
|
|
VARSTACK *thisvar;
|
|
|
|
|
|
|
|
if (varstack) {
|
|
|
|
thisvar = *varstack;
|
|
|
|
result = WCMD_peeknumber(varstack);
|
|
|
|
if (!thisvar->isnum) heap_free(thisvar->variable);
|
|
|
|
*varstack = thisvar->next;
|
|
|
|
heap_free(thisvar);
|
|
|
|
}
|
|
|
|
WINE_TRACE("Popped number %d\n", result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_pushoperator
|
|
|
|
* Push an operator onto the supplied stack
|
|
|
|
*/
|
|
|
|
static void WCMD_pushoperator(WCHAR op, int precedence, OPSTACK **opstack) {
|
2018-11-25 21:56:00 +01:00
|
|
|
OPSTACK *thisstack = heap_xalloc(sizeof(OPSTACK));
|
2012-12-21 00:38:14 +01:00
|
|
|
thisstack->precedence = precedence;
|
|
|
|
thisstack->op = op;
|
|
|
|
thisstack->next = *opstack;
|
|
|
|
WINE_TRACE("Pushed operator %c\n", op);
|
|
|
|
*opstack = thisstack;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_popoperator
|
|
|
|
* Returns the operator from the top of the stack and removes the item from
|
|
|
|
* the stack.
|
|
|
|
*/
|
|
|
|
static WCHAR WCMD_popoperator(OPSTACK **opstack) {
|
|
|
|
WCHAR result = 0;
|
|
|
|
OPSTACK *thisop;
|
|
|
|
|
|
|
|
if (opstack) {
|
|
|
|
thisop = *opstack;
|
|
|
|
result = thisop->op;
|
|
|
|
*opstack = thisop->next;
|
|
|
|
heap_free(thisop);
|
|
|
|
}
|
|
|
|
WINE_TRACE("Popped operator %c\n", result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_reduce
|
|
|
|
* Actions the top operator on the stack against the first and sometimes
|
|
|
|
* second value on the variable stack, and pushes the result
|
|
|
|
* Returns non-zero on error.
|
|
|
|
*/
|
|
|
|
static int WCMD_reduce(OPSTACK **opstack, VARSTACK **varstack) {
|
2016-12-17 09:33:27 +01:00
|
|
|
WCHAR thisop;
|
2012-12-21 00:38:14 +01:00
|
|
|
int var1,var2;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
if (!*opstack || !*varstack) {
|
|
|
|
WINE_TRACE("No operators for the reduce\n");
|
|
|
|
return WCMD_NOOPERATOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the top operator */
|
2016-12-17 09:33:27 +01:00
|
|
|
thisop = WCMD_popoperator(opstack);
|
|
|
|
WINE_TRACE("Reducing the stacks - processing operator %c\n", thisop);
|
2012-12-21 00:38:14 +01:00
|
|
|
|
|
|
|
/* One variable operators */
|
|
|
|
var1 = WCMD_popnumber(varstack);
|
2016-12-17 09:33:27 +01:00
|
|
|
switch (thisop) {
|
2012-12-21 00:38:14 +01:00
|
|
|
case '!': WCMD_pushnumber(NULL, !var1, varstack);
|
|
|
|
break;
|
|
|
|
case '~': WCMD_pushnumber(NULL, ~var1, varstack);
|
|
|
|
break;
|
|
|
|
case OP_POSITIVE: WCMD_pushnumber(NULL, var1, varstack);
|
|
|
|
break;
|
|
|
|
case OP_NEGATIVE: WCMD_pushnumber(NULL, -var1, varstack);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Two variable operators */
|
|
|
|
if (!*varstack) {
|
|
|
|
WINE_TRACE("No operands left for the reduce?\n");
|
|
|
|
return WCMD_NOOPERAND;
|
|
|
|
}
|
2016-12-17 09:33:27 +01:00
|
|
|
switch (thisop) {
|
2012-12-21 00:38:14 +01:00
|
|
|
case '!':
|
|
|
|
case '~':
|
|
|
|
case OP_POSITIVE:
|
|
|
|
case OP_NEGATIVE:
|
|
|
|
break; /* Handled above */
|
|
|
|
case '*': var2 = WCMD_popnumber(varstack);
|
|
|
|
WCMD_pushnumber(NULL, var2*var1, varstack);
|
|
|
|
break;
|
|
|
|
case '/': var2 = WCMD_popnumber(varstack);
|
|
|
|
if (var1 == 0) return WCMD_DIVIDEBYZERO;
|
|
|
|
WCMD_pushnumber(NULL, var2/var1, varstack);
|
|
|
|
break;
|
|
|
|
case '+': var2 = WCMD_popnumber(varstack);
|
|
|
|
WCMD_pushnumber(NULL, var2+var1, varstack);
|
|
|
|
break;
|
|
|
|
case '-': var2 = WCMD_popnumber(varstack);
|
|
|
|
WCMD_pushnumber(NULL, var2-var1, varstack);
|
|
|
|
break;
|
|
|
|
case '&': var2 = WCMD_popnumber(varstack);
|
|
|
|
WCMD_pushnumber(NULL, var2&var1, varstack);
|
|
|
|
break;
|
|
|
|
case '%': var2 = WCMD_popnumber(varstack);
|
2013-01-06 21:23:53 +01:00
|
|
|
if (var1 == 0) return WCMD_DIVIDEBYZERO;
|
2012-12-21 00:38:14 +01:00
|
|
|
WCMD_pushnumber(NULL, var2%var1, varstack);
|
|
|
|
break;
|
|
|
|
case '^': var2 = WCMD_popnumber(varstack);
|
|
|
|
WCMD_pushnumber(NULL, var2^var1, varstack);
|
|
|
|
break;
|
|
|
|
case '<': var2 = WCMD_popnumber(varstack);
|
|
|
|
/* Shift left has to be a positive number, 0-31 otherwise 0 is returned,
|
|
|
|
which differs from the compiler (for example gcc) so being explicit. */
|
|
|
|
if (var1 < 0 || var1 >= (8 * sizeof(INT))) {
|
|
|
|
WCMD_pushnumber(NULL, 0, varstack);
|
|
|
|
} else {
|
|
|
|
WCMD_pushnumber(NULL, var2<<var1, varstack);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '>': var2 = WCMD_popnumber(varstack);
|
|
|
|
WCMD_pushnumber(NULL, var2>>var1, varstack);
|
|
|
|
break;
|
|
|
|
case '|': var2 = WCMD_popnumber(varstack);
|
|
|
|
WCMD_pushnumber(NULL, var2|var1, varstack);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OP_ASSSIGNMUL:
|
|
|
|
case OP_ASSSIGNDIV:
|
|
|
|
case OP_ASSSIGNMOD:
|
|
|
|
case OP_ASSSIGNADD:
|
|
|
|
case OP_ASSSIGNSUB:
|
|
|
|
case OP_ASSSIGNAND:
|
|
|
|
case OP_ASSSIGNNOT:
|
|
|
|
case OP_ASSSIGNOR:
|
|
|
|
case OP_ASSSIGNSHL:
|
|
|
|
case OP_ASSSIGNSHR:
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
/* The left of an equals must be one variable */
|
|
|
|
if (!(*varstack) || (*varstack)->isnum) {
|
|
|
|
return WCMD_NOOPERAND;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make the number stack grow by inserting the value of the variable */
|
|
|
|
var2 = WCMD_peeknumber(varstack);
|
|
|
|
WCMD_pushnumber(NULL, var2, varstack);
|
|
|
|
WCMD_pushnumber(NULL, var1, varstack);
|
|
|
|
|
|
|
|
/* Make the operand stack grow by pushing the assign operator plus the
|
|
|
|
operator to perform */
|
|
|
|
while (calcassignments[i].op != ' ' &&
|
2016-12-17 09:33:27 +01:00
|
|
|
calcassignments[i].calculatedop != thisop) {
|
2012-12-21 00:38:14 +01:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (calcassignments[i].calculatedop == ' ') {
|
2016-12-17 09:33:27 +01:00
|
|
|
WINE_ERR("Unexpected operator %c\n", thisop);
|
2012-12-21 00:38:14 +01:00
|
|
|
return WCMD_NOOPERATOR;
|
|
|
|
}
|
|
|
|
WCMD_pushoperator('=', WCMD_getprecedence('='), opstack);
|
|
|
|
WCMD_pushoperator(calcassignments[i].op,
|
|
|
|
WCMD_getprecedence(calcassignments[i].op), opstack);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case '=':
|
|
|
|
{
|
|
|
|
WCHAR result[MAXSTRING];
|
|
|
|
|
|
|
|
/* Build the result, then push it onto the stack */
|
2020-11-29 23:30:29 +01:00
|
|
|
swprintf(result, ARRAY_SIZE(result), L"%d", var1);
|
2012-12-21 00:38:14 +01:00
|
|
|
WINE_TRACE("Assigning %s a value %s\n", wine_dbgstr_w((*varstack)->variable),
|
|
|
|
wine_dbgstr_w(result));
|
|
|
|
SetEnvironmentVariableW((*varstack)->variable, result);
|
|
|
|
var2 = WCMD_popnumber(varstack);
|
|
|
|
WCMD_pushnumber(NULL, var1, varstack);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-12-17 09:33:27 +01:00
|
|
|
default: WINE_ERR("Unrecognized operator %c\n", thisop);
|
2012-12-21 00:38:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_handleExpression
|
|
|
|
* Handles an expression provided to set /a - If it finds brackets, it uses
|
|
|
|
* recursion to process the parts in brackets.
|
|
|
|
*/
|
|
|
|
static int WCMD_handleExpression(WCHAR **expr, int *ret, int depth)
|
|
|
|
{
|
2020-11-29 23:30:29 +01:00
|
|
|
static const WCHAR mathDelims[] = L" \t()!~-*/%+<>&^|=,";
|
2012-12-21 00:38:14 +01:00
|
|
|
int rc = 0;
|
|
|
|
WCHAR *pos;
|
|
|
|
BOOL lastwasnumber = FALSE; /* FALSE makes a minus at the start of the expression easier to handle */
|
|
|
|
OPSTACK *opstackhead = NULL;
|
|
|
|
VARSTACK *varstackhead = NULL;
|
|
|
|
WCHAR foundhalf = 0;
|
|
|
|
|
|
|
|
/* Initialize */
|
|
|
|
WINE_TRACE("Handling expression '%s'\n", wine_dbgstr_w(*expr));
|
|
|
|
pos = *expr;
|
|
|
|
|
|
|
|
/* Iterate through until whole expression is processed */
|
|
|
|
while (pos && *pos) {
|
|
|
|
BOOL treatasnumber;
|
|
|
|
|
|
|
|
/* Skip whitespace to get to the next character to process*/
|
|
|
|
while (*pos && (*pos==' ' || *pos=='\t')) pos++;
|
|
|
|
if (!*pos) goto exprreturn;
|
|
|
|
|
2014-02-07 22:42:49 +01:00
|
|
|
/* If we have found anything other than an operator then it's a number/variable */
|
2019-05-03 12:28:33 +02:00
|
|
|
if (wcschr(mathDelims, *pos) == NULL) {
|
2012-12-21 00:38:14 +01:00
|
|
|
WCHAR *parmstart, *parm, *dupparm;
|
|
|
|
WCHAR *nextpos;
|
|
|
|
|
|
|
|
/* Cannot have an expression with var/number twice, without an operator
|
2013-01-05 16:17:18 +01:00
|
|
|
in-between, nor or number following a half constructed << or >> operator */
|
2012-12-21 00:38:14 +01:00
|
|
|
if (lastwasnumber || foundhalf) {
|
|
|
|
rc = WCMD_NOOPERATOR;
|
|
|
|
goto exprerrorreturn;
|
|
|
|
}
|
|
|
|
lastwasnumber = TRUE;
|
|
|
|
|
2019-05-03 12:28:33 +02:00
|
|
|
if (iswdigit(*pos)) {
|
2012-12-21 00:38:14 +01:00
|
|
|
/* For a number - just push it onto the stack */
|
2019-05-03 12:28:33 +02:00
|
|
|
int num = wcstoul(pos, &nextpos, 0);
|
2012-12-21 00:38:14 +01:00
|
|
|
WCMD_pushnumber(NULL, num, &varstackhead);
|
|
|
|
pos = nextpos;
|
|
|
|
|
|
|
|
/* Verify the number was validly formed */
|
2019-05-03 12:28:33 +02:00
|
|
|
if (*nextpos && (wcschr(mathDelims, *nextpos) == NULL)) {
|
2012-12-21 00:38:14 +01:00
|
|
|
rc = WCMD_BADHEXOCT;
|
|
|
|
goto exprerrorreturn;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* For a variable - just push it onto the stack */
|
|
|
|
parm = WCMD_parameter_with_delims(pos, 0, &parmstart, FALSE, FALSE, mathDelims);
|
|
|
|
dupparm = heap_strdupW(parm);
|
|
|
|
WCMD_pushnumber(dupparm, 0, &varstackhead);
|
2019-05-03 12:28:33 +02:00
|
|
|
pos = parmstart + lstrlenW(dupparm);
|
2012-12-21 00:38:14 +01:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have found an operator. Some operators are one character, some two, and the minus
|
|
|
|
and plus signs need special processing as they can be either operators or just influence
|
|
|
|
the parameter which follows them */
|
|
|
|
if (foundhalf && (*pos != foundhalf)) {
|
|
|
|
/* Badly constructed operator pair */
|
|
|
|
rc = WCMD_NOOPERATOR;
|
|
|
|
goto exprerrorreturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
treatasnumber = FALSE; /* We are processing an operand */
|
|
|
|
switch (*pos) {
|
|
|
|
|
|
|
|
/* > and < are special as they are double character operators (and spaces can be between them!)
|
|
|
|
If we see these for the first time, set a flag, and second time around we continue.
|
|
|
|
Note these double character operators are stored as just one of the characters on the stack */
|
|
|
|
case '>':
|
|
|
|
case '<': if (!foundhalf) {
|
|
|
|
foundhalf = *pos;
|
|
|
|
pos++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* We have found the rest, so clear up the knowledge of the half completed part and
|
|
|
|
drop through to normal operator processing */
|
|
|
|
foundhalf = 0;
|
|
|
|
/* drop through */
|
|
|
|
|
|
|
|
case '=': if (*pos=='=') {
|
|
|
|
/* = is special cased as if the last was an operator then we may have e.g. += or
|
|
|
|
*= etc which we need to handle by replacing the operator that is on the stack
|
|
|
|
with a calculated assignment equivalent */
|
|
|
|
if (!lastwasnumber && opstackhead) {
|
|
|
|
int i = 0;
|
|
|
|
while (calcassignments[i].op != ' ' && calcassignments[i].op != opstackhead->op) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (calcassignments[i].op == ' ') {
|
|
|
|
rc = WCMD_NOOPERAND;
|
|
|
|
goto exprerrorreturn;
|
|
|
|
} else {
|
|
|
|
/* Remove the operator on the stack, it will be replaced with a ?= equivalent
|
|
|
|
when the general operator handling happens further down. */
|
|
|
|
*pos = calcassignments[i].calculatedop;
|
|
|
|
WCMD_popoperator(&opstackhead);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Drop though */
|
|
|
|
|
|
|
|
/* + and - are slightly special as they can be a numeric prefix, if they follow an operator
|
|
|
|
so if they do, convert the +/- (arithmetic) to +/- (numeric prefix for positive/negative) */
|
|
|
|
case '+': if (!lastwasnumber && *pos=='+') *pos = OP_POSITIVE;
|
|
|
|
/* drop through */
|
|
|
|
case '-': if (!lastwasnumber && *pos=='-') *pos = OP_NEGATIVE;
|
|
|
|
/* drop through */
|
|
|
|
|
|
|
|
/* Normal operators - push onto stack unless precedence means we have to calculate it now */
|
|
|
|
case '!': /* drop through */
|
|
|
|
case '~': /* drop through */
|
|
|
|
case '/': /* drop through */
|
|
|
|
case '%': /* drop through */
|
|
|
|
case '&': /* drop through */
|
|
|
|
case '^': /* drop through */
|
|
|
|
case '*': /* drop through */
|
|
|
|
case '|':
|
|
|
|
/* General code for handling most of the operators - look at the
|
|
|
|
precedence of the top item on the stack, and see if we need to
|
|
|
|
action the stack before we push something else onto it. */
|
|
|
|
{
|
|
|
|
int precedence = WCMD_getprecedence(*pos);
|
|
|
|
WINE_TRACE("Found operator %c precedence %d (head is %d)\n", *pos,
|
|
|
|
precedence, !opstackhead?-1:opstackhead->precedence);
|
|
|
|
|
|
|
|
/* In general, for things with the same precedence, reduce immediately
|
|
|
|
except for assignments and unary operators which do not */
|
|
|
|
while (!rc && opstackhead &&
|
|
|
|
((opstackhead->precedence > precedence) ||
|
|
|
|
((opstackhead->precedence == precedence) &&
|
|
|
|
(precedence != 1) && (precedence != 8)))) {
|
|
|
|
rc = WCMD_reduce(&opstackhead, &varstackhead);
|
|
|
|
}
|
|
|
|
if (rc) goto exprerrorreturn;
|
|
|
|
WCMD_pushoperator(*pos, precedence, &opstackhead);
|
|
|
|
pos++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* comma means start a new expression, ie calculate what we have */
|
|
|
|
case ',':
|
|
|
|
{
|
|
|
|
int prevresult = -1;
|
2016-04-23 07:48:11 +02:00
|
|
|
WINE_TRACE("Found expression delimiter - reducing existing stacks\n");
|
2012-12-21 00:38:14 +01:00
|
|
|
while (!rc && opstackhead) {
|
|
|
|
rc = WCMD_reduce(&opstackhead, &varstackhead);
|
|
|
|
}
|
|
|
|
if (rc) goto exprerrorreturn;
|
|
|
|
/* If we have anything other than one number left, error
|
|
|
|
otherwise throw the number away */
|
|
|
|
if (!varstackhead || varstackhead->next) {
|
|
|
|
rc = WCMD_NOOPERATOR;
|
|
|
|
goto exprerrorreturn;
|
|
|
|
}
|
|
|
|
prevresult = WCMD_popnumber(&varstackhead);
|
|
|
|
WINE_TRACE("Expression resolved to %d\n", prevresult);
|
|
|
|
heap_free(varstackhead);
|
|
|
|
varstackhead = NULL;
|
|
|
|
pos++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open bracket - use iteration to parse the inner expression, then continue */
|
|
|
|
case '(' : {
|
|
|
|
int exprresult = 0;
|
|
|
|
pos++;
|
|
|
|
rc = WCMD_handleExpression(&pos, &exprresult, depth+1);
|
|
|
|
if (rc) goto exprerrorreturn;
|
|
|
|
WCMD_pushnumber(NULL, exprresult, &varstackhead);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close bracket - we have finished this depth, calculate and return */
|
|
|
|
case ')' : {
|
|
|
|
pos++;
|
|
|
|
treatasnumber = TRUE; /* Things in brackets result in a number */
|
|
|
|
if (depth == 0) {
|
|
|
|
rc = WCMD_BADPAREN;
|
|
|
|
goto exprerrorreturn;
|
|
|
|
}
|
|
|
|
goto exprreturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
WINE_ERR("Unrecognized operator %c\n", *pos);
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
lastwasnumber = treatasnumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
exprreturn:
|
|
|
|
*expr = pos;
|
|
|
|
|
|
|
|
/* We need to reduce until we have a single number (or variable) on the
|
|
|
|
stack and set the return value to that */
|
|
|
|
while (!rc && opstackhead) {
|
|
|
|
rc = WCMD_reduce(&opstackhead, &varstackhead);
|
|
|
|
}
|
|
|
|
if (rc) goto exprerrorreturn;
|
|
|
|
|
|
|
|
/* If we have anything other than one number left, error
|
|
|
|
otherwise throw the number away */
|
|
|
|
if (!varstackhead || varstackhead->next) {
|
|
|
|
rc = WCMD_NOOPERATOR;
|
|
|
|
goto exprerrorreturn;
|
|
|
|
}
|
|
|
|
|
2014-02-07 22:42:49 +01:00
|
|
|
/* Now get the number (and convert if it's just a variable name) */
|
2012-12-21 00:38:14 +01:00
|
|
|
*ret = WCMD_popnumber(&varstackhead);
|
|
|
|
|
|
|
|
exprerrorreturn:
|
|
|
|
/* Free all remaining memory */
|
|
|
|
while (opstackhead) WCMD_popoperator(&opstackhead);
|
|
|
|
while (varstackhead) WCMD_popnumber(&varstackhead);
|
|
|
|
|
|
|
|
WINE_TRACE("Returning result %d, rc %d\n", *ret, rc);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
1999-06-06 17:24:04 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_setshow_env
|
|
|
|
*
|
|
|
|
* Set/Show the environment variables
|
|
|
|
*/
|
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
void WCMD_setshow_env (WCHAR *s) {
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2007-03-01 12:43:19 +01:00
|
|
|
LPVOID env;
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR *p;
|
2013-10-16 23:38:31 +02:00
|
|
|
BOOL status;
|
2012-12-21 00:38:14 +01:00
|
|
|
WCHAR string[MAXSTRING];
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2007-03-13 01:11:04 +01:00
|
|
|
if (param1[0] == 0x00 && quals[0] == 0x00) {
|
2009-12-09 18:52:40 +01:00
|
|
|
env = GetEnvironmentStringsW();
|
2007-02-27 00:06:11 +01:00
|
|
|
WCMD_setshow_sortenv( env, NULL );
|
2007-03-13 01:11:04 +01:00
|
|
|
return;
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
2007-03-13 01:11:04 +01:00
|
|
|
|
|
|
|
/* See if /P supplied, and if so echo the prompt, and read in a reply */
|
2009-12-09 18:52:40 +01:00
|
|
|
if (CompareStringW(LOCALE_USER_DEFAULT,
|
2007-03-13 01:11:04 +01:00
|
|
|
NORM_IGNORECASE | SORT_STRINGSORT,
|
2020-11-29 23:30:29 +01:00
|
|
|
s, 2, L"/P", -1) == CSTR_EQUAL) {
|
2007-03-13 01:11:04 +01:00
|
|
|
DWORD count;
|
|
|
|
|
|
|
|
s += 2;
|
2011-08-25 00:48:23 +02:00
|
|
|
while (*s && (*s==' ' || *s=='\t')) s++;
|
2013-06-03 23:38:15 +02:00
|
|
|
/* set /P "var=value"jim ignores anything after the last quote */
|
|
|
|
if (*s=='\"') {
|
|
|
|
WCHAR *lastquote;
|
|
|
|
lastquote = WCMD_strip_quotes(s);
|
|
|
|
if (lastquote) *lastquote = 0x00;
|
|
|
|
WINE_TRACE("set: Stripped command line '%s'\n", wine_dbgstr_w(s));
|
|
|
|
}
|
2007-03-13 01:11:04 +01:00
|
|
|
|
|
|
|
/* If no parameter, or no '=' sign, return an error */
|
2019-05-03 12:28:33 +02:00
|
|
|
if (!(*s) || ((p = wcschr (s, '=')) == NULL )) {
|
2011-09-24 12:50:56 +02:00
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_NOARG));
|
2007-03-13 01:11:04 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Output the prompt */
|
|
|
|
*p++ = '\0';
|
2019-12-09 09:48:16 +01:00
|
|
|
if (*p) WCMD_output_asis(p);
|
2007-03-13 01:11:04 +01:00
|
|
|
|
|
|
|
/* Read the reply */
|
2018-08-20 21:15:42 +02:00
|
|
|
WCMD_ReadFile(GetStdHandle(STD_INPUT_HANDLE), string, ARRAY_SIZE(string), &count);
|
2007-03-13 01:11:04 +01:00
|
|
|
if (count > 1) {
|
|
|
|
string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
|
|
|
|
if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
|
2007-06-03 23:07:42 +02:00
|
|
|
WINE_TRACE("set /p: Setting var '%s' to '%s'\n", wine_dbgstr_w(s),
|
|
|
|
wine_dbgstr_w(string));
|
2017-01-30 23:11:04 +01:00
|
|
|
SetEnvironmentVariableW(s, string);
|
2007-03-13 01:11:04 +01:00
|
|
|
}
|
|
|
|
|
2012-12-21 00:38:14 +01:00
|
|
|
/* See if /A supplied, and if so calculate the results of all the expressions */
|
|
|
|
} else if (CompareStringW(LOCALE_USER_DEFAULT,
|
|
|
|
NORM_IGNORECASE | SORT_STRINGSORT,
|
2020-11-29 23:30:29 +01:00
|
|
|
s, 2, L"/A", -1) == CSTR_EQUAL) {
|
2012-12-21 00:38:14 +01:00
|
|
|
/* /A supplied, so evaluate expressions and set variables appropriately */
|
|
|
|
/* Syntax is set /a var=1,var2=var+4 etc, and it echos back the result */
|
|
|
|
/* of the final computation */
|
|
|
|
int result = 0;
|
|
|
|
int rc = 0;
|
|
|
|
WCHAR *thisexpr;
|
|
|
|
WCHAR *src,*dst;
|
|
|
|
|
|
|
|
/* Remove all quotes before doing any calculations */
|
2019-05-03 12:28:33 +02:00
|
|
|
thisexpr = heap_xalloc((lstrlenW(s+2)+1) * sizeof(WCHAR));
|
2012-12-21 00:38:14 +01:00
|
|
|
src = s+2;
|
|
|
|
dst = thisexpr;
|
|
|
|
while (*src) {
|
|
|
|
if (*src != '"') *dst++ = *src;
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
*dst = 0;
|
|
|
|
|
|
|
|
/* Now calculate the results of the expression */
|
|
|
|
src = thisexpr;
|
|
|
|
rc = WCMD_handleExpression(&src, &result, 0);
|
|
|
|
heap_free(thisexpr);
|
|
|
|
|
|
|
|
/* If parsing failed, issue the error message */
|
|
|
|
if (rc > 0) {
|
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(rc));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we have no context (interactive or cmd.exe /c) print the final result */
|
|
|
|
if (!context) {
|
2020-11-29 23:30:29 +01:00
|
|
|
swprintf(string, ARRAY_SIZE(string), L"%d", result);
|
2012-12-21 00:38:14 +01:00
|
|
|
WCMD_output_asis(string);
|
|
|
|
}
|
|
|
|
|
2007-03-13 01:11:04 +01:00
|
|
|
} else {
|
2007-03-13 01:11:31 +01:00
|
|
|
DWORD gle;
|
2009-08-28 18:34:32 +02:00
|
|
|
|
2013-06-03 23:38:15 +02:00
|
|
|
/* set "var=value"jim ignores anything after the last quote */
|
|
|
|
if (*s=='\"') {
|
|
|
|
WCHAR *lastquote;
|
|
|
|
lastquote = WCMD_strip_quotes(s);
|
|
|
|
if (lastquote) *lastquote = 0x00;
|
|
|
|
WINE_TRACE("set: Stripped command line '%s'\n", wine_dbgstr_w(s));
|
|
|
|
}
|
|
|
|
|
2019-05-03 12:28:33 +02:00
|
|
|
p = wcschr (s, '=');
|
1999-06-06 17:24:04 +02:00
|
|
|
if (p == NULL) {
|
2009-12-09 18:52:40 +01:00
|
|
|
env = GetEnvironmentStringsW();
|
2007-02-27 00:06:11 +01:00
|
|
|
if (WCMD_setshow_sortenv( env, s ) == 0) {
|
2011-09-24 12:50:56 +02:00
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_MISSINGENV), s);
|
2007-03-13 01:11:31 +01:00
|
|
|
errorlevel = 1;
|
2002-06-01 01:06:46 +02:00
|
|
|
}
|
1999-06-06 17:24:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
*p++ = '\0';
|
2000-08-01 04:14:33 +02:00
|
|
|
|
2019-12-09 09:48:16 +01:00
|
|
|
if (!*p) p = NULL;
|
2012-09-06 01:09:23 +02:00
|
|
|
WINE_TRACE("set: Setting var '%s' to '%s'\n", wine_dbgstr_w(s),
|
|
|
|
wine_dbgstr_w(p));
|
2009-12-09 18:52:40 +01:00
|
|
|
status = SetEnvironmentVariableW(s, p);
|
2007-03-13 01:11:31 +01:00
|
|
|
gle = GetLastError();
|
|
|
|
if ((!status) & (gle == ERROR_ENVVAR_NOT_FOUND)) {
|
|
|
|
errorlevel = 1;
|
2013-10-16 23:38:31 +02:00
|
|
|
} else if (!status) WCMD_print_error();
|
2012-09-06 00:41:48 +02:00
|
|
|
else errorlevel = 0;
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_setshow_path
|
|
|
|
*
|
|
|
|
* Set/Show the path environment variable
|
|
|
|
*/
|
|
|
|
|
2012-10-17 00:41:58 +02:00
|
|
|
void WCMD_setshow_path (const WCHAR *args) {
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR string[1024];
|
2007-03-01 12:43:19 +01:00
|
|
|
DWORD status;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2019-12-09 09:48:16 +01:00
|
|
|
if (!*param1 && !*param2) {
|
2020-11-29 23:30:29 +01:00
|
|
|
status = GetEnvironmentVariableW(L"PATH", string, ARRAY_SIZE(string));
|
1999-06-06 17:24:04 +02:00
|
|
|
if (status != 0) {
|
2020-11-29 23:30:29 +01:00
|
|
|
WCMD_output_asis(L"PATH=");
|
2005-12-02 11:25:51 +01:00
|
|
|
WCMD_output_asis ( string);
|
2020-11-29 23:30:28 +01:00
|
|
|
WCMD_output_asis(L"\r\n");
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
else {
|
2011-09-24 12:50:56 +02:00
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_NOPATH));
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2012-10-17 00:41:58 +02:00
|
|
|
if (*args == '=') args++; /* Skip leading '=' */
|
2020-11-29 23:30:29 +01:00
|
|
|
status = SetEnvironmentVariableW(L"PATH", args);
|
1999-06-06 17:24:04 +02:00
|
|
|
if (!status) WCMD_print_error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_setshow_prompt
|
|
|
|
*
|
|
|
|
* Set or show the command prompt.
|
|
|
|
*/
|
|
|
|
|
2003-02-27 02:41:21 +01:00
|
|
|
void WCMD_setshow_prompt (void) {
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR *s;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2019-12-09 09:48:16 +01:00
|
|
|
if (!*param1) {
|
2020-11-29 23:30:29 +01:00
|
|
|
SetEnvironmentVariableW(L"PROMPT", NULL);
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
s = param1;
|
2011-08-25 00:48:23 +02:00
|
|
|
while ((*s == '=') || (*s == ' ') || (*s == '\t')) s++;
|
2019-12-09 09:48:16 +01:00
|
|
|
if (!*s) {
|
2020-11-29 23:30:29 +01:00
|
|
|
SetEnvironmentVariableW(L"PROMPT", NULL);
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
2020-11-29 23:30:29 +01:00
|
|
|
else SetEnvironmentVariableW(L"PROMPT", s);
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_setshow_time
|
|
|
|
*
|
|
|
|
* Set/Show the system time
|
|
|
|
* FIXME: Can't change time yet
|
|
|
|
*/
|
|
|
|
|
2003-02-27 02:41:21 +01:00
|
|
|
void WCMD_setshow_time (void) {
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR curtime[64], buffer[64];
|
2007-03-01 12:43:19 +01:00
|
|
|
DWORD count;
|
|
|
|
SYSTEMTIME st;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2019-12-09 09:48:16 +01:00
|
|
|
if (!*param1) {
|
1999-07-10 13:36:45 +02:00
|
|
|
GetLocalTime(&st);
|
2018-08-20 21:15:42 +02:00
|
|
|
if (GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, NULL, curtime, ARRAY_SIZE(curtime))) {
|
2008-12-30 13:26:53 +01:00
|
|
|
WCMD_output (WCMD_LoadMessage(WCMD_CURRENTTIME), curtime);
|
2020-11-29 23:30:29 +01:00
|
|
|
if (wcsstr(quals, L"/T") == NULL) {
|
2007-06-03 23:07:39 +02:00
|
|
|
WCMD_output (WCMD_LoadMessage(WCMD_NEWTIME));
|
2018-08-20 21:15:42 +02:00
|
|
|
WCMD_ReadFile(GetStdHandle(STD_INPUT_HANDLE), buffer, ARRAY_SIZE(buffer), &count);
|
2007-03-08 01:45:27 +01:00
|
|
|
if (count > 2) {
|
2011-12-02 10:07:27 +01:00
|
|
|
WCMD_output_stderr (WCMD_LoadMessage(WCMD_NYI));
|
2007-03-08 01:45:27 +01:00
|
|
|
}
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else WCMD_print_error ();
|
|
|
|
}
|
|
|
|
else {
|
2011-12-02 10:07:27 +01:00
|
|
|
WCMD_output_stderr (WCMD_LoadMessage(WCMD_NYI));
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_shift
|
|
|
|
*
|
|
|
|
* Shift batch parameters.
|
2007-03-08 01:50:37 +01:00
|
|
|
* Optional /n says where to start shifting (n=0-8)
|
1999-06-06 17:24:04 +02:00
|
|
|
*/
|
|
|
|
|
2012-10-17 00:41:58 +02:00
|
|
|
void WCMD_shift (const WCHAR *args) {
|
2007-03-08 01:50:37 +01:00
|
|
|
int start;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2007-03-08 01:50:37 +01:00
|
|
|
if (context != NULL) {
|
2019-05-03 12:28:33 +02:00
|
|
|
WCHAR *pos = wcschr(args, '/');
|
2007-03-08 01:50:37 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (pos == NULL) {
|
|
|
|
start = 0;
|
|
|
|
} else if (*(pos+1)>='0' && *(pos+1)<='8') {
|
|
|
|
start = (*(pos+1) - '0');
|
|
|
|
} else {
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
WCMD_print_error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WINE_TRACE("Shifting variables, starting at %d\n", start);
|
|
|
|
for (i=start;i<=8;i++) {
|
|
|
|
context -> shift_count[i] = context -> shift_count[i+1] + 1;
|
|
|
|
}
|
|
|
|
context -> shift_count[9] = context -> shift_count[9] + 1;
|
|
|
|
}
|
1999-06-06 17:24:04 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-02-11 16:05:59 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_start
|
|
|
|
*/
|
2017-05-08 18:28:28 +02:00
|
|
|
void WCMD_start(WCHAR *args)
|
2012-02-11 16:05:59 +01:00
|
|
|
{
|
2017-05-08 18:28:28 +02:00
|
|
|
int argno;
|
|
|
|
int have_title;
|
2012-02-11 16:05:59 +01:00
|
|
|
WCHAR file[MAX_PATH];
|
2018-04-27 11:57:42 +02:00
|
|
|
WCHAR *cmdline, *cmdline_params;
|
2012-02-11 16:05:59 +01:00
|
|
|
STARTUPINFOW st;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
|
|
|
|
GetWindowsDirectoryW( file, MAX_PATH );
|
2020-11-29 23:30:29 +01:00
|
|
|
lstrcatW(file, L"\\command\\start.exe");
|
2019-05-03 12:28:33 +02:00
|
|
|
cmdline = heap_xalloc( (lstrlenW(file) + lstrlenW(args) + 8) * sizeof(WCHAR) );
|
|
|
|
lstrcpyW( cmdline, file );
|
2020-11-29 23:30:28 +01:00
|
|
|
lstrcatW(cmdline, L" ");
|
2019-05-03 12:28:33 +02:00
|
|
|
cmdline_params = cmdline + lstrlenW(cmdline);
|
2017-05-08 18:28:28 +02:00
|
|
|
|
|
|
|
/* The start built-in has some special command-line parsing properties
|
|
|
|
* which will be outlined here.
|
|
|
|
*
|
|
|
|
* both '\t' and ' ' are argument separators
|
|
|
|
* '/' has a special double role as both separator and switch prefix, e.g.
|
|
|
|
*
|
|
|
|
* > start /low/i
|
|
|
|
* or
|
|
|
|
* > start "title"/i
|
|
|
|
*
|
|
|
|
* are valid ways to pass multiple options to start. In the latter case
|
|
|
|
* '/i' is not a part of the title but parsed as a switch.
|
|
|
|
*
|
|
|
|
* However, '=', ';' and ',' are not separators:
|
|
|
|
* > start "deus"=ex,machina
|
|
|
|
*
|
|
|
|
* will in fact open a console titled 'deus=ex,machina'
|
|
|
|
*
|
|
|
|
* The title argument parsing code is only interested in quotes themselves,
|
|
|
|
* it does not respect escaping of any kind and all quotes are dropped
|
|
|
|
* from the resulting title, therefore:
|
|
|
|
*
|
|
|
|
* > start "\"" hello"/low
|
|
|
|
*
|
|
|
|
* actually opens a console titled '\ hello' with low priorities.
|
|
|
|
*
|
|
|
|
* To not break compatibility with wine programs relying on
|
|
|
|
* wine's separate 'start.exe', this program's peculiar console
|
|
|
|
* title parsing is actually implemented in 'cmd.exe' which is the
|
|
|
|
* application native Windows programs will use to invoke 'start'.
|
|
|
|
*
|
|
|
|
* WCMD_parameter_with_delims will take care of everything for us.
|
|
|
|
*/
|
|
|
|
have_title = FALSE;
|
|
|
|
for (argno=0; ; argno++) {
|
|
|
|
WCHAR *thisArg, *argN;
|
|
|
|
|
|
|
|
argN = NULL;
|
2020-11-29 23:30:29 +01:00
|
|
|
thisArg = WCMD_parameter_with_delims(args, argno, &argN, FALSE, FALSE, L" \t/");
|
2017-05-08 18:28:28 +02:00
|
|
|
|
|
|
|
/* No more parameters */
|
|
|
|
if (!argN)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Found the title */
|
|
|
|
if (argN[0] == '"') {
|
|
|
|
TRACE("detected console title: %s\n", wine_dbgstr_w(thisArg));
|
|
|
|
have_title = TRUE;
|
|
|
|
|
|
|
|
/* Copy all of the cmdline processed */
|
2018-04-27 11:57:42 +02:00
|
|
|
memcpy(cmdline_params, args, sizeof(WCHAR) * (argN - args));
|
|
|
|
cmdline_params[argN - args] = '\0';
|
2017-05-08 18:28:28 +02:00
|
|
|
|
|
|
|
/* Add quoted title */
|
2020-11-29 23:30:29 +01:00
|
|
|
lstrcatW(cmdline_params, L"\"\\\"");
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW(cmdline_params, thisArg);
|
2020-11-29 23:30:29 +01:00
|
|
|
lstrcatW(cmdline_params, L"\\\"\"");
|
2017-05-08 18:28:28 +02:00
|
|
|
|
|
|
|
/* Concatenate remaining command-line */
|
2020-11-29 23:30:29 +01:00
|
|
|
thisArg = WCMD_parameter_with_delims(args, argno, &argN, TRUE, FALSE, L" \t/");
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW(cmdline_params, argN + lstrlenW(thisArg));
|
2017-05-08 18:28:28 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skipping a regular argument? */
|
|
|
|
else if (argN != args && argN[-1] == '/') {
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Not an argument nor the title, start of program arguments,
|
|
|
|
* stop looking for title.
|
|
|
|
*/
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* build command-line if not built yet */
|
|
|
|
if (!have_title) {
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW( cmdline, args );
|
2017-05-08 18:28:28 +02:00
|
|
|
}
|
2012-02-11 16:05:59 +01:00
|
|
|
|
|
|
|
memset( &st, 0, sizeof(STARTUPINFOW) );
|
|
|
|
st.cb = sizeof(STARTUPINFOW);
|
|
|
|
|
|
|
|
if (CreateProcessW( file, cmdline, NULL, NULL, TRUE, 0, NULL, NULL, &st, &pi ))
|
|
|
|
{
|
|
|
|
WaitForSingleObject( pi.hProcess, INFINITE );
|
|
|
|
GetExitCodeProcess( pi.hProcess, &errorlevel );
|
|
|
|
if (errorlevel == STILL_ACTIVE) errorlevel = 0;
|
|
|
|
CloseHandle(pi.hProcess);
|
|
|
|
CloseHandle(pi.hThread);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_FILE_NOT_FOUND);
|
|
|
|
WCMD_print_error ();
|
|
|
|
errorlevel = 9009;
|
|
|
|
}
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(cmdline);
|
2012-02-11 16:05:59 +01:00
|
|
|
}
|
|
|
|
|
2002-05-04 20:29:31 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_title
|
|
|
|
*
|
|
|
|
* Set the console title
|
|
|
|
*/
|
2012-10-17 00:41:58 +02:00
|
|
|
void WCMD_title (const WCHAR *args) {
|
|
|
|
SetConsoleTitleW(args);
|
2002-05-04 20:29:31 +02:00
|
|
|
}
|
|
|
|
|
1999-06-06 17:24:04 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_type
|
|
|
|
*
|
|
|
|
* Copy a file to standard output.
|
|
|
|
*/
|
|
|
|
|
2012-10-17 00:41:58 +02:00
|
|
|
void WCMD_type (WCHAR *args) {
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2007-03-13 01:09:33 +01:00
|
|
|
int argno = 0;
|
2012-10-17 00:41:58 +02:00
|
|
|
WCHAR *argN = args;
|
2007-03-13 01:09:33 +01:00
|
|
|
BOOL writeHeaders = FALSE;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2006-10-31 21:11:28 +01:00
|
|
|
if (param1[0] == 0x00) {
|
2011-09-24 12:50:56 +02:00
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_NOARG));
|
2006-10-31 21:11:28 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-03-13 01:09:33 +01:00
|
|
|
|
|
|
|
if (param2[0] != 0x00) writeHeaders = TRUE;
|
|
|
|
|
|
|
|
/* Loop through all args */
|
|
|
|
errorlevel = 0;
|
|
|
|
while (argN) {
|
2012-10-25 23:47:59 +02:00
|
|
|
WCHAR *thisArg = WCMD_parameter (args, argno++, &argN, FALSE, FALSE);
|
2007-03-13 01:09:33 +01:00
|
|
|
|
|
|
|
HANDLE h;
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR buffer[512];
|
2007-03-13 01:09:33 +01:00
|
|
|
DWORD count;
|
|
|
|
|
|
|
|
if (!argN) break;
|
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
WINE_TRACE("type: Processing arg '%s'\n", wine_dbgstr_w(thisArg));
|
2009-12-09 18:52:40 +01:00
|
|
|
h = CreateFileW(thisArg, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
|
2007-03-13 01:09:33 +01:00
|
|
|
FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (h == INVALID_HANDLE_VALUE) {
|
|
|
|
WCMD_print_error ();
|
2011-12-02 10:07:27 +01:00
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_READFAIL), thisArg);
|
2007-03-13 01:09:33 +01:00
|
|
|
errorlevel = 1;
|
|
|
|
} else {
|
|
|
|
if (writeHeaders) {
|
2020-11-29 23:30:29 +01:00
|
|
|
WCMD_output_stderr(L"\n%1\n\n\n", thisArg);
|
2007-03-13 01:09:33 +01:00
|
|
|
}
|
2018-08-20 21:15:42 +02:00
|
|
|
while (WCMD_ReadFile(h, buffer, ARRAY_SIZE(buffer) - 1, &count)) {
|
2007-03-13 01:09:33 +01:00
|
|
|
if (count == 0) break; /* ReadFile reports success on EOF! */
|
|
|
|
buffer[count] = 0;
|
|
|
|
WCMD_output_asis (buffer);
|
|
|
|
}
|
|
|
|
CloseHandle (h);
|
|
|
|
}
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-13 22:34:05 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_more
|
|
|
|
*
|
|
|
|
* Output either a file or stdin to screen in pages
|
|
|
|
*/
|
|
|
|
|
2012-10-17 00:41:58 +02:00
|
|
|
void WCMD_more (WCHAR *args) {
|
2007-04-13 22:34:05 +02:00
|
|
|
|
|
|
|
int argno = 0;
|
2012-10-17 00:41:58 +02:00
|
|
|
WCHAR *argN = args;
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR moreStr[100];
|
|
|
|
WCHAR moreStrPage[100];
|
|
|
|
WCHAR buffer[512];
|
2007-04-13 22:34:05 +02:00
|
|
|
DWORD count;
|
|
|
|
|
|
|
|
/* Prefix the NLS more with '-- ', then load the text */
|
|
|
|
errorlevel = 0;
|
2020-11-29 23:30:29 +01:00
|
|
|
lstrcpyW(moreStr, L"-- ");
|
2018-08-20 21:15:42 +02:00
|
|
|
LoadStringW(hinst, WCMD_MORESTR, &moreStr[3], ARRAY_SIZE(moreStr)-3);
|
2007-04-13 22:34:05 +02:00
|
|
|
|
|
|
|
if (param1[0] == 0x00) {
|
|
|
|
|
|
|
|
/* Wine implements pipes via temporary files, and hence stdin is
|
|
|
|
effectively reading from the file. This means the prompts for
|
2008-04-07 13:01:02 +02:00
|
|
|
more are satisfied by the next line from the input (file). To
|
2007-04-13 22:34:05 +02:00
|
|
|
avoid this, ensure stdin is to the console */
|
|
|
|
HANDLE hstdin = GetStdHandle(STD_INPUT_HANDLE);
|
2020-11-29 23:30:29 +01:00
|
|
|
HANDLE hConIn = CreateFileW(L"CONIN$", GENERIC_READ | GENERIC_WRITE,
|
2007-04-13 22:34:05 +02:00
|
|
|
FILE_SHARE_READ, NULL, OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL, 0);
|
2008-03-04 00:14:29 +01:00
|
|
|
WINE_TRACE("No parms - working probably in pipe mode\n");
|
2007-04-13 22:34:05 +02:00
|
|
|
SetStdHandle(STD_INPUT_HANDLE, hConIn);
|
|
|
|
|
|
|
|
/* Warning: No easy way of ending the stream (ctrl+z on windows) so
|
2014-02-07 22:42:49 +01:00
|
|
|
once you get in this bit unless due to a pipe, it's going to end badly... */
|
2020-11-29 23:30:29 +01:00
|
|
|
wsprintfW(moreStrPage, L"%s --\n", moreStr);
|
2007-04-13 22:34:05 +02:00
|
|
|
|
|
|
|
WCMD_enter_paged_mode(moreStrPage);
|
2018-08-20 21:15:42 +02:00
|
|
|
while (WCMD_ReadFile(hstdin, buffer, ARRAY_SIZE(buffer)-1, &count)) {
|
2007-04-13 22:34:05 +02:00
|
|
|
if (count == 0) break; /* ReadFile reports success on EOF! */
|
|
|
|
buffer[count] = 0;
|
|
|
|
WCMD_output_asis (buffer);
|
|
|
|
}
|
|
|
|
WCMD_leave_paged_mode();
|
|
|
|
|
|
|
|
/* Restore stdin to what it was */
|
|
|
|
SetStdHandle(STD_INPUT_HANDLE, hstdin);
|
|
|
|
CloseHandle(hConIn);
|
|
|
|
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
BOOL needsPause = FALSE;
|
|
|
|
|
|
|
|
/* Loop through all args */
|
2008-03-04 00:14:29 +01:00
|
|
|
WINE_TRACE("Parms supplied - working through each file\n");
|
2007-04-13 22:34:05 +02:00
|
|
|
WCMD_enter_paged_mode(moreStrPage);
|
|
|
|
|
|
|
|
while (argN) {
|
2012-10-25 23:47:59 +02:00
|
|
|
WCHAR *thisArg = WCMD_parameter (args, argno++, &argN, FALSE, FALSE);
|
2007-04-13 22:34:05 +02:00
|
|
|
HANDLE h;
|
|
|
|
|
|
|
|
if (!argN) break;
|
|
|
|
|
|
|
|
if (needsPause) {
|
|
|
|
|
|
|
|
/* Wait */
|
2020-11-29 23:30:29 +01:00
|
|
|
wsprintfW(moreStrPage, L"%s (%2.2d%%) --\n", moreStr, 100);
|
2007-04-13 22:34:05 +02:00
|
|
|
WCMD_leave_paged_mode();
|
|
|
|
WCMD_output_asis(moreStrPage);
|
2018-08-20 21:15:42 +02:00
|
|
|
WCMD_ReadFile(GetStdHandle(STD_INPUT_HANDLE), buffer, ARRAY_SIZE(buffer), &count);
|
2007-04-13 22:34:05 +02:00
|
|
|
WCMD_enter_paged_mode(moreStrPage);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
WINE_TRACE("more: Processing arg '%s'\n", wine_dbgstr_w(thisArg));
|
2009-12-09 18:52:40 +01:00
|
|
|
h = CreateFileW(thisArg, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
|
2007-04-13 22:34:05 +02:00
|
|
|
FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (h == INVALID_HANDLE_VALUE) {
|
|
|
|
WCMD_print_error ();
|
2011-09-24 12:50:56 +02:00
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_READFAIL), thisArg);
|
2007-04-13 22:34:05 +02:00
|
|
|
errorlevel = 1;
|
|
|
|
} else {
|
|
|
|
ULONG64 curPos = 0;
|
|
|
|
ULONG64 fileLen = 0;
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fileInfo;
|
|
|
|
|
|
|
|
/* Get the file size */
|
2009-12-09 18:52:40 +01:00
|
|
|
GetFileAttributesExW(thisArg, GetFileExInfoStandard, (void*)&fileInfo);
|
2007-04-13 22:34:05 +02:00
|
|
|
fileLen = (((ULONG64)fileInfo.nFileSizeHigh) << 32) + fileInfo.nFileSizeLow;
|
|
|
|
|
|
|
|
needsPause = TRUE;
|
2018-08-20 21:15:42 +02:00
|
|
|
while (WCMD_ReadFile(h, buffer, ARRAY_SIZE(buffer)-1, &count)) {
|
2007-04-13 22:34:05 +02:00
|
|
|
if (count == 0) break; /* ReadFile reports success on EOF! */
|
|
|
|
buffer[count] = 0;
|
|
|
|
curPos += count;
|
|
|
|
|
|
|
|
/* Update % count (would be used in WCMD_output_asis as prompt) */
|
2020-11-29 23:30:29 +01:00
|
|
|
wsprintfW(moreStrPage, L"%s (%2.2d%%) --\n", moreStr, (int) min(99, (curPos * 100)/fileLen));
|
2007-04-13 22:34:05 +02:00
|
|
|
|
|
|
|
WCMD_output_asis (buffer);
|
|
|
|
}
|
|
|
|
CloseHandle (h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WCMD_leave_paged_mode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-06-06 17:24:04 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_verify
|
|
|
|
*
|
|
|
|
* Display verify flag.
|
1999-06-26 12:24:08 +02:00
|
|
|
* FIXME: We don't actually do anything with the verify flag other than toggle
|
|
|
|
* it...
|
1999-06-06 17:24:04 +02:00
|
|
|
*/
|
|
|
|
|
2012-10-17 00:41:58 +02:00
|
|
|
void WCMD_verify (const WCHAR *args) {
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2007-03-01 12:43:19 +01:00
|
|
|
int count;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2019-05-03 12:28:33 +02:00
|
|
|
count = lstrlenW(args);
|
1999-06-26 12:24:08 +02:00
|
|
|
if (count == 0) {
|
2020-11-29 23:30:29 +01:00
|
|
|
if (verify_mode) WCMD_output(WCMD_LoadMessage(WCMD_VERIFYPROMPT), L"ON");
|
|
|
|
else WCMD_output (WCMD_LoadMessage(WCMD_VERIFYPROMPT), L"OFF");
|
1999-06-26 12:24:08 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-11-29 23:30:29 +01:00
|
|
|
if (lstrcmpiW(args, L"ON") == 0) {
|
2011-09-16 08:44:07 +02:00
|
|
|
verify_mode = TRUE;
|
1999-06-26 12:24:08 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-11-29 23:30:29 +01:00
|
|
|
else if (lstrcmpiW(args, L"OFF") == 0) {
|
2011-09-16 08:44:07 +02:00
|
|
|
verify_mode = FALSE;
|
1999-06-26 12:24:08 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-09-24 12:50:56 +02:00
|
|
|
else WCMD_output_stderr(WCMD_LoadMessage(WCMD_VERIFYERR));
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_version
|
|
|
|
*
|
|
|
|
* Display version info.
|
|
|
|
*/
|
|
|
|
|
2003-02-27 02:41:21 +01:00
|
|
|
void WCMD_version (void) {
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2011-11-24 00:30:23 +01:00
|
|
|
WCMD_output_asis (version_string);
|
1999-06-06 17:24:04 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_volume
|
|
|
|
*
|
2011-09-21 21:17:18 +02:00
|
|
|
* Display volume information (set_label = FALSE)
|
|
|
|
* Additionally set volume label (set_label = TRUE)
|
|
|
|
* Returns 1 on success, 0 otherwise
|
1999-06-06 17:24:04 +02:00
|
|
|
*/
|
|
|
|
|
2011-09-21 21:17:18 +02:00
|
|
|
int WCMD_volume(BOOL set_label, const WCHAR *path)
|
|
|
|
{
|
2007-03-01 12:43:19 +01:00
|
|
|
DWORD count, serial;
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR string[MAX_PATH], label[MAX_PATH], curdir[MAX_PATH];
|
2007-03-01 12:43:19 +01:00
|
|
|
BOOL status;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2019-12-09 09:48:16 +01:00
|
|
|
if (!*path) {
|
2018-08-20 21:15:42 +02:00
|
|
|
status = GetCurrentDirectoryW(ARRAY_SIZE(curdir), curdir);
|
1999-06-06 17:24:04 +02:00
|
|
|
if (!status) {
|
|
|
|
WCMD_print_error ();
|
|
|
|
return 0;
|
|
|
|
}
|
2018-08-20 21:15:42 +02:00
|
|
|
status = GetVolumeInformationW(NULL, label, ARRAY_SIZE(label), &serial, NULL, NULL, NULL, 0);
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-05-03 12:28:33 +02:00
|
|
|
if ((path[1] != ':') || (lstrlenW(path) != 2)) {
|
2011-09-24 12:50:56 +02:00
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_SYNTAXERR));
|
1999-06-06 17:24:04 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2020-11-29 23:30:29 +01:00
|
|
|
wsprintfW (curdir, L"%s\\", path);
|
2018-08-20 21:15:42 +02:00
|
|
|
status = GetVolumeInformationW(curdir, label, ARRAY_SIZE(label), &serial, NULL, NULL, NULL, 0);
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
if (!status) {
|
|
|
|
WCMD_print_error ();
|
|
|
|
return 0;
|
|
|
|
}
|
2011-12-14 13:22:58 +01:00
|
|
|
if (label[0] != '\0') {
|
|
|
|
WCMD_output (WCMD_LoadMessage(WCMD_VOLUMELABEL),
|
|
|
|
curdir[0], label);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
WCMD_output (WCMD_LoadMessage(WCMD_VOLUMENOLABEL),
|
|
|
|
curdir[0]);
|
|
|
|
}
|
|
|
|
WCMD_output (WCMD_LoadMessage(WCMD_VOLUMESERIALNO),
|
|
|
|
HIWORD(serial), LOWORD(serial));
|
2011-09-21 21:17:18 +02:00
|
|
|
if (set_label) {
|
2007-06-03 23:07:39 +02:00
|
|
|
WCMD_output (WCMD_LoadMessage(WCMD_VOLUMEPROMPT));
|
2018-08-20 21:15:42 +02:00
|
|
|
WCMD_ReadFile(GetStdHandle(STD_INPUT_HANDLE), string, ARRAY_SIZE(string), &count);
|
1999-07-10 13:36:45 +02:00
|
|
|
if (count > 1) {
|
|
|
|
string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
|
|
|
|
if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
|
|
|
|
}
|
2019-12-09 09:48:16 +01:00
|
|
|
if (*path) {
|
2009-12-09 18:52:40 +01:00
|
|
|
if (!SetVolumeLabelW(curdir, string)) WCMD_print_error ();
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
else {
|
2009-12-09 18:52:40 +01:00
|
|
|
if (!SetVolumeLabelW(NULL, string)) WCMD_print_error ();
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2007-02-20 18:49:08 +01:00
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
* WCMD_exit
|
|
|
|
*
|
|
|
|
* Exit either the process, or just this batch program
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-06-15 21:59:24 +02:00
|
|
|
void WCMD_exit (CMD_LIST **cmdList) {
|
2019-05-03 12:28:33 +02:00
|
|
|
int rc = wcstol(param1, NULL, 10); /* Note: wcstol of empty parameter is 0 */
|
2007-02-20 18:49:08 +01:00
|
|
|
|
2020-11-29 23:30:29 +01:00
|
|
|
if (context && lstrcmpiW(quals, L"/B") == 0) {
|
2007-02-20 18:49:08 +01:00
|
|
|
errorlevel = rc;
|
|
|
|
context -> skip_rest = TRUE;
|
2007-06-15 21:59:24 +02:00
|
|
|
*cmdList = NULL;
|
2007-02-20 18:49:08 +01:00
|
|
|
} else {
|
|
|
|
ExitProcess(rc);
|
|
|
|
}
|
|
|
|
}
|
2007-02-28 00:18:57 +01:00
|
|
|
|
2007-03-04 23:34:45 +01:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* WCMD_assoc
|
|
|
|
*
|
2007-03-13 01:06:58 +01:00
|
|
|
* Lists or sets file associations (assoc = TRUE)
|
|
|
|
* Lists or sets file types (assoc = FALSE)
|
2007-03-04 23:34:45 +01:00
|
|
|
*/
|
2012-10-17 00:41:58 +02:00
|
|
|
void WCMD_assoc (const WCHAR *args, BOOL assoc) {
|
2007-03-04 23:34:45 +01:00
|
|
|
|
|
|
|
HKEY key;
|
|
|
|
DWORD accessOptions = KEY_READ;
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR *newValue;
|
2007-03-04 23:34:45 +01:00
|
|
|
LONG rc = ERROR_SUCCESS;
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR keyValue[MAXSTRING];
|
2007-03-04 23:34:45 +01:00
|
|
|
DWORD valueLen = MAXSTRING;
|
|
|
|
HKEY readKey;
|
|
|
|
|
|
|
|
/* See if parameter includes '=' */
|
|
|
|
errorlevel = 0;
|
2019-05-03 12:28:33 +02:00
|
|
|
newValue = wcschr(args, '=');
|
2007-03-04 23:34:45 +01:00
|
|
|
if (newValue) accessOptions |= KEY_WRITE;
|
|
|
|
|
|
|
|
/* Open a key to HKEY_CLASSES_ROOT for enumerating */
|
2020-11-29 23:30:28 +01:00
|
|
|
if (RegOpenKeyExW(HKEY_CLASSES_ROOT, L"", 0, accessOptions, &key) != ERROR_SUCCESS) {
|
2007-03-04 23:34:45 +01:00
|
|
|
WINE_FIXME("Unexpected failure opening HKCR key: %d\n", GetLastError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-06 14:26:24 +01:00
|
|
|
/* If no parameters then list all associations */
|
2012-10-17 00:41:58 +02:00
|
|
|
if (*args == 0x00) {
|
2007-03-04 23:34:45 +01:00
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
/* Enumerate all the keys */
|
|
|
|
while (rc != ERROR_NO_MORE_ITEMS) {
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR keyName[MAXSTRING];
|
2007-03-04 23:34:45 +01:00
|
|
|
DWORD nameLen;
|
|
|
|
|
|
|
|
/* Find the next value */
|
|
|
|
nameLen = MAXSTRING;
|
2009-12-09 18:52:40 +01:00
|
|
|
rc = RegEnumKeyExW(key, index++, keyName, &nameLen, NULL, NULL, NULL, NULL);
|
2007-03-04 23:34:45 +01:00
|
|
|
|
|
|
|
if (rc == ERROR_SUCCESS) {
|
|
|
|
|
2007-03-13 01:06:58 +01:00
|
|
|
/* Only interested in extension ones if assoc, or others
|
|
|
|
if not assoc */
|
|
|
|
if ((keyName[0] == '.' && assoc) ||
|
|
|
|
(!(keyName[0] == '.') && (!assoc)))
|
|
|
|
{
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR subkey[MAXSTRING];
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(subkey, keyName);
|
2020-11-29 23:30:29 +01:00
|
|
|
if (!assoc) lstrcatW(subkey, L"\\Shell\\Open\\Command");
|
2007-03-04 23:34:45 +01:00
|
|
|
|
2009-12-09 18:52:40 +01:00
|
|
|
if (RegOpenKeyExW(key, subkey, 0, accessOptions, &readKey) == ERROR_SUCCESS) {
|
2007-03-04 23:34:45 +01:00
|
|
|
|
2018-08-20 21:15:42 +02:00
|
|
|
valueLen = ARRAY_SIZE(keyValue);
|
2009-12-09 18:52:40 +01:00
|
|
|
rc = RegQueryValueExW(readKey, NULL, NULL, NULL, (LPBYTE)keyValue, &valueLen);
|
2007-03-04 23:34:45 +01:00
|
|
|
WCMD_output_asis(keyName);
|
2020-11-29 23:30:28 +01:00
|
|
|
WCMD_output_asis(L"=");
|
2007-03-04 23:34:45 +01:00
|
|
|
/* If no default value found, leave line empty after '=' */
|
|
|
|
if (rc == ERROR_SUCCESS) {
|
|
|
|
WCMD_output_asis(keyValue);
|
|
|
|
}
|
2020-11-29 23:30:28 +01:00
|
|
|
WCMD_output_asis(L"\r\n");
|
2008-01-04 01:03:44 +01:00
|
|
|
RegCloseKey(readKey);
|
2007-03-04 23:34:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2014-02-07 22:42:49 +01:00
|
|
|
/* Parameter supplied - if no '=' on command line, it's a query */
|
2007-03-04 23:34:45 +01:00
|
|
|
if (newValue == NULL) {
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR *space;
|
|
|
|
WCHAR subkey[MAXSTRING];
|
2007-03-04 23:34:45 +01:00
|
|
|
|
|
|
|
/* Query terminates the parameter at the first space */
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(keyValue, args);
|
|
|
|
space = wcschr(keyValue, ' ');
|
2007-03-04 23:34:45 +01:00
|
|
|
if (space) *space=0x00;
|
|
|
|
|
2007-03-13 01:06:58 +01:00
|
|
|
/* Set up key name */
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(subkey, keyValue);
|
2020-11-29 23:30:29 +01:00
|
|
|
if (!assoc) lstrcatW(subkey, L"\\Shell\\Open\\Command");
|
2007-03-13 01:06:58 +01:00
|
|
|
|
2009-12-09 18:52:40 +01:00
|
|
|
if (RegOpenKeyExW(key, subkey, 0, accessOptions, &readKey) == ERROR_SUCCESS) {
|
2007-03-04 23:34:45 +01:00
|
|
|
|
2009-12-09 18:52:40 +01:00
|
|
|
rc = RegQueryValueExW(readKey, NULL, NULL, NULL, (LPBYTE)keyValue, &valueLen);
|
2012-10-17 00:41:58 +02:00
|
|
|
WCMD_output_asis(args);
|
2020-11-29 23:30:28 +01:00
|
|
|
WCMD_output_asis(L"=");
|
2007-03-04 23:34:45 +01:00
|
|
|
/* If no default value found, leave line empty after '=' */
|
|
|
|
if (rc == ERROR_SUCCESS) WCMD_output_asis(keyValue);
|
2020-11-29 23:30:28 +01:00
|
|
|
WCMD_output_asis(L"\r\n");
|
2007-03-04 23:34:45 +01:00
|
|
|
RegCloseKey(readKey);
|
|
|
|
|
|
|
|
} else {
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR msgbuffer[MAXSTRING];
|
2007-03-04 23:34:45 +01:00
|
|
|
|
|
|
|
/* Load the translated 'File association not found' */
|
2007-03-13 01:06:58 +01:00
|
|
|
if (assoc) {
|
2018-08-20 21:15:42 +02:00
|
|
|
LoadStringW(hinst, WCMD_NOASSOC, msgbuffer, ARRAY_SIZE(msgbuffer));
|
2007-03-13 01:06:58 +01:00
|
|
|
} else {
|
2018-08-20 21:15:42 +02:00
|
|
|
LoadStringW(hinst, WCMD_NOFTYPE, msgbuffer, ARRAY_SIZE(msgbuffer));
|
2007-03-13 01:06:58 +01:00
|
|
|
}
|
2011-12-06 17:40:42 +01:00
|
|
|
WCMD_output_stderr(msgbuffer, keyValue);
|
2007-03-04 23:34:45 +01:00
|
|
|
errorlevel = 2;
|
|
|
|
}
|
|
|
|
|
2014-02-07 22:42:49 +01:00
|
|
|
/* Not a query - it's a set or clear of a value */
|
2007-03-04 23:34:45 +01:00
|
|
|
} else {
|
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR subkey[MAXSTRING];
|
2007-03-13 01:06:58 +01:00
|
|
|
|
2007-03-04 23:34:45 +01:00
|
|
|
/* Get pointer to new value */
|
|
|
|
*newValue = 0x00;
|
|
|
|
newValue++;
|
|
|
|
|
2007-03-13 01:06:58 +01:00
|
|
|
/* Set up key name */
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(subkey, args);
|
2020-11-29 23:30:29 +01:00
|
|
|
if (!assoc) lstrcatW(subkey, L"\\Shell\\Open\\Command");
|
2007-03-13 01:06:58 +01:00
|
|
|
|
|
|
|
/* If nothing after '=' then clear value - only valid for ASSOC */
|
2007-03-04 23:34:45 +01:00
|
|
|
if (*newValue == 0x00) {
|
|
|
|
|
2012-10-17 00:41:58 +02:00
|
|
|
if (assoc) rc = RegDeleteKeyW(key, args);
|
2007-03-13 01:06:58 +01:00
|
|
|
if (assoc && rc == ERROR_SUCCESS) {
|
2012-10-17 00:41:58 +02:00
|
|
|
WINE_TRACE("HKCR Key '%s' deleted\n", wine_dbgstr_w(args));
|
2007-03-04 23:34:45 +01:00
|
|
|
|
2007-03-13 01:06:58 +01:00
|
|
|
} else if (assoc && rc != ERROR_FILE_NOT_FOUND) {
|
2007-03-04 23:34:45 +01:00
|
|
|
WCMD_print_error();
|
|
|
|
errorlevel = 2;
|
|
|
|
|
|
|
|
} else {
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR msgbuffer[MAXSTRING];
|
2007-03-04 23:34:45 +01:00
|
|
|
|
|
|
|
/* Load the translated 'File association not found' */
|
2007-03-13 01:06:58 +01:00
|
|
|
if (assoc) {
|
2018-08-20 21:15:42 +02:00
|
|
|
LoadStringW(hinst, WCMD_NOASSOC, msgbuffer, ARRAY_SIZE(msgbuffer));
|
2007-03-13 01:06:58 +01:00
|
|
|
} else {
|
2018-08-20 21:15:42 +02:00
|
|
|
LoadStringW(hinst, WCMD_NOFTYPE, msgbuffer, ARRAY_SIZE(msgbuffer));
|
2007-03-13 01:06:58 +01:00
|
|
|
}
|
2018-07-04 23:08:40 +02:00
|
|
|
WCMD_output_stderr(msgbuffer, args);
|
2007-03-04 23:34:45 +01:00
|
|
|
errorlevel = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It really is a set value = contents */
|
|
|
|
} else {
|
2009-12-09 18:52:40 +01:00
|
|
|
rc = RegCreateKeyExW(key, subkey, 0, NULL, REG_OPTION_NON_VOLATILE,
|
2007-03-04 23:34:45 +01:00
|
|
|
accessOptions, NULL, &readKey, NULL);
|
|
|
|
if (rc == ERROR_SUCCESS) {
|
2009-12-09 18:52:40 +01:00
|
|
|
rc = RegSetValueExW(readKey, NULL, 0, REG_SZ,
|
2011-08-29 21:45:17 +02:00
|
|
|
(LPBYTE)newValue,
|
2019-05-03 12:28:33 +02:00
|
|
|
sizeof(WCHAR) * (lstrlenW(newValue) + 1));
|
2007-03-04 23:34:45 +01:00
|
|
|
RegCloseKey(readKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc != ERROR_SUCCESS) {
|
|
|
|
WCMD_print_error();
|
|
|
|
errorlevel = 2;
|
|
|
|
} else {
|
2012-10-17 00:41:58 +02:00
|
|
|
WCMD_output_asis(args);
|
2020-11-29 23:30:28 +01:00
|
|
|
WCMD_output_asis(L"=");
|
2007-03-04 23:34:45 +01:00
|
|
|
WCMD_output_asis(newValue);
|
2020-11-29 23:30:28 +01:00
|
|
|
WCMD_output_asis(L"\r\n");
|
2007-03-04 23:34:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clean up */
|
|
|
|
RegCloseKey(key);
|
|
|
|
}
|
2007-03-08 01:37:44 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_color
|
|
|
|
*
|
2011-09-27 00:32:38 +02:00
|
|
|
* Colors the terminal screen.
|
2007-03-08 01:37:44 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
void WCMD_color (void) {
|
|
|
|
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
|
|
|
|
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
|
2019-05-03 12:28:33 +02:00
|
|
|
if (param1[0] != 0x00 && lstrlenW(param1) > 2) {
|
2011-09-24 12:50:56 +02:00
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_ARGERR));
|
2007-03-08 01:37:44 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetConsoleScreenBufferInfo(hStdOut, &consoleInfo))
|
|
|
|
{
|
|
|
|
COORD topLeft;
|
|
|
|
DWORD screenSize;
|
|
|
|
DWORD color = 0;
|
|
|
|
|
|
|
|
screenSize = consoleInfo.dwSize.X * (consoleInfo.dwSize.Y + 1);
|
|
|
|
|
|
|
|
topLeft.X = 0;
|
|
|
|
topLeft.Y = 0;
|
|
|
|
|
|
|
|
/* Convert the color hex digits */
|
|
|
|
if (param1[0] == 0x00) {
|
|
|
|
color = defaultColor;
|
|
|
|
} else {
|
2019-05-03 12:28:33 +02:00
|
|
|
color = wcstoul(param1, NULL, 16);
|
2007-03-08 01:37:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fail if fg == bg color */
|
|
|
|
if (((color & 0xF0) >> 4) == (color & 0x0F)) {
|
|
|
|
errorlevel = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the current screen contents and ensure all future writes
|
|
|
|
remain this color */
|
|
|
|
FillConsoleOutputAttribute(hStdOut, color, screenSize, topLeft, &screenSize);
|
|
|
|
SetConsoleTextAttribute(hStdOut, color);
|
|
|
|
}
|
|
|
|
}
|
2017-11-08 00:26:42 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_mklink
|
|
|
|
*/
|
|
|
|
|
|
|
|
void WCMD_mklink(WCHAR *args)
|
|
|
|
{
|
|
|
|
int argno = 0;
|
|
|
|
WCHAR *argN = args;
|
|
|
|
BOOL isdir = FALSE;
|
|
|
|
BOOL junction = FALSE;
|
|
|
|
BOOL hard = FALSE;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
WCHAR file1[MAX_PATH];
|
|
|
|
WCHAR file2[MAX_PATH];
|
|
|
|
|
|
|
|
if (param1[0] == 0x00 || param2[0] == 0x00) {
|
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_NOARG));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
file1[0] = 0;
|
|
|
|
|
|
|
|
while (argN) {
|
|
|
|
WCHAR *thisArg = WCMD_parameter (args, argno++, &argN, FALSE, FALSE);
|
|
|
|
|
|
|
|
if (!argN) break;
|
|
|
|
|
|
|
|
WINE_TRACE("mklink: Processing arg '%s'\n", wine_dbgstr_w(thisArg));
|
|
|
|
|
2020-11-29 23:30:29 +01:00
|
|
|
if (lstrcmpiW(thisArg, L"/D") == 0)
|
2017-11-08 00:26:42 +01:00
|
|
|
isdir = TRUE;
|
2020-11-29 23:30:29 +01:00
|
|
|
else if (lstrcmpiW(thisArg, L"/H") == 0)
|
2017-11-08 00:26:42 +01:00
|
|
|
hard = TRUE;
|
2020-11-29 23:30:29 +01:00
|
|
|
else if (lstrcmpiW(thisArg, L"/J") == 0)
|
2017-11-08 00:26:42 +01:00
|
|
|
junction = TRUE;
|
|
|
|
else {
|
|
|
|
if(!file1[0])
|
|
|
|
lstrcpyW(file1, thisArg);
|
|
|
|
else
|
|
|
|
lstrcpyW(file2, thisArg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(hard)
|
|
|
|
ret = CreateHardLinkW(file1, file2, NULL);
|
|
|
|
else if(!junction)
|
|
|
|
ret = CreateSymbolicLinkW(file1, file2, isdir);
|
|
|
|
else
|
|
|
|
WINE_TRACE("Juction links currently not supported.\n");
|
|
|
|
|
|
|
|
if(!ret)
|
|
|
|
WCMD_output_stderr(WCMD_LoadMessage(WCMD_READFAIL), file1);
|
|
|
|
}
|