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.

1246 lines
33 KiB

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