You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1359 lines
37 KiB

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