2010-04-07 15:59:45 +02:00
|
|
|
#include "value-to-xml.hh"
|
2006-08-24 16:16:55 +02:00
|
|
|
#include "xml-writer.hh"
|
2012-03-05 22:04:40 +01:00
|
|
|
#include "eval-inline.hh"
|
2007-01-13 15:48:41 +01:00
|
|
|
#include "util.hh"
|
2006-08-24 16:16:55 +02:00
|
|
|
|
2008-05-21 13:17:31 +02:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2006-08-24 16:16:55 +02:00
|
|
|
|
2006-09-04 23:06:23 +02:00
|
|
|
namespace nix {
|
|
|
|
|
2015-07-17 19:24:28 +02:00
|
|
|
|
2006-08-24 16:16:55 +02:00
|
|
|
static XMLAttrs singletonAttrs(const string & name, const string & value)
|
|
|
|
{
|
|
|
|
XMLAttrs attrs;
|
|
|
|
attrs[name] = value;
|
|
|
|
return attrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-07 16:46:47 +02:00
|
|
|
static void printValueAsXML(EvalState & state, bool strict, bool location,
|
|
|
|
Value & v, XMLWriter & doc, PathSet & context, PathSet & drvsSeen);
|
2007-01-13 16:11:10 +01:00
|
|
|
|
|
|
|
|
2010-05-07 16:46:47 +02:00
|
|
|
static void posToXML(XMLAttrs & xmlAttrs, const Pos & pos)
|
|
|
|
{
|
|
|
|
xmlAttrs["path"] = pos.file;
|
|
|
|
xmlAttrs["line"] = (format("%1%") % pos.line).str();
|
|
|
|
xmlAttrs["column"] = (format("%1%") % pos.column).str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void showAttrs(EvalState & state, bool strict, bool location,
|
|
|
|
Bindings & attrs, XMLWriter & doc, PathSet & context, PathSet & drvsSeen)
|
2007-01-13 16:11:10 +01:00
|
|
|
{
|
|
|
|
StringSet names;
|
2015-07-17 19:24:28 +02:00
|
|
|
|
|
|
|
for (auto & i : attrs)
|
|
|
|
names.insert(i.name);
|
|
|
|
|
|
|
|
for (auto & i : names) {
|
|
|
|
Attr & a(*attrs.find(state.symbols.create(i)));
|
|
|
|
|
2010-05-07 16:46:47 +02:00
|
|
|
XMLAttrs xmlAttrs;
|
2015-07-17 19:24:28 +02:00
|
|
|
xmlAttrs["name"] = i;
|
2010-05-07 16:46:47 +02:00
|
|
|
if (location && a.pos != &noPos) posToXML(xmlAttrs, *a.pos);
|
2015-07-17 19:24:28 +02:00
|
|
|
|
2010-05-07 16:46:47 +02:00
|
|
|
XMLOpenElement _(doc, "attr", xmlAttrs);
|
|
|
|
printValueAsXML(state, strict, location,
|
2010-10-22 16:47:42 +02:00
|
|
|
*a.value, doc, context, drvsSeen);
|
2008-08-14 14:53:29 +02:00
|
|
|
}
|
2008-08-14 12:04:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-07 16:46:47 +02:00
|
|
|
static void printValueAsXML(EvalState & state, bool strict, bool location,
|
|
|
|
Value & v, XMLWriter & doc, PathSet & context, PathSet & drvsSeen)
|
2006-08-24 16:16:55 +02:00
|
|
|
{
|
2007-01-13 15:48:41 +01:00
|
|
|
checkInterrupt();
|
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
if (strict) state.forceValue(v);
|
2015-07-17 19:24:28 +02:00
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
switch (v.type) {
|
2006-08-24 16:16:55 +02:00
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
case tInt:
|
|
|
|
doc.writeEmptyElement("int", singletonAttrs("value", (format("%1%") % v.integer).str()));
|
|
|
|
break;
|
2006-08-24 16:16:55 +02:00
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
case tBool:
|
|
|
|
doc.writeEmptyElement("bool", singletonAttrs("value", v.boolean ? "true" : "false"));
|
|
|
|
break;
|
2006-08-24 16:16:55 +02:00
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
case tString:
|
|
|
|
/* !!! show the context? */
|
2010-06-10 12:29:50 +02:00
|
|
|
copyContext(v, context);
|
2010-04-07 15:55:46 +02:00
|
|
|
doc.writeEmptyElement("string", singletonAttrs("value", v.string.s));
|
|
|
|
break;
|
2006-08-24 16:16:55 +02:00
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
case tPath:
|
|
|
|
doc.writeEmptyElement("path", singletonAttrs("value", v.path));
|
|
|
|
break;
|
2007-01-13 16:11:10 +01:00
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
case tNull:
|
|
|
|
doc.writeEmptyElement("null");
|
|
|
|
break;
|
2007-01-13 16:11:10 +01:00
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
case tAttrs:
|
|
|
|
if (state.isDerivation(v)) {
|
|
|
|
XMLAttrs xmlAttrs;
|
2015-07-17 19:24:28 +02:00
|
|
|
|
2010-04-13 14:25:42 +02:00
|
|
|
Bindings::iterator a = v.attrs->find(state.symbols.create("derivation"));
|
2010-04-07 15:55:46 +02:00
|
|
|
|
|
|
|
Path drvPath;
|
2010-04-13 14:25:42 +02:00
|
|
|
a = v.attrs->find(state.sDrvPath);
|
2010-05-07 16:46:47 +02:00
|
|
|
if (a != v.attrs->end()) {
|
2010-10-24 02:41:29 +02:00
|
|
|
if (strict) state.forceValue(*a->value);
|
|
|
|
if (a->value->type == tString)
|
|
|
|
xmlAttrs["drvPath"] = drvPath = a->value->string.s;
|
2010-05-07 16:46:47 +02:00
|
|
|
}
|
2015-07-17 19:24:28 +02:00
|
|
|
|
2010-04-13 14:25:42 +02:00
|
|
|
a = v.attrs->find(state.sOutPath);
|
2010-05-07 16:46:47 +02:00
|
|
|
if (a != v.attrs->end()) {
|
2010-10-24 02:41:29 +02:00
|
|
|
if (strict) state.forceValue(*a->value);
|
|
|
|
if (a->value->type == tString)
|
|
|
|
xmlAttrs["outPath"] = a->value->string.s;
|
2010-05-07 16:46:47 +02:00
|
|
|
}
|
2010-04-07 15:55:46 +02:00
|
|
|
|
|
|
|
XMLOpenElement _(doc, "derivation", xmlAttrs);
|
|
|
|
|
|
|
|
if (drvPath != "" && drvsSeen.find(drvPath) == drvsSeen.end()) {
|
|
|
|
drvsSeen.insert(drvPath);
|
2010-05-07 16:46:47 +02:00
|
|
|
showAttrs(state, strict, location, *v.attrs, doc, context, drvsSeen);
|
2010-04-07 15:55:46 +02:00
|
|
|
} else
|
|
|
|
doc.writeEmptyElement("repeated");
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
XMLOpenElement _(doc, "attrs");
|
2010-05-07 16:46:47 +02:00
|
|
|
showAttrs(state, strict, location, *v.attrs, doc, context, drvsSeen);
|
2010-04-07 15:55:46 +02:00
|
|
|
}
|
2015-07-17 19:24:28 +02:00
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
break;
|
2007-01-13 16:11:10 +01:00
|
|
|
|
2015-07-23 22:05:09 +02:00
|
|
|
case tList1: case tList2: case tListN: {
|
2010-04-07 15:55:46 +02:00
|
|
|
XMLOpenElement _(doc, "list");
|
2015-07-23 22:05:09 +02:00
|
|
|
for (unsigned int n = 0; n < v.listSize(); ++n)
|
|
|
|
printValueAsXML(state, strict, location, *v.listElems()[n], doc, context, drvsSeen);
|
2010-04-07 15:55:46 +02:00
|
|
|
break;
|
2007-01-13 16:11:10 +01:00
|
|
|
}
|
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
case tLambda: {
|
2010-05-07 16:46:47 +02:00
|
|
|
XMLAttrs xmlAttrs;
|
|
|
|
if (location) posToXML(xmlAttrs, v.lambda.fun->pos);
|
|
|
|
XMLOpenElement _(doc, "function", xmlAttrs);
|
2015-07-17 19:24:28 +02:00
|
|
|
|
2010-04-12 20:30:11 +02:00
|
|
|
if (v.lambda.fun->matchAttrs) {
|
|
|
|
XMLAttrs attrs;
|
|
|
|
if (!v.lambda.fun->arg.empty()) attrs["name"] = v.lambda.fun->arg;
|
|
|
|
if (v.lambda.fun->formals->ellipsis) attrs["ellipsis"] = "1";
|
|
|
|
XMLOpenElement _(doc, "attrspat", attrs);
|
2015-07-17 19:24:28 +02:00
|
|
|
for (auto & i : v.lambda.fun->formals->formals)
|
|
|
|
doc.writeEmptyElement("attr", singletonAttrs("name", i.name));
|
2010-04-12 20:30:11 +02:00
|
|
|
} else
|
|
|
|
doc.writeEmptyElement("varpat", singletonAttrs("name", v.lambda.fun->arg));
|
2015-07-17 19:24:28 +02:00
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
break;
|
2006-08-24 16:16:55 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 19:16:19 +01:00
|
|
|
case tExternal:
|
|
|
|
v.external->printValueAsXML(state, strict, location, doc, context, drvsSeen);
|
|
|
|
break;
|
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
default:
|
|
|
|
doc.writeEmptyElement("unevaluated");
|
2006-08-24 16:16:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-30 19:16:19 +01:00
|
|
|
void ExternalValueBase::printValueAsXML(EvalState & state, bool strict,
|
2014-12-02 16:02:03 +01:00
|
|
|
bool location, XMLWriter & doc, PathSet & context, PathSet & drvsSeen) const
|
2014-11-30 19:16:19 +01:00
|
|
|
{
|
|
|
|
doc.writeEmptyElement("unevaluated");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-07 16:46:47 +02:00
|
|
|
void printValueAsXML(EvalState & state, bool strict, bool location,
|
2010-04-07 15:55:46 +02:00
|
|
|
Value & v, std::ostream & out, PathSet & context)
|
2006-08-24 16:16:55 +02:00
|
|
|
{
|
|
|
|
XMLWriter doc(true, out);
|
|
|
|
XMLOpenElement root(doc, "expr");
|
2015-07-17 19:24:28 +02:00
|
|
|
PathSet drvsSeen;
|
2010-05-07 16:46:47 +02:00
|
|
|
printValueAsXML(state, strict, location, v, doc, context, drvsSeen);
|
2006-08-24 16:16:55 +02:00
|
|
|
}
|
2006-09-04 23:06:23 +02:00
|
|
|
|
2015-07-17 19:24:28 +02:00
|
|
|
|
2006-09-04 23:06:23 +02:00
|
|
|
}
|