2012-03-18 18:24:12 +02:00
|
|
|
/*
|
|
|
|
* chr.h - Part characteristics
|
|
|
|
*
|
|
|
|
* Copyright 2012 by Werner Almesberger
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2012-04-25 23:14:39 +03:00
|
|
|
#include <assert.h>
|
|
|
|
|
2012-03-18 18:24:12 +02:00
|
|
|
#include "util.h"
|
2012-04-29 04:12:33 +03:00
|
|
|
#include "lang.h"
|
2012-05-21 05:04:41 +03:00
|
|
|
#include "relop.h"
|
2012-03-18 18:24:12 +02:00
|
|
|
#include "param.h"
|
|
|
|
#include "chr.h"
|
|
|
|
|
|
|
|
|
2012-04-25 23:14:39 +03:00
|
|
|
const struct format *field_find(const char *name, const struct field *tail)
|
2012-03-18 18:24:12 +02:00
|
|
|
{
|
2012-04-25 23:14:39 +03:00
|
|
|
while (tail) {
|
|
|
|
if (tail->name == name)
|
|
|
|
return tail->fmt;
|
|
|
|
tail = tail->prev;
|
|
|
|
}
|
2012-03-18 18:24:12 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-25 23:14:39 +03:00
|
|
|
/* ----- Construction of the field hierarchy ------------------------------- */
|
|
|
|
|
|
|
|
|
2012-04-26 08:48:08 +03:00
|
|
|
struct field *new_field(const char *name, const struct format *fmt,
|
2012-04-25 23:14:39 +03:00
|
|
|
const struct field *prev)
|
2012-03-18 18:24:12 +02:00
|
|
|
{
|
|
|
|
struct field *field;
|
|
|
|
|
2012-04-25 23:14:39 +03:00
|
|
|
if (field_find(name, prev))
|
2012-03-18 18:24:12 +02:00
|
|
|
yyerrorf("duplicate field \"%s\"", name);
|
|
|
|
field = alloc_type(struct field);
|
|
|
|
field->name = name;
|
|
|
|
field->fmt = fmt;
|
|
|
|
field->sel = NULL;
|
2012-04-25 23:14:39 +03:00
|
|
|
field->any.fields = NULL;
|
|
|
|
field->any.rules = NULL;
|
|
|
|
field->next = NULL;
|
|
|
|
field->prev = NULL;
|
|
|
|
return field;
|
2012-03-18 18:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-26 08:48:08 +03:00
|
|
|
struct selector *new_selector(void)
|
2012-03-18 18:24:12 +02:00
|
|
|
{
|
2012-04-25 23:14:39 +03:00
|
|
|
struct selector *sel;
|
|
|
|
|
|
|
|
sel = alloc_type(struct selector);
|
|
|
|
sel->cond = NULL;
|
|
|
|
sel->act.fields = NULL;
|
|
|
|
sel->act.rules = NULL;
|
|
|
|
sel->next = NULL;
|
|
|
|
return sel;
|
2012-03-18 18:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-26 08:48:08 +03:00
|
|
|
struct condition *new_condition(enum relop relop, const char *word)
|
2012-03-18 18:24:12 +02:00
|
|
|
{
|
2012-04-25 23:14:39 +03:00
|
|
|
struct condition *cond;
|
|
|
|
|
|
|
|
cond = alloc_type(struct condition);
|
2012-05-21 20:44:33 +03:00
|
|
|
cond->value.u.s = word;
|
2012-04-25 23:14:39 +03:00
|
|
|
cond->relop = relop;
|
|
|
|
cond->next = NULL;
|
|
|
|
return cond;
|
2012-03-18 18:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-31 18:33:52 +03:00
|
|
|
static int comp(const void *a, enum relop op, const void *b, const void *user)
|
|
|
|
{
|
|
|
|
return compare(user, a, op, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-01 16:17:19 +03:00
|
|
|
static void check_unreachable(const struct field *f,
|
2012-05-31 18:33:52 +03:00
|
|
|
const struct condition *c1, const struct condition *c2)
|
|
|
|
{
|
2012-06-01 16:17:19 +03:00
|
|
|
if (relop_unreachable(c1->relop, c2->relop, &c1->value, &c2->value,
|
2012-05-31 18:33:52 +03:00
|
|
|
comp, f->fmt))
|
2012-06-01 16:17:19 +03:00
|
|
|
yywarn("unreachable condition");
|
2012-05-31 18:33:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-25 23:14:39 +03:00
|
|
|
void field_finalize(struct field *field)
|
2012-03-18 18:24:12 +02:00
|
|
|
{
|
2012-05-31 18:33:52 +03:00
|
|
|
const struct selector *sel, *s2;
|
2012-04-25 23:14:39 +03:00
|
|
|
struct condition *cond;
|
2012-05-31 18:33:52 +03:00
|
|
|
const struct condition *c2;
|
2012-04-25 23:14:39 +03:00
|
|
|
|
|
|
|
for (sel = field->sel; sel; sel = sel->next)
|
|
|
|
for (cond = sel->cond; cond; cond = cond->next)
|
2012-05-21 20:44:33 +03:00
|
|
|
if (!evaluate(field->fmt, cond->value.u.s,
|
2012-04-25 23:14:39 +03:00
|
|
|
&cond->value))
|
|
|
|
yyerrorf("invalid value in selection");
|
|
|
|
/* @@@ indicate exact location */
|
2012-05-31 18:33:52 +03:00
|
|
|
for (sel = field->sel; sel; sel = sel->next)
|
|
|
|
for (cond = sel->cond; cond; cond = cond->next) {
|
|
|
|
for (c2 = cond->next; c2; c2 = c2->next)
|
2012-06-01 16:17:19 +03:00
|
|
|
check_unreachable(field, cond, c2);
|
2012-05-31 18:33:52 +03:00
|
|
|
for (s2 = sel->next; s2; s2 = s2->next)
|
|
|
|
for (c2 = s2->cond; c2; c2 = c2->next)
|
2012-06-01 16:17:19 +03:00
|
|
|
check_unreachable(field, cond, c2);
|
2012-05-31 18:33:52 +03:00
|
|
|
}
|
2012-03-18 18:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-25 23:14:39 +03:00
|
|
|
/* ----- Dumping ----------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
#define INDENT 8
|
|
|
|
|
|
|
|
|
2012-05-30 17:53:58 +03:00
|
|
|
static void dump_rules(FILE *file, const struct field *field, int level);
|
2012-04-25 23:14:39 +03:00
|
|
|
|
|
|
|
|
|
|
|
static void dump_set_decl(FILE *file, const struct names *first_name)
|
2012-03-18 18:24:12 +02:00
|
|
|
{
|
2012-04-25 23:14:39 +03:00
|
|
|
const char *name;
|
|
|
|
|
|
|
|
name = nameset_rev_lookup(first_name);
|
|
|
|
assert(name);
|
|
|
|
fprintf(file, "<%s>", name);
|
2012-03-18 18:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-25 23:14:39 +03:00
|
|
|
static void dump_field_decl(FILE *file, const struct field *field)
|
2012-03-18 18:24:12 +02:00
|
|
|
{
|
2012-04-25 23:14:39 +03:00
|
|
|
if (field->fmt->ops == ¶m_ops_name)
|
|
|
|
fprintf(file, "*");
|
|
|
|
else if (field->fmt->ops == ¶m_ops_set)
|
|
|
|
dump_set_decl(file, field->fmt->u.set);
|
|
|
|
else if (field->fmt->ops == ¶m_ops_abs)
|
2012-06-01 06:20:31 +03:00
|
|
|
fprintf(file, "#%s",
|
|
|
|
field->fmt->u.abs ? field->fmt->u.abs : "#");
|
2012-04-25 23:14:39 +03:00
|
|
|
else if (field->fmt->ops == ¶m_ops_rel)
|
2012-06-01 06:20:31 +03:00
|
|
|
fprintf(file, "%%%s",
|
|
|
|
field->fmt->u.rel->u.abs ? field->fmt->u.rel->u.abs : "#");
|
2012-04-25 23:14:39 +03:00
|
|
|
else
|
|
|
|
abort();
|
2012-03-18 18:24:12 +02:00
|
|
|
}
|
|
|
|
|
2012-04-25 23:14:39 +03:00
|
|
|
|
|
|
|
static void dump_action(FILE *file, const struct action *act, int level)
|
|
|
|
{
|
|
|
|
if (act->fields) {
|
2012-05-30 17:53:58 +03:00
|
|
|
fprintf(file, " ");
|
|
|
|
fields_dump(file, act->fields);
|
2012-04-25 23:14:39 +03:00
|
|
|
}
|
2012-05-30 17:53:58 +03:00
|
|
|
fprintf(file, "\n");
|
|
|
|
dump_rules(file, act->rules, level+1);
|
2012-04-25 23:14:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-30 17:53:58 +03:00
|
|
|
static void dump_one_rule(FILE *file, const struct field *field, int level)
|
2012-04-25 23:14:39 +03:00
|
|
|
{
|
|
|
|
const struct selector *sel;
|
|
|
|
const struct condition *cond;
|
|
|
|
|
2012-04-26 00:25:35 +03:00
|
|
|
fprintf(file, "%*s%s=", level*INDENT, "", field->name);
|
2012-04-25 23:14:39 +03:00
|
|
|
dump_field_decl(file, field);
|
|
|
|
|
|
|
|
fprintf(file, " {\n");
|
|
|
|
|
|
|
|
for (sel = field->sel; sel; sel = sel->next) {
|
|
|
|
fprintf(file, "%*s", level*INDENT+INDENT/2, "");
|
|
|
|
for (cond = sel->cond; cond; cond = cond->next) {
|
|
|
|
if (cond != sel->cond)
|
|
|
|
fprintf(file, "|");
|
2012-05-21 05:04:41 +03:00
|
|
|
if (cond->relop != rel_eq)
|
|
|
|
dump_relop(file, cond->relop);
|
2012-06-02 11:18:14 +03:00
|
|
|
dump_param(file, field->fmt, &cond->value);
|
2012-04-25 23:14:39 +03:00
|
|
|
}
|
2012-05-30 17:53:58 +03:00
|
|
|
fprintf(file, ":");
|
2012-04-25 23:14:39 +03:00
|
|
|
dump_action(file, &sel->act, level);
|
|
|
|
}
|
|
|
|
if (field->any.fields || field->any.rules) {
|
|
|
|
fprintf(file, "%*s*:", level*INDENT+INDENT/2, "");
|
|
|
|
dump_action(file, &field->any, level);
|
|
|
|
}
|
|
|
|
fprintf(file, "%*s}\n", level*INDENT, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-30 17:53:58 +03:00
|
|
|
void fields_dump(FILE *file, const struct field *fields)
|
|
|
|
{
|
|
|
|
const struct field *f;
|
|
|
|
|
|
|
|
if (!fields)
|
|
|
|
return;
|
|
|
|
fprintf(file, "{ ");
|
|
|
|
for (f = fields; f; f = f->next) {
|
|
|
|
fprintf(file, "%s%s=", f == fields ? "" : " ", f->name);
|
|
|
|
dump_field_decl(file, f);
|
|
|
|
}
|
|
|
|
fprintf(file, " }");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void dump_rules(FILE *file, const struct field *field, int level)
|
2012-04-25 23:14:39 +03:00
|
|
|
{
|
|
|
|
while (field) {
|
2012-05-30 17:53:58 +03:00
|
|
|
dump_one_rule(file, field, level);
|
2012-04-25 23:14:39 +03:00
|
|
|
field = field->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-30 17:53:58 +03:00
|
|
|
void rules_dump(FILE *file, const struct field *field)
|
2012-04-25 23:14:39 +03:00
|
|
|
{
|
2012-05-30 17:53:58 +03:00
|
|
|
dump_rules(file, field, 0);
|
2012-04-25 23:14:39 +03:00
|
|
|
}
|