summaryrefslogtreecommitdiff
path: root/ext/readline/readline_cli.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/readline/readline_cli.c')
-rw-r--r--ext/readline/readline_cli.c106
1 files changed, 56 insertions, 50 deletions
diff --git a/ext/readline/readline_cli.c b/ext/readline/readline_cli.c
index f4ce12bade..48ab488763 100644
--- a/ext/readline/readline_cli.c
+++ b/ext/readline/readline_cli.c
@@ -1,8 +1,8 @@
/*
+----------------------------------------------------------------------+
- | PHP Version 5 |
+ | PHP Version 7 |
+----------------------------------------------------------------------+
- | Copyright (c) 1997-2013 The PHP Group |
+ | Copyright (c) 1997-2014 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 |
@@ -42,7 +42,7 @@
#include "php_main.h"
#include "fopen_wrappers.h"
#include "ext/standard/php_standard.h"
-#include "ext/standard/php_smart_str.h"
+#include "zend_smart_str.h"
#ifdef __riscos__
#include <unixlib/local.h>
@@ -79,7 +79,7 @@ ZEND_DECLARE_MODULE_GLOBALS(cli_readline);
static char php_last_char = '\0';
static FILE *pager_pipe = NULL;
-static size_t readline_shell_write(const char *str, uint str_length TSRMLS_DC) /* {{{ */
+static size_t readline_shell_write(const char *str, size_t str_length TSRMLS_DC) /* {{{ */
{
if (CLIR_G(prompt_str)) {
smart_str_appendl(CLIR_G(prompt_str), str, str_length);
@@ -97,8 +97,11 @@ static size_t readline_shell_write(const char *str, uint str_length TSRMLS_DC) /
}
/* }}} */
-static int readline_shell_ub_write(const char *str, uint str_length TSRMLS_DC) /* {{{ */
+static size_t readline_shell_ub_write(const char *str, size_t str_length TSRMLS_DC) /* {{{ */
{
+ /* We just store the last char here and then pass back to the
+ caller (sapi_cli_single_write in sapi/cli) which will actually
+ write due to -1 return code */
php_last_char = str[str_length-1];
return -1;
}
@@ -131,7 +134,7 @@ typedef enum {
outside,
} php_code_type;
-static char *cli_get_prompt(char *block, char prompt TSRMLS_DC) /* {{{ */
+static zend_string *cli_get_prompt(char *block, char prompt TSRMLS_DC) /* {{{ */
{
smart_str retval = {0};
char *prompt_spec = CLIR_G(prompt) ? CLIR_G(prompt) : DEFAULT_PROMPT;
@@ -197,11 +200,11 @@ static char *cli_get_prompt(char *block, char prompt TSRMLS_DC) /* {{{ */
}
} while (++prompt_spec && *prompt_spec);
smart_str_0(&retval);
- return retval.c;
+ return retval.s;
}
/* }}} */
-static int cli_is_valid_code(char *code, int len, char **prompt TSRMLS_DC) /* {{{ */
+static int cli_is_valid_code(char *code, int len, zend_string **prompt TSRMLS_DC) /* {{{ */
{
int valid_end = 1, last_valid_end;
int brackets_count = 0;
@@ -267,12 +270,6 @@ static int cli_is_valid_code(char *code, int len, char **prompt TSRMLS_DC) /* {{
}
valid_end = 0;
break;
- case '%':
- if (!CG(asp_tags)) {
- valid_end = 0;
- break;
- }
- /* no break */
case '?':
if (code[i+1] == '>') {
i++;
@@ -357,7 +354,6 @@ static int cli_is_valid_code(char *code, int len, char **prompt TSRMLS_DC) /* {{
break;
case outside:
if ((CG(short_tags) && !strncmp(code+i-1, "<?", 2))
- || (CG(asp_tags) && !strncmp(code+i-1, "<%", 2))
|| (i > 3 && !strncmp(code+i-4, "<?php", 5))
) {
code_type = body;
@@ -405,8 +401,8 @@ static int cli_is_valid_code(char *code, int len, char **prompt TSRMLS_DC) /* {{
static char *cli_completion_generator_ht(const char *text, int textlen, int *state, HashTable *ht, void **pData TSRMLS_DC) /* {{{ */
{
- char *name;
- ulong number;
+ zend_string *name;
+ zend_ulong number;
if (!(*state % 2)) {
zend_hash_internal_pointer_reset(ht);
@@ -414,12 +410,12 @@ static char *cli_completion_generator_ht(const char *text, int textlen, int *sta
}
while(zend_hash_has_more_elements(ht) == SUCCESS) {
zend_hash_get_current_key(ht, &name, &number, 0);
- if (!textlen || !strncmp(name, text, textlen)) {
+ if (!textlen || !strncmp(name->val, text, textlen)) {
if (pData) {
- zend_hash_get_current_data(ht, pData);
+ *pData = zend_hash_get_current_data_ptr(ht);
}
zend_hash_move_forward(ht);
- return name;
+ return name->val;
}
if (zend_hash_move_forward(ht) == FAILURE) {
break;
@@ -432,8 +428,9 @@ static char *cli_completion_generator_ht(const char *text, int textlen, int *sta
static char *cli_completion_generator_var(const char *text, int textlen, int *state TSRMLS_DC) /* {{{ */
{
char *retval, *tmp;
+ zend_array *symbol_table = &EG(symbol_table);
- tmp = retval = cli_completion_generator_ht(text + 1, textlen - 1, state, EG(active_symbol_table), NULL TSRMLS_CC);
+ tmp = retval = cli_completion_generator_ht(text + 1, textlen - 1, state, symbol_table ? &symbol_table->ht : NULL, NULL TSRMLS_CC);
if (retval) {
retval = malloc(strlen(tmp) + 2);
retval[0] = '$';
@@ -463,7 +460,7 @@ static char *cli_completion_generator_func(const char *text, int textlen, int *s
char *retval = cli_completion_generator_ht(text, textlen, state, ht, (void**)&func TSRMLS_CC);
if (retval) {
rl_completion_append_character = '(';
- retval = strdup(func->common.function_name);
+ retval = strdup(func->common.function_name->val);
}
return retval;
@@ -471,11 +468,11 @@ static char *cli_completion_generator_func(const char *text, int textlen, int *s
static char *cli_completion_generator_class(const char *text, int textlen, int *state TSRMLS_DC) /* {{{ */
{
- zend_class_entry **pce;
- char *retval = cli_completion_generator_ht(text, textlen, state, EG(class_table), (void**)&pce TSRMLS_CC);
+ zend_class_entry *ce;
+ char *retval = cli_completion_generator_ht(text, textlen, state, EG(class_table), (void**)&ce TSRMLS_CC);
if (retval) {
rl_completion_append_character = '\0';
- retval = strdup((*pce)->name);
+ retval = strdup(ce->name->val);
}
return retval;
@@ -518,17 +515,18 @@ TODO:
} else if (text[0] == '#') {
retval = cli_completion_generator_ini(text, textlen, &cli_completion_state TSRMLS_CC);
} else {
- char *lc_text, *class_name, *class_name_end;
+ char *lc_text, *class_name_end;
int class_name_len;
- zend_class_entry **pce = NULL;
+ zend_string *class_name;
+ zend_class_entry *ce = NULL;
class_name_end = strstr(text, "::");
if (class_name_end) {
class_name_len = class_name_end - text;
- class_name = zend_str_tolower_dup(text, class_name_len);
- class_name[class_name_len] = '\0'; /* not done automatically */
- if (zend_lookup_class(class_name, class_name_len, &pce TSRMLS_CC)==FAILURE) {
- efree(class_name);
+ class_name = zend_string_alloc(class_name_len, 0);
+ zend_str_tolower_copy(class_name->val, text, class_name_len);
+ if ((ce = zend_lookup_class(class_name TSRMLS_CC)) == NULL) {
+ zend_string_release(class_name);
return NULL;
}
lc_text = zend_str_tolower_dup(class_name_end + 2, textlen - 2 - class_name_len);
@@ -540,14 +538,14 @@ TODO:
switch (cli_completion_state) {
case 0:
case 1:
- retval = cli_completion_generator_func(lc_text, textlen, &cli_completion_state, pce ? &(*pce)->function_table : EG(function_table) TSRMLS_CC);
+ retval = cli_completion_generator_func(lc_text, textlen, &cli_completion_state, ce ? &ce->function_table : EG(function_table) TSRMLS_CC);
if (retval) {
break;
}
case 2:
case 3:
- retval = cli_completion_generator_define(text, textlen, &cli_completion_state, pce ? &(*pce)->constants_table : EG(zend_constants) TSRMLS_CC);
- if (retval || pce) {
+ retval = cli_completion_generator_define(text, textlen, &cli_completion_state, ce ? &ce->constants_table : EG(zend_constants) TSRMLS_CC);
+ if (retval || ce) {
break;
}
case 4:
@@ -559,13 +557,13 @@ TODO:
}
efree(lc_text);
if (class_name_end) {
- efree(class_name);
+ zend_string_release(class_name);
}
- if (pce && retval) {
+ if (ce && retval) {
int len = class_name_len + 2 + strlen(retval) + 1;
char *tmp = malloc(len);
- snprintf(tmp, len, "%s::%s", (*pce)->name, retval);
+ snprintf(tmp, len, "%s::%s", ce->name->val, retval);
free(retval);
retval = tmp;
}
@@ -585,8 +583,9 @@ static int readline_shell_run(TSRMLS_D) /* {{{ */
char *line;
size_t size = 4096, pos = 0, len;
char *code = emalloc(size);
- char *prompt = cli_get_prompt("php", '>' TSRMLS_CC);
+ zend_string *prompt = cli_get_prompt("php", '>' TSRMLS_CC);
char *history_file;
+ int history_lines_to_write = 0;
if (PG(auto_prepend_file) && PG(auto_prepend_file)[0]) {
zend_file_handle *prepend_file_p;
@@ -607,7 +606,7 @@ static int readline_shell_run(TSRMLS_D) /* {{{ */
read_history(history_file);
EG(exit_status) = 0;
- while ((line = readline(prompt)) != NULL) {
+ while ((line = readline(prompt->val)) != NULL) {
if (strcmp(line, "exit") == 0 || strcmp(line, "quit") == 0) {
free(line);
break;
@@ -623,17 +622,15 @@ static int readline_shell_run(TSRMLS_D) /* {{{ */
if (line[0] == '#') {
char *param = strstr(&line[1], "=");
if (param) {
- char *cmd;
- uint cmd_len;
+ zend_string *cmd;
param++;
- cmd_len = param - &line[1] - 1;
- cmd = estrndup(&line[1], cmd_len);
+ cmd = zend_string_init(&line[1], param - &line[1] - 1, 0);
- zend_alter_ini_entry_ex(cmd, cmd_len + 1, param, strlen(param), PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0 TSRMLS_CC);
- efree(cmd);
+ zend_alter_ini_entry_chars_ex(cmd, param, strlen(param), PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0 TSRMLS_CC);
+ zend_string_release(cmd);
add_history(line);
- efree(prompt);
+ zend_string_release(prompt);
/* TODO: This might be wrong! */
prompt = cli_get_prompt("php", '>' TSRMLS_CC);
continue;
@@ -651,15 +648,25 @@ static int readline_shell_run(TSRMLS_D) /* {{{ */
if (*line) {
add_history(line);
+ history_lines_to_write += 1;
}
free(line);
- efree(prompt);
+ zend_string_release(prompt);
if (!cli_is_valid_code(code, pos, &prompt TSRMLS_CC)) {
continue;
}
+ if (history_lines_to_write) {
+#if HAVE_LIBEDIT
+ write_history(history_file);
+#else
+ append_history(history_lines_to_write, history_file);
+#endif
+ history_lines_to_write = 0;
+ }
+
zend_try {
zend_eval_stringl(code, pos, NULL, "php shell code" TSRMLS_CC);
} zend_end_try();
@@ -667,7 +674,7 @@ static int readline_shell_run(TSRMLS_D) /* {{{ */
pos = 0;
if (!pager_pipe && php_last_char != '\0' && php_last_char != '\n') {
- readline_shell_write("\n", 1 TSRMLS_CC);
+ php_write("\n", 1 TSRMLS_CC);
}
if (EG(exception)) {
@@ -681,10 +688,9 @@ static int readline_shell_run(TSRMLS_D) /* {{{ */
php_last_char = '\0';
}
- write_history(history_file);
free(history_file);
efree(code);
- efree(prompt);
+ zend_string_release(prompt);
return EG(exit_status);
}
/* }}} */