implemented batch redirection by saving batch info in tagBATCHCONTEXT by Brandon Turner
Modified: trunk/reactos/subsys/system/cmd/batch.c
Modified: trunk/reactos/subsys/system/cmd/batch.h
Modified: trunk/reactos/subsys/system/cmd/call.c
Modified: trunk/reactos/subsys/system/cmd/cmd.c
Modified: trunk/reactos/subsys/system/cmd/for.c

Modified: trunk/reactos/subsys/system/cmd/batch.c
--- trunk/reactos/subsys/system/cmd/batch.c	2005-08-17 07:23:05 UTC (rev 17422)
+++ trunk/reactos/subsys/system/cmd/batch.c	2005-08-17 18:01:47 UTC (rev 17423)
@@ -218,10 +218,10 @@
 BOOL Batch (LPTSTR fullname, LPTSTR firstword, LPTSTR param)
 {
 	HANDLE hFile;
-
+	SetLastError(0);
 	hFile = CreateFile (fullname, GENERIC_READ, FILE_SHARE_READ, NULL,
 			    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL |
-			    FILE_FLAG_SEQUENTIAL_SCAN, NULL);
+				 FILE_FLAG_SEQUENTIAL_SCAN, NULL);	
 
 #ifdef _DEBUG
 	DebugPrintf (_T("Batch: (\'%s\', \'%s\', \'%s\')  hFile = %x\n"),
@@ -251,6 +251,9 @@
 
 		n->prev = bc;
 		bc = n;
+		bc->In[0] = _T('\0');
+		bc->Out[0] = _T('\0');
+		bc->Err[0] = _T('\0');
 	}
 	else if (bc->hBatchFile != INVALID_HANDLE_VALUE)
 	{
@@ -261,6 +264,7 @@
 	}
 
 	bc->hBatchFile = hFile;
+	SetFilePointer (bc->hBatchFile, 0, NULL, FILE_BEGIN); 
 	bc->bEcho = bEcho; /* Preserve echo across batch calls */
 	bc->shiftlevel = 0;
 
@@ -276,7 +280,19 @@
 	return TRUE;
 }
 
+VOID AddBatchRedirection(TCHAR * ifn, TCHAR * ofn, TCHAR * efn)
+{
+	if(!bc)
+		return;
+	if(_tcslen(ifn))
+		_tcscpy(bc->In,ifn);
+	if(_tcslen(ofn))
+		_tcscpy(bc->Out,ofn);
+	if(_tcslen(efn))
+		_tcscpy(bc->Err,efn);
 
+}
+
 /*
  * Read and return the next executable line form the current batch file
  *
@@ -405,7 +421,6 @@
 
 			continue;
 		}
-
 #ifdef _DEBUG
 		DebugPrintf (_T("ReadBatchLine(): textline: \'%s\'\n"), textline);
 #endif

Modified: trunk/reactos/subsys/system/cmd/batch.h
--- trunk/reactos/subsys/system/cmd/batch.h	2005-08-17 07:23:05 UTC (rev 17422)
+++ trunk/reactos/subsys/system/cmd/batch.h	2005-08-17 18:01:47 UTC (rev 17423)
@@ -18,6 +18,9 @@
 	INT    shiftlevel;
 	BOOL   bEcho;        /* Preserve echo flag across batch calls */
 	HANDLE hFind;        /* Preserve find handle when doing a for */
+	TCHAR In[MAX_PATH];
+	TCHAR Out[MAX_PATH];
+	TCHAR Err[MAX_PATH];
 	TCHAR forvar;
 } BATCH_CONTEXT, *LPBATCH_CONTEXT;
 
@@ -39,5 +42,6 @@
 VOID   ExitBatch (LPTSTR);
 BOOL   Batch (LPTSTR, LPTSTR, LPTSTR);
 LPTSTR ReadBatchLine (LPBOOL);
+VOID AddBatchRedirection(TCHAR *, TCHAR *, TCHAR *);
 
 #endif /* _BATCH_H_INCLUDED_ */

Modified: trunk/reactos/subsys/system/cmd/call.c
--- trunk/reactos/subsys/system/cmd/call.c	2005-08-17 07:23:05 UTC (rev 17422)
+++ trunk/reactos/subsys/system/cmd/call.c	2005-08-17 18:01:47 UTC (rev 17423)
@@ -72,8 +72,20 @@
 	bc->shiftlevel = 0;
 	bc->forvar = 0;        /* HBP004 */
 	bc->forproto = NULL;   /* HBP004 */
-
 	ParseCommandLine (param);
+	if (bc->prev)
+	{
+		_tcscpy(bc->In, bc->prev->In);
+		_tcscpy(bc->Out, bc->prev->Out);
+		_tcscpy(bc->Err, bc->prev->Err);
+	}
+	else
+	{
+		bc->In[0] = _T('\0');
+		bc->Out[0] = _T('\0');
+		bc->Err[0] = _T('\0');
+	}
+	
 
 	/* Wasn't a batch file so remove conext */
 	if (bc->hBatchFile == INVALID_HANDLE_VALUE)

Modified: trunk/reactos/subsys/system/cmd/cmd.c
--- trunk/reactos/subsys/system/cmd/cmd.c	2005-08-17 07:23:05 UTC (rev 17422)
+++ trunk/reactos/subsys/system/cmd/cmd.c	2005-08-17 18:01:47 UTC (rev 17423)
@@ -603,7 +603,7 @@
 	INT  nRedirFlags = 0;
 	INT  Length;
 	UINT Attributes;
-
+	BOOL bNewBatch = TRUE;
 	HANDLE hOldConIn;
 	HANDLE hOldConOut;
 	HANDLE hOldConErr;
@@ -656,6 +656,20 @@
 		;
 	_tcscpy (err, t);
 
+	if(bc && !_tcslen (in) && _tcslen (bc->In))
+		_tcscpy(in, bc->In);
+	if(bc && !out[0] && _tcslen(bc->Out))
+	{
+		nRedirFlags |= OUTPUT_APPEND;
+		_tcscpy(out, bc->Out);
+	}
+	if(bc && !_tcslen (err) && _tcslen (bc->Err))
+	{
+		nRedirFlags |= ERROR_APPEND;
+		_tcscpy(err, bc->Err);
+	}
+		
+
 	/* Set up the initial conditions ... */
 	/* preserve STDIN, STDOUT and STDERR handles */
 	hOldConIn  = GetStdHandle (STD_INPUT_HANDLE);
@@ -675,7 +689,7 @@
 		hFile = CreateFile (in, GENERIC_READ, FILE_SHARE_READ, &sa, OPEN_EXISTING,
 		                    FILE_ATTRIBUTE_NORMAL, NULL);
 		if (hFile == INVALID_HANDLE_VALUE)
-		{      
+		{
 			LoadString(CMD_ModuleHandle, STRING_CMD_ERROR1, szMsg, RC_STRING_MAX_SIZE);
 			ConErrPrintf(szMsg, in);
 			return;
@@ -766,7 +780,7 @@
     /* we need make sure the LastError msg is zero before calling CreateFile */
 		SetLastError(0); 
 
-    hFile = CreateFile (out, GENERIC_WRITE, FILE_SHARE_READ, &sa,
+    hFile = CreateFile (out, GENERIC_WRITE, FILE_SHARE_WRITE, &sa,
 		                    (nRedirFlags & OUTPUT_APPEND) ? OPEN_ALWAYS : CREATE_ALWAYS,
 		                    FILE_ATTRIBUTE_NORMAL, NULL);
 		
@@ -782,7 +796,7 @@
       }
       
       out[size]=_T('\0');
-      hFile = CreateFile (out, GENERIC_WRITE, FILE_SHARE_READ, &sa,
+      hFile = CreateFile (out, GENERIC_WRITE, FILE_SHARE_WRITE, &sa,
 		                    (nRedirFlags & OUTPUT_APPEND) ? OPEN_ALWAYS : CREATE_ALWAYS,
 		                    FILE_ATTRIBUTE_NORMAL, NULL);
 
@@ -884,12 +898,17 @@
 			CloseHandle (hErr);
 		hOldConErr = INVALID_HANDLE_VALUE;
 	}
+
+	if(bc)
+		bNewBatch = FALSE;
 #endif
 
 	/* process final command */
 	DoCommand (s);
 
 #ifdef FEATURE_REDIRECTION
+	if(bNewBatch && bc)
+		AddBatchRedirection(in, out, err);
 	/* close old stdin file */
 #if 0  /* buggy implementation */
 	SetStdHandle (STD_INPUT_HANDLE, hOldConIn);

Modified: trunk/reactos/subsys/system/cmd/for.c
--- trunk/reactos/subsys/system/cmd/for.c	2005-08-17 07:23:05 UTC (rev 17422)
+++ trunk/reactos/subsys/system/cmd/for.c	2005-08-17 18:01:47 UTC (rev 17423)
@@ -141,7 +141,11 @@
 		bc->bEcho = bc->prev->bEcho;
 	else
 		bc->bEcho = bEcho;
+		bc->In[0] = _T('\0');
+		bc->Out[0] = _T('\0');
+		bc->Err[0] = _T('\0');
 
+
 	return 0;
 }