]> granicus.if.org Git - php/commitdiff
use php streams to handle line endings better for #81
authorkrakjoe <joe.watkins@live.co.uk>
Thu, 27 Feb 2014 19:10:59 +0000 (19:10 +0000)
committerkrakjoe <joe.watkins@live.co.uk>
Thu, 27 Feb 2014 19:10:59 +0000 (19:10 +0000)
dev/phpdbg_lexer.l
phpdbg.c
phpdbg_lexer.c
phpdbg_lexer.h
phpdbg_list.c

index d371e59c599cac6801d878f0ec63e0da8105b967..a50360d2499a46d59579e033c735c95b23538cb3 100644 (file)
 #include <stdio.h>
 #include <string.h>
 
-static inline int phpdbg_needs_params(phpdbg_param_t *param TSRMLS_DC) {
-       return 0;
+static inline void phpdbg_append_string(phpdbg_param_t *param, const char *string, size_t length TSRMLS_DC) {
+       if (!param->str) {
+               param->str = malloc(length+1);
+       } else param->str = realloc(param->str, param->len + (length+1));
+       
+       memcpy(&param->str[param->len], string, length);
+       param->len += length;
+       param->str[param->len] = 0;
 }
 %}
 
-%s RAW
+%s RAW 
 
 %option outfile="sapi/phpdbg/phpdbg_lexer.c" header-file="sapi/phpdbg/phpdbg_lexer.h"
 %option warn nodefault
@@ -44,24 +50,7 @@ ADDR                         0x[a-fA-F0-9]+
 OPCODE                         ?i:ZEND_([A-Za-z])+
 INPUT                          [^\n]+
 %%
-<INITIAL>[a-zA-Z]+     {
-       phpdbg_init_param(yylval, STR_PARAM);
-       yylval->str = strndup(yytext, yyleng);
-       yylval->len = yyleng;
-       if (phpdbg_needs_params(yylval TSRMLS_CC)) {
-               BEGIN(PARAMS);
-       }
-       return T_ID;
-}
-
-<PARAMS>[0-9]+         {
-       phpdbg_init_param(yylval, NUMERIC_PARAM);
-       yylval->num = atoi(yytext);
-       return T_ID;
-}
-
-<INITIAL>
-{
+<INITIAL>{
        [#]{1}          { return T_POUND;  }
        [:]{2}          { return T_DCOLON; }
        [:]{1}          { return T_COLON;  }
@@ -113,6 +102,7 @@ INPUT                               [^\n]+
                return T_ID; 
        }
 }
+
 <RAW>{INPUT}           {
        phpdbg_init_param(yylval, STR_PARAM);
        yylval->str = strndup(yytext, yyleng);
@@ -120,5 +110,6 @@ INPUT                               [^\n]+
        BEGIN(INITIAL);
        return T_INPUT;
 }
+
 {WS}                   { /* ignore whitespace */ }
 %%
index b08b09ff989a3493d2fdc780d26e04da009cea01..d830092797e4993814349fb3936319b725dc89cc 100644 (file)
--- a/phpdbg.c
+++ b/phpdbg.c
@@ -144,6 +144,7 @@ static void php_phpdbg_destroy_registered(void *data) /* {{{ */
                function TSRMLS_CC);
 } /* }}} */
 
+
 static PHP_RINIT_FUNCTION(phpdbg) /* {{{ */
 {
        zend_hash_init(&PHPDBG_G(bp)[PHPDBG_BREAK_FILE],   8, NULL, php_phpdbg_destroy_bp_file, 0);
index 841f9b24441d90b81542c8efb91cf0b43ccbb522..880403775ed861232f02c207f603ca236accf823 100644 (file)
@@ -513,8 +513,18 @@ static yyconst flex_int16_t yy_chk[231] =
 #include <stdio.h>
 #include <string.h>
 
+static inline void phpdbg_append_string(phpdbg_param_t *param, const char *string, size_t length TSRMLS_DC) {
+       if (!param->str) {
+               param->str = malloc(length+1);
+       } else param->str = realloc(param->str, param->len + (length+1));
+       
+       memcpy(&param->str[param->len], string, length);
+       param->len += length;
+       param->str[param->len] = 0;
+}
 #define YY_NO_UNISTD_H 1
-#line 518 "sapi/phpdbg/phpdbg_lexer.c"
+#line 528 "sapi/phpdbg/phpdbg_lexer.c"
 
 #define INITIAL 0
 #define RAW 1
@@ -752,9 +762,9 @@ YY_DECL
        register int yy_act;
     struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
 
-#line 42 "sapi/phpdbg/dev/phpdbg_lexer.l"
+#line 52 "sapi/phpdbg/dev/phpdbg_lexer.l"
 
-#line 758 "sapi/phpdbg/phpdbg_lexer.c"
+#line 768 "sapi/phpdbg/phpdbg_lexer.c"
 
     yylval = yylval_param;
 
@@ -837,22 +847,22 @@ do_action:        /* This label is used only to access EOF actions. */
 
 case 1:
 YY_RULE_SETUP
-#line 45 "sapi/phpdbg/dev/phpdbg_lexer.l"
+#line 54 "sapi/phpdbg/dev/phpdbg_lexer.l"
 { return T_POUND;  }
        YY_BREAK
 case 2:
 YY_RULE_SETUP
-#line 46 "sapi/phpdbg/dev/phpdbg_lexer.l"
+#line 55 "sapi/phpdbg/dev/phpdbg_lexer.l"
 { return T_DCOLON; }
        YY_BREAK
 case 3:
 YY_RULE_SETUP
-#line 47 "sapi/phpdbg/dev/phpdbg_lexer.l"
+#line 56 "sapi/phpdbg/dev/phpdbg_lexer.l"
 { return T_COLON;  }
        YY_BREAK
 case 4:
 YY_RULE_SETUP
-#line 48 "sapi/phpdbg/dev/phpdbg_lexer.l"
+#line 57 "sapi/phpdbg/dev/phpdbg_lexer.l"
 {
                BEGIN(RAW);
                phpdbg_init_param(yylval, EMPTY_PARAM);
@@ -861,7 +871,7 @@ YY_RULE_SETUP
        YY_BREAK
 case 5:
 YY_RULE_SETUP
-#line 53 "sapi/phpdbg/dev/phpdbg_lexer.l"
+#line 62 "sapi/phpdbg/dev/phpdbg_lexer.l"
 {
                BEGIN(RAW);
                phpdbg_init_param(yylval, EMPTY_PARAM);
@@ -870,7 +880,7 @@ YY_RULE_SETUP
        YY_BREAK
 case 6:
 YY_RULE_SETUP
-#line 58 "sapi/phpdbg/dev/phpdbg_lexer.l"
+#line 67 "sapi/phpdbg/dev/phpdbg_lexer.l"
 {
                BEGIN(RAW);
                phpdbg_init_param(yylval, EMPTY_PARAM);
@@ -879,7 +889,7 @@ YY_RULE_SETUP
        YY_BREAK
 case 7:
 YY_RULE_SETUP
-#line 63 "sapi/phpdbg/dev/phpdbg_lexer.l"
+#line 72 "sapi/phpdbg/dev/phpdbg_lexer.l"
 {
                phpdbg_init_param(yylval, NUMERIC_PARAM);
                yylval->num = 1;                
@@ -888,7 +898,7 @@ YY_RULE_SETUP
        YY_BREAK
 case 8:
 YY_RULE_SETUP
-#line 68 "sapi/phpdbg/dev/phpdbg_lexer.l"
+#line 77 "sapi/phpdbg/dev/phpdbg_lexer.l"
 {
                phpdbg_init_param(yylval, NUMERIC_PARAM);
                yylval->num = 0;
@@ -897,7 +907,7 @@ YY_RULE_SETUP
        YY_BREAK
 case 9:
 YY_RULE_SETUP
-#line 73 "sapi/phpdbg/dev/phpdbg_lexer.l"
+#line 82 "sapi/phpdbg/dev/phpdbg_lexer.l"
 {
                phpdbg_init_param(yylval, NUMERIC_PARAM);
                yylval->num = atoi(yytext);
@@ -906,7 +916,7 @@ YY_RULE_SETUP
        YY_BREAK
 case 10:
 YY_RULE_SETUP
-#line 78 "sapi/phpdbg/dev/phpdbg_lexer.l"
+#line 87 "sapi/phpdbg/dev/phpdbg_lexer.l"
 {
                phpdbg_init_param(yylval, ADDR_PARAM);
                yylval->addr = strtoul(yytext, NULL, 10);
@@ -915,7 +925,7 @@ YY_RULE_SETUP
        YY_BREAK
 case 11:
 YY_RULE_SETUP
-#line 83 "sapi/phpdbg/dev/phpdbg_lexer.l"
+#line 92 "sapi/phpdbg/dev/phpdbg_lexer.l"
 {
                phpdbg_init_param(yylval, OP_PARAM);
                yylval->str = strndup(yytext, yyleng);
@@ -925,7 +935,7 @@ YY_RULE_SETUP
        YY_BREAK
 case 12:
 YY_RULE_SETUP
-#line 89 "sapi/phpdbg/dev/phpdbg_lexer.l"
+#line 98 "sapi/phpdbg/dev/phpdbg_lexer.l"
 {
                phpdbg_init_param(yylval, STR_PARAM);
                yylval->str = strndup(yytext, yyleng);
@@ -936,7 +946,7 @@ YY_RULE_SETUP
 
 case 13:
 YY_RULE_SETUP
-#line 96 "sapi/phpdbg/dev/phpdbg_lexer.l"
+#line 106 "sapi/phpdbg/dev/phpdbg_lexer.l"
 {
        phpdbg_init_param(yylval, STR_PARAM);
        yylval->str = strndup(yytext, yyleng);
@@ -948,15 +958,15 @@ YY_RULE_SETUP
 case 14:
 /* rule 14 can match eol */
 YY_RULE_SETUP
-#line 103 "sapi/phpdbg/dev/phpdbg_lexer.l"
+#line 114 "sapi/phpdbg/dev/phpdbg_lexer.l"
 { /* ignore whitespace */ }
        YY_BREAK
 case 15:
 YY_RULE_SETUP
-#line 104 "sapi/phpdbg/dev/phpdbg_lexer.l"
+#line 115 "sapi/phpdbg/dev/phpdbg_lexer.l"
 YY_FATAL_ERROR( "flex scanner jammed" );
        YY_BREAK
-#line 960 "sapi/phpdbg/phpdbg_lexer.c"
+#line 970 "sapi/phpdbg/phpdbg_lexer.c"
 case YY_STATE_EOF(INITIAL):
 case YY_STATE_EOF(RAW):
        yyterminate();
@@ -2122,7 +2132,7 @@ void yyfree (void * ptr , yyscan_t yyscanner)
 
 #define YYTABLES_NAME "yytables"
 
-#line 104 "sapi/phpdbg/dev/phpdbg_lexer.l"
+#line 115 "sapi/phpdbg/dev/phpdbg_lexer.l"
 
 
 
index 88aef381a17875d2aa1e2da9685e0c96134d0704..c143d007fdee31a44874ebfa0f1c5638e54cb89c 100644 (file)
@@ -339,7 +339,7 @@ extern int yylex \
 #undef YY_DECL
 #endif
 
-#line 104 "sapi/phpdbg/dev/phpdbg_lexer.l"
+#line 115 "sapi/phpdbg/dev/phpdbg_lexer.l"
 
 
 #line 346 "sapi/phpdbg/phpdbg_lexer.h"
index 37c0851f77cbfe1c60561ce868ad9392b92a232e..96c47287071d7a9abfb7f67ec3293ad412738d60 100644 (file)
@@ -30,6 +30,7 @@
 #include "phpdbg_list.h"
 #include "phpdbg_utils.h"
 #include "phpdbg_prompt.h"
+#include "php_streams.h"
 
 ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
 
@@ -53,6 +54,7 @@ PHPDBG_LIST(lines) /* {{{ */
 
        switch (param->type) {
                case NUMERIC_PARAM:
+                       printf("list lines: %d\n", param->num);
                        phpdbg_list_file(phpdbg_current_file(TSRMLS_C),
                                (param->num < 0 ? 1 - param->num : param->num),
                                (param->num < 0 ? param->num : 0) + zend_get_executed_lineno(TSRMLS_C),
@@ -125,97 +127,48 @@ PHPDBG_LIST(class) /* {{{ */
 
 void phpdbg_list_file(const char *filename, long count, long offset, int highlight TSRMLS_DC) /* {{{ */
 {
-       unsigned char *mem, *pos, *last_pos, *end_pos;
        struct stat st;
-#ifndef _WIN32
-       int fd;
-#else
-       HANDLE fd, map;
-#endif
-       int all_content = (count == 0);
-       int line = 0, displayed = 0;
-
+       char *opened = NULL,
+                *mem = NULL;
+       char buffer[8096] = {0,};
+       size_t buflen = 0L;
+       long line = 0;
+       
+       php_stream *stream = NULL;
+       
        if (VCWD_STAT(filename, &st) == FAILURE) {
                phpdbg_error("Failed to stat file %s", filename);
                return;
        }
-
-#ifndef _WIN32
-       if ((fd = VCWD_OPEN(filename, O_RDONLY)) == FAILURE) {
+       
+       stream = php_stream_open_wrapper(filename, "rb", USE_PATH, &opened);
+       
+       if (!stream) {
                phpdbg_error("Failed to open file %s to list", filename);
                return;
        }
-
-       pos = last_pos = mem = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
-       end_pos = mem + st.st_size;
-#else
-
-       fd = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ,  NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
-       if (fd == INVALID_HANDLE_VALUE) {
-               phpdbg_error("Failed to open file!");
-               return;
-       }
-
-       map = CreateFileMapping(fd, NULL, PAGE_READONLY, 0, 0, NULL);
-       if (map == NULL) {
-               phpdbg_error("Failed to map file!");
-               CloseHandle(fd);
-               return;
-       }
-
-       pos = last_pos = mem = (char*) MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
-       if (mem == NULL) {
-               phpdbg_error("Failed to map file in memory");
-               CloseHandle(map);
-               CloseHandle(fd);
-               return;
-       }
-       end_pos = mem + st.st_size;
-#endif
-       while (1) {
-               if (pos == end_pos) {
-                       break;
-               }
-
-               pos = memchr(last_pos, '\n', end_pos - last_pos);
-
-               if (!pos) {
-                       /* No more line breaks */
-                       pos = end_pos;
-               }
-
+       
+       while ((buflen = php_stream_gets(stream, buffer, sizeof(buffer))) > 0L) {
                ++line;
-
+               
                if (!offset || offset <= line) {
                        /* Without offset, or offset reached */
                        if (!highlight) {
-                               phpdbg_writeln("%05u: %.*s", line, (int)(pos - last_pos), last_pos);
+                               phpdbg_write("%05u: %s", line, buffer);
                        } else {
                                if (highlight != line) {
-                                       phpdbg_writeln(" %05u: %.*s", line, (int)(pos - last_pos), last_pos);
+                                       phpdbg_write(" %05u: %s", line, buffer);
                                } else {
-                                       phpdbg_writeln(">%05u: %.*s", line, (int)(pos - last_pos), last_pos);
+                                       phpdbg_write(">%05u: %s", line, buffer);
                                }
                        }
-                       ++displayed;
                }
-
-               last_pos = pos + 1;
-
-               if (!all_content && displayed == count) {
-                       /* Reached max line to display */
+               
+               if ((count + (offset-1)) == line)
                        break;
-               }
        }
-
-#ifndef _WIN32
-       munmap(mem, st.st_size);
-       close(fd);
-#else
-       UnmapViewOfFile(mem);
-       CloseHandle(map);
-       CloseHandle(fd);
-#endif
+       
+       php_stream_close(stream);
 } /* }}} */
 
 void phpdbg_list_function(const zend_function *fbc TSRMLS_DC) /* {{{ */