Blame src/pty/ptyc_pty_ctx.c

538c8a
/*********************************************************/
538c8a
/*  ptycon: a pty-console bridge                         */
1f983d
/*  Copyright (C) 2016--2017  Z. Gilboa                  */
538c8a
/*  Released under GPLv2 and GPLv3; see COPYING.PTYCON.  */
538c8a
/*********************************************************/
538c8a
538c8a
#include <psxtypes/psxtypes.h>
538c8a
#include <ntapi/ntapi.h>
421963
#include <ntapi/nt_termios.h>
538c8a
#include <ptycon/ptycon.h>
538c8a
#include "ptycon_driver_impl.h"
538c8a
#include "ptycon_ioctl_impl.h"
538c8a
#include "ptycon_status_impl.h"
538c8a
538c8a
static int32_t ptyc_pty_open(nt_pty * hptm, nt_guid * pty_guid, nt_pty ** hpty)
538c8a
{
538c8a
	nt_vfd_dev_name		pty_name;
538c8a
	nt_oa			oa = {sizeof(oa),0,0,0,0,0};
538c8a
538c8a
	ntapi->vfd_dev_name_init(
538c8a
		&pty_name,pty_guid);
538c8a
538c8a
	oa.obj_name = &pty_name.name;
538c8a
	oa.root_dir = hptm;
538c8a
538c8a
	return ntapi->pty_open(
538c8a
		0,hpty,
538c8a
		NT_FILE_ALL_ACCESS,
538c8a
		&oa,0,0,0);
538c8a
}
538c8a
9b8e64
static int32_t ptyc_ptm_open(struct ptyc_common_ctx * cctx)
538c8a
{
538c8a
	nt_guid pty_guid = TTY_PTM_GUID;
9b8e64
538c8a
	return ptyc_pty_open(
538c8a
		0,&pty_guid,
9b8e64
		&cctx->hptm);
538c8a
}
538c8a
9b8e64
static int32_t ptyc_pts_open(struct ptyc_common_ctx * cctx)
538c8a
{
538c8a
	int32_t status;
538c8a
	nt_guid pty_guid = TTY_PTS_GUID;
538c8a
538c8a
	if ((status = ptyc_grant(cctx->hptm)))
538c8a
		return status;
538c8a
538c8a
	return ptyc_pty_open(
538c8a
		cctx->hptm,&pty_guid,
9b8e64
		&cctx->hpts);
538c8a
}
538c8a
421963
static int32_t ptyc_make_raw(nt_pty * hpty)
421963
{
421963
	int32_t			status;
421963
	nt_iosb			iosb;
421963
	nt_tty_sigctl_info	ctlinfo;
421963
	struct tty_termios	termios;
421963
421963
	ntapi->tt_aligned_block_memset(
421963
		&ctlinfo,0,sizeof(ctlinfo));
421963
421963
	if ((status = ntapi->pty_ioctl(
421963
			hpty,
421963
			0,0,0,
421963
			&iosb,
421963
			TTY_TCGETS,
421963
			&ctlinfo,sizeof(ctlinfo),
421963
			&ctlinfo,sizeof(ctlinfo))))
421963
		return status;
421963
421963
	ntapi->tt_generic_memcpy(
421963
		&termios,
421963
		&ctlinfo.terminfo,
421963
		sizeof(termios));
421963
421963
	termios.c_oflag &= ~TTY_OPOST;
421963
421963
	termios.c_cflag &= ~(TTY_CSIZE | TTY_PARENB);
421963
	termios.c_cflag |= TTY_CS8;
421963
421963
	termios.c_lflag &= ~(TTY_ECHO
421963
				| TTY_ECHONL
421963
				| TTY_ICANON
421963
				| TTY_ISIG
421963
				| TTY_IEXTEN);
421963
421963
	termios.c_iflag &= ~(TTY_IGNBRK
421963
				| TTY_BRKINT
421963
				| TTY_PARMRK
421963
				| TTY_ISTRIP
421963
				| TTY_INLCR
421963
				| TTY_IGNCR
421963
				| TTY_ICRNL
421963
				| TTY_IXON);
421963
421963
	ntapi->tt_aligned_block_memset(
421963
		&ctlinfo,0,sizeof(ctlinfo));
421963
421963
	ntapi->tt_generic_memcpy(
421963
		&ctlinfo.terminfo,
421963
		&termios,
421963
		sizeof(termios));
421963
421963
	return ntapi->pty_ioctl(
421963
		hpty,
421963
		0,0,0,
421963
		&iosb,
421963
		TTY_TCSETS,
421963
		&ctlinfo,sizeof(ctlinfo),
421963
		&ctlinfo,sizeof(ctlinfo));
421963
}
421963
e4a335
static int32_t ptyc_make_oven(nt_pty * hpty)
e4a335
{
e4a335
	int32_t			status;
e4a335
	nt_iosb			iosb;
e4a335
	nt_tty_sigctl_info	ctlinfo;
e4a335
	struct tty_termios	termios;
e4a335
e4a335
	ntapi->tt_aligned_block_memset(
e4a335
		&ctlinfo,0,sizeof(ctlinfo));
e4a335
e4a335
	if ((status = ntapi->pty_ioctl(
e4a335
			hpty,
e4a335
			0,0,0,
e4a335
			&iosb,
e4a335
			TTY_TCGETS,
e4a335
			&ctlinfo,sizeof(ctlinfo),
e4a335
			&ctlinfo,sizeof(ctlinfo))))
e4a335
		return status;
e4a335
e4a335
	ntapi->tt_generic_memcpy(
e4a335
		&termios,
e4a335
		&ctlinfo.terminfo,
e4a335
		sizeof(termios));
e4a335
e4a335
	termios.c_cflag = TTY_CBAUD | TTY_CREAD | TTY_CS8 | TTY_HUPCL;
e4a335
	termios.c_iflag = TTY_ICRNL | TTY_BRKINT | TTY_IXON;
e4a335
	termios.c_lflag = TTY_ECHO
e4a335
				| TTY_ECHOCTL
e4a335
				| TTY_ECHOE
e4a335
				| TTY_ECHOK
e4a335
				| TTY_ECHOKE
e4a335
				| TTY_ICANON
e4a335
				| TTY_ISIG
e4a335
				| TTY_IEXTEN;
e4a335
e4a335
	termios.c_oflag = TTY_OPOST
e4a335
				| TTY_ONLCR
e4a335
				| TTY_TAB0
e4a335
				| TTY_VT0
e4a335
				| TTY_BS0
e4a335
				| TTY_CR0
e4a335
				| TTY_FF0
e4a335
				| TTY_NL0;
e4a335
e4a335
	ntapi->tt_aligned_block_memset(
e4a335
		&ctlinfo,0,sizeof(ctlinfo));
e4a335
e4a335
	ntapi->tt_generic_memcpy(
e4a335
		&ctlinfo.terminfo,
e4a335
		&termios,
e4a335
		sizeof(termios));
e4a335
e4a335
	return ntapi->pty_ioctl(
e4a335
		hpty,
e4a335
		0,0,0,
e4a335
		&iosb,
e4a335
		TTY_TCSETS,
e4a335
		&ctlinfo,sizeof(ctlinfo),
e4a335
		&ctlinfo,sizeof(ctlinfo));
e4a335
}
e4a335
538c8a
int ptyc_alloc_pty(struct ptyc_driver_ctx * dctx)
538c8a
{
9b8e64
	int32_t				status;
9b8e64
	struct ptyc_driver_ctx_impl *	ictx;
9b8e64
	struct ptyc_common_ctx *	cctx;
9b8e64
9b8e64
	if (!(ictx = ptyc_get_driver_ictx(dctx)))
9b8e64
		return NT_STATUS_INVALID_HANDLE;
9b8e64
9b8e64
	cctx = &ictx->cctx;
538c8a
9b8e64
	if (cctx->hpts || cctx->hptm)
538c8a
		return ptyc_set_status(
538c8a
			dctx,NT_STATUS_DEVICE_ALREADY_ATTACHED);
538c8a
9b8e64
	if ((status = ptyc_ptm_open(cctx)))
538c8a
		return ptyc_set_status(
538c8a
			dctx,status);
538c8a
421963
	if ((status = ptyc_make_raw(cctx->hptm)))
421963
		return ptyc_set_status(
421963
			dctx,status);
421963
9b8e64
	if ((status = ptyc_pts_open(cctx)))
538c8a
		return ptyc_set_status(
538c8a
			dctx,status);
538c8a
d918be
	if ((status = ptyc_set_client_info(cctx->hpts,&ictx->clctx.clinfo)))
d918be
		return ptyc_set_status(
d918be
			dctx,status);
d918be
e4a335
	if (cctx->drvflags & PTYC_DRIVER_DBG_RAW)
e4a335
		if ((status = ptyc_make_raw(cctx->hpts)))
e4a335
			return ptyc_set_status(
e4a335
				dctx,status);
e4a335
e4a335
	if (cctx->drvflags & PTYC_DRIVER_DBG_OVEN)
e4a335
		if ((status = ptyc_make_oven(cctx->hpts)))
e4a335
			return ptyc_set_status(
e4a335
				dctx,status);
e4a335
48271e
	if (cctx->drvflags & (PTYC_DRIVER_DBG_RAW|PTYC_DRIVER_DBG_OVEN))
48271e
		if ((status = ptyc_pty_own(cctx->hpts,ictx->rtdata)))
48271e
			return ptyc_set_status(
48271e
				dctx,status);
48271e
538c8a
	return ptyc_set_status(
538c8a
		dctx,NT_STATUS_SUCCESS);
538c8a
}
538c8a
538c8a
void ptyc_free_pty(struct ptyc_driver_ctx * dctx)
538c8a
{
9b8e64
	struct ptyc_driver_ctx_impl * ictx;
538c8a
9b8e64
	if (!(ictx = ptyc_get_driver_ictx(dctx)))
9b8e64
		return;
538c8a
9b8e64
	if (ictx->cctx.hpts) {
9b8e64
		ntapi->pty_close(ictx->cctx.hpts);
9b8e64
		ictx->cctx.hpts = 0;
538c8a
	}
538c8a
9b8e64
	if (ictx->cctx.hptm) {
9b8e64
		ntapi->pty_close(ictx->cctx.hptm);
9b8e64
		ictx->cctx.hptm = 0;
538c8a
	}
538c8a
}