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