php-src/phpdbg_utils.c

179 lines
4.5 KiB
C
Raw Normal View History

2013-11-12 02:19:43 +00:00
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2013 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Felipe Pena <felipe@php.net> |
| Authors: Joe Watkins <joe.watkins@live.co.uk> |
+----------------------------------------------------------------------+
*/
2013-11-12 13:33:51 +00:00
#include <stdio.h>
2013-11-12 02:19:43 +00:00
#include <ctype.h>
2013-11-12 15:17:24 +00:00
#include "zend.h"
#include "php.h"
#include "spprintf.h"
#include "phpdbg.h"
2013-11-13 01:35:39 +00:00
#include "phpdbg_opcode.h"
2013-11-12 02:19:43 +00:00
#include "phpdbg_utils.h"
#ifdef _WIN32
# include "win32/time.h"
#endif
ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
2013-11-12 02:19:43 +00:00
int phpdbg_is_numeric(const char *str) /* {{{ */
{
2013-11-14 13:04:59 +00:00
if (!str)
return 0;
2013-11-12 02:19:43 +00:00
for (; *str; str++) {
if (isspace(*str)) {
continue;
}
return isdigit(*str);
}
2013-11-12 02:28:52 +00:00
return 0;
2013-11-12 02:19:43 +00:00
} /* }}} */
2013-11-12 02:50:15 +00:00
int phpdbg_is_empty(const char *str) /* {{{ */
{
2013-11-14 13:04:59 +00:00
if (!str)
return 1;
2013-11-12 02:50:15 +00:00
for (; *str; str++) {
if (isspace(*str)) {
continue;
}
return 0;
}
return 1;
} /* }}} */
int phpdbg_is_addr(const char *str) /* {{{ */
{
return str[0] && str[1] && memcmp(str, "0x", 2) == 0;
} /* }}} */
2013-11-12 13:33:51 +00:00
int phpdbg_is_class_method(const char *str, size_t len, char **class, char **method) /* {{{ */
{
char *sep = NULL;
if (strstr(str, " ") != NULL)
return 0;
sep = strstr(str, "::");
2013-11-12 13:33:51 +00:00
if (!sep || sep == str || sep+2 == str+len-1) {
2013-11-12 13:33:51 +00:00
return 0;
}
2013-11-13 19:32:21 +00:00
if (class != NULL) {
*class = estrndup(str, sep - str);
(*class)[sep - str] = 0;
}
2013-11-13 23:13:41 +00:00
2013-11-13 19:32:21 +00:00
if (method != NULL) {
*method = estrndup(
sep+2, str + len - (sep + 2));
}
2013-11-12 13:33:51 +00:00
return 1;
} /* }}} */
2013-11-13 23:36:02 +00:00
char *phpdbg_resolve_path(const char *path TSRMLS_DC) /* {{{ */
2013-11-13 23:13:41 +00:00
{
2013-11-13 23:36:02 +00:00
char resolved_name[MAXPATHLEN];
2013-11-13 23:13:41 +00:00
2013-11-13 23:36:02 +00:00
if (expand_filepath(path, resolved_name TSRMLS_CC) == NULL) {
return NULL;
2013-11-13 23:13:41 +00:00
}
2013-11-13 23:36:02 +00:00
return estrdup(resolved_name);
} /* }}} */
const char *phpdbg_current_file(TSRMLS_D) /* {{{ */
{
2013-11-14 00:29:44 +00:00
const char *file = zend_get_executed_filename(TSRMLS_C);
if (memcmp(file, "[no active file]", sizeof("[no active file]")) == 0) {
return PHPDBG_G(exec);
}
return file;
2013-11-13 23:36:02 +00:00
} /* }}} */
2013-11-13 23:13:41 +00:00
int phpdbg_print(int type TSRMLS_DC, FILE *fp, const char *format, ...) /* {{{ */
{
2013-11-13 08:05:26 +00:00
int rc = 0;
char *buffer = NULL;
2013-11-13 05:36:01 +00:00
va_list args;
if (format != NULL && strlen(format) > 0L) {
va_start(args, format);
vspprintf(&buffer, 0, format, args);
va_end(args);
}
/* TODO(anyone) colours */
switch (type) {
2013-11-13 21:15:45 +00:00
case P_ERROR:
rc = fprintf(fp, "%s%s%s\n",
((PHPDBG_G(flags) & PHPDBG_IS_COLOURED) ? "\033[1;31m[" : "["),
buffer,
((PHPDBG_G(flags) & PHPDBG_IS_COLOURED) ? "]\033[0m" : "]"));
break;
2013-11-13 21:15:45 +00:00
case P_NOTICE:
rc = fprintf(fp, "%s%s%s\n",
((PHPDBG_G(flags) & PHPDBG_IS_COLOURED) ? "\033[1;64m[" : "["),
buffer,
((PHPDBG_G(flags) & PHPDBG_IS_COLOURED) ? "]\033[0m" : "]"));
break;
2013-11-13 23:13:41 +00:00
2013-11-13 21:15:45 +00:00
case P_WRITELN: {
2013-11-12 23:31:46 +00:00
if (buffer) {
rc = fprintf(fp, "%s%s%s\n",
((PHPDBG_G(flags) & PHPDBG_IS_COLOURED) ? "\033[37m" : ""),
buffer,
((PHPDBG_G(flags) & PHPDBG_IS_COLOURED) ? "\033[0m" : ""));
2013-11-12 23:31:46 +00:00
} else {
rc = fprintf(fp, "\n");
2013-11-12 23:31:46 +00:00
}
} break;
2013-11-13 23:13:41 +00:00
2013-11-13 21:15:45 +00:00
case P_WRITE: if (buffer) {
rc = fprintf(fp, "%s%s%s",
((PHPDBG_G(flags) & PHPDBG_IS_COLOURED) ? "\033[37m" : ""),
buffer,
((PHPDBG_G(flags) & PHPDBG_IS_COLOURED) ? "\033[0m" : ""));
2013-11-12 23:31:46 +00:00
} break;
/* no formatting on logging output */
case P_LOG: if (buffer) {
struct timeval tp;
if (gettimeofday(&tp, NULL) == SUCCESS) {
rc = fprintf(
fp, "[%ld %.8F]: %s\n", tp.tv_sec, tp.tv_usec / 1000000.00, buffer);
} else rc = FAILURE;
} break;
}
2013-11-12 22:55:39 +00:00
if (buffer) {
efree(buffer);
}
2013-11-13 23:13:41 +00:00
2013-11-13 08:05:26 +00:00
return rc;
} /* }}} */