php_cli.c 39 KB

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