20#include <grass/glocale.h>
22#include "parser_local_proto.h"
32static void vector_new(
struct vector *v,
size_t elsize,
size_t increment)
35 v->increment = increment;
41static void vector_append(
struct vector *v,
const void *data)
45 if (v->count >= v->limit) {
46 v->limit += v->increment;
47 v->data = G_realloc(v->data, v->limit * v->elsize);
51 memcpy(p, data, v->elsize);
61static struct vector rules = {.elsize =
sizeof(
struct rule), .increment = 50};
85 vector_append(&rules, &rule);
88static void make_rule(
int type,
void *first, va_list ap)
93 vector_new(&opts,
sizeof(
void *), 10);
96 vector_append(&opts, &opt);
98 opt = va_arg(ap,
void *);
102 vector_append(&opts, &opt);
108static int is_flag(
const void *p)
111 const struct Flag *flag;
113 for (flag = &
st->first_flag; flag; flag = flag->next_flag)
114 if ((
const void *)flag == p)
119 const struct Option *opt;
121 for (opt = &
st->first_option; opt; opt = opt->next_opt)
122 if ((
const void *)opt == p)
126 G_fatal_error(_(
"Internal error: option or flag not found"));
129static int is_present(
const void *p)
132 const struct Flag *flag = p;
134 return (
int)flag->answer;
137 const struct Option *opt = p;
139 return opt->count > 0;
143static char *get_name(
const void *p)
148 G_asprintf(&s,
"-%c", ((
const struct Flag *)p)->key);
152 return G_store(((
const struct Option *)p)->key);
155static int count_present(
const struct rule *rule,
int start)
160 for (i = start; i < rule->count; i++)
161 if (is_present(rule->opts[i]))
167static const char *describe_rule(
const struct rule *rule,
int start,
173 G_asprintf(&s,
"<%s>", get_name(rule->opts[start]));
175 for (i = start + 1; i < rule->count - 1; i++) {
177 char *ss = get_name(rule->opts[i]);
185 if (rule->count - start > 1) {
187 char *ss = get_name(rule->opts[i]);
190 G_asprintf(&s, disjunction ? _(
"%s or <%s>") : _(
"%s and <%s>"), s0,
199static void append_error(
const char *msg)
201 st->error = G_realloc(
st->error,
sizeof(
char *) * (
st->n_errors + 1));
217 make_rule(RULE_EXCLUSIVE, first, ap);
221static void check_exclusive(
const struct rule *rule)
223 if (count_present(rule, 0) > 1) {
227 describe_rule(rule, 0, 0));
243 make_rule(RULE_REQUIRED, first, ap);
247static void check_required(
const struct rule *rule)
249 if (count_present(rule, 0) < 1) {
253 _(
"At least one of the following options is required: %s"),
254 describe_rule(rule, 0, 0));
277 make_rule(RULE_REQUIRES, first, ap);
281static void check_requires(
const struct rule *rule)
283 if (!is_present(rule->opts[0]))
285 if (count_present(rule, 1) < 1) {
289 G_asprintf(&
err, _(
"Option <%s> requires at least one of %s"),
290 get_name(rule->opts[0]), describe_rule(rule, 1, 1));
293 get_name(rule->opts[0]), describe_rule(rule, 1, 1));
315 make_rule(RULE_REQUIRES_ALL, first, ap);
319static void check_requires_all(
const struct rule *rule)
321 if (!is_present(rule->opts[0]))
323 if (count_present(rule, 1) < rule->count - 1) {
327 get_name(rule->opts[0]), describe_rule(rule, 1, 0));
344 make_rule(RULE_EXCLUDES, first, ap);
348static void check_excludes(
const struct rule *rule)
350 if (!is_present(rule->opts[0]))
352 if (count_present(rule, 1) > 0) {
355 G_asprintf(&
err, _(
"Option <%s> is mutually exclusive with all of %s"),
356 get_name(rule->opts[0]), describe_rule(rule, 1, 0));
373 make_rule(RULE_COLLECTIVE, first, ap);
377static void check_collective(
const struct rule *rule)
379 int count = count_present(rule, 0);
384 G_asprintf(&
err, _(
"Either all or none of %s must be given"),
385 describe_rule(rule, 0, 0));
395 for (i = 0; i < rules.count; i++) {
396 const struct rule *rule = &((
const struct rule *)rules.data)[i];
398 switch (rule->type) {
400 check_exclusive(rule);
403 check_required(rule);
406 check_requires(rule);
408 case RULE_REQUIRES_ALL:
409 check_requires_all(rule);
412 check_excludes(rule);
414 case RULE_COLLECTIVE:
415 check_collective(rule);
430 for (i = 0; i < rules.count; i++) {
431 const struct rule *rule = &((
const struct rule *)rules.data)[i];
433 switch (rule->type) {
435 fprintf(stderr,
"Exclusive: %s", describe_rule(rule, 0, 0));
438 fprintf(stderr,
"Required: %s", describe_rule(rule, 0, 1));
441 fprintf(stderr,
"Requires: %s => %s", get_name(rule->opts[0]),
442 describe_rule(rule, 1, 1));
444 case RULE_REQUIRES_ALL:
445 fprintf(stderr,
"Requires: %s => %s", get_name(rule->opts[0]),
446 describe_rule(rule, 1, 0));
449 fprintf(stderr,
"Excludes: %s => %s", get_name(rule->opts[0]),
450 describe_rule(rule, 1, 0));
452 case RULE_COLLECTIVE:
453 fprintf(stderr,
"Collective: %s", describe_rule(rule, 0, 0));
473 for (i = 0; i < rules.count; i++) {
474 const struct rule *rule = &((
const struct rule *)rules.data)[i];
476 if (rule->type == RULE_REQUIRED)
482static const char *
const rule_types[] = {
"exclusive",
"required",
483 "requires",
"requires-all",
484 "excludes",
"collective"};
497 fprintf(fp,
"\t<rules>\n");
498 for (i = 0; i < rules.count; i++) {
499 const struct rule *rule = &((
const struct rule *)rules.data)[i];
502 G_fatal_error(_(
"Internal error: the number of options is < 0"));
504 fprintf(fp,
"\t\t<rule type=\"%s\">\n", rule_types[rule->type]);
505 for (j = 0; j < (
unsigned int)rule->count; j++) {
506 void *p = rule->opts[j];
509 const struct Flag *flag = (
const struct Flag *)p;
511 fprintf(fp,
"\t\t\t<rule-flag key=\"%c\"/>\n", flag->key);
514 const struct Option *opt = (
const struct Option *)p;
516 fprintf(fp,
"\t\t\t<rule-option key=\"%s\"/>\n", opt->key);
519 fprintf(fp,
"\t\t</rule>\n");
521 fprintf(fp,
"\t</rules>\n");
void * G_incr_void_ptr(const void *ptr, size_t size)
Advance void pointer.
void G_free(void *buf)
Free allocated memory.
int G_asprintf(char **out, const char *fmt,...)
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
void G__check_option_rules(void)
Check for option rules (internal use only)
void G_option_rule(int type, int nopts, void **opts)
Set generic option rule.
void G_option_collective(void *first,...)
Sets the options to be collective.
int G__has_required_rule(void)
Checks if there is any rule RULE_REQUIRED (internal use only).
void G__describe_option_rules(void)
Describe option rules (stderr)
void G_option_requires_all(void *first,...)
Define additionally required options for an option.
void G_option_excludes(void *first,...)
Exclude selected options.
void G_option_exclusive(void *first,...)
Sets the options to be mutually exclusive.
void G__describe_option_rules_xml(FILE *fp)
Describe option rules in XML format (internal use only)
void G_option_required(void *first,...)
Sets the options to be required.
void G_option_requires(void *first,...)
Define a list of options from which at least one option is required if first option is present.
char * G_store(const char *s)
Copy string to allocated memory.
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)