helly           Thu May 29 11:41:52 2003 EDT

  Modified files:              (Branch: PHP_4_3)
    /php4/sapi/cgi      cgi_main.c getopt.c php_getopt.h 
  Log:
  MFH: --style switches
  
Index: php4/sapi/cgi/cgi_main.c
diff -u php4/sapi/cgi/cgi_main.c:1.190.2.32 php4/sapi/cgi/cgi_main.c:1.190.2.33
--- php4/sapi/cgi/cgi_main.c:1.190.2.32 Mon May 26 19:21:08 2003
+++ php4/sapi/cgi/cgi_main.c    Thu May 29 11:41:52 2003
@@ -20,7 +20,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: cgi_main.c,v 1.190.2.32 2003/05/26 23:21:08 sniper Exp $ */
+/* $Id: cgi_main.c,v 1.190.2.33 2003/05/29 15:41:52 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);
@@ -897,8 +918,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 +995,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 +1009,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
@@ -1190,8 +1211,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 +1225,8 @@
                                                break;
                                }
                        }
-                       ap_php_optind = orig_optind;
-                       ap_php_optarg = orig_optarg;
+                       optind = orig_optind;
+                       optarg = orig_optarg;
                }
 
 #if PHP_FASTCGI
@@ -1254,7 +1276,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 +1288,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 +1296,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);
@@ -1366,7 +1377,7 @@
                                                break;
 
                                case 'z': /* load extension file */
-                                               zend_load_extension(ap_php_optarg);
+                                               zend_load_extension(optarg);
                                                break;
 
                                        default:
@@ -1384,12 +1395,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
@@ -1401,15 +1412,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, "&");
Index: php4/sapi/cgi/getopt.c
diff -u php4/sapi/cgi/getopt.c:1.5 php4/sapi/cgi/getopt.c:1.5.8.1
--- php4/sapi/cgi/getopt.c:1.5  Sun Jan 27 02:23:21 2002
+++ php4/sapi/cgi/getopt.c      Thu May 29 11:41:52 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 php4/sapi/cgi/php_getopt.h:1.3.14.1
--- php4/sapi/cgi/php_getopt.h:1.3      Wed Feb 21 02:41:01 2001
+++ php4/sapi/cgi/php_getopt.h  Thu May 29 11:41:52 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

Reply via email to