Skip to content

Commit bca0c08

Browse files
authored
Make the CLI mode available via the SAPI globals (#14479)
* Remove tautologically false condition in php_cli.c * Make the CLI mode available via the SAPI globals When hooking into RINIT it is currently pretty much impossible to determine whether a file will actually be executed or if it just will be linted, highlighted, or comments stripped: The startup is identical for all of them and the chosen mode is not currently exposed to other extensions. The `SG(server_context)` is currently entirely unused for the `cli` SAPI. It appears to be appropriate to store the mode as a SAPI-specific information inside of it.
1 parent c3388c1 commit bca0c08

File tree

2 files changed

+78
-69
lines changed

2 files changed

+78
-69
lines changed

sapi/cli/cli.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,23 @@ typedef struct {
3636

3737
extern PHP_CLI_API cli_shell_callbacks_t *php_cli_get_shell_callbacks(void);
3838

39+
typedef enum php_cli_mode {
40+
PHP_CLI_MODE_STANDARD = 1,
41+
PHP_CLI_MODE_HIGHLIGHT = 2,
42+
PHP_CLI_MODE_LINT = 4,
43+
PHP_CLI_MODE_STRIP = 5,
44+
PHP_CLI_MODE_CLI_DIRECT = 6,
45+
PHP_CLI_MODE_PROCESS_STDIN = 7,
46+
PHP_CLI_MODE_REFLECTION_FUNCTION = 8,
47+
PHP_CLI_MODE_REFLECTION_CLASS = 9,
48+
PHP_CLI_MODE_REFLECTION_EXTENSION = 10,
49+
PHP_CLI_MODE_REFLECTION_EXT_INFO = 11,
50+
PHP_CLI_MODE_REFLECTION_ZEND_EXTENSION = 12,
51+
PHP_CLI_MODE_SHOW_INI_CONFIG = 13,
52+
} php_cli_mode;
53+
54+
typedef struct php_cli_server_context {
55+
php_cli_mode mode;
56+
} php_cli_server_context;
57+
3958
#endif /* CLI_H */

sapi/cli/php_cli.c

Lines changed: 59 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,6 @@ static DWORD orig_cp = 0;
107107
#define O_BINARY 0
108108
#endif
109109

110-
#define PHP_MODE_STANDARD 1
111-
#define PHP_MODE_HIGHLIGHT 2
112-
#define PHP_MODE_LINT 4
113-
#define PHP_MODE_STRIP 5
114-
#define PHP_MODE_CLI_DIRECT 6
115-
#define PHP_MODE_PROCESS_STDIN 7
116-
#define PHP_MODE_REFLECTION_FUNCTION 8
117-
#define PHP_MODE_REFLECTION_CLASS 9
118-
#define PHP_MODE_REFLECTION_EXTENSION 10
119-
#define PHP_MODE_REFLECTION_EXT_INFO 11
120-
#define PHP_MODE_REFLECTION_ZEND_EXTENSION 12
121-
#define PHP_MODE_SHOW_INI_CONFIG 13
122-
123110
static cli_shell_callbacks_t cli_shell_callbacks = { NULL, NULL, NULL };
124111
PHP_CLI_API cli_shell_callbacks_t *php_cli_get_shell_callbacks(void)
125112
{
@@ -602,7 +589,9 @@ static int do_cli(int argc, char **argv) /* {{{ */
602589
{
603590
int c;
604591
zend_file_handle file_handle;
605-
int behavior = PHP_MODE_STANDARD;
592+
php_cli_server_context context = {
593+
.mode = PHP_CLI_MODE_STANDARD
594+
};
606595
char *reflection_what = NULL;
607596
volatile int request_started = 0;
608597
char *php_optarg = NULL, *orig_optarg = NULL;
@@ -694,7 +683,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
694683
break;
695684
}
696685
if (!interactive) {
697-
if (behavior != PHP_MODE_STANDARD) {
686+
if (context.mode != PHP_CLI_MODE_STANDARD) {
698687
param_error = param_mode_conflict;
699688
break;
700689
}
@@ -708,21 +697,21 @@ static int do_cli(int argc, char **argv) /* {{{ */
708697
break;
709698

710699
case 'F':
711-
if (behavior == PHP_MODE_PROCESS_STDIN) {
700+
if (context.mode == PHP_CLI_MODE_PROCESS_STDIN) {
712701
if (exec_run || script_file) {
713702
param_error = "You can use -R or -F only once.\n";
714703
break;
715704
}
716-
} else if (behavior != PHP_MODE_STANDARD) {
705+
} else if (context.mode != PHP_CLI_MODE_STANDARD) {
717706
param_error = param_mode_conflict;
718707
break;
719708
}
720-
behavior=PHP_MODE_PROCESS_STDIN;
709+
context.mode=PHP_CLI_MODE_PROCESS_STDIN;
721710
script_file = php_optarg;
722711
break;
723712

724713
case 'f': /* parse file */
725-
if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
714+
if (context.mode == PHP_CLI_MODE_CLI_DIRECT || context.mode == PHP_CLI_MODE_PROCESS_STDIN) {
726715
param_error = param_mode_conflict;
727716
break;
728717
} else if (script_file) {
@@ -733,10 +722,10 @@ static int do_cli(int argc, char **argv) /* {{{ */
733722
break;
734723

735724
case 'l': /* syntax check mode */
736-
if (behavior != PHP_MODE_STANDARD) {
725+
if (context.mode != PHP_CLI_MODE_STANDARD) {
737726
break;
738727
}
739-
behavior=PHP_MODE_LINT;
728+
context.mode=PHP_CLI_MODE_LINT;
740729
/* We want to set the error exit status if at least one lint failed.
741730
* If all were successful we set the exit status to 0.
742731
* We already set EG(exit_status) here such that only failures set the exit status. */
@@ -748,75 +737,75 @@ static int do_cli(int argc, char **argv) /* {{{ */
748737
break;
749738

750739
case 'r': /* run code from command line */
751-
if (behavior == PHP_MODE_CLI_DIRECT) {
740+
if (context.mode == PHP_CLI_MODE_CLI_DIRECT) {
752741
if (exec_direct || script_file) {
753742
param_error = "You can use -r only once.\n";
754743
break;
755744
}
756-
} else if (behavior != PHP_MODE_STANDARD || interactive) {
745+
} else if (context.mode != PHP_CLI_MODE_STANDARD || interactive) {
757746
param_error = param_mode_conflict;
758747
break;
759748
}
760-
behavior=PHP_MODE_CLI_DIRECT;
749+
context.mode=PHP_CLI_MODE_CLI_DIRECT;
761750
exec_direct=php_optarg;
762751
break;
763752

764753
case 'R':
765-
if (behavior == PHP_MODE_PROCESS_STDIN) {
754+
if (context.mode == PHP_CLI_MODE_PROCESS_STDIN) {
766755
if (exec_run || script_file) {
767756
param_error = "You can use -R or -F only once.\n";
768757
break;
769758
}
770-
} else if (behavior != PHP_MODE_STANDARD) {
759+
} else if (context.mode != PHP_CLI_MODE_STANDARD) {
771760
param_error = param_mode_conflict;
772761
break;
773762
}
774-
behavior=PHP_MODE_PROCESS_STDIN;
763+
context.mode=PHP_CLI_MODE_PROCESS_STDIN;
775764
exec_run=php_optarg;
776765
break;
777766

778767
case 'B':
779-
if (behavior == PHP_MODE_PROCESS_STDIN) {
768+
if (context.mode == PHP_CLI_MODE_PROCESS_STDIN) {
780769
if (exec_begin) {
781770
param_error = "You can use -B only once.\n";
782771
break;
783772
}
784-
} else if (behavior != PHP_MODE_STANDARD || interactive) {
773+
} else if (context.mode != PHP_CLI_MODE_STANDARD || interactive) {
785774
param_error = param_mode_conflict;
786775
break;
787776
}
788-
behavior=PHP_MODE_PROCESS_STDIN;
777+
context.mode=PHP_CLI_MODE_PROCESS_STDIN;
789778
exec_begin=php_optarg;
790779
break;
791780

792781
case 'E':
793-
if (behavior == PHP_MODE_PROCESS_STDIN) {
782+
if (context.mode == PHP_CLI_MODE_PROCESS_STDIN) {
794783
if (exec_end) {
795784
param_error = "You can use -E only once.\n";
796785
break;
797786
}
798-
} else if (behavior != PHP_MODE_STANDARD || interactive) {
787+
} else if (context.mode != PHP_CLI_MODE_STANDARD || interactive) {
799788
param_error = param_mode_conflict;
800789
break;
801790
}
802-
behavior=PHP_MODE_PROCESS_STDIN;
791+
context.mode=PHP_CLI_MODE_PROCESS_STDIN;
803792
exec_end=php_optarg;
804793
break;
805794

806795
case 's': /* generate highlighted HTML from source */
807-
if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
796+
if (context.mode == PHP_CLI_MODE_CLI_DIRECT || context.mode == PHP_CLI_MODE_PROCESS_STDIN) {
808797
param_error = "Source highlighting only works for files.\n";
809798
break;
810799
}
811-
behavior=PHP_MODE_HIGHLIGHT;
800+
context.mode=PHP_CLI_MODE_HIGHLIGHT;
812801
break;
813802

814803
case 'w':
815-
if (behavior == PHP_MODE_CLI_DIRECT || behavior == PHP_MODE_PROCESS_STDIN) {
804+
if (context.mode == PHP_CLI_MODE_CLI_DIRECT || context.mode == PHP_CLI_MODE_PROCESS_STDIN) {
816805
param_error = "Source stripping only works for files.\n";
817806
break;
818807
}
819-
behavior=PHP_MODE_STRIP;
808+
context.mode=PHP_CLI_MODE_STRIP;
820809
break;
821810

822811
case 'z': /* load extension file */
@@ -826,27 +815,27 @@ static int do_cli(int argc, char **argv) /* {{{ */
826815
hide_argv = true;
827816
break;
828817
case 10:
829-
behavior=PHP_MODE_REFLECTION_FUNCTION;
818+
context.mode=PHP_CLI_MODE_REFLECTION_FUNCTION;
830819
reflection_what = php_optarg;
831820
break;
832821
case 11:
833-
behavior=PHP_MODE_REFLECTION_CLASS;
822+
context.mode=PHP_CLI_MODE_REFLECTION_CLASS;
834823
reflection_what = php_optarg;
835824
break;
836825
case 12:
837-
behavior=PHP_MODE_REFLECTION_EXTENSION;
826+
context.mode=PHP_CLI_MODE_REFLECTION_EXTENSION;
838827
reflection_what = php_optarg;
839828
break;
840829
case 13:
841-
behavior=PHP_MODE_REFLECTION_ZEND_EXTENSION;
830+
context.mode=PHP_CLI_MODE_REFLECTION_ZEND_EXTENSION;
842831
reflection_what = php_optarg;
843832
break;
844833
case 14:
845-
behavior=PHP_MODE_REFLECTION_EXT_INFO;
834+
context.mode=PHP_CLI_MODE_REFLECTION_EXT_INFO;
846835
reflection_what = php_optarg;
847836
break;
848837
case 15:
849-
behavior = PHP_MODE_SHOW_INI_CONFIG;
838+
context.mode = PHP_CLI_MODE_SHOW_INI_CONFIG;
850839
break;
851840
case 16:
852841
num_repeats = atoi(php_optarg);
@@ -869,8 +858,8 @@ static int do_cli(int argc, char **argv) /* {{{ */
869858
is essential to mitigate buggy console info. */
870859
interactive = php_win32_console_is_own() &&
871860
!(script_file ||
872-
argc > php_optind && behavior!=PHP_MODE_CLI_DIRECT &&
873-
behavior!=PHP_MODE_PROCESS_STDIN &&
861+
argc > php_optind && context.mode!=PHP_CLI_MODE_CLI_DIRECT &&
862+
context.mode!=PHP_CLI_MODE_PROCESS_STDIN &&
874863
strcmp(argv[php_optind-1],"--")
875864
);
876865
}
@@ -890,8 +879,8 @@ static int do_cli(int argc, char **argv) /* {{{ */
890879
/* only set script_file if not set already and not in direct mode and not at end of parameter list */
891880
if (argc > php_optind
892881
&& !script_file
893-
&& behavior!=PHP_MODE_CLI_DIRECT
894-
&& behavior!=PHP_MODE_PROCESS_STDIN
882+
&& context.mode!=PHP_CLI_MODE_CLI_DIRECT
883+
&& context.mode!=PHP_CLI_MODE_PROCESS_STDIN
895884
&& strcmp(argv[php_optind-1],"--"))
896885
{
897886
script_file=argv[php_optind];
@@ -910,13 +899,13 @@ static int do_cli(int argc, char **argv) /* {{{ */
910899
php_self = script_file;
911900
}
912901
} else {
913-
/* We could handle PHP_MODE_PROCESS_STDIN in a different manner */
902+
/* We could handle PHP_CLI_MODE_PROCESS_STDIN in a different manner */
914903
/* here but this would make things only more complicated. And it */
915904
/* is consistent with the way -R works where the stdin file handle*/
916905
/* is also accessible. */
917906
php_self = "Standard input code";
918-
if (behavior < PHP_MODE_CLI_DIRECT
919-
&& (!interactive || PHP_MODE_STANDARD != PHP_MODE_STANDARD)) {
907+
if (context.mode < PHP_CLI_MODE_CLI_DIRECT
908+
&& !interactive) {
920909
zend_stream_init_fp(&file_handle, stdin, php_self);
921910
file_handle.primary_script = 1;
922911
}
@@ -930,6 +919,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
930919
SG(request_info).path_translated = translated_path ? translated_path : php_self;
931920
argv[php_optind-1] = php_self;
932921
SG(request_info).argv=argv+php_optind-1;
922+
SG(server_context) = &context;
933923

934924
if (php_request_startup()==FAILURE) {
935925
*arg_excp = arg_free;
@@ -956,8 +946,8 @@ static int do_cli(int argc, char **argv) /* {{{ */
956946
zend_is_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_SERVER));
957947

958948
PG(during_request_startup) = 0;
959-
switch (behavior) {
960-
case PHP_MODE_STANDARD:
949+
switch (context.mode) {
950+
case PHP_CLI_MODE_STANDARD:
961951
cli_register_file_handles();
962952

963953
if (interactive) {
@@ -966,21 +956,21 @@ static int do_cli(int argc, char **argv) /* {{{ */
966956
php_execute_script(&file_handle);
967957
}
968958
break;
969-
case PHP_MODE_LINT:
959+
case PHP_CLI_MODE_LINT:
970960
if (php_lint_script(&file_handle) == SUCCESS) {
971961
zend_printf("No syntax errors detected in %s\n", php_self);
972962
} else {
973963
zend_printf("Errors parsing %s\n", php_self);
974964
EG(exit_status) = 255;
975965
}
976966
break;
977-
case PHP_MODE_STRIP:
967+
case PHP_CLI_MODE_STRIP:
978968
if (open_file_for_scanning(&file_handle)==SUCCESS) {
979969
zend_strip();
980970
}
981971
goto out;
982972
break;
983-
case PHP_MODE_HIGHLIGHT:
973+
case PHP_CLI_MODE_HIGHLIGHT:
984974
{
985975
zend_syntax_highlighter_ini syntax_highlighter_ini;
986976

@@ -991,12 +981,12 @@ static int do_cli(int argc, char **argv) /* {{{ */
991981
goto out;
992982
}
993983
break;
994-
case PHP_MODE_CLI_DIRECT:
984+
case PHP_CLI_MODE_CLI_DIRECT:
995985
cli_register_file_handles();
996986
zend_eval_string_ex(exec_direct, NULL, "Command line code", 1);
997987
break;
998988

999-
case PHP_MODE_PROCESS_STDIN:
989+
case PHP_CLI_MODE_PROCESS_STDIN:
1000990
{
1001991
char *input;
1002992
size_t len, index = 0;
@@ -1042,32 +1032,32 @@ static int do_cli(int argc, char **argv) /* {{{ */
10421032
break;
10431033
}
10441034

1045-
case PHP_MODE_REFLECTION_FUNCTION:
1046-
case PHP_MODE_REFLECTION_CLASS:
1047-
case PHP_MODE_REFLECTION_EXTENSION:
1048-
case PHP_MODE_REFLECTION_ZEND_EXTENSION:
1035+
case PHP_CLI_MODE_REFLECTION_FUNCTION:
1036+
case PHP_CLI_MODE_REFLECTION_CLASS:
1037+
case PHP_CLI_MODE_REFLECTION_EXTENSION:
1038+
case PHP_CLI_MODE_REFLECTION_ZEND_EXTENSION:
10491039
{
10501040
zend_class_entry *pce = NULL;
10511041
zval arg, ref;
10521042
zend_execute_data execute_data;
10531043

1054-
switch (behavior) {
1044+
switch (context.mode) {
10551045
default:
10561046
break;
1057-
case PHP_MODE_REFLECTION_FUNCTION:
1047+
case PHP_CLI_MODE_REFLECTION_FUNCTION:
10581048
if (strstr(reflection_what, "::")) {
10591049
pce = reflection_method_ptr;
10601050
} else {
10611051
pce = reflection_function_ptr;
10621052
}
10631053
break;
1064-
case PHP_MODE_REFLECTION_CLASS:
1054+
case PHP_CLI_MODE_REFLECTION_CLASS:
10651055
pce = reflection_class_ptr;
10661056
break;
1067-
case PHP_MODE_REFLECTION_EXTENSION:
1057+
case PHP_CLI_MODE_REFLECTION_EXTENSION:
10681058
pce = reflection_extension_ptr;
10691059
break;
1070-
case PHP_MODE_REFLECTION_ZEND_EXTENSION:
1060+
case PHP_CLI_MODE_REFLECTION_ZEND_EXTENSION:
10711061
pce = reflection_zend_extension_ptr;
10721062
break;
10731063
}
@@ -1096,7 +1086,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
10961086

10971087
break;
10981088
}
1099-
case PHP_MODE_REFLECTION_EXT_INFO:
1089+
case PHP_CLI_MODE_REFLECTION_EXT_INFO:
11001090
{
11011091
size_t len = strlen(reflection_what);
11021092
char *lcname = zend_str_tolower_dup(reflection_what, len);
@@ -1117,7 +1107,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
11171107
break;
11181108
}
11191109

1120-
case PHP_MODE_SHOW_INI_CONFIG:
1110+
case PHP_CLI_MODE_SHOW_INI_CONFIG:
11211111
{
11221112
zend_printf("Configuration File (php.ini) Path: %s\n", PHP_CONFIG_FILE_PATH);
11231113
zend_printf("Loaded Configuration File: %s\n", php_ini_opened_path ? php_ini_opened_path : "(none)");
@@ -1140,7 +1130,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
11401130
free(translated_path);
11411131
translated_path = NULL;
11421132
}
1143-
if (behavior == PHP_MODE_LINT && argc > php_optind && strcmp(argv[php_optind],"--")) {
1133+
if (context.mode == PHP_CLI_MODE_LINT && argc > php_optind && strcmp(argv[php_optind],"--")) {
11441134
script_file = NULL;
11451135
goto do_repeat;
11461136
}

0 commit comments

Comments
 (0)