helly           Sat Feb 15 20:23:12 2003 EDT

  Modified files:              
    /php4/sapi/cgi      cgi_main.c getopt.c php_getopt.h 
    /php4/sapi/cli      getopt.c php.1.in php_cli.c php_getopt.h 
  Log:
  - 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.
  
Index: php4/sapi/cgi/cgi_main.c
diff -u php4/sapi/cgi/cgi_main.c:1.211 php4/sapi/cgi/cgi_main.c:1.212
--- php4/sapi/cgi/cgi_main.c:1.211      Fri Feb 14 09:38:20 2003
+++ php4/sapi/cgi/cgi_main.c    Sat Feb 15 20:23:11 2003
@@ -20,7 +20,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: cgi_main.c,v 1.211 2003/02/14 14:38:20 sniper Exp $ */
+/* $Id: cgi_main.c,v 1.212 2003/02/16 01:23:11 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 "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 @@
                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 @@
 /* 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 @@
                /* 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 @@
                                   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 @@
                        && !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 @@
                                                break;
                                }
                        }
-                       ap_php_optind = orig_optind;
-                       ap_php_optarg = orig_optarg;
+                       optind = orig_optind;
+                       optarg = orig_optarg;
                }
 
 #if PHP_FASTCGI
@@ -1236,7 +1258,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 */
@@ -1248,7 +1270,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 */
@@ -1256,29 +1278,18 @@
                                                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 @@
                                                break;
 
                                case 'z': /* load extension file */
-                                               zend_load_extension(ap_php_optarg);
+                                               zend_load_extension(optarg);
                                                break;
 
                                        default:
@@ -1358,7 +1369,7 @@
                                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 @@
                                *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 @@
                                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 */
 
Index: php4/sapi/cgi/getopt.c
diff -u php4/sapi/cgi/getopt.c:1.5 php4/sapi/cgi/getopt.c:1.6
--- php4/sapi/cgi/getopt.c:1.5  Sun Jan 27 02:23:21 2002
+++ php4/sapi/cgi/getopt.c      Sat Feb 15 20:23:11 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.4
--- php4/sapi/cgi/php_getopt.h:1.3      Wed Feb 21 02:41:01 2001
+++ php4/sapi/cgi/php_getopt.h  Sat Feb 15 20:23:11 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);
Index: php4/sapi/cli/getopt.c
diff -u php4/sapi/cli/getopt.c:1.3 php4/sapi/cli/getopt.c:1.4
--- php4/sapi/cli/getopt.c:1.3  Sat May  4 13:27:18 2002
+++ php4/sapi/cli/getopt.c      Sat Feb 15 20:23:11 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,164 +26,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.1.in
diff -u php4/sapi/cli/php.1.in:1.1 php4/sapi/cli/php.1.in:1.2
--- php4/sapi/cli/php.1.in:1.1  Tue Feb 11 23:38:30 2003
+++ php4/sapi/cli/php.1.in      Sat Feb 15 20:23:11 2003
@@ -14,7 +14,7 @@
 ./"    | Author: Marcus Boerger <[EMAIL PROTECTED]>                               |
 ./"    +----------------------------------------------------------------------+
 ./" 
-./" $Id: php.1.in,v 1.1 2003/02/12 04:38:30 sniper Exp $
+./" $Id: php.1.in,v 1.2 2003/02/16 01:23:11 helly Exp $
 ./" 
 .TH PHP 1 "Feb 2003" "The PHP Group" "Scripting Language"
 .SH NAME
@@ -72,7 +72,9 @@
 .LP
 Using parameter \-r you can directly execute PHP 
 .IR code 
-simply as you would do inside a .php file when using the 
+simply as you would do inside a
+.B \.php
+file when using the 
 .B eval() 
 function.
 .LP
@@ -96,9 +98,17 @@
 executed.
 .SH OPTIONS
 .TP 15
+.PD 0
+.B \-\-interactive
+.TP
+.PD 1
 .B \-a
 Run interactively
 .TP
+.PD 0
+.B \-\-php\-ini \fIpath\fP|\fIfile\fP
+.TP
+.PD 1
 .B \-c \fIpath\fP|\fIfile\fP
 Look for 
 .B php.ini 
@@ -107,11 +117,19 @@
 or use the specified
 .IR file
 .TP
+.PD 0
+.B \-\-no\-php\-ini
+.TP
+.PD 1
 .B \-n
 No 
 .B php.ini 
 file will be used
 .TP
+.PD 0
+.B \-\-define \fIfoo\fP[=\fIbar\fP]
+.TP
+.PD 1
 .B \-d \fIfoo\fP[=\fIbar\fP]
 Define INI entry 
 .IR foo 
@@ -121,62 +139,131 @@
 .B \-e
 Generate extended information for debugger/profiler
 .TP
+.PD 0
+.B \-\-file \fIfile\fP
+.TP
+.PD 1
 .B \-f \fIfile\fP
 Parse and execute 
 .IR file
 .TP
+.PD 0
+.B \-\-global \fIname\fP
+.TP
+.PD 1
+.B \-g \fIname\fP
+Make variable
+.IR name
+global in script.
+.TP
+.PD 0
+.B \-\-help
+.TP
+.PD 1
 .B \-h
 This help
 .TP
+.PD 0
+.B \-\-hide\-args
+.TP
+.PD 1
 .B \-H
 Hide script name (\fIfile\fP) and parameters (\fIargs\.\.\.\fP) from external 
 tools. For example you may want to use this when a php script is started as 
 a daemon and the command line contains sensitive data such as passwords.
 .TP
+.PD 0
+.B \-\-info
+.TP
+.PD 1
 .B \-i
 PHP information and configuration
 .TP
+.PD 0
+.B \-\-syntax\-check
+.TP
+.PD 1
 .B \-l
 Syntax check only (lint)
 .TP
+.PD 0
+.B \-\-modules
+.TP
+.PD 1
 .B \-m
 Show compiled in modules
 .TP
+.PD 0
+.B \-\-run \fIcode\fP
+.TP
+.PD 1
 .B \-r \fIcode\fP
 Run PHP 
 .IR code
 without using script tags
 .B '<?..?>'
 .TP
+.PD 0
+.B \-\-process\-begin \fIcode\fP
+.TP
+.PD 1
 .B \-B \fIcode\fP
 Run PHP 
 .IR code
 before processing input lines
 .TP
+.PD 0
+.B \-\-process\-code \fIcode\fP
+.TP
+.PD 1
 .B \-R \fIcode\fP
 Run PHP 
 .IR code
 for every input line
 .TP
+.PD 0
+.B \-\-process\-file \fIfile\fP
+.TP
+.PD 1
 .B \-F \fIfile\fP
 Parse and execute 
 .IR file
 for every input line
 .TP
+.PD 0
+.B \-\-process\-end \fIcode\fP
+.TP
+.PD 1
 .B \-E \fIcode\fP
 Run PHP 
 .IR code
 after processing all input lines
 .TP
+.PD 0
+.B \-\-syntax\-highlight
+.TP
+.PD 1
 .B \-s
 Display colour syntax highlighted source
 .TP
+.PD 0
+.B \-\-version
+.TP
+.PD 1
 .B \-v
 Version number
 .TP
+.PD 0
+.B \-\-stripped
+.TP
+.PD 1
 .B \-w
 Display source with stripped comments and whitespace
 .TP
+.PD 0
+.B \-\-zend\-extension \fIfile\fP
+.TP
+.PD 1
 .B \-z \fIfile\fP
 Load Zend extension 
 .IR file
@@ -212,25 +299,26 @@
 \fIphp \-r 'print_r(dba_handlers(1));'\fP
 .RE
 .TP
-\fIphp \-d html_errors=1 \-i | php \-R 'echo strip_tags($argn)."\\n";'\fP
-This example uses PHP first to generate a HTML output. This is 
-meant to be replaced with any tool that displays HTML (for instance
-you could use 'cat file.html'). The second php command now strips off
-the HTML tags line by line and outputs the result.
+\fIphp \-R'echo strip_tags($argn)."\\n";\fP
+This PHP command strips off the HTML tags line by line and outputs the 
+result. To see how it works you can first look at the following PHP command '
+\fIphp \-d html_errors=1 \-i\fP' which uses PHP to output HTML formatted
+configuration information. If you then combine those two 
+\'\fIphp \.\.\.|php \.\.\.\fP\' you\'ll see what happens.
 .TP
 \fIphp \-E 'echo "Lines: $argi\\n";'\fP
-This command shows the number of lines being input.
+Using this PHP command you can count the lines being input.
 .TP
 \fIphp \-R '$l+=count(file($argn));' \-E'echo "Lines:$l\\n";'\fP
-This commands expects each input line beeing a file. It counts all lines 
+In this example PHP expects each input line beeing a file. It counts all lines 
 of the files specified by each input line and shows the summarized result. 
 You may combine this with tools like find and change the php scriptlet.
 .TP
 \fIphp \-R 'echo "$argn\\n"; fgets(STDIN);'\fP
-Since you have access to STDIN from within \-B \-R and \-F you can skip certain
-input lines with your code. But note that in such cases $argi only counts the
-lines being processed by php itself. Having read this you will guess what the 
-above program does: skipping every second input line.
+Since you have access to STDIN from within \-B \-R \-F and \-E you can skip 
+certain input lines with your code. But note that in such cases $argi only 
+counts the lines being processed by php itself. Having read this you will 
+guess what the above program does: skipping every second input line.
 .SH TIPS
 You can use a shebang line to automatically invoke php
 from scripts. Only the CLI version of PHP will ignore
Index: php4/sapi/cli/php_cli.c
diff -u php4/sapi/cli/php_cli.c:1.70 php4/sapi/cli/php_cli.c:1.71
--- php4/sapi/cli/php_cli.c:1.70        Sat Feb 15 10:20:17 2003
+++ php4/sapi/cli/php_cli.c     Sat Feb 15 20:23:11 2003
@@ -84,10 +84,37 @@
 #define PHP_MODE_CLI_DIRECT    6
 #define PHP_MODE_PROCESS_STDIN 7
 
-extern char *ap_php_optarg;
-extern int ap_php_optind;
+static char *optarg = NULL;
+static int optind = 1;
 
-#define OPTSTRING "aB:Cc:d:E:eF:f:g:hilmnqR:Hr:sw?vz"
+static const opt_struct OPTIONS[] = {
+       {'a', 0, "interactive"},
+       {'B', 1, "process-begin"},
+       {'C', 0, "no-chdir"}, /* for compatibility with CGI (do not chdir to script 
+directory) */
+       {'c', 1, "php-ini"},
+       {'d', 1, "define"},
+       {'E', 1, "process-end"},
+       {'e', 0, "profile-info"},
+       {'F', 1, "process-file"},
+       {'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) */
+       {'R', 1, "process-code"},
+       {'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)
 {
@@ -461,8 +488,8 @@
 /* temporary locals */
        int behavior=PHP_MODE_STANDARD;
        int no_headers=1;
-       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;
@@ -509,18 +536,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];
 
@@ -545,22 +572,6 @@
        module_started = 1;
 
        zend_first_try {
-               while ((c=ap_php_getopt(argc, argv, OPTSTRING))!=-1) {
-                       switch (c) {
-                       case '?':
-                               no_headers = 1;
-                               php_output_startup();
-                               php_output_activate(TSRMLS_C);
-                               SG(headers_sent) = 1;
-                               php_cli_usage(argv[0]);
-                               php_end_ob_buffers(1 TSRMLS_CC);
-                               exit(1);
-                               break;
-                       }
-               }
-               ap_php_optind = orig_optind;
-               ap_php_optarg = orig_optarg;
-
                zend_llist_init(&global_vars, sizeof(char *), NULL, 0);
 
         /* Set some CLI defaults */
@@ -579,9 +590,20 @@
                        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 'h': /* help & quit */
+                       case '?':
+                               no_headers = 1;
+                               php_output_startup();
+                               php_output_activate(TSRMLS_C);
+                               SG(headers_sent) = 1;
+                               php_cli_usage(argv[0]);
+                               php_end_ob_buffers(1 TSRMLS_CC);
+                               exit(1);
+                               break;
+
                        case 'a':       /* interactive mode */
                                printf("Interactive mode enabled\n\n");
                                interactive=1;
@@ -591,7 +613,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 */
@@ -609,7 +631,7 @@
                                        break;
                                }
                                behavior=PHP_MODE_PROCESS_STDIN;
-                               script_file = ap_php_optarg;
+                               script_file = optarg;
                                no_headers = 1;
                                break;
 
@@ -621,29 +643,18 @@
                                        param_error = "You can use -f only once.\n";
                                        break;
                                }
-                               script_file = ap_php_optarg;
+                               script_file = 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_cli_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) {
                                        goto err;
@@ -703,7 +714,7 @@
                                        break;
                                }
                                behavior=PHP_MODE_CLI_DIRECT;
-                               exec_direct=ap_php_optarg;
+                               exec_direct=optarg;
                                break;
                        
                        case 'R':
@@ -717,7 +728,7 @@
                                        break;
                                }
                                behavior=PHP_MODE_PROCESS_STDIN;
-                               exec_run=ap_php_optarg;
+                               exec_run=optarg;
                                break;
 
                        case 'B':
@@ -731,7 +742,7 @@
                                        break;
                                }
                                behavior=PHP_MODE_PROCESS_STDIN;
-                               exec_begin=ap_php_optarg;
+                               exec_begin=optarg;
                                break;
 
                        case 'E':
@@ -746,7 +757,7 @@
                                }
                                scan_input = 1;
                                behavior=PHP_MODE_PROCESS_STDIN;
-                               exec_end=ap_php_optarg;
+                               exec_end=optarg;
                                break;
 
                        case 's': /* generate highlighted HTML from source */
@@ -780,7 +791,7 @@
                                break;
 
                        case 'z': /* load extension file */
-                               zend_load_extension(ap_php_optarg);
+                               zend_load_extension(optarg);
                                break;
                        case 'H':
                                hide_argv = 1;
@@ -801,15 +812,15 @@
                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 
+               if (argc > optind 
                  && !script_file 
                  && behavior!=PHP_MODE_CLI_DIRECT 
                  && behavior!=PHP_MODE_PROCESS_STDIN 
-                 && strcmp(argv[ap_php_optind-1],"--")) 
+                 && strcmp(argv[optind-1],"--")) 
                {
                        no_headers = 1;
-                       script_file=argv[ap_php_optind];
-                       ap_php_optind++;
+                       script_file=argv[optind];
+                       optind++;
                }
                if (script_file) {
                        if (cli_seek_file_begin(&file_handle, script_file, &lineno 
TSRMLS_CC) != SUCCESS) {
@@ -831,12 +842,12 @@
 
                /* 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;
Index: php4/sapi/cli/php_getopt.h
diff -u php4/sapi/cli/php_getopt.h:1.1 php4/sapi/cli/php_getopt.h:1.2
--- php4/sapi/cli/php_getopt.h:1.1      Sun Jan  6 09:08:14 2002
+++ php4/sapi/cli/php_getopt.h  Sat Feb 15 20:23:11 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