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.

697 lines
18 KiB

24 years ago
24 years ago
24 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 4 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2002 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.02 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at |
  10. | http://www.php.net/license/2_02.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. #include "php.h"
  22. #include "php_globals.h"
  23. #include "php_variables.h"
  24. #include "zend_modules.h"
  25. #include "SAPI.h"
  26. #include <stdio.h>
  27. #include "php.h"
  28. #ifdef PHP_WIN32
  29. #include "win32/time.h"
  30. #include "win32/signal.h"
  31. #include <process.h>
  32. #else
  33. #include "build-defs.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. #define PHP_MODE_STANDARD 1
  71. #define PHP_MODE_HIGHLIGHT 2
  72. #define PHP_MODE_INDENT 3
  73. #define PHP_MODE_LINT 4
  74. #define PHP_MODE_STRIP 5
  75. #define PHP_MODE_CLI_DIRECT 6
  76. extern char *ap_php_optarg;
  77. extern int ap_php_optind;
  78. #define OPTSTRING "aCc:d:ef:g:hilmnqr:sw?vz:"
  79. static int _print_module_info(zend_module_entry *module, void *arg TSRMLS_DC)
  80. {
  81. php_printf("%s\n", module->name);
  82. return 0;
  83. }
  84. static int _print_extension_info(zend_extension *module, void *arg TSRMLS_DC)
  85. {
  86. php_printf("%s\n", module->name);
  87. return 0;
  88. }
  89. #ifndef STDOUT_FILENO
  90. #define STDOUT_FILENO 1
  91. #endif
  92. static inline size_t sapi_cli_single_write(const char *str, uint str_length)
  93. {
  94. #ifdef PHP_WRITE_STDOUT
  95. long ret;
  96. ret = write(STDOUT_FILENO, str, str_length);
  97. if (ret <= 0) return 0;
  98. return ret;
  99. #else
  100. size_t ret;
  101. ret = fwrite(str, 1, MIN(str_length, 16384), stdout);
  102. return ret;
  103. #endif
  104. }
  105. static int sapi_cli_ub_write(const char *str, uint str_length TSRMLS_DC)
  106. {
  107. const char *ptr = str;
  108. uint remaining = str_length;
  109. size_t ret;
  110. while (remaining > 0)
  111. {
  112. ret = sapi_cli_single_write(ptr, remaining);
  113. if (!ret) {
  114. php_handle_aborted_connection();
  115. }
  116. ptr += ret;
  117. remaining -= ret;
  118. }
  119. return str_length;
  120. }
  121. static void sapi_cli_flush(void *server_context)
  122. {
  123. if (fflush(stdout)==EOF) {
  124. php_handle_aborted_connection();
  125. }
  126. }
  127. static void sapi_cli_register_variables(zval *track_vars_array TSRMLS_DC)
  128. {
  129. /* In CGI mode, we consider the environment to be a part of the server
  130. * variables
  131. */
  132. php_import_environment_variables(track_vars_array TSRMLS_CC);
  133. /* Build the special-case PHP_SELF variable for the CLI version */
  134. /* php_register_variable("PHP_SELF", SG(request_info).argv[0], track_vars_array TSRMLS_CC);*/
  135. }
  136. static void sapi_cli_log_message(char *message)
  137. {
  138. if (php_header()) {
  139. fprintf(stderr, "%s", message);
  140. fprintf(stderr, "\n");
  141. }
  142. }
  143. static int sapi_cli_deactivate(TSRMLS_D)
  144. {
  145. fflush(stdout);
  146. if(SG(request_info).argv0) {
  147. free(SG(request_info).argv0);
  148. SG(request_info).argv0 = NULL;
  149. }
  150. return SUCCESS;
  151. }
  152. static char* sapi_cli_read_cookies(TSRMLS_D)
  153. {
  154. return NULL;
  155. }
  156. static void sapi_cli_send_header(sapi_header_struct *sapi_header, void *server_context TSRMLS_DC)
  157. {
  158. if (sapi_header) {
  159. PHPWRITE_H(sapi_header->header, sapi_header->header_len);
  160. }
  161. PHPWRITE_H("\r\n", 2);
  162. }
  163. /* {{{ sapi_module_struct cli_sapi_module
  164. */
  165. static sapi_module_struct cli_sapi_module = {
  166. "cli", /* name */
  167. "Command Line Interface", /* pretty name */
  168. php_module_startup, /* startup */
  169. php_module_shutdown_wrapper, /* shutdown */
  170. NULL, /* activate */
  171. sapi_cli_deactivate, /* deactivate */
  172. sapi_cli_ub_write, /* unbuffered write */
  173. sapi_cli_flush, /* flush */
  174. NULL, /* get uid */
  175. NULL, /* getenv */
  176. php_error, /* error handler */
  177. NULL, /* header handler */
  178. NULL, /* send headers handler */
  179. sapi_cli_send_header, /* send header handler */
  180. NULL, /* read POST data */
  181. sapi_cli_read_cookies, /* read Cookies */
  182. sapi_cli_register_variables, /* register server variables */
  183. sapi_cli_log_message, /* Log message */
  184. NULL, /* Block interruptions */
  185. NULL, /* Unblock interruptions */
  186. STANDARD_SAPI_MODULE_PROPERTIES
  187. };
  188. /* }}} */
  189. /* {{{ php_cli_usage
  190. */
  191. static void php_cli_usage(char *argv0)
  192. {
  193. char *prog;
  194. prog = strrchr(argv0, '/');
  195. if (prog) {
  196. prog++;
  197. } else {
  198. prog = "php";
  199. }
  200. php_printf( "Usage: %s [options] [-f] <file> [args...]\n"
  201. " %s [options] -r <code> [args...]\n"
  202. " %s [options] [-- args...]\n"
  203. " -s Display colour syntax highlighted source.\n"
  204. " -w Display source with stripped comments and whitespace.\n"
  205. " -f <file> Parse <file>.\n"
  206. " -v Version number\n"
  207. " -c <path>|<file> Look for php.ini file in this directory\n"
  208. " -a Run interactively\n"
  209. " -d foo[=bar] Define INI entry foo with value 'bar'\n"
  210. " -e Generate extended information for debugger/profiler\n"
  211. " -z <file> Load Zend extension <file>.\n"
  212. " -l Syntax check only (lint)\n"
  213. " -m Show compiled in modules\n"
  214. " -i PHP information\n"
  215. " -r <code> Run PHP <code> without using script tags <?..?>\n"
  216. " -h This help\n"
  217. "\n"
  218. " args... Arguments passed to script. Use -- args when first argument \n"
  219. " starts with - or script is read from stdin\n"
  220. , prog, prog, prog);
  221. }
  222. /* }}} */
  223. static void define_command_line_ini_entry(char *arg)
  224. {
  225. char *name, *value;
  226. name = arg;
  227. value = strchr(arg, '=');
  228. if (value) {
  229. *value = 0;
  230. value++;
  231. } else {
  232. value = "1";
  233. }
  234. zend_alter_ini_entry(name, strlen(name)+1, value, strlen(value), PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
  235. }
  236. static void php_register_command_line_global_vars(char **arg TSRMLS_DC)
  237. {
  238. char *var, *val;
  239. var = *arg;
  240. val = strchr(var, '=');
  241. if (!val) {
  242. printf("No value specified for variable '%s'\n", var);
  243. } else {
  244. *val++ = '\0';
  245. php_register_variable(var, val, NULL TSRMLS_CC);
  246. }
  247. efree(*arg);
  248. }
  249. /* {{{ main
  250. */
  251. int main(int argc, char *argv[])
  252. {
  253. int exit_status = SUCCESS;
  254. int c;
  255. zend_file_handle file_handle;
  256. /* temporary locals */
  257. int behavior=PHP_MODE_STANDARD;
  258. int no_headers=1;
  259. int orig_optind=ap_php_optind;
  260. char *orig_optarg=ap_php_optarg;
  261. char *arg_free=NULL, **arg_excp=&arg_free;
  262. char *script_file=NULL;
  263. zend_llist global_vars;
  264. int interactive=0;
  265. char *exec_direct=NULL;
  266. char *param_error=NULL;
  267. /* end of temporary locals */
  268. #ifdef ZTS
  269. zend_compiler_globals *compiler_globals;
  270. zend_executor_globals *executor_globals;
  271. php_core_globals *core_globals;
  272. sapi_globals_struct *sapi_globals;
  273. void ***tsrm_ls;
  274. #endif
  275. #ifdef HAVE_SIGNAL_H
  276. #if defined(SIGPIPE) && defined(SIG_IGN)
  277. signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
  278. that sockets created via fsockopen()
  279. don't kill PHP if the remote site
  280. closes it. in apache|apxs mode apache
  281. does that for us! thies@thieso.net
  282. 20000419 */
  283. #endif
  284. #endif
  285. #ifdef ZTS
  286. tsrm_startup(1, 1, 0, NULL);
  287. #endif
  288. sapi_startup(&cli_sapi_module);
  289. #ifdef PHP_WIN32
  290. _fmode = _O_BINARY; /*sets default for file streams to binary */
  291. setmode(_fileno(stdin), O_BINARY); /* make the stdio mode be binary */
  292. setmode(_fileno(stdout), O_BINARY); /* make the stdio mode be binary */
  293. setmode(_fileno(stderr), O_BINARY); /* make the stdio mode be binary */
  294. #endif
  295. while ((c=ap_php_getopt(argc, argv, OPTSTRING))!=-1) {
  296. switch (c) {
  297. case 'c':
  298. cli_sapi_module.php_ini_path_override = strdup(ap_php_optarg);
  299. break;
  300. }
  301. }
  302. ap_php_optind = orig_optind;
  303. ap_php_optarg = orig_optarg;
  304. /* startup after we get the above ini override se we get things right */
  305. if (php_module_startup(&cli_sapi_module)==FAILURE) {
  306. return FAILURE;
  307. }
  308. #ifdef ZTS
  309. compiler_globals = ts_resource(compiler_globals_id);
  310. executor_globals = ts_resource(executor_globals_id);
  311. core_globals = ts_resource(core_globals_id);
  312. sapi_globals = ts_resource(sapi_globals_id);
  313. tsrm_ls = ts_resource(0);
  314. #endif
  315. zend_first_try {
  316. while ((c=ap_php_getopt(argc, argv, OPTSTRING))!=-1) {
  317. switch (c) {
  318. case '?':
  319. no_headers = 1;
  320. php_output_startup();
  321. php_output_activate(TSRMLS_C);
  322. SG(headers_sent) = 1;
  323. php_cli_usage(argv[0]);
  324. php_end_ob_buffers(1 TSRMLS_CC);
  325. exit(1);
  326. break;
  327. }
  328. }
  329. ap_php_optind = orig_optind;
  330. ap_php_optarg = orig_optarg;
  331. zend_llist_init(&global_vars, sizeof(char *), NULL, 0);
  332. /* Set some CLI defaults */
  333. SG(options) |= SAPI_OPTION_NO_CHDIR;
  334. zend_alter_ini_entry("register_argc_argv", 19, "1", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
  335. zend_alter_ini_entry("html_errors", 12, "0", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
  336. zend_alter_ini_entry("implicit_flush", 15, "1", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
  337. zend_alter_ini_entry("max_execution_time", 19, "0", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
  338. while ((c = ap_php_getopt(argc, argv, OPTSTRING)) != -1) {
  339. switch (c) {
  340. case 'a': /* interactive mode */
  341. printf("Interactive mode enabled\n\n");
  342. interactive=1;
  343. break;
  344. case 'C': /* don't chdir to the script directory */
  345. /* This is default so NOP */
  346. break;
  347. case 'd': /* define ini entries on command line */
  348. define_command_line_ini_entry(ap_php_optarg);
  349. break;
  350. case 'e': /* enable extended info output */
  351. CG(extended_info) = 1;
  352. break;
  353. case 'f': /* parse file */
  354. if (behavior == PHP_MODE_CLI_DIRECT) {
  355. param_error = "Either execute direct code or use a file.\n";
  356. break;
  357. }
  358. script_file = ap_php_optarg;
  359. no_headers = 1;
  360. break;
  361. case 'g': /* define global variables on command line */
  362. {
  363. char *arg = estrdup(ap_php_optarg);
  364. zend_llist_add_element(&global_vars, &arg);
  365. }
  366. break;
  367. case 'h': /* help & quit */
  368. case '?':
  369. no_headers = 1;
  370. php_output_startup();
  371. php_output_activate(TSRMLS_C);
  372. SG(headers_sent) = 1;
  373. php_cli_usage(argv[0]);
  374. php_end_ob_buffers(1 TSRMLS_CC);
  375. exit(1);
  376. break;
  377. case 'i': /* php info & quit */
  378. if (php_request_startup(TSRMLS_C)==FAILURE) {
  379. php_module_shutdown(TSRMLS_C);
  380. return FAILURE;
  381. }
  382. if (no_headers) {
  383. SG(headers_sent) = 1;
  384. SG(request_info).no_headers = 1;
  385. }
  386. php_print_info(0xFFFFFFFF TSRMLS_CC);
  387. php_end_ob_buffers(1 TSRMLS_CC);
  388. exit(1);
  389. break;
  390. case 'l': /* syntax check mode */
  391. if (behavior != PHP_MODE_STANDARD)
  392. break;
  393. no_headers = 1;
  394. behavior=PHP_MODE_LINT;
  395. break;
  396. case 'm': /* list compiled in modules */
  397. php_output_startup();
  398. php_output_activate(TSRMLS_C);
  399. SG(headers_sent) = 1;
  400. php_printf("Running PHP %s\n%s\n", PHP_VERSION , get_zend_version());
  401. php_printf("[PHP Modules]\n");
  402. zend_hash_apply_with_argument(&module_registry, (apply_func_arg_t) _print_module_info, NULL TSRMLS_CC);
  403. php_printf("\n[Zend Modules]\n");
  404. zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) _print_extension_info, NULL TSRMLS_CC);
  405. php_printf("\n");
  406. php_end_ob_buffers(1 TSRMLS_CC);
  407. exit(1);
  408. break;
  409. #if 0 /* not yet operational, see also below ... */
  410. case 'n': /* generate indented source mode*/
  411. if (behavior == PHP_MODE_CLI_DIRECT) {
  412. param_error = "Source indenting only works for files.\n";
  413. break;
  414. }
  415. behavior=PHP_MODE_INDENT;
  416. break;
  417. #endif
  418. case 'q': /* do not generate HTTP headers */
  419. /* This is default so NOP */
  420. break;
  421. case 'r': /* run code from command line */
  422. if (behavior != PHP_MODE_STANDARD) {
  423. param_error = "Either execute direct code or use a file.\n";
  424. break;
  425. }
  426. behavior=PHP_MODE_CLI_DIRECT;
  427. exec_direct=ap_php_optarg;
  428. break;
  429. case 's': /* generate highlighted HTML from source */
  430. if (behavior == PHP_MODE_CLI_DIRECT) {
  431. param_error = "Source highlighting only works for files.\n";
  432. break;
  433. }
  434. behavior=PHP_MODE_HIGHLIGHT;
  435. break;
  436. case 'v': /* show php version & quit */
  437. no_headers = 1;
  438. if (php_request_startup(TSRMLS_C)==FAILURE) {
  439. php_module_shutdown(TSRMLS_C);
  440. return FAILURE;
  441. }
  442. if (no_headers) {
  443. SG(headers_sent) = 1;
  444. SG(request_info).no_headers = 1;
  445. }
  446. php_printf("%s (%s)\n", PHP_VERSION, sapi_module.name);
  447. php_end_ob_buffers(1 TSRMLS_CC);
  448. exit(1);
  449. break;
  450. case 'w':
  451. if (behavior == PHP_MODE_CLI_DIRECT) {
  452. param_error = "Source stripping only works for files.\n";
  453. break;
  454. }
  455. behavior=PHP_MODE_STRIP;
  456. break;
  457. case 'z': /* load extension file */
  458. zend_load_extension(ap_php_optarg);
  459. break;
  460. default:
  461. break;
  462. }
  463. }
  464. if (param_error) {
  465. SG(headers_sent) = 1;
  466. SG(request_info).no_headers = 1;
  467. PUTS(param_error);
  468. exit(1);
  469. }
  470. CG(interactive) = interactive;
  471. /* only set script_file if not set already and not in direct mode and not at end of parameter list */
  472. if (argc > ap_php_optind && !script_file && behavior!=PHP_MODE_CLI_DIRECT && strcmp(argv[ap_php_optind-1],"--")) {
  473. no_headers = 1;
  474. script_file=argv[ap_php_optind];
  475. ap_php_optind++;
  476. }
  477. /* before registering argv to modulule exchange the *new* argv[0] */
  478. /* we can achieve this without allocating more memory */
  479. SG(request_info).argc=argc-ap_php_optind+1;
  480. arg_excp = argv+ap_php_optind-1;
  481. arg_free = argv[ap_php_optind-1];
  482. if (script_file) {
  483. argv[ap_php_optind-1] = script_file;
  484. } else {
  485. argv[ap_php_optind-1] = "-"; /* should be stdin */
  486. }
  487. SG(request_info).argv=argv+ap_php_optind-1;
  488. if (php_request_startup(TSRMLS_C)==FAILURE) {
  489. php_module_shutdown(TSRMLS_C);
  490. *arg_excp = arg_free;
  491. return FAILURE;
  492. }
  493. *arg_excp = arg_free; /* reconstuct argv */
  494. if (no_headers) {
  495. SG(headers_sent) = 1;
  496. SG(request_info).no_headers = 1;
  497. }
  498. if (script_file) {
  499. if (!(file_handle.handle.fp = VCWD_FOPEN(script_file, "rb"))) {
  500. SG(headers_sent) = 1;
  501. SG(request_info).no_headers = 1;
  502. PUTS("Could not open input file.\n");
  503. php_request_shutdown((void *) 0);
  504. php_module_shutdown(TSRMLS_C);
  505. return FAILURE;
  506. }
  507. php_register_variable("PHP_SELF", script_file, NULL TSRMLS_CC);
  508. file_handle.filename = script_file;
  509. /* #!php support */
  510. c = fgetc(file_handle.handle.fp);
  511. if (c == '#') {
  512. while (c != 10 && c != 13) {
  513. c = fgetc(file_handle.handle.fp); /* skip to end of line */
  514. }
  515. CG(zend_lineno)++;
  516. } else {
  517. rewind(file_handle.handle.fp);
  518. }
  519. } else {
  520. php_register_variable("PHP_SELF", "-", NULL TSRMLS_CC);
  521. file_handle.filename = "-";
  522. file_handle.handle.fp = stdin;
  523. }
  524. file_handle.type = ZEND_HANDLE_FP;
  525. file_handle.opened_path = NULL;
  526. file_handle.free_filename = 0;
  527. /* This actually destructs the elements of the list - ugly hack */
  528. zend_llist_apply(&global_vars, (llist_apply_func_t) php_register_command_line_global_vars TSRMLS_CC);
  529. zend_llist_destroy(&global_vars);
  530. switch (behavior) {
  531. case PHP_MODE_STANDARD:
  532. if (php_execute_script(&file_handle TSRMLS_CC)) {
  533. exit_status = EG(exit_status);
  534. } else {
  535. exit_status = 255;
  536. }
  537. break;
  538. case PHP_MODE_LINT:
  539. PG(during_request_startup) = 0;
  540. exit_status = php_lint_script(&file_handle TSRMLS_CC);
  541. if (exit_status==SUCCESS) {
  542. zend_printf("No syntax errors detected in %s\n", file_handle.filename);
  543. } else {
  544. zend_printf("Errors parsing %s\n", file_handle.filename);
  545. }
  546. break;
  547. case PHP_MODE_STRIP:
  548. if (open_file_for_scanning(&file_handle TSRMLS_CC)==SUCCESS) {
  549. zend_strip(TSRMLS_C);
  550. fclose(file_handle.handle.fp);
  551. }
  552. return SUCCESS;
  553. break;
  554. case PHP_MODE_HIGHLIGHT:
  555. {
  556. zend_syntax_highlighter_ini syntax_highlighter_ini;
  557. if (open_file_for_scanning(&file_handle TSRMLS_CC)==SUCCESS) {
  558. php_get_highlight_struct(&syntax_highlighter_ini);
  559. zend_highlight(&syntax_highlighter_ini TSRMLS_CC);
  560. fclose(file_handle.handle.fp);
  561. }
  562. return SUCCESS;
  563. }
  564. break;
  565. #if 0
  566. /* Zeev might want to do something with this one day */
  567. case PHP_MODE_INDENT:
  568. open_file_for_scanning(&file_handle TSRMLS_CC);
  569. zend_indent();
  570. fclose(file_handle.handle.fp);
  571. return SUCCESS;
  572. break;
  573. #endif
  574. case PHP_MODE_CLI_DIRECT:
  575. if (zend_eval_string(exec_direct, NULL, "Command line code" TSRMLS_CC) == FAILURE) {
  576. exit_status=254;
  577. }
  578. }
  579. php_request_shutdown((void *) 0);
  580. if (cli_sapi_module.php_ini_path_override) {
  581. free(cli_sapi_module.php_ini_path_override);
  582. }
  583. } zend_catch {
  584. exit_status = 255;
  585. } zend_end_try();
  586. php_module_shutdown(TSRMLS_C);
  587. #ifdef ZTS
  588. tsrm_shutdown();
  589. #endif
  590. return exit_status;
  591. }
  592. /* }}} */
  593. /*
  594. * Local variables:
  595. * tab-width: 4
  596. * c-basic-offset: 4
  597. * End:
  598. * vim600: sw=4 ts=4 fdm=marker
  599. * vim<600: sw=4 ts=4
  600. */