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.

1075 lines
29 KiB

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