From: Felipe Pena Date: Tue, 19 Nov 2013 21:26:40 +0000 (-0200) Subject: - Added phpdbg_trim() X-Git-Tag: php-5.6.0alpha1~110^2~191^2~3 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3853f5b370c4b8f741e427eb0000dbef01c08dad;p=php - Added phpdbg_trim() --- diff --git a/phpdbg_cmd.c b/phpdbg_cmd.c index 3ba736d7cb..f6a15b487a 100644 --- a/phpdbg_cmd.c +++ b/phpdbg_cmd.c @@ -240,46 +240,36 @@ phpdbg_input_t *phpdbg_read_input(TSRMLS_D) /* {{{ */ add_history(cmd); #endif - /* strip whitespace */ - while (cmd && isspace(*cmd)) - cmd++; - cmd_len = strlen(cmd); - while (*cmd && isspace(cmd[cmd_len-1])) - cmd_len--; - cmd[cmd_len] = '\0'; - /* allocate and sanitize buffer */ buffer = (phpdbg_input_t*) emalloc(sizeof(phpdbg_input_t)); - if (buffer) { - buffer->length = strlen(cmd); - buffer->string = emalloc(buffer->length+1); - if (buffer->string) { - memcpy( - buffer->string, cmd, buffer->length); - buffer->string[buffer->length] = '\0'; - /* store constant pointer to start of buffer */ - buffer->start = (char* const*) buffer->string; - { - /* temporary, when we switch to argv/argc handling - will be unnecessary */ - char *store = (char*) estrdup(buffer->string); + if (!buffer) { + return NULL; + } - buffer->argv = phpdbg_read_argv( - store, &buffer->argc TSRMLS_CC); + buffer->string = phpdbg_trim(cmd, strlen(cmd), &buffer->length); - if (buffer->argc) { - int arg = 0; + if (buffer->string) { + /* temporary, when we switch to argv/argc handling + will be unnecessary */ + char *store = (char*) estrdup(buffer->string); - while (arg < buffer->argc) { - phpdbg_debug( - "argv %d=%s", arg, buffer->argv[arg]->string); - arg++; - } - } + /* store constant pointer to start of buffer */ + buffer->start = (char* const*) buffer->string; + + buffer->argv = phpdbg_read_argv( + store, &buffer->argc TSRMLS_CC); - efree(store); + if (buffer->argc) { + int arg = 0; + + while (arg < buffer->argc) { + phpdbg_debug( + "argv %d=%s", arg, buffer->argv[arg]->string); + arg++; } } + + efree(store); } #ifdef HAVE_LIBREADLINE diff --git a/phpdbg_utils.c b/phpdbg_utils.c index 0828374eac..4fabd8ab39 100644 --- a/phpdbg_utils.c +++ b/phpdbg_utils.c @@ -50,7 +50,7 @@ int phpdbg_is_empty(const char *str) /* {{{ */ { if (!str) return 1; - + for (; *str; str++) { if (isspace(*str)) { continue; @@ -71,7 +71,7 @@ int phpdbg_is_class_method(const char *str, size_t len, char **class, char **met if (strstr(str, " ") != NULL) return 0; - + sep = strstr(str, "::"); if (!sep || sep == str || sep+2 == str+len-1) { @@ -113,6 +113,36 @@ const char *phpdbg_current_file(TSRMLS_D) /* {{{ */ return file; } /* }}} */ +char *phpdbg_trim(const char *str, size_t len, size_t *new_len) /* {{{ */ +{ + const char *p = str; + char *new = NULL; + + while (p && isspace(*p)) { + ++p; + --len; + } + + while (*p && isspace(*(p + len-1))) { + --len; + } + + if (len == 0) { + *new_len = 0; + return estrndup("", sizeof("")); + } + + new = estrndup(p, len); + *(new + len) = '\0'; + + if (new_len) { + *new_len = len; + } + + return new; + +} /* }}} */ + int phpdbg_print(int type TSRMLS_DC, FILE *fp, const char *format, ...) /* {{{ */ { int rc = 0; @@ -159,7 +189,7 @@ int phpdbg_print(int type TSRMLS_DC, FILE *fp, const char *format, ...) /* {{{ * buffer, ((PHPDBG_G(flags) & PHPDBG_IS_COLOURED) ? "\033[0m" : "")); } break; - + /* no formatting on logging output */ case P_LOG: if (buffer) { struct timeval tp; diff --git a/phpdbg_utils.h b/phpdbg_utils.h index 3de4010ca0..0a6ad4f4fa 100644 --- a/phpdbg_utils.h +++ b/phpdbg_utils.h @@ -29,6 +29,7 @@ int phpdbg_is_addr(const char*); int phpdbg_is_class_method(const char*, size_t, char**, char**); const char *phpdbg_current_file(TSRMLS_D); char *phpdbg_resolve_path(const char* TSRMLS_DC); +char *phpdbg_trim(const char*, size_t, size_t*); /** * Error/notice/formatting helper