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

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