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.

1399 lines
38 KiB

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