PHP Kernel parsing (iii)

Source: Internet
Author: User

The PHP version read here is PHP-7.1.0 RC3, the platform for reading the code is Linux

# Main

The rest of the code added the next note is all posted (this is the simplified main function, the removal of some insignificant code snippets):

IntMain(int argc,Char *argv[]) {... sapi_module_struct *sapi_module = &cli_sapi_module; argv = Save_ps_args (argc, argv);This gets the parameters of the current execution process, environment variables, and so on. In order to modify the ARGV variable for subsequent use on a specific platform. Cli_sapi_module.additional_functions = additional_functions;CLI mode-specific functions ...#Ifdef ZTS Tsrm_startup (1,1,0,NULL); (void) Ts_resource (0); Zend_tsrmls_cache_update ();#endif Zend_signal_startup ();Set the signal to 0 for some signal bits that need to be reactedGet the parameters, do some initialization, or some simple actions, such as Helpwhile (c = php_getopt (argc, argv, OPTIONS, &php_optarg, &php_optind,0,2))! =-1) {Switch (c) {Here c is the ASCII value representing the returned string.Case' C ': ...Case' n ': Ini_ignore =1;Do not use INI file, code or other specified INI valueBreakCase' d ': {Configure the Key,val value of INI on the command line, the following behavior is modified ini_entries this variable ...}Case' H ':/* Help & Quit */Case'? ': Php_cli_usage (argv[0]);Goto out;Case' I ':Case' V ':Case' m ': Sapi_module = &cli_sapi_module;Goto Exit_loop;Case' E ':/* Enable extended info Output */Use_extended_info =1;Break }}exit_loop:sapi_module->ini_defaults = Sapi_cli_ini_defaults;Sets the INI value of initialization sapi_module->php_ini_path_override = Ini_path_override;Set the rewritten Ini_path address, if it is php-c, this is a non-null Sapi_module->phpinfo_as_text =1;Open the Print phpinfo switch, you can print out the phpinfo when needed SAPI_MODULE->PHP_INI_IGNORE_CWD =1;Do not look for php.ini Sapi_startup (sapi_module) in the current path;SAPI initialization behavior, such as initializing global variables sg sapi_started =1;Tag, indicating that the startup has been called, and that it needs to call Shundown when it is closed ...Start calling Sapi's Startup method on the CLI pattern, which is actually called the Php_cli_startup methodif (Sapi_module->startup (sapi_module) = = FAILURE) {Exit_status =1;goto out;} module_started = 1; //tag bit, marks the startup method that has called the module ... zend_first_try {exit_status = Do_cli (argc, argv); //this is actually called content} zend_end_try (); Out: //This code snippet is already exiting if (ini_path_override) {free (ini_path_override);} if (ini_entries) {free (ini_entries);} if (module_started) {Php_module_shutdown ();} if (sapi_started) {Sapi_shutdown ();} #ifdef ZTS tsrm_shutdown ();  #endif Cleanup_ps_args (argv); exit (Exit_status);}           

Actually look at the pseudo code is very simple:

tsrm_startup(1, 1, 0, NULL);  // TSM启动zend_signal_startup(); // 信号设置sapi_startup(sapi_module); // SAPI启动sapi_module->startup(sapi_module); // 当前模块的startupdo_cli(argc, argv); // 做实际的行为php_module_shutdown(); // 当前模块的shutdownsapi_shutdown(); // SAPI关闭tsrm_shutdown(); // TSM关闭

Well, actually saw a circle, inside the heaviest function is do_cli.

PHP parameters

DO_CLI inside you will see that depending on the parameters, there are many branches, and here you need to know what these parameters are all about.

Parameters
Role
Instance


Do_cli

We take the entire function of the DO_CLI function out of excess code, leaving only the key code as follows:

Staticint Do_cli (int argc,Char **argv) {... zend_try {I am dealing with i-output phpinfo content/V-output PHP version/m-Output extension informationwhile (c = php_getopt (argc, argv, OPTIONS, &php_optarg, &php_optind,0,2))! =-1) {Switch (c) {Case' I ':Output phpinfo content ... php_print_info (0xFFFFFFFF); ...GotoOutCase' V ':Output PHP Version information ... get_zend_version () ...GotoOutCase' m ':List all modules ... print_extensions (); ...GotoOutDefaultBreak } } ...The following code does a few things:1 behavior parameters are set according to the parameters2 files exist script_file the file is executedwhile (c = php_getopt (argc, argv, OPTIONS, &php_optarg, &php_optind,0,2))! =-1) {Switch (c) {Case' A ':PHP Interactive mode ... interactive=1; ...BreakCase' C ':Do not turn the CWD directory into the directory where the script resides. The default is that CWD is the current execution path, so nothing is done here.BreakCase' F ':Php-f <FILE> Enter interactive mode, execute <FILE> file once per execution line ... behavior=php_mode_process_stdin; Script_file = Php_optarg;BreakCase' F ':Php-f <FILE> parse and execute file ... script_file = Php_optarg;BreakCase' L ':Check file syntax for errors ... behavior=php_mode_lint;BreakCase' Q ':Quiet mode, default is Quiet modeBreakCase' R ':Execute scripts directly from the command line ... behavior=php_mode_cli_direct; Exec_direct=php_optarg;BreakCase' R ':The code script is executed once per line input, such as Php-r ' echo; ' ... behavior=php_mode_process_stdin; Exec_run=php_optarg;BreakCase' B ':Execute code script once before each input starts ... behavior=php_mode_process_stdin; Exec_begin=php_optarg;BreakCase' E ':Once the code script is executed once each input is completed, the above Rbe can refer to an example: Find Conf.d | Php-b ' $l =0; '-r ' $l + = count (@file ($ARGN)); '-E ' echo ' total Lines: $l \ n '; ' ... behavior=php_mode_process_stdin; Exec_end=php_optarg;BreakCase' s ':Use HTML highlighting to display the code, which may need to be used when some code is displayed ... behavior=php_mode_highlight;BreakCase' W ':PHP <file>-W to remove comments and extra spaces from <file> ... behavior=php_mode_strip;BreakCase' Z ':Load external extension zend_load_extension (PHP_OPTARG);BreakCase' H ':Hide all parameters hide_argv =1;BreakCase10:Show FUNCTION definition behavior=php_mode_reflection_function; Reflection_what = Php_optarg;BreakCase11:Display CLASS definition behavior=php_mode_reflection_class; Reflection_what = Php_optarg;BreakCase12:Show extension definition, note this is PHP extension behavior=php_mode_reflection_extension; Reflection_what = Php_optarg;BreakCase13:Displays ZEND extension definitions, such as Xdebug Behavior=php_mode_reflection_zend_extension; Reflection_what = Php_optarg;BreakCase14:Display the corresponding configuration of the extension behavior=php_mode_reflection_ext_info; Reflection_what = Php_optarg;BreakCase15:Show INI configuration behavior = php_mode_show_ini_config;BreakDefaultBreak } } ...After the request is initialized, the Request_startup is executedif (Php_request_startup () ==failure) {...Goto err; } ... zend_is_auto_global_str (Zend_strl ("_server"));Depending on the behavior to do different specific operations, this is the core methodSwitch (behavior) {Case Php_mode_standard:The standard is to execute a script file ... php_execute_script (&file_handle); ...BreakCase Php_mode_lint:Only check the file for any syntax errors exit_status = Php_lint_script (&file_handle); ...BreakCase Php_mode_strip: ... Zend_strip (); ...BreakCase Php_mode_highlight: Php_get_highlight_struct (&syntax_highlighter_ini); Zend_highlight (&syntax_highlighter_ini);GotoOutBreakCase Php_mode_cli_direct: ...if (ZEND_EVAL_STRING_EX (Exec_direct,Null"Command Line Code",1) = = FAILURE) {exit_status=254; }BreakCase Php_mode_process_stdin: zend_eval_string_ex (Exec_end,Null"Command Line End Code",1) ...BreakCase Php_mode_reflection_function:Case Php_mode_reflection_class:Case Php_mode_reflection_extension: case php_mode_reflection_zend_extension: ... Zval_string (&arg, reflection_what); OBJECT_INIT_EX (&ref, PCE); ... zend_call_method_with_1_params (&ref, PCE, &pce->constructor,  "__construct ", null, &arg); ... break; case php_mode_reflection_ext_info: ... if (module = zend_hash_str_find_ptr (&module_registry, Lcname, len)) = = null) {... display_ini_entries (null) ... break; case php_mode_show_ini_config: ... break;} } zend_end_try (); out: ... err: ...}            

The entire 200 lines of code is very well understood, the whole is wrapped in a zend_try...zend_catch. Take a few steps:

    • Processing-I,-M,-V parameters
    • Set Behavior,script_file and other variables for other parameters
    • Act differently according to behavior.

Back to our initial plan, what we want to know:

Our search is based on the parameter configuration of-R.

It actually calls the

zend_eval_string_ex(exec_direct, NULL, "Command line code", 1)

The exec_direct here is the echo 12 string

Transfer from http://home.cnblogs.com/u/yjf512/

PHP Kernel parsing (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.