18 #include <grass/gis.h> 19 #include <grass/glocale.h> 21 #include "parser_local_proto.h" 31 static void vector_new(
struct vector *v,
size_t elsize,
size_t increment)
34 v->increment = increment;
40 static void vector_append(
struct vector *v,
const void *data)
44 if (v->count >= v->limit) {
45 v->limit += v->increment;
46 v->data = G_realloc(v->data, v->limit * v->elsize);
50 memcpy(p, data, v->elsize);
60 static struct vector rules = {
sizeof(
struct rule), 50};
84 vector_append(&rules, &rule);
87 static void make_rule(
int type,
void *first, va_list ap)
92 vector_new(&opts,
sizeof(
void *), 10);
95 vector_append(&opts, &opt);
97 opt = va_arg(ap,
void*);
100 vector_append(&opts, &opt);
106 static int is_flag(
const void *p)
109 const struct Flag *flag;
110 for (flag = &
st->first_flag; flag; flag = flag->next_flag)
111 if ((
const void *) flag == p)
116 const struct Option *opt;
117 for (opt = &
st->first_option; opt; opt = opt->next_opt)
118 if ((
const void *) opt == p)
122 G_fatal_error(_(
"Internal error: option or flag not found"));
125 static int is_present(
const void *p)
128 const struct Flag *flag = p;
129 return (
int) flag->answer;
132 const struct Option *opt = p;
133 return opt->count > 0;
137 static char *get_name(
const void *p)
141 G_asprintf(&s,
"-%c", ((
const struct Flag *) p)->key);
145 return G_store(((
const struct Option *) p)->key);
148 static int count_present(
const struct rule *rule,
int start)
153 for (i = start; i < rule->count; i++)
154 if (is_present(rule->opts[i]))
160 static const char *describe_rule(
const struct rule *rule,
int start,
163 char *s = get_name(rule->opts[start]);
166 for (i = start + 1; i < rule->count - 1; i++) {
168 char *ss = get_name(rule->opts[i]);
175 if (rule->count - start > 1) {
177 char *ss = get_name(rule->opts[i]);
179 G_asprintf(&s, disjunction ? _(
"<%s> or <%s>") : _(
"<%s> and <%s>"), s0, ss);
187 static void append_error(
const char *msg)
189 st->error = G_realloc(
st->error,
sizeof(
char *) * (
st->n_errors + 1));
204 make_rule(RULE_EXCLUSIVE, first, ap);
208 static void check_exclusive(
const struct rule *rule)
210 if (count_present(rule, 0) > 1) {
212 G_asprintf(&err, _(
"Options %s are mutually exclusive"),
213 describe_rule(rule, 0, 0));
228 make_rule(RULE_REQUIRED, first, ap);
232 static void check_required(
const struct rule *rule)
234 if (count_present(rule, 0) < 1) {
236 G_asprintf(&err, _(
"At least one of the following options is required: %s"),
237 describe_rule(rule, 0, 0));
259 make_rule(RULE_REQUIRES, first, ap);
263 static void check_requires(
const struct rule *rule)
265 if (!is_present(rule->opts[0]))
267 if (count_present(rule, 1) < 1) {
269 G_asprintf(&err, _(
"Option %s requires at least one of %s"),
270 get_name(rule->opts[0]), describe_rule(rule, 1, 1));
291 make_rule(RULE_REQUIRES_ALL, first, ap);
295 static void check_requires_all(
const struct rule *rule)
297 if (!is_present(rule->opts[0]))
299 if (count_present(rule, 1) < rule->count - 1) {
301 G_asprintf(&err, _(
"Option %s requires all of %s"),
302 get_name(rule->opts[0]), describe_rule(rule, 1, 0));
318 make_rule(RULE_EXCLUDES, first, ap);
322 static void check_excludes(
const struct rule *rule)
324 if (!is_present(rule->opts[0]))
326 if (count_present(rule, 1) > 0) {
328 G_asprintf(&err, _(
"Option %s is mutually exclusive with all of %s"),
329 get_name(rule->opts[0]), describe_rule(rule, 1, 0));
345 make_rule(RULE_COLLECTIVE, first, ap);
349 static void check_collective(
const struct rule *rule)
351 int count = count_present(rule, 0);
352 if (count > 0 && count < rule->count) {
354 G_asprintf(&err, _(
"Either all or none of %s must be given"),
355 describe_rule(rule, 0, 0));
365 for (i = 0; i < rules.count; i++) {
366 const struct rule *rule = &((
const struct rule *) rules.data)[i];
367 switch (rule->type) {
369 check_exclusive(rule);
372 check_required(rule);
375 check_requires(rule);
377 case RULE_REQUIRES_ALL:
378 check_requires_all(rule);
381 check_excludes(rule);
383 case RULE_COLLECTIVE:
384 check_collective(rule);
399 for (i = 0; i < rules.count; i++) {
400 const struct rule *rule = &((
const struct rule *) rules.data)[i];
401 switch (rule->type) {
403 fprintf(stderr,
"Exclusive: %s", describe_rule(rule, 0, 0));
406 fprintf(stderr,
"Required: %s", describe_rule(rule, 0, 1));
409 fprintf(stderr,
"Requires: %s => %s", get_name(rule->opts[0]),
410 describe_rule(rule, 1, 1));
412 case RULE_REQUIRES_ALL:
413 fprintf(stderr,
"Requires: %s => %s", get_name(rule->opts[0]),
414 describe_rule(rule, 1, 0));
417 fprintf(stderr,
"Excludes: %s => %s", get_name(rule->opts[0]),
418 describe_rule(rule, 1, 0));
420 case RULE_COLLECTIVE:
421 fprintf(stderr,
"Collective: %s", describe_rule(rule, 0, 0));
441 for (i = 0; i < rules.count; i++) {
442 const struct rule *rule = &((
const struct rule *) rules.data)[i];
443 if (rule->type == RULE_REQUIRED)
449 static const char *
const rule_types[] = {
469 fprintf(fp,
"\t<rules>\n");
470 for (i = 0; i < rules.count; i++) {
471 const struct rule *rule = &((
const struct rule *) rules.data)[i];
472 fprintf(fp,
"\t\t<rule type=\"%s\">\n", rule_types[rule->type]);
473 for (j = 0; j < rule->count; j++) {
474 void *p = rule->opts[j];
476 const struct Flag *flag = (
const struct Flag *) p;
477 fprintf(fp,
"\t\t\t<rule-flag key=\"%c\"/>\n", flag->key);
480 const struct Option *opt = (
const struct Option *) p;
481 fprintf(fp,
"\t\t\t<rule-option key=\"%s\"/>\n", opt->key);
484 fprintf(fp,
"\t\t</rule>\n");
486 fprintf(fp,
"\t</rules>\n");
int G__has_required_rule(void)
Checks if there is any rule RULE_REQUIRED (internal use only).
void G_option_required(void *first,...)
Sets the options to be required.
char * G_store(const char *s)
Copy string to allocated memory.
void G__check_option_rules(void)
Check for option rules (internal use only)
int G_asprintf(char **out, const char *fmt,...)
void G_option_excludes(void *first,...)
Exclude selected options.
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
void G_option_rule(int type, int nopts, void **opts)
Set generic option rule.
void G_option_requires(void *first,...)
Define a list of options from which at least one option is required if first option is present...
void G_option_exclusive(void *first,...)
Sets the options to be mutually exclusive.
void G__describe_option_rules(void)
Describe option rules (stderr)
void G__describe_option_rules_xml(FILE *fp)
Describe option rules in XML format (internal use only)
void G_option_requires_all(void *first,...)
Define additionally required options for an option.
void G_option_collective(void *first,...)
Sets the options to be collective.
void * G_incr_void_ptr(const void *ptr, size_t size)
Advance void pointer.
void G_free(void *buf)
Free allocated memory.