Skip to content

Commit

Permalink
Merge pull request #22 from tsion/quote-invalid-vars
Browse files Browse the repository at this point in the history
Print syntactially invalid attribute names as strings.
  • Loading branch information
edolstra committed Feb 23, 2016
2 parents a52fd0d + 97da6d6 commit 5599665
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions nix-repl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,19 @@ std::ostream & NixRepl::printValue(std::ostream & str, Value & v, unsigned int m
}


std::ostream & printStringValue(std::ostream & str, const char * string) {
str << "\"";
for (const char * i = string; *i; i++)
if (*i == '\"' || *i == '\\') str << "\\" << *i;
else if (*i == '\n') str << "\\n";
else if (*i == '\r') str << "\\r";
else if (*i == '\t') str << "\\t";
else str << *i;
str << "\"";
return str;
}


// FIXME: lot of cut&paste from Nix's eval.cc.
std::ostream & NixRepl::printValue(std::ostream & str, Value & v, unsigned int maxDepth, ValuesSeen & seen)
{
Expand All @@ -491,14 +504,7 @@ std::ostream & NixRepl::printValue(std::ostream & str, Value & v, unsigned int m
break;

case tString:
str << "\"";
for (const char * i = v.string.s; *i; i++)
if (*i == '\"' || *i == '\\') str << "\\" << *i;
else if (*i == '\n') str << "\\n";
else if (*i == '\r') str << "\\r";
else if (*i == '\t') str << "\\t";
else str << *i;
str << "\"";
printStringValue(str, v.string.s);
break;

case tPath:
Expand Down Expand Up @@ -546,7 +552,11 @@ std::ostream & NixRepl::printValue(std::ostream & str, Value & v, unsigned int m
}

for (auto & i : sorted) {
str << i.first << " = ";
if (isVarName(i.first))
str << i.first;
else
printStringValue(str, i.first.c_str());
str << " = ";
if (hidden.find(i.first) != hidden.end())
str << "«...»";
else if (seen.find(i.second) != seen.end())
Expand Down

0 comments on commit 5599665

Please sign in to comment.