1999-06-06 17:24:04 +02:00
|
|
|
/*
|
|
|
|
* WCMD - Wine-compatible command line interface - batch interface.
|
|
|
|
*
|
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
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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);
|
1999-06-06 17:24:04 +02:00
|
|
|
|
|
|
|
extern char nyi[];
|
|
|
|
extern char newline[];
|
|
|
|
extern char version_string[];
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
|
|
|
|
|
|
HANDLE h;
|
|
|
|
char string[MAX_PATH];
|
1999-06-26 12:24:08 +02:00
|
|
|
BATCH_CONTEXT *prev_context;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
|
|
|
strcpy (string, file);
|
|
|
|
CharLower (string);
|
|
|
|
if (strstr (string, ".bat") == NULL) strcat (string, ".bat");
|
2001-01-02 21:40:58 +01:00
|
|
|
h = CreateFile (string, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
1999-06-06 17:24:04 +02:00
|
|
|
if (h == INVALID_HANDLE_VALUE) {
|
2000-08-02 00:02:18 +02:00
|
|
|
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;
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (WCMD_fgets (string, sizeof(string), h)) {
|
1999-06-26 12:24:08 +02:00
|
|
|
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;
|
2000-08-01 04:14:33 +02:00
|
|
|
char cmd1[1024],cmd2[1024];
|
1999-06-26 12:24:08 +02:00
|
|
|
char *p, *s, *t;
|
|
|
|
int i;
|
1999-06-06 17:24:04 +02:00
|
|
|
|
2000-08-01 04:14:33 +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 */
|
|
|
|
|
|
|
|
/* Replace use of %0...%9 */
|
|
|
|
p = cmd1;
|
1999-06-26 12:24:08 +02:00
|
|
|
while ((p = strchr(p, '%'))) {
|
|
|
|
i = *(p+1) - '0';
|
|
|
|
if ((i >= 0) && (i <= 9)) {
|
|
|
|
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);
|
2000-08-01 04:14:33 +02:00
|
|
|
} else {
|
|
|
|
p++;
|
|
|
|
}
|
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% */
|
|
|
|
p = cmd2;
|
|
|
|
while ((p = strchr(p, '%'))) {
|
|
|
|
s = strchr(p+1, '%');
|
|
|
|
if (!s) {
|
|
|
|
*p=0x00;
|
|
|
|
} else {
|
|
|
|
t = strdup(s+1);
|
|
|
|
strcpy(p, t);
|
|
|
|
free(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Show prompt before batch line IF echo is on */
|
|
|
|
if (echo_mode && (line[0] != '@')) {
|
|
|
|
WCMD_show_prompt();
|
|
|
|
WCMD_output ("%s\n", cmd2);
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
1999-07-10 13:36:45 +02:00
|
|
|
if (where != NULL) *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;
|
|
|
|
}
|