2011-11-29 11:21:49 -06:00
|
|
|
#ifndef UTIL_LINUX_OPTUTILS_H
|
|
|
|
#define UTIL_LINUX_OPTUTILS_H
|
|
|
|
|
2017-03-05 14:52:28 -06:00
|
|
|
#include <assert.h>
|
|
|
|
|
2012-06-14 13:17:51 -05:00
|
|
|
#include "c.h"
|
|
|
|
#include "nls.h"
|
2017-04-18 19:57:27 -05:00
|
|
|
#include "cctype.h"
|
2012-06-14 13:17:51 -05:00
|
|
|
|
2011-11-29 11:21:49 -06:00
|
|
|
static inline const char *option_to_longopt(int c, const struct option *opts)
|
|
|
|
{
|
|
|
|
const struct option *o;
|
|
|
|
|
2017-03-05 14:52:28 -06:00
|
|
|
assert(!(opts == NULL));
|
2011-11-29 11:21:49 -06:00
|
|
|
for (o = opts; o->name; o++)
|
|
|
|
if (o->val == c)
|
|
|
|
return o->name;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-06-14 13:17:51 -05:00
|
|
|
#ifndef OPTUTILS_EXIT_CODE
|
|
|
|
# define OPTUTILS_EXIT_CODE EXIT_FAILURE
|
|
|
|
#endif
|
2012-07-26 02:04:55 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check collisions between options.
|
|
|
|
*
|
|
|
|
* The conflicts between options are described in ul_excl_t array. The
|
|
|
|
* array contains groups of mutually exclusive options. For example
|
|
|
|
*
|
|
|
|
* static const ul_excl_t excl[] = {
|
|
|
|
* { 'Z','b','c' }, // first group
|
|
|
|
* { 'b','x' }, // second group
|
|
|
|
* { 0 }
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
|
|
|
|
*
|
|
|
|
* while ((c = getopt_long(argc, argv, "Zbcx", longopts, NULL)) != -1) {
|
|
|
|
*
|
|
|
|
* err_exclusive_options(c, longopts, excl, excl_st);
|
|
|
|
*
|
|
|
|
* switch (c) {
|
|
|
|
* case 'Z':
|
|
|
|
* ....
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* The array excl[] defines two groups of the mutually exclusive options. The
|
|
|
|
* option '-b' is in the both groups.
|
|
|
|
*
|
|
|
|
* Note that the options in the group have to be in ASCII order (ABC..abc..) and
|
|
|
|
* groups have to be also in ASCII order.
|
|
|
|
*
|
2013-03-13 07:04:02 -05:00
|
|
|
* The maximal number of the options in the group is 15 (size of the array is
|
|
|
|
* 16, last is zero).
|
|
|
|
*
|
2012-07-26 02:04:55 -05:00
|
|
|
* The current status of options is stored in excl_st array. The size of the array
|
|
|
|
* must be the same as number of the groups in the ul_excl_t array.
|
|
|
|
*
|
|
|
|
* If you're unsure then see sys-utils/mount.c or misc-utils/findmnt.c.
|
|
|
|
*/
|
|
|
|
#define UL_EXCL_STATUS_INIT { 0 }
|
|
|
|
typedef int ul_excl_t[16];
|
|
|
|
|
|
|
|
static inline void err_exclusive_options(
|
|
|
|
int c,
|
|
|
|
const struct option *opts,
|
|
|
|
const ul_excl_t *excl,
|
|
|
|
int *status)
|
|
|
|
{
|
|
|
|
int e;
|
|
|
|
|
|
|
|
for (e = 0; excl[e][0] && excl[e][0] <= c; e++) {
|
|
|
|
const int *op = excl[e];
|
|
|
|
|
|
|
|
for (; *op && *op <= c; op++) {
|
|
|
|
if (*op != c)
|
|
|
|
continue;
|
|
|
|
if (status[e] == 0)
|
|
|
|
status[e] = c;
|
|
|
|
else if (status[e] != c) {
|
2013-03-13 07:04:02 -05:00
|
|
|
size_t ct = 0;
|
|
|
|
|
2017-04-18 19:57:27 -05:00
|
|
|
fprintf(stderr, _("%s: mutually exclusive "
|
|
|
|
"arguments:"),
|
2012-07-26 02:04:55 -05:00
|
|
|
program_invocation_short_name);
|
2013-03-13 07:04:02 -05:00
|
|
|
|
|
|
|
for (op = excl[e];
|
|
|
|
ct + 1 < ARRAY_SIZE(excl[0]) && *op;
|
|
|
|
op++, ct++) {
|
|
|
|
const char *n = option_to_longopt(*op, opts);
|
|
|
|
if (n)
|
2014-08-06 15:35:06 -05:00
|
|
|
fprintf(stderr, " --%s", n);
|
2017-04-18 19:57:27 -05:00
|
|
|
else if (c_isgraph(*op))
|
2014-08-06 15:35:06 -05:00
|
|
|
fprintf(stderr, " -%c", *op);
|
2012-07-26 02:04:55 -05:00
|
|
|
}
|
|
|
|
fputc('\n', stderr);
|
|
|
|
exit(OPTUTILS_EXIT_CODE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-29 11:21:49 -06:00
|
|
|
#endif
|
|
|
|
|