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
|
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"
|
|
|
|
|
1999-06-26 12:24:08 +02:00
|
|
|
void WCMD_batch_command (char *line);
|
2007-02-20 18:49:43 +01:00
|
|
|
void WCMD_HandleTildaModifiers(char **start, char *forVariable);
|
1999-06-06 17:24:04 +02:00
|
|
|
|
|
|
|
extern int echo_mode;
|
|
|
|
extern char 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
|
|
|
|
2005-12-19 17:42:11 +01:00
|
|
|
/* msdn specified max for Win XP */
|
|
|
|
#define MAXSTRING 8192
|
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.
|
1999-06-06 17:24:04 +02:00
|
|
|
*/
|
|
|
|
|
1999-06-26 12:24:08 +02:00
|
|
|
void WCMD_batch (char *file, char *command, int called) {
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2003-03-25 01:33:56 +01:00
|
|
|
#define WCMD_BATCH_EXT_SIZE 5
|
|
|
|
|
|
|
|
HANDLE h = INVALID_HANDLE_VALUE;
|
2002-07-24 21:00:05 +02:00
|
|
|
char string[MAXSTRING];
|
2005-03-28 12:01:31 +02:00
|
|
|
char extension_batch[][WCMD_BATCH_EXT_SIZE] = {".bat",".cmd"};
|
|
|
|
char extension_exe[WCMD_BATCH_EXT_SIZE] = ".exe";
|
2004-09-22 04:46:38 +02:00
|
|
|
unsigned int i;
|
1999-06-26 12:24:08 +02:00
|
|
|
BATCH_CONTEXT *prev_context;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2005-03-28 12:01:31 +02:00
|
|
|
for(i=0; (i<(sizeof(extension_batch)/WCMD_BATCH_EXT_SIZE)) &&
|
2003-03-25 01:33:56 +01:00
|
|
|
(h == INVALID_HANDLE_VALUE); i++) {
|
1999-06-06 17:24:04 +02:00
|
|
|
strcpy (string, file);
|
|
|
|
CharLower (string);
|
2005-03-28 12:01:31 +02:00
|
|
|
if (strstr (string, extension_batch[i]) == NULL) strcat (string, extension_batch[i]);
|
2003-01-15 04:35:32 +01:00
|
|
|
h = CreateFile (string, GENERIC_READ, FILE_SHARE_READ,
|
|
|
|
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
2003-03-25 01:33:56 +01:00
|
|
|
}
|
1999-06-06 17:24:04 +02:00
|
|
|
if (h == INVALID_HANDLE_VALUE) {
|
2005-03-28 12:01:31 +02:00
|
|
|
strcpy (string, file);
|
|
|
|
CharLower (string);
|
|
|
|
if (strstr (string, extension_exe) == NULL) strcat (string, extension_exe);
|
|
|
|
h = CreateFile (string, GENERIC_READ, FILE_SHARE_READ,
|
|
|
|
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (h != INVALID_HANDLE_VALUE) {
|
2006-02-06 14:11:40 +01:00
|
|
|
WCMD_run_program (command, 0);
|
2005-03-28 12:01:31 +02:00
|
|
|
} else {
|
|
|
|
SetLastError (ERROR_FILE_NOT_FOUND);
|
|
|
|
WCMD_print_error ();
|
|
|
|
}
|
1999-06-06 17:24:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-06-26 12:24:08 +02:00
|
|
|
/*
|
|
|
|
* Create a context structure for this batch file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
prev_context = context;
|
|
|
|
context = (BATCH_CONTEXT *)LocalAlloc (LMEM_FIXED, sizeof (BATCH_CONTEXT));
|
|
|
|
context -> h = h;
|
|
|
|
context -> command = command;
|
|
|
|
context -> shift_count = 0;
|
|
|
|
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
|
|
|
|
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-02-20 18:49:08 +01:00
|
|
|
while (context -> skip_rest == FALSE && WCMD_fgets (string, sizeof(string), h)) {
|
2005-12-19 17:42:11 +01:00
|
|
|
if (strlen(string) == MAXSTRING -1) {
|
|
|
|
WCMD_output_asis( "Line in Batch processing possibly truncated. Using:\n");
|
|
|
|
WCMD_output_asis( string);
|
|
|
|
WCMD_output_asis( "\n");
|
|
|
|
}
|
|
|
|
if (string[0] != ':') { /* Skip over labels */
|
|
|
|
WCMD_batch_command (string);
|
|
|
|
}
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
LocalFree ((HANDLE)context);
|
|
|
|
if ((prev_context != NULL) && (!called)) {
|
|
|
|
CloseHandle (prev_context -> h);
|
|
|
|
context = prev_context -> prev_context;
|
|
|
|
LocalFree ((HANDLE)prev_context);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
context = prev_context;
|
|
|
|
}
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_batch_command
|
|
|
|
*
|
1999-06-26 12:24:08 +02:00
|
|
|
* Execute one line from a batch file, expanding parameters.
|
1999-06-06 17:24:04 +02:00
|
|
|
*/
|
|
|
|
|
1999-06-26 12:24:08 +02:00
|
|
|
void WCMD_batch_command (char *line) {
|
1999-06-06 17:24:04 +02:00
|
|
|
|
|
|
|
DWORD status;
|
2002-07-24 21:00:05 +02:00
|
|
|
char cmd1[MAXSTRING],cmd2[MAXSTRING];
|
1999-06-26 12:24:08 +02:00
|
|
|
char *p, *s, *t;
|
|
|
|
int i;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/* Get working version of command line */
|
|
|
|
strcpy(cmd1, line);
|
1999-07-10 13:36:45 +02:00
|
|
|
|
2000-08-01 04:14:33 +02:00
|
|
|
/* Expand environment variables in a batch file %{0-9} first */
|
|
|
|
/* Then env vars, and if any left (ie use of undefined vars,*/
|
|
|
|
/* replace with spaces */
|
|
|
|
/* FIXME: Winnt would replace %1%fred%1 with first parm, then */
|
|
|
|
/* contents of fred, then the digit 1. Would need to remove */
|
|
|
|
/* ExpandEnvStrings to achieve this */
|
|
|
|
|
2007-02-20 01:40:28 +01:00
|
|
|
/* Replace use of %0...%9 and errorlevel*/
|
2002-06-01 01:06:46 +02:00
|
|
|
p = cmd1;
|
1999-06-26 12:24:08 +02:00
|
|
|
while ((p = strchr(p, '%'))) {
|
|
|
|
i = *(p+1) - '0';
|
2007-02-20 18:49:43 +01:00
|
|
|
if (*(p+1) == '~') {
|
|
|
|
WCMD_HandleTildaModifiers(&p, NULL);
|
|
|
|
p++;
|
|
|
|
} else if ((i >= 0) && (i <= 9)) {
|
1999-06-26 12:24:08 +02:00
|
|
|
s = strdup (p+2);
|
1999-07-10 13:36:45 +02:00
|
|
|
t = WCMD_parameter (context -> command, i + context -> shift_count, NULL);
|
1999-06-26 12:24:08 +02:00
|
|
|
strcpy (p, t);
|
|
|
|
strcat (p, s);
|
|
|
|
free (s);
|
2007-02-20 19:02:29 +01:00
|
|
|
} else if (*(p+1)=='*') {
|
|
|
|
char *startOfParms = NULL;
|
|
|
|
s = strdup (p+2);
|
|
|
|
t = WCMD_parameter (context -> command, 1, &startOfParms);
|
|
|
|
if (startOfParms != NULL) strcpy (p, startOfParms);
|
|
|
|
else *p = 0x00;
|
|
|
|
strcat (p, s);
|
|
|
|
free (s);
|
2007-02-20 01:41:28 +01:00
|
|
|
|
|
|
|
/* Handle DATE, TIME, ERRORLEVEL and CD replacements allowing */
|
|
|
|
/* override if existing env var called that name */
|
|
|
|
} else if ((CompareString (LOCALE_USER_DEFAULT,
|
|
|
|
NORM_IGNORECASE | SORT_STRINGSORT,
|
|
|
|
(p+1), 11, "ERRORLEVEL%", -1) == 2) &&
|
|
|
|
(GetEnvironmentVariable("ERRORLEVEL", cmd2, 1) == 0) &&
|
|
|
|
(GetLastError() == ERROR_ENVVAR_NOT_FOUND)) {
|
|
|
|
char output[10];
|
|
|
|
sprintf(output, "%d", errorlevel);
|
|
|
|
s = strdup (p+12);
|
|
|
|
strcpy (p, output);
|
|
|
|
strcat (p, s);
|
|
|
|
|
|
|
|
} else if ((CompareString (LOCALE_USER_DEFAULT,
|
|
|
|
NORM_IGNORECASE | SORT_STRINGSORT,
|
|
|
|
(p+1), 5, "DATE%", -1) == 2) &&
|
|
|
|
(GetEnvironmentVariable("DATE", cmd2, 1) == 0) &&
|
|
|
|
(GetLastError() == ERROR_ENVVAR_NOT_FOUND)) {
|
|
|
|
|
|
|
|
GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL,
|
|
|
|
NULL, cmd2, MAXSTRING);
|
|
|
|
s = strdup (p+6);
|
|
|
|
strcpy (p, cmd2);
|
|
|
|
strcat (p, s);
|
|
|
|
|
|
|
|
} else if ((CompareString (LOCALE_USER_DEFAULT,
|
|
|
|
NORM_IGNORECASE | SORT_STRINGSORT,
|
|
|
|
(p+1), 5, "TIME%", -1) == 2) &&
|
|
|
|
(GetEnvironmentVariable("TIME", cmd2, 1) == 0) &&
|
|
|
|
(GetLastError() == ERROR_ENVVAR_NOT_FOUND)) {
|
|
|
|
GetTimeFormat(LOCALE_USER_DEFAULT, TIME_NOSECONDS, NULL,
|
|
|
|
NULL, cmd2, MAXSTRING);
|
|
|
|
s = strdup (p+6);
|
|
|
|
strcpy (p, cmd2);
|
|
|
|
strcat (p, s);
|
|
|
|
|
|
|
|
} else if ((CompareString (LOCALE_USER_DEFAULT,
|
|
|
|
NORM_IGNORECASE | SORT_STRINGSORT,
|
|
|
|
(p+1), 3, "CD%", -1) == 2) &&
|
|
|
|
(GetEnvironmentVariable("CD", cmd2, 1) == 0) &&
|
|
|
|
(GetLastError() == ERROR_ENVVAR_NOT_FOUND)) {
|
|
|
|
GetCurrentDirectory (MAXSTRING, cmd2);
|
|
|
|
s = strdup (p+4);
|
|
|
|
strcpy (p, cmd2);
|
|
|
|
strcat (p, s);
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
} else {
|
|
|
|
p++;
|
2000-08-01 04:14:33 +02:00
|
|
|
}
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
2000-08-01 04:14:33 +02:00
|
|
|
|
|
|
|
/* Now replace environment variables */
|
|
|
|
status = ExpandEnvironmentStrings(cmd1, cmd2, sizeof(cmd2));
|
|
|
|
if (!status) {
|
|
|
|
WCMD_print_error ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In a batch program, unknown variables are replace by nothing */
|
|
|
|
/* so remove any remaining %var% */
|
2002-06-01 01:06:46 +02:00
|
|
|
p = cmd2;
|
|
|
|
while ((p = strchr(p, '%'))) {
|
|
|
|
s = strchr(p+1, '%');
|
|
|
|
if (!s) {
|
|
|
|
*p=0x00;
|
|
|
|
} else {
|
|
|
|
t = strdup(s+1);
|
|
|
|
strcpy(p, t);
|
|
|
|
free(t);
|
|
|
|
}
|
|
|
|
}
|
2000-08-01 04:14:33 +02:00
|
|
|
|
|
|
|
/* Show prompt before batch line IF echo is on */
|
2002-06-01 01:06:46 +02:00
|
|
|
if (echo_mode && (line[0] != '@')) {
|
|
|
|
WCMD_show_prompt();
|
2005-12-02 11:25:51 +01:00
|
|
|
WCMD_output_asis ( cmd2);
|
|
|
|
WCMD_output_asis ( "\n");
|
2002-06-01 01:06:46 +02:00
|
|
|
}
|
2000-08-01 04:14:33 +02:00
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
WCMD_process_command (cmd2);
|
1999-06-06 17:24:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************
|
|
|
|
* WCMD_parameter - extract a parameter from a command line.
|
|
|
|
*
|
1999-06-26 12:24:08 +02:00
|
|
|
* Returns the 'n'th space-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
|
|
|
*/
|
|
|
|
|
1999-07-10 13:36:45 +02:00
|
|
|
char *WCMD_parameter (char *s, int n, char **where) {
|
1999-06-06 17:24:04 +02:00
|
|
|
|
1999-06-26 12:24:08 +02:00
|
|
|
int i = 0;
|
1999-06-06 17:24:04 +02:00
|
|
|
static char param[MAX_PATH];
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = param;
|
|
|
|
while (TRUE) {
|
|
|
|
switch (*s) {
|
|
|
|
case ' ':
|
|
|
|
s++;
|
|
|
|
break;
|
|
|
|
case '"':
|
1999-07-10 13:36:45 +02:00
|
|
|
if (where != NULL) *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;
|
1999-06-26 12:24:08 +02:00
|
|
|
case '(':
|
1999-07-10 13:36:45 +02:00
|
|
|
if (where != NULL) *where = s;
|
1999-06-26 12:24:08 +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-26 12:24:08 +02:00
|
|
|
param[0] = '\0';
|
|
|
|
i++;
|
1999-07-10 13:36:45 +02:00
|
|
|
p = param;
|
1999-06-26 12:24:08 +02:00
|
|
|
break;
|
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;
|
1999-06-06 17:24:04 +02:00
|
|
|
while ((*s != '\0') && (*s != ' ')) {
|
|
|
|
*p++ = *s++;
|
|
|
|
}
|
|
|
|
if (i == n) {
|
|
|
|
*p = '\0';
|
|
|
|
return param;
|
|
|
|
}
|
|
|
|
param[0] = '\0';
|
|
|
|
i++;
|
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
|
|
|
*/
|
|
|
|
|
|
|
|
char *WCMD_fgets (char *s, int n, HANDLE h) {
|
|
|
|
|
|
|
|
DWORD bytes;
|
|
|
|
BOOL status;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = s;
|
|
|
|
do {
|
|
|
|
status = 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++;
|
2000-08-02 00:02:18 +02:00
|
|
|
n--;
|
1999-06-26 12:24:08 +02:00
|
|
|
}
|
|
|
|
*s = '\0';
|
1999-06-06 17:24:04 +02:00
|
|
|
} while ((bytes == 1) && (n > 1));
|
|
|
|
return p;
|
|
|
|
}
|
2007-02-20 18:49:43 +01:00
|
|
|
|
|
|
|
/* _splitpath - copied from winefile as no obvious way to use it otherwise */
|
|
|
|
void _splitpath(const CHAR* path, CHAR* drv, CHAR* dir, CHAR* name, CHAR* ext)
|
|
|
|
{
|
|
|
|
const CHAR* end; /* end of processed string */
|
|
|
|
const CHAR* p; /* search pointer */
|
|
|
|
const CHAR* s; /* copy pointer */
|
|
|
|
|
|
|
|
/* 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,
|
|
|
|
* but in a for loop, the for end character may also be a modifier
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
void WCMD_HandleTildaModifiers(char **start, char *forVariable) {
|
|
|
|
|
|
|
|
#define NUMMODIFIERS 11
|
2007-02-23 09:25:00 +01:00
|
|
|
const char validmodifiers[NUMMODIFIERS] = {
|
2007-02-20 18:49:43 +01:00
|
|
|
'~', 'f', 'd', 'p', 'n', 'x', 's', 'a', 't', 'z', '$'
|
|
|
|
};
|
|
|
|
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fileInfo;
|
|
|
|
char outputparam[MAX_PATH];
|
|
|
|
char finaloutput[MAX_PATH];
|
|
|
|
char fullfilename[MAX_PATH];
|
|
|
|
char thisoutput[MAX_PATH];
|
|
|
|
char *pos = *start+1;
|
|
|
|
char *firstModifier = pos;
|
|
|
|
char *lastModifier = NULL;
|
|
|
|
int modifierLen = 0;
|
|
|
|
BOOL finished = FALSE;
|
|
|
|
int i = 0;
|
|
|
|
BOOL exists = TRUE;
|
|
|
|
BOOL skipFileParsing = FALSE;
|
|
|
|
BOOL doneModifier = FALSE;
|
|
|
|
|
|
|
|
/* Search forwards until find invalid character modifier */
|
|
|
|
while (!finished) {
|
|
|
|
|
|
|
|
/* Work on the previous character */
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now make sure the position we stopped at is a valid parameter */
|
|
|
|
if (!(*lastModifier >= '0' || *lastModifier <= '9') &&
|
|
|
|
(forVariable != NULL) &&
|
|
|
|
(toupper(*lastModifier) != toupper(*forVariable))) {
|
|
|
|
|
|
|
|
/* Its not... Step backwards until it matches or we get to the start */
|
|
|
|
while (toupper(*lastModifier) != toupper(*forVariable) &&
|
|
|
|
lastModifier > firstModifier) {
|
|
|
|
lastModifier--;
|
|
|
|
}
|
|
|
|
if (lastModifier == firstModifier) return; /* Invalid syntax */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract the parameter to play with */
|
|
|
|
if ((*lastModifier >= '0' && *lastModifier <= '9')) {
|
|
|
|
strcpy(outputparam, WCMD_parameter (context -> command,
|
|
|
|
*lastModifier-'0' + context -> shift_count, NULL));
|
|
|
|
} else {
|
|
|
|
/* FIXME: Retrieve 'for' variable %c\n", *lastModifier); */
|
|
|
|
/* Need to get 'for' loop variable into outputparam */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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]=='"' &&
|
|
|
|
memchr(firstModifier, '~', modifierLen) != NULL) {
|
|
|
|
int len = strlen(outputparam);
|
|
|
|
if (outputparam[len-1] == '"') {
|
|
|
|
outputparam[len-1]=0x00;
|
|
|
|
len = len - 1;
|
|
|
|
}
|
|
|
|
memmove(outputparam, &outputparam[1], len-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 2. Handle the special case of a $ */
|
|
|
|
if (memchr(firstModifier, '$', modifierLen) != NULL) {
|
|
|
|
/* Special Case: Search envar specified in $[envvar] for outputparam
|
|
|
|
Note both $ and : are guaranteed otherwise check above would fail */
|
|
|
|
char *start = strchr(firstModifier, '$') + 1;
|
|
|
|
char *end = strchr(firstModifier, ':');
|
|
|
|
char env[MAX_PATH];
|
|
|
|
char fullpath[MAX_PATH];
|
|
|
|
|
|
|
|
/* Extract the env var */
|
|
|
|
strncpy(env, start, (end-start));
|
|
|
|
env[(end-start)] = 0x00;
|
|
|
|
|
|
|
|
/* If env var not found, return emptry string */
|
|
|
|
if ((GetEnvironmentVariable(env, fullpath, MAX_PATH) == 0) ||
|
|
|
|
(SearchPath(fullpath, outputparam, NULL,
|
|
|
|
MAX_PATH, outputparam, NULL) == 0)) {
|
|
|
|
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) {
|
|
|
|
if (GetFullPathName(outputparam, MAX_PATH, fullfilename, NULL) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
exists = GetFileAttributesExA(fullfilename, GetFileExInfoStandard,
|
|
|
|
&fileInfo);
|
|
|
|
|
|
|
|
/* 2. Handle 'a' : Output attributes */
|
|
|
|
if (exists &&
|
|
|
|
memchr(firstModifier, 'a', modifierLen) != NULL) {
|
|
|
|
|
|
|
|
doneModifier = TRUE;
|
|
|
|
strcpy(thisoutput, "---------");
|
|
|
|
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';
|
|
|
|
strcat(finaloutput, thisoutput);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 3. Handle 't' : Date+time */
|
|
|
|
if (exists &&
|
|
|
|
memchr(firstModifier, 't', modifierLen) != NULL) {
|
|
|
|
|
|
|
|
SYSTEMTIME systime;
|
|
|
|
int datelen;
|
|
|
|
|
|
|
|
doneModifier = TRUE;
|
|
|
|
if (finaloutput[0] != 0x00) strcat(finaloutput, " ");
|
|
|
|
|
|
|
|
/* Format the time */
|
|
|
|
FileTimeToSystemTime(&fileInfo.ftLastWriteTime, &systime);
|
|
|
|
GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &systime,
|
|
|
|
NULL, thisoutput, MAX_PATH);
|
|
|
|
strcat(thisoutput, " ");
|
|
|
|
datelen = strlen(thisoutput);
|
|
|
|
GetTimeFormat(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &systime,
|
|
|
|
NULL, (thisoutput+datelen), MAX_PATH-datelen);
|
|
|
|
strcat(finaloutput, thisoutput);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 4. Handle 'z' : File length */
|
|
|
|
if (exists &&
|
|
|
|
memchr(firstModifier, 'z', modifierLen) != NULL) {
|
|
|
|
/* FIXME: Output full 64 bit size (sprintf not support I64 here) */
|
|
|
|
ULONG/*64*/ fullsize = /*(fileInfo.nFileSizeHigh << 32) +*/
|
|
|
|
fileInfo.nFileSizeLow;
|
|
|
|
|
|
|
|
doneModifier = TRUE;
|
|
|
|
if (finaloutput[0] != 0x00) strcat(finaloutput, " ");
|
|
|
|
sprintf(thisoutput, "%u", fullsize);
|
|
|
|
strcat(finaloutput, thisoutput);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 4. Handle 's' : Use short paths (File doesnt have to exist) */
|
|
|
|
if (memchr(firstModifier, 's', modifierLen) != NULL) {
|
|
|
|
if (finaloutput[0] != 0x00) strcat(finaloutput, " ");
|
|
|
|
/* Dont flag as doneModifier - %~s on its own is processed later */
|
|
|
|
GetShortPathName(outputparam, outputparam, sizeof(outputparam));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 5. Handle 'f' : Fully qualified path (File doesnt have to exist) */
|
|
|
|
/* Note this overrides d,p,n,x */
|
|
|
|
if (memchr(firstModifier, 'f', modifierLen) != NULL) {
|
|
|
|
doneModifier = TRUE;
|
|
|
|
if (finaloutput[0] != 0x00) strcat(finaloutput, " ");
|
|
|
|
strcat(finaloutput, fullfilename);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
char drive[10];
|
|
|
|
char dir[MAX_PATH];
|
|
|
|
char fname[MAX_PATH];
|
|
|
|
char ext[MAX_PATH];
|
|
|
|
BOOL doneFileModifier = FALSE;
|
|
|
|
|
|
|
|
if (finaloutput[0] != 0x00) strcat(finaloutput, " ");
|
|
|
|
|
|
|
|
/* Split into components */
|
|
|
|
_splitpath(fullfilename, drive, dir, fname, ext);
|
|
|
|
|
|
|
|
/* 5. Handle 'd' : Drive Letter */
|
|
|
|
if (memchr(firstModifier, 'd', modifierLen) != NULL) {
|
|
|
|
strcat(finaloutput, drive);
|
|
|
|
doneModifier = TRUE;
|
|
|
|
doneFileModifier = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 6. Handle 'p' : Path */
|
|
|
|
if (memchr(firstModifier, 'p', modifierLen) != NULL) {
|
|
|
|
strcat(finaloutput, dir);
|
|
|
|
doneModifier = TRUE;
|
|
|
|
doneFileModifier = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 7. Handle 'n' : Name */
|
|
|
|
if (memchr(firstModifier, 'n', modifierLen) != NULL) {
|
|
|
|
strcat(finaloutput, fname);
|
|
|
|
doneModifier = TRUE;
|
|
|
|
doneFileModifier = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 8. Handle 'x' : Ext */
|
|
|
|
if (memchr(firstModifier, 'x', modifierLen) != NULL) {
|
|
|
|
strcat(finaloutput, ext);
|
|
|
|
doneModifier = TRUE;
|
|
|
|
doneFileModifier = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If 's' but no other parameter, dump the whole thing */
|
|
|
|
if (!doneFileModifier &&
|
|
|
|
memchr(firstModifier, 's', modifierLen) != NULL) {
|
|
|
|
doneModifier = TRUE;
|
|
|
|
if (finaloutput[0] != 0x00) strcat(finaloutput, " ");
|
|
|
|
strcat(finaloutput, outputparam);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If No other modifier processed, just add in parameter */
|
|
|
|
if (!doneModifier) strcpy(finaloutput, outputparam);
|
|
|
|
|
|
|
|
/* Finish by inserting the replacement into the string */
|
|
|
|
pos = strdup (lastModifier+1);
|
|
|
|
strcpy(*start, finaloutput);
|
|
|
|
strcat(*start, pos);
|
|
|
|
free(pos);
|
|
|
|
}
|