1999-06-06 17:24:04 +02:00
|
|
|
/*
|
2006-09-04 06:13:08 +02:00
|
|
|
* CMD - Wine-compatible command line interface - batch interface.
|
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
|
1999-06-06 17:24:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "wcmd.h"
|
2007-09-11 22:43:03 +02:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
2012-10-01 00:07:01 +02:00
|
|
|
extern struct env_stack *saved_environment;
|
|
|
|
|
2007-09-11 22:43:03 +02:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(cmd);
|
1999-06-06 17:24:04 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_batch
|
|
|
|
*
|
|
|
|
* Open and execute a batch file.
|
|
|
|
* On entry *command includes the complete command line beginning with the name
|
|
|
|
* of the batch file (if a CALL command was entered the CALL has been removed).
|
|
|
|
* *file is the name of the file, which might not exist and may not have the
|
1999-06-26 12:24:08 +02:00
|
|
|
* .BAT suffix on. Called is 1 for a CALL, 0 otherwise.
|
1999-06-06 17:24:04 +02:00
|
|
|
*
|
|
|
|
* We need to handle recursion correctly, since one batch program might call another.
|
1999-06-26 12:24:08 +02:00
|
|
|
* So parameters for this batch file are held in a BATCH_CONTEXT structure.
|
2007-02-23 23:19:25 +01:00
|
|
|
*
|
|
|
|
* To support call within the same batch program, another input parameter is
|
|
|
|
* a label to goto once opened.
|
1999-06-06 17:24:04 +02:00
|
|
|
*/
|
|
|
|
|
2012-03-29 18:05:59 +02:00
|
|
|
void WCMD_batch (WCHAR *file, WCHAR *command, BOOL called, WCHAR *startLabel, HANDLE pgmHandle)
|
|
|
|
{
|
2007-03-01 12:43:19 +01:00
|
|
|
HANDLE h = INVALID_HANDLE_VALUE;
|
|
|
|
BATCH_CONTEXT *prev_context;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2007-02-23 23:19:25 +01:00
|
|
|
if (startLabel == NULL) {
|
2011-09-11 17:14:55 +02:00
|
|
|
h = CreateFileW (file, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
|
2010-02-18 17:09:04 +01:00
|
|
|
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
2007-02-23 23:19:25 +01:00
|
|
|
if (h == INVALID_HANDLE_VALUE) {
|
2010-02-18 17:09:04 +01:00
|
|
|
SetLastError (ERROR_FILE_NOT_FOUND);
|
|
|
|
WCMD_print_error ();
|
2007-02-23 23:19:25 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DuplicateHandle(GetCurrentProcess(), pgmHandle,
|
|
|
|
GetCurrentProcess(), &h,
|
|
|
|
0, FALSE, DUPLICATE_SAME_ACCESS);
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
|
1999-06-26 12:24:08 +02:00
|
|
|
/*
|
|
|
|
* Create a context structure for this batch file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
prev_context = context;
|
2008-12-04 05:31:43 +01:00
|
|
|
context = LocalAlloc (LMEM_FIXED, sizeof (BATCH_CONTEXT));
|
1999-06-26 12:24:08 +02:00
|
|
|
context -> h = h;
|
2012-11-15 12:32:21 +01:00
|
|
|
context->batchfileW = heap_strdupW(file);
|
1999-06-26 12:24:08 +02:00
|
|
|
context -> command = command;
|
2007-03-08 01:50:37 +01:00
|
|
|
memset(context -> shift_count, 0x00, sizeof(context -> shift_count));
|
1999-06-26 12:24:08 +02:00
|
|
|
context -> prev_context = prev_context;
|
2007-02-20 18:49:08 +01:00
|
|
|
context -> skip_rest = FALSE;
|
1999-06-26 12:24:08 +02:00
|
|
|
|
2007-02-23 23:19:25 +01:00
|
|
|
/* If processing a call :label, 'goto' the label in question */
|
|
|
|
if (startLabel) {
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(param1, startLabel);
|
2007-06-15 21:59:22 +02:00
|
|
|
WCMD_goto(NULL);
|
2007-02-23 23:19:25 +01:00
|
|
|
}
|
|
|
|
|
1999-06-06 17:24:04 +02:00
|
|
|
/*
|
|
|
|
* Work through the file line by line. Specific batch commands are processed here,
|
|
|
|
* the rest are handled by the main command processor.
|
|
|
|
*/
|
|
|
|
|
2007-06-15 21:59:19 +02:00
|
|
|
while (context -> skip_rest == FALSE) {
|
|
|
|
CMD_LIST *toExecute = NULL; /* Commands left to be executed */
|
2011-10-06 18:50:31 +02:00
|
|
|
if (!WCMD_ReadAndParseLine(NULL, &toExecute, h))
|
2007-06-15 21:59:19 +02:00
|
|
|
break;
|
2012-10-14 01:38:53 +02:00
|
|
|
/* Note: although this batch program itself may be called, we are not retrying
|
|
|
|
the command as a result of a call failing to find a program, hence the
|
|
|
|
retryCall parameter below is FALSE */
|
2012-11-05 22:43:08 +01:00
|
|
|
WCMD_process_commands(toExecute, FALSE, FALSE);
|
2007-06-15 21:59:19 +02:00
|
|
|
WCMD_free_commands(toExecute);
|
|
|
|
toExecute = NULL;
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
CloseHandle (h);
|
1999-06-26 12:24:08 +02:00
|
|
|
|
2012-10-01 00:07:01 +02:00
|
|
|
/*
|
|
|
|
* If there are outstanding setlocal's to the current context, unwind them.
|
|
|
|
*/
|
|
|
|
while (saved_environment && saved_environment->batchhandle == context->h) {
|
|
|
|
WCMD_endlocal();
|
|
|
|
}
|
|
|
|
|
1999-06-26 12:24:08 +02:00
|
|
|
/*
|
|
|
|
* If invoked by a CALL, we return to the context of our caller. Otherwise return
|
|
|
|
* to the caller's caller.
|
|
|
|
*/
|
|
|
|
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(context->batchfileW);
|
2008-12-04 05:31:43 +01:00
|
|
|
LocalFree (context);
|
1999-06-26 12:24:08 +02:00
|
|
|
if ((prev_context != NULL) && (!called)) {
|
2012-10-22 23:22:28 +02:00
|
|
|
WINE_TRACE("Batch completed, but was not 'called' so skipping outer batch too\n");
|
2007-07-25 00:36:30 +02:00
|
|
|
prev_context -> skip_rest = TRUE;
|
1999-06-26 12:24:08 +02:00
|
|
|
context = prev_context;
|
|
|
|
}
|
2007-07-25 00:36:30 +02:00
|
|
|
context = prev_context;
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************
|
2012-10-23 23:05:06 +02:00
|
|
|
* WCMD_parameter_with_delims
|
1999-06-06 17:24:04 +02:00
|
|
|
*
|
2012-10-23 23:05:06 +02:00
|
|
|
* Extracts a delimited parameter from an input string, providing
|
|
|
|
* the delimiters characters to use
|
2011-09-06 19:19:40 +02:00
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* s [I] input string, non NULL
|
2012-05-15 15:03:35 +02:00
|
|
|
* n [I] # of the parameter to return, counted from 0
|
|
|
|
* start [O] Optional. Pointer to the first char of param n in s
|
2012-10-25 23:47:59 +02:00
|
|
|
* raw [I] TRUE to return the parameter in raw format (quotes maintained)
|
|
|
|
* FALSE to return the parameter with quotes stripped (including internal ones)
|
|
|
|
* wholecmdline [I] TRUE to indicate this routine is being used to parse the
|
2012-10-17 00:35:33 +02:00
|
|
|
* command line, and special logic for arg0->1 transition
|
|
|
|
* needs to be applied.
|
2012-10-23 23:05:06 +02:00
|
|
|
* delims[I] The delimiter characters to use
|
2011-09-06 19:19:40 +02:00
|
|
|
*
|
|
|
|
* RETURNS
|
2012-09-26 01:07:20 +02:00
|
|
|
* Success: The nth delimited parameter found in s
|
2012-10-25 23:47:59 +02:00
|
|
|
* if start != NULL, *start points to the start of the param (quotes maintained)
|
2012-05-15 15:03:35 +02:00
|
|
|
* Failure: An empty string if the param is not found.
|
2012-10-25 23:47:59 +02:00
|
|
|
* *start == NULL
|
2011-09-06 19:19:40 +02:00
|
|
|
*
|
|
|
|
* NOTES
|
2012-05-15 15:03:35 +02:00
|
|
|
* Return value is stored in static storage (i.e. overwritten after each call).
|
2012-09-26 01:07:20 +02:00
|
|
|
* By default, the parameter is returned with quotes removed, ready for use with
|
|
|
|
* other API calls, e.g. c:\"a b"\c is returned as c:\a b\c. However, some commands
|
|
|
|
* need to preserve the exact syntax (echo, for, etc) hence the raw option.
|
1999-06-06 17:24:04 +02:00
|
|
|
*/
|
2012-10-25 23:47:59 +02:00
|
|
|
WCHAR *WCMD_parameter_with_delims (WCHAR *s, int n, WCHAR **start,
|
2012-10-23 23:05:06 +02:00
|
|
|
BOOL raw, BOOL wholecmdline, const WCHAR *delims)
|
2012-05-15 15:03:35 +02:00
|
|
|
{
|
2011-09-06 19:19:41 +02:00
|
|
|
int curParamNb = 0;
|
2012-11-19 23:34:09 +01:00
|
|
|
static WCHAR param[MAXSTRING];
|
2012-09-26 01:07:20 +02:00
|
|
|
WCHAR *p = s, *begin;
|
2011-09-06 19:19:41 +02:00
|
|
|
|
2011-10-29 13:42:05 +02:00
|
|
|
if (start != NULL) *start = NULL;
|
2011-09-06 19:19:41 +02:00
|
|
|
param[0] = '\0';
|
2012-09-26 01:07:20 +02:00
|
|
|
|
2011-09-06 19:19:41 +02:00
|
|
|
while (TRUE) {
|
2012-09-26 01:07:20 +02:00
|
|
|
|
|
|
|
/* Absorb repeated word delimiters until we get to the next token (or the end!) */
|
2019-05-03 12:28:33 +02:00
|
|
|
while (*p && (wcschr(delims, *p) != NULL))
|
2011-09-06 19:19:41 +02:00
|
|
|
p++;
|
|
|
|
if (*p == '\0') return param;
|
|
|
|
|
2012-09-28 11:13:15 +02:00
|
|
|
/* If we have reached the token number we want, remember the beginning of it */
|
2011-10-29 13:42:05 +02:00
|
|
|
if (start != NULL && curParamNb == n) *start = p;
|
2011-09-06 19:19:41 +02:00
|
|
|
|
2012-09-26 01:07:20 +02:00
|
|
|
/* Return the whole word up to the next delimiter, handling quotes in the middle
|
|
|
|
of it, e.g. a"\b c\"d is a single parameter. */
|
|
|
|
begin = p;
|
|
|
|
|
|
|
|
/* Loop character by character, but just need to special case quotes */
|
|
|
|
while (*p) {
|
|
|
|
/* Once we have found a delimiter, break */
|
2019-05-03 12:28:33 +02:00
|
|
|
if (wcschr(delims, *p) != NULL) break;
|
2012-09-26 01:07:20 +02:00
|
|
|
|
2012-10-17 00:35:33 +02:00
|
|
|
/* Very odd special case - Seems as if a ( acts as a delimiter which is
|
|
|
|
not swallowed but is effective only when it comes between the program
|
|
|
|
name and the parameters. Need to avoid this triggering when used
|
|
|
|
to walk parameters generally. */
|
|
|
|
if (wholecmdline && curParamNb == 0 && *p=='(') break;
|
|
|
|
|
2012-09-26 01:07:20 +02:00
|
|
|
/* If we find a quote, copy until we get the end quote */
|
|
|
|
if (*p == '"') {
|
2011-09-06 19:19:41 +02:00
|
|
|
p++;
|
2012-09-26 01:07:20 +02:00
|
|
|
while (*p && *p != '"') p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now skip the character / quote */
|
|
|
|
if (*p) p++;
|
2011-09-06 19:19:41 +02:00
|
|
|
}
|
2012-09-26 01:07:20 +02:00
|
|
|
|
2011-09-06 19:19:41 +02:00
|
|
|
if (curParamNb == n) {
|
2012-09-26 01:07:20 +02:00
|
|
|
/* Return the parameter in static storage either as-is (raw) or
|
|
|
|
suitable for use with other win32 api calls (quotes stripped) */
|
|
|
|
if (raw) {
|
|
|
|
memcpy(param, begin, (p - begin) * sizeof(WCHAR));
|
|
|
|
param[p-begin] = '\0';
|
|
|
|
} else {
|
|
|
|
int i=0;
|
|
|
|
while (begin < p) {
|
|
|
|
if (*begin != '"') param[i++] = *begin;
|
|
|
|
begin++;
|
|
|
|
}
|
|
|
|
param[i] = '\0';
|
|
|
|
}
|
2011-09-06 19:19:41 +02:00
|
|
|
return param;
|
2007-09-11 22:43:04 +02:00
|
|
|
}
|
2011-09-06 19:19:41 +02:00
|
|
|
curParamNb++;
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 23:05:06 +02:00
|
|
|
/*******************************************************************
|
|
|
|
* WCMD_parameter
|
|
|
|
*
|
|
|
|
* Extracts a delimited parameter from an input string, using a
|
|
|
|
* default set of delimiter characters. For parameters, see the main
|
|
|
|
* function above.
|
|
|
|
*/
|
2012-10-25 23:47:59 +02:00
|
|
|
WCHAR *WCMD_parameter (WCHAR *s, int n, WCHAR **start, BOOL raw,
|
2012-10-23 23:05:06 +02:00
|
|
|
BOOL wholecmdline)
|
|
|
|
{
|
2020-03-24 21:12:50 +01:00
|
|
|
return WCMD_parameter_with_delims (s, n, start, raw, wholecmdline, L" \t,=;");
|
2012-10-23 23:05:06 +02:00
|
|
|
}
|
|
|
|
|
2000-08-02 00:02:18 +02:00
|
|
|
/****************************************************************************
|
1999-06-06 17:24:04 +02:00
|
|
|
* WCMD_fgets
|
|
|
|
*
|
2011-10-05 14:02:44 +02:00
|
|
|
* Gets one line from a file/console and puts it into buffer buf
|
|
|
|
* Pre: buf has size noChars
|
2011-10-01 13:43:17 +02:00
|
|
|
* 1 <= noChars <= MAXSTRING
|
2011-10-05 14:02:44 +02:00
|
|
|
* Post: buf is filled with at most noChars-1 characters, and gets nul-terminated
|
|
|
|
buf does not include EOL terminator
|
2011-10-01 13:43:17 +02:00
|
|
|
* Returns:
|
2011-10-05 14:02:44 +02:00
|
|
|
* buf on success
|
2011-10-01 13:43:17 +02:00
|
|
|
* NULL on error or EOF
|
1999-06-06 17:24:04 +02:00
|
|
|
*/
|
|
|
|
|
2011-11-07 10:52:22 +01:00
|
|
|
WCHAR *WCMD_fgets(WCHAR *buf, DWORD noChars, HANDLE h)
|
2011-09-28 14:41:20 +02:00
|
|
|
{
|
2011-10-06 18:50:32 +02:00
|
|
|
DWORD charsRead;
|
2007-03-01 12:43:19 +01:00
|
|
|
BOOL status;
|
2011-11-07 10:52:22 +01:00
|
|
|
DWORD i;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2011-10-01 13:43:17 +02:00
|
|
|
/* We can't use the native f* functions because of the filename syntax differences
|
|
|
|
between DOS and Unix. Also need to lose the LF (or CRLF) from the line. */
|
|
|
|
|
2020-12-01 17:25:25 +01:00
|
|
|
if (!ReadConsoleW(h, buf, noChars, &charsRead, NULL)) {
|
2012-10-19 18:02:24 +02:00
|
|
|
LARGE_INTEGER filepos;
|
|
|
|
char *bufA;
|
|
|
|
UINT cp;
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
cp = GetConsoleCP();
|
2018-11-25 21:56:00 +01:00
|
|
|
bufA = heap_xalloc(noChars);
|
2012-10-19 18:02:24 +02:00
|
|
|
|
|
|
|
/* Save current file position */
|
|
|
|
filepos.QuadPart = 0;
|
|
|
|
SetFilePointerEx(h, filepos, &filepos, FILE_CURRENT);
|
|
|
|
|
|
|
|
status = ReadFile(h, bufA, noChars, &charsRead, NULL);
|
|
|
|
if (!status || charsRead == 0) {
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(bufA);
|
2012-10-19 18:02:24 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find first EOL */
|
|
|
|
for (p = bufA; p < (bufA + charsRead); p = CharNextExA(cp, p, 0)) {
|
|
|
|
if (*p == '\n' || *p == '\r')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sets file pointer to the start of the next line, if any */
|
|
|
|
filepos.QuadPart += p - bufA + 1 + (*p == '\r' ? 1 : 0);
|
|
|
|
SetFilePointerEx(h, filepos, NULL, FILE_BEGIN);
|
|
|
|
|
|
|
|
i = MultiByteToWideChar(cp, 0, bufA, p - bufA, buf, noChars);
|
2012-11-16 10:37:39 +01:00
|
|
|
heap_free(bufA);
|
2011-09-28 14:41:20 +02:00
|
|
|
}
|
2012-10-19 18:02:24 +02:00
|
|
|
else {
|
2020-12-01 17:25:25 +01:00
|
|
|
if (!charsRead) return NULL;
|
2012-10-19 18:02:24 +02:00
|
|
|
|
|
|
|
/* Find first EOL */
|
|
|
|
for (i = 0; i < charsRead; i++) {
|
|
|
|
if (buf[i] == '\n' || buf[i] == '\r')
|
|
|
|
break;
|
|
|
|
}
|
2011-10-06 18:50:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Truncate at EOL (or end of buffer) */
|
|
|
|
if (i == noChars)
|
|
|
|
i--;
|
|
|
|
|
|
|
|
buf[i] = '\0';
|
|
|
|
|
|
|
|
return buf;
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
2007-02-20 18:49:43 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2020-04-26 05:53:37 +02:00
|
|
|
* WCMD_HandleTildeModifiers
|
2007-02-20 18:49:43 +01:00
|
|
|
*
|
2012-11-05 22:43:08 +01:00
|
|
|
* Handle the ~ modifiers when expanding %0-9 or (%a-z/A-Z in for command)
|
|
|
|
* %~xxxxxV (V=0-9 or A-Z, a-z)
|
2007-02-20 18:49:43 +01:00
|
|
|
* Where xxxx is any combination of:
|
|
|
|
* ~ - Removes quotes
|
|
|
|
* f - Fully qualified path (assumes current dir if not drive\dir)
|
|
|
|
* d - drive letter
|
|
|
|
* p - path
|
|
|
|
* n - filename
|
|
|
|
* x - file extension
|
|
|
|
* s - path with shortnames
|
|
|
|
* a - attributes
|
|
|
|
* t - date/time
|
|
|
|
* z - size
|
|
|
|
* $ENVVAR: - Searches ENVVAR for (contents of V) and expands to fully
|
|
|
|
* qualified path
|
|
|
|
*
|
|
|
|
* To work out the length of the modifier:
|
|
|
|
*
|
|
|
|
* Note: In the case of %0-9 knowing the end of the modifier is easy,
|
2007-06-03 23:07:42 +02:00
|
|
|
* but in a for loop, the for end WCHARacter may also be a modifier
|
2007-02-20 18:49:43 +01:00
|
|
|
* eg. for %a in (c:\a.a) do echo XXX
|
|
|
|
* where XXX = %~a (just ~)
|
|
|
|
* %~aa (~ and attributes)
|
|
|
|
* %~aaxa (~, attributes and extension)
|
|
|
|
* BUT %~aax (~ and attributes followed by 'x')
|
|
|
|
*
|
|
|
|
* Hence search forwards until find an invalid modifier, and then
|
|
|
|
* backwards until find for variable or 0-9
|
|
|
|
*/
|
2020-04-26 05:53:37 +02:00
|
|
|
void WCMD_HandleTildeModifiers(WCHAR **start, BOOL atExecute)
|
2012-11-05 22:43:08 +01:00
|
|
|
{
|
2007-02-20 18:49:43 +01:00
|
|
|
|
|
|
|
#define NUMMODIFIERS 11
|
2007-06-03 23:07:42 +02:00
|
|
|
static const WCHAR validmodifiers[NUMMODIFIERS] = {
|
2007-02-20 18:49:43 +01:00
|
|
|
'~', 'f', 'd', 'p', 'n', 'x', 's', 'a', 't', 'z', '$'
|
|
|
|
};
|
|
|
|
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fileInfo;
|
2020-11-13 17:09:59 +01:00
|
|
|
WCHAR outputparam[MAXSTRING];
|
|
|
|
WCHAR finaloutput[MAXSTRING];
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR fullfilename[MAX_PATH];
|
|
|
|
WCHAR thisoutput[MAX_PATH];
|
2018-08-27 21:41:49 +02:00
|
|
|
WCHAR *filepart = NULL;
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR *pos = *start+1;
|
|
|
|
WCHAR *firstModifier = pos;
|
|
|
|
WCHAR *lastModifier = NULL;
|
2007-02-20 18:49:43 +01:00
|
|
|
int modifierLen = 0;
|
|
|
|
BOOL finished = FALSE;
|
|
|
|
int i = 0;
|
|
|
|
BOOL exists = TRUE;
|
|
|
|
BOOL skipFileParsing = FALSE;
|
|
|
|
BOOL doneModifier = FALSE;
|
|
|
|
|
2007-09-11 22:43:03 +02:00
|
|
|
/* Search forwards until find invalid character modifier */
|
2007-02-20 18:49:43 +01:00
|
|
|
while (!finished) {
|
|
|
|
|
2007-09-11 22:43:03 +02:00
|
|
|
/* Work on the previous character */
|
2007-02-20 18:49:43 +01:00
|
|
|
if (lastModifier != NULL) {
|
|
|
|
|
|
|
|
for (i=0; i<NUMMODIFIERS; i++) {
|
|
|
|
if (validmodifiers[i] == *lastModifier) {
|
|
|
|
|
|
|
|
/* Special case '$' to skip until : found */
|
|
|
|
if (*lastModifier == '$') {
|
|
|
|
while (*pos != ':' && *pos) pos++;
|
|
|
|
if (*pos == 0x00) return; /* Invalid syntax */
|
|
|
|
pos++; /* Skip ':' */
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i==NUMMODIFIERS) {
|
|
|
|
finished = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save this one away */
|
|
|
|
if (!finished) {
|
|
|
|
lastModifier = pos;
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-11 22:43:03 +02:00
|
|
|
while (lastModifier > firstModifier) {
|
2012-11-05 22:43:08 +01:00
|
|
|
WINE_TRACE("Looking backwards for parameter id: %s\n",
|
|
|
|
wine_dbgstr_w(lastModifier));
|
2007-09-11 22:43:03 +02:00
|
|
|
|
2013-01-07 00:06:28 +01:00
|
|
|
if (!atExecute && context && (*lastModifier >= '0' && *lastModifier <= '9')) {
|
2007-09-11 22:43:03 +02:00
|
|
|
/* Its a valid parameter identifier - OK */
|
|
|
|
break;
|
|
|
|
|
2012-11-05 22:43:08 +01:00
|
|
|
} else {
|
|
|
|
int foridx = FOR_VAR_IDX(*lastModifier);
|
2007-09-11 22:43:03 +02:00
|
|
|
/* Its a valid parameter identifier - OK */
|
2012-11-05 22:43:08 +01:00
|
|
|
if ((foridx >= 0) && (forloopcontext.variable[foridx] != NULL)) break;
|
2007-02-20 18:49:43 +01:00
|
|
|
|
2012-11-05 22:43:08 +01:00
|
|
|
/* Its not a valid parameter identifier - step backwards */
|
2007-02-20 18:49:43 +01:00
|
|
|
lastModifier--;
|
|
|
|
}
|
|
|
|
}
|
2007-09-11 22:43:03 +02:00
|
|
|
if (lastModifier == firstModifier) return; /* Invalid syntax */
|
2007-02-20 18:49:43 +01:00
|
|
|
|
2018-08-27 21:41:53 +02:00
|
|
|
/* So now, firstModifier points to beginning of modifiers, lastModifier
|
|
|
|
points to the variable just after the modifiers. Process modifiers
|
|
|
|
in a specific order, remembering there could be duplicates */
|
|
|
|
modifierLen = lastModifier - firstModifier;
|
|
|
|
finaloutput[0] = 0x00;
|
|
|
|
|
|
|
|
/* Extract the parameter to play with
|
|
|
|
Special case param 0 - With %~0 you get the batch label which was called
|
|
|
|
whereas if you start applying other modifiers to it, you get the filename
|
|
|
|
the batch label is in */
|
|
|
|
if (*lastModifier == '0' && modifierLen > 1) {
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(outputparam, context->batchfileW);
|
2018-08-27 21:41:53 +02:00
|
|
|
} else if ((*lastModifier >= '0' && *lastModifier <= '9')) {
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(outputparam,
|
2012-10-17 00:35:33 +02:00
|
|
|
WCMD_parameter (context -> command,
|
|
|
|
*lastModifier-'0' + context -> shift_count[*lastModifier-'0'],
|
2012-10-25 23:47:59 +02:00
|
|
|
NULL, FALSE, TRUE));
|
2007-02-20 18:49:43 +01:00
|
|
|
} else {
|
2012-11-05 22:43:08 +01:00
|
|
|
int foridx = FOR_VAR_IDX(*lastModifier);
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(outputparam, forloopcontext.variable[foridx]);
|
2007-02-20 18:49:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 1. Handle '~' : Strip surrounding quotes */
|
|
|
|
if (outputparam[0]=='"' &&
|
2019-05-03 12:28:33 +02:00
|
|
|
wmemchr(firstModifier, '~', modifierLen) != NULL) {
|
|
|
|
int len = lstrlenW(outputparam);
|
2007-02-20 18:49:43 +01:00
|
|
|
if (outputparam[len-1] == '"') {
|
|
|
|
outputparam[len-1]=0x00;
|
|
|
|
len = len - 1;
|
|
|
|
}
|
2007-06-03 23:07:42 +02:00
|
|
|
memmove(outputparam, &outputparam[1], (len * sizeof(WCHAR))-1);
|
2007-02-20 18:49:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 2. Handle the special case of a $ */
|
2019-05-03 12:28:33 +02:00
|
|
|
if (wmemchr(firstModifier, '$', modifierLen) != NULL) {
|
2007-02-20 18:49:43 +01:00
|
|
|
/* Special Case: Search envar specified in $[envvar] for outputparam
|
|
|
|
Note both $ and : are guaranteed otherwise check above would fail */
|
2019-05-03 12:28:33 +02:00
|
|
|
WCHAR *begin = wcschr(firstModifier, '$') + 1;
|
|
|
|
WCHAR *end = wcschr(firstModifier, ':');
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR env[MAX_PATH];
|
2015-01-06 17:55:01 +01:00
|
|
|
DWORD size;
|
2007-02-20 18:49:43 +01:00
|
|
|
|
|
|
|
/* Extract the env var */
|
2011-03-09 10:07:20 +01:00
|
|
|
memcpy(env, begin, (end-begin) * sizeof(WCHAR));
|
|
|
|
env[(end-begin)] = 0x00;
|
2007-02-20 18:49:43 +01:00
|
|
|
|
2015-01-06 17:55:01 +01:00
|
|
|
size = GetEnvironmentVariableW(env, NULL, 0);
|
|
|
|
if (size > 0) {
|
2018-11-25 21:56:00 +01:00
|
|
|
WCHAR *fullpath = heap_xalloc(size * sizeof(WCHAR));
|
2015-01-06 17:55:01 +01:00
|
|
|
if (!fullpath || (GetEnvironmentVariableW(env, fullpath, size) == 0) ||
|
|
|
|
(SearchPathW(fullpath, outputparam, NULL, MAX_PATH, outputparam, NULL) == 0))
|
|
|
|
size = 0;
|
|
|
|
heap_free(fullpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!size) {
|
|
|
|
/* If env var not found, return empty string */
|
2007-02-20 18:49:43 +01:00
|
|
|
finaloutput[0] = 0x00;
|
|
|
|
outputparam[0] = 0x00;
|
|
|
|
skipFileParsing = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* After this, we need full information on the file,
|
|
|
|
which is valid not to exist. */
|
|
|
|
if (!skipFileParsing) {
|
2022-01-21 10:32:59 +01:00
|
|
|
if (!WCMD_get_fullpath(outputparam, MAX_PATH, fullfilename, &filepart)) {
|
2013-06-02 22:29:58 +02:00
|
|
|
exists = FALSE;
|
|
|
|
fullfilename[0] = 0x00;
|
|
|
|
} else {
|
|
|
|
exists = GetFileAttributesExW(fullfilename, GetFileExInfoStandard,
|
|
|
|
&fileInfo);
|
|
|
|
}
|
2007-02-20 18:49:43 +01:00
|
|
|
|
2012-10-11 23:53:38 +02:00
|
|
|
/* 2. Handle 'a' : Output attributes (File doesn't have to exist) */
|
2019-05-03 12:28:33 +02:00
|
|
|
if (wmemchr(firstModifier, 'a', modifierLen) != NULL) {
|
2007-02-20 18:49:43 +01:00
|
|
|
|
|
|
|
doneModifier = TRUE;
|
2012-10-11 23:53:38 +02:00
|
|
|
|
|
|
|
if (exists) {
|
2020-03-24 21:12:50 +01:00
|
|
|
lstrcpyW(thisoutput, L"---------");
|
2012-10-11 23:53:38 +02:00
|
|
|
if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
|
|
|
thisoutput[0]='d';
|
|
|
|
if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
|
|
|
|
thisoutput[1]='r';
|
|
|
|
if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)
|
|
|
|
thisoutput[2]='a';
|
|
|
|
if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
|
|
|
|
thisoutput[3]='h';
|
|
|
|
if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
|
|
|
|
thisoutput[4]='s';
|
|
|
|
if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED)
|
|
|
|
thisoutput[5]='c';
|
|
|
|
/* FIXME: What are 6 and 7? */
|
|
|
|
if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
|
|
|
|
thisoutput[8]='l';
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW(finaloutput, thisoutput);
|
2012-10-11 23:53:38 +02:00
|
|
|
}
|
2007-02-20 18:49:43 +01:00
|
|
|
}
|
|
|
|
|
2012-10-11 23:53:38 +02:00
|
|
|
/* 3. Handle 't' : Date+time (File doesn't have to exist) */
|
2019-05-03 12:28:33 +02:00
|
|
|
if (wmemchr(firstModifier, 't', modifierLen) != NULL) {
|
2007-02-20 18:49:43 +01:00
|
|
|
|
|
|
|
SYSTEMTIME systime;
|
|
|
|
int datelen;
|
|
|
|
|
|
|
|
doneModifier = TRUE;
|
|
|
|
|
2012-10-11 23:53:38 +02:00
|
|
|
if (exists) {
|
2020-11-29 23:30:28 +01:00
|
|
|
if (finaloutput[0] != 0x00) lstrcatW(finaloutput, L" ");
|
2012-10-11 23:53:38 +02:00
|
|
|
|
|
|
|
/* Format the time */
|
|
|
|
FileTimeToSystemTime(&fileInfo.ftLastWriteTime, &systime);
|
|
|
|
GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &systime,
|
|
|
|
NULL, thisoutput, MAX_PATH);
|
2020-11-29 23:30:28 +01:00
|
|
|
lstrcatW(thisoutput, L" ");
|
2019-05-03 12:28:33 +02:00
|
|
|
datelen = lstrlenW(thisoutput);
|
2012-10-11 23:53:38 +02:00
|
|
|
GetTimeFormatW(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &systime,
|
|
|
|
NULL, (thisoutput+datelen), MAX_PATH-datelen);
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW(finaloutput, thisoutput);
|
2012-10-11 23:53:38 +02:00
|
|
|
}
|
2007-02-20 18:49:43 +01:00
|
|
|
}
|
|
|
|
|
2012-10-11 23:53:38 +02:00
|
|
|
/* 4. Handle 'z' : File length (File doesn't have to exist) */
|
2019-05-03 12:28:33 +02:00
|
|
|
if (wmemchr(firstModifier, 'z', modifierLen) != NULL) {
|
2007-03-06 14:26:24 +01:00
|
|
|
/* FIXME: Output full 64 bit size (sprintf does not support I64 here) */
|
2007-02-20 18:49:43 +01:00
|
|
|
ULONG/*64*/ fullsize = /*(fileInfo.nFileSizeHigh << 32) +*/
|
|
|
|
fileInfo.nFileSizeLow;
|
|
|
|
|
|
|
|
doneModifier = TRUE;
|
2012-10-11 23:53:38 +02:00
|
|
|
if (exists) {
|
2020-11-29 23:30:28 +01:00
|
|
|
if (finaloutput[0] != 0x00) lstrcatW(finaloutput, L" ");
|
2020-03-24 21:12:50 +01:00
|
|
|
wsprintfW(thisoutput, L"%u", fullsize);
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW(finaloutput, thisoutput);
|
2012-10-11 23:53:38 +02:00
|
|
|
}
|
2007-02-20 18:49:43 +01:00
|
|
|
}
|
|
|
|
|
2007-03-06 14:26:24 +01:00
|
|
|
/* 4. Handle 's' : Use short paths (File doesn't have to exist) */
|
2019-05-03 12:28:33 +02:00
|
|
|
if (wmemchr(firstModifier, 's', modifierLen) != NULL) {
|
2020-11-29 23:30:28 +01:00
|
|
|
if (finaloutput[0] != 0x00) lstrcatW(finaloutput, L" ");
|
2018-08-27 21:41:49 +02:00
|
|
|
|
|
|
|
/* Convert fullfilename's path to a short path - Save filename away as
|
|
|
|
only path is valid, name may not exist which causes GetShortPathName
|
|
|
|
to fail if it is provided */
|
|
|
|
if (filepart) {
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(thisoutput, filepart);
|
2018-08-27 21:41:49 +02:00
|
|
|
*filepart = 0x00;
|
|
|
|
GetShortPathNameW(fullfilename, fullfilename, ARRAY_SIZE(fullfilename));
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW(fullfilename, thisoutput);
|
2018-08-27 21:41:49 +02:00
|
|
|
}
|
2007-02-20 18:49:43 +01:00
|
|
|
}
|
|
|
|
|
2007-03-06 14:26:24 +01:00
|
|
|
/* 5. Handle 'f' : Fully qualified path (File doesn't have to exist) */
|
2007-02-20 18:49:43 +01:00
|
|
|
/* Note this overrides d,p,n,x */
|
2019-05-03 12:28:33 +02:00
|
|
|
if (wmemchr(firstModifier, 'f', modifierLen) != NULL) {
|
2007-02-20 18:49:43 +01:00
|
|
|
doneModifier = TRUE;
|
2020-11-29 23:30:28 +01:00
|
|
|
if (finaloutput[0] != 0x00) lstrcatW(finaloutput, L" ");
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW(finaloutput, fullfilename);
|
2007-02-20 18:49:43 +01:00
|
|
|
} else {
|
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR drive[10];
|
|
|
|
WCHAR dir[MAX_PATH];
|
|
|
|
WCHAR fname[MAX_PATH];
|
|
|
|
WCHAR ext[MAX_PATH];
|
2007-02-20 18:49:43 +01:00
|
|
|
BOOL doneFileModifier = FALSE;
|
2012-10-11 23:53:38 +02:00
|
|
|
BOOL addSpace = (finaloutput[0] != 0x00);
|
2007-02-20 18:49:43 +01:00
|
|
|
|
|
|
|
/* Split into components */
|
2020-04-26 05:53:36 +02:00
|
|
|
_wsplitpath(fullfilename, drive, dir, fname, ext);
|
2007-02-20 18:49:43 +01:00
|
|
|
|
|
|
|
/* 5. Handle 'd' : Drive Letter */
|
2019-05-03 12:28:33 +02:00
|
|
|
if (wmemchr(firstModifier, 'd', modifierLen) != NULL) {
|
2012-10-11 23:53:38 +02:00
|
|
|
if (addSpace) {
|
2020-11-29 23:30:28 +01:00
|
|
|
lstrcatW(finaloutput, L" ");
|
2012-10-11 23:53:38 +02:00
|
|
|
addSpace = FALSE;
|
|
|
|
}
|
|
|
|
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW(finaloutput, drive);
|
2007-02-20 18:49:43 +01:00
|
|
|
doneModifier = TRUE;
|
|
|
|
doneFileModifier = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 6. Handle 'p' : Path */
|
2019-05-03 12:28:33 +02:00
|
|
|
if (wmemchr(firstModifier, 'p', modifierLen) != NULL) {
|
2012-10-11 23:53:38 +02:00
|
|
|
if (addSpace) {
|
2020-11-29 23:30:28 +01:00
|
|
|
lstrcatW(finaloutput, L" ");
|
2012-10-11 23:53:38 +02:00
|
|
|
addSpace = FALSE;
|
|
|
|
}
|
|
|
|
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW(finaloutput, dir);
|
2007-02-20 18:49:43 +01:00
|
|
|
doneModifier = TRUE;
|
|
|
|
doneFileModifier = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 7. Handle 'n' : Name */
|
2019-05-03 12:28:33 +02:00
|
|
|
if (wmemchr(firstModifier, 'n', modifierLen) != NULL) {
|
2012-10-11 23:53:38 +02:00
|
|
|
if (addSpace) {
|
2020-11-29 23:30:28 +01:00
|
|
|
lstrcatW(finaloutput, L" ");
|
2012-10-11 23:53:38 +02:00
|
|
|
addSpace = FALSE;
|
|
|
|
}
|
|
|
|
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW(finaloutput, fname);
|
2007-02-20 18:49:43 +01:00
|
|
|
doneModifier = TRUE;
|
|
|
|
doneFileModifier = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 8. Handle 'x' : Ext */
|
2019-05-03 12:28:33 +02:00
|
|
|
if (wmemchr(firstModifier, 'x', modifierLen) != NULL) {
|
2012-10-11 23:53:38 +02:00
|
|
|
if (addSpace) {
|
2020-11-29 23:30:28 +01:00
|
|
|
lstrcatW(finaloutput, L" ");
|
2012-10-11 23:53:38 +02:00
|
|
|
addSpace = FALSE;
|
|
|
|
}
|
|
|
|
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW(finaloutput, ext);
|
2007-02-20 18:49:43 +01:00
|
|
|
doneModifier = TRUE;
|
|
|
|
doneFileModifier = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If 's' but no other parameter, dump the whole thing */
|
|
|
|
if (!doneFileModifier &&
|
2019-05-03 12:28:33 +02:00
|
|
|
wmemchr(firstModifier, 's', modifierLen) != NULL) {
|
2007-02-20 18:49:43 +01:00
|
|
|
doneModifier = TRUE;
|
2020-11-29 23:30:28 +01:00
|
|
|
if (finaloutput[0] != 0x00) lstrcatW(finaloutput, L" ");
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcatW(finaloutput, fullfilename);
|
2007-02-20 18:49:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If No other modifier processed, just add in parameter */
|
2019-05-03 12:28:33 +02:00
|
|
|
if (!doneModifier) lstrcpyW(finaloutput, outputparam);
|
2007-02-20 18:49:43 +01:00
|
|
|
|
|
|
|
/* Finish by inserting the replacement into the string */
|
2009-06-15 10:59:31 +02:00
|
|
|
WCMD_strsubstW(*start, lastModifier+1, finaloutput, -1);
|
2007-02-20 18:49:43 +01:00
|
|
|
}
|
2007-02-23 23:19:25 +01:00
|
|
|
|
|
|
|
/*******************************************************************
|
|
|
|
* WCMD_call - processes a batch call statement
|
|
|
|
*
|
|
|
|
* If there is a leading ':', calls within this batch program
|
|
|
|
* otherwise launches another program.
|
|
|
|
*/
|
2007-06-03 23:07:42 +02:00
|
|
|
void WCMD_call (WCHAR *command) {
|
2007-02-23 23:19:25 +01:00
|
|
|
|
|
|
|
/* Run other program if no leading ':' */
|
|
|
|
if (*command != ':') {
|
2012-03-29 18:05:59 +02:00
|
|
|
WCMD_run_program(command, TRUE);
|
2012-10-14 01:38:53 +02:00
|
|
|
/* If the thing we try to run does not exist, call returns 1 */
|
|
|
|
if (errorlevel) errorlevel=1;
|
2007-02-23 23:19:25 +01:00
|
|
|
} else {
|
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR gotoLabel[MAX_PATH];
|
2007-02-23 23:19:25 +01:00
|
|
|
|
2019-05-03 12:28:33 +02:00
|
|
|
lstrcpyW(gotoLabel, param1);
|
2007-02-23 23:19:25 +01:00
|
|
|
|
|
|
|
if (context) {
|
|
|
|
|
|
|
|
LARGE_INTEGER li;
|
2012-11-05 22:43:08 +01:00
|
|
|
FOR_CONTEXT oldcontext;
|
|
|
|
|
|
|
|
/* Save the for variable context, then start with an empty context
|
|
|
|
as for loop variables do not survive a call */
|
|
|
|
oldcontext = forloopcontext;
|
|
|
|
memset(&forloopcontext, 0, sizeof(forloopcontext));
|
2007-02-23 23:19:25 +01:00
|
|
|
|
|
|
|
/* Save the current file position, call the same file,
|
|
|
|
restore position */
|
|
|
|
li.QuadPart = 0;
|
2007-03-04 01:59:59 +01:00
|
|
|
li.u.LowPart = SetFilePointer(context -> h, li.u.LowPart,
|
|
|
|
&li.u.HighPart, FILE_CURRENT);
|
2018-08-27 21:41:53 +02:00
|
|
|
WCMD_batch (context->batchfileW, command, TRUE, gotoLabel, context->h);
|
2007-03-04 01:59:59 +01:00
|
|
|
SetFilePointer(context -> h, li.u.LowPart,
|
|
|
|
&li.u.HighPart, FILE_BEGIN);
|
2012-11-05 22:43:08 +01:00
|
|
|
|
|
|
|
/* Restore the for loop context */
|
|
|
|
forloopcontext = oldcontext;
|
2007-02-23 23:19:25 +01:00
|
|
|
} else {
|
2011-09-24 12:50:56 +02:00
|
|
|
WCMD_output_asis_stderr(WCMD_LoadMessage(WCMD_CALLINSCRIPT));
|
2007-02-23 23:19:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|