httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "William A. Rowe, Jr." <wr...@lnd.com>
Subject Global data is evil :)
Date Mon, 07 Aug 2000 05:05:18 GMT
Does anyone object to this untested (but compiling) revision?
Assuming the answer is no, I will commit it Monday eve, with the
appropriate changes to http_main.c and mpm/win32/service.c

Bill

(If you are wondering why opts is a apr_getopt() argument, and not
 part of the apr_getopt_t I'm picturing a scenario where -q lets 
 you use -a, while -a first or by itself might be invalid.)

Index: include/apr_getopt.h
===================================================================
RCS file: /home/cvs/apache-2.0/src/lib/apr/include/apr_getopt.h,v
retrieving revision 1.14
diff -u -r1.14 apr_getopt.h
--- include/apr_getopt.h	2000/08/06 06:07:08	1.14
+++ include/apr_getopt.h	2000/08/07 05:04:46
@@ -55,36 +55,47 @@
 #ifndef APR_GETOPT_H
 #define APR_GETOPT_H
 
-APR_VAR_IMPORT int
-    apr_opterr,                          /* if error message should be printed */
-    apr_optind,                          /* index into parent argv vector */
-    apr_optopt,                          /* character checked for validity */
-    apr_optreset;                        /* reset getopt */
-APR_VAR_IMPORT char *
-    apr_optarg;                          /* argument associated with option */
+typedef struct apr_getopt_t {
+    apr_pool_t cont;         /* context for processing */
+    int err;                 /* if error message should be printed */
+    int ind;                 /* index into parent argv vector */
+    int opt;                 /* character checked for validity */
+    int reset;               /* reset getopt */
+    int argc;                /* count of arguments */
+    char const* const* argv; /* array of pointers to arguments */
+    char const* place;       /* argument associated with option */
+} apr_getopt_t;
 
 /**
- * Parse the command line options passed to the program.
- * @param nargc The number of arguments passed to apr_getopt to parse
- * @param nargv The array of command line options to parse
- * @param ostr A string of characters that are acceptable options to the 
+ * Initialize the arguments for parsing by apr_getopt().
+ * @param cont The pool to operate on
+ * @param os   The options structure created for apr_getopt()
+ * @param argc The number of arguments to parse
+ * @param argv The array of arguments to parse
+ * @tip Arguments 2 and 3 are most commonly argc and argv from main(argc, argv)
+ * @deffunc apr_status_t apr_initopt(apr_getopt_t **os, apr_pool_t *cont, int argc, char
const* const* argv)
+ */
+APR_EXPORT(apr_status_t) apr_initopt(apr_getopt_t **os, apr_pool_t *cont,
+                                     int argc, char const* const* argv);
+
+/**
+ * Parse the options initialized by apr_initopt().
+ * @param os   The apr_opt_t structure returned by apr_initopt()
+ * @param opts A string of characters that are acceptable options to the 
  *             program.  Characters followed by ":" are required to have an 
- *             option associated 
- * @param rv The next option found.  There are four potential values for 
- *          this variable on exit. They are:
+ *             option associated
+ * @param rv   The next option character parsed.
+ * @tip There are four potential status values on exit. They are:
  * <PRE>
- *             APR_EOF    --  No more options to parse
- *             APR_BADCH  --  Found a bad option character
- *             APR_BADARG --  Missing @parameter for the found option
- *             Other      --  The next option found.
+ *             APR_EOF      --  No more options to parse
+ *             APR_BADCH    --  Found a bad option character
+ *             APR_BADARG   --  Missing @parameter for rv
+ *             APR_SUCCESS  --  The next option was found.
  * </PRE>
- * @param cont The pool to operate on.
- * @tip Arguments 2 and 3 are most commonly argc and argv from main(argc, argv)
- * @deffunc apr_status_t apr_getopt(apr_int32_t nargc, char *const *nargv, const char *ostr,
apr_int32_t *rv, apr_pool_t *cont)
+ * @deffunc apr_status_t apr_getopt(apr_getopt_t *os, const char *opts, char *optch, char
**optarg)
  */
-APR_EXPORT(apr_status_t) apr_getopt(apr_int32_t nargc, char *const *nargv, 
-                                  const char *ostr, apr_int32_t *rv, 
-                                  apr_pool_t *cont);
+APR_EXPORT(apr_status_t) apr_getopt(apr_getopt_t *os, const char *opts, 
+                                    char *optch, char const** optarg);
 
 #endif  /* ! APR_GETOPT_H */
 
Index: misc/unix/getopt.c
===================================================================
RCS file: /home/cvs/apache-2.0/src/lib/apr/misc/unix/getopt.c,v
retrieving revision 1.18
diff -u -r1.18 getopt.c
--- misc/unix/getopt.c	2000/08/06 06:07:16	1.18
+++ misc/unix/getopt.c	2000/08/07 05:04:46
@@ -33,92 +33,95 @@
 
 #include "misc.h"
 
-APR_VAR_EXPORT int
-    apr_opterr = 1,                      /* if error message should be printed */
-    apr_optind = 1,                      /* index into parent argv vector */
-    apr_optopt,                          /* character checked for validity */
-    apr_optreset;                        /* reset getopt */
-APR_VAR_EXPORT char *apr_optarg = "";    /* argument associated with option */
-
 #define EMSG    ""
+
+APR_EXPORT(apr_status_t) apr_initopt(apr_getopt_t **os, apr_pool_t *cont,
+                                     int argc, char const* const* argv)
+{
+    *os = apr_palloc(cont, sizeof(apr_getopt_t));
+    (*os)->err = 1;
+    (*os)->ind = 1;
+    (*os)->place = EMSG;
+    (*os)->argc = argc;
+    (*os)->argv = argv;
+    return APR_SUCCESS;
+}
 
-APR_EXPORT(apr_status_t) apr_getopt(apr_int32_t nargc, char *const *nargv, 
-                                    const char *ostr, apr_int32_t *rv, 
-                                    apr_pool_t *cont)
+APR_EXPORT(apr_status_t) apr_getopt(apr_getopt_t *os, const char *opts, 
+                                    char *optch, char const** optarg)
 {
-    char *p;
-    static char *place = EMSG;   /* option letter processing */
-    char *oli;                   /* option letter list index */
+    const char *p;
+    const char *oli;  /* option letter list index */
 
-    if (apr_optreset || !*place) {   /* update scanning pointer */
-        apr_optreset = 0;
-        if (apr_optind >= nargc || *(place = nargv[apr_optind]) != '-') {
-            place = EMSG;
-            *rv = apr_optopt;
+    if (os->reset || !*os->place) {   /* update scanning pointer */
+        os->reset = 0;
+        if (os->ind >= os->argc || *(os->place = os->argv[os->ind]) !=
'-') {
+            os->place = EMSG;
+            *optch = os->opt;
             return (APR_EOF);
         }
-        if (place[1] && *++place == '-') {        /* found "--" */
-            ++apr_optind;
-            place = EMSG;
-            *rv = apr_optopt;
+        if (os->place[1] && *++os->place == '-') {        /* found "--" */
+            ++os->ind;
+            os->place = EMSG;
+            *optch = os->opt;
             return (APR_EOF);
         }
     }                                /* option letter okay? */
-    if ((apr_optopt = (int) *place++) == (int) ':' ||
-        !(oli = strchr(ostr, apr_optopt))) {
+    if ((os->opt = (int) *os->place++) == (int) ':' ||
+        !(oli = strchr(opts, os->opt))) {
         /*
          * if the user didn't specify '-' as an option,
          * assume it means -1.
          */
-        if (apr_optopt == (int) '-') {
-            *rv = apr_optopt;
+        if (os->opt == (int) '-') {
+            *optch = os->opt;
             return (APR_EOF);
         }
-        if (!*place)
-            ++apr_optind;
-        if (apr_opterr && *ostr != ':') {
-            if (!(p = strrchr(*nargv, '/')))
-                p = *nargv;
+        if (!*os->place)
+            ++os->ind;
+        if (os->err && *opts != ':') {
+            if (!(p = strrchr(*os->argv, '/')))
+                p = *os->argv;
             else
                 ++p;
             (void) fprintf(stderr,
-                           "%s: illegal option -- %c\n", p, apr_optopt);
+                           "%s: illegal option -- %c\n", p, os->opt);
         }
-        *rv = apr_optopt;
-        return APR_BADCH;
+        *optch = os->opt;
+        return (APR_BADCH);
     }
     if (*++oli != ':') {        /* don't need argument */
-        apr_optarg = NULL;
-        if (!*place)
-            ++apr_optind;
+        *optarg = NULL;
+        if (!*os->place)
+            ++os->ind;
     }
     else {                        /* need an argument */
-        if (*place)                /* no white space */
-            apr_optarg = place;
-        else if (nargc <= ++apr_optind) {        /* no arg */
-            place = EMSG;
-            if (*ostr == ':') {
-                *rv = apr_optopt;
+        if (*os->place)                /* no white space */
+            *optarg = os->place;
+        else if (os->argc <= ++os->ind) {        /* no arg */
+            os->place = EMSG;
+            if (*opts == ':') {
+                *optch = os->opt;
                 return (APR_BADARG);
             }
-            if (apr_opterr) {
-                if (!(p = strrchr(*nargv, '/')))
-                    p = *nargv;
+            if (os->err) {
+                if (!(p = strrchr(*os->argv, '/')))
+                    p = *os->argv;
                 else
                     ++p;
                 (void) fprintf(stderr,
                                "%s: option requires an argument -- %c\n",
-                               p, apr_optopt);
+                               p, os->opt);
             }
-            *rv = apr_optopt;
+            *optch = os->opt;
             return (APR_BADCH);
         }
         else                        /* white space */
-            apr_optarg = nargv[apr_optind];
-        place = EMSG;
-        ++apr_optind;
+            *optarg = os->argv[os->ind];
+        os->place = EMSG;
+        ++os->ind;
     }
-    *rv = apr_optopt;
+    *optch = os->opt;
     return APR_SUCCESS;
 }
 


Mime
View raw message