xref: /freebsd/contrib/bc/include/opt.h (revision 12e0d316644a4f80f5f1f78cf07bd93def43b1ca)
1 /*
2  * *****************************************************************************
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2018-2024 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 	const 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 	const 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
106 bc_opt_init(BcOpt* o, const char** argv);
107 
108 /**
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.
114  */
115 int
116 bc_opt_parse(BcOpt* o, const BcOptLong* longopts);
117 
118 /**
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.
122  */
123 #define BC_OPT_ISDASHDASH(a) \
124 	((a) != NULL && (a)[0] == '-' && (a)[1] == '-' && (a)[2] == '\0')
125 
126 /**
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.
130  */
131 #define BC_OPT_ISSHORTOPT(a) \
132 	((a) != NULL && (a)[0] == '-' && (a)[1] != '-' && (a)[1] != '\0')
133 
134 /**
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.
138  */
139 #define BC_OPT_ISLONGOPT(a) \
140 	((a) != NULL && (a)[0] == '-' && (a)[1] == '-' && (a)[2] != '\0')
141 
142 #endif // BC_OPT_H
143