]> granicus.if.org Git - php/commitdiff
- Added phpdbg_trim()
authorFelipe Pena <felipensp@gmail.com>
Tue, 19 Nov 2013 21:26:40 +0000 (19:26 -0200)
committerFelipe Pena <felipensp@gmail.com>
Tue, 19 Nov 2013 21:26:40 +0000 (19:26 -0200)
phpdbg_cmd.c
phpdbg_utils.c
phpdbg_utils.h

index 3ba736d7cbb7ec4b9690ac02ef083ec766837f5b..f6a15b487ac4ac495c8f66f7015733e14e3b7593 100644 (file)
@@ -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
index 0828374eac6d7b9c40befcd416fc0ecdc38de41f..4fabd8ab394964219a262643ee60557eae86aa53 100644 (file)
@@ -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;
index 3de4010ca0d028b7f4f2f19810dd27268de692fb..0a6ad4f4fa8416664b55ca96ac1abe12deb55ab4 100644 (file)
@@ -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