PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/sapi/cli/php_cli.c

http://github.com/php/php-src
C | 1390 lines | 1149 code | 151 blank | 90 comment | 182 complexity | f774664da0b779318f297762f9c9be2f MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | http://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Edin Kadribasic <edink@php.net> |
  14. | Marcus Boerger <helly@php.net> |
  15. | Johannes Schlueter <johannes@php.net> |
  16. | Parts based on CGI SAPI Module by |
  17. | Rasmus Lerdorf, Stig Bakken and Zeev Suraski |
  18. +----------------------------------------------------------------------+
  19. */
  20. #include "php.h"
  21. #include "php_globals.h"
  22. #include "php_variables.h"
  23. #include "zend_hash.h"
  24. #include "zend_modules.h"
  25. #include "zend_interfaces.h"
  26. #include "ext/reflection/php_reflection.h"
  27. #include "SAPI.h"
  28. #include <stdio.h>
  29. #include "php.h"
  30. #ifdef PHP_WIN32
  31. #include "win32/time.h"
  32. #include "win32/signal.h"
  33. #include "win32/console.h"
  34. #include <process.h>
  35. #include <shellapi.h>
  36. #endif
  37. #if HAVE_SYS_TIME_H
  38. #include <sys/time.h>
  39. #endif
  40. #if HAVE_UNISTD_H
  41. #include <unistd.h>
  42. #endif
  43. #include <signal.h>
  44. #include <locale.h>
  45. #include "zend.h"
  46. #include "zend_extensions.h"
  47. #include "php_ini.h"
  48. #include "php_globals.h"
  49. #include "php_main.h"
  50. #include "fopen_wrappers.h"
  51. #include "ext/standard/php_standard.h"
  52. #include "cli.h"
  53. #ifdef PHP_WIN32
  54. #include <io.h>
  55. #include <fcntl.h>
  56. #include "win32/php_registry.h"
  57. #endif
  58. #ifdef __riscos__
  59. #include <unixlib/local.h>
  60. #endif
  61. #include "zend_compile.h"
  62. #include "zend_execute.h"
  63. #include "zend_highlight.h"
  64. #include "zend_exceptions.h"
  65. #include "php_getopt.h"
  66. #ifndef PHP_CLI_WIN32_NO_CONSOLE
  67. #include "php_cli_server.h"
  68. #endif
  69. #include "ps_title.h"
  70. #include "php_cli_process_title.h"
  71. #ifndef PHP_WIN32
  72. # define php_select(m, r, w, e, t) select(m, r, w, e, t)
  73. #else
  74. # include "win32/select.h"
  75. #endif
  76. #if defined(PHP_WIN32) && defined(HAVE_OPENSSL)
  77. # include "openssl/applink.c"
  78. #endif
  79. PHPAPI extern char *php_ini_opened_path;
  80. PHPAPI extern char *php_ini_scanned_path;
  81. PHPAPI extern char *php_ini_scanned_files;
  82. #if defined(PHP_WIN32)
  83. #if defined(ZTS)
  84. ZEND_TSRMLS_CACHE_DEFINE()
  85. #endif
  86. static DWORD orig_cp = 0;
  87. #endif
  88. #ifndef O_BINARY
  89. #define O_BINARY 0
  90. #endif
  91. #define PHP_MODE_STANDARD 1
  92. #define PHP_MODE_HIGHLIGHT 2
  93. #define PHP_MODE_LINT 4
  94. #define PHP_MODE_STRIP 5
  95. #define PHP_MODE_CLI_DIRECT 6
  96. #define PHP_MODE_PROCESS_STDIN 7
  97. #define PHP_MODE_REFLECTION_FUNCTION 8
  98. #define PHP_MODE_REFLECTION_CLASS 9
  99. #define PHP_MODE_REFLECTION_EXTENSION 10
  100. #define PHP_MODE_REFLECTION_EXT_INFO 11
  101. #define PHP_MODE_REFLECTION_ZEND_EXTENSION 12
  102. #define PHP_MODE_SHOW_INI_CONFIG 13
  103. cli_shell_callbacks_t cli_shell_callbacks = { NULL, NULL, NULL };
  104. PHP_CLI_API cli_shell_callbacks_t *php_cli_get_shell_callbacks()
  105. {
  106. return &cli_shell_callbacks;
  107. }
  108. const char HARDCODED_INI[] =
  109. "html_errors=0\n"
  110. "register_argc_argv=1\n"
  111. "implicit_flush=1\n"
  112. "output_buffering=0\n"
  113. "max_execution_time=0\n"
  114. "max_input_time=-1\n\0";
  115. const opt_struct OPTIONS[] = {
  116. {'a', 0, "interactive"},
  117. {'B', 1, "process-begin"},
  118. {'C', 0, "no-chdir"}, /* for compatibility with CGI (do not chdir to script directory) */
  119. {'c', 1, "php-ini"},
  120. {'d', 1, "define"},
  121. {'E', 1, "process-end"},
  122. {'e', 0, "profile-info"},
  123. {'F', 1, "process-file"},
  124. {'f', 1, "file"},
  125. {'h', 0, "help"},
  126. {'i', 0, "info"},
  127. {'l', 0, "syntax-check"},
  128. {'m', 0, "modules"},
  129. {'n', 0, "no-php-ini"},
  130. {'q', 0, "no-header"}, /* for compatibility with CGI (do not generate HTTP headers) */
  131. {'R', 1, "process-code"},
  132. {'H', 0, "hide-args"},
  133. {'r', 1, "run"},
  134. {'s', 0, "syntax-highlight"},
  135. {'s', 0, "syntax-highlighting"},
  136. {'S', 1, "server"},
  137. {'t', 1, "docroot"},
  138. {'w', 0, "strip"},
  139. {'?', 0, "usage"},/* help alias (both '?' and 'usage') */
  140. {'v', 0, "version"},
  141. {'z', 1, "zend-extension"},
  142. {10, 1, "rf"},
  143. {10, 1, "rfunction"},
  144. {11, 1, "rc"},
  145. {11, 1, "rclass"},
  146. {12, 1, "re"},
  147. {12, 1, "rextension"},
  148. {13, 1, "rz"},
  149. {13, 1, "rzendextension"},
  150. {14, 1, "ri"},
  151. {14, 1, "rextinfo"},
  152. {15, 0, "ini"},
  153. {'-', 0, NULL} /* end of args */
  154. };
  155. static int module_name_cmp(Bucket *f, Bucket *s) /* {{{ */
  156. {
  157. return strcasecmp(((zend_module_entry *)Z_PTR(f->val))->name,
  158. ((zend_module_entry *)Z_PTR(s->val))->name);
  159. }
  160. /* }}} */
  161. static void print_modules(void) /* {{{ */
  162. {
  163. HashTable sorted_registry;
  164. zend_module_entry *module;
  165. zend_hash_init(&sorted_registry, 50, NULL, NULL, 0);
  166. zend_hash_copy(&sorted_registry, &module_registry, NULL);
  167. zend_hash_sort(&sorted_registry, module_name_cmp, 0);
  168. ZEND_HASH_FOREACH_PTR(&sorted_registry, module) {
  169. php_printf("%s\n", module->name);
  170. } ZEND_HASH_FOREACH_END();
  171. zend_hash_destroy(&sorted_registry);
  172. }
  173. /* }}} */
  174. static void print_extension_info(zend_extension *ext) /* {{{ */
  175. {
  176. php_printf("%s\n", ext->name);
  177. }
  178. /* }}} */
  179. static int extension_name_cmp(const zend_llist_element **f, const zend_llist_element **s) /* {{{ */
  180. {
  181. zend_extension *fe = (zend_extension*)(*f)->data;
  182. zend_extension *se = (zend_extension*)(*s)->data;
  183. return strcmp(fe->name, se->name);
  184. }
  185. /* }}} */
  186. static void print_extensions(void) /* {{{ */
  187. {
  188. zend_llist sorted_exts;
  189. zend_llist_copy(&sorted_exts, &zend_extensions);
  190. sorted_exts.dtor = NULL;
  191. zend_llist_sort(&sorted_exts, extension_name_cmp);
  192. zend_llist_apply(&sorted_exts, (llist_apply_func_t) print_extension_info);
  193. zend_llist_destroy(&sorted_exts);
  194. }
  195. /* }}} */
  196. #ifndef STDOUT_FILENO
  197. #define STDOUT_FILENO 1
  198. #endif
  199. #ifndef STDERR_FILENO
  200. #define STDERR_FILENO 2
  201. #endif
  202. static inline int sapi_cli_select(php_socket_t fd)
  203. {
  204. fd_set wfd;
  205. struct timeval tv;
  206. int ret;
  207. FD_ZERO(&wfd);
  208. PHP_SAFE_FD_SET(fd, &wfd);
  209. tv.tv_sec = (long)FG(default_socket_timeout);
  210. tv.tv_usec = 0;
  211. ret = php_select(fd+1, NULL, &wfd, NULL, &tv);
  212. return ret != -1;
  213. }
  214. PHP_CLI_API ssize_t sapi_cli_single_write(const char *str, size_t str_length) /* {{{ */
  215. {
  216. ssize_t ret;
  217. if (cli_shell_callbacks.cli_shell_write) {
  218. cli_shell_callbacks.cli_shell_write(str, str_length);
  219. }
  220. #ifdef PHP_WRITE_STDOUT
  221. do {
  222. ret = write(STDOUT_FILENO, str, str_length);
  223. } while (ret <= 0 && errno == EAGAIN && sapi_cli_select(STDOUT_FILENO));
  224. #else
  225. ret = fwrite(str, 1, MIN(str_length, 16384), stdout);
  226. #endif
  227. return ret;
  228. }
  229. /* }}} */
  230. static size_t sapi_cli_ub_write(const char *str, size_t str_length) /* {{{ */
  231. {
  232. const char *ptr = str;
  233. size_t remaining = str_length;
  234. ssize_t ret;
  235. if (!str_length) {
  236. return 0;
  237. }
  238. if (cli_shell_callbacks.cli_shell_ub_write) {
  239. size_t ub_wrote;
  240. ub_wrote = cli_shell_callbacks.cli_shell_ub_write(str, str_length);
  241. if (ub_wrote != (size_t) -1) {
  242. return ub_wrote;
  243. }
  244. }
  245. while (remaining > 0)
  246. {
  247. ret = sapi_cli_single_write(ptr, remaining);
  248. if (ret < 0) {
  249. #ifndef PHP_CLI_WIN32_NO_CONSOLE
  250. EG(exit_status) = 255;
  251. php_handle_aborted_connection();
  252. #endif
  253. break;
  254. }
  255. ptr += ret;
  256. remaining -= ret;
  257. }
  258. return (ptr - str);
  259. }
  260. /* }}} */
  261. static void sapi_cli_flush(void *server_context) /* {{{ */
  262. {
  263. /* Ignore EBADF here, it's caused by the fact that STDIN/STDOUT/STDERR streams
  264. * are/could be closed before fflush() is called.
  265. */
  266. if (fflush(stdout)==EOF && errno!=EBADF) {
  267. #ifndef PHP_CLI_WIN32_NO_CONSOLE
  268. php_handle_aborted_connection();
  269. #endif
  270. }
  271. }
  272. /* }}} */
  273. static char *php_self = "";
  274. static char *script_filename = "";
  275. static void sapi_cli_register_variables(zval *track_vars_array) /* {{{ */
  276. {
  277. size_t len;
  278. char *docroot = "";
  279. /* In CGI mode, we consider the environment to be a part of the server
  280. * variables
  281. */
  282. php_import_environment_variables(track_vars_array);
  283. /* Build the special-case PHP_SELF variable for the CLI version */
  284. len = strlen(php_self);
  285. if (sapi_module.input_filter(PARSE_SERVER, "PHP_SELF", &php_self, len, &len)) {
  286. php_register_variable("PHP_SELF", php_self, track_vars_array);
  287. }
  288. if (sapi_module.input_filter(PARSE_SERVER, "SCRIPT_NAME", &php_self, len, &len)) {
  289. php_register_variable("SCRIPT_NAME", php_self, track_vars_array);
  290. }
  291. /* filenames are empty for stdin */
  292. len = strlen(script_filename);
  293. if (sapi_module.input_filter(PARSE_SERVER, "SCRIPT_FILENAME", &script_filename, len, &len)) {
  294. php_register_variable("SCRIPT_FILENAME", script_filename, track_vars_array);
  295. }
  296. if (sapi_module.input_filter(PARSE_SERVER, "PATH_TRANSLATED", &script_filename, len, &len)) {
  297. php_register_variable("PATH_TRANSLATED", script_filename, track_vars_array);
  298. }
  299. /* just make it available */
  300. len = 0U;
  301. if (sapi_module.input_filter(PARSE_SERVER, "DOCUMENT_ROOT", &docroot, len, &len)) {
  302. php_register_variable("DOCUMENT_ROOT", docroot, track_vars_array);
  303. }
  304. }
  305. /* }}} */
  306. static void sapi_cli_log_message(char *message, int syslog_type_int) /* {{{ */
  307. {
  308. fprintf(stderr, "%s\n", message);
  309. #ifdef PHP_WIN32
  310. fflush(stderr);
  311. #endif
  312. }
  313. /* }}} */
  314. static int sapi_cli_deactivate(void) /* {{{ */
  315. {
  316. fflush(stdout);
  317. if(SG(request_info).argv0) {
  318. free(SG(request_info).argv0);
  319. SG(request_info).argv0 = NULL;
  320. }
  321. return SUCCESS;
  322. }
  323. /* }}} */
  324. static char* sapi_cli_read_cookies(void) /* {{{ */
  325. {
  326. return NULL;
  327. }
  328. /* }}} */
  329. static int sapi_cli_header_handler(sapi_header_struct *h, sapi_header_op_enum op, sapi_headers_struct *s) /* {{{ */
  330. {
  331. return 0;
  332. }
  333. /* }}} */
  334. static int sapi_cli_send_headers(sapi_headers_struct *sapi_headers) /* {{{ */
  335. {
  336. /* We do nothing here, this function is needed to prevent that the fallback
  337. * header handling is called. */
  338. return SAPI_HEADER_SENT_SUCCESSFULLY;
  339. }
  340. /* }}} */
  341. static void sapi_cli_send_header(sapi_header_struct *sapi_header, void *server_context) /* {{{ */
  342. {
  343. }
  344. /* }}} */
  345. static int php_cli_startup(sapi_module_struct *sapi_module) /* {{{ */
  346. {
  347. if (php_module_startup(sapi_module, NULL, 0)==FAILURE) {
  348. return FAILURE;
  349. }
  350. return SUCCESS;
  351. }
  352. /* }}} */
  353. /* {{{ sapi_cli_ini_defaults */
  354. /* overwritable ini defaults must be set in sapi_cli_ini_defaults() */
  355. #define INI_DEFAULT(name,value)\
  356. ZVAL_NEW_STR(&tmp, zend_string_init(value, sizeof(value)-1, 1));\
  357. zend_hash_str_update(configuration_hash, name, sizeof(name)-1, &tmp);\
  358. static void sapi_cli_ini_defaults(HashTable *configuration_hash)
  359. {
  360. zval tmp;
  361. INI_DEFAULT("report_zend_debug", "0");
  362. INI_DEFAULT("display_errors", "1");
  363. }
  364. /* }}} */
  365. /* {{{ sapi_module_struct cli_sapi_module
  366. */
  367. static sapi_module_struct cli_sapi_module = {
  368. "cli", /* name */
  369. "Command Line Interface", /* pretty name */
  370. php_cli_startup, /* startup */
  371. php_module_shutdown_wrapper, /* shutdown */
  372. NULL, /* activate */
  373. sapi_cli_deactivate, /* deactivate */
  374. sapi_cli_ub_write, /* unbuffered write */
  375. sapi_cli_flush, /* flush */
  376. NULL, /* get uid */
  377. NULL, /* getenv */
  378. php_error, /* error handler */
  379. sapi_cli_header_handler, /* header handler */
  380. sapi_cli_send_headers, /* send headers handler */
  381. sapi_cli_send_header, /* send header handler */
  382. NULL, /* read POST data */
  383. sapi_cli_read_cookies, /* read Cookies */
  384. sapi_cli_register_variables, /* register server variables */
  385. sapi_cli_log_message, /* Log message */
  386. NULL, /* Get request time */
  387. NULL, /* Child terminate */
  388. STANDARD_SAPI_MODULE_PROPERTIES
  389. };
  390. /* }}} */
  391. /* {{{ arginfo ext/standard/dl.c */
  392. ZEND_BEGIN_ARG_INFO(arginfo_dl, 0)
  393. ZEND_ARG_INFO(0, extension_filename)
  394. ZEND_END_ARG_INFO()
  395. /* }}} */
  396. static const zend_function_entry additional_functions[] = {
  397. ZEND_FE(dl, arginfo_dl)
  398. PHP_FE(cli_set_process_title, arginfo_cli_set_process_title)
  399. PHP_FE(cli_get_process_title, arginfo_cli_get_process_title)
  400. PHP_FE_END
  401. };
  402. /* {{{ php_cli_usage
  403. */
  404. static void php_cli_usage(char *argv0)
  405. {
  406. char *prog;
  407. prog = strrchr(argv0, '/');
  408. if (prog) {
  409. prog++;
  410. } else {
  411. prog = "php";
  412. }
  413. printf( "Usage: %s [options] [-f] <file> [--] [args...]\n"
  414. " %s [options] -r <code> [--] [args...]\n"
  415. " %s [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]\n"
  416. " %s [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]\n"
  417. " %s [options] -S <addr>:<port> [-t docroot] [router]\n"
  418. " %s [options] -- [args...]\n"
  419. " %s [options] -a\n"
  420. "\n"
  421. #if (HAVE_LIBREADLINE || HAVE_LIBEDIT) && !defined(COMPILE_DL_READLINE)
  422. " -a Run as interactive shell\n"
  423. #else
  424. " -a Run interactively\n"
  425. #endif
  426. " -c <path>|<file> Look for php.ini file in this directory\n"
  427. " -n No configuration (ini) files will be used\n"
  428. " -d foo[=bar] Define INI entry foo with value 'bar'\n"
  429. " -e Generate extended information for debugger/profiler\n"
  430. " -f <file> Parse and execute <file>.\n"
  431. " -h This help\n"
  432. " -i PHP information\n"
  433. " -l Syntax check only (lint)\n"
  434. " -m Show compiled in modules\n"
  435. " -r <code> Run PHP <code> without using script tags <?..?>\n"
  436. " -B <begin_code> Run PHP <begin_code> before processing input lines\n"
  437. " -R <code> Run PHP <code> for every input line\n"
  438. " -F <file> Parse and execute <file> for every input line\n"
  439. " -E <end_code> Run PHP <end_code> after processing all input lines\n"
  440. " -H Hide any passed arguments from external tools.\n"
  441. " -S <addr>:<port> Run with built-in web server.\n"
  442. " -t <docroot> Specify document root <docroot> for built-in web server.\n"
  443. " -s Output HTML syntax highlighted source.\n"
  444. " -v Version number\n"
  445. " -w Output source with stripped comments and whitespace.\n"
  446. " -z <file> Load Zend extension <file>.\n"
  447. "\n"
  448. " args... Arguments passed to script. Use -- args when first argument\n"
  449. " starts with - or script is read from stdin\n"
  450. "\n"
  451. " --ini Show configuration file names\n"
  452. "\n"
  453. " --rf <name> Show information about function <name>.\n"
  454. " --rc <name> Show information about class <name>.\n"
  455. " --re <name> Show information about extension <name>.\n"
  456. " --rz <name> Show information about Zend extension <name>.\n"
  457. " --ri <name> Show configuration for extension <name>.\n"
  458. "\n"
  459. , prog, prog, prog, prog, prog, prog, prog);
  460. }
  461. /* }}} */
  462. static php_stream *s_in_process = NULL;
  463. static void cli_register_file_handles(void) /* {{{ */
  464. {
  465. php_stream *s_in, *s_out, *s_err;
  466. php_stream_context *sc_in=NULL, *sc_out=NULL, *sc_err=NULL;
  467. zend_constant ic, oc, ec;
  468. s_in = php_stream_open_wrapper_ex("php://stdin", "rb", 0, NULL, sc_in);
  469. s_out = php_stream_open_wrapper_ex("php://stdout", "wb", 0, NULL, sc_out);
  470. s_err = php_stream_open_wrapper_ex("php://stderr", "wb", 0, NULL, sc_err);
  471. if (s_in==NULL || s_out==NULL || s_err==NULL) {
  472. if (s_in) php_stream_close(s_in);
  473. if (s_out) php_stream_close(s_out);
  474. if (s_err) php_stream_close(s_err);
  475. return;
  476. }
  477. #if PHP_DEBUG
  478. /* do not close stdout and stderr */
  479. s_out->flags |= PHP_STREAM_FLAG_NO_CLOSE;
  480. s_err->flags |= PHP_STREAM_FLAG_NO_CLOSE;
  481. #endif
  482. s_in_process = s_in;
  483. php_stream_to_zval(s_in, &ic.value);
  484. php_stream_to_zval(s_out, &oc.value);
  485. php_stream_to_zval(s_err, &ec.value);
  486. ZEND_CONSTANT_SET_FLAGS(&ic, CONST_CS, 0);
  487. ic.name = zend_string_init_interned("STDIN", sizeof("STDIN")-1, 0);
  488. zend_register_constant(&ic);
  489. ZEND_CONSTANT_SET_FLAGS(&oc, CONST_CS, 0);
  490. oc.name = zend_string_init_interned("STDOUT", sizeof("STDOUT")-1, 0);
  491. zend_register_constant(&oc);
  492. ZEND_CONSTANT_SET_FLAGS(&ec, CONST_CS, 0);
  493. ec.name = zend_string_init_interned("STDERR", sizeof("STDERR")-1, 0);
  494. zend_register_constant(&ec);
  495. }
  496. /* }}} */
  497. static const char *param_mode_conflict = "Either execute direct code, process stdin or use a file.\n";
  498. /* {{{ cli_seek_file_begin
  499. */
  500. static int cli_seek_file_begin(zend_file_handle *file_handle, char *script_file)
  501. {
  502. FILE *fp = VCWD_FOPEN(script_file, "rb");
  503. if (!fp) {
  504. php_printf("Could not open input file: %s\n", script_file);
  505. return FAILURE;
  506. }
  507. zend_stream_init_fp(file_handle, fp, script_file);
  508. return SUCCESS;
  509. }
  510. /* }}} */
  511. /*{{{ php_cli_win32_ctrl_handler */
  512. #if defined(PHP_WIN32)
  513. BOOL WINAPI php_cli_win32_ctrl_handler(DWORD sig)
  514. {
  515. (void)php_win32_cp_cli_do_restore(orig_cp);
  516. return FALSE;
  517. }
  518. #endif
  519. /*}}}*/
  520. static int do_cli(int argc, char **argv) /* {{{ */
  521. {
  522. int c;
  523. zend_file_handle file_handle;
  524. int behavior = PHP_MODE_STANDARD;
  525. char *reflection_what = NULL;
  526. volatile int request_started = 0;
  527. volatile int exit_status = 0;
  528. char *php_optarg = NULL, *orig_optarg = NULL;
  529. int php_optind = 1, orig_optind = 1;
  530. char *exec_direct=NULL, *exec_run=NULL, *exec_begin=NULL, *exec_end=NULL;
  531. char *arg_free=NULL, **arg_excp=&arg_free;
  532. char *script_file=NULL, *translated_path = NULL;
  533. int interactive=0;
  534. const char *param_error=NULL;
  535. int hide_argv = 0;
  536. zend_try {
  537. CG(in_compilation) = 0; /* not initialized but needed for several options */
  538. while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
  539. switch (c) {
  540. case 'i': /* php info & quit */
  541. if (php_request_startup()==FAILURE) {
  542. goto err;
  543. }
  544. request_started = 1;
  545. php_print_info(0xFFFFFFFF);
  546. php_output_end_all();
  547. exit_status = (c == '?' && argc > 1 && !strchr(argv[1], c));
  548. goto out;
  549. case 'v': /* show php version & quit */
  550. php_printf("PHP %s (%s) (built: %s %s) ( %s)\nCopyright (c) The PHP Group\n%s",
  551. PHP_VERSION, cli_sapi_module.name, __DATE__, __TIME__,
  552. #if ZTS
  553. "ZTS "
  554. #else
  555. "NTS "
  556. #endif
  557. #ifdef COMPILER
  558. COMPILER
  559. " "
  560. #endif
  561. #ifdef ARCHITECTURE
  562. ARCHITECTURE
  563. " "
  564. #endif
  565. #if ZEND_DEBUG
  566. "DEBUG "
  567. #endif
  568. #ifdef HAVE_GCOV
  569. "GCOV "
  570. #endif
  571. ,
  572. get_zend_version()
  573. );
  574. sapi_deactivate();
  575. goto out;
  576. case 'm': /* list compiled in modules */
  577. if (php_request_startup()==FAILURE) {
  578. goto err;
  579. }
  580. request_started = 1;
  581. php_printf("[PHP Modules]\n");
  582. print_modules();
  583. php_printf("\n[Zend Modules]\n");
  584. print_extensions();
  585. php_printf("\n");
  586. php_output_end_all();
  587. exit_status=0;
  588. goto out;
  589. default:
  590. break;
  591. }
  592. }
  593. /* Set some CLI defaults */
  594. SG(options) |= SAPI_OPTION_NO_CHDIR;
  595. php_optind = orig_optind;
  596. php_optarg = orig_optarg;
  597. while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
  598. switch (c) {
  599. case 'a': /* interactive mode */
  600. if (!interactive) {
  601. if (behavior != PHP_MODE_STANDARD) {
  602. param_error = param_mode_conflict;
  603. break;
  604. }
  605. interactive=1;
  606. }
  607. break;
  608. case 'C': /* don't chdir to the script directory */
  609. /* This is default so NOP */
  610. break;
  611. case 'F':
  612. if (behavior == PHP_MODE_PROCESS_STDIN) {
  613. if (exec_run || script_file) {
  614. param_error = "You can use -R or -F only once.\n";
  615. break;
  616. }
  617. } else if (behavior != PHP_MODE_STANDARD) {
  618. param_error = param_mode_conflict;
  619. break;
  620. }
  621. behavior=PHP_MODE_PROCESS_STDIN;
  622. script_file = php_optarg;
  623. break;
  624. case 'f': /* parse file */
  625. if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
  626. param_error = param_mode_conflict;
  627. break;
  628. } else if (script_file) {
  629. param_error = "You can use -f only once.\n";
  630. break;
  631. }
  632. script_file = php_optarg;
  633. break;
  634. case 'l': /* syntax check mode */
  635. if (behavior != PHP_MODE_STANDARD) {
  636. break;
  637. }
  638. behavior=PHP_MODE_LINT;
  639. break;
  640. case 'q': /* do not generate HTTP headers */
  641. /* This is default so NOP */
  642. break;
  643. case 'r': /* run code from command line */
  644. if (behavior == PHP_MODE_CLI_DIRECT) {
  645. if (exec_direct || script_file) {
  646. param_error = "You can use -r only once.\n";
  647. break;
  648. }
  649. } else if (behavior != PHP_MODE_STANDARD || interactive) {
  650. param_error = param_mode_conflict;
  651. break;
  652. }
  653. behavior=PHP_MODE_CLI_DIRECT;
  654. exec_direct=php_optarg;
  655. break;
  656. case 'R':
  657. if (behavior == PHP_MODE_PROCESS_STDIN) {
  658. if (exec_run || script_file) {
  659. param_error = "You can use -R or -F only once.\n";
  660. break;
  661. }
  662. } else if (behavior != PHP_MODE_STANDARD) {
  663. param_error = param_mode_conflict;
  664. break;
  665. }
  666. behavior=PHP_MODE_PROCESS_STDIN;
  667. exec_run=php_optarg;
  668. break;
  669. case 'B':
  670. if (behavior == PHP_MODE_PROCESS_STDIN) {
  671. if (exec_begin) {
  672. param_error = "You can use -B only once.\n";
  673. break;
  674. }
  675. } else if (behavior != PHP_MODE_STANDARD || interactive) {
  676. param_error = param_mode_conflict;
  677. break;
  678. }
  679. behavior=PHP_MODE_PROCESS_STDIN;
  680. exec_begin=php_optarg;
  681. break;
  682. case 'E':
  683. if (behavior == PHP_MODE_PROCESS_STDIN) {
  684. if (exec_end) {
  685. param_error = "You can use -E only once.\n";
  686. break;
  687. }
  688. } else if (behavior != PHP_MODE_STANDARD || interactive) {
  689. param_error = param_mode_conflict;
  690. break;
  691. }
  692. behavior=PHP_MODE_PROCESS_STDIN;
  693. exec_end=php_optarg;
  694. break;
  695. case 's': /* generate highlighted HTML from source */
  696. if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
  697. param_error = "Source highlighting only works for files.\n";
  698. break;
  699. }
  700. behavior=PHP_MODE_HIGHLIGHT;
  701. break;
  702. case 'w':
  703. if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
  704. param_error = "Source stripping only works for files.\n";
  705. break;
  706. }
  707. behavior=PHP_MODE_STRIP;
  708. break;
  709. case 'z': /* load extension file */
  710. zend_load_extension(php_optarg);
  711. break;
  712. case 'H':
  713. hide_argv = 1;
  714. break;
  715. case 10:
  716. behavior=PHP_MODE_REFLECTION_FUNCTION;
  717. reflection_what = php_optarg;
  718. break;
  719. case 11:
  720. behavior=PHP_MODE_REFLECTION_CLASS;
  721. reflection_what = php_optarg;
  722. break;
  723. case 12:
  724. behavior=PHP_MODE_REFLECTION_EXTENSION;
  725. reflection_what = php_optarg;
  726. break;
  727. case 13:
  728. behavior=PHP_MODE_REFLECTION_ZEND_EXTENSION;
  729. reflection_what = php_optarg;
  730. break;
  731. case 14:
  732. behavior=PHP_MODE_REFLECTION_EXT_INFO;
  733. reflection_what = php_optarg;
  734. break;
  735. case 15:
  736. behavior = PHP_MODE_SHOW_INI_CONFIG;
  737. break;
  738. default:
  739. break;
  740. }
  741. }
  742. if (param_error) {
  743. PUTS(param_error);
  744. exit_status=1;
  745. goto err;
  746. }
  747. #if defined(PHP_WIN32) && !defined(PHP_CLI_WIN32_NO_CONSOLE) && (HAVE_LIBREADLINE || HAVE_LIBEDIT) && !defined(COMPILE_DL_READLINE)
  748. if (!interactive) {
  749. /* The -a option was not passed. If there is no file, it could
  750. still make sense to run interactively. The presence of a file
  751. is essential to mitigate buggy console info. */
  752. interactive = php_win32_console_is_own() &&
  753. !(script_file ||
  754. argc > php_optind && behavior!=PHP_MODE_CLI_DIRECT &&
  755. behavior!=PHP_MODE_PROCESS_STDIN &&
  756. strcmp(argv[php_optind-1],"--")
  757. );
  758. }
  759. #endif
  760. if (interactive) {
  761. #if (HAVE_LIBREADLINE || HAVE_LIBEDIT) && !defined(COMPILE_DL_READLINE)
  762. printf("Interactive shell\n\n");
  763. #else
  764. printf("Interactive mode enabled\n\n");
  765. #endif
  766. fflush(stdout);
  767. }
  768. /* only set script_file if not set already and not in direct mode and not at end of parameter list */
  769. if (argc > php_optind
  770. && !script_file
  771. && behavior!=PHP_MODE_CLI_DIRECT
  772. && behavior!=PHP_MODE_PROCESS_STDIN
  773. && strcmp(argv[php_optind-1],"--"))
  774. {
  775. script_file=argv[php_optind];
  776. php_optind++;
  777. }
  778. if (script_file) {
  779. if (cli_seek_file_begin(&file_handle, script_file) != SUCCESS) {
  780. goto err;
  781. } else {
  782. char real_path[MAXPATHLEN];
  783. if (VCWD_REALPATH(script_file, real_path)) {
  784. translated_path = strdup(real_path);
  785. }
  786. script_filename = script_file;
  787. }
  788. } else {
  789. /* We could handle PHP_MODE_PROCESS_STDIN in a different manner */
  790. /* here but this would make things only more complicated. And it */
  791. /* is consistent with the way -R works where the stdin file handle*/
  792. /* is also accessible. */
  793. zend_stream_init_fp(&file_handle, stdin, "Standard input code");
  794. }
  795. php_self = (char*)file_handle.filename;
  796. /* before registering argv to module exchange the *new* argv[0] */
  797. /* we can achieve this without allocating more memory */
  798. SG(request_info).argc=argc-php_optind+1;
  799. arg_excp = argv+php_optind-1;
  800. arg_free = argv[php_optind-1];
  801. SG(request_info).path_translated = translated_path? translated_path: (char*)file_handle.filename;
  802. argv[php_optind-1] = (char*)file_handle.filename;
  803. SG(request_info).argv=argv+php_optind-1;
  804. if (php_request_startup()==FAILURE) {
  805. *arg_excp = arg_free;
  806. fclose(file_handle.handle.fp);
  807. PUTS("Could not startup.\n");
  808. goto err;
  809. }
  810. request_started = 1;
  811. CG(skip_shebang) = 1;
  812. zend_register_bool_constant(
  813. ZEND_STRL("PHP_CLI_PROCESS_TITLE"),
  814. is_ps_title_available() == PS_TITLE_SUCCESS,
  815. CONST_CS, 0);
  816. *arg_excp = arg_free; /* reconstuct argv */
  817. if (hide_argv) {
  818. int i;
  819. for (i = 1; i < argc; i++) {
  820. memset(argv[i], 0, strlen(argv[i]));
  821. }
  822. }
  823. zend_is_auto_global_str(ZEND_STRL("_SERVER"));
  824. PG(during_request_startup) = 0;
  825. switch (behavior) {
  826. case PHP_MODE_STANDARD:
  827. if (strcmp(file_handle.filename, "Standard input code")) {
  828. cli_register_file_handles();
  829. }
  830. if (interactive && cli_shell_callbacks.cli_shell_run) {
  831. exit_status = cli_shell_callbacks.cli_shell_run();
  832. } else {
  833. php_execute_script(&file_handle);
  834. exit_status = EG(exit_status);
  835. }
  836. break;
  837. case PHP_MODE_LINT:
  838. exit_status = php_lint_script(&file_handle);
  839. if (exit_status==SUCCESS) {
  840. zend_printf("No syntax errors detected in %s\n", file_handle.filename);
  841. } else {
  842. zend_printf("Errors parsing %s\n", file_handle.filename);
  843. }
  844. break;
  845. case PHP_MODE_STRIP:
  846. if (open_file_for_scanning(&file_handle)==SUCCESS) {
  847. zend_strip();
  848. }
  849. goto out;
  850. break;
  851. case PHP_MODE_HIGHLIGHT:
  852. {
  853. zend_syntax_highlighter_ini syntax_highlighter_ini;
  854. if (open_file_for_scanning(&file_handle)==SUCCESS) {
  855. php_get_highlight_struct(&syntax_highlighter_ini);
  856. zend_highlight(&syntax_highlighter_ini);
  857. }
  858. goto out;
  859. }
  860. break;
  861. case PHP_MODE_CLI_DIRECT:
  862. cli_register_file_handles();
  863. if (zend_eval_string_ex(exec_direct, NULL, "Command line code", 1) == FAILURE) {
  864. exit_status=254;
  865. }
  866. break;
  867. case PHP_MODE_PROCESS_STDIN:
  868. {
  869. char *input;
  870. size_t len, index = 0;
  871. zval argn, argi;
  872. cli_register_file_handles();
  873. if (exec_begin && zend_eval_string_ex(exec_begin, NULL, "Command line begin code", 1) == FAILURE) {
  874. exit_status=254;
  875. }
  876. while (exit_status == SUCCESS && (input=php_stream_gets(s_in_process, NULL, 0)) != NULL) {
  877. len = strlen(input);
  878. while (len > 0 && len-- && (input[len]=='\n' || input[len]=='\r')) {
  879. input[len] = '\0';
  880. }
  881. ZVAL_STRINGL(&argn, input, len + 1);
  882. zend_hash_str_update(&EG(symbol_table), "argn", sizeof("argn")-1, &argn);
  883. ZVAL_LONG(&argi, ++index);
  884. zend_hash_str_update(&EG(symbol_table), "argi", sizeof("argi")-1, &argi);
  885. if (exec_run) {
  886. if (zend_eval_string_ex(exec_run, NULL, "Command line run code", 1) == FAILURE) {
  887. exit_status=254;
  888. }
  889. } else {
  890. if (script_file) {
  891. if (cli_seek_file_begin(&file_handle, script_file) != SUCCESS) {
  892. exit_status = 1;
  893. } else {
  894. CG(skip_shebang) = 1;
  895. php_execute_script(&file_handle);
  896. exit_status = EG(exit_status);
  897. }
  898. }
  899. }
  900. efree(input);
  901. }
  902. if (exec_end && zend_eval_string_ex(exec_end, NULL, "Command line end code", 1) == FAILURE) {
  903. exit_status=254;
  904. }
  905. break;
  906. }
  907. case PHP_MODE_REFLECTION_FUNCTION:
  908. case PHP_MODE_REFLECTION_CLASS:
  909. case PHP_MODE_REFLECTION_EXTENSION:
  910. case PHP_MODE_REFLECTION_ZEND_EXTENSION:
  911. {
  912. zend_class_entry *pce = NULL;
  913. zval arg, ref;
  914. zend_execute_data execute_data;
  915. switch (behavior) {
  916. default:
  917. break;
  918. case PHP_MODE_REFLECTION_FUNCTION:
  919. if (strstr(reflection_what, "::")) {
  920. pce = reflection_method_ptr;
  921. } else {
  922. pce = reflection_function_ptr;
  923. }
  924. break;
  925. case PHP_MODE_REFLECTION_CLASS:
  926. pce = reflection_class_ptr;
  927. break;
  928. case PHP_MODE_REFLECTION_EXTENSION:
  929. pce = reflection_extension_ptr;
  930. break;
  931. case PHP_MODE_REFLECTION_ZEND_EXTENSION:
  932. pce = reflection_zend_extension_ptr;
  933. break;
  934. }
  935. ZVAL_STRING(&arg, reflection_what);
  936. object_init_ex(&ref, pce);
  937. memset(&execute_data, 0, sizeof(zend_execute_data));
  938. EG(current_execute_data) = &execute_data;
  939. zend_call_method_with_1_params(Z_OBJ(ref), pce, &pce->constructor, "__construct", NULL, &arg);
  940. if (EG(exception)) {
  941. zval tmp, *msg, rv;
  942. ZVAL_OBJ(&tmp, EG(exception));
  943. msg = zend_read_property(zend_ce_exception, &tmp, "message", sizeof("message")-1, 0, &rv);
  944. zend_printf("Exception: %s\n", Z_STRVAL_P(msg));
  945. zval_ptr_dtor(&tmp);
  946. EG(exception) = NULL;
  947. } else {
  948. zend_print_zval(&ref, 0);
  949. zend_write("\n", 1);
  950. }
  951. zval_ptr_dtor(&ref);
  952. zval_ptr_dtor(&arg);
  953. break;
  954. }
  955. case PHP_MODE_REFLECTION_EXT_INFO:
  956. {
  957. size_t len = strlen(reflection_what);
  958. char *lcname = zend_str_tolower_dup(reflection_what, len);
  959. zend_module_entry *module;
  960. if ((module = zend_hash_str_find_ptr(&module_registry, lcname, len)) == NULL) {
  961. if (!strcmp(reflection_what, "main")) {
  962. display_ini_entries(NULL);
  963. } else {
  964. zend_printf("Extension '%s' not present.\n", reflection_what);
  965. exit_status = 1;
  966. }
  967. } else {
  968. php_info_print_module(module);
  969. }
  970. efree(lcname);
  971. break;
  972. }
  973. case PHP_MODE_SHOW_INI_CONFIG:
  974. {
  975. zend_printf("Configuration File (php.ini) Path: %s\n", PHP_CONFIG_FILE_PATH);
  976. zend_printf("Loaded Configuration File: %s\n", php_ini_opened_path ? php_ini_opened_path : "(none)");
  977. zend_printf("Scan for additional .ini files in: %s\n", php_ini_scanned_path ? php_ini_scanned_path : "(none)");
  978. zend_printf("Additional .ini files parsed: %s\n", php_ini_scanned_files ? php_ini_scanned_files : "(none)");
  979. break;
  980. }
  981. }
  982. } zend_end_try();
  983. out:
  984. if (request_started) {
  985. php_request_shutdown((void *) 0);
  986. }
  987. if (translated_path) {
  988. free(translated_path);
  989. }
  990. if (exit_status == 0) {
  991. exit_status = EG(exit_status);
  992. }
  993. return exit_status;
  994. err:
  995. sapi_deactivate();
  996. zend_ini_deactivate();
  997. exit_status = 1;
  998. goto out;
  999. }
  1000. /* }}} */
  1001. /* {{{ main
  1002. */
  1003. #ifdef PHP_CLI_WIN32_NO_CONSOLE
  1004. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  1005. #else
  1006. int main(int argc, char *argv[])
  1007. #endif
  1008. {
  1009. #if defined(PHP_WIN32)
  1010. # ifdef PHP_CLI_WIN32_NO_CONSOLE
  1011. int argc = __argc;
  1012. char **argv = __argv;
  1013. # endif
  1014. int num_args;
  1015. wchar_t **argv_wide;
  1016. char **argv_save = argv;
  1017. BOOL using_wide_argv = 0;
  1018. #endif
  1019. int c;
  1020. int exit_status = SUCCESS;
  1021. int module_started = 0, sapi_started = 0;
  1022. char *php_optarg = NULL;
  1023. int php_optind = 1, use_extended_info = 0;
  1024. char *ini_path_override = NULL;
  1025. char *ini_entries = NULL;
  1026. size_t ini_entries_len = 0;
  1027. int ini_ignore = 0;
  1028. sapi_module_struct *sapi_module = &cli_sapi_module;
  1029. /*
  1030. * Do not move this initialization. It needs to happen before argv is used
  1031. * in any way.
  1032. */
  1033. argv = save_ps_args(argc, argv);
  1034. #if defined(PHP_WIN32) && !defined(PHP_CLI_WIN32_NO_CONSOLE)
  1035. php_win32_console_fileno_set_vt100(STDOUT_FILENO, TRUE);
  1036. php_win32_console_fileno_set_vt100(STDERR_FILENO, TRUE);
  1037. #endif
  1038. cli_sapi_module.additional_functions = additional_functions;
  1039. #if defined(PHP_WIN32) && defined(_DEBUG) && defined(PHP_WIN32_DEBUG_HEAP)
  1040. {
  1041. int tmp_flag;
  1042. _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
  1043. _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
  1044. _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
  1045. _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
  1046. _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
  1047. _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
  1048. tmp_flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
  1049. tmp_flag |= _CRTDBG_DELAY_FREE_MEM_DF;
  1050. tmp_flag |= _CRTDBG_LEAK_CHECK_DF;
  1051. _CrtSetDbgFlag(tmp_flag);
  1052. }
  1053. #endif
  1054. #if defined(SIGPIPE) && defined(SIG_IGN)
  1055. signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
  1056. that sockets created via fsockopen()
  1057. don't kill PHP if the remote site
  1058. closes it. in apache|apxs mode apache
  1059. does that for us! thies@thieso.net
  1060. 20000419 */
  1061. #endif
  1062. #ifdef ZTS
  1063. php_tsrm_startup();
  1064. # ifdef PHP_WIN32
  1065. ZEND_TSRMLS_CACHE_UPDATE();
  1066. # endif
  1067. #endif
  1068. zend_signal_startup();
  1069. #ifdef PHP_WIN32
  1070. _fmode = _O_BINARY; /*sets default for file streams to binary */
  1071. setmode(_fileno(stdin), O_BINARY); /* make the stdio mode be binary */
  1072. setmode(_fileno(stdout), O_BINARY); /* make the stdio mode be binary */
  1073. setmode(_fileno(stderr), O_BINARY); /* make the stdio mode be binary */
  1074. #endif
  1075. while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 1, 2))!=-1) {
  1076. switch (c) {
  1077. case 'c':
  1078. if (ini_path_override) {
  1079. free(ini_path_override);
  1080. }
  1081. ini_path_override = strdup(php_optarg);
  1082. break;
  1083. case 'n':
  1084. ini_ignore = 1;
  1085. break;
  1086. case 'd': {
  1087. /* define ini entries on command line */
  1088. size_t len = strlen(php_optarg);
  1089. char *val;
  1090. if ((val = strchr(php_optarg, '='))) {
  1091. val++;
  1092. if (!isalnum(*val) && *val != '"' && *val != '\'' && *val != '\0') {
  1093. ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("\"\"\n\0"));
  1094. memcpy(ini_entries + ini_entries_len, php_optarg, (val - php_optarg));
  1095. ini_entries_len += (val - php_optarg);
  1096. memcpy(ini_entries + ini_entries_len, "\"", 1);
  1097. ini_entries_len++;
  1098. memcpy(ini_entries + ini_entries_len, val, len - (val - php_optarg));
  1099. ini_entries_len += len - (val - php_optarg);
  1100. memcpy(ini_entries + ini_entries_len, "\"\n\0", sizeof("\"\n\0"));
  1101. ini_entries_len += sizeof("\n\0\"") - 2;
  1102. } else {
  1103. ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("\n\0"));
  1104. memcpy(ini_entries + ini_entries_len, php_optarg, len);
  1105. memcpy(ini_entries + ini_entries_len + len, "\n\0", sizeof("\n\0"));
  1106. ini_entries_len += len + sizeof("\n\0") - 2;
  1107. }
  1108. } else {
  1109. ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("=1\n\0"));
  1110. memcpy(ini_entries + ini_entries_len, php_optarg, len);
  1111. memcpy(ini_entries + ini_entries_len + len, "=1\n\0", sizeof("=1\n\0"));
  1112. ini_entries_len += len + sizeof("=1\n\0") - 2;
  1113. }
  1114. break;
  1115. }
  1116. #ifndef PHP_CLI_WIN32_NO_CONSOLE
  1117. case 'S':
  1118. sapi_module = &cli_server_sapi_module;
  1119. cli_server_sapi_module.additional_functions = server_additional_functions;
  1120. break;
  1121. #endif
  1122. case 'h': /* help & quit */
  1123. case '?':
  1124. php_cli_usage(argv[0]);
  1125. goto out;
  1126. case PHP_GETOPT_INVALID_ARG: /* print usage on bad options, exit 1 */
  1127. php_cli_usage(argv[0]);
  1128. exit_status = 1;
  1129. goto out;
  1130. case 'i': case 'v': case 'm':
  1131. sapi_module = &cli_sapi_module;
  1132. goto exit_loop;
  1133. case 'e': /* enable extended info output */
  1134. use_extended_info = 1;
  1135. break;
  1136. }
  1137. }
  1138. exit_loop:
  1139. sapi_module->ini_defaults = sapi_cli_ini_defaults;
  1140. sapi_module->php_ini_path_override = ini_path_override;
  1141. sapi_module->phpinfo_as_text = 1;
  1142. sapi_module->php_ini_ignore_cwd = 1;
  1143. sapi_startup(sapi_module);
  1144. sapi_started = 1;
  1145. sapi_module->php_ini_ignore = ini_ignore;
  1146. sapi_module->executable_location = argv[0];
  1147. if (sapi_module == &cli_sapi_module) {
  1148. if (ini_entries) {
  1149. ini_entries = realloc(ini_entries, ini_entries_len + sizeof(HARDCODED_INI));
  1150. memmove(ini_entries + sizeof(HARDCODED_INI) - 2, ini_entries, ini_entries_len + 1);
  1151. memcpy(ini_entries, HARDCODED_INI, sizeof(HARDCODED_INI) - 2);
  1152. } else {
  1153. ini_entries = malloc(sizeof(HARDCODED_INI));
  1154. memcpy(ini_entries, HARDCODED_INI, sizeof(HARDCODED_INI));
  1155. }
  1156. ini_entries_len += sizeof(HARDCODED_INI) - 2;
  1157. }
  1158. sapi_module->ini_entries = ini_entries;
  1159. /* startup after we get the above ini override se we get things right */
  1160. if (sapi_module->startup(sapi_module) == FAILURE) {
  1161. /* there is no way to see if we must call zend_ini_deactivate()
  1162. * since we cannot check if EG(ini_directives) has been initialised
  1163. * because the executor's constructor does not set initialize it.
  1164. * Apart from that there seems no need for zend_ini_deactivate() yet.
  1165. * So we goto out_err.*/
  1166. exit_status = 1;
  1167. goto out;
  1168. }
  1169. module_started = 1;
  1170. #if defined(PHP_WIN32)
  1171. php_win32_cp_cli_setup();
  1172. orig_cp = (php_win32_cp_get_orig())->id;
  1173. /* Ignore the delivered argv and argc, read from W API. This place
  1174. might be too late though, but this is the earliest place ATW
  1175. we can access the internal charset information from PHP. */
  1176. argv_wide = CommandLineToArgvW(GetCommandLineW(), &num_args);
  1177. PHP_WIN32_CP_W_TO_ANY_ARRAY(argv_wide, num_args, argv, argc)
  1178. using_wide_argv = 1;
  1179. SetConsoleCtrlHandler(php_cli_win32_ctrl_handler, TRUE);
  1180. #endif
  1181. /* -e option */
  1182. if (use_extended_info) {
  1183. CG(compiler_options) |= ZEND_COMPILE_EXTENDED_INFO;
  1184. }
  1185. zend_first_try {
  1186. #ifndef PHP_CLI_WIN32_NO_CONSOLE
  1187. if (sapi_module == &cli_sapi_module) {
  1188. #endif
  1189. exit_status = do_cli(argc, argv);
  1190. #ifndef PHP_CLI_WIN32_NO_CONSOLE
  1191. } else {
  1192. exit_status = do_cli_server(argc, argv);
  1193. }
  1194. #endif
  1195. } zend_end_try();
  1196. out:
  1197. if (ini_path_override) {
  1198. free(ini_path_override);
  1199. }
  1200. if (ini_entries) {
  1201. free(ini_entries);
  1202. }
  1203. if (module_started) {
  1204. php_module_shutdown();
  1205. }
  1206. if (sapi_started) {
  1207. sapi_shutdown();
  1208. }
  1209. #ifdef ZTS
  1210. tsrm_shutdown();
  1211. #endif
  1212. #if defined(PHP_WIN32)
  1213. (void)php_win32_cp_cli_restore();
  1214. if (using_wide_argv) {
  1215. PHP_WIN32_CP_FREE_ARRAY(argv, argc);
  1216. LocalFree(argv_wide);
  1217. }
  1218. argv = argv_save;
  1219. #endif
  1220. /*
  1221. * Do not move this de-initialization. It needs to happen right before
  1222. * exiting.
  1223. */
  1224. cleanup_ps_args(argv);
  1225. exit(exit_status);
  1226. }
  1227. /* }}} */