xref: /freebsd/contrib/bc/include/opt.h (revision 3aa99676b43a74a61fae17e19fd554e1937fa746)
1252884aeSStefan Eßer /*
2252884aeSStefan Eßer  * *****************************************************************************
3252884aeSStefan Eßer  *
4*3aa99676SStefan Eßer  * SPDX-License-Identifier: BSD-2-Clause
5252884aeSStefan Eßer  *
6*3aa99676SStefan Eßer  * Copyright (c) 2018-2020 Gavin D. Howard and contributors.
7252884aeSStefan Eßer  *
8252884aeSStefan Eßer  * Redistribution and use in source and binary forms, with or without
9252884aeSStefan Eßer  * modification, are permitted provided that the following conditions are met:
10252884aeSStefan Eßer  *
11252884aeSStefan Eßer  * * Redistributions of source code must retain the above copyright notice, this
12252884aeSStefan Eßer  *   list of conditions and the following disclaimer.
13252884aeSStefan Eßer  *
14252884aeSStefan Eßer  * * Redistributions in binary form must reproduce the above copyright notice,
15252884aeSStefan Eßer  *   this list of conditions and the following disclaimer in the documentation
16252884aeSStefan Eßer  *   and/or other materials provided with the distribution.
17252884aeSStefan Eßer  *
18252884aeSStefan Eßer  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19252884aeSStefan Eßer  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20252884aeSStefan Eßer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21252884aeSStefan Eßer  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22252884aeSStefan Eßer  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23252884aeSStefan Eßer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24252884aeSStefan Eßer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25252884aeSStefan Eßer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26252884aeSStefan Eßer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27252884aeSStefan Eßer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28252884aeSStefan Eßer  * POSSIBILITY OF SUCH DAMAGE.
29252884aeSStefan Eßer  *
30252884aeSStefan Eßer  * *****************************************************************************
31252884aeSStefan Eßer  *
32252884aeSStefan Eßer  * Adapted from https://github.com/skeeto/optparse
33252884aeSStefan Eßer  *
34252884aeSStefan Eßer  * *****************************************************************************
35252884aeSStefan Eßer  *
36252884aeSStefan Eßer  * Definitions for getopt_long() replacement.
37252884aeSStefan Eßer  *
38252884aeSStefan Eßer  */
39252884aeSStefan Eßer 
40252884aeSStefan Eßer #ifndef BC_OPT_H
41252884aeSStefan Eßer #define BC_OPT_H
42252884aeSStefan Eßer 
43252884aeSStefan Eßer #include <stdbool.h>
44252884aeSStefan Eßer 
45252884aeSStefan Eßer typedef struct BcOpt {
46252884aeSStefan Eßer 	char **argv;
47252884aeSStefan Eßer 	size_t optind;
48252884aeSStefan Eßer 	int optopt;
49252884aeSStefan Eßer 	int subopt;
50252884aeSStefan Eßer 	char *optarg;
51252884aeSStefan Eßer } BcOpt;
52252884aeSStefan Eßer 
53252884aeSStefan Eßer typedef enum BcOptType {
54252884aeSStefan Eßer 	BC_OPT_NONE,
55252884aeSStefan Eßer 	BC_OPT_REQUIRED,
56252884aeSStefan Eßer 	BC_OPT_BC_ONLY,
57252884aeSStefan Eßer 	BC_OPT_DC_ONLY,
58252884aeSStefan Eßer } BcOptType;
59252884aeSStefan Eßer 
60252884aeSStefan Eßer typedef struct BcOptLong {
61252884aeSStefan Eßer 	const char *name;
62252884aeSStefan Eßer 	BcOptType type;
63252884aeSStefan Eßer 	int val;
64252884aeSStefan Eßer } BcOptLong;
65252884aeSStefan Eßer 
66252884aeSStefan Eßer void bc_opt_init(BcOpt *o, char **argv);
67252884aeSStefan Eßer 
68252884aeSStefan Eßer int bc_opt_parse(BcOpt *o, const BcOptLong *longopts);
69252884aeSStefan Eßer 
70252884aeSStefan Eßer #define BC_OPT_ISDASHDASH(a) \
71252884aeSStefan Eßer 	((a) != NULL && (a)[0] == '-' && (a)[1] == '-' && (a)[2] == '\0')
72252884aeSStefan Eßer 
73252884aeSStefan Eßer #define BC_OPT_ISSHORTOPT(a) \
74252884aeSStefan Eßer 	((a) != NULL && (a)[0] == '-' && (a)[1] != '-' && (a)[1] != '\0')
75252884aeSStefan Eßer 
76252884aeSStefan Eßer #define BC_OPT_ISLONGOPT(a) \
77252884aeSStefan Eßer 	((a) != NULL && (a)[0] == '-' && (a)[1] == '-' && (a)[2] != '\0')
78252884aeSStefan Eßer 
79252884aeSStefan Eßer #endif // BC_OPT_H
80