wrowe 00/08/09 07:54:04 Modified: src/lib/apr/include apr_getopt.h src/lib/apr/misc/unix getopt.c Log: Eliminate apr_opt*** symbols from the namespace, and allow parallel or multiple argument parsers to play with the argument list. This change forces the user to call apr_initopt followed by the usual calls to apr_getopt, with one exception. The argument to a switch (-x) option is passed to apr_getopt as the &((const char *) arg). Also, the old int rv status/switch value is now simply the switch value, and the status is returned as the ap_status_t result. Revision Changes Path 1.15 +37 -25 apache-2.0/src/lib/apr/include/apr_getopt.h Index: apr_getopt.h =================================================================== RCS file: /home/cvs/apache-2.0/src/lib/apr/include/apr_getopt.h,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- apr_getopt.h 2000/08/06 06:07:08 1.14 +++ apr_getopt.h 2000/08/09 14:54:03 1.15 @@ -55,36 +55,48 @@ #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 - * 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: + * 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 optch The next option character parsed + * @param optarg The argument following the option character: + * @tip There are four potential status values on exit. They are: *
- * 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 -- No argument followed @parameter:
+ * APR_SUCCESS -- The next option was found.
*
- * @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 */
1.19 +55 -52 apache-2.0/src/lib/apr/misc/unix/getopt.c
Index: getopt.c
===================================================================
RCS file: /home/cvs/apache-2.0/src/lib/apr/misc/unix/getopt.c,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- getopt.c 2000/08/06 06:07:16 1.18
+++ getopt.c 2000/08/09 14:54:03 1.19
@@ -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;
}