-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathvterm_read.c
More file actions
183 lines (149 loc) · 4.65 KB
/
Copy pathvterm_read.c
File metadata and controls
183 lines (149 loc) · 4.65 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <poll.h>
#include <errno.h>
#include <limits.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include "vterm.h"
#include "vterm_private.h"
#include "vterm_render.h"
#include "vterm_reply.h"
/*
PIPE_BUF is defined as the maximum number of bytes that can be
written to a pipe atomically. On many systems, this value
is relatively low (around 4KB). We'll set our read size to
a multiple of this to prevent blocking in the child
process.
*/
// #define MAX_PIPE_READ (PIPE_BUF * 8)
ssize_t
vterm_read_pipe(vterm_t *vterm, int timeout)
{
struct pollfd fd_array[1];
char *pos;
int bytes_peek = 0;
size_t bytes_waiting = 0;
ssize_t bytes_read = 0;
ssize_t bytes_written = 0;
size_t bytes_remaining = 0;
size_t bytes_total = 0;
int retval;
pid_t child_pid;
int pid_status;
int errcpy = 0;
#ifdef _DEBUG
FILE *f_debug;
char debug_file[NAME_MAX];
#endif
// saftey precaution
if(vterm == NULL) return -1;
// make sure pty file descriptor is good
if(vterm->pty_fd < 0) return -1;
// check to see if child pid has exited
child_pid = waitpid(vterm->child_pid, &pid_status, WNOHANG);
if(child_pid == vterm->child_pid || child_pid == -1)
{
vterm->internal_state |= STATE_CHILD_EXITED;
return -1;
}
// don't use poll in async io mode
if(!(vterm->flags & VTERM_FLAG_AIO))
{
fd_array[0].fd = vterm->pty_fd;
fd_array[0].events = POLLIN;
// wait 10 millisecond for data on pty file descriptor.
retval = poll(fd_array, 1, timeout);
// no data or poll() error.
if(retval <= 0)
{
if(errno == EINTR) return 0;
return retval;
}
}
retval = 0;
bytes_peek = 0;
#ifdef TIOCINQ
retval = ioctl(vterm->pty_fd, TIOCINQ, &bytes_peek);
#else
retval = ioctl(vterm->pty_fd, FIONREAD, &bytes_peek);
#endif
if(retval == -1) return 0;
#if defined(__APPLE__) && defined(__MACH__)
/*
the FIONREAD ioctl() seems to be busted on Mac OS. the
ioctl will claim 0 bytes to read. setting it to a value
of PIPE buf and then expecting what read() actually
does is the best way to handle this.
*/
if(bytes_peek == 0) bytes_peek = PIPE_BUF;
#endif
if(bytes_peek == 0) return 0;
bytes_waiting = bytes_peek;
// see notes at top of file regarding MAX_PIPE_READ and PIPE_BUF
if(bytes_waiting > MAX_PIPE_READ) bytes_waiting = MAX_PIPE_READ;
bytes_remaining = bytes_waiting;
// 10 byte padding
// buf = (char *)calloc(bytes_waiting + 10, sizeof(char));
// pos = buf;
pos = vterm->read_buf;
do
{
bytes_read = read(vterm->pty_fd, pos, bytes_remaining);
if(bytes_read == -1)
{
// retry if interrupted by a signal
if(errno == EINTR)
{
bytes_read = 0;
continue;
}
else errcpy = errno;
}
if(bytes_read <= 0) break;
#if defined(__APPLE__) && defined(__MACH__)
// shore up bytes read (see MacOS ioctl comment above)
bytes_remaining = bytes_read;
#endif
bytes_remaining -= bytes_read;
bytes_total += bytes_read;
pos += bytes_read;
}
while(bytes_remaining > 0);
// render the data to the offscreen terminal buffer.
if((bytes_waiting > 0) && (bytes_read != -1))
{
// write debug information if enabled
if(vterm->flags & VTERM_FLAG_DUMP)
{
bytes_written = write(vterm->debug_fd,
(const void *)vterm->read_buf, bytes_read);
if( bytes_written != bytes_read )
{
fprintf(stderr,
"ERROR: wrote fewer bytes than read (%d w / %d r)\n",
(int)bytes_written, (int)bytes_read);
if(bytes_written == -1)
fprintf(stderr, "%s\n", strerror(errno));
}
}
// vterm_render(vterm, buf, bytes_read);
vterm_render(vterm, vterm->read_buf, bytes_read);
}
// release memory
// if(buf != NULL) free(buf);
if(bytes_read == -1 && errcpy != EINTR) return -1;
if(vterm->event_mask & VTERM_MASK_PIPE_READ)
{
if(vterm->event_hook != NULL)
{
vterm->event_hook(vterm,
VTERM_EVENT_PIPE_READ, (void *)&bytes_read);
}
}
return bytes_waiting - bytes_remaining;
}