Skip to content

Commit 3709ea9

Browse files
committed
Merge branch 'master' of https://github.com/krakjoe/phpdbg
2 parents b65cef9 + dace704 commit 3709ea9

File tree

4 files changed

+91
-74
lines changed

4 files changed

+91
-74
lines changed

phpdbg_cmd.c

Lines changed: 52 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,24 @@ void phpdbg_clear_param(phpdbg_param_t *param TSRMLS_DC) /* {{{ */
115115
break;
116116
}
117117
}
118-
118+
119119
} /* }}} */
120120

121121
static inline phpdbg_input_t** phpdbg_read_argv(char *buffer, int *argc TSRMLS_DC) /* {{{ */
122122
{
123-
char *p, *s;
123+
char *p;
124124
char b[PHPDBG_MAX_CMD];
125125
int l=0;
126126
enum states {
127127
IN_BETWEEN,
128-
IN_WORD,
129-
IN_STRING
128+
IN_WORD,
129+
IN_STRING
130130
} state = IN_BETWEEN;
131131
phpdbg_input_t **argv = NULL;
132-
132+
133133
argv = (phpdbg_input_t**) emalloc(sizeof(phpdbg_input_t**));
134134
(*argc) = 0;
135-
135+
136136
#define RESET_STATE() do {\
137137
phpdbg_input_t *arg = emalloc(sizeof(phpdbg_input_t));\
138138
if (arg) {\
@@ -157,11 +157,9 @@ static inline phpdbg_input_t** phpdbg_read_argv(char *buffer, int *argc TSRMLS_D
157157
}
158158
if (c == '"') {
159159
state = IN_STRING;
160-
s = p + 1;
161160
continue;
162161
}
163162
state = IN_WORD;
164-
s = p;
165163
b[l++]=c;
166164
continue;
167165

@@ -186,39 +184,39 @@ static inline phpdbg_input_t** phpdbg_read_argv(char *buffer, int *argc TSRMLS_D
186184
continue;
187185
}
188186
}
189-
187+
190188
switch (state) {
191189
case IN_WORD: {
192190
RESET_STATE();
193191
} break;
194-
192+
195193
case IN_STRING:
196194
phpdbg_error(
197-
"Malformed command line (unclosed quote) @ %d: %s!",
195+
"Malformed command line (unclosed quote) @ %d: %s!",
198196
(p - buffer)-1, &buffer[(p - buffer)-1]);
199197
break;
200198
}
201-
199+
202200
if ((*argc) == 0) {
203201
/* not needed */
204202
efree(argv);
205-
203+
206204
/* to be sure */
207205
return NULL;
208206
}
209-
207+
210208
return argv;
211209
} /* }}} */
212210

213-
phpdbg_input_t* phpdbg_read_input(TSRMLS_D) /* {{{ */
211+
phpdbg_input_t *phpdbg_read_input(TSRMLS_D) /* {{{ */
214212
{
215213
if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
216214
phpdbg_input_t *buffer = NULL;
217215
size_t cmd_len = 0L;
218216

219217
#ifndef HAVE_LIBREADLINE
220218
char *cmd = NULL;
221-
char buf[PHPDBG_MAX_CMD];
219+
char buf[PHPDBG_MAX_CMD];
222220
if (!phpdbg_write(PROMPT) ||
223221
!fgets(buf, PHPDBG_MAX_CMD, stdin)) {
224222
/* the user has gone away */
@@ -227,7 +225,7 @@ phpdbg_input_t* phpdbg_read_input(TSRMLS_D) /* {{{ */
227225
zend_bailout();
228226
return NULL;
229227
}
230-
228+
231229
cmd = buf;
232230
#else
233231
char *cmd = readline(PROMPT);
@@ -242,46 +240,36 @@ phpdbg_input_t* phpdbg_read_input(TSRMLS_D) /* {{{ */
242240
add_history(cmd);
243241
#endif
244242

245-
/* strip whitespace */
246-
while (cmd && isspace(*cmd))
247-
cmd++;
248-
cmd_len = strlen(cmd);
249-
while (*cmd && isspace(cmd[cmd_len-1]))
250-
cmd_len--;
251-
cmd[cmd_len] = '\0';
252-
253243
/* allocate and sanitize buffer */
254244
buffer = (phpdbg_input_t*) emalloc(sizeof(phpdbg_input_t));
255-
if (buffer) {
256-
buffer->length = strlen(cmd);
257-
buffer->string = emalloc(buffer->length+1);
258-
if (buffer->string) {
259-
memcpy(
260-
buffer->string, cmd, buffer->length);
261-
buffer->string[buffer->length] = '\0';
262-
/* store constant pointer to start of buffer */
263-
buffer->start = (char* const*) buffer->string;
264-
{
265-
/* temporary, when we switch to argv/argc handling
266-
will be unnecessary */
267-
char *store = (char*) estrdup(buffer->string);
268-
269-
buffer->argv = phpdbg_read_argv(
270-
store, &buffer->argc TSRMLS_CC);
271-
272-
if (buffer->argc) {
273-
int arg = 0;
274-
275-
while (arg < buffer->argc) {
276-
phpdbg_debug(
277-
"argv %d=%s", arg, buffer->argv[arg]->string);
278-
arg++;
279-
}
280-
}
281-
282-
efree(store);
245+
if (!buffer) {
246+
return NULL;
247+
}
248+
249+
buffer->string = phpdbg_trim(cmd, strlen(cmd), &buffer->length);
250+
251+
if (buffer->string) {
252+
/* temporary, when we switch to argv/argc handling
253+
will be unnecessary */
254+
char *store = (char*) estrdup(buffer->string);
255+
256+
/* store constant pointer to start of buffer */
257+
buffer->start = (char* const*) buffer->string;
258+
259+
buffer->argv = phpdbg_read_argv(
260+
store, &buffer->argc TSRMLS_CC);
261+
262+
if (buffer->argc) {
263+
int arg = 0;
264+
265+
while (arg < buffer->argc) {
266+
phpdbg_debug(
267+
"argv %d=%s", arg, buffer->argv[arg]->string);
268+
arg++;
283269
}
284270
}
271+
272+
efree(store);
285273
}
286274

287275
#ifdef HAVE_LIBREADLINE
@@ -292,18 +280,17 @@ phpdbg_input_t* phpdbg_read_input(TSRMLS_D) /* {{{ */
292280

293281
return buffer;
294282
}
295-
283+
296284
return NULL;
297285
} /* }}} */
298286

299287
void phpdbg_destroy_input(phpdbg_input_t **input TSRMLS_DC) /*{{{ */
300288
{
301289
if (*input) {
302-
303290
if ((*input)->string) {
304291
efree((*input)->string);
305292
}
306-
293+
307294
if ((*input)->argc > 0) {
308295
int arg;
309296
for (arg=0; arg<(*input)->argc; arg++) {
@@ -315,7 +302,7 @@ void phpdbg_destroy_input(phpdbg_input_t **input TSRMLS_DC) /*{{{ */
315302
if ((*input)->argv) {
316303
efree((*input)->argv);
317304
}
318-
305+
319306
efree(*input);
320307
}
321308
} /* }}} */
@@ -326,10 +313,10 @@ int phpdbg_do_cmd_ex(const phpdbg_command_t *command, phpdbg_input_t *input TSRM
326313

327314
if (input->argc > 0) {
328315
while (command && command->name && command->handler) {
329-
if (((command->name_len == input->argv[0]->length) &&
316+
if (((command->name_len == input->argv[0]->length) &&
330317
(memcmp(command->name, input->argv[0]->string, command->name_len) == SUCCESS)) ||
331318
(command->alias &&
332-
(input->argv[0]->length == 1) &&
319+
(input->argv[0]->length == 1) &&
333320
(command->alias == *input->argv[0]->string))) {
334321

335322
phpdbg_param_t param;
@@ -353,18 +340,18 @@ int phpdbg_do_cmd_ex(const phpdbg_command_t *command, phpdbg_input_t *input TSRM
353340
input->argv[1]->string,
354341
input->argv[1]->length,
355342
&param TSRMLS_CC);
356-
}
343+
}
357344
}
358345

359346
phpdbg_debug(
360-
"found command \"%s\" for \"%s\" have %d arguments",
347+
"found command %s for %s with %d arguments",
361348
command->name, input->argv[0]->string, input->argc-1);
362349
{
363350
int arg;
364351
for (arg=1; arg<input->argc; arg++) {
365352
phpdbg_debug(
366-
"\t#%d: [%s=%d]",
367-
arg,
353+
"\t#%d: [%s=%d]",
354+
arg,
368355
input->argv[arg]->string,
369356
input->argv[arg]->length);
370357
}
@@ -376,7 +363,6 @@ int phpdbg_do_cmd_ex(const phpdbg_command_t *command, phpdbg_input_t *input TSRM
376363
PHPDBG_G(lparam) = param;
377364

378365
rc = command->handler(&param TSRMLS_CC);
379-
380366
break;
381367
}
382368
command++;
@@ -386,7 +372,7 @@ int phpdbg_do_cmd_ex(const phpdbg_command_t *command, phpdbg_input_t *input TSRM
386372
phpdbg_error(
387373
"No function executed !!");
388374
}
389-
375+
390376
return rc;
391377
} /* }}} */
392378

@@ -409,7 +395,7 @@ int phpdbg_do_cmd(const phpdbg_command_t *command, char *cmd_line, size_t cmd_le
409395
expr,
410396
(cmd_len - expr_len) ? (((cmd_len - expr_len) - sizeof(" "))+1) : 0,
411397
&param TSRMLS_CC);
412-
398+
413399
if (command->subs && param.type == STR_PARAM) {
414400
if (phpdbg_do_cmd(command->subs, param.str, param.len TSRMLS_CC) == SUCCESS) {
415401
rc = SUCCESS;

phpdbg_prompt.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ static PHPDBG_COMMAND(register) /* {{{ */
696696
phpdbg_error(
697697
"The requested name (%s) is already in use", lcname);
698698
}
699-
699+
700700
efree(lcname);
701701
} break;
702702

@@ -969,7 +969,7 @@ static inline int phpdbg_call_register(phpdbg_input_t *input TSRMLS_DC) /* {{{ *
969969
}
970970
return SUCCESS;
971971
}
972-
972+
973973
return FAILURE;
974974
} /* }}} */
975975

@@ -978,7 +978,7 @@ int phpdbg_interactive(TSRMLS_D) /* {{{ */
978978
int ret = SUCCESS;
979979

980980
phpdbg_input_t* input = phpdbg_read_input(TSRMLS_C);
981-
981+
982982
if (input && input->length > 0L) {
983983
do {
984984
switch (ret = phpdbg_do_cmd_ex(phpdbg_prompt_commands, input TSRMLS_CC)) {
@@ -1000,10 +1000,10 @@ int phpdbg_interactive(TSRMLS_D) /* {{{ */
10001000
goto out;
10011001
}
10021002
}
1003-
1003+
10041004
phpdbg_destroy_input(&input TSRMLS_CC);
10051005
} while ((input = phpdbg_read_input(TSRMLS_C)) && (input->length > 0L));
1006-
1006+
10071007
if (!input->length)
10081008
goto last;
10091009

phpdbg_utils.c

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ int phpdbg_is_empty(const char *str) /* {{{ */
5050
{
5151
if (!str)
5252
return 1;
53-
53+
5454
for (; *str; str++) {
5555
if (isspace(*str)) {
5656
continue;
@@ -71,7 +71,7 @@ int phpdbg_is_class_method(const char *str, size_t len, char **class, char **met
7171

7272
if (strstr(str, " ") != NULL)
7373
return 0;
74-
74+
7575
sep = strstr(str, "::");
7676

7777
if (!sep || sep == str || sep+2 == str+len-1) {
@@ -113,6 +113,36 @@ const char *phpdbg_current_file(TSRMLS_D) /* {{{ */
113113
return file;
114114
} /* }}} */
115115

116+
char *phpdbg_trim(const char *str, size_t len, size_t *new_len) /* {{{ */
117+
{
118+
const char *p = str;
119+
char *new = NULL;
120+
121+
while (p && isspace(*p)) {
122+
++p;
123+
--len;
124+
}
125+
126+
while (*p && isspace(*(p + len -1))) {
127+
--len;
128+
}
129+
130+
if (len == 0) {
131+
new = estrndup("", sizeof(""));
132+
*new_len = 0;
133+
} else {
134+
new = estrndup(p, len);
135+
*(new + len) = '\0';
136+
137+
if (new_len) {
138+
*new_len = len;
139+
}
140+
}
141+
142+
return new;
143+
144+
} /* }}} */
145+
116146
int phpdbg_print(int type TSRMLS_DC, FILE *fp, const char *format, ...) /* {{{ */
117147
{
118148
int rc = 0;
@@ -159,7 +189,7 @@ int phpdbg_print(int type TSRMLS_DC, FILE *fp, const char *format, ...) /* {{{ *
159189
buffer,
160190
((PHPDBG_G(flags) & PHPDBG_IS_COLOURED) ? "\033[0m" : ""));
161191
} break;
162-
192+
163193
/* no formatting on logging output */
164194
case P_LOG: if (buffer) {
165195
struct timeval tp;

phpdbg_utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ int phpdbg_is_addr(const char*);
2929
int phpdbg_is_class_method(const char*, size_t, char**, char**);
3030
const char *phpdbg_current_file(TSRMLS_D);
3131
char *phpdbg_resolve_path(const char* TSRMLS_DC);
32+
char *phpdbg_trim(const char*, size_t, size_t*);
3233

3334
/**
3435
* Error/notice/formatting helper

0 commit comments

Comments
 (0)