xref: /PHP-7.0/sapi/phpdbg/phpdbg_cmd.h (revision 478f119a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2017 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Felipe Pena <felipe@php.net>                                |
16    | Authors: Joe Watkins <joe.watkins@live.co.uk>                        |
17    | Authors: Bob Weinand <bwoebi@php.net>                                |
18    +----------------------------------------------------------------------+
19 */
20 
21 #ifndef PHPDBG_CMD_H
22 #define PHPDBG_CMD_H
23 
24 #include "TSRM.h"
25 
26 /* {{{ Command and Parameter */
27 enum {
28 	NO_ARG = 0,
29 	REQUIRED_ARG,
30 	OPTIONAL_ARG
31 };
32 
33 typedef enum {
34 	EMPTY_PARAM = 0,
35 	ADDR_PARAM,
36 	FILE_PARAM,
37 	NUMERIC_FILE_PARAM,
38 	METHOD_PARAM,
39 	STR_PARAM,
40 	NUMERIC_PARAM,
41 	NUMERIC_FUNCTION_PARAM,
42 	NUMERIC_METHOD_PARAM,
43 	STACK_PARAM,
44 	EVAL_PARAM,
45 	SHELL_PARAM,
46 	COND_PARAM,
47 	OP_PARAM,
48 	ORIG_PARAM,
49 	RUN_PARAM
50 } phpdbg_param_type;
51 
52 typedef struct _phpdbg_param phpdbg_param_t;
53 struct _phpdbg_param {
54 	phpdbg_param_type type;
55 	long num;
56 	zend_ulong addr;
57 	struct {
58 		char *name;
59 		long line;
60 	} file;
61 	struct {
62 		char *class;
63 		char *name;
64 	} method;
65 	char *str;
66 	size_t len;
67 	phpdbg_param_t *next;
68 	phpdbg_param_t *top;
69 };
70 
71 #define phpdbg_init_param(v, t) do{ \
72 	(v)->type = (t); \
73 	(v)->addr = 0; \
74 	(v)->num = 0; \
75 	(v)->file.name = NULL; \
76 	(v)->file.line = 0; \
77 	(v)->method.class = NULL; \
78 	(v)->method.name = NULL; \
79 	(v)->str = NULL; \
80 	(v)->len = 0; \
81 	(v)->next = NULL; \
82 	(v)->top = NULL; \
83 } while(0)
84 
85 #ifndef YYSTYPE
86 #define YYSTYPE phpdbg_param_t
87 #endif
88 
89 #define PHPDBG_ASYNC_SAFE 1
90 
91 typedef int (*phpdbg_command_handler_t)(const phpdbg_param_t*);
92 
93 typedef struct _phpdbg_command_t phpdbg_command_t;
94 struct _phpdbg_command_t {
95 	const char *name;                   /* Command name */
96 	size_t name_len;                    /* Command name length */
97 	const char *tip;                    /* Menu tip */
98 	size_t tip_len;                     /* Menu tip length */
99 	char alias;                         /* Alias */
100 	phpdbg_command_handler_t handler;   /* Command handler */
101 	const phpdbg_command_t *subs;       /* Sub Commands */
102 	char *args;                         /* Argument Spec */
103 	const phpdbg_command_t *parent;     /* Parent Command */
104 	zend_bool flags;                    /* General flags */
105 };
106 /* }}} */
107 
108 /* {{{ misc */
109 #define PHPDBG_STRL(s) s, sizeof(s)-1
110 #define PHPDBG_MAX_CMD 500
111 #define PHPDBG_FRAME(v) (PHPDBG_G(frame).v)
112 #define PHPDBG_EX(v) (EG(current_execute_data)->v)
113 
114 typedef struct {
115 	int num;
116 	zend_execute_data *execute_data;
117 } phpdbg_frame_t;
118 /* }}} */
119 
120 /*
121 * Workflow:
122 * 1) the lexer/parser creates a stack of commands and arguments from input
123 * 2) the commands at the top of the stack are resolved sensibly using aliases, abbreviations and case insensitive matching
124 * 3) the remaining arguments in the stack are verified (optionally) against the handlers declared argument specification
125 * 4) the handler is called passing the top of the stack as the only parameter
126 * 5) the stack is destroyed upon return from the handler
127 */
128 
129 /*
130 * Input Management
131 */
132 PHPDBG_API char* phpdbg_read_input(char *buffered);
133 PHPDBG_API void phpdbg_destroy_input(char**);
134 PHPDBG_API int phpdbg_ask_user_permission(const char *question);
135 
136 /**
137  * Stack Management
138  */
139 PHPDBG_API void phpdbg_stack_push(phpdbg_param_t *stack, phpdbg_param_t *param);
140 PHPDBG_API void phpdbg_stack_separate(phpdbg_param_t *param);
141 PHPDBG_API const phpdbg_command_t *phpdbg_stack_resolve(const phpdbg_command_t *commands, const phpdbg_command_t *parent, phpdbg_param_t **top);
142 PHPDBG_API int phpdbg_stack_verify(const phpdbg_command_t *command, phpdbg_param_t **stack);
143 PHPDBG_API int phpdbg_stack_execute(phpdbg_param_t *stack, zend_bool allow_async_unsafe);
144 PHPDBG_API void phpdbg_stack_free(phpdbg_param_t *stack);
145 
146 /*
147 * Parameter Management
148 */
149 PHPDBG_API void phpdbg_clear_param(phpdbg_param_t*);
150 PHPDBG_API void phpdbg_copy_param(const phpdbg_param_t*, phpdbg_param_t*);
151 PHPDBG_API zend_bool phpdbg_match_param(const phpdbg_param_t *, const phpdbg_param_t *);
152 PHPDBG_API zend_ulong phpdbg_hash_param(const phpdbg_param_t *);
153 PHPDBG_API const char* phpdbg_get_param_type(const phpdbg_param_t*);
154 PHPDBG_API char* phpdbg_param_tostring(const phpdbg_param_t *param, char **pointer);
155 PHPDBG_API void phpdbg_param_debug(const phpdbg_param_t *param, const char *msg);
156 
157 /**
158  * Command Declarators
159  */
160 #define PHPDBG_COMMAND_HANDLER(name) phpdbg_do_##name
161 
162 #define PHPDBG_COMMAND_D_EXP(name, tip, alias, handler, children, args, parent, flags) \
163 	{PHPDBG_STRL(#name), tip, sizeof(tip)-1, alias, phpdbg_do_##handler, children, args, parent, flags}
164 
165 #define PHPDBG_COMMAND_D_EX(name, tip, alias, handler, children, args, flags) \
166 	{PHPDBG_STRL(#name), tip, sizeof(tip)-1, alias, phpdbg_do_##handler, children, args, NULL, flags}
167 
168 #define PHPDBG_COMMAND_D(name, tip, alias, children, args, flags) \
169 	{PHPDBG_STRL(#name), tip, sizeof(tip)-1, alias, phpdbg_do_##name, children, args, NULL, flags}
170 
171 #define PHPDBG_COMMAND(name) int phpdbg_do_##name(const phpdbg_param_t *param)
172 
173 #define PHPDBG_COMMAND_ARGS param
174 
175 #define PHPDBG_END_COMMAND {NULL, 0, NULL, 0, '\0', NULL, NULL, NULL, NULL, 0}
176 
177 /*
178 * Default Switch Case
179 */
180 #define phpdbg_default_switch_case() \
181 	default: \
182 		phpdbg_error("command", "type=\"wrongarg\" got=\"%s\"", "Unsupported parameter type (%s) for command", phpdbg_get_param_type(param)); \
183 	break
184 
185 #endif /* PHPDBG_CMD_H */
186