1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
/*********************************************************/
/* ptycon: a pty-console bridge */
/* Copyright (C) 2016--2017 SysDeer Technologies, LLC */
/* Released under GPLv2 and GPLv3; see COPYING.PTYCON. */
/*********************************************************/
#include <psxtypes/psxtypes.h>
#include <ntcon/ntcon.h>
#include <ntapi/ntapi.h>
#include <gdi/gdi.h>
#include <ptycon/ptycon.h>
#include "ptycon_driver_impl.h"
#include "ptycon_status_impl.h"
static ptyc_term_handler ptyc_screen_handler;
static ptyc_term_handler ptyc_ctrl_handler;
static ptyc_term_handler ptyc_flush_screen_buffer;
int __stdcall ptyc_console_writer(struct ptyc_driver_ctx_impl * ictx)
{
int32_t status;
nt_iosb iosb;
size_t nsteps;
void * hwait;
unsigned char * inbuf;
struct ptyc_term_ctx * tctx;
nt_unicode_conversion_params_utf8_to_utf16 uc_params;
if ((status = ntapi->tt_create_private_event(
&hwait,
NT_NOTIFICATION_EVENT,
NT_EVENT_NOT_SIGNALED)))
return status;
tctx = &ictx->tctx;
inbuf = (unsigned char *)&tctx->data.in[1];
uc_params.dst_size_in_bytes = sizeof(tctx->data.out);
uc_params.leftover_count = 0;
uc_params.leftover_bytes = 0;
tctx->handler = ptyc_screen_handler;
tctx->char_handler = ptyc_screen_handler;
tctx->ctrl_handler = ptyc_ctrl_handler;
tctx->ctrl_handlers[0] = ptyc_esi_handlers;
tctx->wch_con = tctx->data.screen;
do {
uc_params.code_points = 0;
uc_params.addr_failed = 0;
uc_params.bytes_written = 0;
status = ntapi->pty_read(
ictx->cctx.hptm,
hwait,0,0,
&iosb,
inbuf,
sizeof(tctx->data.in) - sizeof(uintptr_t),
0,0);
if (status == NT_STATUS_PENDING)
if (!(status = ntapi->zw_wait_for_single_object(
hwait,NT_SYNC_ALERTABLE,0)))
status = iosb.status;
if (status)
return ptyc_set_status(&ictx->ctx,status);
/* account for leftover bytes from the previous conversion */
tctx->data.in[0] = uc_params.leftover_bytes;
uc_params.src = inbuf - uc_params.leftover_count;
uc_params.src_size_in_bytes = iosb.info + uc_params.leftover_count;
uc_params.dst = tctx->data.out;
/* utf-8 --> utf-16 */
status = ntapi->uc_convert_unicode_stream_utf8_to_utf16(
&uc_params);
/* process the converted (utf-16) stream */
tctx->wch_pty = tctx->data.out;
nsteps = uc_params.bytes_written / sizeof(wchar16_t);
for (; nsteps; nsteps--)
tctx->handler = (ptyc_term_handler *)tctx->handler(tctx);
/* output to console */
ptyc_flush_screen_buffer(tctx);
} while (1);
return NT_STATUS_INTERNAL_ERROR;
}
static void * __fastcall ptyc_screen_handler(struct ptyc_term_ctx * tctx)
{
if (*tctx->wch_pty == '\x1b') {
/* output all pending characters */
ptyc_flush_screen_buffer(tctx);
/* advance stream pointer */
tctx->wch_pty++;
/* set initial control state */
tctx->ctrl_state = PTYC_CTRL_STATE_ESI;
tctx->ctrl_cap = PTYC_ESI_ARRAY_SIZE;
/* switch mode */
return tctx->ctrl_handler;
} else {
/* filter out bell key? */
if (*tctx->wch_pty == 0x7)
if (!(tctx->drvflags & PTYC_DRIVER_BELL))
return ptyc_screen_handler;
/* copy character to the screen buffer */
*tctx->wch_con = *tctx->wch_pty;
/* advance the screen and stream pointers */
tctx->wch_con++;
tctx->wch_pty++;
/* retain mode */
return ptyc_screen_handler;
}
}
static void * __fastcall ptyc_ctrl_handler(struct ptyc_term_ctx * tctx)
{
ptyc_term_handler * const * handlers;
ptyc_term_handler * handler;
unsigned idx;
idx = (*tctx->wch_pty < tctx->ctrl_cap)
? *tctx->wch_pty
: tctx->ctrl_cap - 1;
handlers = tctx->ctrl_handlers[tctx->ctrl_state];
handler = handlers[idx];
return handler(tctx);
}
static void * __fastcall ptyc_flush_screen_buffer(struct ptyc_term_ctx * tctx)
{
uint32_t ncunits;
uint32_t nwritten;
if (!(ncunits = (uint32_t)(tctx->wch_con - tctx->data.screen)))
return tctx->wch_con;
ntcon->write_console_utf16(
tctx->hout,
tctx->data.screen,
ncunits,
&nwritten,
0);
/* heuristics: interactive input? */
if (ncunits <= 4) {
ntcon->get_console_screen_buffer_info(
tctx->hout,
&tctx->screen_info);
ntcon->set_console_cursor_position(
tctx->hout,
tctx->screen_info.cursor_position);
}
tctx->wch_con = tctx->data.screen;
return tctx->wch_con;
}
|