1251875Speter/* Licensed to the Apache Software Foundation (ASF) under one or more
2251875Speter * contributor license agreements.  See the NOTICE file distributed with
3251875Speter * this work for additional information regarding copyright ownership.
4251875Speter * The ASF licenses this file to You under the Apache License, Version 2.0
5251875Speter * (the "License"); you may not use this file except in compliance with
6251875Speter * the License.  You may obtain a copy of the License at
7251875Speter *
8251875Speter *     http://www.apache.org/licenses/LICENSE-2.0
9251875Speter *
10251875Speter * Unless required by applicable law or agreed to in writing, software
11251875Speter * distributed under the License is distributed on an "AS IS" BASIS,
12251875Speter * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13251875Speter * See the License for the specific language governing permissions and
14251875Speter * limitations under the License.
15251875Speter */
16251875Speter
17251875Speter#ifndef APR_GETOPT_H
18251875Speter#define APR_GETOPT_H
19251875Speter
20251875Speter/**
21251875Speter * @file apr_getopt.h
22251875Speter * @brief APR Command Arguments (getopt)
23251875Speter */
24251875Speter
25251875Speter#include "apr_pools.h"
26251875Speter
27251875Speter#ifdef __cplusplus
28251875Speterextern "C" {
29251875Speter#endif /* __cplusplus */
30251875Speter
31251875Speter/**
32251875Speter * @defgroup apr_getopt Command Argument Parsing
33251875Speter * @ingroup APR
34251875Speter * @{
35251875Speter */
36251875Speter
37251875Speter/**
38251875Speter * An @c apr_getopt_t error callback function.
39251875Speter *
40251875Speter * @a arg is this @c apr_getopt_t's @c errarg member.
41251875Speter */
42251875Spetertypedef void (apr_getopt_err_fn_t)(void *arg, const char *err, ...);
43251875Speter
44251875Speter/** @see apr_getopt_t */
45251875Spetertypedef struct apr_getopt_t apr_getopt_t;
46251875Speter
47251875Speter/**
48251875Speter * Structure to store command line argument information.
49251875Speter */
50251875Speterstruct apr_getopt_t {
51251875Speter    /** context for processing */
52251875Speter    apr_pool_t *cont;
53251875Speter    /** function to print error message (NULL == no messages) */
54251875Speter    apr_getopt_err_fn_t *errfn;
55251875Speter    /** user defined first arg to pass to error message  */
56251875Speter    void *errarg;
57251875Speter    /** index into parent argv vector */
58251875Speter    int ind;
59251875Speter    /** character checked for validity */
60251875Speter    int opt;
61251875Speter    /** reset getopt */
62251875Speter    int reset;
63251875Speter    /** count of arguments */
64251875Speter    int argc;
65251875Speter    /** array of pointers to arguments */
66251875Speter    const char **argv;
67251875Speter    /** argument associated with option */
68251875Speter    char const* place;
69251875Speter    /** set to nonzero to support interleaving options with regular args */
70251875Speter    int interleave;
71251875Speter    /** start of non-option arguments skipped for interleaving */
72251875Speter    int skip_start;
73251875Speter    /** end of non-option arguments skipped for interleaving */
74251875Speter    int skip_end;
75251875Speter};
76251875Speter
77251875Speter/** @see apr_getopt_option_t */
78251875Spetertypedef struct apr_getopt_option_t apr_getopt_option_t;
79251875Speter
80251875Speter/**
81251875Speter * Structure used to describe options that getopt should search for.
82251875Speter */
83251875Speterstruct apr_getopt_option_t {
84251875Speter    /** long option name, or NULL if option has no long name */
85251875Speter    const char *name;
86251875Speter    /** option letter, or a value greater than 255 if option has no letter */
87251875Speter    int optch;
88251875Speter    /** nonzero if option takes an argument */
89251875Speter    int has_arg;
90251875Speter    /** a description of the option */
91251875Speter    const char *description;
92251875Speter};
93251875Speter
94251875Speter/**
95251875Speter * Initialize the arguments for parsing by apr_getopt().
96251875Speter * @param os   The options structure created for apr_getopt()
97251875Speter * @param cont The pool to operate on
98251875Speter * @param argc The number of arguments to parse
99251875Speter * @param argv The array of arguments to parse
100251875Speter * @remark Arguments 3 and 4 are most commonly argc and argv from main(argc, argv)
101251875Speter * The (*os)->errfn is initialized to fprintf(stderr... but may be overridden.
102251875Speter */
103251875SpeterAPR_DECLARE(apr_status_t) apr_getopt_init(apr_getopt_t **os, apr_pool_t *cont,
104251875Speter                                      int argc, const char * const *argv);
105251875Speter
106251875Speter/**
107251875Speter * Parse the options initialized by apr_getopt_init().
108251875Speter * @param os     The apr_opt_t structure returned by apr_getopt_init()
109251875Speter * @param opts   A string of characters that are acceptable options to the
110251875Speter *               program.  Characters followed by ":" are required to have an
111251875Speter *               option associated
112251875Speter * @param option_ch  The next option character parsed
113251875Speter * @param option_arg The argument following the option character:
114251875Speter * @return There are four potential status values on exit. They are:
115251875Speter * <PRE>
116251875Speter *             APR_EOF      --  No more options to parse
117251875Speter *             APR_BADCH    --  Found a bad option character
118251875Speter *             APR_BADARG   --  No argument followed the option flag
119251875Speter *             APR_SUCCESS  --  The next option was found.
120251875Speter * </PRE>
121251875Speter */
122251875SpeterAPR_DECLARE(apr_status_t) apr_getopt(apr_getopt_t *os, const char *opts,
123251875Speter                                     char *option_ch, const char **option_arg);
124251875Speter
125251875Speter/**
126251875Speter * Parse the options initialized by apr_getopt_init(), accepting long
127251875Speter * options beginning with "--" in addition to single-character
128251875Speter * options beginning with "-".
129251875Speter * @param os     The apr_getopt_t structure created by apr_getopt_init()
130251875Speter * @param opts   A pointer to a list of apr_getopt_option_t structures, which
131251875Speter *               can be initialized with { "name", optch, has_args }.  has_args
132251875Speter *               is nonzero if the option requires an argument.  A structure
133251875Speter *               with an optch value of 0 terminates the list.
134251875Speter * @param option_ch  Receives the value of "optch" from the apr_getopt_option_t
135251875Speter *                   structure corresponding to the next option matched.
136251875Speter * @param option_arg Receives the argument following the option, if any.
137251875Speter * @return There are four potential status values on exit.   They are:
138251875Speter * <PRE>
139251875Speter *             APR_EOF      --  No more options to parse
140251875Speter *             APR_BADCH    --  Found a bad option character
141251875Speter *             APR_BADARG   --  No argument followed the option flag
142251875Speter *             APR_SUCCESS  --  The next option was found.
143251875Speter * </PRE>
144251875Speter * When APR_SUCCESS is returned, os->ind gives the index of the first
145251875Speter * non-option argument.  On error, a message will be printed to stdout unless
146251875Speter * os->err is set to 0.  If os->interleave is set to nonzero, options can come
147251875Speter * after arguments, and os->argv will be permuted to leave non-option arguments
148251875Speter * at the end (the original argv is unaffected).
149251875Speter */
150251875SpeterAPR_DECLARE(apr_status_t) apr_getopt_long(apr_getopt_t *os,
151251875Speter					  const apr_getopt_option_t *opts,
152251875Speter					  int *option_ch,
153251875Speter                                          const char **option_arg);
154251875Speter/** @} */
155251875Speter
156251875Speter#ifdef __cplusplus
157251875Speter}
158251875Speter#endif
159251875Speter
160251875Speter#endif  /* ! APR_GETOPT_H */
161