Blame src/debug/ptyc_dbg_cat.c

9cf365
/*********************************************************/
9cf365
/*  ptycon: a pty-console bridge                         */
9cf365
/*  Copyright (C) 2016  Z. Gilboa                        */
9cf365
/*  Released under GPLv2 and GPLv3; see COPYING.PTYCON.  */
9cf365
/*********************************************************/
9cf365
9cf365
#include <psxtypes/psxtypes.h>
9cf365
#include <ntcon/ntcon.h>
9cf365
#include <ntapi/ntapi.h>
9cf365
9cf365
#include <ptycon/ptycon.h>
9cf365
#include "ptycon_driver_impl.h"
9cf365
9cf365
static int32_t ptyc_cat(
9cf365
	struct ptyc_driver_ctx* dctx,
9cf365
	void *			hat,
9cf365
	const char *		unit,
9cf365
	void *			hevent)
9cf365
{
9cf365
	int32_t		status;
9cf365
	void *		hfile;
9cf365
	nt_iosb		iosb;
4d974e
	uintptr_t	nread;
9cf365
	uintptr_t	buffer[32768/sizeof(uintptr_t)];
9cf365
	char *		ch;
9cf365
acafe9
	if ((status = ptyc_open_file(&hfile,hat,unit,true)))
9cf365
		return status;
9cf365
9cf365
	status = ntapi->zw_read_file(
9cf365
		hfile,
9cf365
		0,0,0,
9cf365
		&iosb,
9cf365
		buffer,sizeof(buffer),
9cf365
		0,0);
9cf365
9cf365
	while (status == NT_STATUS_SUCCESS) {
9cf365
		ch    = (char *)buffer;
9cf365
		nread = iosb.info;
9cf365
9cf365
		for ( ; nread; ) {
9cf365
			status = ntapi->pty_write(
9cf365
				dctx->cctx->hpts,
4d974e
				hevent,0,0,&iosb,
4d974e
				ch,(uint32_t)nread,
9cf365
				0,0);
9cf365
9cf365
			if (status == NT_STATUS_PENDING)
9cf365
				status = ntapi->zw_wait_for_single_object(
9cf365
					hevent,NT_SYNC_ALERTABLE,0);
9cf365
9cf365
			if (status || iosb.status) {
9cf365
				ntapi->zw_close(hfile);
9cf365
				return status ? status : iosb.status;
9cf365
			}
9cf365
9cf365
			ch    += iosb.info;
9cf365
			nread -= iosb.info;
9cf365
		}
9cf365
9cf365
		status = ntapi->zw_read_file(
9cf365
			hfile,
9cf365
			0,0,0,
9cf365
			&iosb,
9cf365
			buffer,sizeof(buffer),
9cf365
			0,0);
9cf365
	}
9cf365
9cf365
	ntapi->zw_close(hfile);
9cf365
9cf365
	return (status == NT_STATUS_END_OF_FILE)
9cf365
		? NT_STATUS_SUCCESS
9cf365
		: status;
9cf365
}
9cf365
5399d4
int __stdcall ptyc_dbg_cat(struct ptyc_driver_ctx * dctx)
9cf365
{
9cf365
	int32_t		status;
9cf365
	void *		hevent;
9cf365
	const char **	punit;
9cf365
	nt_rtdata *	rtdata;
9cf365
	nt_peb * 	peb;
9cf365
	void *		hat;
9cf365
9cf365
	if (!dctx->units[0])
9cf365
		return 0;
9cf365
9cf365
	if ((status = ntapi->tt_create_private_event(
9cf365
			&hevent,
9cf365
			NT_NOTIFICATION_EVENT,
9cf365
			NT_EVENT_NOT_SIGNALED)))
9cf365
		return status;
9cf365
9cf365
	if ((status = ntapi->tt_get_runtime_data(&rtdata,0)))
9cf365
		return status;
9cf365
9cf365
	if (!(peb = (nt_peb *)pe_get_peb_address()))
9cf365
		return NT_STATUS_INTERNAL_ERROR;
9cf365
9cf365
	if (!peb->process_params)
9cf365
		return NT_STATUS_INTERNAL_ERROR;
9cf365
9cf365
	hat = rtdata->hcwd
9cf365
		? rtdata->hcwd
9cf365
		: peb->process_params->cwd_handle;
9cf365
9cf365
	for (punit=dctx->units, status=0; *punit && !status; punit++)
9cf365
		status = ptyc_cat(
9cf365
			dctx,hat,
9cf365
			*punit,
9cf365
			hevent);
9cf365
9cf365
	ntapi->zw_close(hevent);
9cf365
	return status;
9cf365
}