- Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathphpdbg_io.c
121 lines (101 loc) · 3.25 KB
/
phpdbg_io.c
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
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Anatol Belski <ab@php.net> |
+----------------------------------------------------------------------+
*/
#ifdefHAVE_CONFIG_H
#include<config.h>
#endif
#include"phpdbg_io.h"
ZEND_EXTERN_MODULE_GLOBALS(phpdbg)
/* is easy to generalize ... but not needed for now */
PHPDBG_APIintphpdbg_consume_stdin_line(char*buf) {
intbytes=PHPDBG_G(input_buflen), len=0;
if (PHPDBG_G(input_buflen)) {
memcpy(buf, PHPDBG_G(input_buffer), bytes);
}
PHPDBG_G(last_was_newline) =1;
do {
inti;
if (bytes <= 0) {
continue;
}
for (i=len; i<len+bytes; i++) {
if (buf[i] =='\x03') {
if (i!=len+bytes-1) {
memmove(buf+i, buf+i+1, len+bytes-i-1);
}
len--;
i--;
continue;
}
if (buf[i] =='\n') {
PHPDBG_G(input_buflen) =len+bytes-1-i;
if (PHPDBG_G(input_buflen)) {
memcpy(PHPDBG_G(input_buffer), buf+i+1, PHPDBG_G(input_buflen));
}
if (i!=PHPDBG_MAX_CMD-1) {
buf[i+1] =0;
}
returni;
}
}
len+=bytes;
} while ((bytes=phpdbg_mixed_read(PHPDBG_G(io)[PHPDBG_STDIN].fd, buf+len, PHPDBG_MAX_CMD-len, -1)) >0);
if (bytes <= 0) {
PHPDBG_G(flags) |= PHPDBG_IS_QUITTING;
zend_bailout();
}
returnbytes;
}
PHPDBG_APIintphpdbg_mixed_read(intfd, char*ptr, intlen, inttmo) {
intret;
do {
ret=read(fd, ptr, len);
} while (ret==-1&&errno==EINTR);
returnret;
}
staticintphpdbg_output_pager(intfd, constchar*ptr, intlen) {
intcount=0, bytes=0;
constchar*p=ptr, *endp=ptr+len;
while ((p=memchr(p, '\n', endp-p))) {
count++;
p++;
if (count % PHPDBG_G(lines) ==0) {
bytes+=write(fd, ptr+bytes, (p-ptr) -bytes);
if (memchr(p, '\n', endp-p)) {
charbuf[PHPDBG_MAX_CMD];
zend_quiet_write(fd, ZEND_STRL("\r---Type <return> to continue or q <return> to quit---"));
phpdbg_consume_stdin_line(buf);
if (*buf=='q') {
break;
}
zend_quiet_write(fd, "\r", 1);
} elsebreak;
}
}
if (bytes&&count % PHPDBG_G(lines) !=0) {
bytes+=write(fd, ptr+bytes, len-bytes);
} elseif (!bytes) {
bytes+=write(fd, ptr, len);
}
returnbytes;
}
PHPDBG_APIintphpdbg_mixed_write(intfd, constchar*ptr, intlen) {
if ((PHPDBG_G(flags) &PHPDBG_HAS_PAGINATION)
&&PHPDBG_G(io)[PHPDBG_STDOUT].fd==fd
&&PHPDBG_G(lines) >0) {
returnphpdbg_output_pager(fd, ptr, len);
}
returnwrite(fd, ptr, len);
}