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.

1409 lines
39 KiB

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