]> granicus.if.org Git - php/commitdiff
- Added common getopt implementation to core.
authorJani Taskinen <jani@php.net>
Mon, 1 Oct 2007 12:32:39 +0000 (12:32 +0000)
committerJani Taskinen <jani@php.net>
Mon, 1 Oct 2007 12:32:39 +0000 (12:32 +0000)
- Added long-option feature to getopt().
- Made getopt() available on win32 systems.
Patch by: David Soria Parra <dsp@php.net>
[DOC]: These changes will be available from 5.3+

13 files changed:
configure.in
ext/standard/basic_functions.c
ext/standard/config.m4
main/getopt.c [moved from sapi/cgi/getopt.c with 81% similarity]
main/php_getopt.h [moved from sapi/cgi/php_getopt.h with 73% similarity]
sapi/cgi/cgi_main.c
sapi/cgi/config.w32
sapi/cgi/config9.m4
sapi/cli/config.w32
sapi/cli/getopt.c [deleted file]
sapi/cli/php_cli.c
sapi/cli/php_getopt.h [deleted file]
win32/build/config.w32

index 985849237afe8e589b2292cff868485a5bf05da9..6f7a849db46d64e73621336778b71e7bb4480a59 100644 (file)
@@ -1054,7 +1054,7 @@ esac
 if test "$PHP_CLI" != "no"; then
   PHP_CLI_TARGET="\$(SAPI_CLI_PATH)"
   PHP_INSTALL_CLI_TARGET="install-cli"
-  PHP_ADD_SOURCES(sapi/cli, php_cli.c php_cli_readline.c getopt.c,, cli)
+  PHP_ADD_SOURCES(sapi/cli, php_cli.c php_cli_readline.c,, cli)
   PHP_INSTALLED_SAPIS="cli $PHP_SAPI"
 else
   PHP_INSTALLED_SAPIS="$PHP_SAPI"
@@ -1208,7 +1208,7 @@ PHP_ADD_SOURCES(main, main.c snprintf.c spprintf.c php_sprintf.c \
        php_ini.c SAPI.c rfc1867.c php_content_types.c strlcpy.c \
        strlcat.c mergesort.c reentrancy.c php_variables.c php_ticks.c \
        network.c php_open_temporary_file.c php_logos.c \
-       output.c )
+       output.c getopt.c)
 
 PHP_ADD_SOURCES(main/streams, streams.c cast.c memory.c filter.c \
        plain_wrapper.c userspace.c transports.c xp_socket.c mmap.c \
index a1f99b25277da99e38a3fec9a277d3fc0c35fc87..c3ff9ce48ebe840649723ec0e115131c8d1ff752 100644 (file)
@@ -28,6 +28,7 @@
 #include "php_math.h"
 #include "php_http.h"
 #include "php_incomplete_class.h"
+#include "php_getopt.h"
 #include "ext/standard/info.h"
 #include "ext/session/php_session.h"
 #include "zend_operators.h"
@@ -83,10 +84,6 @@ typedef struct yy_buffer_state *YY_BUFFER_STATE;
 # include <sys/loadavg.h>
 #endif
 
-#ifdef HARTMUT_0
-#include <getopt.h>
-#endif
-
 #ifdef PHP_WIN32
 # include "win32/unistd.h"
 #endif
@@ -100,7 +97,6 @@ typedef struct yy_buffer_state *YY_BUFFER_STATE;
 #include "SAPI.h"
 #include "php_ticks.h"
 
-
 #ifdef ZTS
 PHPAPI int basic_globals_id;
 #else
@@ -725,13 +721,11 @@ ZEND_BEGIN_ARG_INFO(arginfo_putenv, 0)
 ZEND_END_ARG_INFO()
 #endif
 
-#ifdef HAVE_GETOPT
 static
 ZEND_BEGIN_ARG_INFO_EX(arginfo_getopt, 0, 0, 1)
        ZEND_ARG_INFO(0, options)
        ZEND_ARG_INFO(0, opts) /* ARRAY_INFO(0, opts, 1) */
 ZEND_END_ARG_INFO()
-#endif
 
 static
 ZEND_BEGIN_ARG_INFO(arginfo_flush, 0)
@@ -3128,7 +3122,7 @@ const zend_function_entry basic_functions[] = { /* {{{ */
        PHP_FE(iptcembed,                                                                                                               arginfo_iptcembed)
        PHP_FE(getimagesize,                                                                                                    arginfo_getimagesize)
        PHP_FE(image_type_to_mime_type,                                                                                 arginfo_image_type_to_mime_type)
-       PHP_FE(image_type_to_extension,                                                                                 arginfo_image_type_to_extension)
+       PHP_FE(image_type_to_extension,                                                                                 arginfo_image_type_to_extension)
 
        PHP_FE(phpinfo,                                                                                                                 arginfo_phpinfo)
        PHP_FE(phpversion,                                                                                                              arginfo_phpversion)
@@ -3178,7 +3172,7 @@ const zend_function_entry basic_functions[] = { /* {{{ */
 #endif
 
 #ifdef HAVE_STRFMON
-       PHP_FE(money_format,                                                    arginfo_money_format)
+       PHP_FE(money_format,                                                                                                    arginfo_money_format)
 #endif
 
        PHP_FE(substr,                                                                                                                  arginfo_substr)
@@ -3359,9 +3353,7 @@ const zend_function_entry basic_functions[] = { /* {{{ */
        PHP_FE(putenv,                                                                                                                  arginfo_putenv)
 #endif
 
-#ifdef HAVE_GETOPT
        PHP_FE(getopt,                                                                                                                  arginfo_getopt)
-#endif
 
 #ifdef HAVE_GETLOADAVG
        PHP_FE(sys_getloadavg,                                                                                                  arginfo_sys_getloadavg)
@@ -3408,7 +3400,7 @@ const zend_function_entry basic_functions[] = { /* {{{ */
        PHP_FE(unregister_tick_function,                                                                                arginfo_unregister_tick_function)
 
        PHP_FE(highlight_file,                                                                                                  arginfo_highlight_file)
-       PHP_FALIAS(show_source,                 highlight_file,                                                 arginfo_highlight_file)
+       PHP_FALIAS(show_source,                 highlight_file,                                                 arginfo_highlight_file)
        PHP_FE(highlight_string,                                                                                                arginfo_highlight_string)
        PHP_FE(php_strip_whitespace,                                                                                    arginfo_php_strip_whitespace)
 
@@ -3424,7 +3416,7 @@ const zend_function_entry basic_functions[] = { /* {{{ */
        PHP_FE(setcookie,                                                                                                               arginfo_setcookie)
        PHP_FE(setrawcookie,                                                                                                    arginfo_setrawcookie)
        PHP_FE(header,                                                                                                                  arginfo_header)
-       PHP_FE(headers_sent,                                                                                                    arginfo_headers_sent)
+       PHP_FE(headers_sent,                                                                                                    arginfo_headers_sent)
        PHP_FE(headers_list,                                                                                                    arginfo_headers_list)
 
        PHP_FE(connection_aborted,                                                                                              arginfo_connection_aborted)
@@ -3447,7 +3439,7 @@ const zend_function_entry basic_functions[] = { /* {{{ */
        PHP_FALIAS(checkdnsrr,                  dns_check_record,                                               arginfo_dns_check_record)
 # if HAVE_DN_SKIPNAME && HAVE_DN_EXPAND
        PHP_FE(dns_get_mx,                                                                                                              arginfo_dns_get_mx)
-       PHP_FALIAS(getmxrr,                     dns_get_mx,                                                     arginfo_dns_get_mx)
+       PHP_FALIAS(getmxrr,                             dns_get_mx,                                                             arginfo_dns_get_mx)
 # endif
 # if HAVE_DNS_FUNCS
        PHP_FE(dns_get_record,                                                                                                  arginfo_dns_get_record)
@@ -3457,7 +3449,7 @@ const zend_function_entry basic_functions[] = { /* {{{ */
        /* functions from type.c */
        PHP_FE(intval,                                                                                                                  arginfo_intval)
        PHP_FE(floatval,                                                                                                                arginfo_floatval)
-       PHP_FALIAS(doubleval,          floatval,                                                                arginfo_floatval)
+       PHP_FALIAS(doubleval,                   floatval,                                                               arginfo_floatval)
        PHP_FE(strval,                                                                                                                  arginfo_strval)
        PHP_FE(gettype,                                                                                                                 arginfo_gettype)
        PHP_FE(settype,                                                                                                                 arginfo_settype)
@@ -3754,7 +3746,7 @@ const zend_function_entry basic_functions[] = { /* {{{ */
 
        /* aliases from array.c */
        PHP_FALIAS(pos,                                 current,                                                                arginfo_current)
-       PHP_FALIAS(sizeof,                              count,                                                                  arginfo_count)
+       PHP_FALIAS(sizeof,                              count,                                                                  arginfo_count)
        PHP_FALIAS(key_exists,                  array_key_exists,                                               arginfo_array_key_exists)
 
        /* functions from assert.c */
@@ -3769,9 +3761,9 @@ const zend_function_entry basic_functions[] = { /* {{{ */
        PHP_FE(ftok,                                                                                                                    arginfo_ftok)
 #endif
 
-       PHP_FE(str_rot13,                                                                                                               arginfo_str_rot13)
-       PHP_FE(stream_get_filters,                                                                                              arginfo_stream_get_filters)
-       PHP_FE(stream_filter_register,                                                                                  arginfo_stream_filter_register)
+       PHP_FE(str_rot13,                                                                                                               arginfo_str_rot13)
+       PHP_FE(stream_get_filters,                                                                                              arginfo_stream_get_filters)
+       PHP_FE(stream_filter_register,                                                                                  arginfo_stream_filter_register)
        PHP_FE(stream_bucket_make_writeable,                                                                    arginfo_stream_bucket_make_writeable)
        PHP_FE(stream_bucket_prepend,                                                                                   arginfo_stream_bucket_prepend)
        PHP_FE(stream_bucket_append,                                                                                    arginfo_stream_bucket_append)
@@ -4432,7 +4424,6 @@ PHP_FUNCTION(putenv)
 /* }}} */
 #endif
 
-#ifdef HAVE_GETOPT
 /* {{{ free_argv()
    Free the memory allocated to an argv array. */
 static void free_argv(char **argv, int argc)
@@ -4450,23 +4441,54 @@ static void free_argv(char **argv, int argc)
 }
 /* }}} */
 
-#ifdef HARTMUT_0
 /* {{{ free_longopts()
    Free the memory allocated to an longopt array. */
-static void free_longopts(struct option *longopts)
+static void free_longopts(opt_struct *longopts)
 {
-       struct option *p;
+       opt_struct *p;
 
        if (longopts) {
-               for (p = longopts; p->name; p++) {
-                       efree((char *)(p->name));
+               for (p = longopts; p && p->opt_char != '-'; p++) {
+                       if (p->opt_name != NULL) {
+                               efree((char *)(p->opt_name));
+                       }
                }
+       }
+}
+/* }}} */
 
-               efree(longopts);
+/* {{{ parse_opts()
+   Convert the typical getopt input characters to the php_getopt struct array */
+static int parse_opts(char * opts, opt_struct ** result)
+{
+       opt_struct * paras = NULL;
+       int i, count = 0;
+
+       for (i = 0; i < strlen(opts); i++) {
+               if ((opts[i] >= 65 && opts[i] <= 90) ||
+                       (opts[i] >= 97 && opts[i] <= 122)
+               ) {
+                       count++;
+               }
        }
+
+       paras = safe_emalloc(sizeof(opt_struct), count, 0);
+       memset(paras, 0, sizeof(opt_struct) * count);
+       *result = paras;
+       while ( (*opts >= 65 && *opts <= 90) ||
+                       (*opts >= 97 && *opts <= 122)
+       ) {
+               paras->opt_char = *opts;
+               paras->need_param = (*(++opts) == ':') ? 1 : 0;
+               paras->opt_name = NULL;
+               if (paras->need_param == 1) {
+                       opts++;
+               }
+               paras++;
+       }
+       return count;
 }
 /* }}} */
-#endif
 
 /* {{{ proto array getopt(string options [, array longopts]) U
    Get options from the command line argument list */
@@ -4475,13 +4497,12 @@ PHP_FUNCTION(getopt)
        char *options = NULL, **argv = NULL;
        char opt[2] = { '\0' };
        char *optname;
-       int argc = 0, options_len = 0, o;
+       int argc = 0, options_len = 0, len, o;
+       char *php_optarg = NULL;
+       int php_optind = 1;
        zval *val, **args = NULL, *p_longopts = NULL;
        int optname_len = 0;
-#ifdef HARTMUT_0
-       struct option *longopts = NULL;
-       int longindex = 0;
-#endif
+       opt_struct *opts, *orig_opts;
 
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a", &options, &options_len, &p_longopts) == FAILURE) {
                RETURN_FALSE;
@@ -4519,65 +4540,64 @@ PHP_FUNCTION(getopt)
                RETURN_FALSE;
        }
 
+       len = parse_opts(options, &opts);
+
        if (p_longopts) {
-#ifdef HARTMUT_0
-               int len, c = zend_hash_num_elements(Z_ARRVAL_P(p_longopts));
-               struct option *p;
+               int count;
                zval **arg;
-               char *name;
 
-               longopts = (struct option *)ecalloc(c+1, sizeof(struct option));
+               count = zend_hash_num_elements(Z_ARRVAL_P(p_longopts));
+               
+               /* the first <len> slots are filled by the one short ops
+                * we now extend our array and jump to the new added structs */
+               opts = (opt_struct *) erealloc(opts, sizeof(opt_struct) * (len + count + 1));
+        orig_opts = opts;
+        opts += len;
+
+        memset(opts, 0, count * sizeof(opt_struct));
 
-               if (!longopts) {
+               if (!opts) {
                        RETURN_FALSE;
                }
 
                /* Reset the array indexes. */
                zend_hash_internal_pointer_reset(Z_ARRVAL_P(p_longopts));
-               p = longopts;
 
                /* Iterate over the hash to construct the argv array. */
                while (zend_hash_get_current_data(Z_ARRVAL_P(p_longopts), (void **)&arg) == SUCCESS) {
-
-                       p->has_arg = 0;
-                       name = estrdup(Z_STRVAL_PP(arg));
-                       len = strlen(name);
-                       if ((len > 0) && (name[len-1] == ':')) {
-                               p->has_arg++;
-                               name[len-1] = '\0';
-                               if ((len > 1) && (name[len-2] == ':')) {
-                                       p->has_arg++;
-                                       name[len-2] = '\0';
+                       opts->need_param = 0;
+                       opts->opt_name = estrdup(Z_STRVAL_PP(arg));
+                       len = strlen(opts->opt_name);
+                       if ((len > 0) && (opts->opt_name[len-1] == ':')) {
+                               opts->need_param++;
+                               opts->opt_name[len-1] = '\0';
+                               if ((len > 1) && (opts->opt_name[len-2] == ':')) {
+                                       opts->need_param++;
+                                       opts->opt_name[len-2] = '\0';
                                }
                        }
-
-                       p->name = name;
-                       p->flag = NULL;
-                       p->val = 0;
-
+                       opts->opt_char = 0;
+                       opts++;
                        zend_hash_move_forward(Z_ARRVAL_P(p_longopts));
-                       p++;
                }
-#else
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "No support for long options in this build");
-#endif
+       } else {
+               opts = (opt_struct*) erealloc(opts, sizeof(opt_struct) * (len + 1));
+               orig_opts = opts;
+               opts += len;
        }
 
+       /* php_getopt want to identify the last param */
+       opts->opt_char   = '-';
+       opts->need_param = 0;
+       opts->opt_name   = NULL;
+
        /* Initialize the return value as an array. */
        array_init(return_value);
 
-       /* Disable getopt()'s error messages. */
-       opterr = 0;
-
-       /* Force reinitialization of getopt() (via optind reset) on every call. */
-       optind = 1;
+       /* after our pointer arithmetic jump back to the first element */
+       opts = orig_opts;
 
-       /* Invoke getopt(3) on the argument array. */
-#ifdef HARTMUT_0
-       while ((o = getopt_long(argc, argv, options, longopts, &longindex)) != -1) {
-#else
-       while ((o = getopt(argc, argv, options)) != -1) {
-#endif
+       while ((o = php_getopt(argc, argv, opts, &php_optarg, &php_optind, 0, 1)) != -1) {
                /* Skip unknown arguments. */
                if (o == '?') {
                        continue;
@@ -4585,12 +4605,7 @@ PHP_FUNCTION(getopt)
 
                /* Prepare the option character and the argument string. */
                if (o == 0) {
-#ifdef HARTMUT_0
-                       optname = (char *)longopts[longindex].name;
-#else
-                       /* o == 0 shall never happen so this only fixes a compiler warning */
-                       optname = NULL;
-#endif
+                       optname = opts[php_optidx].opt_name;
                } else {
                        if (o == 1) {
                                o = '-';
@@ -4600,9 +4615,9 @@ PHP_FUNCTION(getopt)
                }
 
                MAKE_STD_ZVAL(val);
-               if (optarg != NULL) {
+               if (php_optarg != NULL) {
                        /* keep the arg as binary, since the encoding is not known */
-                       ZVAL_STRING(val, optarg, 1);
+                       ZVAL_STRING(val, php_optarg, 1);
                } else {
                        ZVAL_FALSE(val);
                }
@@ -4631,15 +4646,15 @@ PHP_FUNCTION(getopt)
                                zend_hash_add(HASH_OF(return_value), optname, strlen(optname)+1, (void *)&val, sizeof(zval *), NULL);
                        }
                }
+
+               php_optarg = NULL;
        }
 
+       free_longopts(orig_opts);
+       efree(orig_opts);
        free_argv(argv, argc);
-#ifdef HARTMUT_0
-       free_longopts(longopts);
-#endif
 }
 /* }}} */
-#endif
 
 /* {{{ proto void flush(void) U
    Flush the output buffer */
@@ -5746,9 +5761,9 @@ PHP_FUNCTION(get_include_path)
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
                return;
        }
-       
+
        str = zend_ini_string("include_path", sizeof("include_path"), 0);
-       
+
        if (str == NULL) {
                RETURN_FALSE;
        }
@@ -6294,7 +6309,7 @@ static int copy_request_variable(void *pDest, int num_args, va_list args, zend_h
        }
 
        if (hash_key->nKeyLength) {
-               php_prefix_varname(&new_key, prefix, hash_key->arKey, hash_key->nKeyLength-1, hash_key->type, 0 TSRMLS_CC);
+               php_prefix_varname(&new_key, prefix, hash_key->arKey, hash_key->nKeyLength - 1, hash_key->type, 0 TSRMLS_CC);
        } else {
                zval num;
                ZVAL_LONG(&num, hash_key->h);
index d19a595692741333329d7e3d27dacdc8b8c33b35..462ac15dd91b737a882162cafa73ff3d84701071 100644 (file)
@@ -251,12 +251,6 @@ else
   AC_MSG_RESULT(no)
 fi
 
-dnl getopt long options disabled for now
-dnl as we can't be sure that we get the right getopt.h here
-dnl using the standard AC_CHECK macros
-dnl AC_CHECK_HEADERS(getopt.h)
-dnl AC_CHECK_FUNCS(getopt_long getopt_long_only)
-
 if test "$PHP_SAPI" = "cgi" || test "$PHP_SAPI" = "cli" || test "$PHP_SAPI" = "embed"; then
   AC_DEFINE(ENABLE_CHROOT_FUNC, 1, [Whether to enable chroot() function])
 fi
similarity index 81%
rename from sapi/cgi/getopt.c
rename to main/getopt.c
index 4821a9c84dbadc11d994365a866d208735e97330..2c00920e959184f25b7e1c361dbc6dc6e597a17b 100644 (file)
 #include <assert.h>
 #include <stdlib.h>
 #include "php_getopt.h"
+
 #define OPTERRCOLON (1)
 #define OPTERRNF (2)
 #define OPTERRARG (3)
 
-
-static int php_opt_error(int argc, char * const *argv, int oint, int optchr, int err, int show_err)
+static int php_opt_error(int argc, char * const *argv, int oint, int optchr, int err, int show_err) /* {{{ */
 {
        if (show_err)
        {
@@ -51,14 +51,16 @@ static int php_opt_error(int argc, char * const *argv, int oint, int optchr, int
        }
        return('?');
 }
+/* }}} */
+
+PHPAPI int php_optidx = -1;
 
-int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **optarg, int *optind, int show_err)
+PHPAPI int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **optarg, int *optind, int show_err, int arg_start) /* {{{ */
 {
        static int optchr = 0;
        static int dash = 0; /* have already seen the - */
-       int arg_start = 2;
 
-       int opts_idx = -1;
+       php_optidx = -1;
 
        if (*optind >= argc) {
                return(EOF);
@@ -85,17 +87,17 @@ int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **opta
                }
 
                while (1) {
-                       opts_idx++;
-                       if (opts[opts_idx].opt_char == '-') {
+                       php_optidx++;
+                       if (opts[php_optidx].opt_char == '-') {
                                (*optind)++;
                                return(php_opt_error(argc, argv, *optind-1, optchr, OPTERRARG, show_err));
-                       } else if (opts[opts_idx].opt_name && !strcmp(&argv[*optind][2], opts[opts_idx].opt_name)) {
+                       } else if (opts[php_optidx].opt_name && !strcmp(&argv[*optind][2], opts[php_optidx].opt_name)) {
                                break;
                        }
                }
                optchr = 0;
                dash = 0;
-               arg_start = 2 + strlen(opts[opts_idx].opt_name);
+               arg_start = 2 + strlen(opts[php_optidx].opt_name);
        } else {
                if (!dash) {
                        dash = 1;
@@ -109,13 +111,13 @@ int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **opta
                }
                arg_start = 1 + optchr;
        }
-       if (opts_idx < 0) {
+       if (php_optidx < 0) {
                while (1) {
-                       opts_idx++;
-                       if (opts[opts_idx].opt_char == '-') {
+                       php_optidx++;
+                       if (opts[php_optidx].opt_char == '-') {
                                int errind = *optind;
                                int errchr = optchr;
-               
+
                                if (!argv[*optind][optchr+1]) {
                                        dash = 0;
                                        (*optind)++;
@@ -124,16 +126,16 @@ int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **opta
                                        arg_start++;
                                }
                                return(php_opt_error(argc, argv, errind, errchr, OPTERRNF, show_err));
-                       } else if (argv[*optind][optchr] == opts[opts_idx].opt_char) {
+                       } else if (argv[*optind][optchr] == opts[php_optidx].opt_char) {
                                break;
                        }
                }
        }
-       if (opts[opts_idx].need_param) {
+       if (opts[php_optidx].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]) {
+               if (!argv[*optind][arg_start]) {
                        (*optind)++;
                        if (*optind == argc) {
                                return(php_opt_error(argc, argv, *optind-1, optchr, OPTERRARG, show_err));
@@ -143,7 +145,7 @@ int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **opta
                        *optarg = &argv[*optind][arg_start];
                        (*optind)++;
                }
-               return opts[opts_idx].opt_char;
+               return opts[php_optidx].opt_char;
        } else {
                /* multiple options specified as one (exclude long opts) */
                if (arg_start >= 2 && !((argv[*optind][0] == '-') && (argv[*optind][1] == '-'))) {
@@ -157,8 +159,18 @@ int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **opta
                } else {
                        (*optind)++;
                }
-               return opts[opts_idx].opt_char;
+               return opts[php_optidx].opt_char;
        }
        assert(0);
        return(0);      /* never reached */
 }
+/* }}} */
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ * vim600: sw=4 ts=4 fdm=marker
+ * vim<600: sw=4 ts=4
+ */
similarity index 73%
rename from sapi/cgi/php_getopt.h
rename to main/php_getopt.h
index fc14c967437b096e8a996932716225c3704d8b93..2739cc9ee4cd4ef42045ec3de6a708b2aa6090b8 100644 (file)
@@ -18,6 +18,9 @@
 
 /* $Id$ */
 
+#ifndef PHP_GETOPT_H
+#define PHP_GETOPT_H
+
 #include "php.h"
 
 #ifdef NETWARE
@@ -27,13 +30,28 @@ As NetWare LibC has optind and optarg macros defined in unistd.h our local varia
 #undef optarg
 #undef optind
 #endif
+
 /* Define structure for one recognized option (both single char and long name).
- * If short_open is '-' this is the last option.
- */
+ * 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;
+       char opt_char;
+       int  need_param;
+       char * opt_name;
 } opt_struct;
 
-int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **optarg, int *optind, int show_err);
+BEGIN_EXTERN_C()
+/* holds the index of the latest fetched element from the opts array */
+PHPAPI int php_optidx;
+PHPAPI int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **optarg, int *optind, int show_err, int arg_start);
+END_EXTERN_C()
+
+#endif
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ * vim600: noet sw=4 ts=4 fdm=marker
+ * vim<600: noet sw=4 ts=4
+ */
index 2e3b2a8d9c0a99b6eef624be8e0f7c3b437dbd9e..90b9ea29885bceb2715933dca61f823eec3374a3 100644 (file)
@@ -1319,7 +1319,7 @@ int main(int argc, char *argv[])
                }
        }
 
-       while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0)) != -1) {
+       while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
                switch (c) {
                        case 'c':
                                if (cgi_sapi_module.php_ini_path_override) {
@@ -1541,7 +1541,7 @@ consult the installation file that came with this distribution, or visit \n\
        }
 
        zend_first_try {
-               while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 1)) != -1) {
+               while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 1, 2)) != -1) {
                        switch (c) {
                                case 'h':
                                case '?':
@@ -1581,7 +1581,7 @@ consult the installation file that came with this distribution, or visit \n\
                                        goto out;
                                }
 
-                               while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0)) != -1) {
+                               while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
                                        switch (c) {
 
                                                case 'a':       /* interactive mode */
index 18dbc69dc17b16bd2bd60fa5d6f706086958fb59..c4ff7528e56a6de313660dc29044f3a4af7cdb3b 100644 (file)
@@ -4,6 +4,6 @@
 ARG_ENABLE('cgi', 'Build CGI version of PHP', 'yes');
 
 if (PHP_CGI == "yes") {
-       SAPI('cgi', 'cgi_main.c getopt.c fastcgi.c', 'php-cgi.exe');
+       SAPI('cgi', 'cgi_main.c ..\\..\\main\\getopt.c fastcgi.c', 'php-cgi.exe');
        ADD_FLAG('LIBS_CGI', 'ws2_32.lib kernel32.lib advapi32.lib');
 }
index 8db26ad1c8057559d877cb825168c828a6c818ba..144b440e8875871bdb3503c252a8e057a909b915 100644 (file)
@@ -50,7 +50,7 @@ if test "$PHP_SAPI" = "default"; then
     PHP_SUBST(SAPI_CGI_PATH)
 
     INSTALL_IT="@echo \"Installing PHP CGI binary: \$(INSTALL_ROOT)\$(bindir)/\"; \$(INSTALL) -m 0755 \$(SAPI_CGI_PATH) \$(INSTALL_ROOT)\$(bindir)/\$(program_prefix)php-cgi\$(program_suffix)\$(EXEEXT)"
-    PHP_SELECT_SAPI(cgi, program, fastcgi.c cgi_main.c getopt.c, , '$(SAPI_CGI_PATH)')
+    PHP_SELECT_SAPI(cgi, program, fastcgi.c cgi_main.c, , '$(SAPI_CGI_PATH)')
 
     case $host_alias in
       *aix*)
index 83692489f5c927d3174addae7d4522822dba9dc8..864254fa6ff06cfa081d4414fef63b837b0da640 100644 (file)
@@ -6,13 +6,13 @@ ARG_ENABLE('crt-debug', 'Extra CRT debugging', 'no');
 ARG_ENABLE('cli-win32', 'Build console-less CLI version of PHP', 'no');
 
 if (PHP_CLI == "yes") {
-       SAPI('cli', 'getopt.c php_cli.c php_cli_readline.c', 'php.exe');
+       SAPI('cli', '..\\..\\main\\getopt.c php_cli.c php_cli_readline.c', 'php.exe');
        if (PHP_CRT_DEBUG == "yes") {
                ADD_FLAG("CFLAGS_CLI", "/D PHP_WIN32_DEBUG_HEAP");
        }
 }
 
 if (PHP_CLI_WIN32 == "yes") {
-       SAPI('cli_win32', 'getopt.c cli_win32.c php_cli_readline.c', 'php-win.exe');
+       SAPI('cli_win32', '..\\..\\main\\getopt.c cli_win32.c php_cli_readline.c', 'php-win.exe');
 }
 
diff --git a/sapi/cli/getopt.c b/sapi/cli/getopt.c
deleted file mode 100644 (file)
index 4821a9c..0000000
+++ /dev/null
@@ -1,164 +0,0 @@
-/*
-   +----------------------------------------------------------------------+
-   | PHP Version 5                                                        |
-   +----------------------------------------------------------------------+
-   | Copyright (c) 1997-2007 The PHP Group                                |
-   +----------------------------------------------------------------------+
-   | This source file is subject to version 3.01 of the PHP license,      |
-   | that is bundled with this package in the file LICENSE, and is        |
-   | available through the world-wide-web at the following url:           |
-   | http://www.php.net/license/3_01.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>                               |
-   +----------------------------------------------------------------------+
-*/
-
-/* $Id$ */
-
-#include <stdio.h>
-#include <string.h>
-#include <assert.h>
-#include <stdlib.h>
-#include "php_getopt.h"
-#define OPTERRCOLON (1)
-#define OPTERRNF (2)
-#define OPTERRARG (3)
-
-
-static int php_opt_error(int argc, char * const *argv, int oint, int optchr, int err, int show_err)
-{
-       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 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 */
-               if (argv[*optind][2] == '\0') {
-                       (*optind)++;
-                       return(EOF);
-               }
-
-               while (1) {
-                       opts_idx++;
-                       if (opts[opts_idx].opt_char == '-') {
-                               (*optind)++;
-                               return(php_opt_error(argc, argv, *optind-1, optchr, OPTERRARG, show_err));
-                       } else if (opts[opts_idx].opt_name && !strcmp(&argv[*optind][2], opts[opts_idx].opt_name)) {
-                               break;
-                       }
-               }
-               optchr = 0;
-               dash = 0;
-               arg_start = 2 + strlen(opts[opts_idx].opt_name);
-       } else {
-               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));
-               }
-               arg_start = 1 + optchr;
-       }
-       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++;
-                                       arg_start++;
-                               }
-                               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 {
-               /* multiple options specified as one (exclude long opts) */
-               if (arg_start >= 2 && !((argv[*optind][0] == '-') && (argv[*optind][1] == '-'))) {
-                       if (!argv[*optind][optchr+1])
-                       {
-                               dash = 0;
-                               (*optind)++;
-                       } else {
-                               optchr++;
-                       }
-               } else {
-                       (*optind)++;
-               }
-               return opts[opts_idx].opt_char;
-       }
-       assert(0);
-       return(0);      /* never reached */
-}
index 7b8e096620eb5eabb67079a9b7318bbe911bb1b3..bacddcec8028d1cbe56d5a6aa64853384469b6e0 100644 (file)
@@ -666,7 +666,7 @@ int main(int argc, char *argv[])
        memcpy(cli_sapi_module.ini_entries, HARDCODED_INI, ini_entries_len+1);
        cli_sapi_module.ini_entries[ini_entries_len+1] = 0;
 
-       while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0))!=-1) {
+       while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2))!=-1) {
                switch (c) {
                        case 'c':
                                if (cli_sapi_module.php_ini_path_override) {
@@ -738,7 +738,7 @@ int main(int argc, char *argv[])
                        goto err;
                }
 
-               while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0)) != -1) {
+               while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
                        switch (c) {
 
                        case 'h': /* help & quit */
@@ -809,7 +809,7 @@ int main(int argc, char *argv[])
 
                php_optind = orig_optind;
                php_optarg = orig_optarg;
-               while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0)) != -1) {
+               while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
                        switch (c) {
 
                        case 'a':       /* interactive mode */
diff --git a/sapi/cli/php_getopt.h b/sapi/cli/php_getopt.h
deleted file mode 100644 (file)
index fc14c96..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
-   +----------------------------------------------------------------------+
-   | PHP Version 5                                                        |
-   +----------------------------------------------------------------------+
-   | Copyright (c) 1997-2007 The PHP Group                                |
-   +----------------------------------------------------------------------+
-   | This source file is subject to version 3.01 of the PHP license,      |
-   | that is bundled with this package in the file LICENSE, and is        |
-   | available through the world-wide-web at the following url:           |
-   | http://www.php.net/license/3_01.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>                               |
-   +----------------------------------------------------------------------+
-*/
-
-/* $Id$ */
-
-#include "php.h"
-
-#ifdef NETWARE
-/*
-As NetWare LibC has optind and optarg macros defined in unistd.h our local variables were getting mistakenly preprocessed so undeffing optind and optarg
-*/
-#undef optarg
-#undef optind
-#endif
-/* Define structure for one recognized option (both single char and long name).
- * If short_open is '-' this is the last option.
- */
-typedef struct _opt_struct {
-       const char opt_char;
-       const int  need_param;
-       const char * opt_name;
-} opt_struct;
-
-int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **optarg, int *optind, int show_err);
index ea607539def55d7f57da3c5e31034935499a9ce9..3c584944f555ee87c9199bacbbb9528930fe4891 100644 (file)
@@ -324,7 +324,8 @@ ADD_SOURCES("Zend", "zend_language_parser.c zend_language_scanner.c \
 ADD_SOURCES("main", "main.c snprintf.c spprintf.c fopen_wrappers.c \
        php_scandir.c php_ini.c SAPI.c rfc1867.c php_content_types.c strlcpy.c \
        strlcat.c mergesort.c reentrancy.c php_variables.c php_ticks.c network.c \
-       php_open_temporary_file.c php_logos.c output.c internal_functions.c php_sprintf.c");
+       php_open_temporary_file.c php_logos.c output.c internal_functions.c php_sprintf.c \
+       getopt.c");
 
 ADD_SOURCES("main/streams", "streams.c cast.c memory.c filter.c plain_wrapper.c \
        userspace.c transports.c xp_socket.c mmap.c unicode_filter.c glob_wrapper.c");