2 * No copyright is claimed. This code is in the public domain; do with
5 #ifndef UTIL_LINUX_OPTUTILS_H
6 #define UTIL_LINUX_OPTUTILS_H
14 static inline const char *option_to_longopt(int c
, const struct option
*opts
)
16 const struct option
*o
;
18 assert(!(opts
== NULL
));
19 for (o
= opts
; o
->name
; o
++)
25 #ifndef OPTUTILS_EXIT_CODE
26 # define OPTUTILS_EXIT_CODE EXIT_FAILURE
30 * Check collisions between options.
32 * The conflicts between options are described in ul_excl_t array. The
33 * array contains groups of mutually exclusive options. For example
35 * static const ul_excl_t excl[] = {
36 * { 'Z','b','c' }, // first group
37 * { 'b','x' }, // second group
41 * int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
43 * while ((c = getopt_long(argc, argv, "Zbcx", longopts, NULL)) != -1) {
45 * err_exclusive_options(c, longopts, excl, excl_st);
53 * The array excl[] defines two groups of the mutually exclusive options. The
54 * option '-b' is in the both groups.
56 * Note that the options in the group have to be in ASCII order (ABC..abc..) and
57 * groups have to be also in ASCII order.
59 * The maximal number of the options in the group is 15 (size of the array is
62 * The current status of options is stored in excl_st array. The size of the array
63 * must be the same as number of the groups in the ul_excl_t array.
65 * If you're unsure then see sys-utils/mount.c or misc-utils/findmnt.c.
67 #define UL_EXCL_STATUS_INIT { 0 }
68 typedef int ul_excl_t
[16];
70 static inline void err_exclusive_options(
72 const struct option
*opts
,
73 const ul_excl_t
*excl
,
78 for (e
= 0; excl
[e
][0] && excl
[e
][0] <= c
; e
++) {
79 const int *op
= excl
[e
];
81 for (; *op
&& *op
<= c
; op
++) {
86 else if (status
[e
] != c
) {
89 fprintf(stderr
, _("%s: mutually exclusive "
91 program_invocation_short_name
);
94 ct
+ 1 < ARRAY_SIZE(excl
[0]) && *op
;
96 const char *n
= option_to_longopt(*op
, opts
);
98 fprintf(stderr
, " --%s", n
);
99 else if (c_isgraph(*op
))
100 fprintf(stderr
, " -%c", *op
);
103 exit(OPTUTILS_EXIT_CODE
);