summaryrefslogtreecommitdiffhomepage
path: root/src/internal/ptycon_ntaio_impl.c
blob: b0ba88bc56a350fb922145f6ffbf3461d4dbd638 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*********************************************************/
/*  ptycon: a pty-console bridge                         */
/*  Copyright (C) 2016--2017  Z. Gilboa                  */
/*  Released under GPLv2 and GPLv3; see COPYING.PTYCON.  */
/*********************************************************/

#include <ntapi/ntapi.h>
#include <stdio.h>

extern const ntapi_vtbl * ptyc_ntapi;

typedef struct ptyc_file {
        void *  hany;
} FILE;

int ptyc_fputs(const char * str, FILE * file)
{
	int32_t			status;
	nt_runtime_data *	rtdata;
	ntapi_zw_write_file *	iofn;
	void *			hio;
	void *			hevent;
	int			fdtype;
	size_t			size;
	size_t			nbytes;
	nt_iosb			iosb;

	/* rtdata */
	if (ptyc_ntapi->tt_get_runtime_data(&rtdata,0))
		return -1;

	/* io type */
	if (file == (void *)0) {
		hio    = rtdata->hstdin;
		fdtype = rtdata->stdin_type;

	} else if (file == (void *)1) {
		hio    = rtdata->hstdout;
		fdtype = rtdata->stdout_type;

	} else if (file == (void *)2) {
		hio    = rtdata->hstderr;
		fdtype = rtdata->stderr_type;
	} else {
		return -1;
	}

	if (!hio)
		return -1;

	/* buffer */
	size   = ptyc_ntapi->tt_string_null_offset_multibyte(str);
	nbytes = size;

	/* iofn */
	iofn = (fdtype ==  NT_FILE_TYPE_PTY)
		? (ntapi_zw_write_file *)ptyc_ntapi->pty_write
		: ptyc_ntapi->zw_write_file;

	/* hevent */
	if ((status = ptyc_ntapi->tt_create_private_event(
			&hevent,
			NT_NOTIFICATION_EVENT,
			NT_EVENT_NOT_SIGNALED)))
		return -1;

	while (nbytes) {
		/* iowrite */
		status = iofn(
			hio,hevent,
			0,0,&iosb,
			(void *)str,
			(uint32_t)nbytes,0,0);

		/* wait */
		if (status == NT_STATUS_PENDING)
			status = ptyc_ntapi->zw_wait_for_single_object(
				hevent,0,0);

		/* check */
		if (status || iosb.status) {
			ptyc_ntapi->zw_close(hevent);
			return -1;
		}

		str    += iosb.info;
		nbytes -= iosb.info;
	}

	/* all done */
	ptyc_ntapi->zw_close(
		hevent);

	return (int)size;
}

int ptyc_fprintf(FILE * file, const char * fmt, ...)
{
	va_list	ap;
	char *	str;
	size_t	buffer[32768/sizeof(size_t)];

	ptyc_ntapi->tt_aligned_block_memset(
		buffer,0,sizeof(buffer));

	str = (char *)buffer;

	va_start(ap, fmt);
	ptyc_ntapi->vsnprintf(str, sizeof(buffer), fmt, ap);
	va_end(ap);

	str[sizeof(buffer)-1] = 0;

	return ptyc_fputs(str,file);
}

int ptyc_sprintf(char * str, const char * fmt, ...)
{
	int     ret;
	va_list ap;

	va_start(ap, fmt);
	ret = ptyc_ntapi->vsprintf(str, fmt, ap);
	va_end(ap);

	return ret;
}

int ptyc_snprintf(char * str, size_t n, const char * fmt, ...)
{
	int     ret;
	va_list ap;

	va_start(ap, fmt);
	ret = ptyc_ntapi->vsnprintf(str, n, fmt, ap);
	va_end(ap);

	return ret;
}

int ptyc_isatty(int fildes)
{
	nt_runtime_data * rtdata;

	if ((ptyc_ntapi->tt_get_runtime_data(&rtdata,0)))
		return 0;

	if (fildes == 0)
		return (rtdata->stdin_type == NT_FILE_TYPE_PTY);

	else if (fildes == 1)
		return (rtdata->stdout_type == NT_FILE_TYPE_PTY);

	else if (fildes == 2)
		return (rtdata->stderr_type == NT_FILE_TYPE_PTY);

	else
		return 0;
}

int ptyc_fileno(void * any)
{
	return (int)(intptr_t)any;
}

int ptyc_write(int fd, const void * buf, size_t size)
{
	int32_t			status;
	nt_runtime_data *	rtdata;
	ntapi_zw_write_file *	iofn;
	void *			hio;
	void *			hevent;
	int			fdtype;
	nt_iosb			iosb;

	/* size */
	if (size >= 0x80000000)
		return NT_STATUS_INVALID_PARAMETER;

	/* rtdata */
	if (ptyc_ntapi->tt_get_runtime_data(&rtdata,0))
		return NT_STATUS_REINITIALIZATION_NEEDED;

	/* hio, io type */
	if (fd == STDIN_FILENO) {
		hio    = rtdata->hstdin;
		fdtype = rtdata->stdin_type;

	} else if (fd == STDOUT_FILENO) {
		hio    = rtdata->hstdout;
		fdtype = rtdata->stdout_type;

	} else if (fd == STDERR_FILENO) {
		hio    = rtdata->hstderr;
		fdtype = rtdata->stderr_type;
	} else {
		return NT_STATUS_INVALID_PARAMETER;
	}

	if (!hio)
		return NT_STATUS_INVALID_HANDLE;

	/* iofn */
	iofn = (fdtype ==  NT_FILE_TYPE_PTY)
		? (ntapi_zw_write_file *)ptyc_ntapi->pty_write
		: ptyc_ntapi->zw_write_file;

	/* hevent */
	if ((status = ptyc_ntapi->tt_create_private_event(
			&hevent,
			NT_NOTIFICATION_EVENT,
			NT_EVENT_NOT_SIGNALED)))
		return status;

	/* iowrite */
	status = iofn(
		hio,hevent,
		0,0,&iosb,
		(void *)buf,size,
		0,0);

	/* wait */
	if (status == NT_STATUS_PENDING)
		status = ptyc_ntapi->zw_wait_for_single_object(
			hevent,0,0);

	/* hevent */
	ptyc_ntapi->zw_close(hevent);

	/* ret */
	return status ? status : iosb.status;
}