summaryrefslogtreecommitdiff
path: root/sapi/cgi
diff options
context:
space:
mode:
authorMarcus Boerger <helly@php.net>2003-02-16 01:23:11 +0000
committerMarcus Boerger <helly@php.net>2003-02-16 01:23:11 +0000
commitd4fd1c07b1dfac42061d96c3a7aae9f0b91d218d (patch)
treee8986edb10020c657cf7c692af02b5be53d13bd6 /sapi/cgi
parent824baa0384e301712ff222a60b0049a06b0bdbd4 (diff)
downloadphp-git-d4fd1c07b1dfac42061d96c3a7aae9f0b91d218d.tar.gz
- Allow long option names
- Update CLI's manpage @Added support for long options in CLI & CGI (e.g. --version). (Marcus) # In contrast to the preliminary patch this should work now completely. # If all long option names are accepted we may even think about MFHing.
Diffstat (limited to 'sapi/cgi')
-rw-r--r--sapi/cgi/cgi_main.c83
-rw-r--r--sapi/cgi/getopt.c293
-rw-r--r--sapi/cgi/php_getopt.h31
3 files changed, 210 insertions, 197 deletions
diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c
index 593b7b87d5..207d8e2949 100644
--- a/sapi/cgi/cgi_main.c
+++ b/sapi/cgi/cgi_main.c
@@ -117,8 +117,31 @@ static pid_t pgroup;
#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 @@ long fix_pathinfo=1;
#define TRANSLATE_SLASHES(path)
#endif
-#define OPTSTRING "abCc:d:ef:g:hilmnqsw?vz:"
-
static int print_module_info(zend_module_entry *module, void *arg TSRMLS_DC)
{
php_printf("%s\n", module->name);
@@ -515,7 +536,7 @@ static void php_cgi_usage(char *argv0)
prog = "php";
}
- php_printf("Usage: %s [-q] [-h] [-s [-v] [-i] [-f <file>] \n"
+ php_printf("Usage: %s [-q] [-h] [-s] [-v] [-i] [-f <file>]\n"
" %s <file> [args...]\n"
" -a Run interactively\n"
#if PHP_FASTCGI
@@ -881,8 +902,8 @@ int main(int argc, char *argv[])
/* 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;
@@ -958,10 +979,10 @@ int main(int argc, char *argv[])
/* 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;
@@ -971,14 +992,14 @@ int main(int argc, char *argv[])
we are being started as an 'external' fastcgi
server by accepting a bindpath parameter. */
case 'b':
- if (!fastcgi) bindpath= strdup(ap_php_optarg);
+ if (!fastcgi) bindpath= strdup(optarg);
break;
#endif
}
}
- ap_php_optind = orig_optind;
- ap_php_optarg = orig_optarg;
+ optind = orig_optind;
+ optarg = orig_optarg;
}
#ifdef ZTS
@@ -1172,8 +1193,9 @@ consult the installation file that came with this distribution, or visit \n\
&& !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();
@@ -1185,8 +1207,8 @@ consult the installation file that came with this distribution, or visit \n\
break;
}
}
- ap_php_optind = orig_optind;
- ap_php_optarg = orig_optarg;
+ optind = orig_optind;
+ optarg = orig_optarg;
}
#if PHP_FASTCGI
@@ -1236,7 +1258,7 @@ consult the installation file that came with this distribution, or visit \n\
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 */
@@ -1248,7 +1270,7 @@ consult the installation file that came with this distribution, or visit \n\
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 */
@@ -1256,29 +1278,18 @@ consult the installation file that came with this distribution, or visit \n\
break;
case 'f': /* parse file */
- script_file = estrdup(ap_php_optarg);
+ script_file = estrdup(optarg);
no_headers = 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);
@@ -1345,7 +1356,7 @@ consult the installation file that came with this distribution, or visit \n\
break;
case 'z': /* load extension file */
- zend_load_extension(ap_php_optarg);
+ zend_load_extension(optarg);
break;
default:
@@ -1358,7 +1369,7 @@ consult the installation file that came with this distribution, or visit \n\
if (script_file) {
len += strlen(script_file) + 1;
}
- for (i = ap_php_optind; i < argc; i++) {
+ for (i = optind; i < argc; i++) {
len += strlen(argv[i]) + 1;
}
@@ -1366,11 +1377,11 @@ consult the installation file that came with this distribution, or visit \n\
*s = '\0'; /* we are pretending it came from the environment */
if (script_file) {
strcpy(s, script_file);
- if (ap_php_optind<argc) {
+ if (optind<argc) {
strcat(s, "+");
}
}
- 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, "+");
@@ -1389,9 +1400,9 @@ consult the installation file that came with this distribution, or visit \n\
SG(request_info).no_headers = 1;
}
- if (!SG(request_info).path_translated && argc > ap_php_optind) {
+ if (!SG(request_info).path_translated && argc > 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]);
}
} /* end !cgi && !fastcgi */
diff --git a/sapi/cgi/getopt.c b/sapi/cgi/getopt.c
index a26ca3c431..e048e95058 100644
--- a/sapi/cgi/getopt.c
+++ b/sapi/cgi/getopt.c
@@ -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 |
+ | license@php.net so we can mail you a copy immediately. |
+ +----------------------------------------------------------------------+
+ | Author: Marcus Boerger <helly@php.net> |
+ +----------------------------------------------------------------------+
+*/
#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;
+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;
- 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;
- }
+ int opts_idx = -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 (*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;
+ }
- 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 */
+ /* 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 */
diff --git a/sapi/cgi/php_getopt.h b/sapi/cgi/php_getopt.h
index 40da432b59..1e2355ff63 100644
--- a/sapi/cgi/php_getopt.h
+++ b/sapi/cgi/php_getopt.h
@@ -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 |
+ | license@php.net so we can mail you a copy immediately. |
+ +----------------------------------------------------------------------+
+ | Author: Marcus Boerger <helly@php.net> |
+ +----------------------------------------------------------------------+
+*/
+
#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);