wine/programs/cmd/builtins.c

2354 lines
67 KiB
C
Raw Normal View History

1999-06-06 15:24:04 +00:00
/*
2006-09-04 04:13:08 +00:00
* CMD - Wine-compatible command line interface - built-in functions.
1999-06-06 15:24:04 +00:00
*
* Copyright (C) 1999 D A Pickles
1999-06-06 15:24:04 +00: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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/*
* NOTES:
1999-06-06 15:24:04 +00:00
* On entry to each function, global variables quals, param1, param2 contain
* the qualifiers (uppercased and concatenated) and parameters entered, with
* environment-variable and batch parameter substitution already done.
*/
/*
* FIXME:
* - No support for pipes, shell parameters
1999-06-06 15:24:04 +00:00
* - Lots of functionality missing from builtins
* - Messages etc need international support
*/
#define WIN32_LEAN_AND_MEAN
1999-06-06 15:24:04 +00:00
#include "wcmd.h"
2007-02-26 23:04:40 +00:00
#include <shellapi.h>
2007-03-04 22:33:27 +00:00
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(cmd);
1999-06-06 15:24:04 +00:00
2007-06-03 21:07:42 +00:00
void WCMD_execute (WCHAR *orig_command, WCHAR *parameter, WCHAR *substitution);
2004-03-22 22:56:58 +00:00
struct env_stack *saved_environment;
2007-02-23 22:17:28 +00:00
struct env_stack *pushd_directories;
2004-03-22 22:56:58 +00:00
1999-06-06 15:24:04 +00:00
extern HINSTANCE hinst;
extern WCHAR inbuilt[][10];
2007-03-08 00:37:44 +00:00
extern int echo_mode, verify_mode, defaultColor;
2007-06-03 21:07:42 +00:00
extern WCHAR quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
extern BATCH_CONTEXT *context;
extern DWORD errorlevel;
1999-06-06 15:24:04 +00:00
2007-06-03 21:07:42 +00:00
static const WCHAR dotW[] = {'.','\0'};
static const WCHAR dotdotW[] = {'.','.','\0'};
static const WCHAR slashW[] = {'\\','\0'};
static const WCHAR starW[] = {'*','\0'};
static const WCHAR equalW[] = {'=','\0'};
static const WCHAR fslashW[] = {'/','\0'};
static const WCHAR onW[] = {'O','N','\0'};
static const WCHAR offW[] = {'O','F','F','\0'};
static const WCHAR parmY[] = {'/','Y','\0'};
static const WCHAR parmNoY[] = {'/','-','Y','\0'};
static const WCHAR nullW[] = {'\0'};
1999-06-06 15:24:04 +00:00
/****************************************************************************
* WCMD_clear_screen
*
* Clear the terminal screen.
*/
void WCMD_clear_screen (void) {
1999-06-06 15:24:04 +00:00
2002-04-29 17:12:57 +00:00
/* Emulate by filling the screen from the top left to bottom right with
spaces, then moving the cursor to the top left afterwards */
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
1999-06-06 15:24:04 +00:00
if (GetConsoleScreenBufferInfo(hStdOut, &consoleInfo))
{
COORD topLeft;
2005-08-19 10:04:03 +00:00
DWORD screenSize;
screenSize = consoleInfo.dwSize.X * (consoleInfo.dwSize.Y + 1);
2002-04-29 17:12:57 +00:00
topLeft.X = 0;
topLeft.Y = 0;
FillConsoleOutputCharacter(hStdOut, ' ', screenSize, topLeft, &screenSize);
SetConsoleCursorPosition(hStdOut, topLeft);
}
1999-06-06 15:24:04 +00:00
}
/****************************************************************************
* WCMD_change_tty
*
* Change the default i/o device (ie redirect STDin/STDout).
*/
void WCMD_change_tty (void) {
1999-06-06 15:24:04 +00:00
WCMD_output (WCMD_LoadMessage(WCMD_NYI));
1999-06-06 15:24:04 +00:00
}
/****************************************************************************
* WCMD_copy
*
* Copy a file or wildcarded set.
* FIXME: No wildcard support
*/
void WCMD_copy (void) {
1999-06-06 15:24:04 +00:00
WIN32_FIND_DATA fd;
HANDLE hff;
BOOL force, status;
2007-06-03 21:07:42 +00:00
WCHAR outpath[MAX_PATH], inpath[MAX_PATH], *infile, copycmd[3];
DWORD len;
2007-06-03 21:07:42 +00:00
static const WCHAR copyCmdW[] = {'C','O','P','Y','C','M','D','\0'};
1999-06-06 15:24:04 +00:00
if (param1[0] == 0x00) {
WCMD_output (WCMD_LoadMessage(WCMD_NOARG));
return;
}
2007-06-03 21:07:42 +00:00
if ((strchrW(param1,'*') != NULL) && (strchrW(param1,'%') != NULL)) {
WCMD_output (WCMD_LoadMessage(WCMD_NYI));
1999-06-06 15:24:04 +00:00
return;
}
/* If no destination supplied, assume current directory */
if (param2[0] == 0x00) {
2007-06-03 21:07:42 +00:00
strcpyW(param2, dotW);
}
2007-06-03 21:07:42 +00:00
GetFullPathName (param2, sizeof(outpath)/sizeof(WCHAR), outpath, NULL);
if (outpath[strlenW(outpath) - 1] == '\\')
outpath[strlenW(outpath) - 1] = '\0';
hff = FindFirstFile (outpath, &fd);
if (hff != INVALID_HANDLE_VALUE) {
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
2007-06-03 21:07:42 +00:00
GetFullPathName (param1, sizeof(inpath)/sizeof(WCHAR), inpath, &infile);
strcatW (outpath, slashW);
strcatW (outpath, infile);
}
FindClose (hff);
}
/* /-Y has the highest priority, then /Y and finally the COPYCMD env. variable */
2007-06-03 21:07:42 +00:00
if (strstrW (quals, parmNoY))
force = FALSE;
2007-06-03 21:07:42 +00:00
else if (strstrW (quals, parmY))
force = TRUE;
else {
2007-06-03 21:07:42 +00:00
len = GetEnvironmentVariable (copyCmdW, copycmd, sizeof(copycmd)/sizeof(WCHAR));
force = (len && len < (sizeof(copycmd)/sizeof(WCHAR)) && ! lstrcmpiW (copycmd, parmY));
}
1999-06-06 15:24:04 +00:00
if (!force) {
hff = FindFirstFile (outpath, &fd);
if (hff != INVALID_HANDLE_VALUE) {
2007-06-03 21:07:42 +00:00
WCHAR buffer[MAXSTRING];
1999-06-06 15:24:04 +00:00
FindClose (hff);
2007-06-03 21:07:42 +00:00
wsprintf(buffer, WCMD_LoadMessage(WCMD_OVERWRITE), outpath);
force = WCMD_ask_confirm(buffer, FALSE, NULL);
1999-06-06 15:24:04 +00:00
}
else force = TRUE;
}
if (force) {
status = CopyFile (param1, outpath, FALSE);
if (!status) WCMD_print_error ();
}
}
/****************************************************************************
* WCMD_create_dir
*
* Create a directory.
*
* this works recursivly. so mkdir dir1\dir2\dir3 will create dir1 and dir2 if
* they do not already exist.
1999-06-06 15:24:04 +00:00
*/
2007-06-03 21:07:42 +00:00
static BOOL create_full_path(WCHAR* path)
{
int len;
2007-06-03 21:07:42 +00:00
WCHAR *new_path;
BOOL ret = TRUE;
2007-06-03 21:07:42 +00:00
new_path = HeapAlloc(GetProcessHeap(),0,(strlenW(path) * sizeof(WCHAR))+1);
strcpyW(new_path,path);
2007-06-03 21:07:42 +00:00
while ((len = strlenW(new_path)) && new_path[len - 1] == '\\')
new_path[len - 1] = 0;
while (!CreateDirectory(new_path,NULL))
{
2007-06-03 21:07:42 +00:00
WCHAR *slash;
DWORD last_error = GetLastError();
if (last_error == ERROR_ALREADY_EXISTS)
break;
if (last_error != ERROR_PATH_NOT_FOUND)
{
ret = FALSE;
break;
}
2007-06-03 21:07:42 +00:00
if (!(slash = strrchrW(new_path,'\\')) && ! (slash = strrchrW(new_path,'/')))
{
ret = FALSE;
break;
}
len = slash - new_path;
new_path[len] = 0;
if (!create_full_path(new_path))
{
ret = FALSE;
break;
}
new_path[len] = '\\';
}
HeapFree(GetProcessHeap(),0,new_path);
return ret;
}
void WCMD_create_dir (void) {
1999-06-06 15:24:04 +00:00
if (param1[0] == 0x00) {
WCMD_output (WCMD_LoadMessage(WCMD_NOARG));
return;
}
if (!create_full_path(param1)) WCMD_print_error ();
1999-06-06 15:24:04 +00:00
}
/****************************************************************************
* WCMD_delete
*
* Delete a file or wildcarded set.
*
2007-02-27 23:20:55 +00:00
* Note on /A:
* - Testing shows /A is repeatable, eg. /a-r /ar matches all files
* - Each set is a pattern, eg /ahr /as-r means
* readonly+hidden OR nonreadonly system files
* - The '-' applies to a single field, ie /a:-hr means read only
* non-hidden files
1999-06-06 15:24:04 +00:00
*/
2007-06-03 21:07:42 +00:00
BOOL WCMD_delete (WCHAR *command, BOOL expectDir) {
1999-06-06 15:24:04 +00:00
int argno = 0;
int argsProcessed = 0;
2007-06-03 21:07:42 +00:00
WCHAR *argN = command;
2007-04-23 22:24:38 +00:00
BOOL foundAny = FALSE;
2007-06-03 21:07:42 +00:00
static const WCHAR parmA[] = {'/','A','\0'};
static const WCHAR parmQ[] = {'/','Q','\0'};
static const WCHAR parmP[] = {'/','P','\0'};
static const WCHAR parmS[] = {'/','S','\0'};
static const WCHAR parmF[] = {'/','F','\0'};
2007-04-23 22:24:38 +00:00
/* If not recursing, clear error flag */
if (expectDir) errorlevel = 0;
1999-06-06 15:24:04 +00:00
/* Loop through all args */
while (argN) {
2007-06-03 21:07:42 +00:00
WCHAR *thisArg = WCMD_parameter (command, argno++, &argN);
WCHAR argCopy[MAX_PATH];
2007-04-23 22:24:38 +00:00
if (argN && argN[0] != '/') {
WIN32_FIND_DATA fd;
HANDLE hff;
2007-06-03 21:07:42 +00:00
WCHAR fpath[MAX_PATH];
WCHAR *p;
2007-04-23 22:24:38 +00:00
BOOL handleParm = TRUE;
BOOL found = FALSE;
2007-06-03 21:07:42 +00:00
static const WCHAR anyExt[]= {'.','*','\0'};
2007-06-03 21:07:42 +00:00
strcpyW(argCopy, thisArg);
WINE_TRACE("del: Processing arg %s (quals:%s)\n",
wine_dbgstr_w(argCopy), wine_dbgstr_w(quals));
argsProcessed++;
/* If filename part of parameter is * or *.*, prompt unless
/Q supplied. */
2007-06-03 21:07:42 +00:00
if ((strstrW (quals, parmQ) == NULL) && (strstrW (quals, parmP) == NULL)) {
2007-06-03 21:07:42 +00:00
WCHAR drive[10];
WCHAR dir[MAX_PATH];
WCHAR fname[MAX_PATH];
WCHAR ext[MAX_PATH];
/* Convert path into actual directory spec */
2007-06-03 21:07:42 +00:00
GetFullPathName (argCopy, sizeof(fpath)/sizeof(WCHAR), fpath, NULL);
WCMD_splitpath(fpath, drive, dir, fname, ext);
/* Only prompt for * and *.*, not *a, a*, *.a* etc */
2007-06-03 21:07:42 +00:00
if ((strcmpW(fname, starW) == 0) &&
(*ext == 0x00 || (strcmpW(ext, anyExt) == 0))) {
BOOL ok;
2007-06-03 21:07:42 +00:00
WCHAR question[MAXSTRING];
static const WCHAR fmt[] = {'%','s',' ','\0'};
2007-02-27 23:20:25 +00:00
2007-04-23 22:24:38 +00:00
/* Note: Flag as found, to avoid file not found message */
found = TRUE;
/* Ask for confirmation */
2007-06-03 21:07:42 +00:00
wsprintf(question, fmt, fpath);
ok = WCMD_ask_confirm(question, TRUE, NULL);
2007-02-27 23:20:55 +00:00
/* Abort if answer is 'N' */
if (!ok) continue;
}
}
2007-02-27 23:20:55 +00:00
2007-04-23 22:24:38 +00:00
/* First, try to delete in the current directory */
hff = FindFirstFile (argCopy, &fd);
if (hff == INVALID_HANDLE_VALUE) {
2007-04-23 22:24:38 +00:00
handleParm = FALSE;
} else {
found = TRUE;
}
2007-04-23 22:24:38 +00:00
/* Support del <dirname> by just deleting all files dirname\* */
2007-06-03 21:07:42 +00:00
if (handleParm && (strchrW(argCopy,'*') == NULL) && (strchrW(argCopy,'?') == NULL)
&& (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
2007-06-03 21:07:42 +00:00
WCHAR modifiedParm[MAX_PATH];
static const WCHAR slashStar[] = {'\\','*','\0'};
strcpyW(modifiedParm, argCopy);
strcatW(modifiedParm, slashStar);
FindClose(hff);
2007-04-23 22:24:38 +00:00
found = TRUE;
WCMD_delete(modifiedParm, FALSE);
2007-04-23 22:24:38 +00:00
} else if (handleParm) {
/* Build the filename to delete as <supplied directory>\<findfirst filename> */
2007-06-03 21:07:42 +00:00
strcpyW (fpath, argCopy);
do {
2007-06-03 21:07:42 +00:00
p = strrchrW (fpath, '\\');
if (p != NULL) {
*++p = '\0';
2007-06-03 21:07:42 +00:00
strcatW (fpath, fd.cFileName);
2007-02-27 23:20:55 +00:00
}
2007-06-03 21:07:42 +00:00
else strcpyW (fpath, fd.cFileName);
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
BOOL ok = TRUE;
2007-06-03 21:07:42 +00:00
WCHAR *nextA = strstrW (quals, parmA);
/* Handle attribute matching (/A) */
if (nextA != NULL) {
ok = FALSE;
while (nextA != NULL && !ok) {
2007-06-03 21:07:42 +00:00
WCHAR *thisA = (nextA+2);
BOOL stillOK = TRUE;
/* Skip optional : */
if (*thisA == ':') thisA++;
/* Parse each of the /A[:]xxx in turn */
while (*thisA && *thisA != '/') {
BOOL negate = FALSE;
BOOL attribute = FALSE;
/* Match negation of attribute first */
if (*thisA == '-') {
negate=TRUE;
thisA++;
}
/* Match attribute */
switch (*thisA) {
case 'R': attribute = (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY);
break;
case 'H': attribute = (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN);
break;
case 'S': attribute = (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM);
break;
case 'A': attribute = (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE);
break;
default:
WCMD_output (WCMD_LoadMessage(WCMD_SYNTAXERR));
}
/* Now check result, keeping a running boolean about whether it
matches all parsed attribues so far */
if (attribute && !negate) {
stillOK = stillOK;
} else if (!attribute && negate) {
stillOK = stillOK;
} else {
stillOK = FALSE;
}
thisA++;
}
/* Save the running total as the final result */
ok = stillOK;
/* Step on to next /A set */
2007-06-03 21:07:42 +00:00
nextA = strstrW (nextA+1, parmA);
}
}
2007-02-27 23:20:55 +00:00
/* /P means prompt for each file */
2007-06-03 21:07:42 +00:00
if (ok && strstrW (quals, parmP) != NULL) {
WCHAR question[MAXSTRING];
2007-02-27 23:20:55 +00:00
/* Ask for confirmation */
2007-06-03 21:07:42 +00:00
wsprintf(question, WCMD_LoadMessage(WCMD_DELPROMPT), fpath);
ok = WCMD_ask_confirm(question, FALSE, NULL);
}
2007-02-27 23:20:05 +00:00
/* Only proceed if ok to */
if (ok) {
2007-02-27 23:20:05 +00:00
/* If file is read only, and /F supplied, delete it */
if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY &&
2007-06-03 21:07:42 +00:00
strstrW (quals, parmF) != NULL) {
SetFileAttributes(fpath, fd.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY);
}
2007-02-27 23:20:25 +00:00
/* Now do the delete */
if (!DeleteFile (fpath)) WCMD_print_error ();
}
2007-02-27 23:20:25 +00:00
}
} while (FindNextFile(hff, &fd) != 0);
FindClose (hff);
2007-02-27 23:20:05 +00:00
}
2007-04-23 22:24:38 +00:00
2007-04-30 00:06:11 +00:00
/* Now recurse into all subdirectories handling the parameter in the same way */
2007-06-03 21:07:42 +00:00
if (strstrW (quals, parmS) != NULL) {
2007-04-23 22:24:38 +00:00
2007-06-03 21:07:42 +00:00
WCHAR thisDir[MAX_PATH];
2007-04-23 22:24:38 +00:00
int cPos;
2007-06-03 21:07:42 +00:00
WCHAR drive[10];
WCHAR dir[MAX_PATH];
WCHAR fname[MAX_PATH];
WCHAR ext[MAX_PATH];
2007-04-23 22:24:38 +00:00
/* Convert path into actual directory spec */
2007-06-03 21:07:42 +00:00
GetFullPathName (argCopy, sizeof(thisDir)/sizeof(WCHAR), thisDir, NULL);
2007-04-23 22:24:38 +00:00
WCMD_splitpath(thisDir, drive, dir, fname, ext);
2007-06-03 21:07:42 +00:00
strcpyW(thisDir, drive);
strcatW(thisDir, dir);
cPos = strlenW(thisDir);
2007-04-23 22:24:38 +00:00
2007-06-03 21:07:42 +00:00
WINE_TRACE("Searching recursively in '%s'\n", wine_dbgstr_w(thisDir));
2007-04-23 22:24:38 +00:00
/* Append '*' to the directory */
thisDir[cPos] = '*';
thisDir[cPos+1] = 0x00;
hff = FindFirstFile (thisDir, &fd);
/* Remove residual '*' */
thisDir[cPos] = 0x00;
if (hff != INVALID_HANDLE_VALUE) {
DIRECTORY_STACK *allDirs = NULL;
DIRECTORY_STACK *lastEntry = NULL;
do {
if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
2007-06-03 21:07:42 +00:00
(strcmpW(fd.cFileName, dotdotW) != 0) &&
(strcmpW(fd.cFileName, dotW) != 0)) {
2007-04-23 22:24:38 +00:00
DIRECTORY_STACK *nextDir;
2007-06-03 21:07:42 +00:00
WCHAR subParm[MAX_PATH];
2007-04-23 22:24:38 +00:00
/* Work out search parameter in sub dir */
2007-06-03 21:07:42 +00:00
strcpyW (subParm, thisDir);
strcatW (subParm, fd.cFileName);
strcatW (subParm, slashW);
strcatW (subParm, fname);
strcatW (subParm, ext);
WINE_TRACE("Recursive, Adding to search list '%s'\n", wine_dbgstr_w(subParm));
2007-04-23 22:24:38 +00:00
/* Allocate memory, add to list */
nextDir = (DIRECTORY_STACK *) HeapAlloc(GetProcessHeap(),0,sizeof(DIRECTORY_STACK));
if (allDirs == NULL) allDirs = nextDir;
if (lastEntry != NULL) lastEntry->next = nextDir;
lastEntry = nextDir;
nextDir->next = NULL;
2007-06-03 21:07:42 +00:00
nextDir->dirName = HeapAlloc(GetProcessHeap(),0,
(strlenW(subParm)+1) * sizeof(WCHAR));
strcpyW(nextDir->dirName, subParm);
2007-04-23 22:24:38 +00:00
}
} while (FindNextFile(hff, &fd) != 0);
FindClose (hff);
/* Go through each subdir doing the delete */
while (allDirs != NULL) {
DIRECTORY_STACK *tempDir;
tempDir = allDirs->next;
found |= WCMD_delete (allDirs->dirName, FALSE);
HeapFree(GetProcessHeap(),0,allDirs->dirName);
HeapFree(GetProcessHeap(),0,allDirs);
allDirs = tempDir;
}
}
}
/* Keep running total to see if any found, and if not recursing
issue error message */
if (expectDir) {
if (!found) {
errorlevel = 1;
WCMD_output (WCMD_LoadMessage(WCMD_FILENOTFOUND), argCopy);
2007-04-23 22:24:38 +00:00
}
}
foundAny |= found;
1999-06-06 15:24:04 +00:00
}
}
/* Handle no valid args */
if (argsProcessed == 0) {
WCMD_output (WCMD_LoadMessage(WCMD_NOARG));
}
2007-04-23 22:24:38 +00:00
return foundAny;
1999-06-06 15:24:04 +00:00
}
/****************************************************************************
* WCMD_echo
*
* Echo input to the screen (or not). We don't try to emulate the bugs
* in DOS (try typing "ECHO ON AGAIN" for an example).
*/
2007-06-03 21:07:42 +00:00
void WCMD_echo (const WCHAR *command) {
1999-06-06 15:24:04 +00:00
int count;
1999-06-06 15:24:04 +00:00
2004-03-18 04:01:32 +00:00
if ((command[0] == '.') && (command[1] == 0)) {
WCMD_output (newline);
return;
}
if (command[0]==' ')
command++;
2007-06-03 21:07:42 +00:00
count = strlenW(command);
1999-06-06 15:24:04 +00:00
if (count == 0) {
2007-06-03 21:07:42 +00:00
if (echo_mode) WCMD_output (WCMD_LoadMessage(WCMD_ECHOPROMPT), onW);
else WCMD_output (WCMD_LoadMessage(WCMD_ECHOPROMPT), offW);
1999-06-06 15:24:04 +00:00
return;
}
2007-06-03 21:07:42 +00:00
if (lstrcmpiW(command, onW) == 0) {
1999-06-06 15:24:04 +00:00
echo_mode = 1;
return;
}
2007-06-03 21:07:42 +00:00
if (lstrcmpiW(command, offW) == 0) {
1999-06-06 15:24:04 +00:00
echo_mode = 0;
return;
}
2002-05-31 23:06:46 +00:00
WCMD_output_asis (command);
1999-06-06 15:24:04 +00:00
WCMD_output (newline);
}
/**************************************************************************
1999-06-06 15:24:04 +00:00
* WCMD_for
*
* Batch file loop processing.
* FIXME: We don't exhaustively check syntax. Any command which works in MessDOS
* will probably work here, but the reverse is not necessarily the case...
1999-06-06 15:24:04 +00:00
*/
2007-06-03 21:07:42 +00:00
void WCMD_for (WCHAR *p) {
1999-06-06 15:24:04 +00:00
WIN32_FIND_DATA fd;
HANDLE hff;
2007-06-03 21:07:42 +00:00
WCHAR *cmd, *item;
WCHAR set[MAX_PATH], param[MAX_PATH];
int i;
2007-06-03 21:07:42 +00:00
const WCHAR inW[] = {'i', 'n', '\0'};
const WCHAR doW[] = {'d', 'o', '\0'};
2007-06-03 21:07:42 +00:00
if (lstrcmpiW (WCMD_parameter (p, 1, NULL), inW)
|| lstrcmpiW (WCMD_parameter (p, 3, NULL), doW)
|| (param1[0] != '%')) {
WCMD_output (WCMD_LoadMessage(WCMD_SYNTAXERR));
return;
}
2007-06-03 21:07:42 +00:00
lstrcpynW (set, WCMD_parameter (p, 2, NULL), sizeof(set)/sizeof(WCHAR));
WCMD_parameter (p, 4, &cmd);
2007-06-03 21:07:42 +00:00
strcpyW (param, param1);
/*
* If the parameter within the set has a wildcard then search for matching files
* otherwise do a literal substitution.
*/
i = 0;
while (*(item = WCMD_parameter (set, i, NULL))) {
2007-06-03 21:07:42 +00:00
static const WCHAR wildcards[] = {'*','?','\0'};
if (strpbrkW (item, wildcards)) {
hff = FindFirstFile (item, &fd);
if (hff == INVALID_HANDLE_VALUE) {
return;
}
do {
WCMD_execute (cmd, param, fd.cFileName);
} while (FindNextFile(hff, &fd) != 0);
FindClose (hff);
}
else {
WCMD_execute (cmd, param, item);
}
i++;
}
}
/*****************************************************************************
* WCMD_Execute
*
* Execute a command after substituting variable text for the supplied parameter
*/
2007-06-03 21:07:42 +00:00
void WCMD_execute (WCHAR *orig_cmd, WCHAR *param, WCHAR *subst) {
1999-06-06 15:24:04 +00:00
2007-06-03 21:07:42 +00:00
WCHAR *new_cmd, *p, *s, *dup;
int size;
2007-06-03 21:07:42 +00:00
size = strlenW (orig_cmd);
new_cmd = (WCHAR *) LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, size);
dup = s = WCMD_strdupW(orig_cmd);
2007-06-03 21:07:42 +00:00
while ((p = strstrW (s, param))) {
*p = '\0';
2007-06-03 21:07:42 +00:00
size += strlenW (subst);
new_cmd = (WCHAR *) LocalReAlloc ((HANDLE)new_cmd, size, 0);
strcatW (new_cmd, s);
strcatW (new_cmd, subst);
s = p + strlenW (param);
}
2007-06-03 21:07:42 +00:00
strcatW (new_cmd, s);
WCMD_process_command (new_cmd);
free (dup);
LocalFree ((HANDLE)new_cmd);
1999-06-06 15:24:04 +00:00
}
1999-06-06 15:24:04 +00:00
/**************************************************************************
* WCMD_give_help
*
* Simple on-line help. Help text is stored in the resource file.
*/
2007-06-03 21:07:42 +00:00
void WCMD_give_help (WCHAR *command) {
1999-06-06 15:24:04 +00:00
int i;
1999-06-06 15:24:04 +00:00
command = WCMD_strtrim_leading_spaces(command);
2007-06-03 21:07:42 +00:00
if (strlenW(command) == 0) {
WCMD_output_asis (WCMD_LoadMessage(WCMD_ALLHELP));
1999-06-06 15:24:04 +00:00
}
else {
for (i=0; i<=WCMD_EXIT; i++) {
if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
param1, -1, inbuilt[i], -1) == 2) {
WCMD_output_asis (WCMD_LoadMessage(i));
return;
1999-06-06 15:24:04 +00:00
}
}
WCMD_output (WCMD_LoadMessage(WCMD_NOCMDHELP), param1);
1999-06-06 15:24:04 +00:00
}
return;
}
/****************************************************************************
* WCMD_go_to
*
* Batch file jump instruction. Not the most efficient algorithm ;-)
* Prints error message if the specified label cannot be found - the file pointer is
* then at EOF, effectively stopping the batch file.
* FIXME: DOS is supposed to allow labels with spaces - we don't.
*/
void WCMD_goto (void) {
2007-06-03 21:07:42 +00:00
WCHAR string[MAX_PATH];
if (param1[0] == 0x00) {
WCMD_output (WCMD_LoadMessage(WCMD_NOARG));
return;
}
if (context != NULL) {
2007-06-03 21:07:42 +00:00
WCHAR *paramStart = param1;
static const WCHAR eofW[] = {':','e','o','f','\0'};
2007-02-20 18:00:37 +00:00
/* Handle special :EOF label */
2007-06-03 21:07:42 +00:00
if (lstrcmpiW (eofW, param1) == 0) {
2007-02-20 18:00:37 +00:00
context -> skip_rest = TRUE;
return;
}
/* Support goto :label as well as goto label */
if (*paramStart == ':') paramStart++;
SetFilePointer (context -> h, 0, NULL, FILE_BEGIN);
2007-06-03 21:07:42 +00:00
while (WCMD_fgets (string, sizeof(string)/sizeof(WCHAR), context -> h)) {
if ((string[0] == ':') && (lstrcmpiW (&string[1], paramStart) == 0)) return;
}
WCMD_output (WCMD_LoadMessage(WCMD_NOTARGET));
}
return;
}
2007-02-23 22:17:28 +00:00
/*****************************************************************************
* WCMD_pushd
*
* Push a directory onto the stack
*/
2007-06-03 21:07:42 +00:00
void WCMD_pushd (WCHAR *command) {
2007-02-23 22:17:28 +00:00
struct env_stack *curdir;
WCHAR *thisdir;
2007-06-03 21:07:42 +00:00
static const WCHAR parmD[] = {'/','D','\0'};
2007-02-23 22:17:28 +00:00
2007-06-03 21:07:42 +00:00
if (strchrW(command, '/') != NULL) {
2007-03-08 00:44:46 +00:00
SetLastError(ERROR_INVALID_PARAMETER);
WCMD_print_error();
return;
}
2007-02-23 22:17:28 +00:00
curdir = LocalAlloc (LMEM_FIXED, sizeof (struct env_stack));
thisdir = LocalAlloc (LMEM_FIXED, 1024 * sizeof(WCHAR));
if( !curdir || !thisdir ) {
LocalFree(curdir);
LocalFree(thisdir);
WINE_ERR ("out of memory\n");
2007-02-23 22:17:28 +00:00
return;
}
2007-03-08 00:50:14 +00:00
/* Change directory using CD code with /D parameter */
2007-06-03 21:07:42 +00:00
strcpyW(quals, parmD);
2007-02-23 22:17:28 +00:00
GetCurrentDirectoryW (1024, thisdir);
2007-03-08 00:44:09 +00:00
errorlevel = 0;
WCMD_setshow_default(command);
if (errorlevel) {
2007-02-23 22:17:28 +00:00
LocalFree(curdir);
LocalFree(thisdir);
return;
} else {
curdir -> next = pushd_directories;
curdir -> strings = thisdir;
if (pushd_directories == NULL) {
curdir -> u.stackdepth = 1;
} else {
curdir -> u.stackdepth = pushd_directories -> u.stackdepth + 1;
}
2007-02-23 22:17:28 +00:00
pushd_directories = curdir;
}
}
/*****************************************************************************
* WCMD_popd
*
* Pop a directory from the stack
*/
void WCMD_popd (void) {
struct env_stack *temp = pushd_directories;
if (!pushd_directories)
return;
/* pop the old environment from the stack, and make it the current dir */
pushd_directories = temp->next;
SetCurrentDirectoryW(temp->strings);
LocalFree (temp->strings);
LocalFree (temp);
}
1999-06-06 15:24:04 +00:00
/****************************************************************************
* WCMD_if
*
* Batch file conditional.
* FIXME: Much more syntax checking needed!
1999-06-06 15:24:04 +00:00
*/
2007-06-03 21:07:42 +00:00
void WCMD_if (WCHAR *p) {
1999-06-06 15:24:04 +00:00
int negate = 0, test = 0;
2007-06-03 21:07:42 +00:00
WCHAR condition[MAX_PATH], *command, *s;
static const WCHAR notW[] = {'n','o','t','\0'};
static const WCHAR errlvlW[] = {'e','r','r','o','r','l','e','v','e','l','\0'};
static const WCHAR existW[] = {'e','x','i','s','t','\0'};
static const WCHAR defdW[] = {'d','e','f','i','n','e','d','\0'};
static const WCHAR eqeqW[] = {'=','=','\0'};
if (!lstrcmpiW (param1, notW)) {
negate = 1;
2007-06-03 21:07:42 +00:00
strcpyW (condition, param2);
}
else {
2007-06-03 21:07:42 +00:00
strcpyW (condition, param1);
}
2007-06-03 21:07:42 +00:00
if (!lstrcmpiW (condition, errlvlW)) {
if (errorlevel >= atoiW(WCMD_parameter (p, 1+negate, NULL))) test = 1;
WCMD_parameter (p, 2+negate, &command);
}
2007-06-03 21:07:42 +00:00
else if (!lstrcmpiW (condition, existW)) {
if (GetFileAttributes(WCMD_parameter (p, 1+negate, NULL)) != INVALID_FILE_ATTRIBUTES) {
test = 1;
}
WCMD_parameter (p, 2+negate, &command);
}
2007-06-03 21:07:42 +00:00
else if (!lstrcmpiW (condition, defdW)) {
if (GetEnvironmentVariable(WCMD_parameter (p, 1+negate, NULL), NULL, 0) > 0) {
2007-02-20 17:47:35 +00:00
test = 1;
}
WCMD_parameter (p, 2+negate, &command);
}
2007-06-03 21:07:42 +00:00
else if ((s = strstrW (p, eqeqW))) {
s += 2;
2007-06-03 21:07:42 +00:00
if (!lstrcmpiW (condition, WCMD_parameter (s, 0, NULL))) test = 1;
WCMD_parameter (s, 1, &command);
}
else {
WCMD_output (WCMD_LoadMessage(WCMD_SYNTAXERR));
return;
}
if (test != negate) {
2007-06-03 21:07:42 +00:00
command = WCMD_strdupW(command);
WCMD_process_command (command);
free (command);
}
1999-06-06 15:24:04 +00:00
}
/****************************************************************************
* WCMD_move
*
* Move a file, directory tree or wildcarded set of files.
*/
void WCMD_move (void) {
1999-06-06 15:24:04 +00:00
int status;
WIN32_FIND_DATA fd;
HANDLE hff;
2007-06-03 21:07:42 +00:00
WCHAR input[MAX_PATH];
WCHAR output[MAX_PATH];
WCHAR drive[10];
WCHAR dir[MAX_PATH];
WCHAR fname[MAX_PATH];
WCHAR ext[MAX_PATH];
if (param1[0] == 0x00) {
WCMD_output (WCMD_LoadMessage(WCMD_NOARG));
return;
}
/* If no destination supplied, assume current directory */
if (param2[0] == 0x00) {
2007-06-03 21:07:42 +00:00
strcpyW(param2, dotW);
}
/* If 2nd parm is directory, then use original filename */
/* Convert partial path to full path */
2007-06-03 21:07:42 +00:00
GetFullPathName (param1, sizeof(input)/sizeof(WCHAR), input, NULL);
GetFullPathName (param2, sizeof(output)/sizeof(WCHAR), output, NULL);
WINE_TRACE("Move from '%s'('%s') to '%s'\n", wine_dbgstr_w(input),
wine_dbgstr_w(param1), wine_dbgstr_w(output));
/* Split into components */
WCMD_splitpath(input, drive, dir, fname, ext);
hff = FindFirstFile (input, &fd);
while (hff != INVALID_HANDLE_VALUE) {
2007-06-03 21:07:42 +00:00
WCHAR dest[MAX_PATH];
WCHAR src[MAX_PATH];
DWORD attribs;
2007-06-03 21:07:42 +00:00
WINE_TRACE("Processing file '%s'\n", wine_dbgstr_w(fd.cFileName));
/* Build src & dest name */
2007-06-03 21:07:42 +00:00
strcpyW(src, drive);
strcatW(src, dir);
/* See if dest is an existing directory */
attribs = GetFileAttributes(output);
if (attribs != INVALID_FILE_ATTRIBUTES &&
(attribs & FILE_ATTRIBUTE_DIRECTORY)) {
2007-06-03 21:07:42 +00:00
strcpyW(dest, output);
strcatW(dest, slashW);
strcatW(dest, fd.cFileName);
} else {
2007-06-03 21:07:42 +00:00
strcpyW(dest, output);
}
2007-06-03 21:07:42 +00:00
strcatW(src, fd.cFileName);
2007-06-03 21:07:42 +00:00
WINE_TRACE("Source '%s'\n", wine_dbgstr_w(src));
WINE_TRACE("Dest '%s'\n", wine_dbgstr_w(dest));
/* Check if file is read only, otherwise move it */
2007-06-03 21:07:42 +00:00
attribs = GetFileAttributes(src);
if ((attribs != INVALID_FILE_ATTRIBUTES) &&
(attribs & FILE_ATTRIBUTE_READONLY)) {
SetLastError(ERROR_ACCESS_DENIED);
status = 0;
} else {
BOOL ok = TRUE;
/* If destination exists, prompt unless /Y supplied */
2007-06-03 21:07:42 +00:00
if (GetFileAttributes(dest) != INVALID_FILE_ATTRIBUTES) {
BOOL force = FALSE;
2007-06-03 21:07:42 +00:00
WCHAR copycmd[MAXSTRING];
int len;
/* /-Y has the highest priority, then /Y and finally the COPYCMD env. variable */
2007-06-03 21:07:42 +00:00
if (strstrW (quals, parmNoY))
force = FALSE;
2007-06-03 21:07:42 +00:00
else if (strstrW (quals, parmY))
force = TRUE;
else {
2007-06-03 21:07:42 +00:00
const WCHAR copyCmdW[] = {'C','O','P','Y','C','M','D','\0'};
len = GetEnvironmentVariable (copyCmdW, copycmd, sizeof(copycmd)/sizeof(WCHAR));
force = (len && len < (sizeof(copycmd)/sizeof(WCHAR))
&& ! lstrcmpiW (copycmd, parmY));
}
/* Prompt if overwriting */
if (!force) {
2007-06-03 21:07:42 +00:00
WCHAR question[MAXSTRING];
WCHAR yesChar[10];
2007-06-03 21:07:42 +00:00
strcpyW(yesChar, WCMD_LoadMessage(WCMD_YES));
/* Ask for confirmation */
2007-06-03 21:07:42 +00:00
wsprintf(question, WCMD_LoadMessage(WCMD_OVERWRITE), dest);
ok = WCMD_ask_confirm(question, FALSE, NULL);
/* So delete the destination prior to the move */
if (ok) {
if (!DeleteFile (dest)) {
WCMD_print_error ();
errorlevel = 1;
ok = FALSE;
}
}
}
}
if (ok) {
status = MoveFile (src, dest);
} else {
status = 1; /* Anything other than 0 to prevent error msg below */
}
}
if (!status) {
WCMD_print_error ();
errorlevel = 1;
}
/* Step on to next match */
if (FindNextFile(hff, &fd) == 0) {
FindClose(hff);
hff = INVALID_HANDLE_VALUE;
break;
}
}
1999-06-06 15:24:04 +00:00
}
/****************************************************************************
* WCMD_pause
*
* Wait for keyboard input.
*/
void WCMD_pause (void) {
1999-06-06 15:24:04 +00:00
DWORD count;
2007-06-03 21:07:42 +00:00
WCHAR string[32];
1999-06-06 15:24:04 +00:00
WCMD_output (anykey);
2007-06-03 21:07:42 +00:00
WCMD_ReadFile (GetStdHandle(STD_INPUT_HANDLE), string,
sizeof(string)/sizeof(WCHAR), &count, NULL);
1999-06-06 15:24:04 +00:00
}
/****************************************************************************
* WCMD_remove_dir
*
* Delete a directory.
*/
2007-06-03 21:07:42 +00:00
void WCMD_remove_dir (WCHAR *command) {
1999-06-06 15:24:04 +00:00
2007-03-04 22:33:27 +00:00
int argno = 0;
int argsProcessed = 0;
2007-06-03 21:07:42 +00:00
WCHAR *argN = command;
static const WCHAR parmS[] = {'/','S','\0'};
static const WCHAR parmQ[] = {'/','Q','\0'};
2007-02-26 23:04:40 +00:00
2007-03-04 22:33:27 +00:00
/* Loop through all args */
while (argN) {
2007-06-03 21:07:42 +00:00
WCHAR *thisArg = WCMD_parameter (command, argno++, &argN);
2007-03-04 22:33:27 +00:00
if (argN && argN[0] != '/') {
2007-06-03 21:07:42 +00:00
WINE_TRACE("rd: Processing arg %s (quals:%s)\n", wine_dbgstr_w(thisArg),
wine_dbgstr_w(quals));
2007-03-04 22:33:27 +00:00
argsProcessed++;
2007-02-26 23:04:40 +00:00
2007-03-04 22:33:27 +00:00
/* If subdirectory search not supplied, just try to remove
and report error if it fails (eg if it contains a file) */
2007-06-03 21:07:42 +00:00
if (strstrW (quals, parmS) == NULL) {
2007-03-04 22:33:27 +00:00
if (!RemoveDirectory (thisArg)) WCMD_print_error ();
2007-02-26 23:04:40 +00:00
2007-03-04 22:33:27 +00:00
/* Otherwise use ShFileOp to recursively remove a directory */
} else {
2007-02-27 23:18:57 +00:00
2007-03-04 22:33:27 +00:00
SHFILEOPSTRUCT lpDir;
2007-02-27 23:18:57 +00:00
2007-03-04 22:33:27 +00:00
/* Ask first */
2007-06-03 21:07:42 +00:00
if (strstrW (quals, parmQ) == NULL) {
2007-03-04 22:33:27 +00:00
BOOL ok;
2007-06-03 21:07:42 +00:00
WCHAR question[MAXSTRING];
static const WCHAR fmt[] = {'%','s',' ','\0'};
2007-02-27 23:18:57 +00:00
2007-03-04 22:33:27 +00:00
/* Ask for confirmation */
2007-06-03 21:07:42 +00:00
wsprintf(question, fmt, thisArg);
ok = WCMD_ask_confirm(question, TRUE, NULL);
2007-03-04 22:33:27 +00:00
/* Abort if answer is 'N' */
if (!ok) return;
}
/* Do the delete */
lpDir.hwnd = NULL;
lpDir.pTo = NULL;
lpDir.pFrom = thisArg;
lpDir.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI;
lpDir.wFunc = FO_DELETE;
2007-06-03 21:07:42 +00:00
if (SHFileOperation(&lpDir)) WCMD_print_error ();
2007-03-04 22:33:27 +00:00
}
2007-02-27 23:18:57 +00:00
}
2007-03-04 22:33:27 +00:00
}
2007-02-27 23:18:57 +00:00
2007-03-04 22:33:27 +00:00
/* Handle no valid args */
if (argsProcessed == 0) {
WCMD_output (WCMD_LoadMessage(WCMD_NOARG));
2007-03-04 22:33:27 +00:00
return;
2007-02-26 23:04:40 +00:00
}
2007-03-04 22:33:27 +00:00
1999-06-06 15:24:04 +00:00
}
/****************************************************************************
* WCMD_rename
*
* Rename a file.
*/
void WCMD_rename (void) {
1999-06-06 15:24:04 +00:00
int status;
HANDLE hff;
WIN32_FIND_DATA fd;
2007-06-03 21:07:42 +00:00
WCHAR input[MAX_PATH];
WCHAR *dotDst = NULL;
WCHAR drive[10];
WCHAR dir[MAX_PATH];
WCHAR fname[MAX_PATH];
WCHAR ext[MAX_PATH];
DWORD attribs;
1999-06-06 15:24:04 +00:00
errorlevel = 0;
/* Must be at least two args */
if (param1[0] == 0x00 || param2[0] == 0x00) {
WCMD_output (WCMD_LoadMessage(WCMD_NOARG));
errorlevel = 1;
return;
}
/* Destination cannot contain a drive letter or directory separator */
2007-06-03 21:07:42 +00:00
if ((strchrW(param1,':') != NULL) || (strchrW(param1,'\\') != NULL)) {
SetLastError(ERROR_INVALID_PARAMETER);
WCMD_print_error();
errorlevel = 1;
return;
}
/* Convert partial path to full path */
2007-06-03 21:07:42 +00:00
GetFullPathName (param1, sizeof(input)/sizeof(WCHAR), input, NULL);
WINE_TRACE("Rename from '%s'('%s') to '%s'\n", wine_dbgstr_w(input),
wine_dbgstr_w(param1), wine_dbgstr_w(param2));
dotDst = strchrW(param2, '.');
/* Split into components */
WCMD_splitpath(input, drive, dir, fname, ext);
hff = FindFirstFile (input, &fd);
while (hff != INVALID_HANDLE_VALUE) {
2007-06-03 21:07:42 +00:00
WCHAR dest[MAX_PATH];
WCHAR src[MAX_PATH];
WCHAR *dotSrc = NULL;
int dirLen;
2007-06-03 21:07:42 +00:00
WINE_TRACE("Processing file '%s'\n", wine_dbgstr_w(fd.cFileName));
/* FIXME: If dest name or extension is *, replace with filename/ext
part otherwise use supplied name. This supports:
ren *.fred *.jim
ren jim.* fred.* etc
However, windows has a more complex algorithum supporting eg
?'s and *'s mid name */
2007-06-03 21:07:42 +00:00
dotSrc = strchrW(fd.cFileName, '.');
/* Build src & dest name */
2007-06-03 21:07:42 +00:00
strcpyW(src, drive);
strcatW(src, dir);
strcpyW(dest, src);
dirLen = strlenW(src);
strcatW(src, fd.cFileName);
/* Build name */
if (param2[0] == '*') {
2007-06-03 21:07:42 +00:00
strcatW(dest, fd.cFileName);
if (dotSrc) dest[dirLen + (dotSrc - fd.cFileName)] = 0x00;
} else {
2007-06-03 21:07:42 +00:00
strcatW(dest, param2);
if (dotDst) dest[dirLen + (dotDst - param2)] = 0x00;
}
/* Build Extension */
if (dotDst && (*(dotDst+1)=='*')) {
2007-06-03 21:07:42 +00:00
if (dotSrc) strcatW(dest, dotSrc);
} else if (dotDst) {
2007-06-03 21:07:42 +00:00
if (dotDst) strcatW(dest, dotDst);
}
2007-06-03 21:07:42 +00:00
WINE_TRACE("Source '%s'\n", wine_dbgstr_w(src));
WINE_TRACE("Dest '%s'\n", wine_dbgstr_w(dest));
/* Check if file is read only, otherwise move it */
2007-06-03 21:07:42 +00:00
attribs = GetFileAttributes(src);
if ((attribs != INVALID_FILE_ATTRIBUTES) &&
(attribs & FILE_ATTRIBUTE_READONLY)) {
SetLastError(ERROR_ACCESS_DENIED);
status = 0;
} else {
status = MoveFile (src, dest);
}
if (!status) {
WCMD_print_error ();
errorlevel = 1;
}
/* Step on to next match */
if (FindNextFile(hff, &fd) == 0) {
FindClose(hff);
hff = INVALID_HANDLE_VALUE;
break;
}
1999-06-06 15:24:04 +00:00
}
}
2004-03-22 22:56:58 +00:00
/*****************************************************************************
* WCMD_dupenv
*
* Make a copy of the environment.
*/
static WCHAR *WCMD_dupenv( const WCHAR *env )
2004-03-22 22:56:58 +00:00
{
WCHAR *env_copy;
int len;
if( !env )
return NULL;
len = 0;
while ( env[len] )
2007-06-03 21:07:42 +00:00
len += (strlenW(&env[len]) + 1);
2004-03-22 22:56:58 +00:00
env_copy = LocalAlloc (LMEM_FIXED, (len+1) * sizeof (WCHAR) );
if (!env_copy)
{
WINE_ERR("out of memory\n");
2004-03-22 22:56:58 +00:00
return env_copy;
}
memcpy (env_copy, env, len*sizeof (WCHAR));
env_copy[len] = 0;
return env_copy;
}
/*****************************************************************************
* WCMD_setlocal
*
* setlocal pushes the environment onto a stack
* Save the environment as unicode so we don't screw anything up.
*/
2007-06-03 21:07:42 +00:00
void WCMD_setlocal (const WCHAR *s) {
2004-03-22 22:56:58 +00:00
WCHAR *env;
struct env_stack *env_copy;
2007-06-03 21:07:42 +00:00
WCHAR cwd[MAX_PATH];
2004-03-22 22:56:58 +00:00
/* DISABLEEXTENSIONS ignored */
env_copy = LocalAlloc (LMEM_FIXED, sizeof (struct env_stack));
if( !env_copy )
{
WINE_ERR ("out of memory\n");
2004-03-22 22:56:58 +00:00
return;
}
env = GetEnvironmentStringsW ();
env_copy->strings = WCMD_dupenv (env);
if (env_copy->strings)
{
env_copy->next = saved_environment;
saved_environment = env_copy;
/* Save the current drive letter */
GetCurrentDirectory (MAX_PATH, cwd);
env_copy->u.cwd = cwd[0];
2004-03-22 22:56:58 +00:00
}
else
LocalFree (env_copy);
FreeEnvironmentStringsW (env);
2004-03-22 22:56:58 +00:00
}
/*****************************************************************************
* WCMD_endlocal
*
* endlocal pops the environment off a stack
2007-06-03 21:07:42 +00:00
* Note: When searching for '=', search from WCHAR position 1, to handle
* special internal environment variables =C:, =D: etc
2004-03-22 22:56:58 +00:00
*/
void WCMD_endlocal (void) {
WCHAR *env, *old, *p;
struct env_stack *temp;
int len, n;
if (!saved_environment)
return;
/* pop the old environment from the stack */
temp = saved_environment;
saved_environment = temp->next;
/* delete the current environment, totally */
env = GetEnvironmentStringsW ();
old = WCMD_dupenv (GetEnvironmentStringsW ());
len = 0;
while (old[len]) {
2007-06-03 21:07:42 +00:00
n = strlenW(&old[len]) + 1;
p = strchrW(&old[len] + 1, '=');
2004-03-22 22:56:58 +00:00
if (p)
{
*p++ = 0;
SetEnvironmentVariableW (&old[len], NULL);
}
len += n;
}
LocalFree (old);
FreeEnvironmentStringsW (env);
2007-02-27 23:18:57 +00:00
2004-03-22 22:56:58 +00:00
/* restore old environment */
env = temp->strings;
len = 0;
while (env[len]) {
2007-06-03 21:07:42 +00:00
n = strlenW(&env[len]) + 1;
p = strchrW(&env[len] + 1, '=');
2004-03-22 22:56:58 +00:00
if (p)
{
*p++ = 0;
SetEnvironmentVariableW (&env[len], p);
}
len += n;
}
/* Restore current drive letter */
if (IsCharAlpha(temp->u.cwd)) {
2007-06-03 21:07:42 +00:00
WCHAR envvar[4];
WCHAR cwd[MAX_PATH];
static const WCHAR fmt[] = {'=','%','c',':','\0'};
wsprintf(envvar, fmt, temp->u.cwd);
if (GetEnvironmentVariable(envvar, cwd, MAX_PATH)) {
2007-06-03 21:07:42 +00:00
WINE_TRACE("Resetting cwd to %s\n", wine_dbgstr_w(cwd));
SetCurrentDirectory(cwd);
}
}
2004-03-22 22:56:58 +00:00
LocalFree (env);
LocalFree (temp);
}
1999-06-06 15:24:04 +00:00
/*****************************************************************************
* WCMD_setshow_attrib
*
* Display and optionally sets DOS attributes on a file or directory
*
* FIXME: Wine currently uses the Unix stat() function to get file attributes.
* As a result only the Readonly flag is correctly reported, the Archive bit
* is always set and the rest are not implemented. We do the Right Thing anyway.
*
* FIXME: No SET functionality.
*
1999-06-06 15:24:04 +00:00
*/
void WCMD_setshow_attrib (void) {
1999-06-06 15:24:04 +00:00
DWORD count;
HANDLE hff;
WIN32_FIND_DATA fd;
2007-06-03 21:07:42 +00:00
WCHAR flags[9] = {' ',' ',' ',' ',' ',' ',' ',' ','\0'};
1999-06-06 15:24:04 +00:00
if (param1[0] == '-') {
WCMD_output (WCMD_LoadMessage(WCMD_NYI));
return;
}
2007-06-03 21:07:42 +00:00
if (strlenW(param1) == 0) {
static const WCHAR slashStarW[] = {'\\','*','\0'};
GetCurrentDirectory (sizeof(param1)/sizeof(WCHAR), param1);
strcatW (param1, slashStarW);
1999-06-06 15:24:04 +00:00
}
hff = FindFirstFile (param1, &fd);
if (hff == INVALID_HANDLE_VALUE) {
WCMD_output (WCMD_LoadMessage(WCMD_FILENOTFOUND), param1);
1999-06-06 15:24:04 +00:00
}
else {
do {
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
2007-06-03 21:07:42 +00:00
static const WCHAR fmt[] = {'%','s',' ',' ',' ','%','s','\n','\0'};
1999-06-06 15:24:04 +00:00
if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
flags[0] = 'H';
}
if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
flags[1] = 'S';
}
if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
flags[2] = 'A';
}
if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
flags[3] = 'R';
}
if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) {
flags[4] = 'T';
}
if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) {
flags[5] = 'C';
}
2007-06-03 21:07:42 +00:00
WCMD_output (fmt, flags, fd.cFileName);
1999-06-06 15:24:04 +00:00
for (count=0; count < 8; count++) flags[count] = ' ';
}
} while (FindNextFile(hff, &fd) != 0);
}
FindClose (hff);
}
/*****************************************************************************
* WCMD_setshow_default
*
* Set/Show the current default directory
*/
2007-06-03 21:07:42 +00:00
void WCMD_setshow_default (WCHAR *command) {
1999-06-06 15:24:04 +00:00
BOOL status;
2007-06-03 21:07:42 +00:00
WCHAR string[1024];
WCHAR cwd[1024];
WCHAR *pos;
WIN32_FIND_DATA fd;
HANDLE hff;
2007-06-03 21:07:42 +00:00
static const WCHAR parmD[] = {'/','D','\0'};
1999-06-06 15:24:04 +00:00
2007-06-03 21:07:42 +00:00
WINE_TRACE("Request change to directory '%s'\n", wine_dbgstr_w(command));
2007-03-08 00:50:14 +00:00
/* Skip /D and trailing whitespace if on the front of the command line */
if (CompareString (LOCALE_USER_DEFAULT,
NORM_IGNORECASE | SORT_STRINGSORT,
2007-06-03 21:07:42 +00:00
command, 2, parmD, -1) == 2) {
2007-03-08 00:50:14 +00:00
command += 2;
while (*command && *command==' ') command++;
}
2007-06-03 21:07:42 +00:00
GetCurrentDirectory (sizeof(cwd)/sizeof(WCHAR), cwd);
if (strlenW(command) == 0) {
strcatW (cwd, newline);
2007-03-08 00:50:14 +00:00
WCMD_output (cwd);
1999-06-06 15:24:04 +00:00
}
else {
/* Remove any double quotes, which may be in the
middle, eg. cd "C:\Program Files"\Microsoft is ok */
pos = string;
while (*command) {
if (*command != '"') *pos++ = *command;
command++;
}
*pos = 0x00;
/* Search for approprate directory */
2007-06-03 21:07:42 +00:00
WINE_TRACE("Looking for directory '%s'\n", wine_dbgstr_w(string));
hff = FindFirstFile (string, &fd);
while (hff != INVALID_HANDLE_VALUE) {
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
2007-06-03 21:07:42 +00:00
WCHAR fpath[MAX_PATH];
WCHAR drive[10];
WCHAR dir[MAX_PATH];
WCHAR fname[MAX_PATH];
WCHAR ext[MAX_PATH];
static const WCHAR fmt[] = {'%','s','%','s','%','s','\0'};
/* Convert path into actual directory spec */
2007-06-03 21:07:42 +00:00
GetFullPathName (string, sizeof(fpath)/sizeof(WCHAR), fpath, NULL);
WCMD_splitpath(fpath, drive, dir, fname, ext);
/* Rebuild path */
2007-06-03 21:07:42 +00:00
wsprintf(string, fmt, drive, dir, fd.cFileName);
FindClose(hff);
hff = INVALID_HANDLE_VALUE;
break;
}
/* Step on to next match */
if (FindNextFile(hff, &fd) == 0) {
FindClose(hff);
hff = INVALID_HANDLE_VALUE;
break;
}
}
/* Change to that directory */
2007-06-03 21:07:42 +00:00
WINE_TRACE("Really changing to directory '%s'\n", wine_dbgstr_w(string));
status = SetCurrentDirectory (string);
1999-06-06 15:24:04 +00:00
if (!status) {
2007-03-08 00:43:32 +00:00
errorlevel = 1;
1999-06-06 15:24:04 +00:00
WCMD_print_error ();
return;
2007-03-08 00:50:14 +00:00
} else {
/* Restore old directory if drive letter would change, and
CD x:\directory /D (or pushd c:\directory) not supplied */
2007-06-03 21:07:42 +00:00
if ((strstrW(quals, parmD) == NULL) &&
2007-03-08 00:50:14 +00:00
(param1[1] == ':') && (toupper(param1[0]) != toupper(cwd[0]))) {
SetCurrentDirectory(cwd);
}
1999-06-06 15:24:04 +00:00
}
2007-03-08 00:50:14 +00:00
/* Set special =C: type environment variable, for drive letter of
change of directory, even if path was restored due to missing
/D (allows changing drive letter when not resident on that
drive */
if ((string[1] == ':') && IsCharAlpha (string[0])) {
2007-06-03 21:07:42 +00:00
WCHAR env[4];
strcpyW(env, equalW);
memcpy(env+1, string, 2 * sizeof(WCHAR));
env[3] = 0x00;
2007-06-03 21:07:42 +00:00
WINE_FIXME("Setting '%s' to '%s'\n", wine_dbgstr_w(env), wine_dbgstr_w(string));
SetEnvironmentVariable(env, string);
}
1999-06-06 15:24:04 +00:00
}
return;
}
/****************************************************************************
* WCMD_setshow_date
*
* Set/Show the system date
* FIXME: Can't change date yet
*/
void WCMD_setshow_date (void) {
1999-06-06 15:24:04 +00:00
2007-06-03 21:07:42 +00:00
WCHAR curdate[64], buffer[64];
DWORD count;
2007-06-03 21:07:42 +00:00
static const WCHAR parmT[] = {'/','T','\0'};
1999-06-06 15:24:04 +00:00
2007-06-03 21:07:42 +00:00
if (strlenW(param1) == 0) {
1999-06-06 15:24:04 +00:00
if (GetDateFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL,
2007-06-03 21:07:42 +00:00
curdate, sizeof(curdate)/sizeof(WCHAR))) {
WCMD_output (WCMD_LoadMessage(WCMD_CURRENTDATE), curdate);
2007-06-03 21:07:42 +00:00
if (strstrW (quals, parmT) == NULL) {
WCMD_output (WCMD_LoadMessage(WCMD_NEWDATE));
2007-06-03 21:07:42 +00:00
WCMD_ReadFile (GetStdHandle(STD_INPUT_HANDLE),
buffer, sizeof(buffer)/sizeof(WCHAR), &count, NULL);
2007-03-08 00:45:05 +00:00
if (count > 2) {
WCMD_output (WCMD_LoadMessage(WCMD_NYI));
2007-03-08 00:45:05 +00:00
}
1999-06-06 15:24:04 +00:00
}
}
else WCMD_print_error ();
}
else {
WCMD_output (WCMD_LoadMessage(WCMD_NYI));
1999-06-06 15:24:04 +00:00
}
}
2003-12-30 19:17:31 +00:00
/****************************************************************************
* WCMD_compare
*/
static int WCMD_compare( const void *a, const void *b )
2003-12-30 19:17:31 +00:00
{
int r;
2007-06-03 21:07:42 +00:00
const WCHAR * const *str_a = a, * const *str_b = b;
2003-12-30 19:17:31 +00:00
r = CompareString( LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
*str_a, -1, *str_b, -1 );
if( r == CSTR_LESS_THAN ) return -1;
if( r == CSTR_GREATER_THAN ) return 1;
return 0;
}
/****************************************************************************
* WCMD_setshow_sortenv
*
* sort variables into order for display
* Optionally only display those who start with a stub
* returns the count displayed
2003-12-30 19:17:31 +00:00
*/
2007-06-03 21:07:42 +00:00
static int WCMD_setshow_sortenv(const WCHAR *s, const WCHAR *stub)
2003-12-30 19:17:31 +00:00
{
UINT count=0, len=0, i, displayedcount=0, stublen=0;
2007-06-03 21:07:42 +00:00
const WCHAR **str;
2003-12-30 19:17:31 +00:00
2007-06-03 21:07:42 +00:00
if (stub) stublen = strlenW(stub);
2003-12-30 19:17:31 +00:00
/* count the number of strings, and the total length */
while ( s[len] ) {
2007-06-03 21:07:42 +00:00
len += (strlenW(&s[len]) + 1);
2003-12-30 19:17:31 +00:00
count++;
}
/* add the strings to an array */
2007-06-03 21:07:42 +00:00
str = LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, count * sizeof (WCHAR*) );
2003-12-30 19:17:31 +00:00
if( !str )
return 0;
2003-12-30 19:17:31 +00:00
str[0] = s;
for( i=1; i<count; i++ )
2007-06-03 21:07:42 +00:00
str[i] = str[i-1] + strlenW(str[i-1]) + 1;
2003-12-30 19:17:31 +00:00
/* sort the array */
2007-06-03 21:07:42 +00:00
qsort( str, count, sizeof (WCHAR*), WCMD_compare );
2003-12-30 19:17:31 +00:00
/* print it */
for( i=0; i<count; i++ ) {
if (!stub || CompareString (LOCALE_USER_DEFAULT,
NORM_IGNORECASE | SORT_STRINGSORT,
str[i], stublen, stub, -1) == 2) {
/* Don't display special internal variables */
if (str[i][0] != '=') {
WCMD_output_asis(str[i]);
WCMD_output_asis(newline);
displayedcount++;
}
}
}
2003-12-30 19:17:31 +00:00
LocalFree( str );
return displayedcount;
2003-12-30 19:17:31 +00:00
}
1999-06-06 15:24:04 +00:00
/****************************************************************************
* WCMD_setshow_env
*
* Set/Show the environment variables
*/
2007-06-03 21:07:42 +00:00
void WCMD_setshow_env (WCHAR *s) {
1999-06-06 15:24:04 +00:00
LPVOID env;
2007-06-03 21:07:42 +00:00
WCHAR *p;
int status;
2007-06-03 21:07:42 +00:00
static const WCHAR parmP[] = {'/','P','\0'};
1999-06-06 15:24:04 +00:00
errorlevel = 0;
2007-03-13 00:11:04 +00:00
if (param1[0] == 0x00 && quals[0] == 0x00) {
1999-06-06 15:24:04 +00:00
env = GetEnvironmentStrings ();
WCMD_setshow_sortenv( env, NULL );
2007-03-13 00:11:04 +00:00
return;
1999-06-06 15:24:04 +00:00
}
2007-03-13 00:11:04 +00:00
/* See if /P supplied, and if so echo the prompt, and read in a reply */
if (CompareString (LOCALE_USER_DEFAULT,
NORM_IGNORECASE | SORT_STRINGSORT,
2007-06-03 21:07:42 +00:00
s, 2, parmP, -1) == 2) {
WCHAR string[MAXSTRING];
2007-03-13 00:11:04 +00:00
DWORD count;
s += 2;
while (*s && *s==' ') s++;
/* If no parameter, or no '=' sign, return an error */
2007-06-03 21:07:42 +00:00
if (!(*s) || ((p = strchrW (s, '=')) == NULL )) {
WCMD_output (WCMD_LoadMessage(WCMD_NOARG));
2007-03-13 00:11:04 +00:00
return;
}
/* Output the prompt */
*p++ = '\0';
2007-06-03 21:07:42 +00:00
if (strlenW(p) != 0) WCMD_output(p);
2007-03-13 00:11:04 +00:00
/* Read the reply */
2007-06-03 21:07:42 +00:00
WCMD_ReadFile (GetStdHandle(STD_INPUT_HANDLE), string,
sizeof(string)/sizeof(WCHAR), &count, NULL);
2007-03-13 00:11:04 +00:00
if (count > 1) {
string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
2007-06-03 21:07:42 +00:00
WINE_TRACE("set /p: Setting var '%s' to '%s'\n", wine_dbgstr_w(s),
wine_dbgstr_w(string));
2007-03-13 00:11:04 +00:00
status = SetEnvironmentVariable (s, string);
}
} else {
DWORD gle;
2007-06-03 21:07:42 +00:00
p = strchrW (s, '=');
1999-06-06 15:24:04 +00:00
if (p == NULL) {
env = GetEnvironmentStrings ();
if (WCMD_setshow_sortenv( env, s ) == 0) {
WCMD_output (WCMD_LoadMessage(WCMD_MISSINGENV), s);
errorlevel = 1;
2002-05-31 23:06:46 +00:00
}
1999-06-06 15:24:04 +00:00
return;
}
*p++ = '\0';
2007-06-03 21:07:42 +00:00
if (strlenW(p) == 0) p = NULL;
2002-05-31 23:06:46 +00:00
status = SetEnvironmentVariable (s, p);
gle = GetLastError();
if ((!status) & (gle == ERROR_ENVVAR_NOT_FOUND)) {
errorlevel = 1;
} else if ((!status)) WCMD_print_error();
1999-06-06 15:24:04 +00:00
}
}
/****************************************************************************
* WCMD_setshow_path
*
* Set/Show the path environment variable
*/
2007-06-03 21:07:42 +00:00
void WCMD_setshow_path (WCHAR *command) {
1999-06-06 15:24:04 +00:00
2007-06-03 21:07:42 +00:00
WCHAR string[1024];
DWORD status;
2007-06-03 21:07:42 +00:00
static const WCHAR pathW[] = {'P','A','T','H','\0'};
static const WCHAR pathEqW[] = {'P','A','T','H','=','\0'};
1999-06-06 15:24:04 +00:00
2007-06-03 21:07:42 +00:00
if (strlenW(param1) == 0) {
status = GetEnvironmentVariable (pathW, string, sizeof(string)/sizeof(WCHAR));
1999-06-06 15:24:04 +00:00
if (status != 0) {
2007-06-03 21:07:42 +00:00
WCMD_output_asis ( pathEqW);
WCMD_output_asis ( string);
WCMD_output_asis ( newline);
1999-06-06 15:24:04 +00:00
}
else {
2007-06-03 21:07:42 +00:00
WCMD_output (WCMD_LoadMessage(WCMD_NOPATH));
1999-06-06 15:24:04 +00:00
}
}
else {
if (*command == '=') command++; /* Skip leading '=' */
2007-06-03 21:07:42 +00:00
status = SetEnvironmentVariable (pathW, command);
1999-06-06 15:24:04 +00:00
if (!status) WCMD_print_error();
}
}
/****************************************************************************
* WCMD_setshow_prompt
*
* Set or show the command prompt.
*/
void WCMD_setshow_prompt (void) {
1999-06-06 15:24:04 +00:00
2007-06-03 21:07:42 +00:00
WCHAR *s;
static const WCHAR promptW[] = {'P','R','O','M','P','T','\0'};
1999-06-06 15:24:04 +00:00
2007-06-03 21:07:42 +00:00
if (strlenW(param1) == 0) {
SetEnvironmentVariable (promptW, NULL);
1999-06-06 15:24:04 +00:00
}
else {
s = param1;
while ((*s == '=') || (*s == ' ')) s++;
2007-06-03 21:07:42 +00:00
if (strlenW(s) == 0) {
SetEnvironmentVariable (promptW, NULL);
1999-06-06 15:24:04 +00:00
}
2007-06-03 21:07:42 +00:00
else SetEnvironmentVariable (promptW, s);
1999-06-06 15:24:04 +00:00
}
}
/****************************************************************************
* WCMD_setshow_time
*
* Set/Show the system time
* FIXME: Can't change time yet
*/
void WCMD_setshow_time (void) {
1999-06-06 15:24:04 +00:00
2007-06-03 21:07:42 +00:00
WCHAR curtime[64], buffer[64];
DWORD count;
SYSTEMTIME st;
2007-06-03 21:07:42 +00:00
static const WCHAR parmT[] = {'/','T','\0'};
1999-06-06 15:24:04 +00:00
2007-06-03 21:07:42 +00:00
if (strlenW(param1) == 0) {
GetLocalTime(&st);
if (GetTimeFormat (LOCALE_USER_DEFAULT, 0, &st, NULL,
2007-06-03 21:07:42 +00:00
curtime, sizeof(curtime)/sizeof(WCHAR))) {
WCMD_output (WCMD_LoadMessage(WCMD_CURRENTDATE), curtime);
2007-06-03 21:07:42 +00:00
if (strstrW (quals, parmT) == NULL) {
WCMD_output (WCMD_LoadMessage(WCMD_NEWTIME));
2007-06-03 21:07:42 +00:00
WCMD_ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer,
sizeof(buffer)/sizeof(WCHAR), &count, NULL);
2007-03-08 00:45:27 +00:00
if (count > 2) {
WCMD_output (WCMD_LoadMessage(WCMD_NYI));
2007-03-08 00:45:27 +00:00
}
1999-06-06 15:24:04 +00:00
}
}
else WCMD_print_error ();
}
else {
WCMD_output (WCMD_LoadMessage(WCMD_NYI));
1999-06-06 15:24:04 +00:00
}
}
/****************************************************************************
* WCMD_shift
*
* Shift batch parameters.
2007-03-08 00:50:37 +00:00
* Optional /n says where to start shifting (n=0-8)
1999-06-06 15:24:04 +00:00
*/
2007-06-03 21:07:42 +00:00
void WCMD_shift (WCHAR *command) {
2007-03-08 00:50:37 +00:00
int start;
1999-06-06 15:24:04 +00:00
2007-03-08 00:50:37 +00:00
if (context != NULL) {
2007-06-03 21:07:42 +00:00
WCHAR *pos = strchrW(command, '/');
2007-03-08 00:50:37 +00:00
int i;
if (pos == NULL) {
start = 0;
} else if (*(pos+1)>='0' && *(pos+1)<='8') {
start = (*(pos+1) - '0');
} else {
SetLastError(ERROR_INVALID_PARAMETER);
WCMD_print_error();
return;
}
WINE_TRACE("Shifting variables, starting at %d\n", start);
for (i=start;i<=8;i++) {
context -> shift_count[i] = context -> shift_count[i+1] + 1;
}
context -> shift_count[9] = context -> shift_count[9] + 1;
}
1999-06-06 15:24:04 +00:00
}
2002-05-04 18:29:31 +00:00
/****************************************************************************
* WCMD_title
*
* Set the console title
*/
2007-06-03 21:07:42 +00:00
void WCMD_title (WCHAR *command) {
2002-05-04 18:29:31 +00:00
SetConsoleTitle(command);
}
1999-06-06 15:24:04 +00:00
/****************************************************************************
* WCMD_type
*
* Copy a file to standard output.
*/
2007-06-03 21:07:42 +00:00
void WCMD_type (WCHAR *command) {
1999-06-06 15:24:04 +00:00
int argno = 0;
2007-06-03 21:07:42 +00:00
WCHAR *argN = command;
BOOL writeHeaders = FALSE;
1999-06-06 15:24:04 +00:00
if (param1[0] == 0x00) {
WCMD_output (WCMD_LoadMessage(WCMD_NOARG));
return;
}
if (param2[0] != 0x00) writeHeaders = TRUE;
/* Loop through all args */
errorlevel = 0;
while (argN) {
2007-06-03 21:07:42 +00:00
WCHAR *thisArg = WCMD_parameter (command, argno++, &argN);
HANDLE h;
2007-06-03 21:07:42 +00:00
WCHAR buffer[512];
DWORD count;
if (!argN) break;
2007-06-03 21:07:42 +00:00
WINE_TRACE("type: Processing arg '%s'\n", wine_dbgstr_w(thisArg));
h = CreateFile (thisArg, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE) {
WCMD_print_error ();
WCMD_output (WCMD_LoadMessage(WCMD_READFAIL), thisArg);
errorlevel = 1;
} else {
if (writeHeaders) {
2007-06-03 21:07:42 +00:00
static const WCHAR fmt[] = {'\n','%','s','\n','\n','\0'};
WCMD_output(fmt, thisArg);
}
2007-06-03 21:07:42 +00:00
while (WCMD_ReadFile (h, buffer, sizeof(buffer)/sizeof(WCHAR), &count, NULL)) {
if (count == 0) break; /* ReadFile reports success on EOF! */
buffer[count] = 0;
WCMD_output_asis (buffer);
}
CloseHandle (h);
}
1999-06-06 15:24:04 +00:00
}
}
/****************************************************************************
* WCMD_more
*
* Output either a file or stdin to screen in pages
*/
2007-06-03 21:07:42 +00:00
void WCMD_more (WCHAR *command) {
int argno = 0;
2007-06-03 21:07:42 +00:00
WCHAR *argN = command;
BOOL useinput = FALSE;
2007-06-03 21:07:42 +00:00
WCHAR moreStr[100];
WCHAR moreStrPage[100];
WCHAR buffer[512];
DWORD count;
2007-06-03 21:07:42 +00:00
static const WCHAR moreStart[] = {'-','-',' ','\0'};
static const WCHAR moreFmt[] = {'%','s',' ','-','-','\n','\0'};
static const WCHAR moreFmt2[] = {'%','s',' ','(','%','2','.','2','d','%','%',
')',' ','-','-','\n','\0'};
static const WCHAR conInW[] = {'C','O','N','I','N','$','\0'};
/* Prefix the NLS more with '-- ', then load the text */
errorlevel = 0;
2007-06-03 21:07:42 +00:00
strcpyW(moreStr, moreStart);
LoadString (hinst, WCMD_MORESTR, &moreStr[3],
(sizeof(moreStr)/sizeof(WCHAR))-3);
if (param1[0] == 0x00) {
/* Wine implements pipes via temporary files, and hence stdin is
effectively reading from the file. This means the prompts for
more are satistied by the next line from the input (file). To
avoid this, ensure stdin is to the console */
HANDLE hstdin = GetStdHandle(STD_INPUT_HANDLE);
2007-06-03 21:07:42 +00:00
HANDLE hConIn = CreateFile(conInW, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
SetStdHandle(STD_INPUT_HANDLE, hConIn);
/* Warning: No easy way of ending the stream (ctrl+z on windows) so
once you get in this bit unless due to a pipe, its going to end badly... */
useinput = TRUE;
2007-06-03 21:07:42 +00:00
wsprintf(moreStrPage, moreFmt, moreStr);
WCMD_enter_paged_mode(moreStrPage);
2007-06-03 21:07:42 +00:00
while (WCMD_ReadFile (hstdin, buffer, (sizeof(buffer)/sizeof(WCHAR))-1, &count, NULL)) {
if (count == 0) break; /* ReadFile reports success on EOF! */
buffer[count] = 0;
WCMD_output_asis (buffer);
}
WCMD_leave_paged_mode();
/* Restore stdin to what it was */
SetStdHandle(STD_INPUT_HANDLE, hstdin);
CloseHandle(hConIn);
return;
} else {
BOOL needsPause = FALSE;
/* Loop through all args */
WCMD_enter_paged_mode(moreStrPage);
while (argN) {
2007-06-03 21:07:42 +00:00
WCHAR *thisArg = WCMD_parameter (command, argno++, &argN);
HANDLE h;
if (!argN) break;
if (needsPause) {
/* Wait */
2007-06-03 21:07:42 +00:00
wsprintf(moreStrPage, moreFmt2, moreStr, 100);
WCMD_leave_paged_mode();
WCMD_output_asis(moreStrPage);
2007-06-03 21:07:42 +00:00
WCMD_ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer,
sizeof(buffer)/sizeof(WCHAR), &count, NULL);
WCMD_enter_paged_mode(moreStrPage);
}
2007-06-03 21:07:42 +00:00
WINE_TRACE("more: Processing arg '%s'\n", wine_dbgstr_w(thisArg));
h = CreateFile (thisArg, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE) {
WCMD_print_error ();
WCMD_output (WCMD_LoadMessage(WCMD_READFAIL), thisArg);
errorlevel = 1;
} else {
ULONG64 curPos = 0;
ULONG64 fileLen = 0;
WIN32_FILE_ATTRIBUTE_DATA fileInfo;
/* Get the file size */
GetFileAttributesEx(thisArg, GetFileExInfoStandard, (void*)&fileInfo);
fileLen = (((ULONG64)fileInfo.nFileSizeHigh) << 32) + fileInfo.nFileSizeLow;
needsPause = TRUE;
2007-06-03 21:07:42 +00:00
while (WCMD_ReadFile (h, buffer, (sizeof(buffer)/sizeof(WCHAR))-1, &count, NULL)) {
if (count == 0) break; /* ReadFile reports success on EOF! */
buffer[count] = 0;
curPos += count;
/* Update % count (would be used in WCMD_output_asis as prompt) */
2007-06-03 21:07:42 +00:00
wsprintf(moreStrPage, moreFmt2, moreStr, (int) min(99, (curPos * 100)/fileLen));
WCMD_output_asis (buffer);
}
CloseHandle (h);
}
}
WCMD_leave_paged_mode();
}
}
1999-06-06 15:24:04 +00:00
/****************************************************************************
* WCMD_verify
*
* Display verify flag.
* FIXME: We don't actually do anything with the verify flag other than toggle
* it...
1999-06-06 15:24:04 +00:00
*/
2007-06-03 21:07:42 +00:00
void WCMD_verify (WCHAR *command) {
1999-06-06 15:24:04 +00:00
int count;
1999-06-06 15:24:04 +00:00
2007-06-03 21:07:42 +00:00
count = strlenW(command);
if (count == 0) {
2007-06-03 21:07:42 +00:00
if (verify_mode) WCMD_output (WCMD_LoadMessage(WCMD_VERIFYPROMPT), onW);
else WCMD_output (WCMD_LoadMessage(WCMD_VERIFYPROMPT), offW);
return;
}
2007-06-03 21:07:42 +00:00
if (lstrcmpiW(command, onW) == 0) {
verify_mode = 1;
return;
}
2007-06-03 21:07:42 +00:00
else if (lstrcmpiW(command, offW) == 0) {
verify_mode = 0;
return;
}
else WCMD_output (WCMD_LoadMessage(WCMD_VERIFYERR));
1999-06-06 15:24:04 +00:00
}
/****************************************************************************
* WCMD_version
*
* Display version info.
*/
void WCMD_version (void) {
1999-06-06 15:24:04 +00:00
WCMD_output (version_string);
}
/****************************************************************************
* WCMD_volume
*
* Display volume info and/or set volume label. Returns 0 if error.
*/
2007-06-03 21:07:42 +00:00
int WCMD_volume (int mode, WCHAR *path) {
1999-06-06 15:24:04 +00:00
DWORD count, serial;
2007-06-03 21:07:42 +00:00
WCHAR string[MAX_PATH], label[MAX_PATH], curdir[MAX_PATH];
BOOL status;
1999-06-06 15:24:04 +00:00
2007-06-03 21:07:42 +00:00
if (strlenW(path) == 0) {
status = GetCurrentDirectory (sizeof(curdir)/sizeof(WCHAR), curdir);
1999-06-06 15:24:04 +00:00
if (!status) {
WCMD_print_error ();
return 0;
}
2007-06-03 21:07:42 +00:00
status = GetVolumeInformation (NULL, label, sizeof(label)/sizeof(WCHAR),
&serial, NULL, NULL, NULL, 0);
1999-06-06 15:24:04 +00:00
}
else {
2007-06-03 21:07:42 +00:00
static const WCHAR fmt[] = {'%','s','\\','\0'};
if ((path[1] != ':') || (strlenW(path) != 2)) {
WCMD_output (WCMD_LoadMessage(WCMD_SYNTAXERR));
1999-06-06 15:24:04 +00:00
return 0;
}
2007-06-03 21:07:42 +00:00
wsprintf (curdir, fmt, path);
status = GetVolumeInformation (curdir, label, sizeof(label)/sizeof(WCHAR),
&serial, NULL,
1999-06-06 15:24:04 +00:00
NULL, NULL, 0);
}
if (!status) {
WCMD_print_error ();
return 0;
}
WCMD_output (WCMD_LoadMessage(WCMD_VOLUMEDETAIL),
1999-06-06 15:24:04 +00:00
curdir[0], label, HIWORD(serial), LOWORD(serial));
if (mode) {
WCMD_output (WCMD_LoadMessage(WCMD_VOLUMEPROMPT));
2007-06-03 21:07:42 +00:00
WCMD_ReadFile (GetStdHandle(STD_INPUT_HANDLE), string,
sizeof(string)/sizeof(WCHAR), &count, NULL);
if (count > 1) {
string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
}
2007-06-03 21:07:42 +00:00
if (strlenW(path) != 0) {
1999-06-06 15:24:04 +00:00
if (!SetVolumeLabel (curdir, string)) WCMD_print_error ();
}
else {
if (!SetVolumeLabel (NULL, string)) WCMD_print_error ();
}
}
return 1;
}
2007-02-20 17:49:08 +00:00
/**************************************************************************
* WCMD_exit
*
* Exit either the process, or just this batch program
*
*/
void WCMD_exit (void) {
2007-06-03 21:07:42 +00:00
static const WCHAR parmB[] = {'/','B','\0'};
int rc = atoiW(param1); /* Note: atoi of empty parameter is 0 */
2007-02-20 17:49:08 +00:00
2007-06-03 21:07:42 +00:00
if (context && lstrcmpiW(quals, parmB) == 0) {
2007-02-20 17:49:08 +00:00
errorlevel = rc;
context -> skip_rest = TRUE;
} else {
ExitProcess(rc);
}
}
2007-02-27 23:18:57 +00:00
/**************************************************************************
* WCMD_ask_confirm
*
* Issue a message and ask 'Are you sure (Y/N)', waiting on a valid
* answer.
*
* Returns True if Y (or A) answer is selected
* If optionAll contains a pointer, ALL is allowed, and if answered
* set to TRUE
2007-02-27 23:18:57 +00:00
*
*/
2007-06-03 21:07:42 +00:00
BOOL WCMD_ask_confirm (WCHAR *message, BOOL showSureText, BOOL *optionAll) {
2007-02-27 23:18:57 +00:00
2007-06-03 21:07:42 +00:00
WCHAR msgbuffer[MAXSTRING];
WCHAR Ybuffer[MAXSTRING];
WCHAR Nbuffer[MAXSTRING];
WCHAR Abuffer[MAXSTRING];
WCHAR answer[MAX_PATH] = {'\0'};
2007-02-27 23:18:57 +00:00
DWORD count = 0;
/* Load the translated 'Are you sure', plus valid answers */
2007-06-03 21:07:42 +00:00
LoadString (hinst, WCMD_CONFIRM, msgbuffer, sizeof(msgbuffer)/sizeof(WCHAR));
LoadString (hinst, WCMD_YES, Ybuffer, sizeof(Ybuffer)/sizeof(WCHAR));
LoadString (hinst, WCMD_NO, Nbuffer, sizeof(Nbuffer)/sizeof(WCHAR));
LoadString (hinst, WCMD_ALL, Abuffer, sizeof(Abuffer)/sizeof(WCHAR));
2007-02-27 23:18:57 +00:00
/* Loop waiting on a Y or N */
while (answer[0] != Ybuffer[0] && answer[0] != Nbuffer[0]) {
2007-06-03 21:07:42 +00:00
static const WCHAR startBkt[] = {' ','(','\0'};
static const WCHAR endBkt[] = {')','?','\0'};
2007-02-27 23:18:57 +00:00
WCMD_output_asis (message);
2007-02-27 23:20:05 +00:00
if (showSureText) {
WCMD_output_asis (msgbuffer);
}
2007-06-03 21:07:42 +00:00
WCMD_output_asis (startBkt);
2007-02-27 23:18:57 +00:00
WCMD_output_asis (Ybuffer);
2007-06-03 21:07:42 +00:00
WCMD_output_asis (fslashW);
2007-02-27 23:18:57 +00:00
WCMD_output_asis (Nbuffer);
if (optionAll) {
2007-06-03 21:07:42 +00:00
WCMD_output_asis (fslashW);
WCMD_output_asis (Abuffer);
}
2007-06-03 21:07:42 +00:00
WCMD_output_asis (endBkt);
WCMD_ReadFile (GetStdHandle(STD_INPUT_HANDLE), answer,
sizeof(answer)/sizeof(WCHAR), &count, NULL);
2007-02-27 23:18:57 +00:00
answer[0] = toupper(answer[0]);
}
/* Return the answer */
return ((answer[0] == Ybuffer[0]) ||
(optionAll && (answer[0] == Abuffer[0])));
2007-02-27 23:18:57 +00:00
}
2007-03-04 22:34:45 +00:00
/*****************************************************************************
* WCMD_assoc
*
2007-03-13 00:06:58 +00:00
* Lists or sets file associations (assoc = TRUE)
* Lists or sets file types (assoc = FALSE)
2007-03-04 22:34:45 +00:00
*/
2007-06-03 21:07:42 +00:00
void WCMD_assoc (WCHAR *command, BOOL assoc) {
2007-03-04 22:34:45 +00:00
HKEY key;
DWORD accessOptions = KEY_READ;
2007-06-03 21:07:42 +00:00
WCHAR *newValue;
2007-03-04 22:34:45 +00:00
LONG rc = ERROR_SUCCESS;
2007-06-03 21:07:42 +00:00
WCHAR keyValue[MAXSTRING];
2007-03-04 22:34:45 +00:00
DWORD valueLen = MAXSTRING;
HKEY readKey;
2007-06-03 21:07:42 +00:00
static const WCHAR shOpCmdW[] = {'\\','S','h','e','l','l','\\',
'O','p','e','n','\\','C','o','m','m','a','n','d','\0'};
2007-03-04 22:34:45 +00:00
/* See if parameter includes '=' */
errorlevel = 0;
2007-06-03 21:07:42 +00:00
newValue = strchrW(command, '=');
2007-03-04 22:34:45 +00:00
if (newValue) accessOptions |= KEY_WRITE;
/* Open a key to HKEY_CLASSES_ROOT for enumerating */
2007-06-03 21:07:42 +00:00
if (RegOpenKeyEx(HKEY_CLASSES_ROOT, nullW, 0,
2007-03-04 22:34:45 +00:00
accessOptions, &key) != ERROR_SUCCESS) {
WINE_FIXME("Unexpected failure opening HKCR key: %d\n", GetLastError());
return;
}
2007-03-06 13:26:24 +00:00
/* If no parameters then list all associations */
2007-03-04 22:34:45 +00:00
if (*command == 0x00) {
int index = 0;
/* Enumerate all the keys */
while (rc != ERROR_NO_MORE_ITEMS) {
2007-06-03 21:07:42 +00:00
WCHAR keyName[MAXSTRING];
2007-03-04 22:34:45 +00:00
DWORD nameLen;
/* Find the next value */
nameLen = MAXSTRING;
rc = RegEnumKeyEx(key, index++,
keyName, &nameLen,
NULL, NULL, NULL, NULL);
if (rc == ERROR_SUCCESS) {
2007-03-13 00:06:58 +00:00
/* Only interested in extension ones if assoc, or others
if not assoc */
if ((keyName[0] == '.' && assoc) ||
(!(keyName[0] == '.') && (!assoc)))
{
2007-06-03 21:07:42 +00:00
WCHAR subkey[MAXSTRING];
strcpyW(subkey, keyName);
if (!assoc) strcatW(subkey, shOpCmdW);
2007-03-04 22:34:45 +00:00
2007-03-13 00:06:58 +00:00
if (RegOpenKeyEx(key, subkey, 0,
2007-03-04 22:34:45 +00:00
accessOptions, &readKey) == ERROR_SUCCESS) {
2007-06-03 21:07:42 +00:00
valueLen = sizeof(keyValue)/sizeof(WCHAR);
2007-03-04 22:34:45 +00:00
rc = RegQueryValueEx(readKey, NULL, NULL, NULL,
(LPBYTE)keyValue, &valueLen);
WCMD_output_asis(keyName);
2007-06-03 21:07:42 +00:00
WCMD_output_asis(equalW);
2007-03-04 22:34:45 +00:00
/* If no default value found, leave line empty after '=' */
if (rc == ERROR_SUCCESS) {
WCMD_output_asis(keyValue);
}
WCMD_output_asis(newline);
2007-03-04 22:34:45 +00:00
}
}
}
}
RegCloseKey(readKey);
} else {
/* Parameter supplied - if no '=' on command line, its a query */
if (newValue == NULL) {
2007-06-03 21:07:42 +00:00
WCHAR *space;
WCHAR subkey[MAXSTRING];
2007-03-04 22:34:45 +00:00
/* Query terminates the parameter at the first space */
2007-06-03 21:07:42 +00:00
strcpyW(keyValue, command);
space = strchrW(keyValue, ' ');
2007-03-04 22:34:45 +00:00
if (space) *space=0x00;
2007-03-13 00:06:58 +00:00
/* Set up key name */
2007-06-03 21:07:42 +00:00
strcpyW(subkey, keyValue);
if (!assoc) strcatW(subkey, shOpCmdW);
2007-03-13 00:06:58 +00:00
if (RegOpenKeyEx(key, subkey, 0,
2007-03-04 22:34:45 +00:00
accessOptions, &readKey) == ERROR_SUCCESS) {
rc = RegQueryValueEx(readKey, NULL, NULL, NULL,
(LPBYTE)keyValue, &valueLen);
WCMD_output_asis(command);
2007-06-03 21:07:42 +00:00
WCMD_output_asis(equalW);
2007-03-04 22:34:45 +00:00
/* If no default value found, leave line empty after '=' */
if (rc == ERROR_SUCCESS) WCMD_output_asis(keyValue);
WCMD_output_asis(newline);
2007-03-04 22:34:45 +00:00
RegCloseKey(readKey);
} else {
2007-06-03 21:07:42 +00:00
WCHAR msgbuffer[MAXSTRING];
WCHAR outbuffer[MAXSTRING];
2007-03-04 22:34:45 +00:00
/* Load the translated 'File association not found' */
2007-03-13 00:06:58 +00:00
if (assoc) {
2007-06-03 21:07:42 +00:00
LoadString (hinst, WCMD_NOASSOC, msgbuffer, sizeof(msgbuffer)/sizeof(WCHAR));
2007-03-13 00:06:58 +00:00
} else {
2007-06-03 21:07:42 +00:00
LoadString (hinst, WCMD_NOFTYPE, msgbuffer, sizeof(msgbuffer)/sizeof(WCHAR));
2007-03-13 00:06:58 +00:00
}
2007-06-03 21:07:42 +00:00
wsprintf(outbuffer, msgbuffer, keyValue);
2007-03-04 22:34:45 +00:00
WCMD_output_asis(outbuffer);
errorlevel = 2;
}
/* Not a query - its a set or clear of a value */
} else {
2007-06-03 21:07:42 +00:00
WCHAR subkey[MAXSTRING];
2007-03-13 00:06:58 +00:00
2007-03-04 22:34:45 +00:00
/* Get pointer to new value */
*newValue = 0x00;
newValue++;
2007-03-13 00:06:58 +00:00
/* Set up key name */
2007-06-03 21:07:42 +00:00
strcpyW(subkey, command);
if (!assoc) strcatW(subkey, shOpCmdW);
2007-03-13 00:06:58 +00:00
/* If nothing after '=' then clear value - only valid for ASSOC */
2007-03-04 22:34:45 +00:00
if (*newValue == 0x00) {
2007-03-13 00:06:58 +00:00
if (assoc) rc = RegDeleteKey(key, command);
if (assoc && rc == ERROR_SUCCESS) {
2007-06-03 21:07:42 +00:00
WINE_TRACE("HKCR Key '%s' deleted\n", wine_dbgstr_w(command));
2007-03-04 22:34:45 +00:00
2007-03-13 00:06:58 +00:00
} else if (assoc && rc != ERROR_FILE_NOT_FOUND) {
2007-03-04 22:34:45 +00:00
WCMD_print_error();
errorlevel = 2;
} else {
2007-06-03 21:07:42 +00:00
WCHAR msgbuffer[MAXSTRING];
WCHAR outbuffer[MAXSTRING];
2007-03-04 22:34:45 +00:00
/* Load the translated 'File association not found' */
2007-03-13 00:06:58 +00:00
if (assoc) {
2007-06-03 21:07:42 +00:00
LoadString (hinst, WCMD_NOASSOC, msgbuffer,
sizeof(msgbuffer)/sizeof(WCHAR));
2007-03-13 00:06:58 +00:00
} else {
2007-06-03 21:07:42 +00:00
LoadString (hinst, WCMD_NOFTYPE, msgbuffer,
sizeof(msgbuffer)/sizeof(WCHAR));
2007-03-13 00:06:58 +00:00
}
2007-06-03 21:07:42 +00:00
wsprintf(outbuffer, msgbuffer, keyValue);
2007-03-04 22:34:45 +00:00
WCMD_output_asis(outbuffer);
errorlevel = 2;
}
/* It really is a set value = contents */
} else {
2007-03-13 00:06:58 +00:00
rc = RegCreateKeyEx(key, subkey, 0, NULL, REG_OPTION_NON_VOLATILE,
2007-03-04 22:34:45 +00:00
accessOptions, NULL, &readKey, NULL);
if (rc == ERROR_SUCCESS) {
rc = RegSetValueEx(readKey, NULL, 0, REG_SZ,
2007-06-03 21:07:42 +00:00
(LPBYTE)newValue, strlenW(newValue));
2007-03-04 22:34:45 +00:00
RegCloseKey(readKey);
}
if (rc != ERROR_SUCCESS) {
WCMD_print_error();
errorlevel = 2;
} else {
WCMD_output_asis(command);
2007-06-03 21:07:42 +00:00
WCMD_output_asis(equalW);
2007-03-04 22:34:45 +00:00
WCMD_output_asis(newValue);
WCMD_output_asis(newline);
2007-03-04 22:34:45 +00:00
}
}
}
}
/* Clean up */
RegCloseKey(key);
}
2007-03-08 00:37:44 +00:00
/****************************************************************************
* WCMD_color
*
* Clear the terminal screen.
*/
void WCMD_color (void) {
/* Emulate by filling the screen from the top left to bottom right with
spaces, then moving the cursor to the top left afterwards */
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
2007-06-03 21:07:42 +00:00
if (param1[0] != 0x00 && strlenW(param1) > 2) {
WCMD_output (WCMD_LoadMessage(WCMD_ARGERR));
2007-03-08 00:37:44 +00:00
return;
}
if (GetConsoleScreenBufferInfo(hStdOut, &consoleInfo))
{
COORD topLeft;
DWORD screenSize;
DWORD color = 0;
screenSize = consoleInfo.dwSize.X * (consoleInfo.dwSize.Y + 1);
topLeft.X = 0;
topLeft.Y = 0;
/* Convert the color hex digits */
if (param1[0] == 0x00) {
color = defaultColor;
} else {
2007-06-03 21:07:42 +00:00
color = strtoulW(param1, NULL, 16);
2007-03-08 00:37:44 +00:00
}
/* Fail if fg == bg color */
if (((color & 0xF0) >> 4) == (color & 0x0F)) {
errorlevel = 1;
return;
}
/* Set the current screen contents and ensure all future writes
remain this color */
FillConsoleOutputAttribute(hStdOut, color, screenSize, topLeft, &screenSize);
SetConsoleTextAttribute(hStdOut, color);
}
}