Skip to content

Commit 3853f5b

Browse files
committed
- Added phpdbg_trim()
1 parent dee048c commit 3853f5b

File tree

3 files changed

+56
-35
lines changed

3 files changed

+56
-35
lines changed

phpdbg_cmd.c

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -240,46 +240,36 @@ phpdbg_input_t *phpdbg_read_input(TSRMLS_D) /* {{{ */
240240
add_history(cmd);
241241
#endif
242242

243-
/* strip whitespace */
244-
while (cmd && isspace(*cmd))
245-
cmd++;
246-
cmd_len = strlen(cmd);
247-
while (*cmd && isspace(cmd[cmd_len-1]))
248-
cmd_len--;
249-
cmd[cmd_len] = '\0';
250-
251243
/* allocate and sanitize buffer */
252244
buffer = (phpdbg_input_t*) emalloc(sizeof(phpdbg_input_t));
253-
if (buffer) {
254-
buffer->length = strlen(cmd);
255-
buffer->string = emalloc(buffer->length+1);
256-
if (buffer->string) {
257-
memcpy(
258-
buffer->string, cmd, buffer->length);
259-
buffer->string[buffer->length] = '\0';
260-
/* store constant pointer to start of buffer */
261-
buffer->start = (char* const*) buffer->string;
262-
{
263-
/* temporary, when we switch to argv/argc handling
264-
will be unnecessary */
265-
char *store = (char*) estrdup(buffer->string);
245+
if (!buffer) {
246+
return NULL;
247+
}
266248

267-
buffer->argv = phpdbg_read_argv(
268-
store, &buffer->argc TSRMLS_CC);
249+
buffer->string = phpdbg_trim(cmd, strlen(cmd), &buffer->length);
269250

270-
if (buffer->argc) {
271-
int arg = 0;
251+
if (buffer->string) {
252+
/* temporary, when we switch to argv/argc handling
253+
will be unnecessary */
254+
char *store = (char*) estrdup(buffer->string);
272255

273-
while (arg < buffer->argc) {
274-
phpdbg_debug(
275-
"argv %d=%s", arg, buffer->argv[arg]->string);
276-
arg++;
277-
}
278-
}
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);
279261

280-
efree(store);
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++;
281269
}
282270
}
271+
272+
efree(store);
283273
}
284274

285275
#ifdef HAVE_LIBREADLINE

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_len = 0;
132+
return estrndup("", sizeof(""));
133+
}
134+
135+
new = estrndup(p, len);
136+
*(new + len) = '\0';
137+
138+
if (new_len) {
139+
*new_len = len;
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)