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.

1168 lines
31 KiB

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