2004-03-18 23:04:14 +02:00
|
|
|
#include <vector>
|
2004-03-15 23:51:14 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <string>
|
2008-05-21 14:17:31 +03:00
|
|
|
#include <cstring>
|
2004-03-15 23:51:14 +02:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
|
|
struct Decoder
|
|
|
|
{
|
|
|
|
enum { stTop, stEscape, stCSI } state;
|
|
|
|
string line;
|
|
|
|
bool inHeader;
|
|
|
|
int level;
|
2004-03-18 23:04:14 +02:00
|
|
|
vector<int> args;
|
|
|
|
bool newNumber;
|
|
|
|
int priority;
|
2004-03-18 23:32:15 +02:00
|
|
|
bool ignoreLF;
|
2004-03-27 17:33:19 +02:00
|
|
|
int lineNo, charNo;
|
2004-03-15 23:51:14 +02:00
|
|
|
|
|
|
|
Decoder()
|
|
|
|
{
|
|
|
|
state = stTop;
|
|
|
|
line = "";
|
|
|
|
inHeader = false;
|
|
|
|
level = 0;
|
2004-03-18 23:04:14 +02:00
|
|
|
priority = 1;
|
2004-03-18 23:32:15 +02:00
|
|
|
ignoreLF = false;
|
2004-03-27 17:33:19 +02:00
|
|
|
lineNo = 1;
|
|
|
|
charNo = 0;
|
2004-03-15 23:51:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void pushChar(char c);
|
|
|
|
|
|
|
|
void finishLine();
|
2008-11-25 03:06:15 +02:00
|
|
|
|
|
|
|
void decodeFile(istream & st);
|
2004-03-15 23:51:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void Decoder::pushChar(char c)
|
|
|
|
{
|
2004-03-27 17:33:19 +02:00
|
|
|
if (c == '\n') {
|
|
|
|
lineNo++;
|
|
|
|
charNo = 0;
|
|
|
|
} else charNo++;
|
|
|
|
|
2004-03-15 23:51:14 +02:00
|
|
|
switch (state) {
|
|
|
|
|
|
|
|
case stTop:
|
|
|
|
if (c == '\e') {
|
|
|
|
state = stEscape;
|
2004-03-18 23:32:15 +02:00
|
|
|
} else if (c == '\n' && !ignoreLF) {
|
2004-03-15 23:51:14 +02:00
|
|
|
finishLine();
|
2004-03-16 14:47:09 +02:00
|
|
|
} else line += c;
|
2004-03-15 23:51:14 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case stEscape:
|
2004-03-18 23:04:14 +02:00
|
|
|
if (c == '[') {
|
2004-03-15 23:51:14 +02:00
|
|
|
state = stCSI;
|
2004-03-18 23:04:14 +02:00
|
|
|
args.clear();
|
|
|
|
newNumber = true;
|
|
|
|
} else
|
2004-03-15 23:51:14 +02:00
|
|
|
state = stTop; /* !!! wrong */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case stCSI:
|
|
|
|
if (c >= 0x40 && c != 0x7e) {
|
|
|
|
state = stTop;
|
|
|
|
switch (c) {
|
|
|
|
case 'p':
|
|
|
|
if (line.size()) finishLine();
|
|
|
|
level++;
|
|
|
|
inHeader = true;
|
|
|
|
cout << "<nest>" << endl;
|
2004-03-18 23:04:14 +02:00
|
|
|
priority = args.size() >= 1 ? args[0] : 1;
|
2004-03-15 23:51:14 +02:00
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
if (line.size()) finishLine();
|
|
|
|
if (level > 0) {
|
|
|
|
level--;
|
|
|
|
cout << "</nest>" << endl;
|
|
|
|
} else
|
2004-03-27 17:33:19 +02:00
|
|
|
cerr << "not enough nesting levels at line "
|
|
|
|
<< lineNo << ", character " << charNo << endl;
|
2004-03-15 23:51:14 +02:00
|
|
|
break;
|
2004-03-18 23:04:14 +02:00
|
|
|
case 's':
|
|
|
|
if (line.size()) finishLine();
|
|
|
|
priority = args.size() >= 1 ? args[0] : 1;
|
|
|
|
break;
|
2004-03-18 23:32:15 +02:00
|
|
|
case 'a':
|
|
|
|
ignoreLF = true;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
ignoreLF = false;
|
|
|
|
break;
|
2004-03-18 23:04:14 +02:00
|
|
|
}
|
|
|
|
} else if (c >= '0' && c <= '9') {
|
|
|
|
int n = 0;
|
|
|
|
if (!newNumber) {
|
|
|
|
n = args.back() * 10;
|
|
|
|
args.pop_back();
|
2004-03-15 23:51:14 +02:00
|
|
|
}
|
2004-03-18 23:04:14 +02:00
|
|
|
n += c - '0';
|
|
|
|
args.push_back(n);
|
2004-03-15 23:51:14 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Decoder::finishLine()
|
|
|
|
{
|
2004-03-16 14:47:09 +02:00
|
|
|
string storeDir = "/nix/store/";
|
|
|
|
int sz = storeDir.size();
|
2004-03-15 23:51:14 +02:00
|
|
|
string tag = inHeader ? "head" : "line";
|
2004-03-18 23:04:14 +02:00
|
|
|
cout << "<" << tag;
|
|
|
|
if (priority != 1) cout << " priority='" << priority << "'";
|
|
|
|
cout << ">";
|
2004-03-16 14:47:09 +02:00
|
|
|
|
2005-08-01 16:39:19 +03:00
|
|
|
for (unsigned int i = 0; i < line.size(); i++) {
|
2004-03-16 14:47:09 +02:00
|
|
|
|
|
|
|
if (line[i] == '<') cout << "<";
|
|
|
|
else if (line[i] == '&') cout << "&";
|
2005-08-01 16:39:19 +03:00
|
|
|
else if (line[i] < 32 && line[i] != 9) cout << "�";
|
2004-03-16 14:47:09 +02:00
|
|
|
else if (i + sz + 33 < line.size() &&
|
|
|
|
string(line, i, sz) == storeDir &&
|
|
|
|
line[i + sz + 32] == '-')
|
|
|
|
{
|
|
|
|
int j = i + sz + 32;
|
|
|
|
/* skip name */
|
|
|
|
while (!strchr("/\n\r\t ()[]:;?<>", line[j])) j++;
|
|
|
|
int k = j;
|
|
|
|
while (!strchr("\n\r\t ()[]:;?<>", line[k])) k++;
|
|
|
|
// !!! escaping
|
|
|
|
cout << "<storeref>"
|
|
|
|
<< "<storedir>"
|
|
|
|
<< string(line, i, sz)
|
|
|
|
<< "</storedir>"
|
|
|
|
<< "<hash>"
|
|
|
|
<< string(line, i + sz, 32)
|
|
|
|
<< "</hash>"
|
|
|
|
<< "<name>"
|
|
|
|
<< string(line, i + sz + 32, j - (i + sz + 32))
|
|
|
|
<< "</name>"
|
|
|
|
<< "<path>"
|
|
|
|
<< string(line, j, k - j)
|
|
|
|
<< "</path>"
|
|
|
|
<< "</storeref>";
|
|
|
|
i = k - 1;
|
|
|
|
} else cout << line[i];
|
|
|
|
}
|
|
|
|
|
2004-03-15 23:51:14 +02:00
|
|
|
cout << "</" << tag << ">" << endl;
|
|
|
|
line = "";
|
|
|
|
inHeader = false;
|
2004-03-18 23:04:14 +02:00
|
|
|
priority = 1;
|
2004-03-15 23:51:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-25 03:06:15 +02:00
|
|
|
void Decoder::decodeFile(istream & st)
|
2004-03-15 23:51:14 +02:00
|
|
|
{
|
|
|
|
int c;
|
2008-11-25 03:06:15 +02:00
|
|
|
|
2004-03-15 23:51:14 +02:00
|
|
|
cout << "<logfile>" << endl;
|
|
|
|
|
2008-11-25 03:06:15 +02:00
|
|
|
while ((c = st.get()) != EOF) {
|
|
|
|
pushChar(c);
|
2004-03-15 23:51:14 +02:00
|
|
|
}
|
|
|
|
|
2008-11-25 03:06:15 +02:00
|
|
|
if (line.size()) finishLine();
|
|
|
|
|
|
|
|
while (level--) cout << "</nest>" << endl;
|
|
|
|
|
2004-03-15 23:51:14 +02:00
|
|
|
cout << "</logfile>" << endl;
|
|
|
|
}
|
2008-11-25 03:06:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char * * argv)
|
|
|
|
{
|
|
|
|
Decoder dec;
|
|
|
|
dec.decodeFile(cin);
|
|
|
|
}
|