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