helly Fri May 30 22:20:08 2003 EDT Modified files: (Branch: PHP_4_3) /php4/sapi/cli getopt.c php_cli.c php_getopt.h /php4/sapi/cgi cgi_main.c getopt.c php_getopt.h Log: Reapply long opts
Index: php4/sapi/cli/getopt.c diff -u php4/sapi/cli/getopt.c:1.3.4.2 php4/sapi/cli/getopt.c:1.3.4.3 --- php4/sapi/cli/getopt.c:1.3.4.2 Fri May 30 21:37:45 2003 +++ php4/sapi/cli/getopt.c Fri May 30 22:20:08 2003 @@ -1,4 +1,22 @@ -/* Borrowed from Apache NT Port */ +/* + +----------------------------------------------------------------------+ + | PHP Version 4 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2003 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 2.02 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available at through the world-wide-web at | + | http://www.php.net/license/2_02.txt. | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | [EMAIL PROTECTED] so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Marcus Boerger <[EMAIL PROTECTED]> | + +----------------------------------------------------------------------+ +*/ + +/* $Id: getopt.c,v 1.3.4.3 2003/05/31 02:20:08 helly Exp $ */ #include <stdio.h> #include <string.h> @@ -10,164 +28,129 @@ #define OPTERRARG (3) -char *ap_php_optarg; -int ap_php_optind = 1; -static int ap_php_opterr = 1; - -static int -ap_php_optiserr(int argc, char * const *argv, int oint, const char *optstr, - int optchr, int err) +static int php_opt_error(int argc, char * const *argv, int oint, int optchr, int err, int show_err) { - if (ap_php_opterr) - { - fprintf(stderr, "Error in argument %d, char %d: ", oint, optchr+1); - switch(err) - { - case OPTERRCOLON: - fprintf(stderr, ": in flags\n"); - break; - case OPTERRNF: - fprintf(stderr, "option not found %c\n", argv[oint][optchr]); - break; - case OPTERRARG: - fprintf(stderr, "no argument for option %c\n", argv[oint][optchr]); - break; - default: - fprintf(stderr, "unknown\n"); - break; - } - } - return('?'); + if (show_err) + { + fprintf(stderr, "Error in argument %d, char %d: ", oint, optchr+1); + switch(err) + { + case OPTERRCOLON: + fprintf(stderr, ": in flags\n"); + break; + case OPTERRNF: + fprintf(stderr, "option not found %c\n", argv[oint][optchr]); + break; + case OPTERRARG: + fprintf(stderr, "no argument for option %c\n", argv[oint][optchr]); + break; + default: + fprintf(stderr, "unknown\n"); + break; + } + } + return('?'); } - -int ap_php_getopt(int argc, char* const *argv, const char *optstr) -{ - static int optchr = 0; - static int dash = 0; /* have already seen the - */ - - char *cp; - if (ap_php_optind >= argc) - return(EOF); - if (!dash && (argv[ap_php_optind][0] != '-')) - return(EOF); - if (!dash && (argv[ap_php_optind][0] == '-') && !argv[ap_php_optind][1]) - { - /* - * use to specify stdin. Need to let pgm process this and - * the following args - */ - return(EOF); - } - if ((argv[ap_php_optind][0] == '-') && (argv[ap_php_optind][1] == '-')) - { - /* -- indicates end of args */ - ap_php_optind++; - return(EOF); - } - if (!dash) - { - assert((argv[ap_php_optind][0] == '-') && argv[ap_php_optind][1]); - dash = 1; - optchr = 1; - } - - /* Check if the guy tries to do a -: kind of flag */ - assert(dash); - if (argv[ap_php_optind][optchr] == ':') - { - dash = 0; - ap_php_optind++; - return(ap_php_optiserr(argc, argv, ap_php_optind-1, optstr, optchr, OPTERRCOLON)); - } - if (!(cp = strchr(optstr, argv[ap_php_optind][optchr]))) - { - int errind = ap_php_optind; - int errchr = optchr; - - if (!argv[ap_php_optind][optchr+1]) - { - dash = 0; - ap_php_optind++; - } - else - optchr++; - return(ap_php_optiserr(argc, argv, errind, optstr, errchr, OPTERRNF)); - } - if (cp[1] == ':') - { - /* Check for cases where the value of the argument - is in the form -<arg> <val> or in the form -<arg><val> */ - dash = 0; - if(!argv[ap_php_optind][2]) { - ap_php_optind++; - if (ap_php_optind == argc) - return(ap_php_optiserr(argc, argv, ap_php_optind-1, optstr, optchr, OPTERRARG)); - ap_php_optarg = argv[ap_php_optind++]; - } - else - { - ap_php_optarg = &argv[ap_php_optind][2]; - ap_php_optind++; - } - return(*cp); - } - else - { - if (!argv[ap_php_optind][optchr+1]) - { - dash = 0; - ap_php_optind++; - } - else - optchr++; - return(*cp); - } - assert(0); - return(0); /* never reached */ +int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **optarg, int *optind, int show_err) +{ + static int optchr = 0; + static int dash = 0; /* have already seen the - */ + int arg_start = 2; + + int opts_idx = -1; + + if (*optind >= argc) { + return(EOF); + } + if (!dash) { + if ((argv[*optind][0] != '-')) { + return(EOF); + } else { + if (!argv[*optind][1]) + { + /* + * use to specify stdin. Need to let pgm process this and + * the following args + */ + return(EOF); + } + } + } + if ((argv[*optind][0] == '-') && (argv[*optind][1] == '-')) { + /* '--' indicates end of args if not followed by a known long option name */ + while (1) { + opts_idx++; + if (opts[opts_idx].opt_char == '-') { + (*optind)++; + return(EOF); + } else if (opts[opts_idx].opt_name && !strcmp(&argv[*optind][2], opts[opts_idx].opt_name)) { + break; + } + } + optchr = 0; + dash = 1; + arg_start = 2 + strlen(opts[opts_idx].opt_name); + } + if (!dash) { + dash = 1; + optchr = 1; + } + + /* Check if the guy tries to do a -: kind of flag */ + if (argv[*optind][optchr] == ':') { + dash = 0; + (*optind)++; + return (php_opt_error(argc, argv, *optind-1, optchr, OPTERRCOLON, show_err)); + } + if (opts_idx < 0) { + while (1) { + opts_idx++; + if (opts[opts_idx].opt_char == '-') { + int errind = *optind; + int errchr = optchr; + + if (!argv[*optind][optchr+1]) { + dash = 0; + (*optind)++; + } else { + optchr++; + } + return(php_opt_error(argc, argv, errind, errchr, OPTERRNF, show_err)); + } else if (argv[*optind][optchr] == opts[opts_idx].opt_char) { + break; + } + } + } + if (opts[opts_idx].need_param) { + /* Check for cases where the value of the argument + is in the form -<arg> <val> or in the form -<arg><val> */ + dash = 0; + if(!argv[*optind][arg_start]) { + (*optind)++; + if (*optind == argc) { + return(php_opt_error(argc, argv, *optind-1, optchr, OPTERRARG, show_err)); + } + *optarg = argv[(*optind)++]; + } else { + *optarg = &argv[*optind][arg_start]; + (*optind)++; + } + return opts[opts_idx].opt_char; + } else { + if (arg_start == 2) { + if (!argv[*optind][optchr+1]) + { + dash = 0; + (*optind)++; + } else { + optchr++; + } + } else { + (*optind)++; + } + return opts[opts_idx].opt_char; + } + assert(0); + return(0); /* never reached */ } - -#ifdef TESTGETOPT -int - main (int argc, char **argv) - { - int c; - extern char *ap_php_optarg; - extern int ap_php_optind; - int aflg = 0; - int bflg = 0; - int errflg = 0; - char *ofile = NULL; - - while ((c = ap_php_getopt(argc, argv, "abo:")) != EOF) - switch (c) { - case 'a': - if (bflg) - errflg++; - else - aflg++; - break; - case 'b': - if (aflg) - errflg++; - else - bflg++; - break; - case 'o': - ofile = ap_php_optarg; - (void)printf("ofile = %s\n", ofile); - break; - case '?': - errflg++; - } - if (errflg) { - (void)fprintf(stderr, - "usage: cmd [-a|-b] [-o <filename>] files...\n"); - exit (2); - } - for ( ; ap_php_optind < argc; ap_php_optind++) - (void)printf("%s\n", argv[ap_php_optind]); - return 0; - } - -#endif /* TESTGETOPT */ Index: php4/sapi/cli/php_cli.c diff -u php4/sapi/cli/php_cli.c:1.51.2.23 php4/sapi/cli/php_cli.c:1.51.2.24 --- php4/sapi/cli/php_cli.c:1.51.2.23 Fri May 30 21:37:45 2003 +++ php4/sapi/cli/php_cli.c Fri May 30 22:20:08 2003 @@ -23,6 +23,7 @@ #include "php.h" #include "php_globals.h" #include "php_variables.h" +#include "zend_hash.h" #include "zend_modules.h" #include "SAPI.h" @@ -90,10 +91,33 @@ #define PHP_MODE_STRIP 5 #define PHP_MODE_CLI_DIRECT 6 -extern char *ap_php_optarg; -extern int ap_php_optind; +static char *optarg = NULL; +static int optind = 1; -#define OPTSTRING "aCc:d:ef:g:hilmnqr:sw?vz:" +static const opt_struct OPTIONS[] = { + {'a', 0, "interactive"}, + {'C', 0, "no-chdir"}, /* for compatibility with CGI (do not chdir to script directory) */ + {'c', 1, "php-ini"}, + {'d', 1, "define"}, + {'e', 0, "profile-info"}, + {'f', 1, "file"}, + {'g', 1, "global"}, + {'h', 0, "help"}, + {'i', 0, "info"}, + {'l', 0, "syntax-check"}, + {'m', 0, "modules"}, + {'n', 0, "no-php-ini"}, + {'q', 0, "no-header"}, /* for compatibility with CGI (do not generate HTTP headers) */ + {'H', 0, "hide-args"}, + {'r', 1, "run"}, + {'s', 0, "syntax-highlight"}, + {'s', 0, "syntax-highlighting"}, + {'w', 0, "strip"}, + {'?', 0, "usage"},/* help alias (both '?' and 'usage') */ + {'v', 0, "version"}, + {'z', 1, "zend-extension"}, + {'-', 0, NULL} /* end of args */ +}; static int print_module_info(zend_module_entry *module, void *arg TSRMLS_DC) { @@ -432,6 +456,40 @@ FREE_ZVAL(zerr); } +/* {{{ cli_seek_file_begin + */ +static int cli_seek_file_begin(zend_file_handle *file_handle, char *script_file, int *lineno TSRMLS_DC) +{ + int c; + + *lineno = 1; + + if (!(file_handle->handle.fp = VCWD_FOPEN(script_file, "rb"))) { + php_printf("Could not open input file: %s.\n", script_file); + return FAILURE; + } + file_handle->filename = script_file; + /* #!php support */ + c = fgetc(file_handle->handle.fp); + if (c == '#') { + while (c != 10 && c != 13) { + c = fgetc(file_handle->handle.fp); /* skip to end of line */ + } + /* handle situations where line is terminated by \r\n */ + if (c == 13) { + if (fgetc(file_handle->handle.fp) != 10) { + long pos = ftell(file_handle->handle.fp); + fseek(file_handle->handle.fp, pos - 1, SEEK_SET); + } + } + *lineno = -2; + } else { + rewind(file_handle->handle.fp); + } + return SUCCESS; +} +/* }}} */ + /* {{{ main */ int main(int argc, char *argv[]) @@ -441,15 +499,17 @@ zend_file_handle file_handle; /* temporary locals */ int behavior=PHP_MODE_STANDARD; - int orig_optind=ap_php_optind; - char *orig_optarg=ap_php_optarg; + int orig_optind=optind; + char *orig_optarg=optarg; char *arg_free=NULL, **arg_excp=&arg_free; char *script_file=NULL; zend_llist global_vars; - int interactive=0, is_hashbang=0; + int interactive=0; int module_started = 0; + int lineno = 0; char *exec_direct=NULL; char *param_error=NULL; + int hide_argv = 0; /* end of temporary locals */ #ifdef ZTS zend_compiler_globals *compiler_globals; @@ -488,18 +548,18 @@ #endif - while ((c=ap_php_getopt(argc, argv, OPTSTRING))!=-1) { + while ((c = php_getopt(argc, argv, OPTIONS, &optarg, &optind, 0))!=-1) { switch (c) { case 'c': - cli_sapi_module.php_ini_path_override = strdup(ap_php_optarg); + cli_sapi_module.php_ini_path_override = strdup(optarg); break; case 'n': cli_sapi_module.php_ini_ignore = 1; break; } } - ap_php_optind = orig_optind; - ap_php_optarg = orig_optarg; + optind = orig_optind; + optarg = orig_optarg; cli_sapi_module.executable_location = argv[0]; @@ -524,21 +584,65 @@ module_started = 1; zend_first_try { - while ((c=ap_php_getopt(argc, argv, OPTSTRING))!=-1) { + zend_llist_init(&global_vars, sizeof(char *), NULL, 0); + + zend_uv.html_errors = 0; /* tell the engine we're in non-html mode */ + CG(in_compilation) = 0; /* not initialized but needed for several options */ + EG(uninitialized_zval_ptr) = NULL; + + if (cli_sapi_module.php_ini_path_override && cli_sapi_module.php_ini_ignore) { + PUTS("You cannot use both -n and -c switch. Use -h for help.\n"); + exit_status=1; + goto out_err; + } + + while ((c = php_getopt(argc, argv, OPTIONS, &optarg, &optind, 0)) != -1) { switch (c) { + + case 'h': /* help & quit */ case '?': php_output_startup(); php_output_activate(TSRMLS_C); php_cli_usage(argv[0]); php_end_ob_buffers(1 TSRMLS_CC); - exit(1); + exit_status=1; + goto out_err; + + + case 'i': /* php info & quit */ + if (php_request_startup(TSRMLS_C)==FAILURE) { + goto err; + } + php_print_info(0xFFFFFFFF TSRMLS_CC); + php_end_ob_buffers(1 TSRMLS_CC); + exit_status=1; + goto out; + + case 'm': /* list compiled in modules */ + php_output_startup(); + php_output_activate(TSRMLS_C); + php_printf("[PHP Modules]\n"); + print_modules(TSRMLS_C); + php_printf("\n[Zend Modules]\n"); + print_extensions(TSRMLS_C); + php_printf("\n"); + php_end_ob_buffers(1 TSRMLS_CC); + exit_status=1; + goto out_err; + + case 'v': /* show php version & quit */ + if (php_request_startup(TSRMLS_C)==FAILURE) { + goto err; + } + php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2003 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version()); + php_end_ob_buffers(1 TSRMLS_CC); + exit_status=1; + goto out; + + default: break; } } - ap_php_optind = orig_optind; - ap_php_optarg = orig_optarg; - - zend_llist_init(&global_vars, sizeof(char *), NULL, 0); /* Set some CLI defaults */ SG(options) |= SAPI_OPTION_NO_CHDIR; @@ -548,17 +652,9 @@ INI_HARDCODED("implicit_flush", "1"); INI_HARDCODED("max_execution_time", "0"); - zend_uv.html_errors = 0; /* tell the engine we're in non-html mode */ - CG(in_compilation) = 0; /* not initialized but needed for several options */ - EG(uninitialized_zval_ptr) = NULL; - - if (cli_sapi_module.php_ini_path_override && cli_sapi_module.php_ini_ignore) { - PUTS("You cannot use both -n and -c switch. Use -h for help.\n"); - exit_status=1; - goto out_err; - } - - while ((c = ap_php_getopt(argc, argv, OPTSTRING)) != -1) { + optind = orig_optind; + optarg = orig_optarg; + while ((c = php_getopt(argc, argv, OPTIONS, &optarg, &optind, 0)) != -1) { switch (c) { case 'a': /* interactive mode */ @@ -570,7 +666,7 @@ /* This is default so NOP */ break; case 'd': /* define ini entries on command line */ - define_command_line_ini_entry(ap_php_optarg); + define_command_line_ini_entry(optarg); break; case 'e': /* enable extended info output */ @@ -582,36 +678,17 @@ param_error = "Either execute direct code or use a file.\n"; break; } - script_file = ap_php_optarg; + script_file = optarg; break; case 'g': /* define global variables on command line */ { - char *arg = estrdup(ap_php_optarg); + char *arg = estrdup(optarg); zend_llist_add_element(&global_vars, &arg); } break; - case 'h': /* help & quit */ - case '?': - php_output_startup(); - php_output_activate(TSRMLS_C); - php_cli_usage(argv[0]); - php_end_ob_buffers(1 TSRMLS_CC); - exit_status=1; - zend_ini_deactivate(TSRMLS_C); - goto out_err; - - case 'i': /* php info & quit */ - if (php_request_startup(TSRMLS_C)==FAILURE) { - goto err; - } - php_print_info(0xFFFFFFFF TSRMLS_CC); - php_end_ob_buffers(1 TSRMLS_CC); - exit_status=1; - goto out; - case 'l': /* syntax check mode */ if (behavior != PHP_MODE_STANDARD) { break; @@ -619,19 +696,6 @@ behavior=PHP_MODE_LINT; break; - case 'm': /* list compiled in modules */ - php_output_startup(); - php_output_activate(TSRMLS_C); - php_printf("[PHP Modules]\n"); - print_modules(TSRMLS_C); - php_printf("\n[Zend Modules]\n"); - print_extensions(TSRMLS_C); - php_printf("\n"); - php_end_ob_buffers(1 TSRMLS_CC); - exit_status=1; - zend_ini_deactivate(TSRMLS_C); - goto out_err; - #if 0 /* not yet operational, see also below ... */ case '': /* generate indented source mode*/ if (behavior == PHP_MODE_CLI_DIRECT) { @@ -652,7 +716,7 @@ break; } behavior=PHP_MODE_CLI_DIRECT; - exec_direct=ap_php_optarg; + exec_direct=optarg; break; case 's': /* generate highlighted HTML from source */ @@ -663,21 +727,6 @@ behavior=PHP_MODE_HIGHLIGHT; break; - case 'v': /* show php version & quit */ - if (php_request_startup(TSRMLS_C)==FAILURE) { - goto err; - } - -#if ZEND_DEBUG - php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2003 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version()); -#else - php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2003 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version()); -#endif - php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2003 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version()); - php_end_ob_buffers(1 TSRMLS_CC); - exit_status=1; - goto out; - case 'w': if (behavior == PHP_MODE_CLI_DIRECT) { param_error = "Source stripping only works for files.\n"; @@ -687,7 +736,10 @@ break; case 'z': /* load extension file */ - zend_load_extension(ap_php_optarg); + zend_load_extension(optarg); + break; + case 'H': + hide_argv = 1; break; default: @@ -704,34 +756,16 @@ CG(interactive) = interactive; /* only set script_file if not set already and not in direct mode and not at end of parameter list */ - if (argc > ap_php_optind && !script_file && behavior!=PHP_MODE_CLI_DIRECT && strcmp(argv[ap_php_optind-1],"--")) { - script_file=argv[ap_php_optind]; - ap_php_optind++; + if (argc > optind && !script_file && behavior!=PHP_MODE_CLI_DIRECT && strcmp(argv[optind-1],"--")) { + script_file=argv[optind]; + optind++; } if (script_file) { - if (!(file_handle.handle.fp = VCWD_FOPEN(script_file, "rb"))) { - php_printf("Could not open input file: %s.\n", script_file); + if (cli_seek_file_begin(&file_handle, script_file, &lineno TSRMLS_CC) != SUCCESS) { goto err; } file_handle.filename = script_file; script_filename = script_file; - /* #!php support */ - c = fgetc(file_handle.handle.fp); - if (c == '#') { - while (c != 10 && c != 13) { - c = fgetc(file_handle.handle.fp); /* skip to end of line */ - } - /* handle situations where line is terminated by \r\n */ - if (c == 13) { - if (fgetc(file_handle.handle.fp) != 10) { - long pos = ftell(file_handle.handle.fp); - fseek(file_handle.handle.fp, pos - 1, SEEK_SET); - } - } - is_hashbang = 1; - } else { - rewind(file_handle.handle.fp); - } } else { file_handle.filename = "-"; file_handle.handle.fp = stdin; @@ -741,14 +775,14 @@ file_handle.free_filename = 0; php_self = file_handle.filename; - /* before registering argv to modulule exchange the *new* argv[0] */ + /* before registering argv to module exchange the *new* argv[0] */ /* we can achieve this without allocating more memory */ - SG(request_info).argc=argc-ap_php_optind+1; - arg_excp = argv+ap_php_optind-1; - arg_free = argv[ap_php_optind-1]; + SG(request_info).argc=argc-optind+1; + arg_excp = argv+optind-1; + arg_free = argv[optind-1]; SG(request_info).path_translated = file_handle.filename; - argv[ap_php_optind-1] = file_handle.filename; - SG(request_info).argv=argv+ap_php_optind-1; + argv[optind-1] = file_handle.filename; + SG(request_info).argv=argv+optind-1; if (php_request_startup(TSRMLS_C)==FAILURE) { *arg_excp = arg_free; @@ -757,14 +791,16 @@ PUTS("Could not startup.\n"); goto err; } + CG(zend_lineno) = lineno; + *arg_excp = arg_free; /* reconstuct argv */ - /* Correct line numbers when #!php is used. This is reset in php_request_startup(). */ - if (is_hashbang) { - CG(zend_lineno) = -2; + if (hide_argv) { + int i; + for (i = 1; i < argc; i++) { + memset(argv[i], 0, strlen(argv[i])); + } } - *arg_excp = arg_free; /* reconstuct argv */ - /* This actually destructs the elements of the list - ugly hack */ zend_llist_apply(&global_vars, (llist_apply_func_t) php_register_command_line_global_vars TSRMLS_CC); zend_llist_destroy(&global_vars); @@ -858,3 +894,8 @@ * vim600: sw=4 ts=4 fdm=marker * vim<600: sw=4 ts=4 */ + + + + + Index: php4/sapi/cli/php_getopt.h diff -u php4/sapi/cli/php_getopt.h:1.1.8.2 php4/sapi/cli/php_getopt.h:1.1.8.3 --- php4/sapi/cli/php_getopt.h:1.1.8.2 Fri May 30 21:37:45 2003 +++ php4/sapi/cli/php_getopt.h Fri May 30 22:20:08 2003 @@ -16,13 +16,17 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_getopt.h,v 1.1.8.2 2003/05/31 01:37:45 sniper Exp $ */ +/* $Id: php_getopt.h,v 1.1.8.3 2003/05/31 02:20:08 helly Exp $ */ #include "php.h" -extern char *ap_php_optarg; -extern int ap_php_optind; - -int ap_php_getopt(int argc, char* const *argv, const char *optstr); - - \ No newline at end of file +/* Define structure for one recognized option (both single char and long name). + * If short_open is '-' this is the last option. + */ +typedef struct _opt_struct { + const char opt_char; + const int need_param; + const char * opt_name; +} opt_struct; + +int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **optarg, int *optind, int show_err); Index: php4/sapi/cgi/cgi_main.c diff -u php4/sapi/cgi/cgi_main.c:1.190.2.35 php4/sapi/cgi/cgi_main.c:1.190.2.36 --- php4/sapi/cgi/cgi_main.c:1.190.2.35 Fri May 30 21:37:44 2003 +++ php4/sapi/cgi/cgi_main.c Fri May 30 22:20:08 2003 @@ -20,7 +20,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: cgi_main.c,v 1.190.2.35 2003/05/31 01:37:44 sniper Exp $ */ +/* $Id: cgi_main.c,v 1.190.2.36 2003/05/31 02:20:08 helly Exp $ */ #include "php.h" #include "php_globals.h" @@ -117,8 +117,31 @@ #define PHP_MODE_LINT 4 #define PHP_MODE_STRIP 5 -extern char *ap_php_optarg; -extern int ap_php_optind; +static char *optarg = NULL; +static int optind = 1; + +static const opt_struct OPTIONS[] = { + {'a', 0, "interactive"}, + {'C', 0, "no-chdir"}, + {'c', 1, "php-ini"}, + {'d', 1, "define"}, + {'e', 0, "profile-info"}, + {'f', 1, "file"}, + {'g', 1, "global"}, + {'h', 0, "help"}, + {'i', 0, "info"}, + {'l', 0, "syntax-check"}, + {'m', 0, "modules"}, + {'n', 0, "no-php-ini"}, + {'q', 0, "no-header"}, + {'s', 0, "syntax-highlight"}, + {'s', 0, "syntax-highlighting"}, + {'w', 0, "strip"}, + {'?', 0, "usage"},/* help alias (both '?' and 'usage') */ + {'v', 0, "version"}, + {'z', 1, "zend-extension"}, + {'-', 0, NULL} /* end of args */ +}; #if ENABLE_PATHINFO_CHECK /* true global. this is retreived once only, even for fastcgi */ @@ -138,8 +161,6 @@ #define TRANSLATE_SLASHES(path) #endif -#define OPTSTRING "ab:Cc:d:ef:g:hilmnqsw?vz:" - static int print_module_info(zend_module_entry *module, void *arg TSRMLS_DC) { php_printf("%s\n", module->name); @@ -403,7 +424,6 @@ return getenv(name); } - static char *sapi_cgi_read_cookies(TSRMLS_D) { return sapi_cgibin_getenv((char *)"HTTP_COOKIE",0 TSRMLS_CC); @@ -543,7 +563,6 @@ } /* }}} */ - /* {{{ init_request_info initializes request_info structure @@ -702,7 +721,7 @@ * of it by stat'ing back through the '/' * this fixes url's like /info.php/test */ - if (stat( script_path_translated, &st ) == -1 ) { + if (script_path_translated && stat( script_path_translated, &st ) == -1 ) { char *pt = estrdup(script_path_translated); int len = strlen(pt); char *ptr; @@ -897,8 +916,8 @@ /* temporary locals */ int behavior=PHP_MODE_STANDARD; int no_headers=0; - int orig_optind=ap_php_optind; - char *orig_optarg=ap_php_optarg; + int orig_optind=optind; + char *orig_optarg=optarg; char *script_file=NULL; zend_llist global_vars; int interactive=0; @@ -974,10 +993,10 @@ /* allow ini override for fastcgi */ #endif ) { - while ((c=ap_php_getopt(argc, argv, OPTSTRING))!=-1) { + while ((c=php_getopt(argc, argv, OPTIONS, &optarg, &optind, 0))!=-1) { switch (c) { case 'c': - cgi_sapi_module.php_ini_path_override = strdup(ap_php_optarg); + cgi_sapi_module.php_ini_path_override = strdup(optarg); break; case 'n': cgi_sapi_module.php_ini_ignore = 1; @@ -988,15 +1007,15 @@ server by accepting a bindpath parameter. */ case 'b': if (!fastcgi) { - bindpath = strdup(ap_php_optarg); + bindpath = strdup(optarg); } break; #endif } } - ap_php_optind = orig_optind; - ap_php_optarg = orig_optarg; + optind = orig_optind; + optarg = orig_optarg; } #ifdef ZTS @@ -1005,6 +1024,7 @@ core_globals = ts_resource(core_globals_id); sapi_globals = ts_resource(sapi_globals_id); tsrm_ls = ts_resource(0); + SG(request_info).path_translated = NULL; #endif cgi_sapi_module.executable_location = argv[0]; @@ -1190,8 +1210,9 @@ && !fastcgi #endif ) { - while ((c=ap_php_getopt(argc, argv, OPTSTRING))!=-1) { + while ((c=php_getopt(argc, argv, OPTIONS, &optarg, &optind, 1))!=-1) { switch (c) { + case 'h': case '?': no_headers = 1; php_output_startup(); @@ -1203,8 +1224,8 @@ break; } } - ap_php_optind = orig_optind; - ap_php_optarg = orig_optarg; + optind = orig_optind; + optarg = orig_optarg; } #if PHP_FASTCGI @@ -1254,7 +1275,7 @@ exit(1); } - while ((c = ap_php_getopt(argc, argv, OPTSTRING)) != -1) { + while ((c = php_getopt(argc, argv, OPTIONS, &optarg, &optind, 0)) != -1) { switch (c) { case 'a': /* interactive mode */ @@ -1266,7 +1287,7 @@ SG(options) |= SAPI_OPTION_NO_CHDIR; break; case 'd': /* define ini entries on command line */ - define_command_line_ini_entry(ap_php_optarg); + define_command_line_ini_entry(optarg); break; case 'e': /* enable extended info output */ @@ -1274,32 +1295,21 @@ break; case 'f': /* parse file */ - script_file = estrdup(ap_php_optarg); + script_file = estrdup(optarg); no_headers = 1; /* arguments after the file are considered script args */ - SG(request_info).argc = argc - (ap_php_optind-1); - SG(request_info).argv = &argv[ap_php_optind-1]; + SG(request_info).argc = argc - (optind-1); + SG(request_info).argv = &argv[optind-1]; break; case 'g': /* define global variables on command line */ { - char *arg = estrdup(ap_php_optarg); + char *arg = estrdup(optarg); zend_llist_add_element(&global_vars, &arg); } break; - case 'h': /* help & quit */ - case '?': - no_headers = 1; - php_output_startup(); - php_output_activate(TSRMLS_C); - SG(headers_sent) = 1; - php_cgi_usage(argv[0]); - php_end_ob_buffers(1 TSRMLS_CC); - exit(1); - break; - case 'i': /* php info & quit */ if (php_request_startup(TSRMLS_C) == FAILURE) { php_module_shutdown(TSRMLS_C); @@ -1370,7 +1380,7 @@ break; case 'z': /* load extension file */ - zend_load_extension(ap_php_optarg); + zend_load_extension(optarg); break; default: @@ -1388,12 +1398,12 @@ SG(request_info).no_headers = 1; } - if (!SG(request_info).path_translated && argc > ap_php_optind) { + if (!SG(request_info).path_translated && argc > optind) { /* arguments after the file are considered script args */ - SG(request_info).argc = argc - ap_php_optind; - SG(request_info).argv = &argv[ap_php_optind]; + SG(request_info).argc = argc - optind; + SG(request_info).argv = &argv[optind]; /* file is on command line, but not in -f opt */ - SG(request_info).path_translated = estrdup(argv[ap_php_optind++]); + SG(request_info).path_translated = estrdup(argv[optind++]); } /* all remaining arguments are part of the query string @@ -1405,15 +1415,15 @@ test.php "v1=test&v2=hello world!" test.php v1=test "v2=hello world!" */ - if (!SG(request_info).query_string && argc > ap_php_optind) { + if (!SG(request_info).query_string && argc > optind) { len = 0; - for (i = ap_php_optind; i < argc; i++) { + for (i = optind; i < argc; i++) { len += strlen(argv[i]) + 1; } s = malloc(len + 1); /* leak - but only for command line version, so ok */ *s = '\0'; /* we are pretending it came from the environment */ - for (i = ap_php_optind, len = 0; i < argc; i++) { + for (i = optind, len = 0; i < argc; i++) { strcat(s, argv[i]); if (i < (argc - 1)) { strcat(s, "&"); @@ -1432,7 +1442,8 @@ #if PHP_FASTCGI || fastcgi #endif - ) { + ) + { file_handle.type = ZEND_HANDLE_FILENAME; file_handle.filename = SG(request_info).path_translated; file_handle.handle.fp = NULL; Index: php4/sapi/cgi/getopt.c diff -u php4/sapi/cgi/getopt.c:1.5.8.2 php4/sapi/cgi/getopt.c:1.5.8.3 --- php4/sapi/cgi/getopt.c:1.5.8.2 Fri May 30 21:37:44 2003 +++ php4/sapi/cgi/getopt.c Fri May 30 22:20:08 2003 @@ -1,4 +1,20 @@ -/* Borrowed from Apache NT Port */ +/* + +----------------------------------------------------------------------+ + | PHP Version 4 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2003 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 2.02 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available at through the world-wide-web at | + | http://www.php.net/license/2_02.txt. | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | [EMAIL PROTECTED] so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Marcus Boerger <[EMAIL PROTECTED]> | + +----------------------------------------------------------------------+ +*/ #include <stdio.h> #include <string.h> @@ -10,166 +26,129 @@ #define OPTERRARG (3) -char *ap_php_optarg; -int ap_php_optind = 1; -static int ap_php_opterr = 1; -static int ap_php_optopt; - -static int -ap_php_optiserr(int argc, char * const *argv, int oint, const char *optstr, - int optchr, int err) +static int php_opt_error(int argc, char * const *argv, int oint, int optchr, int err, int show_err) { - if (ap_php_opterr) - { - fprintf(stderr, "Error in argument %d, char %d: ", oint, optchr+1); - switch(err) - { - case OPTERRCOLON: - fprintf(stderr, ": in flags\n"); - break; - case OPTERRNF: - fprintf(stderr, "option not found %c\n", argv[oint][optchr]); - break; - case OPTERRARG: - fprintf(stderr, "no argument for option %c\n", argv[oint][optchr]); - break; - default: - fprintf(stderr, "unknown\n"); - break; - } - } - ap_php_optopt = argv[oint][optchr]; - return('?'); + if (show_err) + { + fprintf(stderr, "Error in argument %d, char %d: ", oint, optchr+1); + switch(err) + { + case OPTERRCOLON: + fprintf(stderr, ": in flags\n"); + break; + case OPTERRNF: + fprintf(stderr, "option not found %c\n", argv[oint][optchr]); + break; + case OPTERRARG: + fprintf(stderr, "no argument for option %c\n", argv[oint][optchr]); + break; + default: + fprintf(stderr, "unknown\n"); + break; + } + } + return('?'); } - -int ap_php_getopt(int argc, char* const *argv, const char *optstr) -{ - static int optchr = 0; - static int dash = 0; /* have already seen the - */ - - char *cp; - if (ap_php_optind >= argc) - return(EOF); - if (!dash && (argv[ap_php_optind][0] != '-')) - return(EOF); - if (!dash && (argv[ap_php_optind][0] == '-') && !argv[ap_php_optind][1]) - { - /* - * use to specify stdin. Need to let pgm process this and - * the following args - */ - return(EOF); - } - if ((argv[ap_php_optind][0] == '-') && (argv[ap_php_optind][1] == '-')) - { - /* -- indicates end of args */ - ap_php_optind++; - return(EOF); - } - if (!dash) - { - assert((argv[ap_php_optind][0] == '-') && argv[ap_php_optind][1]); - dash = 1; - optchr = 1; - } - - /* Check if the guy tries to do a -: kind of flag */ - assert(dash); - if (argv[ap_php_optind][optchr] == ':') - { - dash = 0; - ap_php_optind++; - return(ap_php_optiserr(argc, argv, ap_php_optind-1, optstr, optchr, OPTERRCOLON)); - } - if (!(cp = strchr(optstr, argv[ap_php_optind][optchr]))) - { - int errind = ap_php_optind; - int errchr = optchr; - - if (!argv[ap_php_optind][optchr+1]) - { - dash = 0; - ap_php_optind++; - } - else - optchr++; - return(ap_php_optiserr(argc, argv, errind, optstr, errchr, OPTERRNF)); - } - if (cp[1] == ':') - { - /* Check for cases where the value of the argument - is in the form -<arg> <val> or in the form -<arg><val> */ - dash = 0; - if(!argv[ap_php_optind][2]) { - ap_php_optind++; - if (ap_php_optind == argc) - return(ap_php_optiserr(argc, argv, ap_php_optind-1, optstr, optchr, OPTERRARG)); - ap_php_optarg = argv[ap_php_optind++]; - } - else - { - ap_php_optarg = &argv[ap_php_optind][2]; - ap_php_optind++; - } - return(*cp); - } - else - { - if (!argv[ap_php_optind][optchr+1]) - { - dash = 0; - ap_php_optind++; - } - else - optchr++; - return(*cp); - } - assert(0); - return(0); /* never reached */ +int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **optarg, int *optind, int show_err) +{ + static int optchr = 0; + static int dash = 0; /* have already seen the - */ + int arg_start = 2; + + int opts_idx = -1; + + if (*optind >= argc) { + return(EOF); + } + if (!dash) { + if ((argv[*optind][0] != '-')) { + return(EOF); + } else { + if (!argv[*optind][1]) + { + /* + * use to specify stdin. Need to let pgm process this and + * the following args + */ + return(EOF); + } + } + } + if ((argv[*optind][0] == '-') && (argv[*optind][1] == '-')) { + /* '--' indicates end of args if not followed by a known long option name */ + while (1) { + opts_idx++; + if (opts[opts_idx].opt_char == '-') { + (*optind)++; + return(EOF); + } else if (opts[opts_idx].opt_name && !strcmp(&argv[*optind][2], opts[opts_idx].opt_name)) { + break; + } + } + optchr = 0; + dash = 1; + arg_start = 2 + strlen(opts[opts_idx].opt_name); + } + if (!dash) { + dash = 1; + optchr = 1; + } + + /* Check if the guy tries to do a -: kind of flag */ + if (argv[*optind][optchr] == ':') { + dash = 0; + (*optind)++; + return (php_opt_error(argc, argv, *optind-1, optchr, OPTERRCOLON, show_err)); + } + if (opts_idx < 0) { + while (1) { + opts_idx++; + if (opts[opts_idx].opt_char == '-') { + int errind = *optind; + int errchr = optchr; + + if (!argv[*optind][optchr+1]) { + dash = 0; + (*optind)++; + } else { + optchr++; + } + return(php_opt_error(argc, argv, errind, errchr, OPTERRNF, show_err)); + } else if (argv[*optind][optchr] == opts[opts_idx].opt_char) { + break; + } + } + } + if (opts[opts_idx].need_param) { + /* Check for cases where the value of the argument + is in the form -<arg> <val> or in the form -<arg><val> */ + dash = 0; + if(!argv[*optind][arg_start]) { + (*optind)++; + if (*optind == argc) { + return(php_opt_error(argc, argv, *optind-1, optchr, OPTERRARG, show_err)); + } + *optarg = argv[(*optind)++]; + } else { + *optarg = &argv[*optind][arg_start]; + (*optind)++; + } + return opts[opts_idx].opt_char; + } else { + if (arg_start == 2) { + if (!argv[*optind][optchr+1]) + { + dash = 0; + (*optind)++; + } else { + optchr++; + } + } else { + (*optind)++; + } + return opts[opts_idx].opt_char; + } + assert(0); + return(0); /* never reached */ } - -#ifdef TESTGETOPT -int - main (int argc, char **argv) - { - int c; - extern char *ap_php_optarg; - extern int ap_php_optind; - int aflg = 0; - int bflg = 0; - int errflg = 0; - char *ofile = NULL; - - while ((c = ap_php_getopt(argc, argv, "abo:")) != EOF) - switch (c) { - case 'a': - if (bflg) - errflg++; - else - aflg++; - break; - case 'b': - if (aflg) - errflg++; - else - bflg++; - break; - case 'o': - ofile = ap_php_optarg; - (void)printf("ofile = %s\n", ofile); - break; - case '?': - errflg++; - } - if (errflg) { - (void)fprintf(stderr, - "usage: cmd [-a|-b] [-o <filename>] files...\n"); - exit (2); - } - for ( ; ap_php_optind < argc; ap_php_optind++) - (void)printf("%s\n", argv[ap_php_optind]); - return 0; - } - -#endif /* TESTGETOPT */ Index: php4/sapi/cgi/php_getopt.h diff -u php4/sapi/cgi/php_getopt.h:1.3.14.2 php4/sapi/cgi/php_getopt.h:1.3.14.3 --- php4/sapi/cgi/php_getopt.h:1.3.14.2 Fri May 30 21:37:44 2003 +++ php4/sapi/cgi/php_getopt.h Fri May 30 22:20:08 2003 @@ -1,7 +1,30 @@ -/* Borrowed from Apache NT Port */ +/* + +----------------------------------------------------------------------+ + | PHP Version 4 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2003 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 2.02 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available at through the world-wide-web at | + | http://www.php.net/license/2_02.txt. | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | [EMAIL PROTECTED] so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Marcus Boerger <[EMAIL PROTECTED]> | + +----------------------------------------------------------------------+ +*/ + #include "php.h" -extern char *ap_php_optarg; -extern int ap_php_optind; +/* Define structure for one recognized option (both single char and long name). + * If short_open is '-' this is the last option. + */ +typedef struct _opt_struct { + const char opt_char; + const int need_param; + const char * opt_name; +} opt_struct; -int ap_php_getopt(int argc, char* const *argv, const char *optstr); +int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **optarg, int *optind, int show_err);
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php