2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2023 Gavin D. Howard and contributors.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * *****************************************************************************
32 * Adapted from https://github.com/skeeto/optparse
34 * *****************************************************************************
36 * Definitions for getopt_long() replacement.
46 /// The data required to parse command-line arguments.
49 /// The array of arguments.
52 /// The index of the current argument.
55 /// The actual parse option character.
58 /// Where in the option we are for multi-character single-character options.
61 /// The option argument.
66 /// The types of arguments. This is specially adapted for bc.
67 typedef enum BcOptType
69 /// No argument required.
72 /// An argument required.
75 /// An option that is bc-only.
78 /// An option that is bc-only that requires an argument.
79 BC_OPT_REQUIRED_BC_ONLY,
81 /// An option that is dc-only.
86 /// A struct to hold const data for long options.
87 typedef struct BcOptLong
89 /// The name of the option.
92 /// The type of the option.
95 /// The character to return if the long option was parsed.
101 * Initialize data for parsing options.
102 * @param o The option data to initialize.
103 * @param argv The array of arguments.
106 bc_opt_init(BcOpt* o, char** argv);
109 * Parse an option. This returns a value the same way getopt() and getopt_long()
110 * do, so it returns a character for the parsed option or -1 if done.
111 * @param o The option data.
112 * @param longopts The long options.
113 * @return A character for the parsed option, or -1 if done.
116 bc_opt_parse(BcOpt* o, const BcOptLong* longopts);
119 * Returns true if the option is `--` and not a long option.
120 * @param a The argument to parse.
121 * @return True if @a a is the `--` option, false otherwise.
123 #define BC_OPT_ISDASHDASH(a) \
124 ((a) != NULL && (a)[0] == '-' && (a)[1] == '-' && (a)[2] == '\0')
127 * Returns true if the option is a short option.
128 * @param a The argument to parse.
129 * @return True if @a a is a short option, false otherwise.
131 #define BC_OPT_ISSHORTOPT(a) \
132 ((a) != NULL && (a)[0] == '-' && (a)[1] != '-' && (a)[1] != '\0')
135 * Returns true if the option has `--` at the beginning, i.e., is a long option.
136 * @param a The argument to parse.
137 * @return True if @a a is a long option, false otherwise.
139 #define BC_OPT_ISLONGOPT(a) \
140 ((a) != NULL && (a)[0] == '-' && (a)[1] == '-' && (a)[2] != '\0')