xref: /PHP-5.6/sapi/phpdbg/phpdbg_set.c (revision 49493a2d)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2016 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 #include "phpdbg.h"
22 #include "phpdbg_cmd.h"
23 #include "phpdbg_set.h"
24 #include "phpdbg_utils.h"
25 #include "phpdbg_bp.h"
26 #include "phpdbg_prompt.h"
27 
28 ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
29 
30 #define PHPDBG_SET_COMMAND_D(f, h, a, m, l, s) \
31 	PHPDBG_COMMAND_D_EXP(f, h, a, m, l, s, &phpdbg_prompt_commands[18])
32 
33 const phpdbg_command_t phpdbg_set_commands[] = {
34 	PHPDBG_SET_COMMAND_D(prompt,       "usage: set prompt [<string>]",            'p', set_prompt,       NULL, "|s"),
35 #ifndef _WIN32
36 	PHPDBG_SET_COMMAND_D(color,        "usage: set color  <element> <color>",     'c', set_color,        NULL, "ss"),
37 	PHPDBG_SET_COMMAND_D(colors,       "usage: set colors [<on|off>]",            'C', set_colors,       NULL, "|b"),
38 #endif
39 	PHPDBG_SET_COMMAND_D(oplog,        "usage: set oplog  [<output>]",            'O', set_oplog,        NULL, "|s"),
40 	PHPDBG_SET_COMMAND_D(break,        "usage: set break id [<on|off>]",          'b', set_break,        NULL, "l|b"),
41 	PHPDBG_SET_COMMAND_D(breaks,       "usage: set breaks [<on|off>]",            'B', set_breaks,       NULL, "|b"),
42 	PHPDBG_SET_COMMAND_D(quiet,        "usage: set quiet [<on|off>]",             'q', set_quiet,        NULL, "|b"),
43 	PHPDBG_SET_COMMAND_D(stepping,     "usage: set stepping [<line|op>]",         's', set_stepping,     NULL, "|s"),
44 	PHPDBG_SET_COMMAND_D(refcount,     "usage: set refcount [<on|off>]",          'r', set_refcount,     NULL, "|b"),
45 	PHPDBG_END_COMMAND
46 };
47 
PHPDBG_SET(prompt)48 PHPDBG_SET(prompt) /* {{{ */
49 {
50 	if (!param || param->type == EMPTY_PARAM) {
51 		phpdbg_writeln("%s", phpdbg_get_prompt(TSRMLS_C));
52 	} else phpdbg_set_prompt(param->str TSRMLS_CC);
53 
54 	return SUCCESS;
55 } /* }}} */
56 
PHPDBG_SET(break)57 PHPDBG_SET(break) /* {{{ */
58 {
59 	switch (param->type) {
60 		case NUMERIC_PARAM: {
61 			if (param->next) {
62 				if (param->next->num) {
63 					phpdbg_enable_breakpoint(param->num TSRMLS_CC);
64 				} else phpdbg_disable_breakpoint(param->num TSRMLS_CC);
65 			} else {
66 				phpdbg_breakbase_t *brake = phpdbg_find_breakbase(param->num TSRMLS_CC);
67 				if (brake) {
68 					phpdbg_writeln(
69 						"%s", brake->disabled ? "off" : "on");
70 				} else {
71 					phpdbg_error("Failed to find breakpoint #%ld", param->num);
72 				}
73 			}
74 		} break;
75 
76 		default:
77 			phpdbg_error(
78 				"set break used incorrectly: set break [id] <on|off>");
79 	}
80 
81 	return SUCCESS;
82 } /* }}} */
83 
PHPDBG_SET(breaks)84 PHPDBG_SET(breaks) /* {{{ */
85 {
86 	if (!param || param->type == EMPTY_PARAM) {
87 		phpdbg_writeln("%s",
88 			PHPDBG_G(flags) & PHPDBG_IS_BP_ENABLED ? "on" : "off");
89 	} else switch (param->type) {
90 		case NUMERIC_PARAM: {
91 			if (param->num) {
92 				phpdbg_enable_breakpoints(TSRMLS_C);
93 			} else phpdbg_disable_breakpoints(TSRMLS_C);
94 		} break;
95 
96 		default:
97 			phpdbg_error(
98 				"set break used incorrectly: set break [id] <on|off>");
99 	}
100 
101 	return SUCCESS;
102 } /* }}} */
103 
104 #ifndef _WIN32
PHPDBG_SET(color)105 PHPDBG_SET(color) /* {{{ */
106 {
107 	const phpdbg_color_t *color = phpdbg_get_color(
108 			param->next->str, param->next->len TSRMLS_CC);
109 
110 	if (!color) {
111 		phpdbg_error(
112 			"Failed to find the requested color (%s)", param->next->str);
113 		return SUCCESS;
114 	}
115 
116 	switch (phpdbg_get_element(param->str, param->len TSRMLS_CC)) {
117 		case PHPDBG_COLOR_PROMPT:
118 			phpdbg_notice(
119 				"setting prompt color to %s (%s)", color->name, color->code);
120 			if (PHPDBG_G(prompt)[1]) {
121 				free(PHPDBG_G(prompt)[1]);
122 				PHPDBG_G(prompt)[1]=NULL;
123 			}
124 			phpdbg_set_color(PHPDBG_COLOR_PROMPT, color TSRMLS_CC);
125 		break;
126 
127 		case PHPDBG_COLOR_ERROR:
128 			phpdbg_notice(
129 				"setting error color to %s (%s)", color->name, color->code);
130 			phpdbg_set_color(PHPDBG_COLOR_ERROR, color TSRMLS_CC);
131 		break;
132 
133 		case PHPDBG_COLOR_NOTICE:
134 			phpdbg_notice(
135 				"setting notice color to %s (%s)", color->name, color->code);
136 			phpdbg_set_color(PHPDBG_COLOR_NOTICE, color TSRMLS_CC);
137 		break;
138 
139 		default:
140 			phpdbg_error(
141 				"Failed to find the requested element (%s)", param->str);
142 	}
143 
144 	return SUCCESS;
145 } /* }}} */
146 
PHPDBG_SET(colors)147 PHPDBG_SET(colors) /* {{{ */
148 {
149 	if (!param || param->type == EMPTY_PARAM) {
150 		phpdbg_writeln("%s", PHPDBG_G(flags) & PHPDBG_IS_COLOURED ? "on" : "off");
151 	} else switch (param->type) {
152 		case NUMERIC_PARAM: {
153 			if (param->num) {
154 				PHPDBG_G(flags) |= PHPDBG_IS_COLOURED;
155 			} else {
156 				PHPDBG_G(flags) &= ~PHPDBG_IS_COLOURED;
157 			}
158 		} break;
159 
160 		default:
161 			phpdbg_error(
162 				"set colors used incorrectly: set colors <on|off>");
163 	}
164 
165 	return SUCCESS;
166 } /* }}} */
167 #endif
168 
PHPDBG_SET(oplog)169 PHPDBG_SET(oplog) /* {{{ */
170 {
171 	if (!param || param->type == EMPTY_PARAM) {
172 		phpdbg_notice("Oplog %s", PHPDBG_G(oplog) ? "enabled" : "disabled");
173 	} else switch (param->type) {
174 		case STR_PARAM: {
175 			/* open oplog */
176 			FILE *old = PHPDBG_G(oplog);
177 
178 			PHPDBG_G(oplog) = fopen(param->str, "w+");
179 			if (!PHPDBG_G(oplog)) {
180 				phpdbg_error("Failed to open %s for oplog", param->str);
181 				PHPDBG_G(oplog) = old;
182 			} else {
183 				if (old) {
184 					phpdbg_notice("Closing previously open oplog");
185 					fclose(old);
186 				}
187 				phpdbg_notice("Successfully opened oplog %s", param->str);
188 			}
189 		} break;
190 
191 		phpdbg_default_switch_case();
192 	}
193 
194 	return SUCCESS;
195 } /* }}} */
196 
PHPDBG_SET(quiet)197 PHPDBG_SET(quiet) /* {{{ */
198 {
199 	if (!param || param->type == EMPTY_PARAM) {
200 		phpdbg_writeln("Quietness %s",
201 			PHPDBG_G(flags) & PHPDBG_IS_QUIET ? "on" : "off");
202 	} else switch (param->type) {
203 		case NUMERIC_PARAM: {
204 			if (param->num) {
205 				PHPDBG_G(flags) |= PHPDBG_IS_QUIET;
206 			} else {
207 				PHPDBG_G(flags) &= ~PHPDBG_IS_QUIET;
208 			}
209 		} break;
210 
211 		phpdbg_default_switch_case();
212 	}
213 
214 	return SUCCESS;
215 } /* }}} */
216 
PHPDBG_SET(stepping)217 PHPDBG_SET(stepping) /* {{{ */
218 {
219 	if (!param || param->type == EMPTY_PARAM) {
220 		phpdbg_writeln("Stepping %s",
221 			PHPDBG_G(flags) & PHPDBG_STEP_OPCODE ? "opcode" : "line");
222 	} else switch (param->type) {
223 		case STR_PARAM: {
224 			if ((param->len == sizeof("opcode")-1) &&
225 				(memcmp(param->str, "opcode", sizeof("opcode")) == SUCCESS)) {
226 				PHPDBG_G(flags) |= PHPDBG_STEP_OPCODE;
227 			} else if ((param->len == sizeof("line")-1) &&
228 				(memcmp(param->str, "line", sizeof("line")) == SUCCESS)) {
229 				PHPDBG_G(flags) &= ~PHPDBG_STEP_OPCODE;
230 			} else {
231 				phpdbg_error("usage set stepping [<opcode|line>]");
232 			}
233 		} break;
234 
235 		phpdbg_default_switch_case();
236 	}
237 
238 	return SUCCESS;
239 } /* }}} */
240 
PHPDBG_SET(refcount)241 PHPDBG_SET(refcount) /* {{{ */
242 {
243 	if (!param || param->type == EMPTY_PARAM) {
244 		phpdbg_writeln("Refcount %s", PHPDBG_G(flags) & PHPDBG_IS_QUIET ? "on" : "off");
245 	} else switch (param->type) {
246 		case NUMERIC_PARAM: {
247 			if (param->num) {
248 				PHPDBG_G(flags) |= PHPDBG_SHOW_REFCOUNTS;
249 			} else {
250 				PHPDBG_G(flags) &= ~PHPDBG_SHOW_REFCOUNTS;
251 			}
252 		} break;
253 
254 		phpdbg_default_switch_case();
255 	}
256 
257 	return SUCCESS;
258 } /* }}} */
259