1 #ifndef UTIL_LINUX_OPTUTILS_H
2 #define UTIL_LINUX_OPTUTILS_H
10 static inline const char *option_to_longopt(int c
, const struct option
*opts
)
12 const struct option
*o
;
14 assert(!(opts
== NULL
));
15 for (o
= opts
; o
->name
; o
++)
21 #ifndef OPTUTILS_EXIT_CODE
22 # define OPTUTILS_EXIT_CODE EXIT_FAILURE
26 * Check collisions between options.
28 * The conflicts between options are described in ul_excl_t array. The
29 * array contains groups of mutually exclusive options. For example
31 * static const ul_excl_t excl[] = {
32 * { 'Z','b','c' }, // first group
33 * { 'b','x' }, // second group
37 * int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
39 * while ((c = getopt_long(argc, argv, "Zbcx", longopts, NULL)) != -1) {
41 * err_exclusive_options(c, longopts, excl, excl_st);
49 * The array excl[] defines two groups of the mutually exclusive options. The
50 * option '-b' is in the both groups.
52 * Note that the options in the group have to be in ASCII order (ABC..abc..) and
53 * groups have to be also in ASCII order.
55 * The maximal number of the options in the group is 15 (size of the array is
58 * The current status of options is stored in excl_st array. The size of the array
59 * must be the same as number of the groups in the ul_excl_t array.
61 * If you're unsure then see sys-utils/mount.c or misc-utils/findmnt.c.
63 #define UL_EXCL_STATUS_INIT { 0 }
64 typedef int ul_excl_t
[16];
66 static inline void err_exclusive_options(
68 const struct option
*opts
,
69 const ul_excl_t
*excl
,
74 for (e
= 0; excl
[e
][0] && excl
[e
][0] <= c
; e
++) {
75 const int *op
= excl
[e
];
77 for (; *op
&& *op
<= c
; op
++) {
82 else if (status
[e
] != c
) {
85 fprintf(stderr
, _("%s: mutually exclusive "
87 program_invocation_short_name
);
90 ct
+ 1 < ARRAY_SIZE(excl
[0]) && *op
;
92 const char *n
= option_to_longopt(*op
, opts
);
94 fprintf(stderr
, " --%s", n
);
95 else if (c_isgraph(*op
))
96 fprintf(stderr
, " -%c", *op
);
99 exit(OPTUTILS_EXIT_CODE
);