2006-02-16 23:23:37 +00:00
|
|
|
/*
|
|
|
|
* REDIR.C - redirection handling.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* History:
|
|
|
|
*
|
|
|
|
* 12/15/95 (Tim Norman)
|
|
|
|
* started.
|
|
|
|
*
|
|
|
|
* 12 Jul 98 (Hans B Pufal)
|
|
|
|
* Rewrote to make more efficient and to conform to new command.c
|
|
|
|
* and batch.c processing.
|
|
|
|
*
|
|
|
|
* 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
|
|
|
|
* Added config.h include
|
|
|
|
*
|
2007-05-05 11:32:25 +00:00
|
|
|
* 22-Jan-1999 (Eric Kohl)
|
2006-02-16 23:23:37 +00:00
|
|
|
* Unicode safe!
|
|
|
|
* Added new error redirection "2>" and "2>>".
|
|
|
|
*
|
2007-05-05 11:32:25 +00:00
|
|
|
* 26-Jan-1999 (Eric Kohl)
|
2006-02-16 23:23:37 +00:00
|
|
|
* Added new error AND output redirection "&>" and "&>>".
|
|
|
|
*
|
|
|
|
* 24-Jun-2005 (Brandon Turner <turnerb7@msu.edu>)
|
|
|
|
* simple check to fix > and | bug with 'rem'
|
|
|
|
*/
|
|
|
|
|
2013-01-24 23:00:42 +00:00
|
|
|
#include "precomp.h"
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
#ifdef FEATURE_REDIRECTION
|
|
|
|
|
2017-10-01 16:30:39 +00:00
|
|
|
/*
|
|
|
|
* CMD allows redirection of handles numbered 3-9 even though
|
|
|
|
* these don't correspond to any STD_ constant.
|
|
|
|
*/
|
|
|
|
static const PCON_STREAM StdStreams[] = { StdIn, StdOut, StdErr };
|
|
|
|
static HANDLE ExtraHandles[10 - 3]; // 3 == ARRAYSIZE(StdStreams)
|
2008-08-21 15:33:59 +00:00
|
|
|
|
2017-10-02 17:03:01 +00:00
|
|
|
HANDLE GetHandle(UINT Number)
|
2008-08-21 15:33:59 +00:00
|
|
|
{
|
2013-06-29 23:50:13 +00:00
|
|
|
if (Number < 3)
|
2017-10-01 16:30:39 +00:00
|
|
|
return ConStreamGetOSHandle(StdStreams[Number]);
|
|
|
|
// return GetStdHandle(STD_INPUT_HANDLE - Number);
|
2017-10-02 17:03:01 +00:00
|
|
|
else if (Number < ARRAYSIZE(ExtraHandles) + 3)
|
2013-06-29 23:50:13 +00:00
|
|
|
return ExtraHandles[Number - 3];
|
2017-10-02 17:03:01 +00:00
|
|
|
else
|
|
|
|
return INVALID_HANDLE_VALUE;
|
2008-08-21 15:33:59 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 17:03:01 +00:00
|
|
|
VOID SetHandle(UINT Number, HANDLE Handle)
|
2008-08-21 15:33:59 +00:00
|
|
|
{
|
2013-06-29 23:50:13 +00:00
|
|
|
if (Number < 3)
|
2017-10-01 16:30:39 +00:00
|
|
|
{
|
|
|
|
ConStreamSetOSHandle(StdStreams[Number], Handle);
|
|
|
|
/* Synchronize the associated Win32 handle */
|
2013-06-29 23:50:13 +00:00
|
|
|
SetStdHandle(STD_INPUT_HANDLE - Number, Handle);
|
2017-10-01 16:30:39 +00:00
|
|
|
}
|
2017-10-02 17:03:01 +00:00
|
|
|
else if (Number < ARRAYSIZE(ExtraHandles) + 3)
|
2013-06-29 23:50:13 +00:00
|
|
|
ExtraHandles[Number - 3] = Handle;
|
2008-08-21 15:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
PerformRedirection(REDIRECTION *RedirList)
|
|
|
|
{
|
2013-06-29 23:50:13 +00:00
|
|
|
REDIRECTION *Redir;
|
|
|
|
LPTSTR Filename;
|
|
|
|
HANDLE hNew;
|
|
|
|
UINT DupNumber;
|
|
|
|
|
|
|
|
static SECURITY_ATTRIBUTES SecAttr = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
|
|
|
|
|
|
|
|
/* Some parameters used for read, write, and append, respectively */
|
2013-07-01 00:07:39 +00:00
|
|
|
static struct REDIR_PARAMS
|
2013-06-29 23:50:13 +00:00
|
|
|
{
|
|
|
|
DWORD dwDesiredAccess;
|
|
|
|
DWORD dwShareMode;
|
|
|
|
DWORD dwCreationDisposition;
|
|
|
|
} RedirParams[] =
|
|
|
|
{
|
|
|
|
{GENERIC_READ , FILE_SHARE_READ | FILE_SHARE_WRITE, OPEN_EXISTING}, // REDIR_READ
|
|
|
|
{GENERIC_WRITE, FILE_SHARE_READ , CREATE_ALWAYS}, // REDIR_WRITE
|
|
|
|
{GENERIC_WRITE, FILE_SHARE_READ , OPEN_ALWAYS } // REDIR_APPEND
|
|
|
|
};
|
|
|
|
|
|
|
|
for (Redir = RedirList; Redir; Redir = Redir->Next)
|
|
|
|
{
|
|
|
|
Filename = DoDelayedExpansion(Redir->Filename);
|
|
|
|
if (!Filename)
|
|
|
|
goto redir_error;
|
|
|
|
StripQuotes(Filename);
|
|
|
|
|
|
|
|
if (*Filename == _T('&'))
|
|
|
|
{
|
|
|
|
DupNumber = Filename[1] - _T('0');
|
|
|
|
if (DupNumber >= 10 ||
|
|
|
|
!DuplicateHandle(GetCurrentProcess(),
|
|
|
|
GetHandle(DupNumber),
|
|
|
|
GetCurrentProcess(),
|
|
|
|
&hNew,
|
|
|
|
0,
|
|
|
|
TRUE,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
hNew = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hNew = CreateFile(Filename,
|
|
|
|
RedirParams[Redir->Mode].dwDesiredAccess,
|
|
|
|
RedirParams[Redir->Mode].dwShareMode,
|
|
|
|
&SecAttr,
|
|
|
|
RedirParams[Redir->Mode].dwCreationDisposition,
|
|
|
|
0,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hNew == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
/* TODO: Print a more detailed message */
|
|
|
|
ConErrResPrintf(Redir->Mode == REDIR_READ ? STRING_CMD_ERROR1 : STRING_CMD_ERROR3,
|
|
|
|
Filename);
|
|
|
|
cmd_free(Filename);
|
2009-02-14 01:13:17 +00:00
|
|
|
redir_error:
|
2013-06-29 23:50:13 +00:00
|
|
|
/* Undo all the redirections before this one */
|
|
|
|
UndoRedirection(RedirList, Redir);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Redir->Mode == REDIR_APPEND)
|
|
|
|
SetFilePointer(hNew, 0, NULL, FILE_END);
|
|
|
|
Redir->OldHandle = GetHandle(Redir->Number);
|
|
|
|
SetHandle(Redir->Number, hNew);
|
|
|
|
|
|
|
|
TRACE("%d redirected to: %s\n", Redir->Number, debugstr_aw(Filename));
|
|
|
|
cmd_free(Filename);
|
|
|
|
}
|
|
|
|
return TRUE;
|
2008-08-21 15:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
UndoRedirection(REDIRECTION *Redir, REDIRECTION *End)
|
|
|
|
{
|
2013-06-29 23:50:13 +00:00
|
|
|
for (; Redir != End; Redir = Redir->Next)
|
|
|
|
{
|
|
|
|
CloseHandle(GetHandle(Redir->Number));
|
|
|
|
SetHandle(Redir->Number, Redir->OldHandle);
|
|
|
|
Redir->OldHandle = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
2008-08-21 15:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
FreeRedirection(REDIRECTION *Redir)
|
|
|
|
{
|
2013-06-29 23:50:13 +00:00
|
|
|
REDIRECTION *Next;
|
|
|
|
for (; Redir; Redir = Next)
|
|
|
|
{
|
|
|
|
Next = Redir->Next;
|
|
|
|
ASSERT(Redir->OldHandle == INVALID_HANDLE_VALUE);
|
|
|
|
cmd_free(Redir);
|
|
|
|
}
|
2008-08-21 15:33:59 +00:00
|
|
|
}
|
|
|
|
|
2006-02-16 23:23:37 +00:00
|
|
|
#endif /* FEATURE_REDIRECTION */
|