parser: improve debug output of transhar

Make transchar stream output work with a broader range of values.

Signed-off-by: John Johansen <john.johansen@canonical.com>
This commit is contained in:
John Johansen 2019-08-08 03:17:24 -07:00
parent 72f93d9aba
commit 7c29bfebe3
4 changed files with 82 additions and 36 deletions

View file

@ -41,19 +41,38 @@
/* Use a single static EpsNode as it carries no node specific information */
EpsNode epsnode;
ostream &operator<<(ostream &os, transchar c)
ostream &transchar::dump(ostream &os) const
{
const char *search = "\a\033\f\n\r\t|*+[](). ",
*replace = "aefnrt|*+[](). ", *s;
if (this->c < 0)
os << "-0x" << hex << -this->c << dec;
else if (this->c > 255)
os << "0x" << hex << this->c << dec;
else if ((s = strchr(search, this->c)) && *s != '\0')
os << '\\' << replace[s - search] << " 0x" << hex << this->c << dec;
else if (!isprint(this->c))
os << "0x" << hex << this->c << dec;
else
os << (char)this->c << " 0x" << hex << this->c << dec;
return os;
}
ostream &operator<<(ostream &os, transchar tc)
{
const char *search = "\a\033\f\n\r\t|*+[](). ",
*replace = "aefnrt|*+[](). ", *s;
short c = tc.c;
if ((s = strchr(search, c.c)) && *s != '\0') {
if (c < 0)
os << "\\d" << "" << tc.c;
else if ((s = strchr(search, c)) && *s != '\0')
os << '\\' << replace[s - search];
} else if (c.c < 32 || c.c >= 127) {
os << '\\' << '0' << char ('0' + (c.c >> 6))
<< char ('0' + ((c.c >> 3) & 7)) << char ('0' + (c.c & 7));
} else {
os << (char)c.c;
}
else if (!isprint(c))
os << "\\x" << hex << c << dec;
else
os << (char)c;
return os;
}

View file

@ -81,6 +81,8 @@ public:
return tmp;
}
ostream &dump(ostream &os) const;
};
class Chars {

View file

@ -73,6 +73,15 @@ ostream &operator<<(ostream &os, const State &state)
return os;
}
ostream &operator<<(ostream &os, State &state)
{
/* dump the state label */
os << '{';
os << state.label;
os << '}';
return os;
}
/**
* diff_weight - Find differential compression distance between @rel and @this
* @rel: State to compare too
@ -1058,39 +1067,52 @@ void DFA::dump(ostream & os)
for (Partition::iterator i = states.begin(); i != states.end(); i++) {
Chars excluded;
bool first = true;
for (StateTrans::iterator j = (*i)->trans.begin();
j != (*i)->trans.end(); j++) {
if (j->second == nonmatching) {
excluded.insert(j->first);
} else {
os << **i;
if ((*i)->perms.is_accept())
os << " ", (*i)->perms.dump(os);
os << " -> " << *(j)->second << ": 0x"
<< hex << j->first.c;
if (j->first.c < 256 && isprint(j->first.c))
os << " " << j->first.c;
os << dec << "\n";
if (first) {
first = false;
os << **i << " perms: ";
if ((*i)->perms.is_accept())
(*i)->perms.dump(os);
else
os << "none";
os << "\n";
}
os << " "; j->first.dump(os) << " -> " <<
*(j)->second;
if ((j)->second->perms.is_accept())
os << " ", (j->second)->perms.dump(os);
os << "\n";
}
}
if ((*i)->otherwise != nonmatching) {
os << **i;
if ((*i)->perms.is_accept())
os << " ", (*i)->perms.dump(os);
os << " -> " << *(*i)->otherwise << ": [";
if (first) {
first = false;
os << **i << " perms: ";
if ((*i)->perms.is_accept())
(*i)->perms.dump(os);
else
os << "none";
os << "\n";
}
os << " [";
if (!excluded.empty()) {
os << "^";
for (Chars::iterator k = excluded.begin();
k != excluded.end(); k++) {
if (k->c < 256 && isprint(k->c))
os << k->c;
else
os << "\\0x" << hex << k->c << dec;
os << *k;
}
}
os << "]\n";
os << "] -> " << *(*i)->otherwise;
if ((*i)->otherwise->perms.is_accept())
os << " ", (*i)->otherwise->perms.dump(os);
os << "\n";
}
}
os << "\n";
@ -1128,11 +1150,7 @@ void DFA::dump_dot_graph(ostream & os)
os << "\t\"" << **i << "\" -> \"" << *j->second
<< "\" [" << "\n";
os << "\t\tlabel=\"";
if (j->first.c < 256 && isprint(j->first.c))
os << j->first.c;
else
os << "\\0x" << hex << j->first.c << dec;
j->first.dump(os);
os << "\"\n\t]" << "\n";
}
}
@ -1143,10 +1161,7 @@ void DFA::dump_dot_graph(ostream & os)
os << "\t\tlabel=\"[^";
for (Chars::iterator i = excluded.begin();
i != excluded.end(); i++) {
if (i->c < 256 && isprint(i->c))
os << i->c;
else
os << "\\0x" << hex << i->c << dec;
i->dump(os);
}
os << "]\"" << "\n";
}

View file

@ -42,6 +42,9 @@ typedef list<State *> Partition;
#include "../immunix.h"
ostream &operator<<(ostream &os, const State &state);
ostream &operator<<(ostream &os, State &state);
class perms_t {
public:
perms_t(void): allow(0), deny(0), audit(0), quiet(0), exact(0) { };
@ -229,6 +232,15 @@ public:
return NULL;
}
ostream &dump(ostream &os)
{
cerr << *this << "\n";
for (StateTrans::iterator i = trans.begin(); i != trans.end(); i++) {
os << " " << i->first.c << " -> " << *i->second << "\n";
}
return os;
}
int diff_weight(State *rel);
int make_relative(State *rel);
void flatten_relative(void);
@ -249,8 +261,6 @@ public:
};
};
ostream &operator<<(ostream &os, const State &state);
class NodeMap: public CacheStats
{
public: