Skip to content

Commit 623888a

Browse files
committed
use php streams to handle line endings better for #81
1 parent d8ed537 commit 623888a

File tree

5 files changed

+68
-113
lines changed

5 files changed

+68
-113
lines changed

dev/phpdbg_lexer.l

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@
1212
#include <stdio.h>
1313
#include <string.h>
1414

15-
static inline int phpdbg_needs_params(phpdbg_param_t *param TSRMLS_DC) {
16-
return 0;
15+
static inline void phpdbg_append_string(phpdbg_param_t *param, const char *string, size_t length TSRMLS_DC) {
16+
if (!param->str) {
17+
param->str = malloc(length+1);
18+
} else param->str = realloc(param->str, param->len + (length+1));
19+
20+
memcpy(&param->str[param->len], string, length);
21+
param->len += length;
22+
param->str[param->len] = 0;
1723
}
1824
%}
1925

20-
%s RAW
26+
%s RAW
2127

2228
%option outfile="sapi/phpdbg/phpdbg_lexer.c" header-file="sapi/phpdbg/phpdbg_lexer.h"
2329
%option warn nodefault
@@ -44,24 +50,7 @@ ADDR 0x[a-fA-F0-9]+
4450
OPCODE ?i:ZEND_([A-Za-z])+
4551
INPUT [^\n]+
4652
%%
47-
<INITIAL>[a-zA-Z]+ {
48-
phpdbg_init_param(yylval, STR_PARAM);
49-
yylval->str = strndup(yytext, yyleng);
50-
yylval->len = yyleng;
51-
if (phpdbg_needs_params(yylval TSRMLS_CC)) {
52-
BEGIN(PARAMS);
53-
}
54-
return T_ID;
55-
}
56-
57-
<PARAMS>[0-9]+ {
58-
phpdbg_init_param(yylval, NUMERIC_PARAM);
59-
yylval->num = atoi(yytext);
60-
return T_ID;
61-
}
62-
63-
<INITIAL>
64-
{
53+
<INITIAL>{
6554
[#]{1} { return T_POUND; }
6655
[:]{2} { return T_DCOLON; }
6756
[:]{1} { return T_COLON; }
@@ -113,12 +102,14 @@ INPUT [^\n]+
113102
return T_ID;
114103
}
115104
}
105+
116106
<RAW>{INPUT} {
117107
phpdbg_init_param(yylval, STR_PARAM);
118108
yylval->str = strndup(yytext, yyleng);
119109
yylval->len = yyleng;
120110
BEGIN(INITIAL);
121111
return T_INPUT;
122112
}
113+
123114
{WS} { /* ignore whitespace */ }
124115
%%

phpdbg.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ static void php_phpdbg_destroy_registered(void *data) /* {{{ */
144144
function TSRMLS_CC);
145145
} /* }}} */
146146

147+
147148
static PHP_RINIT_FUNCTION(phpdbg) /* {{{ */
148149
{
149150
zend_hash_init(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE], 8, NULL, php_phpdbg_destroy_bp_file, 0);

phpdbg_lexer.c

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,18 @@ static yyconst flex_int16_t yy_chk[231] =
513513
#include <stdio.h>
514514
#include <string.h>
515515

516+
static inline void phpdbg_append_string(phpdbg_param_t *param, const char *string, size_t length TSRMLS_DC) {
517+
if (!param->str) {
518+
param->str = malloc(length+1);
519+
} else param->str = realloc(param->str, param->len + (length+1));
520+
521+
memcpy(&param->str[param->len], string, length);
522+
param->len += length;
523+
param->str[param->len] = 0;
524+
}
525+
516526
#define YY_NO_UNISTD_H 1
517-
#line 518 "sapi/phpdbg/phpdbg_lexer.c"
527+
#line 528 "sapi/phpdbg/phpdbg_lexer.c"
518528

519529
#define INITIAL 0
520530
#define RAW 1
@@ -752,9 +762,9 @@ YY_DECL
752762
register int yy_act;
753763
struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
754764

755-
#line 42 "sapi/phpdbg/dev/phpdbg_lexer.l"
765+
#line 52 "sapi/phpdbg/dev/phpdbg_lexer.l"
756766

757-
#line 758 "sapi/phpdbg/phpdbg_lexer.c"
767+
#line 768 "sapi/phpdbg/phpdbg_lexer.c"
758768

759769
yylval = yylval_param;
760770

@@ -837,22 +847,22 @@ YY_DECL
837847

838848
case 1:
839849
YY_RULE_SETUP
840-
#line 45 "sapi/phpdbg/dev/phpdbg_lexer.l"
850+
#line 54 "sapi/phpdbg/dev/phpdbg_lexer.l"
841851
{ return T_POUND; }
842852
YY_BREAK
843853
case 2:
844854
YY_RULE_SETUP
845-
#line 46 "sapi/phpdbg/dev/phpdbg_lexer.l"
855+
#line 55 "sapi/phpdbg/dev/phpdbg_lexer.l"
846856
{ return T_DCOLON; }
847857
YY_BREAK
848858
case 3:
849859
YY_RULE_SETUP
850-
#line 47 "sapi/phpdbg/dev/phpdbg_lexer.l"
860+
#line 56 "sapi/phpdbg/dev/phpdbg_lexer.l"
851861
{ return T_COLON; }
852862
YY_BREAK
853863
case 4:
854864
YY_RULE_SETUP
855-
#line 48 "sapi/phpdbg/dev/phpdbg_lexer.l"
865+
#line 57 "sapi/phpdbg/dev/phpdbg_lexer.l"
856866
{
857867
BEGIN(RAW);
858868
phpdbg_init_param(yylval, EMPTY_PARAM);
@@ -861,7 +871,7 @@ YY_RULE_SETUP
861871
YY_BREAK
862872
case 5:
863873
YY_RULE_SETUP
864-
#line 53 "sapi/phpdbg/dev/phpdbg_lexer.l"
874+
#line 62 "sapi/phpdbg/dev/phpdbg_lexer.l"
865875
{
866876
BEGIN(RAW);
867877
phpdbg_init_param(yylval, EMPTY_PARAM);
@@ -870,7 +880,7 @@ YY_RULE_SETUP
870880
YY_BREAK
871881
case 6:
872882
YY_RULE_SETUP
873-
#line 58 "sapi/phpdbg/dev/phpdbg_lexer.l"
883+
#line 67 "sapi/phpdbg/dev/phpdbg_lexer.l"
874884
{
875885
BEGIN(RAW);
876886
phpdbg_init_param(yylval, EMPTY_PARAM);
@@ -879,7 +889,7 @@ YY_RULE_SETUP
879889
YY_BREAK
880890
case 7:
881891
YY_RULE_SETUP
882-
#line 63 "sapi/phpdbg/dev/phpdbg_lexer.l"
892+
#line 72 "sapi/phpdbg/dev/phpdbg_lexer.l"
883893
{
884894
phpdbg_init_param(yylval, NUMERIC_PARAM);
885895
yylval->num = 1;
@@ -888,7 +898,7 @@ YY_RULE_SETUP
888898
YY_BREAK
889899
case 8:
890900
YY_RULE_SETUP
891-
#line 68 "sapi/phpdbg/dev/phpdbg_lexer.l"
901+
#line 77 "sapi/phpdbg/dev/phpdbg_lexer.l"
892902
{
893903
phpdbg_init_param(yylval, NUMERIC_PARAM);
894904
yylval->num = 0;
@@ -897,7 +907,7 @@ YY_RULE_SETUP
897907
YY_BREAK
898908
case 9:
899909
YY_RULE_SETUP
900-
#line 73 "sapi/phpdbg/dev/phpdbg_lexer.l"
910+
#line 82 "sapi/phpdbg/dev/phpdbg_lexer.l"
901911
{
902912
phpdbg_init_param(yylval, NUMERIC_PARAM);
903913
yylval->num = atoi(yytext);
@@ -906,7 +916,7 @@ YY_RULE_SETUP
906916
YY_BREAK
907917
case 10:
908918
YY_RULE_SETUP
909-
#line 78 "sapi/phpdbg/dev/phpdbg_lexer.l"
919+
#line 87 "sapi/phpdbg/dev/phpdbg_lexer.l"
910920
{
911921
phpdbg_init_param(yylval, ADDR_PARAM);
912922
yylval->addr = strtoul(yytext, NULL, 10);
@@ -915,7 +925,7 @@ YY_RULE_SETUP
915925
YY_BREAK
916926
case 11:
917927
YY_RULE_SETUP
918-
#line 83 "sapi/phpdbg/dev/phpdbg_lexer.l"
928+
#line 92 "sapi/phpdbg/dev/phpdbg_lexer.l"
919929
{
920930
phpdbg_init_param(yylval, OP_PARAM);
921931
yylval->str = strndup(yytext, yyleng);
@@ -925,7 +935,7 @@ YY_RULE_SETUP
925935
YY_BREAK
926936
case 12:
927937
YY_RULE_SETUP
928-
#line 89 "sapi/phpdbg/dev/phpdbg_lexer.l"
938+
#line 98 "sapi/phpdbg/dev/phpdbg_lexer.l"
929939
{
930940
phpdbg_init_param(yylval, STR_PARAM);
931941
yylval->str = strndup(yytext, yyleng);
@@ -936,7 +946,7 @@ YY_RULE_SETUP
936946

937947
case 13:
938948
YY_RULE_SETUP
939-
#line 96 "sapi/phpdbg/dev/phpdbg_lexer.l"
949+
#line 106 "sapi/phpdbg/dev/phpdbg_lexer.l"
940950
{
941951
phpdbg_init_param(yylval, STR_PARAM);
942952
yylval->str = strndup(yytext, yyleng);
@@ -948,15 +958,15 @@ YY_RULE_SETUP
948958
case 14:
949959
/* rule 14 can match eol */
950960
YY_RULE_SETUP
951-
#line 103 "sapi/phpdbg/dev/phpdbg_lexer.l"
961+
#line 114 "sapi/phpdbg/dev/phpdbg_lexer.l"
952962
{ /* ignore whitespace */ }
953963
YY_BREAK
954964
case 15:
955965
YY_RULE_SETUP
956-
#line 104 "sapi/phpdbg/dev/phpdbg_lexer.l"
966+
#line 115 "sapi/phpdbg/dev/phpdbg_lexer.l"
957967
YY_FATAL_ERROR( "flex scanner jammed" );
958968
YY_BREAK
959-
#line 960 "sapi/phpdbg/phpdbg_lexer.c"
969+
#line 970 "sapi/phpdbg/phpdbg_lexer.c"
960970
case YY_STATE_EOF(INITIAL):
961971
case YY_STATE_EOF(RAW):
962972
yyterminate();
@@ -2122,7 +2132,7 @@ void yyfree (void * ptr , yyscan_t yyscanner)
21222132

21232133
#define YYTABLES_NAME "yytables"
21242134

2125-
#line 104 "sapi/phpdbg/dev/phpdbg_lexer.l"
2135+
#line 115 "sapi/phpdbg/dev/phpdbg_lexer.l"
21262136
21272137

21282138

phpdbg_lexer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ extern int yylex \
339339
#undef YY_DECL
340340
#endif
341341

342-
#line 104 "sapi/phpdbg/dev/phpdbg_lexer.l"
342+
#line 115 "sapi/phpdbg/dev/phpdbg_lexer.l"
343343

344344

345345
#line 346 "sapi/phpdbg/phpdbg_lexer.h"

phpdbg_list.c

Lines changed: 24 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "phpdbg_list.h"
3131
#include "phpdbg_utils.h"
3232
#include "phpdbg_prompt.h"
33+
#include "php_streams.h"
3334

3435
ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
3536

@@ -53,6 +54,7 @@ PHPDBG_LIST(lines) /* {{{ */
5354

5455
switch (param->type) {
5556
case NUMERIC_PARAM:
57+
printf("list lines: %d\n", param->num);
5658
phpdbg_list_file(phpdbg_current_file(TSRMLS_C),
5759
(param->num < 0 ? 1 - param->num : param->num),
5860
(param->num < 0 ? param->num : 0) + zend_get_executed_lineno(TSRMLS_C),
@@ -125,97 +127,48 @@ PHPDBG_LIST(class) /* {{{ */
125127

126128
void phpdbg_list_file(const char *filename, long count, long offset, int highlight TSRMLS_DC) /* {{{ */
127129
{
128-
unsigned char *mem, *pos, *last_pos, *end_pos;
129130
struct stat st;
130-
#ifndef _WIN32
131-
int fd;
132-
#else
133-
HANDLE fd, map;
134-
#endif
135-
int all_content = (count == 0);
136-
int line = 0, displayed = 0;
137-
131+
char *opened = NULL,
132+
*mem = NULL;
133+
char buffer[8096] = {0,};
134+
size_t buflen = 0L;
135+
long line = 0;
136+
137+
php_stream *stream = NULL;
138+
138139
if (VCWD_STAT(filename, &st) == FAILURE) {
139140
phpdbg_error("Failed to stat file %s", filename);
140141
return;
141142
}
142-
143-
#ifndef _WIN32
144-
if ((fd = VCWD_OPEN(filename, O_RDONLY)) == FAILURE) {
143+
144+
stream = php_stream_open_wrapper(filename, "rb", USE_PATH, &opened);
145+
146+
if (!stream) {
145147
phpdbg_error("Failed to open file %s to list", filename);
146148
return;
147149
}
148-
149-
pos = last_pos = mem = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
150-
end_pos = mem + st.st_size;
151-
#else
152-
153-
fd = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
154-
if (fd == INVALID_HANDLE_VALUE) {
155-
phpdbg_error("Failed to open file!");
156-
return;
157-
}
158-
159-
map = CreateFileMapping(fd, NULL, PAGE_READONLY, 0, 0, NULL);
160-
if (map == NULL) {
161-
phpdbg_error("Failed to map file!");
162-
CloseHandle(fd);
163-
return;
164-
}
165-
166-
pos = last_pos = mem = (char*) MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
167-
if (mem == NULL) {
168-
phpdbg_error("Failed to map file in memory");
169-
CloseHandle(map);
170-
CloseHandle(fd);
171-
return;
172-
}
173-
end_pos = mem + st.st_size;
174-
#endif
175-
while (1) {
176-
if (pos == end_pos) {
177-
break;
178-
}
179-
180-
pos = memchr(last_pos, '\n', end_pos - last_pos);
181-
182-
if (!pos) {
183-
/* No more line breaks */
184-
pos = end_pos;
185-
}
186-
150+
151+
while ((buflen = php_stream_gets(stream, buffer, sizeof(buffer))) > 0L) {
187152
++line;
188-
153+
189154
if (!offset || offset <= line) {
190155
/* Without offset, or offset reached */
191156
if (!highlight) {
192-
phpdbg_writeln("%05u: %.*s", line, (int)(pos - last_pos), last_pos);
157+
phpdbg_write("%05u: %s", line, buffer);
193158
} else {
194159
if (highlight != line) {
195-
phpdbg_writeln(" %05u: %.*s", line, (int)(pos - last_pos), last_pos);
160+
phpdbg_write(" %05u: %s", line, buffer);
196161
} else {
197-
phpdbg_writeln(">%05u: %.*s", line, (int)(pos - last_pos), last_pos);
162+
phpdbg_write(">%05u: %s", line, buffer);
198163
}
199164
}
200-
++displayed;
201165
}
202-
203-
last_pos = pos + 1;
204-
205-
if (!all_content && displayed == count) {
206-
/* Reached max line to display */
166+
167+
if ((count + (offset-1)) == line)
207168
break;
208-
}
209169
}
210-
211-
#ifndef _WIN32
212-
munmap(mem, st.st_size);
213-
close(fd);
214-
#else
215-
UnmapViewOfFile(mem);
216-
CloseHandle(map);
217-
CloseHandle(fd);
218-
#endif
170+
171+
php_stream_close(stream);
219172
} /* }}} */
220173

221174
void phpdbg_list_function(const zend_function *fbc TSRMLS_DC) /* {{{ */

0 commit comments

Comments
 (0)