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.

663 lines
17 KiB

  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> 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. /* end of temporary locals */
  267. #ifdef ZTS
  268. zend_compiler_globals *compiler_globals;
  269. zend_executor_globals *executor_globals;
  270. php_core_globals *core_globals;
  271. sapi_globals_struct *sapi_globals;
  272. void ***tsrm_ls;
  273. #endif
  274. #ifdef HAVE_SIGNAL_H
  275. #if defined(SIGPIPE) && defined(SIG_IGN)
  276. signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
  277. that sockets created via fsockopen()
  278. don't kill PHP if the remote site
  279. closes it. in apache|apxs mode apache
  280. does that for us! thies@thieso.net
  281. 20000419 */
  282. #endif
  283. #endif
  284. #ifdef ZTS
  285. tsrm_startup(1, 1, 0, NULL);
  286. #endif
  287. sapi_startup(&cli_sapi_module);
  288. #ifdef PHP_WIN32
  289. _fmode = _O_BINARY; /*sets default for file streams to binary */
  290. setmode(_fileno(stdin), O_BINARY); /* make the stdio mode be binary */
  291. setmode(_fileno(stdout), O_BINARY); /* make the stdio mode be binary */
  292. setmode(_fileno(stderr), O_BINARY); /* make the stdio mode be binary */
  293. #endif
  294. while ((c=ap_php_getopt(argc, argv, OPTSTRING))!=-1) {
  295. switch (c) {
  296. case 'c':
  297. cli_sapi_module.php_ini_path_override = strdup(ap_php_optarg);
  298. break;
  299. }
  300. }
  301. ap_php_optind = orig_optind;
  302. ap_php_optarg = orig_optarg;
  303. /* startup after we get the above ini override se we get things right */
  304. if (php_module_startup(&cli_sapi_module)==FAILURE) {
  305. return FAILURE;
  306. }
  307. #ifdef ZTS
  308. compiler_globals = ts_resource(compiler_globals_id);
  309. executor_globals = ts_resource(executor_globals_id);
  310. core_globals = ts_resource(core_globals_id);
  311. sapi_globals = ts_resource(sapi_globals_id);
  312. tsrm_ls = ts_resource(0);
  313. #endif
  314. zend_first_try {
  315. while ((c=ap_php_getopt(argc, argv, OPTSTRING))!=-1) {
  316. switch (c) {
  317. case '?':
  318. no_headers = 1;
  319. php_output_startup();
  320. php_output_activate(TSRMLS_C);
  321. SG(headers_sent) = 1;
  322. php_cli_usage(argv[0]);
  323. php_end_ob_buffers(1 TSRMLS_CC);
  324. exit(1);
  325. break;
  326. }
  327. }
  328. ap_php_optind = orig_optind;
  329. ap_php_optarg = orig_optarg;
  330. zend_llist_init(&global_vars, sizeof(char *), NULL, 0);
  331. /* Set some CLI defaults */
  332. SG(options) |= SAPI_OPTION_NO_CHDIR;
  333. zend_alter_ini_entry("html_errors", 12, "0", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
  334. zend_alter_ini_entry("implicit_flush", 15, "1", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
  335. while ((c = ap_php_getopt(argc, argv, OPTSTRING)) != -1) {
  336. switch (c) {
  337. case 'a': /* interactive mode */
  338. printf("Interactive mode enabled\n\n");
  339. interactive=1;
  340. break;
  341. case 'C': /* don't chdir to the script directory */
  342. /* This is default so NOP */
  343. break;
  344. case 'd': /* define ini entries on command line */
  345. define_command_line_ini_entry(ap_php_optarg);
  346. break;
  347. case 'e': /* enable extended info output */
  348. CG(extended_info) = 1;
  349. break;
  350. case 'f': /* parse file */
  351. script_file = ap_php_optarg;
  352. no_headers = 1;
  353. break;
  354. case 'g': /* define global variables on command line */
  355. {
  356. char *arg = estrdup(ap_php_optarg);
  357. zend_llist_add_element(&global_vars, &arg);
  358. }
  359. break;
  360. case 'h': /* help & quit */
  361. case '?':
  362. no_headers = 1;
  363. php_output_startup();
  364. php_output_activate(TSRMLS_C);
  365. SG(headers_sent) = 1;
  366. php_cli_usage(argv[0]);
  367. php_end_ob_buffers(1 TSRMLS_CC);
  368. exit(1);
  369. break;
  370. case 'i': /* php info & quit */
  371. if (php_request_startup(TSRMLS_C)==FAILURE) {
  372. php_module_shutdown(TSRMLS_C);
  373. return FAILURE;
  374. }
  375. if (no_headers) {
  376. SG(headers_sent) = 1;
  377. SG(request_info).no_headers = 1;
  378. }
  379. php_print_info(0xFFFFFFFF TSRMLS_CC);
  380. php_end_ob_buffers(1 TSRMLS_CC);
  381. exit(1);
  382. break;
  383. case 'l': /* syntax check mode */
  384. no_headers = 1;
  385. behavior=PHP_MODE_LINT;
  386. break;
  387. case 'm': /* list compiled in modules */
  388. php_output_startup();
  389. php_output_activate(TSRMLS_C);
  390. SG(headers_sent) = 1;
  391. php_printf("Running PHP %s\n%s\n", PHP_VERSION , get_zend_version());
  392. php_printf("[PHP Modules]\n");
  393. zend_hash_apply_with_argument(&module_registry, (apply_func_arg_t) _print_module_info, NULL TSRMLS_CC);
  394. php_printf("\n[Zend Modules]\n");
  395. zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) _print_extension_info, NULL TSRMLS_CC);
  396. php_printf("\n");
  397. php_end_ob_buffers(1 TSRMLS_CC);
  398. exit(1);
  399. break;
  400. #if 0 /* not yet operational, see also below ... */
  401. case 'n': /* generate indented source mode*/
  402. behavior=PHP_MODE_INDENT;
  403. break;
  404. #endif
  405. case 'q': /* do not generate HTTP headers */
  406. /* This is default so NOP */
  407. break;
  408. case 's': /* generate highlighted HTML from source */
  409. behavior=PHP_MODE_HIGHLIGHT;
  410. break;
  411. case 'r': /* run code from command line */
  412. behavior=PHP_MODE_CLI_DIRECT;
  413. exec_direct=ap_php_optarg;
  414. break;
  415. case 'v': /* show php version & quit */
  416. no_headers = 1;
  417. if (php_request_startup(TSRMLS_C)==FAILURE) {
  418. php_module_shutdown(TSRMLS_C);
  419. return FAILURE;
  420. }
  421. if (no_headers) {
  422. SG(headers_sent) = 1;
  423. SG(request_info).no_headers = 1;
  424. }
  425. php_printf("%s\n", PHP_VERSION);
  426. php_end_ob_buffers(1 TSRMLS_CC);
  427. exit(1);
  428. break;
  429. case 'w':
  430. behavior=PHP_MODE_STRIP;
  431. break;
  432. case 'z': /* load extension file */
  433. zend_load_extension(ap_php_optarg);
  434. break;
  435. default:
  436. break;
  437. }
  438. }
  439. CG(interactive) = interactive;
  440. /* only set script_file if not set already and not in direct mode and not at end of parameter list */
  441. if (argc > ap_php_optind && !script_file && !exec_direct && strcmp(argv[ap_php_optind-1],"--")) {
  442. script_file=argv[ap_php_optind];
  443. ap_php_optind++;
  444. }
  445. /* before registering argv to modulule exchange the *new* argv[0] */
  446. /* we can achieve this without allocating more memory */
  447. SG(request_info).argc=argc-ap_php_optind+1;
  448. arg_excp = argv+ap_php_optind-1;
  449. arg_free = argv[ap_php_optind-1];
  450. if (script_file) {
  451. argv[ap_php_optind-1] = script_file;
  452. } else {
  453. argv[ap_php_optind-1] = "-"; /* should be stdin */
  454. }
  455. SG(request_info).argv=argv+ap_php_optind-1;
  456. if (php_request_startup(TSRMLS_C)==FAILURE) {
  457. php_module_shutdown(TSRMLS_C);
  458. *arg_excp = arg_free;
  459. return FAILURE;
  460. }
  461. *arg_excp = arg_free; /* reconstuct argv */
  462. if (no_headers) {
  463. SG(headers_sent) = 1;
  464. SG(request_info).no_headers = 1;
  465. }
  466. if (script_file) {
  467. if (!(file_handle.handle.fp = VCWD_FOPEN(script_file, "rb"))) {
  468. PUTS("No input file specified.\n");
  469. php_request_shutdown((void *) 0);
  470. php_module_shutdown(TSRMLS_C);
  471. return FAILURE;
  472. }
  473. php_register_variable("PHP_SELF", script_file, NULL TSRMLS_CC);
  474. file_handle.filename = script_file;
  475. /* #!php support */
  476. c = fgetc(file_handle.handle.fp);
  477. if (c == '#') {
  478. while (c != 10 && c != 13) {
  479. c = fgetc(file_handle.handle.fp); /* skip to end of line */
  480. }
  481. CG(zend_lineno)++;
  482. } else {
  483. rewind(file_handle.handle.fp);
  484. }
  485. } else {
  486. php_register_variable("PHP_SELF", "-", NULL TSRMLS_CC);
  487. file_handle.filename = "-";
  488. file_handle.handle.fp = stdin;
  489. }
  490. file_handle.type = ZEND_HANDLE_FP;
  491. file_handle.opened_path = NULL;
  492. file_handle.free_filename = 0;
  493. /* This actually destructs the elements of the list - ugly hack */
  494. zend_llist_apply(&global_vars, (llist_apply_func_t) php_register_command_line_global_vars TSRMLS_CC);
  495. zend_llist_destroy(&global_vars);
  496. switch (behavior) {
  497. case PHP_MODE_STANDARD:
  498. if (php_execute_script(&file_handle TSRMLS_CC)) {
  499. exit_status = EG(exit_status);
  500. } else {
  501. exit_status = 255;
  502. }
  503. break;
  504. case PHP_MODE_LINT:
  505. PG(during_request_startup) = 0;
  506. exit_status = php_lint_script(&file_handle TSRMLS_CC);
  507. if (exit_status==SUCCESS) {
  508. zend_printf("No syntax errors detected in %s\n", file_handle.filename);
  509. } else {
  510. zend_printf("Errors parsing %s\n", file_handle.filename);
  511. }
  512. break;
  513. case PHP_MODE_STRIP:
  514. if (open_file_for_scanning(&file_handle TSRMLS_CC)==SUCCESS) {
  515. zend_strip(TSRMLS_C);
  516. fclose(file_handle.handle.fp);
  517. }
  518. return SUCCESS;
  519. break;
  520. case PHP_MODE_HIGHLIGHT:
  521. {
  522. zend_syntax_highlighter_ini syntax_highlighter_ini;
  523. if (open_file_for_scanning(&file_handle TSRMLS_CC)==SUCCESS) {
  524. php_get_highlight_struct(&syntax_highlighter_ini);
  525. zend_highlight(&syntax_highlighter_ini TSRMLS_CC);
  526. fclose(file_handle.handle.fp);
  527. }
  528. return SUCCESS;
  529. }
  530. break;
  531. #if 0
  532. /* Zeev might want to do something with this one day */
  533. case PHP_MODE_INDENT:
  534. open_file_for_scanning(&file_handle TSRMLS_CC);
  535. zend_indent();
  536. fclose(file_handle.handle.fp);
  537. return SUCCESS;
  538. break;
  539. #endif
  540. case PHP_MODE_CLI_DIRECT:
  541. if (zend_eval_string(exec_direct, NULL, "Command line code" TSRMLS_CC) == FAILURE) {
  542. exit_status=254;
  543. }
  544. }
  545. php_request_shutdown((void *) 0);
  546. if (cli_sapi_module.php_ini_path_override) {
  547. free(cli_sapi_module.php_ini_path_override);
  548. }
  549. } zend_catch {
  550. exit_status = 255;
  551. } zend_end_try();
  552. php_module_shutdown(TSRMLS_C);
  553. #ifdef ZTS
  554. tsrm_shutdown();
  555. #endif
  556. return exit_status;
  557. }
  558. /* }}} */
  559. /*
  560. * Local variables:
  561. * tab-width: 4
  562. * c-basic-offset: 4
  563. * End:
  564. * vim600: sw=4 ts=4 fdm=marker
  565. * vim<600: sw=4 ts=4
  566. */