php_cli.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Edin Kadribasic <edink@php.net> |
  14. | Marcus Boerger <helly@php.net> |
  15. | Johannes Schlueter <johannes@php.net> |
  16. | Parts based on CGI SAPI Module by |
  17. | Rasmus Lerdorf, Stig Bakken and Zeev Suraski |
  18. +----------------------------------------------------------------------+
  19. */
  20. #include "php.h"
  21. #include "php_globals.h"
  22. #include "php_variables.h"
  23. #include "zend_hash.h"
  24. #include "zend_modules.h"
  25. #include "zend_interfaces.h"
  26. #include "ext/reflection/php_reflection.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 "win32/console.h"
  34. #include <process.h>
  35. #include <shellapi.h>
  36. #endif
  37. #if HAVE_SYS_TIME_H
  38. #include <sys/time.h>
  39. #endif
  40. #if HAVE_UNISTD_H
  41. #include <unistd.h>
  42. #endif
  43. #include <signal.h>
  44. #include <locale.h>
  45. #include "zend.h"
  46. #include "zend_extensions.h"
  47. #include "php_ini.h"
  48. #include "php_globals.h"
  49. #include "php_main.h"
  50. #include "fopen_wrappers.h"
  51. #include "ext/standard/php_standard.h"
  52. #include "ext/standard/dl_arginfo.h"
  53. #include "cli.h"
  54. #ifdef PHP_WIN32
  55. #include <io.h>
  56. #include <fcntl.h>
  57. #include "win32/php_registry.h"
  58. #endif
  59. #ifdef __riscos__
  60. #include <unixlib/local.h>
  61. #endif
  62. #include "zend_compile.h"
  63. #include "zend_execute.h"
  64. #include "zend_highlight.h"
  65. #include "zend_exceptions.h"
  66. #include "php_getopt.h"
  67. #ifndef PHP_CLI_WIN32_NO_CONSOLE
  68. #include "php_cli_server.h"
  69. #endif
  70. #include "ps_title.h"
  71. #include "php_cli_process_title.h"
  72. #include "php_cli_process_title_arginfo.h"
  73. #ifndef PHP_WIN32
  74. # define php_select(m, r, w, e, t) select(m, r, w, e, t)
  75. #else
  76. # include "win32/select.h"
  77. #endif
  78. #if defined(PHP_WIN32) && defined(HAVE_OPENSSL)
  79. # include "openssl/applink.c"
  80. #endif
  81. PHPAPI extern char *php_ini_opened_path;
  82. PHPAPI extern char *php_ini_scanned_path;
  83. PHPAPI extern char *php_ini_scanned_files;
  84. #if defined(PHP_WIN32)
  85. #if defined(ZTS)
  86. ZEND_TSRMLS_CACHE_DEFINE()
  87. #endif
  88. static DWORD orig_cp = 0;
  89. #endif
  90. #ifndef O_BINARY
  91. #define O_BINARY 0
  92. #endif
  93. #define PHP_MODE_STANDARD 1
  94. #define PHP_MODE_HIGHLIGHT 2
  95. #define PHP_MODE_LINT 4
  96. #define PHP_MODE_STRIP 5
  97. #define PHP_MODE_CLI_DIRECT 6
  98. #define PHP_MODE_PROCESS_STDIN 7
  99. #define PHP_MODE_REFLECTION_FUNCTION 8
  100. #define PHP_MODE_REFLECTION_CLASS 9
  101. #define PHP_MODE_REFLECTION_EXTENSION 10
  102. #define PHP_MODE_REFLECTION_EXT_INFO 11
  103. #define PHP_MODE_REFLECTION_ZEND_EXTENSION 12
  104. #define PHP_MODE_SHOW_INI_CONFIG 13
  105. cli_shell_callbacks_t cli_shell_callbacks = { NULL, NULL, NULL };
  106. PHP_CLI_API cli_shell_callbacks_t *php_cli_get_shell_callbacks(void)
  107. {
  108. return &cli_shell_callbacks;
  109. }
  110. const char HARDCODED_INI[] =
  111. "html_errors=0\n"
  112. "register_argc_argv=1\n"
  113. "implicit_flush=1\n"
  114. "output_buffering=0\n"
  115. "max_execution_time=0\n"
  116. "max_input_time=-1\n\0";
  117. const opt_struct OPTIONS[] = {
  118. {'a', 0, "interactive"},
  119. {'B', 1, "process-begin"},
  120. {'C', 0, "no-chdir"}, /* for compatibility with CGI (do not chdir to script directory) */
  121. {'c', 1, "php-ini"},
  122. {'d', 1, "define"},
  123. {'E', 1, "process-end"},
  124. {'e', 0, "profile-info"},
  125. {'F', 1, "process-file"},
  126. {'f', 1, "file"},
  127. {'h', 0, "help"},
  128. {'i', 0, "info"},
  129. {'l', 0, "syntax-check"},
  130. {'m', 0, "modules"},
  131. {'n', 0, "no-php-ini"},
  132. {'q', 0, "no-header"}, /* for compatibility with CGI (do not generate HTTP headers) */
  133. {'R', 1, "process-code"},
  134. {'H', 0, "hide-args"},
  135. {'r', 1, "run"},
  136. {'s', 0, "syntax-highlight"},
  137. {'s', 0, "syntax-highlighting"},
  138. {'S', 1, "server"},
  139. {'t', 1, "docroot"},
  140. {'w', 0, "strip"},
  141. {'?', 0, "usage"},/* help alias (both '?' and 'usage') */
  142. {'v', 0, "version"},
  143. {'z', 1, "zend-extension"},
  144. {10, 1, "rf"},
  145. {10, 1, "rfunction"},
  146. {11, 1, "rc"},
  147. {11, 1, "rclass"},
  148. {12, 1, "re"},
  149. {12, 1, "rextension"},
  150. {13, 1, "rz"},
  151. {13, 1, "rzendextension"},
  152. {14, 1, "ri"},
  153. {14, 1, "rextinfo"},
  154. {15, 0, "ini"},
  155. /* Internal testing option -- may be changed or removed without notice,
  156. * including in patch releases. */
  157. {16, 1, "repeat"},
  158. {'-', 0, NULL} /* end of args */
  159. };
  160. static int module_name_cmp(Bucket *f, Bucket *s) /* {{{ */
  161. {
  162. return strcasecmp(((zend_module_entry *)Z_PTR(f->val))->name,
  163. ((zend_module_entry *)Z_PTR(s->val))->name);
  164. }
  165. /* }}} */
  166. static void print_modules(void) /* {{{ */
  167. {
  168. HashTable sorted_registry;
  169. zend_module_entry *module;
  170. zend_hash_init(&sorted_registry, 50, NULL, NULL, 0);
  171. zend_hash_copy(&sorted_registry, &module_registry, NULL);
  172. zend_hash_sort(&sorted_registry, module_name_cmp, 0);
  173. ZEND_HASH_FOREACH_PTR(&sorted_registry, module) {
  174. php_printf("%s\n", module->name);
  175. } ZEND_HASH_FOREACH_END();
  176. zend_hash_destroy(&sorted_registry);
  177. }
  178. /* }}} */
  179. static void print_extension_info(zend_extension *ext) /* {{{ */
  180. {
  181. php_printf("%s\n", ext->name);
  182. }
  183. /* }}} */
  184. static int extension_name_cmp(const zend_llist_element **f, const zend_llist_element **s) /* {{{ */
  185. {
  186. zend_extension *fe = (zend_extension*)(*f)->data;
  187. zend_extension *se = (zend_extension*)(*s)->data;
  188. return strcmp(fe->name, se->name);
  189. }
  190. /* }}} */
  191. static void print_extensions(void) /* {{{ */
  192. {
  193. zend_llist sorted_exts;
  194. zend_llist_copy(&sorted_exts, &zend_extensions);
  195. sorted_exts.dtor = NULL;
  196. zend_llist_sort(&sorted_exts, extension_name_cmp);
  197. zend_llist_apply(&sorted_exts, (llist_apply_func_t) print_extension_info);
  198. zend_llist_destroy(&sorted_exts);
  199. }
  200. /* }}} */
  201. #ifndef STDOUT_FILENO
  202. #define STDOUT_FILENO 1
  203. #endif
  204. #ifndef STDERR_FILENO
  205. #define STDERR_FILENO 2
  206. #endif
  207. static inline int sapi_cli_select(php_socket_t fd)
  208. {
  209. fd_set wfd;
  210. struct timeval tv;
  211. int ret;
  212. FD_ZERO(&wfd);
  213. PHP_SAFE_FD_SET(fd, &wfd);
  214. tv.tv_sec = (long)FG(default_socket_timeout);
  215. tv.tv_usec = 0;
  216. ret = php_select(fd+1, NULL, &wfd, NULL, &tv);
  217. return ret != -1;
  218. }
  219. PHP_CLI_API ssize_t sapi_cli_single_write(const char *str, size_t str_length) /* {{{ */
  220. {
  221. ssize_t ret;
  222. if (cli_shell_callbacks.cli_shell_write) {
  223. cli_shell_callbacks.cli_shell_write(str, str_length);
  224. }
  225. #ifdef PHP_WRITE_STDOUT
  226. do {
  227. ret = write(STDOUT_FILENO, str, str_length);
  228. } while (ret <= 0 && errno == EAGAIN && sapi_cli_select(STDOUT_FILENO));
  229. #else
  230. ret = fwrite(str, 1, MIN(str_length, 16384), stdout);
  231. if (ret == 0 && ferror(stdout)) {
  232. return -1;
  233. }
  234. #endif
  235. return ret;
  236. }
  237. /* }}} */
  238. static size_t sapi_cli_ub_write(const char *str, size_t str_length) /* {{{ */
  239. {
  240. const char *ptr = str;
  241. size_t remaining = str_length;
  242. ssize_t ret;
  243. if (!str_length) {
  244. return 0;
  245. }
  246. if (cli_shell_callbacks.cli_shell_ub_write) {
  247. size_t ub_wrote;
  248. ub_wrote = cli_shell_callbacks.cli_shell_ub_write(str, str_length);
  249. if (ub_wrote != (size_t) -1) {
  250. return ub_wrote;
  251. }
  252. }
  253. while (remaining > 0)
  254. {
  255. ret = sapi_cli_single_write(ptr, remaining);
  256. if (ret < 0) {
  257. #ifndef PHP_CLI_WIN32_NO_CONSOLE
  258. EG(exit_status) = 255;
  259. php_handle_aborted_connection();
  260. #endif
  261. break;
  262. }
  263. ptr += ret;
  264. remaining -= ret;
  265. }
  266. return (ptr - str);
  267. }
  268. /* }}} */
  269. static void sapi_cli_flush(void *server_context) /* {{{ */
  270. {
  271. /* Ignore EBADF here, it's caused by the fact that STDIN/STDOUT/STDERR streams
  272. * are/could be closed before fflush() is called.
  273. */
  274. if (fflush(stdout)==EOF && errno!=EBADF) {
  275. #ifndef PHP_CLI_WIN32_NO_CONSOLE
  276. php_handle_aborted_connection();
  277. #endif
  278. }
  279. }
  280. /* }}} */
  281. static char *php_self = "";
  282. static char *script_filename = "";
  283. static void sapi_cli_register_variables(zval *track_vars_array) /* {{{ */
  284. {
  285. size_t len;
  286. char *docroot = "";
  287. /* In CGI mode, we consider the environment to be a part of the server
  288. * variables
  289. */
  290. php_import_environment_variables(track_vars_array);
  291. /* Build the special-case PHP_SELF variable for the CLI version */
  292. len = strlen(php_self);
  293. if (sapi_module.input_filter(PARSE_SERVER, "PHP_SELF", &php_self, len, &len)) {
  294. php_register_variable("PHP_SELF", php_self, track_vars_array);
  295. }
  296. if (sapi_module.input_filter(PARSE_SERVER, "SCRIPT_NAME", &php_self, len, &len)) {
  297. php_register_variable("SCRIPT_NAME", php_self, track_vars_array);
  298. }
  299. /* filenames are empty for stdin */
  300. len = strlen(script_filename);
  301. if (sapi_module.input_filter(PARSE_SERVER, "SCRIPT_FILENAME", &script_filename, len, &len)) {
  302. php_register_variable("SCRIPT_FILENAME", script_filename, track_vars_array);
  303. }
  304. if (sapi_module.input_filter(PARSE_SERVER, "PATH_TRANSLATED", &script_filename, len, &len)) {
  305. php_register_variable("PATH_TRANSLATED", script_filename, track_vars_array);
  306. }
  307. /* just make it available */
  308. len = 0U;
  309. if (sapi_module.input_filter(PARSE_SERVER, "DOCUMENT_ROOT", &docroot, len, &len)) {
  310. php_register_variable("DOCUMENT_ROOT", docroot, track_vars_array);
  311. }
  312. }
  313. /* }}} */
  314. static void sapi_cli_log_message(const char *message, int syslog_type_int) /* {{{ */
  315. {
  316. fprintf(stderr, "%s\n", message);
  317. #ifdef PHP_WIN32
  318. fflush(stderr);
  319. #endif
  320. }
  321. /* }}} */
  322. static int sapi_cli_deactivate(void) /* {{{ */
  323. {
  324. fflush(stdout);
  325. if(SG(request_info).argv0) {
  326. free(SG(request_info).argv0);
  327. SG(request_info).argv0 = NULL;
  328. }
  329. return SUCCESS;
  330. }
  331. /* }}} */
  332. static char* sapi_cli_read_cookies(void) /* {{{ */
  333. {
  334. return NULL;
  335. }
  336. /* }}} */
  337. static int sapi_cli_header_handler(sapi_header_struct *h, sapi_header_op_enum op, sapi_headers_struct *s) /* {{{ */
  338. {
  339. return 0;
  340. }
  341. /* }}} */
  342. static int sapi_cli_send_headers(sapi_headers_struct *sapi_headers) /* {{{ */
  343. {
  344. /* We do nothing here, this function is needed to prevent that the fallback
  345. * header handling is called. */
  346. return SAPI_HEADER_SENT_SUCCESSFULLY;
  347. }
  348. /* }}} */
  349. static void sapi_cli_send_header(sapi_header_struct *sapi_header, void *server_context) /* {{{ */
  350. {
  351. }
  352. /* }}} */
  353. static int php_cli_startup(sapi_module_struct *sapi_module) /* {{{ */
  354. {
  355. if (php_module_startup(sapi_module, NULL, 0)==FAILURE) {
  356. return FAILURE;
  357. }
  358. return SUCCESS;
  359. }
  360. /* }}} */
  361. /* {{{ sapi_cli_ini_defaults */
  362. /* overwritable ini defaults must be set in sapi_cli_ini_defaults() */
  363. #define INI_DEFAULT(name,value)\
  364. ZVAL_NEW_STR(&tmp, zend_string_init(value, sizeof(value)-1, 1));\
  365. zend_hash_str_update(configuration_hash, name, sizeof(name)-1, &tmp);\
  366. static void sapi_cli_ini_defaults(HashTable *configuration_hash)
  367. {
  368. zval tmp;
  369. INI_DEFAULT("display_errors", "1");
  370. }
  371. /* }}} */
  372. /* {{{ sapi_module_struct cli_sapi_module */
  373. static sapi_module_struct cli_sapi_module = {
  374. "cli", /* name */
  375. "Command Line Interface", /* pretty name */
  376. php_cli_startup, /* startup */
  377. php_module_shutdown_wrapper, /* shutdown */
  378. NULL, /* activate */
  379. sapi_cli_deactivate, /* deactivate */
  380. sapi_cli_ub_write, /* unbuffered write */
  381. sapi_cli_flush, /* flush */
  382. NULL, /* get uid */
  383. NULL, /* getenv */
  384. php_error, /* error handler */
  385. sapi_cli_header_handler, /* header handler */
  386. sapi_cli_send_headers, /* send headers handler */
  387. sapi_cli_send_header, /* send header handler */
  388. NULL, /* read POST data */
  389. sapi_cli_read_cookies, /* read Cookies */
  390. sapi_cli_register_variables, /* register server variables */
  391. sapi_cli_log_message, /* Log message */
  392. NULL, /* Get request time */
  393. NULL, /* Child terminate */
  394. STANDARD_SAPI_MODULE_PROPERTIES
  395. };
  396. /* }}} */
  397. static const zend_function_entry additional_functions[] = {
  398. ZEND_FE(dl, arginfo_dl)
  399. PHP_FE(cli_set_process_title, arginfo_cli_set_process_title)
  400. PHP_FE(cli_get_process_title, arginfo_cli_get_process_title)
  401. PHP_FE_END
  402. };
  403. /* {{{ php_cli_usage */
  404. static void php_cli_usage(char *argv0)
  405. {
  406. char *prog;
  407. prog = strrchr(argv0, '/');
  408. if (prog) {
  409. prog++;
  410. } else {
  411. prog = "php";
  412. }
  413. printf( "Usage: %s [options] [-f] <file> [--] [args...]\n"
  414. " %s [options] -r <code> [--] [args...]\n"
  415. " %s [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]\n"
  416. " %s [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]\n"
  417. " %s [options] -S <addr>:<port> [-t docroot] [router]\n"
  418. " %s [options] -- [args...]\n"
  419. " %s [options] -a\n"
  420. "\n"
  421. " -a Run as interactive shell (requires readline extension)\n"
  422. " -c <path>|<file> Look for php.ini file in this directory\n"
  423. " -n No configuration (ini) files will be used\n"
  424. " -d foo[=bar] Define INI entry foo with value 'bar'\n"
  425. " -e Generate extended information for debugger/profiler\n"
  426. " -f <file> Parse and execute <file>.\n"
  427. " -h This help\n"
  428. " -i PHP information\n"
  429. " -l Syntax check only (lint)\n"
  430. " -m Show compiled in modules\n"
  431. " -r <code> Run PHP <code> without using script tags <?..?>\n"
  432. " -B <begin_code> Run PHP <begin_code> before processing input lines\n"
  433. " -R <code> Run PHP <code> for every input line\n"
  434. " -F <file> Parse and execute <file> for every input line\n"
  435. " -E <end_code> Run PHP <end_code> after processing all input lines\n"
  436. " -H Hide any passed arguments from external tools.\n"
  437. " -S <addr>:<port> Run with built-in web server.\n"
  438. " -t <docroot> Specify document root <docroot> for built-in web server.\n"
  439. " -s Output HTML syntax highlighted source.\n"
  440. " -v Version number\n"
  441. " -w Output source with stripped comments and whitespace.\n"
  442. " -z <file> Load Zend extension <file>.\n"
  443. "\n"
  444. " args... Arguments passed to script. Use -- args when first argument\n"
  445. " starts with - or script is read from stdin\n"
  446. "\n"
  447. " --ini Show configuration file names\n"
  448. "\n"
  449. " --rf <name> Show information about function <name>.\n"
  450. " --rc <name> Show information about class <name>.\n"
  451. " --re <name> Show information about extension <name>.\n"
  452. " --rz <name> Show information about Zend extension <name>.\n"
  453. " --ri <name> Show configuration for extension <name>.\n"
  454. "\n"
  455. , prog, prog, prog, prog, prog, prog, prog);
  456. }
  457. /* }}} */
  458. static php_stream *s_in_process = NULL;
  459. static void cli_register_file_handles(bool no_close) /* {{{ */
  460. {
  461. php_stream *s_in, *s_out, *s_err;
  462. php_stream_context *sc_in=NULL, *sc_out=NULL, *sc_err=NULL;
  463. zend_constant ic, oc, ec;
  464. s_in = php_stream_open_wrapper_ex("php://stdin", "rb", 0, NULL, sc_in);
  465. s_out = php_stream_open_wrapper_ex("php://stdout", "wb", 0, NULL, sc_out);
  466. s_err = php_stream_open_wrapper_ex("php://stderr", "wb", 0, NULL, sc_err);
  467. if (s_in==NULL || s_out==NULL || s_err==NULL) {
  468. if (s_in) php_stream_close(s_in);
  469. if (s_out) php_stream_close(s_out);
  470. if (s_err) php_stream_close(s_err);
  471. return;
  472. }
  473. if (no_close) {
  474. s_in->flags |= PHP_STREAM_FLAG_NO_CLOSE;
  475. s_out->flags |= PHP_STREAM_FLAG_NO_CLOSE;
  476. s_err->flags |= PHP_STREAM_FLAG_NO_CLOSE;
  477. }
  478. s_in_process = s_in;
  479. php_stream_to_zval(s_in, &ic.value);
  480. php_stream_to_zval(s_out, &oc.value);
  481. php_stream_to_zval(s_err, &ec.value);
  482. ZEND_CONSTANT_SET_FLAGS(&ic, CONST_CS, 0);
  483. ic.name = zend_string_init_interned("STDIN", sizeof("STDIN")-1, 0);
  484. zend_register_constant(&ic);
  485. ZEND_CONSTANT_SET_FLAGS(&oc, CONST_CS, 0);
  486. oc.name = zend_string_init_interned("STDOUT", sizeof("STDOUT")-1, 0);
  487. zend_register_constant(&oc);
  488. ZEND_CONSTANT_SET_FLAGS(&ec, CONST_CS, 0);
  489. ec.name = zend_string_init_interned("STDERR", sizeof("STDERR")-1, 0);
  490. zend_register_constant(&ec);
  491. }
  492. /* }}} */
  493. static const char *param_mode_conflict = "Either execute direct code, process stdin or use a file.\n";
  494. /* {{{ cli_seek_file_begin */
  495. static int cli_seek_file_begin(zend_file_handle *file_handle, char *script_file)
  496. {
  497. FILE *fp = VCWD_FOPEN(script_file, "rb");
  498. if (!fp) {
  499. php_printf("Could not open input file: %s\n", script_file);
  500. return FAILURE;
  501. }
  502. zend_stream_init_fp(file_handle, fp, script_file);
  503. file_handle->primary_script = 1;
  504. return SUCCESS;
  505. }
  506. /* }}} */
  507. /*{{{ php_cli_win32_ctrl_handler */
  508. #if defined(PHP_WIN32)
  509. BOOL WINAPI php_cli_win32_ctrl_handler(DWORD sig)
  510. {
  511. (void)php_win32_cp_cli_do_restore(orig_cp);
  512. return FALSE;
  513. }
  514. #endif
  515. /*}}}*/
  516. static int do_cli(int argc, char **argv) /* {{{ */
  517. {
  518. int c;
  519. zend_file_handle file_handle;
  520. int behavior = PHP_MODE_STANDARD;
  521. char *reflection_what = NULL;
  522. volatile int request_started = 0;
  523. char *php_optarg = NULL, *orig_optarg = NULL;
  524. int php_optind = 1, orig_optind = 1;
  525. char *exec_direct=NULL, *exec_run=NULL, *exec_begin=NULL, *exec_end=NULL;
  526. char *arg_free=NULL, **arg_excp=&arg_free;
  527. char *script_file=NULL, *translated_path = NULL;
  528. int interactive=0;
  529. const char *param_error=NULL;
  530. int hide_argv = 0;
  531. int num_repeats = 1;
  532. pid_t pid = getpid();
  533. file_handle.filename = NULL;
  534. zend_try {
  535. CG(in_compilation) = 0; /* not initialized but needed for several options */
  536. while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
  537. switch (c) {
  538. case 'i': /* php info & quit */
  539. if (php_request_startup()==FAILURE) {
  540. goto err;
  541. }
  542. request_started = 1;
  543. php_print_info(PHP_INFO_ALL & ~PHP_INFO_CREDITS);
  544. php_output_end_all();
  545. EG(exit_status) = (c == '?' && argc > 1 && !strchr(argv[1], c));
  546. goto out;
  547. case 'v': /* show php version & quit */
  548. php_printf("PHP %s (%s) (built: %s %s) (%s)\nCopyright (c) The PHP Group\n%s",
  549. PHP_VERSION, cli_sapi_module.name, __DATE__, __TIME__,
  550. #ifdef ZTS
  551. "ZTS"
  552. #else
  553. "NTS"
  554. #endif
  555. #ifdef PHP_BUILD_COMPILER
  556. " " PHP_BUILD_COMPILER
  557. #endif
  558. #ifdef PHP_BUILD_ARCH
  559. " " PHP_BUILD_ARCH
  560. #endif
  561. #if ZEND_DEBUG
  562. " DEBUG"
  563. #endif
  564. #ifdef HAVE_GCOV
  565. " GCOV"
  566. #endif
  567. ,
  568. get_zend_version()
  569. );
  570. sapi_deactivate();
  571. goto out;
  572. case 'm': /* list compiled in modules */
  573. if (php_request_startup()==FAILURE) {
  574. goto err;
  575. }
  576. request_started = 1;
  577. php_printf("[PHP Modules]\n");
  578. print_modules();
  579. php_printf("\n[Zend Modules]\n");
  580. print_extensions();
  581. php_printf("\n");
  582. php_output_end_all();
  583. EG(exit_status) = 0;
  584. goto out;
  585. default:
  586. break;
  587. }
  588. }
  589. /* Set some CLI defaults */
  590. SG(options) |= SAPI_OPTION_NO_CHDIR;
  591. php_optind = orig_optind;
  592. php_optarg = orig_optarg;
  593. while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
  594. switch (c) {
  595. case 'a': /* interactive mode */
  596. if (!cli_shell_callbacks.cli_shell_run) {
  597. param_error = "Interactive shell (-a) requires the readline extension.\n";
  598. break;
  599. }
  600. if (!interactive) {
  601. if (behavior != PHP_MODE_STANDARD) {
  602. param_error = param_mode_conflict;
  603. break;
  604. }
  605. interactive=1;
  606. }
  607. break;
  608. case 'C': /* don't chdir to the script directory */
  609. /* This is default so NOP */
  610. break;
  611. case 'F':
  612. if (behavior == PHP_MODE_PROCESS_STDIN) {
  613. if (exec_run || script_file) {
  614. param_error = "You can use -R or -F only once.\n";
  615. break;
  616. }
  617. } else if (behavior != PHP_MODE_STANDARD) {
  618. param_error = param_mode_conflict;
  619. break;
  620. }
  621. behavior=PHP_MODE_PROCESS_STDIN;
  622. script_file = php_optarg;
  623. break;
  624. case 'f': /* parse file */
  625. if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
  626. param_error = param_mode_conflict;
  627. break;
  628. } else if (script_file) {
  629. param_error = "You can use -f only once.\n";
  630. break;
  631. }
  632. script_file = php_optarg;
  633. break;
  634. case 'l': /* syntax check mode */
  635. if (behavior != PHP_MODE_STANDARD) {
  636. break;
  637. }
  638. behavior=PHP_MODE_LINT;
  639. break;
  640. case 'q': /* do not generate HTTP headers */
  641. /* This is default so NOP */
  642. break;
  643. case 'r': /* run code from command line */
  644. if (behavior == PHP_MODE_CLI_DIRECT) {
  645. if (exec_direct || script_file) {
  646. param_error = "You can use -r only once.\n";
  647. break;
  648. }
  649. } else if (behavior != PHP_MODE_STANDARD || interactive) {
  650. param_error = param_mode_conflict;
  651. break;
  652. }
  653. behavior=PHP_MODE_CLI_DIRECT;
  654. exec_direct=php_optarg;
  655. break;
  656. case 'R':
  657. if (behavior == PHP_MODE_PROCESS_STDIN) {
  658. if (exec_run || script_file) {
  659. param_error = "You can use -R or -F only once.\n";
  660. break;
  661. }
  662. } else if (behavior != PHP_MODE_STANDARD) {
  663. param_error = param_mode_conflict;
  664. break;
  665. }
  666. behavior=PHP_MODE_PROCESS_STDIN;
  667. exec_run=php_optarg;
  668. break;
  669. case 'B':
  670. if (behavior == PHP_MODE_PROCESS_STDIN) {
  671. if (exec_begin) {
  672. param_error = "You can use -B only once.\n";
  673. break;
  674. }
  675. } else if (behavior != PHP_MODE_STANDARD || interactive) {
  676. param_error = param_mode_conflict;
  677. break;
  678. }
  679. behavior=PHP_MODE_PROCESS_STDIN;
  680. exec_begin=php_optarg;
  681. break;
  682. case 'E':
  683. if (behavior == PHP_MODE_PROCESS_STDIN) {
  684. if (exec_end) {
  685. param_error = "You can use -E only once.\n";
  686. break;
  687. }
  688. } else if (behavior != PHP_MODE_STANDARD || interactive) {
  689. param_error = param_mode_conflict;
  690. break;
  691. }
  692. behavior=PHP_MODE_PROCESS_STDIN;
  693. exec_end=php_optarg;
  694. break;
  695. case 's': /* generate highlighted HTML from source */
  696. if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
  697. param_error = "Source highlighting only works for files.\n";
  698. break;
  699. }
  700. behavior=PHP_MODE_HIGHLIGHT;
  701. break;
  702. case 'w':
  703. if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
  704. param_error = "Source stripping only works for files.\n";
  705. break;
  706. }
  707. behavior=PHP_MODE_STRIP;
  708. break;
  709. case 'z': /* load extension file */
  710. zend_load_extension(php_optarg);
  711. break;
  712. case 'H':
  713. hide_argv = 1;
  714. break;
  715. case 10:
  716. behavior=PHP_MODE_REFLECTION_FUNCTION;
  717. reflection_what = php_optarg;
  718. break;
  719. case 11:
  720. behavior=PHP_MODE_REFLECTION_CLASS;
  721. reflection_what = php_optarg;
  722. break;
  723. case 12:
  724. behavior=PHP_MODE_REFLECTION_EXTENSION;
  725. reflection_what = php_optarg;
  726. break;
  727. case 13:
  728. behavior=PHP_MODE_REFLECTION_ZEND_EXTENSION;
  729. reflection_what = php_optarg;
  730. break;
  731. case 14:
  732. behavior=PHP_MODE_REFLECTION_EXT_INFO;
  733. reflection_what = php_optarg;
  734. break;
  735. case 15:
  736. behavior = PHP_MODE_SHOW_INI_CONFIG;
  737. break;
  738. case 16:
  739. num_repeats = atoi(php_optarg);
  740. break;
  741. default:
  742. break;
  743. }
  744. }
  745. if (param_error) {
  746. PUTS(param_error);
  747. EG(exit_status) = 1;
  748. goto err;
  749. }
  750. #if defined(PHP_WIN32) && !defined(PHP_CLI_WIN32_NO_CONSOLE) && (HAVE_LIBREADLINE || HAVE_LIBEDIT) && !defined(COMPILE_DL_READLINE)
  751. if (!interactive) {
  752. /* The -a option was not passed. If there is no file, it could
  753. still make sense to run interactively. The presence of a file
  754. is essential to mitigate buggy console info. */
  755. interactive = php_win32_console_is_own() &&
  756. !(script_file ||
  757. argc > php_optind && behavior!=PHP_MODE_CLI_DIRECT &&
  758. behavior!=PHP_MODE_PROCESS_STDIN &&
  759. strcmp(argv[php_optind-1],"--")
  760. );
  761. }
  762. #endif
  763. if (interactive) {
  764. printf("Interactive shell\n\n");
  765. fflush(stdout);
  766. }
  767. if (num_repeats > 1) {
  768. fprintf(stdout, "Executing for the first time...\n");
  769. fflush(stdout);
  770. }
  771. do_repeat:
  772. /* only set script_file if not set already and not in direct mode and not at end of parameter list */
  773. if (argc > php_optind
  774. && !script_file
  775. && behavior!=PHP_MODE_CLI_DIRECT
  776. && behavior!=PHP_MODE_PROCESS_STDIN
  777. && strcmp(argv[php_optind-1],"--"))
  778. {
  779. script_file=argv[php_optind];
  780. php_optind++;
  781. }
  782. if (script_file) {
  783. virtual_cwd_activate();
  784. if (cli_seek_file_begin(&file_handle, script_file) != SUCCESS) {
  785. goto err;
  786. } else {
  787. char real_path[MAXPATHLEN];
  788. if (VCWD_REALPATH(script_file, real_path)) {
  789. translated_path = strdup(real_path);
  790. }
  791. script_filename = script_file;
  792. php_self = script_file;
  793. }
  794. } else {
  795. /* We could handle PHP_MODE_PROCESS_STDIN in a different manner */
  796. /* here but this would make things only more complicated. And it */
  797. /* is consistent with the way -R works where the stdin file handle*/
  798. /* is also accessible. */
  799. php_self = "Standard input code";
  800. if (behavior < PHP_MODE_CLI_DIRECT
  801. && (!interactive || PHP_MODE_STANDARD != PHP_MODE_STANDARD)) {
  802. zend_stream_init_fp(&file_handle, stdin, php_self);
  803. file_handle.primary_script = 1;
  804. }
  805. }
  806. /* before registering argv to module exchange the *new* argv[0] */
  807. /* we can achieve this without allocating more memory */
  808. SG(request_info).argc=argc-php_optind+1;
  809. arg_excp = argv+php_optind-1;
  810. arg_free = argv[php_optind-1];
  811. SG(request_info).path_translated = translated_path ? translated_path : php_self;
  812. argv[php_optind-1] = php_self;
  813. SG(request_info).argv=argv+php_optind-1;
  814. if (php_request_startup()==FAILURE) {
  815. *arg_excp = arg_free;
  816. PUTS("Could not startup.\n");
  817. goto err;
  818. }
  819. request_started = 1;
  820. CG(skip_shebang) = 1;
  821. zend_register_bool_constant(
  822. ZEND_STRL("PHP_CLI_PROCESS_TITLE"),
  823. is_ps_title_available() == PS_TITLE_SUCCESS,
  824. CONST_CS, 0);
  825. *arg_excp = arg_free; /* reconstruct argv */
  826. if (hide_argv) {
  827. int i;
  828. for (i = 1; i < argc; i++) {
  829. memset(argv[i], 0, strlen(argv[i]));
  830. }
  831. }
  832. zend_is_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_SERVER));
  833. PG(during_request_startup) = 0;
  834. switch (behavior) {
  835. case PHP_MODE_STANDARD:
  836. if (script_file) {
  837. cli_register_file_handles(/* no_close */ PHP_DEBUG || num_repeats > 1);
  838. }
  839. if (interactive) {
  840. EG(exit_status) = cli_shell_callbacks.cli_shell_run();
  841. } else {
  842. php_execute_script(&file_handle);
  843. }
  844. break;
  845. case PHP_MODE_LINT:
  846. EG(exit_status) = php_lint_script(&file_handle);
  847. if (EG(exit_status) == SUCCESS) {
  848. zend_printf("No syntax errors detected in %s\n", php_self);
  849. } else {
  850. zend_printf("Errors parsing %s\n", php_self);
  851. }
  852. break;
  853. case PHP_MODE_STRIP:
  854. if (open_file_for_scanning(&file_handle)==SUCCESS) {
  855. zend_strip();
  856. }
  857. goto out;
  858. break;
  859. case PHP_MODE_HIGHLIGHT:
  860. {
  861. zend_syntax_highlighter_ini syntax_highlighter_ini;
  862. if (open_file_for_scanning(&file_handle)==SUCCESS) {
  863. php_get_highlight_struct(&syntax_highlighter_ini);
  864. zend_highlight(&syntax_highlighter_ini);
  865. }
  866. goto out;
  867. }
  868. break;
  869. case PHP_MODE_CLI_DIRECT:
  870. cli_register_file_handles(/* no_close */ PHP_DEBUG || num_repeats > 1);
  871. zend_eval_string_ex(exec_direct, NULL, "Command line code", 1);
  872. break;
  873. case PHP_MODE_PROCESS_STDIN:
  874. {
  875. char *input;
  876. size_t len, index = 0;
  877. zval argn, argi;
  878. if (!exec_run && script_file) {
  879. zend_string_release_ex(file_handle.filename, 0);
  880. file_handle.filename = NULL;
  881. }
  882. cli_register_file_handles(/* no_close */ PHP_DEBUG || num_repeats > 1);
  883. if (exec_begin) {
  884. zend_eval_string_ex(exec_begin, NULL, "Command line begin code", 1);
  885. }
  886. while (EG(exit_status) == SUCCESS && (input=php_stream_gets(s_in_process, NULL, 0)) != NULL) {
  887. len = strlen(input);
  888. while (len > 0 && len-- && (input[len]=='\n' || input[len]=='\r')) {
  889. input[len] = '\0';
  890. }
  891. ZVAL_STRINGL(&argn, input, len + 1);
  892. zend_hash_str_update(&EG(symbol_table), "argn", sizeof("argn")-1, &argn);
  893. ZVAL_LONG(&argi, ++index);
  894. zend_hash_str_update(&EG(symbol_table), "argi", sizeof("argi")-1, &argi);
  895. if (exec_run) {
  896. zend_eval_string_ex(exec_run, NULL, "Command line run code", 1);
  897. } else {
  898. if (script_file) {
  899. if (cli_seek_file_begin(&file_handle, script_file) != SUCCESS) {
  900. EG(exit_status) = 1;
  901. } else {
  902. CG(skip_shebang) = 1;
  903. php_execute_script(&file_handle);
  904. }
  905. }
  906. }
  907. efree(input);
  908. }
  909. if (exec_end) {
  910. zend_eval_string_ex(exec_end, NULL, "Command line end code", 1);
  911. }
  912. break;
  913. }
  914. case PHP_MODE_REFLECTION_FUNCTION:
  915. case PHP_MODE_REFLECTION_CLASS:
  916. case PHP_MODE_REFLECTION_EXTENSION:
  917. case PHP_MODE_REFLECTION_ZEND_EXTENSION:
  918. {
  919. zend_class_entry *pce = NULL;
  920. zval arg, ref;
  921. zend_execute_data execute_data;
  922. switch (behavior) {
  923. default:
  924. break;
  925. case PHP_MODE_REFLECTION_FUNCTION:
  926. if (strstr(reflection_what, "::")) {
  927. pce = reflection_method_ptr;
  928. } else {
  929. pce = reflection_function_ptr;
  930. }
  931. break;
  932. case PHP_MODE_REFLECTION_CLASS:
  933. pce = reflection_class_ptr;
  934. break;
  935. case PHP_MODE_REFLECTION_EXTENSION:
  936. pce = reflection_extension_ptr;
  937. break;
  938. case PHP_MODE_REFLECTION_ZEND_EXTENSION:
  939. pce = reflection_zend_extension_ptr;
  940. break;
  941. }
  942. ZVAL_STRING(&arg, reflection_what);
  943. object_init_ex(&ref, pce);
  944. memset(&execute_data, 0, sizeof(zend_execute_data));
  945. EG(current_execute_data) = &execute_data;
  946. zend_call_known_instance_method_with_1_params(
  947. pce->constructor, Z_OBJ(ref), NULL, &arg);
  948. if (EG(exception)) {
  949. zval rv;
  950. zval *msg = zend_read_property(zend_ce_exception, EG(exception), "message", sizeof("message")-1, 0, &rv);
  951. zend_printf("Exception: %s\n", Z_STRVAL_P(msg));
  952. zend_object_release(EG(exception));
  953. EG(exception) = NULL;
  954. EG(exit_status) = 1;
  955. } else {
  956. zend_print_zval(&ref, 0);
  957. zend_write("\n", 1);
  958. }
  959. zval_ptr_dtor(&ref);
  960. zval_ptr_dtor(&arg);
  961. break;
  962. }
  963. case PHP_MODE_REFLECTION_EXT_INFO:
  964. {
  965. size_t len = strlen(reflection_what);
  966. char *lcname = zend_str_tolower_dup(reflection_what, len);
  967. zend_module_entry *module;
  968. if ((module = zend_hash_str_find_ptr(&module_registry, lcname, len)) == NULL) {
  969. if (!strcmp(reflection_what, "main")) {
  970. display_ini_entries(NULL);
  971. } else {
  972. zend_printf("Extension '%s' not present.\n", reflection_what);
  973. EG(exit_status) = 1;
  974. }
  975. } else {
  976. php_info_print_module(module);
  977. }
  978. efree(lcname);
  979. break;
  980. }
  981. case PHP_MODE_SHOW_INI_CONFIG:
  982. {
  983. zend_printf("Configuration File (php.ini) Path: %s\n", PHP_CONFIG_FILE_PATH);
  984. zend_printf("Loaded Configuration File: %s\n", php_ini_opened_path ? php_ini_opened_path : "(none)");
  985. zend_printf("Scan for additional .ini files in: %s\n", php_ini_scanned_path ? php_ini_scanned_path : "(none)");
  986. zend_printf("Additional .ini files parsed: %s\n", php_ini_scanned_files ? php_ini_scanned_files : "(none)");
  987. break;
  988. }
  989. }
  990. } zend_end_try();
  991. out:
  992. if (file_handle.filename) {
  993. zend_destroy_file_handle(&file_handle);
  994. }
  995. if (request_started) {
  996. php_request_shutdown((void *) 0);
  997. }
  998. if (translated_path) {
  999. free(translated_path);
  1000. }
  1001. /* Don't repeat fork()ed processes. */
  1002. if (--num_repeats && pid == getpid()) {
  1003. fprintf(stdout, "Finished execution, repeating...\n");
  1004. fflush(stdout);
  1005. goto do_repeat;
  1006. }
  1007. return EG(exit_status);
  1008. err:
  1009. sapi_deactivate();
  1010. zend_ini_deactivate();
  1011. EG(exit_status) = 1;
  1012. goto out;
  1013. }
  1014. /* }}} */
  1015. /* {{{ main */
  1016. #ifdef PHP_CLI_WIN32_NO_CONSOLE
  1017. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  1018. #else
  1019. int main(int argc, char *argv[])
  1020. #endif
  1021. {
  1022. #if defined(PHP_WIN32)
  1023. # ifdef PHP_CLI_WIN32_NO_CONSOLE
  1024. int argc = __argc;
  1025. char **argv = __argv;
  1026. # endif
  1027. int num_args;
  1028. wchar_t **argv_wide;
  1029. char **argv_save = argv;
  1030. BOOL using_wide_argv = 0;
  1031. #endif
  1032. int c;
  1033. int exit_status = SUCCESS;
  1034. int module_started = 0, sapi_started = 0;
  1035. char *php_optarg = NULL;
  1036. int php_optind = 1, use_extended_info = 0;
  1037. char *ini_path_override = NULL;
  1038. char *ini_entries = NULL;
  1039. size_t ini_entries_len = 0;
  1040. int ini_ignore = 0;
  1041. sapi_module_struct *sapi_module = &cli_sapi_module;
  1042. /*
  1043. * Do not move this initialization. It needs to happen before argv is used
  1044. * in any way.
  1045. */
  1046. argv = save_ps_args(argc, argv);
  1047. #if defined(PHP_WIN32) && !defined(PHP_CLI_WIN32_NO_CONSOLE)
  1048. php_win32_console_fileno_set_vt100(STDOUT_FILENO, TRUE);
  1049. php_win32_console_fileno_set_vt100(STDERR_FILENO, TRUE);
  1050. #endif
  1051. cli_sapi_module.additional_functions = additional_functions;
  1052. #if defined(PHP_WIN32) && defined(_DEBUG)
  1053. {
  1054. char *tmp = getenv("PHP_WIN32_DEBUG_HEAP");
  1055. if (tmp && ZEND_ATOL(tmp)) {
  1056. int tmp_flag;
  1057. _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
  1058. _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
  1059. _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
  1060. _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
  1061. _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
  1062. _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
  1063. tmp_flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
  1064. tmp_flag |= _CRTDBG_DELAY_FREE_MEM_DF;
  1065. tmp_flag |= _CRTDBG_LEAK_CHECK_DF;
  1066. _CrtSetDbgFlag(tmp_flag);
  1067. }
  1068. }
  1069. #endif
  1070. #if defined(SIGPIPE) && defined(SIG_IGN)
  1071. signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
  1072. that sockets created via fsockopen()
  1073. don't kill PHP if the remote site
  1074. closes it. in apache|apxs mode apache
  1075. does that for us! thies@thieso.net
  1076. 20000419 */
  1077. #endif
  1078. #ifdef ZTS
  1079. php_tsrm_startup();
  1080. # ifdef PHP_WIN32
  1081. ZEND_TSRMLS_CACHE_UPDATE();
  1082. # endif
  1083. #endif
  1084. zend_signal_startup();
  1085. #ifdef PHP_WIN32
  1086. _fmode = _O_BINARY; /*sets default for file streams to binary */
  1087. setmode(_fileno(stdin), O_BINARY); /* make the stdio mode be binary */
  1088. setmode(_fileno(stdout), O_BINARY); /* make the stdio mode be binary */
  1089. setmode(_fileno(stderr), O_BINARY); /* make the stdio mode be binary */
  1090. #endif
  1091. while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 1, 2))!=-1) {
  1092. switch (c) {
  1093. case 'c':
  1094. if (ini_path_override) {
  1095. free(ini_path_override);
  1096. }
  1097. ini_path_override = strdup(php_optarg);
  1098. break;
  1099. case 'n':
  1100. ini_ignore = 1;
  1101. break;
  1102. case 'd': {
  1103. /* define ini entries on command line */
  1104. size_t len = strlen(php_optarg);
  1105. char *val;
  1106. if ((val = strchr(php_optarg, '='))) {
  1107. val++;
  1108. if (!isalnum(*val) && *val != '"' && *val != '\'' && *val != '\0') {
  1109. ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("\"\"\n\0"));
  1110. memcpy(ini_entries + ini_entries_len, php_optarg, (val - php_optarg));
  1111. ini_entries_len += (val - php_optarg);
  1112. memcpy(ini_entries + ini_entries_len, "\"", 1);
  1113. ini_entries_len++;
  1114. memcpy(ini_entries + ini_entries_len, val, len - (val - php_optarg));
  1115. ini_entries_len += len - (val - php_optarg);
  1116. memcpy(ini_entries + ini_entries_len, "\"\n\0", sizeof("\"\n\0"));
  1117. ini_entries_len += sizeof("\n\0\"") - 2;
  1118. } else {
  1119. ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("\n\0"));
  1120. memcpy(ini_entries + ini_entries_len, php_optarg, len);
  1121. memcpy(ini_entries + ini_entries_len + len, "\n\0", sizeof("\n\0"));
  1122. ini_entries_len += len + sizeof("\n\0") - 2;
  1123. }
  1124. } else {
  1125. ini_entries = realloc(ini_entries, ini_entries_len + len + sizeof("=1\n\0"));
  1126. memcpy(ini_entries + ini_entries_len, php_optarg, len);
  1127. memcpy(ini_entries + ini_entries_len + len, "=1\n\0", sizeof("=1\n\0"));
  1128. ini_entries_len += len + sizeof("=1\n\0") - 2;
  1129. }
  1130. break;
  1131. }
  1132. #ifndef PHP_CLI_WIN32_NO_CONSOLE
  1133. case 'S':
  1134. sapi_module = &cli_server_sapi_module;
  1135. cli_server_sapi_module.additional_functions = server_additional_functions;
  1136. break;
  1137. #endif
  1138. case 'h': /* help & quit */
  1139. case '?':
  1140. php_cli_usage(argv[0]);
  1141. goto out;
  1142. case PHP_GETOPT_INVALID_ARG: /* print usage on bad options, exit 1 */
  1143. php_cli_usage(argv[0]);
  1144. exit_status = 1;
  1145. goto out;
  1146. case 'i': case 'v': case 'm':
  1147. sapi_module = &cli_sapi_module;
  1148. goto exit_loop;
  1149. case 'e': /* enable extended info output */
  1150. use_extended_info = 1;
  1151. break;
  1152. }
  1153. }
  1154. exit_loop:
  1155. sapi_module->ini_defaults = sapi_cli_ini_defaults;
  1156. sapi_module->php_ini_path_override = ini_path_override;
  1157. sapi_module->phpinfo_as_text = 1;
  1158. sapi_module->php_ini_ignore_cwd = 1;
  1159. sapi_startup(sapi_module);
  1160. sapi_started = 1;
  1161. sapi_module->php_ini_ignore = ini_ignore;
  1162. sapi_module->executable_location = argv[0];
  1163. if (sapi_module == &cli_sapi_module) {
  1164. if (ini_entries) {
  1165. ini_entries = realloc(ini_entries, ini_entries_len + sizeof(HARDCODED_INI));
  1166. memmove(ini_entries + sizeof(HARDCODED_INI) - 2, ini_entries, ini_entries_len + 1);
  1167. memcpy(ini_entries, HARDCODED_INI, sizeof(HARDCODED_INI) - 2);
  1168. } else {
  1169. ini_entries = malloc(sizeof(HARDCODED_INI));
  1170. memcpy(ini_entries, HARDCODED_INI, sizeof(HARDCODED_INI));
  1171. }
  1172. ini_entries_len += sizeof(HARDCODED_INI) - 2;
  1173. }
  1174. sapi_module->ini_entries = ini_entries;
  1175. /* startup after we get the above ini override se we get things right */
  1176. if (sapi_module->startup(sapi_module) == FAILURE) {
  1177. /* there is no way to see if we must call zend_ini_deactivate()
  1178. * since we cannot check if EG(ini_directives) has been initialized
  1179. * because the executor's constructor does not set initialize it.
  1180. * Apart from that there seems no need for zend_ini_deactivate() yet.
  1181. * So we goto out_err.*/
  1182. exit_status = 1;
  1183. goto out;
  1184. }
  1185. module_started = 1;
  1186. #if defined(PHP_WIN32)
  1187. php_win32_cp_cli_setup();
  1188. orig_cp = (php_win32_cp_get_orig())->id;
  1189. /* Ignore the delivered argv and argc, read from W API. This place
  1190. might be too late though, but this is the earliest place ATW
  1191. we can access the internal charset information from PHP. */
  1192. argv_wide = CommandLineToArgvW(GetCommandLineW(), &num_args);
  1193. PHP_WIN32_CP_W_TO_ANY_ARRAY(argv_wide, num_args, argv, argc)
  1194. using_wide_argv = 1;
  1195. SetConsoleCtrlHandler(php_cli_win32_ctrl_handler, TRUE);
  1196. #endif
  1197. /* -e option */
  1198. if (use_extended_info) {
  1199. CG(compiler_options) |= ZEND_COMPILE_EXTENDED_INFO;
  1200. }
  1201. zend_first_try {
  1202. #ifndef PHP_CLI_WIN32_NO_CONSOLE
  1203. if (sapi_module == &cli_sapi_module) {
  1204. #endif
  1205. exit_status = do_cli(argc, argv);
  1206. #ifndef PHP_CLI_WIN32_NO_CONSOLE
  1207. } else {
  1208. exit_status = do_cli_server(argc, argv);
  1209. }
  1210. #endif
  1211. } zend_end_try();
  1212. out:
  1213. if (ini_path_override) {
  1214. free(ini_path_override);
  1215. }
  1216. if (ini_entries) {
  1217. free(ini_entries);
  1218. }
  1219. if (module_started) {
  1220. php_module_shutdown();
  1221. }
  1222. if (sapi_started) {
  1223. sapi_shutdown();
  1224. }
  1225. #ifdef ZTS
  1226. tsrm_shutdown();
  1227. #endif
  1228. #if defined(PHP_WIN32)
  1229. (void)php_win32_cp_cli_restore();
  1230. if (using_wide_argv) {
  1231. PHP_WIN32_CP_FREE_ARRAY(argv, argc);
  1232. LocalFree(argv_wide);
  1233. }
  1234. argv = argv_save;
  1235. #endif
  1236. /*
  1237. * Do not move this de-initialization. It needs to happen right before
  1238. * exiting.
  1239. */
  1240. cleanup_ps_args(argv);
  1241. exit(exit_status);
  1242. }
  1243. /* }}} */