xref: /freebsd/contrib/bc/include/opt.h (revision 105fd928b0b5b35ab529e5f6914788dc49582901)
1 /*
2  * *****************************************************************************
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2018-2021 Gavin D. Howard and contributors.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * * Redistributions of source code must retain the above copyright notice, this
12  *   list of conditions and the following disclaimer.
13  *
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.
17  *
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.
29  *
30  * *****************************************************************************
31  *
32  * Adapted from https://github.com/skeeto/optparse
33  *
34  * *****************************************************************************
35  *
36  * Definitions for getopt_long() replacement.
37  *
38  */
39 
40 #ifndef BC_OPT_H
41 #define BC_OPT_H
42 
43 #include <stdbool.h>
44 #include <stdlib.h>
45 
46 /// The data required to parse command-line arguments.
47 typedef struct BcOpt {
48 
49 	/// The array of arguments.
50 	char **argv;
51 
52 	/// The index of the current argument.
53 	size_t optind;
54 
55 	/// The actual parse option character.
56 	int optopt;
57 
58 	/// Where in the option we are for multi-character single-character options.
59 	int subopt;
60 
61 	/// The option argument.
62 	char *optarg;
63 
64 } BcOpt;
65 
66 /// The types of arguments. This is specially adapted for bc.
67 typedef enum BcOptType {
68 
69 	/// No argument required.
70 	BC_OPT_NONE,
71 
72 	/// An argument required.
73 	BC_OPT_REQUIRED,
74 
75 	/// An option that is bc-only.
76 	BC_OPT_BC_ONLY,
77 
78 	/// An option that is bc-only that requires an argument.
79 	BC_OPT_REQUIRED_BC_ONLY,
80 
81 	/// An option that is dc-only.
82 	BC_OPT_DC_ONLY,
83 
84 } BcOptType;
85 
86 /// A struct to hold const data for long options.
87 typedef struct BcOptLong {
88 
89 	/// The name of the option.
90 	const char *name;
91 
92 	/// The type of the option.
93 	BcOptType type;
94 
95 	/// The character to return if the long option was parsed.
96 	int val;
97 
98 } BcOptLong;
99 
100 /**
101  * Initialize data for parsing options.
102  * @param o     The option data to initialize.
103  * @param argv  The array of arguments.
104  */
105 void bc_opt_init(BcOpt *o, char **argv);
106 
107 /**
108  * Parse an option. This returns a value the same way getopt() and getopt_long()
109  * do, so it returns a character for the parsed option or -1 if done.
110  * @param o         The option data.
111  * @param longopts  The long options.
112  * @return          A character for the parsed option, or -1 if done.
113  */
114 int bc_opt_parse(BcOpt *o, const BcOptLong *longopts);
115 
116 /**
117  * Returns true if the option is `--` and not a long option.
118  * @param a  The argument to parse.
119  * @return   True if @a a is the `--` option, false otherwise.
120  */
121 #define BC_OPT_ISDASHDASH(a) \
122 	((a) != NULL && (a)[0] == '-' && (a)[1] == '-' && (a)[2] == '\0')
123 
124 /**
125  * Returns true if the option is a short option.
126  * @param a  The argument to parse.
127  * @return   True if @a a is a short option, false otherwise.
128  */
129 #define BC_OPT_ISSHORTOPT(a) \
130 	((a) != NULL && (a)[0] == '-' && (a)[1] != '-' && (a)[1] != '\0')
131 
132 /**
133  * Returns true if the option has `--` at the beginning, i.e., is a long option.
134  * @param a  The argument to parse.
135  * @return   True if @a a is a long option, false otherwise.
136  */
137 #define BC_OPT_ISLONGOPT(a) \
138 	((a) != NULL && (a)[0] == '-' && (a)[1] == '-' && (a)[2] != '\0')
139 
140 #endif // BC_OPT_H
141