summaryrefslogtreecommitdiff
path: root/sapi/cli/php_cli.c
diff options
context:
space:
mode:
authorNikita Popov <nikita.ppv@gmail.com>2019-07-15 16:21:46 +0200
committerNikita Popov <nikita.ppv@gmail.com>2019-07-15 16:25:49 +0200
commitc5f1b384b591009310370f0b06b10868d2d62741 (patch)
treee3277d15bdbc0e3efd5800fd8d556de724ba7bf7 /sapi/cli/php_cli.c
parent17d4e86ddaadfc9b6452a13c15d03360fbf5ec26 (diff)
downloadphp-git-c5f1b384b591009310370f0b06b10868d2d62741.tar.gz
Move shebang handling into the lexer
Instead of handling shebang lines by adjusting the file pointer in individual SAPIs, move the handling into the lexer, where this is both a lot simpler and more robust. Whether the shebang should be skipped is controlled by CG(skip_shebang) -- we might want to do that in more cases. This fixed bugs #60677 and #78066.
Diffstat (limited to 'sapi/cli/php_cli.c')
-rw-r--r--sapi/cli/php_cli.c34
1 files changed, 7 insertions, 27 deletions
diff --git a/sapi/cli/php_cli.c b/sapi/cli/php_cli.c
index 144a610a56..c285711d76 100644
--- a/sapi/cli/php_cli.c
+++ b/sapi/cli/php_cli.c
@@ -587,12 +587,9 @@ static const char *param_mode_conflict = "Either execute direct code, process st
/* {{{ cli_seek_file_begin
*/
-static int cli_seek_file_begin(zend_file_handle *file_handle, char *script_file, int *lineno)
+static int cli_seek_file_begin(zend_file_handle *file_handle, char *script_file)
{
- int c;
-
- *lineno = 1;
-
+ // TODO: Is this still needed?
file_handle->type = ZEND_HANDLE_FP;
file_handle->opened_path = NULL;
file_handle->free_filename = 0;
@@ -602,23 +599,7 @@ static int cli_seek_file_begin(zend_file_handle *file_handle, char *script_file,
}
file_handle->filename = script_file;
- /* #!php support */
- c = fgetc(file_handle->handle.fp);
- if (c == '#' && (c = fgetc(file_handle->handle.fp)) == '!') {
- while (c != '\n' && c != '\r' && c != EOF) {
- c = fgetc(file_handle->handle.fp); /* skip to end of line */
- }
- /* handle situations where line is terminated by \r\n */
- if (c == '\r') {
- if (fgetc(file_handle->handle.fp) != '\n') {
- zend_long pos = zend_ftell(file_handle->handle.fp);
- zend_fseek(file_handle->handle.fp, pos - 1, SEEK_SET);
- }
- }
- *lineno = 2;
- } else {
- rewind(file_handle->handle.fp);
- }
+ rewind(file_handle->handle.fp);
return SUCCESS;
}
@@ -649,7 +630,6 @@ static int do_cli(int argc, char **argv) /* {{{ */
char *arg_free=NULL, **arg_excp=&arg_free;
char *script_file=NULL, *translated_path = NULL;
int interactive=0;
- int lineno = 0;
const char *param_error=NULL;
int hide_argv = 0;
@@ -922,7 +902,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
php_optind++;
}
if (script_file) {
- if (cli_seek_file_begin(&file_handle, script_file, &lineno) != SUCCESS) {
+ if (cli_seek_file_begin(&file_handle, script_file) != SUCCESS) {
goto err;
} else {
char real_path[MAXPATHLEN];
@@ -960,7 +940,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
goto err;
}
request_started = 1;
- CG(start_lineno) = lineno;
+ CG(skip_shebang) = 1;
zend_register_bool_constant(
ZEND_STRL("PHP_CLI_PROCESS_TITLE"),
@@ -1050,10 +1030,10 @@ static int do_cli(int argc, char **argv) /* {{{ */
}
} else {
if (script_file) {
- if (cli_seek_file_begin(&file_handle, script_file, &lineno) != SUCCESS) {
+ if (cli_seek_file_begin(&file_handle, script_file) != SUCCESS) {
exit_status = 1;
} else {
- CG(start_lineno) = lineno;
+ CG(skip_shebang) = 1;
php_execute_script(&file_handle);
exit_status = EG(exit_status);
}