Пример #1
0
VOID ExitBatch()
{
    ClearBatch();

    TRACE ("ExitBatch\n");

    UndoRedirection(bc->RedirList, NULL);
    FreeRedirection(bc->RedirList);

    /* Preserve echo state across batch calls */
    bEcho = bc->bEcho;

    while (bc->setlocal)
        cmd_endlocal(_T(""));

    bc = bc->prev;
}
Пример #2
0
BOOL
PerformRedirection(REDIRECTION *RedirList)
{
	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 */
	static const DWORD dwAccess[] = {
		GENERIC_READ, 
		GENERIC_WRITE,
		GENERIC_WRITE
	};
	static const DWORD dwShareMode[] = {
		FILE_SHARE_READ | FILE_SHARE_WRITE,
		FILE_SHARE_READ,
		FILE_SHARE_READ
	};
	static const DWORD dwCreationDisposition[] = {
		OPEN_EXISTING,
		CREATE_ALWAYS,
		OPEN_ALWAYS
	};

	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,
			                  dwAccess[Redir->Type],
			                  dwShareMode[Redir->Type],
			                  &SecAttr,
			                  dwCreationDisposition[Redir->Type],
			                  0,
			                  NULL);
		}

		if (hNew == INVALID_HANDLE_VALUE)
		{
			ConErrResPrintf(Redir->Type == REDIR_READ ? STRING_CMD_ERROR1 : STRING_CMD_ERROR3,
			                Filename);
			cmd_free(Filename);
redir_error:
			/* Undo all the redirections before this one */
			UndoRedirection(RedirList, Redir);
			return FALSE;
		}

		if (Redir->Type == 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;
}