Skip to content

Commit f231ddd

Browse files
committed
- Implemented output paging
1 parent daddb7a commit f231ddd

File tree

7 files changed

+103
-6
lines changed

7 files changed

+103
-6
lines changed

Diff for: sapi/phpdbg/phpdbg.c

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ static inline void php_phpdbg_globals_ctor(zend_phpdbg_globals *pg) /* {{{ */
8181
pg->colors[1] = NULL;
8282
pg->colors[2] = NULL;
8383

84+
pg->lines = phpdbg_get_terminal_height();
8485
pg->exec = NULL;
8586
pg->exec_len = 0;
8687
pg->buffer = NULL;

Diff for: sapi/phpdbg/phpdbg.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -195,17 +195,19 @@ int phpdbg_do_parse(phpdbg_param_t *stack, char *input);
195195

196196
#define PHPDBG_DISCARD_OUTPUT (1ULL<<35)
197197

198+
#define PHPDBG_HAS_PAGINATION (1ULL<<36)
199+
198200
#define PHPDBG_SEEK_MASK (PHPDBG_IN_UNTIL | PHPDBG_IN_FINISH | PHPDBG_IN_LEAVE)
199201
#define PHPDBG_BP_RESOLVE_MASK (PHPDBG_HAS_FUNCTION_OPLINE_BP | PHPDBG_HAS_METHOD_OPLINE_BP | PHPDBG_HAS_FILE_OPLINE_BP)
200202
#define PHPDBG_BP_MASK (PHPDBG_HAS_FILE_BP | PHPDBG_HAS_SYM_BP | PHPDBG_HAS_METHOD_BP | PHPDBG_HAS_OPLINE_BP | PHPDBG_HAS_COND_BP | PHPDBG_HAS_OPCODE_BP | PHPDBG_HAS_FUNCTION_OPLINE_BP | PHPDBG_HAS_METHOD_OPLINE_BP | PHPDBG_HAS_FILE_OPLINE_BP)
201203
#define PHPDBG_IS_STOPPING (PHPDBG_IS_QUITTING | PHPDBG_IS_CLEANING)
202204

203-
#define PHPDBG_PRESERVE_FLAGS_MASK (PHPDBG_SHOW_REFCOUNTS | PHPDBG_IS_STEPONEVAL | PHPDBG_IS_BP_ENABLED | PHPDBG_STEP_OPCODE | PHPDBG_IS_QUIET | PHPDBG_IS_COLOURED | PHPDBG_IS_REMOTE | PHPDBG_WRITE_XML | PHPDBG_IS_DISCONNECTED)
205+
#define PHPDBG_PRESERVE_FLAGS_MASK (PHPDBG_SHOW_REFCOUNTS | PHPDBG_IS_STEPONEVAL | PHPDBG_IS_BP_ENABLED | PHPDBG_STEP_OPCODE | PHPDBG_IS_QUIET | PHPDBG_IS_COLOURED | PHPDBG_IS_REMOTE | PHPDBG_WRITE_XML | PHPDBG_IS_DISCONNECTED | PHPDBG_HAS_PAGINATION)
204206

205207
#ifndef _WIN32
206-
# define PHPDBG_DEFAULT_FLAGS (PHPDBG_IS_QUIET | PHPDBG_IS_COLOURED | PHPDBG_IS_BP_ENABLED)
208+
# define PHPDBG_DEFAULT_FLAGS (PHPDBG_IS_QUIET | PHPDBG_IS_COLOURED | PHPDBG_IS_BP_ENABLED | PHPDBG_HAS_PAGINATION)
207209
#else
208-
# define PHPDBG_DEFAULT_FLAGS (PHPDBG_IS_QUIET | PHPDBG_IS_BP_ENABLED)
210+
# define PHPDBG_DEFAULT_FLAGS (PHPDBG_IS_QUIET | PHPDBG_IS_BP_ENABLED | PHPDBG_HAS_PAGINATION)
209211
#endif /* }}} */
210212

211213
/* {{{ output descriptors */
@@ -314,6 +316,7 @@ ZEND_BEGIN_MODULE_GLOBALS(phpdbg)
314316
HANDLE sigio_watcher_thread; /* sigio watcher thread handle */
315317
struct win32_sigio_watcher_data swd;
316318
#endif
319+
long lines; /* max number of lines to display */
317320
ZEND_END_MODULE_GLOBALS(phpdbg) /* }}} */
318321

319322
#endif

Diff for: sapi/phpdbg/phpdbg_io.c

+35-1
Original file line numberDiff line numberDiff line change
@@ -190,16 +190,50 @@ PHPDBG_API int phpdbg_mixed_read(int sock, char *ptr, int len, int tmo) {
190190
return ret;
191191
}
192192

193+
static int phpdbg_output_pager(int sock, const char *ptr, int len) {
194+
int count = 0, bytes = 0;
195+
const char *p = ptr, *endp = ptr + len;
196+
197+
while ((p = memchr(p, '\n', endp - p))) {
198+
count++;
199+
p++;
200+
201+
if (count % PHPDBG_G(lines) == 0) {
202+
bytes += write(sock, ptr + bytes, (p - ptr) - bytes);
203+
204+
if (memchr(p, '\n', endp - p)) {
205+
int chr;
206+
printf("\r---Type <return> to continue or q <return> to quit---");
207+
chr = getchar();
208+
if (chr == 'q') {
209+
break;
210+
}
211+
printf("\r");
212+
} else break;
213+
}
214+
}
215+
if (bytes && count % PHPDBG_G(lines) != 0) {
216+
bytes += write(sock, ptr + bytes, len - bytes);
217+
} else if (!bytes) {
218+
bytes += write(sock, ptr, len);
219+
}
220+
return bytes;
221+
}
193222

194223
PHPDBG_API int phpdbg_mixed_write(int sock, const char *ptr, int len) {
195224
if (PHPDBG_G(flags) & PHPDBG_IS_REMOTE) {
196225
return phpdbg_send_bytes(sock, ptr, len);
197226
}
227+
228+
if (PHPDBG_G(flags) & PHPDBG_HAS_PAGINATION
229+
&& PHPDBG_G(io)[PHPDBG_STDOUT].fd == sock
230+
&& PHPDBG_G(lines) > 0) {
231+
return phpdbg_output_pager(sock, ptr, len);
232+
}
198233

199234
return write(sock, ptr, len);
200235
}
201236

202-
203237
PHPDBG_API int phpdbg_open_socket(const char *interface, unsigned short port) {
204238
struct addrinfo res;
205239
int fd = phpdbg_create_listenable_socket(interface, port, &res);

Diff for: sapi/phpdbg/phpdbg_set.c

+38
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
3232

3333
const phpdbg_command_t phpdbg_set_commands[] = {
3434
PHPDBG_SET_COMMAND_D(prompt, "usage: set prompt [<string>]", 'p', set_prompt, NULL, "|s", 0),
35+
PHPDBG_SET_COMMAND_D(pagination, "usage: set pagination [<on|off>]", 'P', set_pagination, NULL, "|b", PHPDBG_ASYNC_SAFE),
3536
#ifndef _WIN32
3637
PHPDBG_SET_COMMAND_D(color, "usage: set color <element> <color>", 'c', set_color, NULL, "ss", PHPDBG_ASYNC_SAFE),
3738
PHPDBG_SET_COMMAND_D(colors, "usage: set colors [<on|off>]", 'C', set_colors, NULL, "|b", PHPDBG_ASYNC_SAFE),
@@ -42,6 +43,7 @@ const phpdbg_command_t phpdbg_set_commands[] = {
4243
PHPDBG_SET_COMMAND_D(quiet, "usage: set quiet [<on|off>]", 'q', set_quiet, NULL, "|b", PHPDBG_ASYNC_SAFE),
4344
PHPDBG_SET_COMMAND_D(stepping, "usage: set stepping [<line|op>]", 's', set_stepping, NULL, "|s", PHPDBG_ASYNC_SAFE),
4445
PHPDBG_SET_COMMAND_D(refcount, "usage: set refcount [<on|off>]", 'r', set_refcount, NULL, "|b", PHPDBG_ASYNC_SAFE),
46+
PHPDBG_SET_COMMAND_D(lines, "usage: set lines [<number>]", 'l', set_lines, NULL, "|l", PHPDBG_ASYNC_SAFE),
4547
PHPDBG_END_COMMAND
4648
};
4749

@@ -56,6 +58,42 @@ PHPDBG_SET(prompt) /* {{{ */
5658
return SUCCESS;
5759
} /* }}} */
5860

61+
PHPDBG_SET(pagination) /* {{{ */
62+
{
63+
if (!param || param->type == EMPTY_PARAM) {
64+
phpdbg_writeln("setpagination", "active=\"%s\"", "Pagination %s", PHPDBG_G(flags) & PHPDBG_HAS_PAGINATION ? "on" : "off");
65+
} else switch (param->type) {
66+
case NUMERIC_PARAM: {
67+
if (param->num) {
68+
PHPDBG_G(flags) |= PHPDBG_HAS_PAGINATION;
69+
} else {
70+
PHPDBG_G(flags) &= ~PHPDBG_HAS_PAGINATION;
71+
}
72+
} break;
73+
74+
default:
75+
phpdbg_error("setpagination", "type=\"wrongargs\"", "set pagination used incorrectly: set pagination <on|off>");
76+
}
77+
78+
return SUCCESS;
79+
} /* }}} */
80+
81+
PHPDBG_SET(lines) /* {{{ */
82+
{
83+
if (!param || param->type == EMPTY_PARAM) {
84+
phpdbg_writeln("setlines", "active=\"%s\"", "Lines %ld", PHPDBG_G(lines));
85+
} else switch (param->type) {
86+
case NUMERIC_PARAM: {
87+
PHPDBG_G(lines) = param->num;
88+
} break;
89+
90+
default:
91+
phpdbg_error("setlines", "type=\"wrongargs\"", "set lines used incorrectly: set lines <number>");
92+
}
93+
94+
return SUCCESS;
95+
} /* }}} */
96+
5997
PHPDBG_SET(break) /* {{{ */
6098
{
6199
switch (param->type) {

Diff for: sapi/phpdbg/phpdbg_set.h

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ PHPDBG_SET(breaks);
3636
PHPDBG_SET(quiet);
3737
PHPDBG_SET(stepping);
3838
PHPDBG_SET(refcount);
39+
PHPDBG_SET(pagination);
40+
PHPDBG_SET(lines);
3941

4042
extern const phpdbg_command_t phpdbg_set_commands[];
4143

Diff for: sapi/phpdbg/phpdbg_utils.c

+18
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,24 @@ PHPDBG_API int phpdbg_get_terminal_width(void) /* {{{ */
351351
return columns;
352352
} /* }}} */
353353

354+
PHPDBG_API int phpdbg_get_terminal_height(void) /* {{{ */
355+
{
356+
int lines;
357+
#ifdef _WIN32
358+
CONSOLE_SCREEN_BUFFER_INFO csbi;
359+
360+
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
361+
lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
362+
#elif defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ)
363+
struct winsize w;
364+
365+
lines = ioctl(fileno(stdout), TIOCGWINSZ, &w) == 0 ? w.ws_row : 40;
366+
#else
367+
lines = 40;
368+
#endif
369+
return lines;
370+
} /* }}} */
371+
354372
PHPDBG_API void phpdbg_set_async_io(int fd) {
355373
#if !defined(_WIN32) && defined(FASYNC)
356374
int flags;

Diff for: sapi/phpdbg/phpdbg_utils.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ PHPDBG_API int phpdbg_get_element(const char *name, size_t len); /* }}} */
7373
PHPDBG_API void phpdbg_set_prompt(const char*);
7474
PHPDBG_API const char *phpdbg_get_prompt(void); /* }}} */
7575

76-
/* {{{ Console Width */
77-
PHPDBG_API int phpdbg_get_terminal_width(void); /* }}} */
76+
/* {{{ Console size */
77+
PHPDBG_API int phpdbg_get_terminal_width(void);
78+
PHPDBG_API int phpdbg_get_terminal_height(void); /* }}} */
7879

7980
PHPDBG_API void phpdbg_set_async_io(int fd);
8081

0 commit comments

Comments
 (0)