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"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(cmd);
|
1999-06-06 17:24:04 +02:00
|
|
|
|
|
|
|
extern int echo_mode;
|
2007-06-03 23:07:42 +02:00
|
|
|
extern WCHAR quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
|
1999-06-26 12:24:08 +02:00
|
|
|
extern BATCH_CONTEXT *context;
|
2000-08-02 00:02:18 +02:00
|
|
|
extern DWORD errorlevel;
|
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
|
|
|
*/
|
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
void WCMD_batch (WCHAR *file, WCHAR *command, int called, WCHAR *startLabel, HANDLE pgmHandle) {
|
1999-06-06 17:24:04 +02:00
|
|
|
|
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) {
|
2010-02-18 17:09:04 +01:00
|
|
|
h = CreateFileW (file, GENERIC_READ, FILE_SHARE_READ,
|
|
|
|
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;
|
2010-02-18 17:09:04 +01:00
|
|
|
context->batchfileW = WCMD_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) {
|
2007-06-03 23:07:42 +02:00
|
|
|
strcpyW(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 */
|
|
|
|
if (WCMD_ReadAndParseLine(NULL, &toExecute, h) == NULL)
|
|
|
|
break;
|
2007-06-15 21:59:28 +02:00
|
|
|
WCMD_process_commands(toExecute, FALSE, NULL, NULL);
|
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
|
|
|
|
|
|
|
/*
|
|
|
|
* If invoked by a CALL, we return to the context of our caller. Otherwise return
|
|
|
|
* to the caller's caller.
|
|
|
|
*/
|
|
|
|
|
2010-02-03 01:56:38 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, 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)) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************
|
|
|
|
* WCMD_parameter - extract a parameter from a command line.
|
|
|
|
*
|
2007-09-11 22:43:04 +02:00
|
|
|
* Returns the 'n'th delimited parameter on the command line (zero-based).
|
1999-06-06 17:24:04 +02:00
|
|
|
* Parameter is in static storage overwritten on the next call.
|
1999-06-26 12:24:08 +02:00
|
|
|
* Parameters in quotes (and brackets) are handled.
|
1999-07-10 13:36:45 +02:00
|
|
|
* Also returns a pointer to the location of the parameter in the command line.
|
1999-06-06 17:24:04 +02:00
|
|
|
*/
|
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR *WCMD_parameter (WCHAR *s, int n, WCHAR **where) {
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2007-03-01 12:43:19 +01:00
|
|
|
int i = 0;
|
2007-06-03 23:07:42 +02:00
|
|
|
static WCHAR param[MAX_PATH];
|
|
|
|
WCHAR *p;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2007-02-28 00:21:59 +01:00
|
|
|
if (where != NULL) *where = NULL;
|
1999-06-06 17:24:04 +02:00
|
|
|
p = param;
|
|
|
|
while (TRUE) {
|
|
|
|
switch (*s) {
|
2007-09-11 22:43:04 +02:00
|
|
|
case ' ': /* Skip leading spaces */
|
2009-10-14 12:22:34 +02:00
|
|
|
case '\t': /* Treat tabs as spaces */
|
1999-06-06 17:24:04 +02:00
|
|
|
s++;
|
|
|
|
break;
|
|
|
|
case '"':
|
2007-02-28 00:21:59 +01:00
|
|
|
if (where != NULL && i==n) *where = s;
|
1999-06-06 17:24:04 +02:00
|
|
|
s++;
|
|
|
|
while ((*s != '\0') && (*s != '"')) {
|
|
|
|
*p++ = *s++;
|
|
|
|
}
|
|
|
|
if (i == n) {
|
|
|
|
*p = '\0';
|
|
|
|
return param;
|
|
|
|
}
|
1999-07-10 13:36:45 +02:00
|
|
|
if (*s == '"') s++;
|
1999-06-06 17:24:04 +02:00
|
|
|
param[0] = '\0';
|
|
|
|
i++;
|
1999-07-10 13:36:45 +02:00
|
|
|
p = param;
|
1999-06-06 17:24:04 +02:00
|
|
|
break;
|
2007-06-15 21:59:28 +02:00
|
|
|
/* The code to handle bracketed parms is removed because it should no longer
|
|
|
|
be necessary after the multiline support has been added and the for loop
|
|
|
|
set of data is now parseable individually. */
|
1999-06-06 17:24:04 +02:00
|
|
|
case '\0':
|
|
|
|
return param;
|
|
|
|
default:
|
2007-02-20 19:02:29 +01:00
|
|
|
/* Only return where if it is for the right parameter */
|
|
|
|
if (where != NULL && i==n) *where = s;
|
2009-10-14 12:22:34 +02:00
|
|
|
while ((*s != '\0') && (*s != ' ') && (*s != ',') && (*s != '=') && (*s != '\t')) {
|
1999-06-06 17:24:04 +02:00
|
|
|
*p++ = *s++;
|
|
|
|
}
|
2007-09-11 22:43:04 +02:00
|
|
|
if (i == n && (p!=param)) {
|
1999-06-06 17:24:04 +02:00
|
|
|
*p = '\0';
|
|
|
|
return param;
|
|
|
|
}
|
2007-09-11 22:43:04 +02:00
|
|
|
/* Skip double delimiters, eg. dir a.a,,,,,b.b */
|
|
|
|
if (p != param) {
|
1999-06-06 17:24:04 +02:00
|
|
|
param[0] = '\0';
|
|
|
|
i++;
|
2007-09-11 22:43:04 +02:00
|
|
|
} else {
|
2008-04-07 13:01:02 +02:00
|
|
|
s++; /* Skip delimiter */
|
2007-09-11 22:43:04 +02:00
|
|
|
}
|
1999-07-10 13:36:45 +02:00
|
|
|
p = param;
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-02 00:02:18 +02:00
|
|
|
/****************************************************************************
|
1999-06-06 17:24:04 +02:00
|
|
|
* WCMD_fgets
|
|
|
|
*
|
|
|
|
* Get one line from a batch file. We can't use the native f* functions because
|
1999-06-26 12:24:08 +02:00
|
|
|
* of the filename syntax differences between DOS and Unix. Also need to lose
|
|
|
|
* the LF (or CRLF) from the line.
|
1999-06-06 17:24:04 +02:00
|
|
|
*/
|
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR *WCMD_fgets (WCHAR *s, int noChars, HANDLE h) {
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2007-03-01 12:43:19 +01:00
|
|
|
DWORD bytes;
|
|
|
|
BOOL status;
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR *p;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
|
|
|
p = s;
|
|
|
|
do {
|
2007-06-03 23:07:42 +02:00
|
|
|
status = WCMD_ReadFile (h, s, 1, &bytes, NULL);
|
1999-06-26 12:24:08 +02:00
|
|
|
if ((status == 0) || ((bytes == 0) && (s == p))) return NULL;
|
1999-06-06 17:24:04 +02:00
|
|
|
if (*s == '\n') bytes = 0;
|
1999-06-26 12:24:08 +02:00
|
|
|
else if (*s != '\r') {
|
|
|
|
s++;
|
2007-06-03 23:07:42 +02:00
|
|
|
noChars--;
|
1999-06-26 12:24:08 +02:00
|
|
|
}
|
|
|
|
*s = '\0';
|
2007-06-03 23:07:42 +02:00
|
|
|
} while ((bytes == 1) && (noChars > 1));
|
1999-06-06 17:24:04 +02:00
|
|
|
return p;
|
|
|
|
}
|
2007-02-20 18:49:43 +01:00
|
|
|
|
2007-02-28 00:19:24 +01:00
|
|
|
/* WCMD_splitpath - copied from winefile as no obvious way to use it otherwise */
|
2007-06-03 23:07:42 +02:00
|
|
|
void WCMD_splitpath(const WCHAR* path, WCHAR* drv, WCHAR* dir, WCHAR* name, WCHAR* ext)
|
2007-02-20 18:49:43 +01:00
|
|
|
{
|
2007-06-03 23:07:42 +02:00
|
|
|
const WCHAR* end; /* end of processed string */
|
|
|
|
const WCHAR* p; /* search pointer */
|
|
|
|
const WCHAR* s; /* copy pointer */
|
2007-02-20 18:49:43 +01:00
|
|
|
|
|
|
|
/* extract drive name */
|
|
|
|
if (path[0] && path[1]==':') {
|
|
|
|
if (drv) {
|
|
|
|
*drv++ = *path++;
|
|
|
|
*drv++ = *path++;
|
|
|
|
*drv = '\0';
|
|
|
|
}
|
|
|
|
} else if (drv)
|
|
|
|
*drv = '\0';
|
|
|
|
|
|
|
|
/* search for end of string or stream separator */
|
|
|
|
for(end=path; *end && *end!=':'; )
|
|
|
|
end++;
|
|
|
|
|
|
|
|
/* search for begin of file extension */
|
|
|
|
for(p=end; p>path && *--p!='\\' && *p!='/'; )
|
|
|
|
if (*p == '.') {
|
|
|
|
end = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ext)
|
|
|
|
for(s=end; (*ext=*s++); )
|
|
|
|
ext++;
|
|
|
|
|
|
|
|
/* search for end of directory name */
|
|
|
|
for(p=end; p>path; )
|
|
|
|
if (*--p=='\\' || *p=='/') {
|
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
for(s=p; s<end; )
|
|
|
|
*name++ = *s++;
|
|
|
|
|
|
|
|
*name = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dir) {
|
|
|
|
for(s=path; s<p; )
|
|
|
|
*dir++ = *s++;
|
|
|
|
|
|
|
|
*dir = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_HandleTildaModifiers
|
|
|
|
*
|
|
|
|
* Handle the ~ modifiers when expanding %0-9 or (%a-z in for command)
|
|
|
|
* %~xxxxxV (V=0-9 or A-Z)
|
|
|
|
* 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
|
|
|
|
*/
|
2007-09-11 22:43:03 +02:00
|
|
|
void WCMD_HandleTildaModifiers(WCHAR **start, WCHAR *forVariable, WCHAR *forValue, BOOL justFors) {
|
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', '$'
|
|
|
|
};
|
2007-06-03 23:07:42 +02:00
|
|
|
static const WCHAR space[] = {' ', '\0'};
|
2007-02-20 18:49:43 +01:00
|
|
|
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fileInfo;
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR outputparam[MAX_PATH];
|
|
|
|
WCHAR finaloutput[MAX_PATH];
|
|
|
|
WCHAR fullfilename[MAX_PATH];
|
|
|
|
WCHAR thisoutput[MAX_PATH];
|
|
|
|
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) {
|
|
|
|
WINE_TRACE("Looking backwards for parameter id: %s / %s\n",
|
|
|
|
wine_dbgstr_w(lastModifier), wine_dbgstr_w(forVariable));
|
|
|
|
|
2009-06-15 19:05:33 +02:00
|
|
|
if (!justFors && context && (*lastModifier >= '0' && *lastModifier <= '9')) {
|
2007-09-11 22:43:03 +02:00
|
|
|
/* Its a valid parameter identifier - OK */
|
|
|
|
break;
|
|
|
|
|
|
|
|
} else if (forVariable && *lastModifier == *(forVariable+1)) {
|
|
|
|
/* Its a valid parameter identifier - OK */
|
|
|
|
break;
|
2007-02-20 18:49:43 +01:00
|
|
|
|
2007-09-11 22:43:03 +02:00
|
|
|
} else {
|
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
|
|
|
|
|
|
|
/* Extract the parameter to play with */
|
2010-02-03 01:56:38 +01:00
|
|
|
if (*lastModifier == '0') {
|
|
|
|
strcpyW(outputparam, context->batchfileW);
|
|
|
|
} else if ((*lastModifier >= '1' && *lastModifier <= '9')) {
|
2007-06-03 23:07:42 +02:00
|
|
|
strcpyW(outputparam, WCMD_parameter (context -> command,
|
2007-03-08 01:50:37 +01:00
|
|
|
*lastModifier-'0' + context -> shift_count[*lastModifier-'0'], NULL));
|
2007-02-20 18:49:43 +01:00
|
|
|
} else {
|
2007-09-11 22:43:03 +02:00
|
|
|
strcpyW(outputparam, forValue);
|
2007-02-20 18:49:43 +01: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;
|
|
|
|
|
|
|
|
/* Useful for debugging purposes: */
|
|
|
|
/*printf("Modifier string '%*.*s' and variable is %c\n Param starts as '%s'\n",
|
|
|
|
(modifierLen), (modifierLen), firstModifier, *lastModifier,
|
|
|
|
outputparam);*/
|
|
|
|
|
|
|
|
/* 1. Handle '~' : Strip surrounding quotes */
|
|
|
|
if (outputparam[0]=='"' &&
|
2007-06-03 23:07:42 +02:00
|
|
|
memchrW(firstModifier, '~', modifierLen) != NULL) {
|
|
|
|
int len = strlenW(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 $ */
|
2007-06-03 23:07:42 +02:00
|
|
|
if (memchrW(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 */
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR *start = strchrW(firstModifier, '$') + 1;
|
|
|
|
WCHAR *end = strchrW(firstModifier, ':');
|
|
|
|
WCHAR env[MAX_PATH];
|
|
|
|
WCHAR fullpath[MAX_PATH];
|
2007-02-20 18:49:43 +01:00
|
|
|
|
|
|
|
/* Extract the env var */
|
2007-06-03 23:07:42 +02:00
|
|
|
memcpy(env, start, (end-start) * sizeof(WCHAR));
|
2007-02-20 18:49:43 +01:00
|
|
|
env[(end-start)] = 0x00;
|
|
|
|
|
2008-01-16 12:20:50 +01:00
|
|
|
/* If env var not found, return empty string */
|
2009-12-09 18:52:40 +01:00
|
|
|
if ((GetEnvironmentVariableW(env, fullpath, MAX_PATH) == 0) ||
|
|
|
|
(SearchPathW(fullpath, outputparam, NULL, MAX_PATH, outputparam, NULL) == 0)) {
|
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) {
|
2009-12-09 18:52:40 +01:00
|
|
|
if (GetFullPathNameW(outputparam, MAX_PATH, fullfilename, NULL) == 0)
|
2007-02-20 18:49:43 +01:00
|
|
|
return;
|
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
exists = GetFileAttributesExW(fullfilename, GetFileExInfoStandard,
|
2007-02-20 18:49:43 +01:00
|
|
|
&fileInfo);
|
|
|
|
|
|
|
|
/* 2. Handle 'a' : Output attributes */
|
|
|
|
if (exists &&
|
2007-06-03 23:07:42 +02:00
|
|
|
memchrW(firstModifier, 'a', modifierLen) != NULL) {
|
2007-02-20 18:49:43 +01:00
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR defaults[] = {'-','-','-','-','-','-','-','-','-','\0'};
|
2007-02-20 18:49:43 +01:00
|
|
|
doneModifier = TRUE;
|
2007-06-03 23:07:42 +02:00
|
|
|
strcpyW(thisoutput, defaults);
|
2007-02-20 18:49:43 +01: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';
|
2007-06-03 23:07:42 +02:00
|
|
|
strcatW(finaloutput, thisoutput);
|
2007-02-20 18:49:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 3. Handle 't' : Date+time */
|
|
|
|
if (exists &&
|
2007-06-03 23:07:42 +02:00
|
|
|
memchrW(firstModifier, 't', modifierLen) != NULL) {
|
2007-02-20 18:49:43 +01:00
|
|
|
|
|
|
|
SYSTEMTIME systime;
|
|
|
|
int datelen;
|
|
|
|
|
|
|
|
doneModifier = TRUE;
|
2007-06-03 23:07:42 +02:00
|
|
|
if (finaloutput[0] != 0x00) strcatW(finaloutput, space);
|
2007-02-20 18:49:43 +01:00
|
|
|
|
|
|
|
/* Format the time */
|
|
|
|
FileTimeToSystemTime(&fileInfo.ftLastWriteTime, &systime);
|
2009-12-09 18:52:40 +01:00
|
|
|
GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &systime,
|
2007-02-20 18:49:43 +01:00
|
|
|
NULL, thisoutput, MAX_PATH);
|
2007-06-03 23:07:42 +02:00
|
|
|
strcatW(thisoutput, space);
|
|
|
|
datelen = strlenW(thisoutput);
|
2009-12-09 18:52:40 +01:00
|
|
|
GetTimeFormatW(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &systime,
|
2007-02-20 18:49:43 +01:00
|
|
|
NULL, (thisoutput+datelen), MAX_PATH-datelen);
|
2007-06-03 23:07:42 +02:00
|
|
|
strcatW(finaloutput, thisoutput);
|
2007-02-20 18:49:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 4. Handle 'z' : File length */
|
|
|
|
if (exists &&
|
2007-06-03 23:07:42 +02:00
|
|
|
memchrW(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;
|
2007-06-03 23:07:42 +02:00
|
|
|
static const WCHAR fmt[] = {'%','u','\0'};
|
2007-02-20 18:49:43 +01:00
|
|
|
|
|
|
|
doneModifier = TRUE;
|
2007-06-03 23:07:42 +02:00
|
|
|
if (finaloutput[0] != 0x00) strcatW(finaloutput, space);
|
2009-12-09 18:52:40 +01:00
|
|
|
wsprintfW(thisoutput, fmt, fullsize);
|
2007-06-03 23:07:42 +02:00
|
|
|
strcatW(finaloutput, thisoutput);
|
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) */
|
2007-06-03 23:07:42 +02:00
|
|
|
if (memchrW(firstModifier, 's', modifierLen) != NULL) {
|
|
|
|
if (finaloutput[0] != 0x00) strcatW(finaloutput, space);
|
2007-03-06 14:26:24 +01:00
|
|
|
/* Don't flag as doneModifier - %~s on its own is processed later */
|
2009-12-09 18:52:40 +01:00
|
|
|
GetShortPathNameW(outputparam, outputparam, sizeof(outputparam)/sizeof(outputparam[0]));
|
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 */
|
2007-06-03 23:07:42 +02:00
|
|
|
if (memchrW(firstModifier, 'f', modifierLen) != NULL) {
|
2007-02-20 18:49:43 +01:00
|
|
|
doneModifier = TRUE;
|
2007-06-03 23:07:42 +02:00
|
|
|
if (finaloutput[0] != 0x00) strcatW(finaloutput, space);
|
|
|
|
strcatW(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;
|
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
if (finaloutput[0] != 0x00) strcatW(finaloutput, space);
|
2007-02-20 18:49:43 +01:00
|
|
|
|
|
|
|
/* Split into components */
|
2007-02-28 00:19:24 +01:00
|
|
|
WCMD_splitpath(fullfilename, drive, dir, fname, ext);
|
2007-02-20 18:49:43 +01:00
|
|
|
|
|
|
|
/* 5. Handle 'd' : Drive Letter */
|
2007-06-03 23:07:42 +02:00
|
|
|
if (memchrW(firstModifier, 'd', modifierLen) != NULL) {
|
|
|
|
strcatW(finaloutput, drive);
|
2007-02-20 18:49:43 +01:00
|
|
|
doneModifier = TRUE;
|
|
|
|
doneFileModifier = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 6. Handle 'p' : Path */
|
2007-06-03 23:07:42 +02:00
|
|
|
if (memchrW(firstModifier, 'p', modifierLen) != NULL) {
|
|
|
|
strcatW(finaloutput, dir);
|
2007-02-20 18:49:43 +01:00
|
|
|
doneModifier = TRUE;
|
|
|
|
doneFileModifier = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 7. Handle 'n' : Name */
|
2007-06-03 23:07:42 +02:00
|
|
|
if (memchrW(firstModifier, 'n', modifierLen) != NULL) {
|
|
|
|
strcatW(finaloutput, fname);
|
2007-02-20 18:49:43 +01:00
|
|
|
doneModifier = TRUE;
|
|
|
|
doneFileModifier = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 8. Handle 'x' : Ext */
|
2007-06-03 23:07:42 +02:00
|
|
|
if (memchrW(firstModifier, 'x', modifierLen) != NULL) {
|
|
|
|
strcatW(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 &&
|
2007-06-03 23:07:42 +02:00
|
|
|
memchrW(firstModifier, 's', modifierLen) != NULL) {
|
2007-02-20 18:49:43 +01:00
|
|
|
doneModifier = TRUE;
|
2007-06-03 23:07:42 +02:00
|
|
|
if (finaloutput[0] != 0x00) strcatW(finaloutput, space);
|
|
|
|
strcatW(finaloutput, outputparam);
|
2007-02-20 18:49:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If No other modifier processed, just add in parameter */
|
2007-06-03 23:07:42 +02:00
|
|
|
if (!doneModifier) strcpyW(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 != ':') {
|
|
|
|
WCMD_run_program(command, 1);
|
|
|
|
} else {
|
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
WCHAR gotoLabel[MAX_PATH];
|
2007-02-23 23:19:25 +01:00
|
|
|
|
2007-06-03 23:07:42 +02:00
|
|
|
strcpyW(gotoLabel, param1);
|
2007-02-23 23:19:25 +01:00
|
|
|
|
|
|
|
if (context) {
|
|
|
|
|
|
|
|
LARGE_INTEGER li;
|
|
|
|
|
|
|
|
/* 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);
|
2007-02-23 23:19:25 +01:00
|
|
|
|
|
|
|
WCMD_batch (param1, command, 1, gotoLabel, context->h);
|
|
|
|
|
2007-03-04 01:59:59 +01:00
|
|
|
SetFilePointer(context -> h, li.u.LowPart,
|
|
|
|
&li.u.HighPart, FILE_BEGIN);
|
2007-02-23 23:19:25 +01:00
|
|
|
} else {
|
2007-06-03 23:07:39 +02:00
|
|
|
WCMD_output_asis( WCMD_LoadMessage(WCMD_CALLINSCRIPT));
|
2007-02-23 23:19:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|