Skip to content

Commit d4fd1c0

Browse files
committed
- 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.
1 parent 824baa0 commit d4fd1c0

File tree

7 files changed

+540
-424
lines changed

7 files changed

+540
-424
lines changed

sapi/cgi/cgi_main.c

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,31 @@ static pid_t pgroup;
117117
#define PHP_MODE_LINT 4
118118
#define PHP_MODE_STRIP 5
119119

120-
extern char *ap_php_optarg;
121-
extern int ap_php_optind;
120+
static char *optarg = NULL;
121+
static int optind = 1;
122+
123+
static const opt_struct OPTIONS[] = {
124+
{'a', 0, "interactive"},
125+
{'C', 0, "no-chdir"},
126+
{'c', 1, "php-ini"},
127+
{'d', 1, "define"},
128+
{'e', 0, "profile-info"},
129+
{'f', 1, "file"},
130+
{'g', 1, "global"},
131+
{'h', 0, "help"},
132+
{'i', 0, "info"},
133+
{'l', 0, "syntax-check"},
134+
{'m', 0, "modules"},
135+
{'n', 0, "no-php-ini"},
136+
{'q', 0, "no-header"},
137+
{'s', 0, "syntax-highlight"},
138+
{'s', 0, "syntax-highlighting"},
139+
{'w', 0, "strip"},
140+
{'?', 0, "usage"},/* help alias (both '?' and 'usage') */
141+
{'v', 0, "version"},
142+
{'z', 1, "zend-extension"},
143+
{'-', 0, NULL} /* end of args */
144+
};
122145

123146
#if ENABLE_PATHINFO_CHECK
124147
/* true global. this is retreived once only, even for fastcgi */
@@ -138,8 +161,6 @@ long fix_pathinfo=1;
138161
#define TRANSLATE_SLASHES(path)
139162
#endif
140163

141-
#define OPTSTRING "abCc:d:ef:g:hilmnqsw?vz:"
142-
143164
static int print_module_info(zend_module_entry *module, void *arg TSRMLS_DC)
144165
{
145166
php_printf("%s\n", module->name);
@@ -515,7 +536,7 @@ static void php_cgi_usage(char *argv0)
515536
prog = "php";
516537
}
517538

518-
php_printf("Usage: %s [-q] [-h] [-s [-v] [-i] [-f <file>] \n"
539+
php_printf("Usage: %s [-q] [-h] [-s] [-v] [-i] [-f <file>]\n"
519540
" %s <file> [args...]\n"
520541
" -a Run interactively\n"
521542
#if PHP_FASTCGI
@@ -881,8 +902,8 @@ int main(int argc, char *argv[])
881902
/* temporary locals */
882903
int behavior=PHP_MODE_STANDARD;
883904
int no_headers=0;
884-
int orig_optind=ap_php_optind;
885-
char *orig_optarg=ap_php_optarg;
905+
int orig_optind=optind;
906+
char *orig_optarg=optarg;
886907
char *script_file=NULL;
887908
zend_llist global_vars;
888909
int interactive=0;
@@ -958,10 +979,10 @@ int main(int argc, char *argv[])
958979
/* allow ini override for fastcgi */
959980
#endif
960981
) {
961-
while ((c=ap_php_getopt(argc, argv, OPTSTRING))!=-1) {
982+
while ((c=php_getopt(argc, argv, OPTIONS, &optarg, &optind, 0))!=-1) {
962983
switch (c) {
963984
case 'c':
964-
cgi_sapi_module.php_ini_path_override = strdup(ap_php_optarg);
985+
cgi_sapi_module.php_ini_path_override = strdup(optarg);
965986
break;
966987
case 'n':
967988
cgi_sapi_module.php_ini_ignore = 1;
@@ -971,14 +992,14 @@ int main(int argc, char *argv[])
971992
we are being started as an 'external' fastcgi
972993
server by accepting a bindpath parameter. */
973994
case 'b':
974-
if (!fastcgi) bindpath= strdup(ap_php_optarg);
995+
if (!fastcgi) bindpath= strdup(optarg);
975996
break;
976997
#endif
977998
}
978999

9791000
}
980-
ap_php_optind = orig_optind;
981-
ap_php_optarg = orig_optarg;
1001+
optind = orig_optind;
1002+
optarg = orig_optarg;
9821003
}
9831004

9841005
#ifdef ZTS
@@ -1172,8 +1193,9 @@ consult the installation file that came with this distribution, or visit \n\
11721193
&& !fastcgi
11731194
#endif
11741195
) {
1175-
while ((c=ap_php_getopt(argc, argv, OPTSTRING))!=-1) {
1196+
while ((c=php_getopt(argc, argv, OPTIONS, &optarg, &optind, 1))!=-1) {
11761197
switch (c) {
1198+
case 'h':
11771199
case '?':
11781200
no_headers = 1;
11791201
php_output_startup();
@@ -1185,8 +1207,8 @@ consult the installation file that came with this distribution, or visit \n\
11851207
break;
11861208
}
11871209
}
1188-
ap_php_optind = orig_optind;
1189-
ap_php_optarg = orig_optarg;
1210+
optind = orig_optind;
1211+
optarg = orig_optarg;
11901212
}
11911213

11921214
#if PHP_FASTCGI
@@ -1236,7 +1258,7 @@ consult the installation file that came with this distribution, or visit \n\
12361258
exit(1);
12371259
}
12381260

1239-
while ((c = ap_php_getopt(argc, argv, OPTSTRING)) != -1) {
1261+
while ((c = php_getopt(argc, argv, OPTIONS, &optarg, &optind, 0)) != -1) {
12401262
switch (c) {
12411263

12421264
case 'a': /* interactive mode */
@@ -1248,37 +1270,26 @@ consult the installation file that came with this distribution, or visit \n\
12481270
SG(options) |= SAPI_OPTION_NO_CHDIR;
12491271
break;
12501272
case 'd': /* define ini entries on command line */
1251-
define_command_line_ini_entry(ap_php_optarg);
1273+
define_command_line_ini_entry(optarg);
12521274
break;
12531275

12541276
case 'e': /* enable extended info output */
12551277
CG(extended_info) = 1;
12561278
break;
12571279

12581280
case 'f': /* parse file */
1259-
script_file = estrdup(ap_php_optarg);
1281+
script_file = estrdup(optarg);
12601282
no_headers = 1;
12611283
break;
12621284

12631285
case 'g': /* define global variables on command line */
12641286
{
1265-
char *arg = estrdup(ap_php_optarg);
1287+
char *arg = estrdup(optarg);
12661288

12671289
zend_llist_add_element(&global_vars, &arg);
12681290
}
12691291
break;
12701292

1271-
case 'h': /* help & quit */
1272-
case '?':
1273-
no_headers = 1;
1274-
php_output_startup();
1275-
php_output_activate(TSRMLS_C);
1276-
SG(headers_sent) = 1;
1277-
php_cgi_usage(argv[0]);
1278-
php_end_ob_buffers(1 TSRMLS_CC);
1279-
exit(1);
1280-
break;
1281-
12821293
case 'i': /* php info & quit */
12831294
if (php_request_startup(TSRMLS_C)==FAILURE) {
12841295
php_module_shutdown(TSRMLS_C);
@@ -1345,7 +1356,7 @@ consult the installation file that came with this distribution, or visit \n\
13451356
break;
13461357

13471358
case 'z': /* load extension file */
1348-
zend_load_extension(ap_php_optarg);
1359+
zend_load_extension(optarg);
13491360
break;
13501361

13511362
default:
@@ -1358,19 +1369,19 @@ consult the installation file that came with this distribution, or visit \n\
13581369
if (script_file) {
13591370
len += strlen(script_file) + 1;
13601371
}
1361-
for (i = ap_php_optind; i < argc; i++) {
1372+
for (i = optind; i < argc; i++) {
13621373
len += strlen(argv[i]) + 1;
13631374
}
13641375

13651376
s = malloc(len + 1); /* leak - but only for command line version, so ok */
13661377
*s = '\0'; /* we are pretending it came from the environment */
13671378
if (script_file) {
13681379
strcpy(s, script_file);
1369-
if (ap_php_optind<argc) {
1380+
if (optind<argc) {
13701381
strcat(s, "+");
13711382
}
13721383
}
1373-
for (i = ap_php_optind, len = 0; i < argc; i++) {
1384+
for (i = optind, len = 0; i < argc; i++) {
13741385
strcat(s, argv[i]);
13751386
if (i < (argc - 1)) {
13761387
strcat(s, "+");
@@ -1389,9 +1400,9 @@ consult the installation file that came with this distribution, or visit \n\
13891400
SG(request_info).no_headers = 1;
13901401
}
13911402

1392-
if (!SG(request_info).path_translated && argc > ap_php_optind) {
1403+
if (!SG(request_info).path_translated && argc > optind) {
13931404
/* file is on command line, but not in -f opt */
1394-
SG(request_info).path_translated = estrdup(argv[ap_php_optind]);
1405+
SG(request_info).path_translated = estrdup(argv[optind]);
13951406
}
13961407
} /* end !cgi && !fastcgi */
13971408

0 commit comments

Comments
 (0)