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