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.

1285 lines
34 KiB

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