ReactOS  0.4.14-dev-114-gc8cbd56
redir.c File Reference
#include "precomp.h"
Include dependency graph for redir.c:

Go to the source code of this file.

Functions

HANDLE GetHandle (UINT Number)
 
VOID SetHandle (UINT Number, HANDLE Handle)
 
BOOL PerformRedirection (REDIRECTION *RedirList)
 
VOID UndoRedirection (REDIRECTION *Redir, REDIRECTION *End)
 
VOID FreeRedirection (REDIRECTION *Redir)
 

Variables

static const PCON_STREAM StdStreams [] = { StdIn, StdOut, StdErr }
 
static HANDLE ExtraHandles [10 - 3]
 

Function Documentation

◆ FreeRedirection()

VOID FreeRedirection ( REDIRECTION Redir)

Definition at line 153 of file redir.c.

154 {
155  REDIRECTION *Next;
156  for (; Redir; Redir = Next)
157  {
158  Next = Redir->Next;
160  cmd_free(Redir);
161  }
162 }
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
HANDLE OldHandle
Definition: cmd.h:349
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define cmd_free(ptr)
Definition: cmddbg.h:31
struct _REDIRECTION * Next
Definition: cmd.h:348

Referenced by ExitBatch(), FreeCommand(), ParseBlock(), ParseCommandPart(), ParsePrimary(), and ParseRedirection().

◆ GetHandle()

HANDLE GetHandle ( UINT  Number)

Definition at line 39 of file redir.c.

40 {
41  if (Number < 3)
43  // return GetStdHandle(STD_INPUT_HANDLE - Number);
44  else if (Number < ARRAYSIZE(ExtraHandles) + 3)
45  return ExtraHandles[Number - 3];
46  else
47  return INVALID_HANDLE_VALUE;
48 }
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
HANDLE ConStreamGetOSHandle(IN PCON_STREAM Stream)
Definition: stream.c:240
static HANDLE ExtraHandles[10 - 3]
Definition: redir.c:37
static const PCON_STREAM StdStreams[]
Definition: redir.c:36
_In_opt_ PENTER_STATE_SYSTEM_HANDLER _In_opt_ PVOID _In_ LONG _In_opt_ LONG volatile * Number
Definition: ntpoapi.h:204

Referenced by PerformRedirection(), and UndoRedirection().

◆ PerformRedirection()

BOOL PerformRedirection ( REDIRECTION RedirList)

Definition at line 63 of file redir.c.

64 {
65  REDIRECTION *Redir;
67  HANDLE hNew;
68  UINT DupNumber;
69 
70  static SECURITY_ATTRIBUTES SecAttr = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
71 
72  /* Some parameters used for read, write, and append, respectively */
73  static struct REDIR_PARAMS
74  {
75  DWORD dwDesiredAccess;
76  DWORD dwShareMode;
77  DWORD dwCreationDisposition;
78  } RedirParams[] =
79  {
81  {GENERIC_WRITE, FILE_SHARE_READ , CREATE_ALWAYS}, // REDIR_WRITE
82  {GENERIC_WRITE, FILE_SHARE_READ , OPEN_ALWAYS } // REDIR_APPEND
83  };
84 
85  for (Redir = RedirList; Redir; Redir = Redir->Next)
86  {
88  if (!Filename)
89  goto redir_error;
91 
92  if (*Filename == _T('&'))
93  {
94  DupNumber = Filename[1] - _T('0');
95  if (DupNumber >= 10 ||
97  GetHandle(DupNumber),
99  &hNew,
100  0,
101  TRUE,
103  {
104  hNew = INVALID_HANDLE_VALUE;
105  }
106  }
107  else
108  {
109  hNew = CreateFile(Filename,
110  RedirParams[Redir->Mode].dwDesiredAccess,
111  RedirParams[Redir->Mode].dwShareMode,
112  &SecAttr,
113  RedirParams[Redir->Mode].dwCreationDisposition,
114  0,
115  NULL);
116  }
117 
118  if (hNew == INVALID_HANDLE_VALUE)
119  {
120  /* TODO: Print a more detailed message */
122  Filename);
124 redir_error:
125  /* Undo all the redirections before this one */
126  UndoRedirection(RedirList, Redir);
127  return FALSE;
128  }
129 
130  if (Redir->Mode == REDIR_APPEND)
131  SetFilePointer(hNew, 0, NULL, FILE_END);
132  Redir->OldHandle = GetHandle(Redir->Number);
133  SetHandle(Redir->Number, hNew);
134 
135  TRACE("%d redirected to: %s\n", Redir->Number, debugstr_aw(Filename));
137  }
138  return TRUE;
139 }
#define STRING_CMD_ERROR1
Definition: resource.h:27
#define TRUE
Definition: types.h:120
#define STRING_CMD_ERROR3
Definition: resource.h:29
#define debugstr_aw
Definition: precomp.h:43
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
static VOID StripQuotes(LPSTR in)
Definition: cmdcons.c:116
CHAR * LPTSTR
Definition: xmlstorage.h:192
IN PVCB IN PBCB OUT PDIRENT IN USHORT IN POEM_STRING Filename
Definition: fatprocs.h:925
#define FILE_SHARE_READ
Definition: compat.h:125
DWORD WINAPI DECLSPEC_HOTPATCH SetFilePointer(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod)
Definition: fileinfo.c:204
REDIR_MODE Mode
Definition: cmd.h:351
HANDLE OldHandle
Definition: cmd.h:349
#define DUPLICATE_SAME_ACCESS
#define ConErrResPrintf(uID,...)
Definition: console.h:51
#define GENERIC_WRITE
Definition: nt_native.h:90
HANDLE GetHandle(UINT Number)
Definition: redir.c:39
smooth NULL
Definition: ftsmooth.c:416
#define OPEN_EXISTING
Definition: compat.h:426
BYTE Number
Definition: cmd.h:350
#define _T(x)
Definition: vfdio.h:22
#define TRACE(s)
Definition: solgame.cpp:4
#define FILE_END
Definition: winbase.h:114
HANDLE WINAPI GetCurrentProcess(VOID)
Definition: proc.c:1138
TCHAR Filename[]
Definition: cmd.h:352
LPTSTR DoDelayedExpansion(LPTSTR Line)
Definition: cmd.c:1408
unsigned long DWORD
Definition: ntddk_ex.h:95
#define OPEN_ALWAYS
Definition: disk.h:70
#define GENERIC_READ
Definition: compat.h:124
#define CREATE_ALWAYS
Definition: disk.h:72
#define cmd_free(ptr)
Definition: cmddbg.h:31
IN OUT PVCB OUT PDIRENT OUT PBCB IN BOOLEAN CreateFile
Definition: fatprocs.h:904
VOID SetHandle(UINT Number, HANDLE Handle)
Definition: redir.c:50
unsigned int UINT
Definition: ndis.h:50
struct _REDIRECTION * Next
Definition: cmd.h:348
VOID UndoRedirection(REDIRECTION *Redir, REDIRECTION *End)
Definition: redir.c:142
BOOL WINAPI DuplicateHandle(IN HANDLE hSourceProcessHandle, IN HANDLE hSourceHandle, IN HANDLE hTargetProcessHandle, OUT LPHANDLE lpTargetHandle, IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN DWORD dwOptions)
Definition: handle.c:149
struct _SECURITY_ATTRIBUTES SECURITY_ATTRIBUTES

Referenced by ExecuteCommand().

◆ SetHandle()

VOID SetHandle ( UINT  Number,
HANDLE  Handle 
)

Definition at line 50 of file redir.c.

51 {
52  if (Number < 3)
53  {
55  /* Synchronize the associated Win32 handle */
57  }
58  else if (Number < ARRAYSIZE(ExtraHandles) + 3)
59  ExtraHandles[Number - 3] = Handle;
60 }
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
#define STD_INPUT_HANDLE
Definition: winbase.h:264
_In_ HANDLE Handle
Definition: extypes.h:390
static HANDLE ExtraHandles[10 - 3]
Definition: redir.c:37
static const PCON_STREAM StdStreams[]
Definition: redir.c:36
BOOL WINAPI DECLSPEC_HOTPATCH SetStdHandle(DWORD nStdHandle, HANDLE hHandle)
Definition: console.c:1213
BOOL ConStreamSetOSHandle(IN PCON_STREAM Stream, IN HANDLE Handle)
Definition: stream.c:263
_In_opt_ PENTER_STATE_SYSTEM_HANDLER _In_opt_ PVOID _In_ LONG _In_opt_ LONG volatile * Number
Definition: ntpoapi.h:204

Referenced by cmd_ctty(), PerformRedirection(), and UndoRedirection().

◆ UndoRedirection()

VOID UndoRedirection ( REDIRECTION Redir,
REDIRECTION End 
)

Definition at line 142 of file redir.c.

143 {
144  for (; Redir != End; Redir = Redir->Next)
145  {
146  CloseHandle(GetHandle(Redir->Number));
147  SetHandle(Redir->Number, Redir->OldHandle);
149  }
150 }
#define CloseHandle
Definition: compat.h:398
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
HANDLE OldHandle
Definition: cmd.h:349
HANDLE GetHandle(UINT Number)
Definition: redir.c:39
BYTE Number
Definition: cmd.h:350
VOID SetHandle(UINT Number, HANDLE Handle)
Definition: redir.c:50
struct _REDIRECTION * Next
Definition: cmd.h:348

Referenced by ExecuteCommand(), ExitBatch(), and PerformRedirection().

Variable Documentation

◆ ExtraHandles

HANDLE ExtraHandles[10 - 3]
static

Definition at line 37 of file redir.c.

Referenced by GetHandle(), and SetHandle().

◆ StdStreams

const PCON_STREAM StdStreams[] = { StdIn, StdOut, StdErr }
static

Definition at line 36 of file redir.c.

Referenced by GetHandle(), and SetHandle().