From: krakjoe Date: Sun, 16 Feb 2014 19:25:15 +0000 (+0000) Subject: build stack from command line X-Git-Tag: php-5.6.0beta2~1^2~37^2~20^2~46 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c744605dc7f313835fb7ba584f88c3138fa50782;p=php build stack from command line --- diff --git a/phpdbg_cmd.h b/phpdbg_cmd.h index c86f92bb47..6eb4e73e90 100644 --- a/phpdbg_cmd.h +++ b/phpdbg_cmd.h @@ -53,7 +53,8 @@ struct _phpdbg_input_t { int argc; }; -typedef struct _phpdbg_param { +typedef struct _phpdbg_param phpdbg_param_t; +struct _phpdbg_param { phpdbg_param_type type; long num; zend_ulong addr; @@ -67,7 +68,21 @@ typedef struct _phpdbg_param { } method; char *str; size_t len; -} phpdbg_param_t; + phpdbg_param_t *next; +}; + +#define phpdbg_init_param(v, t) do{ \ + v->type = t; \ + v->addr = 0; \ + v->num = 0; \ + v->file.name = NULL; \ + v->file.line = 0; \ + v->method.class = NULL; \ + v->method.name = NULL; \ + v->str = NULL; \ + v->len = 0; \ + v->next = NULL; \ +} while(0) typedef int (*phpdbg_command_handler_t)(const phpdbg_param_t*, const phpdbg_input_t* TSRMLS_DC); diff --git a/phpdbg_lexer.l b/phpdbg_lexer.l index 6915ab62e0..3b6c9cae27 100644 --- a/phpdbg_lexer.l +++ b/phpdbg_lexer.l @@ -44,65 +44,74 @@ INPUT [^\n]+ { {C_YES}|{C_ON}|{C_ENABLED}|{C_TRUE} { - yylval->type = NUMERIC_PARAM; + phpdbg_init_param(yylval, NUMERIC_PARAM); yylval->num = 1; return C_TRUTHY; } {C_NO}|{C_OFF}|{C_DISABLED}|{C_FALSE} { - yylval->type = NUMERIC_PARAM; + phpdbg_init_param(yylval, NUMERIC_PARAM); yylval->num = 0; return C_FALSY; } - {C_EVAL} { + {C_EVAL} { BEGIN(RAW); - return C_EVAL; + phpdbg_init_param(yylval, STR_PARAM); + yylval->str = strndup(yytext, yyleng); + yylval->len = yyleng; + return C_EVAL; } {C_SHELL} { BEGIN(RAW); + phpdbg_init_param(yylval, STR_PARAM); + yylval->str = strndup(yytext, yyleng); + yylval->len = yyleng; return C_SHELL; } {DIGITS} { - yylval->type = NUMERIC_PARAM; + phpdbg_init_param(yylval, NUMERIC_PARAM); yylval->num = atoi(yytext); return T_DIGITS; } {METHOD} { - yylval->type = METHOD_PARAM; + phpdbg_init_param(yylval, METHOD_PARAM); yylval->method.class = "class"; yylval->method.name = "func"; return T_METHOD; } {FILE} { - yylval->type = FILE_PARAM; + phpdbg_init_param(yylval, FILE_PARAM); yylval->file.name = strndup(yytext, yyleng); yylval->file.line = 0; return T_FILE; } {OPLINE} { - yylval->type = ADDR_PARAM; + phpdbg_init_param(yylval, ADDR_PARAM); yylval->addr = strtoul(yytext, NULL, 10); return T_OPLINE; } - {LITERAL} { - yylval->type = STR_PARAM; + {LITERAL} { + phpdbg_init_param(yylval, STR_PARAM); yylval->str = strndup(yytext, yyleng); yylval->len = yyleng; return T_LITERAL; } - {ID} { - yylval->type = STR_PARAM; + {ID} { + phpdbg_init_param(yylval, STR_PARAM); yylval->str = strndup(yytext, yyleng); yylval->len = yyleng; return T_ID; } } {INPUT} { - yylval->type = STR_PARAM; + phpdbg_init_param(yylval, STR_PARAM); yylval->str = strndup(yytext, yyleng); yylval->len = yyleng; BEGIN(INITIAL); return T_INPUT; } -{WS} { /* ignore whitespace */ } -. { return T_UNEXPECTED; } +{WS} { /* ignore whitespace */ } +. { + phpdbg_init_param(yylval, EMPTY_PARAM); + return T_UNEXPECTED; +} %% diff --git a/phpdbg_parser.y b/phpdbg_parser.y index 9b87847ebc..dfc13a48e1 100644 --- a/phpdbg_parser.y +++ b/phpdbg_parser.y @@ -4,7 +4,7 @@ /* * phpdbg_parser.y * - * flex phpdb_lexer.l + * flex phpdbg_lexer.l * bison phpdbg_parser.y * gcc -g -o parser phpdbg_lexer.c phpdbg_parser.c -I/usr/src/php-src/main -I/usr/src/php-src/Zend -I/usr/src/php-src/TSRM -I/usr/src/php-src */ @@ -43,37 +43,64 @@ void phpdbg_debug_param(const phpdbg_param_t *param, const char *msg) { #include "phpdbg_parser.h" #include "phpdbg_lexer.h" +static void phpdbg_stack_push(phpdbg_param_t *stack, phpdbg_param_t *param) { + phpdbg_param_t *next = calloc(1, sizeof(phpdbg_param_t)); + + if (!next) + return; + + *(next) = *(param); + + if (stack->addr) { + next->next = + stack->next; + } else { + stack->addr = (ulong) next; + } + + stack->next = next; + stack->len++; +} + int yyerror(phpdbg_param_t **param, yyscan_t scanner, const char *msg) { fprintf(stderr, "Parse Error: %s\n", msg); } int main(int argc, char **argv) { do { - phpdbg_param_t *expression; yyscan_t scanner; YY_BUFFER_STATE state; char buffer[8096]; size_t buflen = 0L; + phpdbg_param_t *stack = malloc(sizeof(phpdbg_param_t)); + + phpdbg_init_param(stack, EMPTY_PARAM); if (fgets(&buffer[0], 8096, stdin) != NULL) { if (yylex_init(&scanner)) { - // couldn't initialize fprintf(stderr, "could not initialize scanner\n"); return 1; } state = yy_scan_string(buffer, scanner); - if (yyparse(&expression, scanner) <= 0) { - // error parsing + if (yyparse(&stack, scanner) <= 0) { + fprintf(stderr, "got stack %p\n", stack); + while (stack) { + phpdbg_debug_param(stack, "\t -> "); + stack = stack->next; + } yy_delete_buffer(state, scanner); yylex_destroy(scanner); - } else fprintf(stderr, "could not parse input (%s) !!\n", buffer); + } } else fprintf(stderr, "could not get input !!\n"); + + free(stack); } while (1); return 0; } + %} %code requires { @@ -88,7 +115,7 @@ typedef void* yyscan_t; %define api.pure %lex-param { yyscan_t scanner } -%parse-param { phpdbg_param_t **expression } +%parse-param { phpdbg_param_t **stack } %parse-param { yyscan_t scanner } %token C_TRUTHY "truthy (true, on, yes or enabled)" @@ -108,25 +135,32 @@ typedef void* yyscan_t; %% input - : handler {} + : handler ; parameters - : parameters parameter { phpdbg_debug_param(&$2, "got another parameter"); } - | parameter { phpdbg_debug_param(&$1, "got first parameter"); } + : parameter { phpdbg_stack_push(*stack, &$1); } + | parameters parameter { phpdbg_stack_push(*stack, &$2); } ; params - : /* empty */ { /* do nothing */ } - | parameters { $$ = $1; } + : /* empty */ + | parameters + ; + +normal + : T_ID { phpdbg_stack_push(*stack, &$1); } + | normal T_ID { phpdbg_stack_push(*stack, &$2); } + ; + +special + : C_EVAL T_INPUT { phpdbg_stack_push(*stack, &$1); phpdbg_stack_push(*stack, &$2); } + | C_SHELL T_INPUT { phpdbg_stack_push(*stack, &$1); phpdbg_stack_push(*stack, &$2); } ; command - : T_ID { fprintf(stderr, "got cmd: %s\n", $1.str); } - | T_ID T_ID { fprintf(stderr, "got sub: %s -> %s\n", $1.str, $2.str); } - /* exceptional cases below */ - | C_EVAL T_INPUT { fprintf(stderr, "got eval: %s\n", $2.str); } - | C_SHELL T_INPUT { fprintf(stderr, "got shell: %s\n", $2.str); } + : normal + | special ; parameter @@ -141,6 +175,6 @@ parameter ; handler - : command params {} + : command params ; %%