67#include <unixlib/local.h>
77#ifndef PHP_CLI_WIN32_NO_CONSOLE
86# define php_select(m, r, w, e, t) select(m, r, w, e, t)
91#if defined(PHP_WIN32) && defined(HAVE_OPENSSL_EXT)
92# include "openssl/applink.c"
113 return &cli_shell_callbacks;
116static const char HARDCODED_INI[] =
118 "register_argc_argv=1\n"
120 "output_buffering=0\n"
121 "max_execution_time=0\n"
122 "max_input_time=-1\n";
126 {
'a', 0,
"interactive"},
127 {
'B', 1,
"process-begin"},
128 {
'C', 0,
"no-chdir"},
131 {
'E', 1,
"process-end"},
132 {
'e', 0,
"profile-info"},
133 {
'F', 1,
"process-file"},
137 {
'l', 0,
"syntax-check"},
139 {
'n', 0,
"no-php-ini"},
140 {
'q', 0,
"no-header"},
141 {
'R', 1,
"process-code"},
142 {
'H', 0,
"hide-args"},
144 {
's', 0,
"syntax-highlight"},
145 {
's', 0,
"syntax-highlighting"},
151 {
'z', 1,
"zend-extension"},
153 {10, 1,
"rfunction"},
157 {12, 1,
"rextension"},
159 {13, 1,
"rzendextension"},
176static void print_modules(
void)
183 zend_hash_sort(&sorted_registry, module_name_cmp, 0);
201 return strcmp(fe->name, se->name);
205static void print_extensions(
void)
218#define STDOUT_FILENO 1
221#define STDERR_FILENO 2
234 tv.tv_sec = (long)
FG(default_socket_timeout);
246 if (cli_shell_callbacks.cli_shell_write) {
247 cli_shell_callbacks.cli_shell_write(str, str_length);
250#ifdef PHP_WRITE_STDOUT
256 if (
ret == 0 && ferror(stdout)) {
264static size_t sapi_cli_ub_write(
const char *str,
size_t str_length)
266 const char *
ptr = str;
267 size_t remaining = str_length;
274 if (cli_shell_callbacks.cli_shell_ub_write) {
276 ub_wrote = cli_shell_callbacks.cli_shell_ub_write(str, str_length);
277 if (ub_wrote != (
size_t) -1) {
282 while (remaining > 0)
286#ifndef PHP_CLI_WIN32_NO_CONSOLE
287 EG(exit_status) = 255;
300static void sapi_cli_flush(
void *server_context)
306#ifndef PHP_CLI_WIN32_NO_CONSOLE
313static char *php_self =
"";
314static char *script_filename =
"";
316static void sapi_cli_register_variables(
zval *track_vars_array)
350static void sapi_cli_log_message(
const char *message,
int syslog_type_int)
352 fprintf(stderr,
"%s\n", message);
359static int sapi_cli_deactivate(
void)
362 if(
SG(request_info).argv0) {
363 free(
SG(request_info).argv0);
364 SG(request_info).argv0 =
NULL;
370static char* sapi_cli_read_cookies(
void)
390static void sapi_cli_send_header(
sapi_header_struct *sapi_header,
void *server_context)
404#define INI_DEFAULT(name,value)\
405 ZVAL_NEW_STR(&tmp, zend_string_init(value, sizeof(value)-1, 1));\
406 zend_hash_str_update(configuration_hash, name, sizeof(name)-1, &tmp);\
408static void sapi_cli_ini_defaults(
HashTable *configuration_hash)
418 "Command Line Interface",
433 sapi_cli_header_handler,
434 sapi_cli_send_headers,
435 sapi_cli_send_header,
438 sapi_cli_read_cookies,
440 sapi_cli_register_variables,
441 sapi_cli_log_message,
457static void php_cli_usage(
char *argv0)
468 printf(
"Usage: %s [options] [-f] <file> [--] [args...]\n"
469 " %s [options] -r <code> [--] [args...]\n"
470 " %s [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]\n"
471 " %s [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]\n"
472 " %s [options] -S <addr>:<port> [-t docroot] [router]\n"
473 " %s [options] -- [args...]\n"
476 " -a Run as interactive shell (requires readline extension)\n"
477 " -c <path>|<file> Look for php.ini file in this directory\n"
478 " -n No configuration (ini) files will be used\n"
479 " -d foo[=bar] Define INI entry foo with value 'bar'\n"
480 " -e Generate extended information for debugger/profiler\n"
481 " -f <file> Parse and execute <file>.\n"
483 " -i PHP information\n"
484 " -l Syntax check only (lint)\n"
485 " -m Show compiled in modules\n"
486 " -r <code> Run PHP <code> without using script tags <?..?>\n"
487 " -B <begin_code> Run PHP <begin_code> before processing input lines\n"
488 " -R <code> Run PHP <code> for every input line\n"
489 " -F <file> Parse and execute <file> for every input line\n"
490 " -E <end_code> Run PHP <end_code> after processing all input lines\n"
491 " -H Hide any passed arguments from external tools.\n"
492 " -S <addr>:<port> Run with built-in web server.\n"
493 " -t <docroot> Specify document root <docroot> for built-in web server.\n"
494 " -s Output HTML syntax highlighted source.\n"
495 " -v Version number\n"
496 " -w Output source with stripped comments and whitespace.\n"
497 " -z <file> Load Zend extension <file>.\n"
499 " args... Arguments passed to script. Use -- args when first argument\n"
500 " starts with - or script is read from stdin\n"
502 " --ini Show configuration file names\n"
504 " --rf <name> Show information about function <name>.\n"
505 " --rc <name> Show information about class <name>.\n"
506 " --re <name> Show information about extension <name>.\n"
507 " --rz <name> Show information about Zend extension <name>.\n"
508 " --ri <name> Show configuration for extension <name>.\n"
510 , prog, prog, prog, prog, prog, prog, prog);
516static void cli_register_file_handles(
void)
560static const char *param_mode_conflict =
"Either execute direct code, process stdin or use a file.\n";
567 fprintf(stderr,
"Could not open input file: %s\n", script_file);
578#if defined(PHP_WIN32)
579BOOL WINAPI php_cli_win32_ctrl_handler(
DWORD sig)
588static int do_cli(
int argc,
char **argv)
595 char *reflection_what =
NULL;
596 volatile int request_started = 0;
597 char *php_optarg =
NULL, *orig_optarg =
NULL;
598 int php_optind = 1, orig_optind = 1;
599 char *exec_direct =
NULL, *exec_run =
NULL, *exec_begin =
NULL, *exec_end =
NULL;
600 char *arg_free =
NULL, **arg_excp = &arg_free;
601 char *script_file =
NULL, *translated_path =
NULL;
602 bool interactive =
false;
603 const char *param_error =
NULL;
604 bool hide_argv =
false;
606 pid_t pid = getpid();
612 CG(in_compilation) = 0;
614 while ((c =
php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
654 php_optind = orig_optind;
655 php_optarg = orig_optarg;
656 while ((c =
php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
660 if (!cli_shell_callbacks.cli_shell_run) {
661 param_error =
"Interactive shell (-a) requires the readline extension.\n";
666 param_error = param_mode_conflict;
680 if (exec_run || script_file) {
681 param_error =
"You can use -R or -F only once.\n";
685 param_error = param_mode_conflict;
689 script_file = php_optarg;
694 param_error = param_mode_conflict;
696 }
else if (script_file) {
697 param_error =
"You can use -f only once.\n";
700 script_file = php_optarg;
720 if (exec_direct || script_file) {
721 param_error =
"You can use -r only once.\n";
725 param_error = param_mode_conflict;
729 exec_direct = php_optarg;
734 if (exec_run || script_file) {
735 param_error =
"You can use -R or -F only once.\n";
739 param_error = param_mode_conflict;
743 exec_run = php_optarg;
749 param_error =
"You can use -B only once.\n";
753 param_error = param_mode_conflict;
757 exec_begin = php_optarg;
763 param_error =
"You can use -E only once.\n";
767 param_error = param_mode_conflict;
771 exec_end = php_optarg;
776 param_error =
"Source highlighting only works for files.\n";
784 param_error =
"Source stripping only works for files.\n";
798 reflection_what = php_optarg;
802 reflection_what = php_optarg;
806 reflection_what = php_optarg;
810 reflection_what = php_optarg;
814 reflection_what = php_optarg;
820 num_repeats = atoi(php_optarg);
833#if defined(PHP_WIN32) && !defined(PHP_CLI_WIN32_NO_CONSOLE) && defined(HAVE_LIBEDIT) && !defined(COMPILE_DL_READLINE)
842 strcmp(argv[php_optind-1],
"--")
848 printf(
"Interactive shell\n\n");
852 if (num_repeats > 1) {
853 fprintf(stdout,
"Executing for the first time...\n");
859 if (argc > php_optind
863 &&
strcmp(argv[php_optind - 1],
"--"))
865 script_file = argv[php_optind];
870 if (cli_seek_file_begin(&file_handle, script_file) ==
FAILURE) {
875 translated_path = strdup(real_path);
877 script_filename = script_file;
878 php_self = script_file;
885 php_self =
"Standard input code";
895 SG(request_info).argc = argc - php_optind + 1;
896 arg_excp = argv + php_optind - 1;
897 arg_free = argv[php_optind - 1];
898 SG(request_info).path_translated = translated_path ? translated_path : php_self;
899 argv[php_optind - 1] = php_self;
900 SG(request_info).argv = argv + php_optind - 1;
904 *arg_excp = arg_free;
905 PUTS(
"Could not startup.\n");
909 CG(skip_shebang) = 1;
916 *arg_excp = arg_free;
920 for (i = 1; i < argc; i++) {
927 PG(during_request_startup) = 0;
930 cli_register_file_handles();
933 EG(exit_status) = cli_shell_callbacks.cli_shell_run();
940 zend_printf(
"No syntax errors detected in %s\n", php_self);
943 EG(exit_status) = 255;
964 cli_register_file_handles();
971 size_t len, index = 0;
974 if (!exec_run && script_file) {
979 cli_register_file_handles();
986 while (
len > 0 &&
len-- && (input[
len]==
'\n' || input[
len]==
'\r')) {
997 if (cli_seek_file_begin(&file_handle, script_file) ==
FAILURE) {
1000 CG(skip_shebang) = 1;
1027 if (
strstr(reflection_what,
"::")) {
1050 zend_call_known_instance_method_with_1_params(
1059 EG(exit_status) = 1;
1076 if (!
strcmp(reflection_what,
"main")) {
1079 zend_printf(
"Extension '%s' not present.\n", reflection_what);
1080 EG(exit_status) = 1;
1105 if (request_started) {
1107 request_started = 0;
1109 if (translated_path) {
1110 free(translated_path);
1111 translated_path =
NULL;
1118 if (--num_repeats && pid == getpid()) {
1119 fprintf(stdout,
"Finished execution, repeating...\n");
1123 return EG(exit_status);
1127 EG(exit_status) = 1;
1133#ifdef PHP_CLI_WIN32_NO_CONSOLE
1134int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
int nShowCmd)
1136int main(
int argc,
char *argv[])
1139#if defined(PHP_WIN32)
1140# ifdef PHP_CLI_WIN32_NO_CONSOLE
1142 char **argv = __argv;
1145 wchar_t **argv_wide;
1146 char **argv_save = argv;
1147 BOOL using_wide_argv = 0;
1152 int module_started = 0, sapi_started = 0;
1153 char *php_optarg =
NULL;
1154 int php_optind = 1, use_extended_info = 0;
1155 char *ini_path_override =
NULL;
1166#if defined(PHP_WIN32) && !defined(PHP_CLI_WIN32_NO_CONSOLE)
1171 cli_sapi_module.additional_functions = additional_functions;
1173#if defined(PHP_WIN32) && defined(_DEBUG)
1175 char *tmp =
getenv(
"PHP_WIN32_DEBUG_HEAP");
1178 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
1179 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
1180 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
1181 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
1182 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
1183 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
1184 tmp_flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
1185 tmp_flag |= _CRTDBG_DELAY_FREE_MEM_DF;
1186 tmp_flag |= _CRTDBG_LEAK_CHECK_DF;
1188 _CrtSetDbgFlag(tmp_flag);
1193#if defined(SIGPIPE) && defined(SIG_IGN)
1213 _setmode(_fileno(stdin),
O_BINARY);
1214 _setmode(_fileno(stdout),
O_BINARY);
1215 _setmode(_fileno(stderr),
O_BINARY);
1218 php_ini_builder_init(&ini_builder);
1220 while ((c =
php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 1, 2))!=-1) {
1223 if (ini_path_override) {
1224 free(ini_path_override);
1226 ini_path_override = strdup(php_optarg);
1235#ifndef PHP_CLI_WIN32_NO_CONSOLE
1243 php_cli_usage(argv[0]);
1246 php_cli_usage(argv[0]);
1249 case 'i':
case 'v':
case 'm':
1253 use_extended_info = 1;
1259 sapi_module->ini_defaults = sapi_cli_ini_defaults;
1260 sapi_module->php_ini_path_override = ini_path_override;
1274 sapi_module->ini_entries = php_ini_builder_finish(&ini_builder);
1288#if defined(PHP_WIN32)
1294 argv_wide = CommandLineToArgvW(GetCommandLineW(), &
num_args);
1296 using_wide_argv = 1;
1298 SetConsoleCtrlHandler(php_cli_win32_ctrl_handler,
TRUE);
1302 if (use_extended_info) {
1307#ifndef PHP_CLI_WIN32_NO_CONSOLE
1310 exit_status = do_cli(argc, argv);
1311#ifndef PHP_CLI_WIN32_NO_CONSOLE
1318 if (ini_path_override) {
1319 free(ini_path_override);
1321 php_ini_builder_deinit(&ini_builder);
1322 if (module_started) {
1332#if defined(PHP_WIN32)
1335 if (using_wide_argv) {
1337 LocalFree(argv_wide);
SAPI_API sapi_module_struct sapi_module
SAPI_API void sapi_deactivate(void)
SAPI_API void sapi_startup(sapi_module_struct *sf)
SAPI_API void sapi_shutdown(void)
#define SAPI_OPTION_NO_CHDIR
#define SAPI_HEADER_SENT_SUCCESSFULLY
#define STANDARD_SAPI_MODULE_PROPERTIES
struct _sapi_module_struct sapi_module_struct
file_private const char ext[]
ZEND_API void php_get_highlight_struct(zend_syntax_highlighter_ini *syntax_highlighter_ini)
fprintf($stream, string $format, mixed ... $values)
getenv(?string $name=null, bool $local_only=false)
printf(string $format, mixed ... $values)
strrchr(string $haystack, string $needle, bool $before_needle=false)
fwrite($stream, string $data, ?int $length=null)
strstr(string $haystack, string $needle, bool $before_needle=false)
@ PHP_CLI_MODE_REFLECTION_EXT_INFO
@ PHP_CLI_MODE_PROCESS_STDIN
@ PHP_CLI_MODE_REFLECTION_ZEND_EXTENSION
@ PHP_CLI_MODE_REFLECTION_EXTENSION
@ PHP_CLI_MODE_REFLECTION_CLASS
@ PHP_CLI_MODE_SHOW_INI_CONFIG
@ PHP_CLI_MODE_REFLECTION_FUNCTION
@ PHP_CLI_MODE_CLI_DIRECT
PW32CP const struct php_win32_cp * php_win32_cp_cli_do_restore(DWORD id)
PW32CP const struct php_win32_cp * php_win32_cp_get_orig(void)
ZEND_TLS const struct php_win32_cp * orig_cp
#define php_win32_cp_cli_setup()
#define PHP_WIN32_CP_W_TO_ANY_ARRAY(aw, aw_len, aa, aa_len)
#define PHP_WIN32_CP_FREE_ARRAY(a, a_len)
#define php_win32_cp_cli_restore()
PHP_WINUTIL_API BOOL php_win32_console_is_own(void)
PHP_WINUTIL_API BOOL php_win32_console_fileno_set_vt100(zend_long fileno, BOOL enable)
dl(string $extension_filename)
memset(ptr, 0, type->size)
PHPAPI int php_getopt(int argc, char *const *argv, const opt_struct opts[], char **optarg, int *optind, int show_err, int arg_start)
PHPAPI ZEND_COLD void php_info_print_module(zend_module_entry *zend_module)
PHPAPI ZEND_COLD void php_print_info(int flag)
PHPAPI zend_result php_lint_script(zend_file_handle *file)
PHPAPI size_t php_printf(const char *format,...)
void php_request_shutdown(void *dummy)
zend_result php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_module)
PHPAPI void php_handle_aborted_connection(void)
PHPAPI bool php_execute_script(zend_file_handle *primary_file)
void php_module_shutdown(void)
PHPAPI void php_print_version(sapi_module_struct *sapi_module)
zend_result php_request_startup(void)
int php_module_shutdown_wrapper(sapi_module_struct *sapi_globals)
const PHP_CONFIG_FILE_PATH
PHPAPI void php_output_end_all(void)
PHP_CLI_API ssize_t sapi_cli_single_write(const char *str, size_t str_length)
#define php_select(m, r, w, e, t)
#define INI_DEFAULT(name, value)
PHP_CLI_API cli_shell_callbacks_t * php_cli_get_shell_callbacks(void)
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
cli_set_process_title(string $title)
sapi_module_struct cli_server_sapi_module
int do_cli_server(int argc, char **argv)
const zend_function_entry server_additional_functions[]
#define PHP_GETOPT_INVALID_ARG
struct _opt_struct opt_struct
PHPAPI char * php_ini_scanned_files
PHPAPI char * php_ini_scanned_path
PHPAPI char * php_ini_opened_path
PHPAPI void php_ini_builder_define(struct php_ini_builder *b, const char *arg)
#define php_ini_builder_prepend_literal(b, l)
PHP_JSON_API size_t int options
#define PHP_SAFE_FD_SET(fd, set)
PHPAPI zend_class_entry * reflection_zend_extension_ptr
PHPAPI zend_class_entry * reflection_extension_ptr
PHPAPI zend_class_entry * reflection_function_ptr
PHPAPI zend_class_entry * reflection_method_ptr
PHPAPI zend_class_entry * reflection_class_ptr
struct _php_stream php_stream
struct _php_stream_context php_stream_context
#define php_stream_gets(stream, buf, maxlen)
#define php_stream_to_zval(stream, zval)
#define php_stream_open_wrapper_ex(path, mode, options, opened, context)
#define php_stream_close(stream)
#define PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE
PHPAPI void(* php_import_environment_variables)(zval *array_ptr)
PHPAPI void php_register_variable(const char *var, const char *strval, zval *track_vars_array)
int is_ps_title_available(void)
void cleanup_ps_args(char **argv)
char ** save_ps_args(int argc, char **argv)
zend_function * constructor
ZEND_API size_t(* zend_printf)(const char *format,...)
ZEND_API size_t zend_print_zval(zval *expr, int indent)
ZEND_API zend_write_func_t zend_write
#define ZEND_TSRMLS_CACHE_UPDATE()
#define ZEND_TSRMLS_CACHE_DEFINE()
ZEND_API zval * zend_read_property_ex(zend_class_entry *scope, zend_object *object, zend_string *name, bool silent, zval *rv)
ZEND_API HashTable module_registry
ZEND_API zend_result object_init_ex(zval *arg, zend_class_entry *class_type)
struct _zend_function_entry zend_function_entry
#define ZVAL_STRING(z, s)
#define ZEND_FE(name, arg_info)
#define ZVAL_STRINGL(z, s, l)
exit(string|int $status=0)
strcmp(string $string1, string $string2)
zend_string_release_ex(func->internal_function.function_name, 0)
ZEND_API bool zend_is_auto_global(zend_string *name)
#define ZEND_COMPILE_EXTENDED_INFO
ZEND_API void zend_destroy_file_handle(zend_file_handle *file_handle)
ZEND_API zend_result open_file_for_scanning(zend_file_handle *file_handle)
#define strcasecmp(s1, s2)
ZEND_API void zend_register_bool_constant(const char *name, size_t name_len, bool bval, int flags, int module_number)
ZEND_API zend_result zend_register_constant(zend_constant *c)
struct _zend_constant zend_constant
ZEND_API zend_class_entry * zend_ce_exception
ZEND_API const zend_internal_function zend_pass_function
ZEND_API void(ZEND_FASTCALL *zend_touch_vm_stack_data)(void *vm_stack_data)
ZEND_API zend_result zend_eval_string_ex(const char *str, zval *retval_ptr, const char *string_name, bool handle_exceptions)
zend_result zend_load_extension(const char *path)
ZEND_API zend_llist zend_extensions
struct _zend_extension zend_extension
union _zend_function zend_function
ZEND_API void ZEND_FASTCALL zend_hash_destroy(HashTable *ht)
ZEND_API zval *ZEND_FASTCALL zend_hash_str_update(HashTable *ht, const char *str, size_t len, zval *pData)
ZEND_API void ZEND_FASTCALL zend_hash_copy(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor)
#define zend_hash_init(ht, nSize, pHashFunction, pDestructor, persistent)
#define ZEND_HASH_MAP_FOREACH_PTR(ht, _ptr)
#define ZEND_HASH_FOREACH_END()
ZEND_API void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini)
ZEND_API void zend_strip(void)
zend_syntax_highlighter_ini syntax_highlighter_ini
struct _zend_syntax_highlighter_ini zend_syntax_highlighter_ini
ZEND_API void zend_ini_deactivate(void)
ZEND_API void display_ini_entries(zend_module_entry *module)
struct _zend_file_handle zend_file_handle
ZEND_API void zend_llist_destroy(zend_llist *l)
ZEND_API void zend_llist_copy(zend_llist *dst, zend_llist *src)
ZEND_API void zend_llist_sort(zend_llist *l, llist_compare_func_t comp_func)
ZEND_API void zend_llist_apply(zend_llist *l, llist_apply_func_t func)
struct _zend_llist_element zend_llist_element
void(* llist_apply_func_t)(void *)
struct _zend_llist zend_llist
struct _zend_module_entry zend_module_entry
ZEND_API char *ZEND_FASTCALL zend_str_tolower_dup(const char *source, size_t length)
struct _zend_class_entry zend_class_entry
#define zend_signal_startup()
ZEND_API void zend_stream_init_fp(zend_file_handle *handle, FILE *fp, const char *filename)
ZEND_API zend_string_init_interned_func_t zend_string_init_interned
#define Z_STRVAL_P(zval_p)
#define Z_CONSTANT_FLAGS(zval)
struct _zend_array HashTable
ZEND_RESULT_CODE zend_result
struct _zend_execute_data zend_execute_data
ZEND_API void zval_ptr_dtor(zval *zval_ptr)
CWD_API void virtual_cwd_activate(void)
#define VCWD_FOPEN(path, mode)
#define VCWD_REALPATH(path, real_path)