php_cli.c 39 KB

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