wrowe 01/11/28 22:45:35
Modified: include apr_getopt.h
misc/unix getopt.c
Log:
Change getopt_t ->err flag to a real ->errfn that mirrors fprintf,
introduce an ->errarg to pass the first void* (e.g. stderr by default),
and change apr_getopt_init() to initialize those to fprintf(stderr,
so the -default- behavior is unchanged.
This is necessary to get away from unwise assumptions within library code.
Revision Changes Path
1.35 +7 -2 apr/include/apr_getopt.h
Index: apr_getopt.h
===================================================================
RCS file: /home/cvs/apr/include/apr_getopt.h,v
retrieving revision 1.34
retrieving revision 1.35
diff -u -r1.34 -r1.35
--- apr_getopt.h 2001/08/14 04:05:16 1.34
+++ apr_getopt.h 2001/11/29 06:45:34 1.35
@@ -71,6 +71,8 @@
* @{
*/
+typedef void (apr_getopt_err_fn_t)(void *arg, const char *err, ...);
+
typedef struct apr_getopt_t apr_getopt_t;
/**
* Structure to store command line argument information.
@@ -78,8 +80,10 @@
struct apr_getopt_t {
/** context for processing */
apr_pool_t *cont;
- /** if error message should be printed */
- int err;
+ /** function to print error message (NULL == no messages) */
+ apr_getopt_err_fn_t *errfn;
+ /** user defined first arg to pass to error message */
+ void *errarg;
/** index into parent argv vector */
int ind;
/** character checked for validity */
@@ -121,6 +125,7 @@
* @param argc The number of arguments to parse
* @param argv The array of arguments to parse
* @remark Arguments 2 and 3 are most commonly argc and argv from main(argc,
argv)
+ * The errfn is initialized to fprintf(stderr... but may be overridden.
*/
APR_DECLARE(apr_status_t) apr_getopt_init(apr_getopt_t **os, apr_pool_t
*cont,
int argc, const char * const *argv);
1.35 +16 -25 apr/misc/unix/getopt.c
Index: getopt.c
===================================================================
RCS file: /home/cvs/apr/misc/unix/getopt.c,v
retrieving revision 1.34
retrieving revision 1.35
diff -u -r1.34 -r1.35
--- getopt.c 2001/07/19 04:13:05 1.34
+++ getopt.c 2001/11/29 06:45:35 1.35
@@ -33,21 +33,10 @@
#include "misc.h"
#include "apr_strings.h"
+#include "apr_lib.h"
#define EMSG ""
-/* Regardless of what we're invoked as, just print out the last part
- * of the path */
-static const char *pretty_path (const char *name)
-{
- const char *p;
-
- if (!(p = strrchr(name, '/')))
- return name;
- else
- return p + 1;
-}
-
APR_DECLARE(apr_status_t) apr_getopt_init(apr_getopt_t **os, apr_pool_t
*cont,
int argc, const char *const *argv)
{
@@ -56,7 +45,9 @@
*os = apr_palloc(cont, sizeof(apr_getopt_t));
(*os)->cont = cont;
(*os)->reset = 0;
- (*os)->err = 1;
+ (*os)->errfn = (apr_getopt_err_fn_t*)(fprintf);
+ (*os)->errarg = (void*)(stderr);
+
(*os)->place = EMSG;
(*os)->argc = argc;
@@ -108,10 +99,9 @@
}
if (!*os->place)
++os->ind;
- if (os->err && *opts != ':') {
- (void) fprintf(stderr,
- "%s: illegal option -- %c\n",
- pretty_path(*os->argv), os->opt);
+ if (os->errfn && *opts != ':') {
+ (os->errfn)(os->errarg, "%s: illegal option -- %c\n",
+ apr_filename_of_pathname(*os->argv), os->opt);
}
*optch = os->opt;
return (APR_BADCH);
@@ -130,10 +120,9 @@
*optch = os->opt;
return (APR_BADARG);
}
- if (os->err) {
- (void) fprintf(stderr,
- "%s: option requires an argument -- %c\n",
- pretty_path(*os->argv), os->opt);
+ if (os->errfn) {
+ (os->errfn)(os->errarg, "%s: option requires an argument --
%c\n",
+ apr_filename_of_pathname(*os->argv), os->opt);
}
*optch = os->opt;
return (APR_BADCH);
@@ -191,8 +180,9 @@
static apr_status_t serr(apr_getopt_t *os, const char *err, const char *str,
apr_status_t status)
{
- if (os->err)
- fprintf(stderr, "%s: %s: %s\n", pretty_path(*os->argv), err, str);
+ if (os->errfn)
+ (os->errfn)(os->errarg, "%s: %s: %s\n",
+ apr_filename_of_pathname(*os->argv), err, str);
return status;
}
@@ -200,8 +190,9 @@
static apr_status_t cerr(apr_getopt_t *os, const char *err, int ch,
apr_status_t status)
{
- if (os->err)
- fprintf(stderr, "%s: %s: %c\n", pretty_path(*os->argv), err, ch);
+ if (os->errfn)
+ (os->errfn)(os->errarg, "%s: %s: %c\n",
+ apr_filename_of_pathname(*os->argv), err, ch);
return status;
}