From b1117ef29d35822647bda32f8cd3887f4f6eaede Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 18 Nov 2003 11:38:25 +0000 Subject: * nix -> nix-store, fix -> nix-instantiate. --- configure.ac | 4 +- src/Makefile.am | 2 +- src/fix-ng/Makefile.am | 26 ---- src/fix-ng/bin2c.c | 23 --- src/fix-ng/eval.cc | 265 ---------------------------------- src/fix-ng/eval.hh | 42 ------ src/fix-ng/fix.cc | 117 ---------------- src/fix-ng/fix.sdf | 203 --------------------------- src/fix-ng/fixexpr.cc | 215 ---------------------------- src/fix-ng/fixexpr.hh | 75 ---------- src/fix-ng/parser.cc | 165 ---------------------- src/fix-ng/parser.hh | 10 -- src/fix-ng/primops.cc | 246 -------------------------------- src/fix-ng/primops.hh | 34 ----- src/nix-instantiate/Makefile.am | 26 ++++ src/nix-instantiate/bin2c.c | 23 +++ src/nix-instantiate/eval.cc | 265 ++++++++++++++++++++++++++++++++++ src/nix-instantiate/eval.hh | 42 ++++++ src/nix-instantiate/fix-expr.cc | 215 ++++++++++++++++++++++++++++ src/nix-instantiate/fix-expr.hh | 75 ++++++++++ src/nix-instantiate/fix.cc | 117 ++++++++++++++++ src/nix-instantiate/fix.sdf | 203 +++++++++++++++++++++++++++ src/nix-instantiate/fixexpr.cc | 215 ++++++++++++++++++++++++++++ src/nix-instantiate/fixexpr.hh | 75 ++++++++++ src/nix-instantiate/parser.cc | 165 ++++++++++++++++++++++ src/nix-instantiate/parser.hh | 10 ++ src/nix-instantiate/primops.cc | 246 ++++++++++++++++++++++++++++++++ src/nix-instantiate/primops.hh | 34 +++++ src/nix-store/Makefile.am | 27 ++++ src/nix-store/dotgraph.cc | 135 ++++++++++++++++++ src/nix-store/dotgraph.hh | 8 ++ src/nix-store/nix-help.txt | 34 +++++ src/nix-store/nix.cc | 304 ++++++++++++++++++++++++++++++++++++++++ src/nix/Makefile.am | 27 ---- src/nix/dotgraph.cc | 135 ------------------ src/nix/dotgraph.hh | 8 -- src/nix/nix-help.txt | 32 ----- src/nix/nix.cc | 304 ---------------------------------------- 38 files changed, 2222 insertions(+), 1930 deletions(-) delete mode 100644 src/fix-ng/Makefile.am delete mode 100644 src/fix-ng/bin2c.c delete mode 100644 src/fix-ng/eval.cc delete mode 100644 src/fix-ng/eval.hh delete mode 100644 src/fix-ng/fix.cc delete mode 100644 src/fix-ng/fix.sdf delete mode 100644 src/fix-ng/fixexpr.cc delete mode 100644 src/fix-ng/fixexpr.hh delete mode 100644 src/fix-ng/parser.cc delete mode 100644 src/fix-ng/parser.hh delete mode 100644 src/fix-ng/primops.cc delete mode 100644 src/fix-ng/primops.hh create mode 100644 src/nix-instantiate/Makefile.am create mode 100644 src/nix-instantiate/bin2c.c create mode 100644 src/nix-instantiate/eval.cc create mode 100644 src/nix-instantiate/eval.hh create mode 100644 src/nix-instantiate/fix-expr.cc create mode 100644 src/nix-instantiate/fix-expr.hh create mode 100644 src/nix-instantiate/fix.cc create mode 100644 src/nix-instantiate/fix.sdf create mode 100644 src/nix-instantiate/fixexpr.cc create mode 100644 src/nix-instantiate/fixexpr.hh create mode 100644 src/nix-instantiate/parser.cc create mode 100644 src/nix-instantiate/parser.hh create mode 100644 src/nix-instantiate/primops.cc create mode 100644 src/nix-instantiate/primops.hh create mode 100644 src/nix-store/Makefile.am create mode 100644 src/nix-store/dotgraph.cc create mode 100644 src/nix-store/dotgraph.hh create mode 100644 src/nix-store/nix-help.txt create mode 100644 src/nix-store/nix.cc delete mode 100644 src/nix/Makefile.am delete mode 100644 src/nix/dotgraph.cc delete mode 100644 src/nix/dotgraph.hh delete mode 100644 src/nix/nix-help.txt delete mode 100644 src/nix/nix.cc diff --git a/configure.ac b/configure.ac index 9d4b41b6bdba..a0ab10dc4dd9 100644 --- a/configure.ac +++ b/configure.ac @@ -31,9 +31,9 @@ AC_CONFIG_FILES([Makefile src/libutil/Makefile src/libstore/Makefile src/libmain/Makefile - src/nix/Makefile + src/nix-store/Makefile src/nix-hash/Makefile - src/fix-ng/Makefile + src/nix-instantiate/Makefile scripts/Makefile corepkgs/Makefile corepkgs/fetchurl/Makefile diff --git a/src/Makefile.am b/src/Makefile.am index ed989fed20b2..e35b82a889f0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1 +1 @@ -SUBDIRS = boost libutil libstore libmain nix nix-hash fix-ng +SUBDIRS = boost libutil libstore libmain nix-store nix-hash nix-instantiate diff --git a/src/fix-ng/Makefile.am b/src/fix-ng/Makefile.am deleted file mode 100644 index d30a5b2ea065..000000000000 --- a/src/fix-ng/Makefile.am +++ /dev/null @@ -1,26 +0,0 @@ -bin_PROGRAMS = fix-ng - -fix_ng_SOURCES = fixexpr.cc parser.cc eval.cc primops.cc fix.cc -fix_ng_LDADD = ../libmain/libmain.a ../libstore/libstore.a ../libutil/libutil.a \ - ../boost/format/libformat.a -L../../externals/inst/lib -ldb_cxx \ - -lsglr -lATB -lconversion -lasfix2 -lmept -lATerm - -AM_CXXFLAGS = \ - -I.. -I../../externals/inst/include -I../libutil -I../libstore -I../libmain - - -# Parse table generation. - -parser.o: parse-table.h - -parse-table.h: fix.tbl bin2c - ./bin2c fixParseTable < $< > $@ || (rm $@ && exit 1) - -noinst_PROGRAMS = bin2c - -bin2c_SOURCES = bin2c.c - -%.tbl: %.sdf - ../../externals/inst/bin/sdf2table -i $< -o $@ - -CLEANFILES = parse-table.h fix.tbl diff --git a/src/fix-ng/bin2c.c b/src/fix-ng/bin2c.c deleted file mode 100644 index 18bf81d69e25..000000000000 --- a/src/fix-ng/bin2c.c +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include -#include - -void print(const char *format, ...) -{ - va_list ap; - va_start(ap, format); - if (vprintf(format, ap) < 0) abort(); - va_end(ap); -} - -int main(int argc, char * * argv) -{ - int c; - if (argc != 2) abort(); - print("static unsigned char %s[] = {", argv[1]); - while ((c = getchar()) != EOF) { - print("0x%02x, ", (unsigned char) c); - } - print("};\n"); - return 0; -} diff --git a/src/fix-ng/eval.cc b/src/fix-ng/eval.cc deleted file mode 100644 index b110c3a4a41c..000000000000 --- a/src/fix-ng/eval.cc +++ /dev/null @@ -1,265 +0,0 @@ -#include "eval.hh" -#include "parser.hh" -#include "primops.hh" - - -EvalState::EvalState() - : normalForms(32768, 75) -{ - blackHole = ATmake("BlackHole()"); - if (!blackHole) throw Error("cannot build black hole"); - nrEvaluated = nrCached = 0; -} - - -/* Substitute an argument set into the body of a function. */ -static Expr substArgs(Expr body, ATermList formals, Expr arg) -{ - ATMatcher m; - ATermMap subs; - Expr undefined = ATmake("Undefined"); - - /* Get the formal arguments. */ - for (ATermIterator i(formals); i; ++i) { - Expr name, def; - if (atMatch(m, *i) >> "NoDefFormal" >> name) - subs.set(name, undefined); - else if (atMatch(m, *i) >> "DefFormal" >> name >> def) - subs.set(name, def); - else abort(); /* can't happen */ - } - - /* Get the actual arguments, and check that they match with the - formals. */ - ATermMap args; - queryAllAttrs(arg, args); - for (ATermIterator i(args.keys()); i; ++i) { - Expr key = *i; - Expr cur = subs.get(key); - if (!cur) - throw badTerm(format("function has no formal argument `%1%'") - % aterm2String(key), arg); - subs.set(key, args.get(key)); - } - - /* Check that all arguments are defined. */ - for (ATermIterator i(subs.keys()); i; ++i) - if (subs.get(*i) == undefined) - throw badTerm(format("formal argument `%1%' missing") - % aterm2String(*i), arg); - - return substitute(subs, body); -} - - -/* Transform a mutually recursive set into a non-recursive set. Each - attribute is transformed into an expression that has all references - to attributes substituted with selection expressions on the - original set. E.g., e = `rec {x = f x y, y = x}' becomes `{x = f - (e.x) (e.y), y = e.x}'. */ -ATerm expandRec(ATerm e, ATermList bnds) -{ - ATMatcher m; - - /* Create the substitution list. */ - ATermMap subs; - for (ATermIterator i(bnds); i; ++i) { - string s; - Expr e2; - if (!(atMatch(m, *i) >> "Bind" >> s >> e2)) - abort(); /* can't happen */ - subs.set(s, ATmake("Select(, )", e, s.c_str())); - } - - /* Create the non-recursive set. */ - ATermMap as; - for (ATermIterator i(bnds); i; ++i) { - string s; - Expr e2; - if (!(atMatch(m, *i) >> "Bind" >> s >> e2)) - abort(); /* can't happen */ - as.set(s, substitute(subs, e2)); - } - - return makeAttrs(as); -} - - -string evalString(EvalState & state, Expr e) -{ - e = evalExpr(state, e); - ATMatcher m; - string s; - if (!(atMatch(m, e) >> "Str" >> s)) - throw badTerm("string expected", e); - return s; -} - - -Path evalPath(EvalState & state, Expr e) -{ - e = evalExpr(state, e); - ATMatcher m; - string s; - if (!(atMatch(m, e) >> "Path" >> s)) - throw badTerm("path expected", e); - return s; -} - - -bool evalBool(EvalState & state, Expr e) -{ - e = evalExpr(state, e); - ATMatcher m; - if (atMatch(m, e) >> "Bool" >> "True") return true; - else if (atMatch(m, e) >> "Bool" >> "False") return false; - else throw badTerm("expecting a boolean", e); -} - - -Expr evalExpr2(EvalState & state, Expr e) -{ - ATMatcher m; - Expr e1, e2, e3, e4; - string s1; - - /* Normal forms. */ - if (atMatch(m, e) >> "Str" || - atMatch(m, e) >> "Path" || - atMatch(m, e) >> "Uri" || - atMatch(m, e) >> "Bool" || - atMatch(m, e) >> "Function" || - atMatch(m, e) >> "Attrs" || - atMatch(m, e) >> "List") - return e; - - /* Any encountered variables must be undeclared or primops. */ - if (atMatch(m, e) >> "Var" >> s1) { - if (s1 == "null") return primNull(state); - return e; - } - - /* Function application. */ - if (atMatch(m, e) >> "Call" >> e1 >> e2) { - - ATermList formals; - - /* Evaluate the left-hand side. */ - e1 = evalExpr(state, e1); - - /* Is it a primop or a function? */ - if (atMatch(m, e1) >> "Var" >> s1) { - if (s1 == "import") return primImport(state, e2); - if (s1 == "derivation") return primDerivation(state, e2); - if (s1 == "toString") return primToString(state, e2); - if (s1 == "baseNameOf") return primBaseNameOf(state, e2); - if (s1 == "isNull") return primIsNull(state, e2); - else throw badTerm("undefined variable/primop", e1); - } - - else if (atMatch(m, e1) >> "Function" >> formals >> e4) - return evalExpr(state, - substArgs(e4, formals, evalExpr(state, e2))); - - else throw badTerm("expecting a function or primop", e1); - } - - /* Attribute selection. */ - if (atMatch(m, e) >> "Select" >> e1 >> s1) { - Expr a = queryAttr(evalExpr(state, e1), s1); - if (!a) throw badTerm(format("missing attribute `%1%'") % s1, e); - return evalExpr(state, a); - } - - /* Mutually recursive sets. */ - ATermList bnds; - if (atMatch(m, e) >> "Rec" >> bnds) - return expandRec(e, bnds); - - /* Let expressions `let {..., body = ...}' are just desugared - into `(rec {..., body = ...}).body'. */ - if (atMatch(m, e) >> "LetRec" >> bnds) - return evalExpr(state, ATmake("Select(Rec(), \"body\")", bnds)); - - /* Conditionals. */ - if (atMatch(m, e) >> "If" >> e1 >> e2 >> e3) { - if (evalBool(state, e1)) - return evalExpr(state, e2); - else - return evalExpr(state, e3); - } - - /* Assertions. */ - if (atMatch(m, e) >> "Assert" >> e1 >> e2) { - if (!evalBool(state, e1)) throw badTerm("guard failed", e); - return evalExpr(state, e2); - } - - /* Generic equality. */ - if (atMatch(m, e) >> "OpEq" >> e1 >> e2) - return makeBool(evalExpr(state, e1) == evalExpr(state, e2)); - - /* Generic inequality. */ - if (atMatch(m, e) >> "OpNEq" >> e1 >> e2) - return makeBool(evalExpr(state, e1) != evalExpr(state, e2)); - - /* Negation. */ - if (atMatch(m, e) >> "OpNot" >> e1) - return makeBool(!evalBool(state, e1)); - - /* Implication. */ - if (atMatch(m, e) >> "OpImpl" >> e1 >> e2) - return makeBool(!evalBool(state, e1) || evalBool(state, e2)); - - /* Conjunction (logical AND). */ - if (atMatch(m, e) >> "OpAnd" >> e1 >> e2) - return makeBool(evalBool(state, e1) && evalBool(state, e2)); - - /* Disjunction (logical OR). */ - if (atMatch(m, e) >> "OpOr" >> e1 >> e2) - return makeBool(evalBool(state, e1) || evalBool(state, e2)); - - /* Barf. */ - throw badTerm("invalid expression", e); -} - - -Expr evalExpr(EvalState & state, Expr e) -{ - startNest(nest, lvlVomit, - format("evaluating expression: %1%") % e); - - state.nrEvaluated++; - - /* Consult the memo table to quickly get the normal form of - previously evaluated expressions. */ - Expr nf = state.normalForms.get(e); - if (nf) { - if (nf == state.blackHole) - throw badTerm("infinite recursion", e); - state.nrCached++; - return nf; - } - - /* Otherwise, evaluate and memoize. */ - state.normalForms.set(e, state.blackHole); - nf = evalExpr2(state, e); - state.normalForms.set(e, nf); - return nf; -} - - -Expr evalFile(EvalState & state, const Path & path) -{ - startNest(nest, lvlTalkative, format("evaluating file `%1%'") % path); - Expr e = parseExprFromFile(path); - return evalExpr(state, e); -} - - -void printEvalStats(EvalState & state) -{ - debug(format("evaluated %1% expressions, %2% cache hits, %3%%% efficiency") - % state.nrEvaluated % state.nrCached - % ((float) state.nrCached / (float) state.nrEvaluated * 100)); -} diff --git a/src/fix-ng/eval.hh b/src/fix-ng/eval.hh deleted file mode 100644 index 061c840a7576..000000000000 --- a/src/fix-ng/eval.hh +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef __EVAL_H -#define __EVAL_H - -#include - -#include "aterm.hh" -#include "hash.hh" -#include "fixexpr.hh" - - -typedef map DrvPaths; -typedef map DrvHashes; - -struct EvalState -{ - ATermMap normalForms; - DrvPaths drvPaths; - DrvHashes drvHashes; /* normalised derivation hashes */ - Expr blackHole; - - unsigned int nrEvaluated; - unsigned int nrCached; - - EvalState(); -}; - - -/* Evaluate an expression to normal form. */ -Expr evalExpr(EvalState & state, Expr e); - -/* Evaluate an expression read from the given file to normal form. */ -Expr evalFile(EvalState & state, const Path & path); - -/* Specific results. */ -string evalString(EvalState & state, Expr e); -Path evalPath(EvalState & state, Expr e); - -/* Print statistics. */ -void printEvalStats(EvalState & state); - - -#endif /* !__EVAL_H */ diff --git a/src/fix-ng/fix.cc b/src/fix-ng/fix.cc deleted file mode 100644 index e407aaf44346..000000000000 --- a/src/fix-ng/fix.cc +++ /dev/null @@ -1,117 +0,0 @@ -#include -#include - -#include "globals.hh" -#include "normalise.hh" -#include "shared.hh" -#include "eval.hh" - - -#if 0 -static Path searchPath(const Paths & searchDirs, const Path & relPath) -{ - if (string(relPath, 0, 1) == "/") return relPath; - - for (Paths::const_iterator i = searchDirs.begin(); - i != searchDirs.end(); i++) - { - Path path = *i + "/" + relPath; - if (pathExists(path)) return path; - } - - throw Error( - format("path `%1%' not found in any of the search directories") - % relPath); -} -#endif - - -static Expr evalStdin(EvalState & state) -{ - startNest(nest, lvlTalkative, format("evaluating standard input")); - Expr e = ATreadFromFile(stdin); - if (!e) - throw Error(format("unable to read a term from stdin")); - return evalExpr(state, e); -} - - -static void printNixExpr(EvalState & state, Expr e) -{ - ATMatcher m; - ATermList es; - - if (atMatch(m, e) >> "Attrs" >> es) { - Expr a = queryAttr(e, "type"); - if (a && evalString(state, a) == "derivation") { - a = queryAttr(e, "drvPath"); - if (a) { - cout << format("%1%\n") % evalPath(state, a); - return; - } - } - } - - if (ATgetType(e) == AT_LIST) { - for (ATermIterator i((ATermList) e); i; ++i) - printNixExpr(state, evalExpr(state, *i)); - return; - } - - throw badTerm("top level does not evaluate to one or more Nix expressions", e); -} - - -void run(Strings args) -{ - EvalState state; - Strings files; - bool readStdin = false; - -#if 0 - state.searchDirs.push_back("."); - state.searchDirs.push_back(nixDataDir + "/fix"); -#endif - - for (Strings::iterator it = args.begin(); - it != args.end(); ) - { - string arg = *it++; - -#if 0 - if (arg == "--includedir" || arg == "-I") { - if (it == args.end()) - throw UsageError(format("argument required in `%1%'") % arg); - state.searchDirs.push_back(*it++); - } - else -#endif - if (arg == "--verbose" || arg == "-v") - verbosity = (Verbosity) ((int) verbosity + 1); - else if (arg == "-") - readStdin = true; - else if (arg[0] == '-') - throw UsageError(format("unknown flag `%1%`") % arg); - else - files.push_back(arg); - } - - openDB(); - - if (readStdin) { - Expr e = evalStdin(state); - printNixExpr(state, e); - } - - for (Strings::iterator it = files.begin(); - it != files.end(); it++) - { - Expr e = evalFile(state, absPath(*it)); - printNixExpr(state, e); - } - - printEvalStats(state); -} - - -string programId = "fix"; diff --git a/src/fix-ng/fix.sdf b/src/fix-ng/fix.sdf deleted file mode 100644 index 54f5d5266f79..000000000000 --- a/src/fix-ng/fix.sdf +++ /dev/null @@ -1,203 +0,0 @@ -definition - -module Main -imports Fix - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% Top level syntax. - -module Fix -imports Fix-Exprs Fix-Layout - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% Expressions. - -module Fix-Exprs -imports Fix-Lexicals URI -exports - sorts Expr Bind Formal - context-free syntax - - Id -> Expr {cons("Var")} - - Int -> Expr {cons("Int")} - - Str -> Expr {cons("Str")} - - Uri -> Expr {cons("Uri")} - - Path -> Expr {cons("Path")} - - "(" Expr ")" -> Expr {bracket} - - Expr Expr -> Expr {cons("Call"), left} - - "{" {Formal ","}* "}" ":" Expr -> Expr {cons("Function"), right} - Id -> Formal {cons("NoDefFormal")} - Id "?" Expr -> Formal {cons("DefFormal")} - - "assert" Expr ";" Expr -> Expr {cons("Assert"), right} - - "rec" "{" Binds "}" -> Expr {cons("Rec")} - "let" "{" Binds "}" -> Expr {cons("LetRec")} - "{" Binds "}" -> Expr {cons("Attrs")} - - Id "=" Expr -> Bind {cons("Bind")} - {Bind ";"}* -> Binds - Bind ";" -> BindSemi - BindSemi* -> Binds - - "[" ExprList "]" -> Expr {cons("List")} - "" -> ExprList {cons("ExprNil")} - Expr ExprList -> ExprList {cons("ExprCons")} - - Expr "." Id -> Expr {cons("Select")} - - "if" Expr "then" Expr "else" Expr -> Expr {cons("If")} - - Expr "==" Expr -> Expr {cons("OpEq"), non-assoc} - Expr "!=" Expr -> Expr {cons("OpNEq"), non-assoc} - - "!" Expr -> Expr {cons("OpNot")} - Expr "&&" Expr -> Expr {cons("OpAnd"), right} - Expr "||" Expr -> Expr {cons("OpOr"), right} - Expr "->" Expr -> Expr {cons("OpImpl"), right} - - Bool -> Expr {cons("Bool")} - - context-free priorities - - Expr "." Id -> Expr - > Expr ExprList -> ExprList - > Expr Expr -> Expr - > "!" Expr -> Expr - > Expr "==" Expr -> Expr - > Expr "!=" Expr -> Expr - > Expr "&&" Expr -> Expr - > Expr "||" Expr -> Expr - > Expr "->" Expr -> Expr - > "assert" Expr ";" Expr -> Expr - > "{" {Formal ","}* "}" ":" Expr -> Expr - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% Lexical syntax. - -module Fix-Lexicals -exports - sorts Id Path - lexical syntax - [a-zA-Z\_][a-zA-Z0-9\_\']* -> Id - "rec" -> Id {reject} - "let" -> Id {reject} - "if" -> Id {reject} - "then" -> Id {reject} - "else" -> Id {reject} - "true" -> Id {reject} - "false" -> Id {reject} - "assert" -> Id {reject} - - [0-9]+ -> Int - - "\"" ~[\n\"]* "\"" -> Str - - PathComp ("/" PathComp)+ -> Path - [a-zA-Z0-9\.\_\-\+]+ -> PathComp - - "true" -> Bool - "false" -> Bool - - lexical restrictions - Id -/- [a-zA-Z0-9\_\'] - Int -/- [0-9] - Path -/- [a-zA-Z0-9\.\_\-\+\/] - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% URIs (RFC 2396, appendix A). - -module URI -exports - sorts Uri - lexical syntax - Uscheme ":" (Uhierpath | Uopaquepath) -> Uri - - (Unetpath | Uabspath) ("?" Uquery)? -> Uhierpath - Uuricnoslash Uuric* -> Uopaquepath - - Uunreserved | Uescaped | [\;\?\:\@\&\=\+\$\,] -> Uuricnoslash - - "//" Uauthority Uabspath? -> Unetpath - "/" Upathsegments -> Uabspath - "//" Uuric* -> Uabspath {reject} - Urelsegment Uabspath? -> Urelpath - - (Uunreserved | Uescaped | [\;\@\&\=\+\$\,])+ -> Urelsegment - - Ualpha (Ualpha | Udigit | [\+\-\.])* -> Uscheme - - Userver | Uregname -> Uauthority - - (Uunreserved | Uescaped | [\$\,\;\:\@\&\=\+])+ -> Uregname - - ((Uuserinfo "@") Uhostport) -> Userver - (Uunreserved | Uescaped | [\;\:\&\=\+\$\,])* -> Uuserinfo - - Uhost (":" Uport)? -> Uhostport - Uhostname | UIPv4address -> Uhost - (Udomainlabel ".")+ Utoplabel "."? -> Uhostname - Ualphanum | Ualphanum (Ualphanum | "-")* Ualphanum -> Udomainlabel - Ualpha | Ualpha (Ualphanum | "-")* Ualphanum -> Utoplabel - Udigit+ "." Udigit+ "." Udigit+ "." Udigit+ -> UIPv4address - Udigit* -> Uport - - Uabspath | Uopaquepart -> Upath - Usegment ("/" Usegment)* -> Upathsegments - Upchar* (";" Uparam)* -> Usegment - Upchar* -> Uparam - Uunreserved | Uescaped | [\:\@\&\=\+\$\,] -> Upchar - - Uuric* -> Uquery - - Uuric* -> Ufragment - - Ureserved | Uunreserved | Uescaped -> Uuric - [\;\/\?\:\@\&\=\+\$\,] -> Ureserved - Ualphanum | Umark -> Uunreserved - [\-\_\.\!\~\*\'\(\)] -> Umark - - "%" Uhex Uhex -> Uescaped - Udigit | [A-Fa-f] -> Uhex - - Ualpha | Udigit -> Ualphanum - Ulowalpha | Uupalpha -> Ualpha - - [a-z] -> Ulowalpha - [A-Z] -> Uupalpha - [0-9] -> Udigit - - lexical restrictions - Uri -/- [a-zA-Z0-9\-\_\.\!\~\*\'\(\)] - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% Layout. - -module Fix-Layout -exports - lexical syntax - [\ \t\n] -> LAYOUT - HashComment -> LAYOUT - Comment -> LAYOUT - "#" ~[\n]* [\n] -> HashComment - "//" ~[\n]* [\n] -> HashComment - "/*" ( ~[\*] | Asterisk )* "*/" -> Comment - [\*] -> Asterisk - lexical restrictions - Asterisk -/- [\/] - context-free restrictions - LAYOUT? -/- [\ \t\n] | [\#] - syntax - HashComment -> diff --git a/src/fix-ng/fixexpr.cc b/src/fix-ng/fixexpr.cc deleted file mode 100644 index 721fa8afa5f2..000000000000 --- a/src/fix-ng/fixexpr.cc +++ /dev/null @@ -1,215 +0,0 @@ -#include "fixexpr.hh" -#include "storeexpr.hh" - - -ATermMap::ATermMap(unsigned int initialSize, unsigned int maxLoadPct) -{ - table = ATtableCreate(initialSize, maxLoadPct); - if (!table) throw Error("cannot create ATerm table"); -} - - -ATermMap::ATermMap(const ATermMap & map) - : table(0) -{ - ATermList keys = map.keys(); - - /* !!! adjust allocation for load pct */ - table = ATtableCreate(ATgetLength(keys), map.maxLoadPct); - if (!table) throw Error("cannot create ATerm table"); - - for (ATermIterator i(keys); i; ++i) - set(*i, map.get(*i)); -} - - -ATermMap::~ATermMap() -{ - if (table) ATtableDestroy(table); -} - - -void ATermMap::set(ATerm key, ATerm value) -{ - return ATtablePut(table, key, value); -} - - -void ATermMap::set(const string & key, ATerm value) -{ - set(string2ATerm(key), value); -} - - -ATerm ATermMap::get(ATerm key) const -{ - return ATtableGet(table, key); -} - - -ATerm ATermMap::get(const string & key) const -{ - return get(string2ATerm(key)); -} - - -void ATermMap::remove(ATerm key) -{ - ATtableRemove(table, key); -} - - -void ATermMap::remove(const string & key) -{ - remove(string2ATerm(key)); -} - - -ATermList ATermMap::keys() const -{ - ATermList keys = ATtableKeys(table); - if (!keys) throw Error("cannot query aterm map keys"); - return keys; -} - - -ATerm string2ATerm(const string & s) -{ - return (ATerm) ATmakeAppl0(ATmakeAFun((char *) s.c_str(), 0, ATtrue)); -} - - -string aterm2String(ATerm t) -{ - return ATgetName(ATgetAFun(t)); -} - - -ATerm bottomupRewrite(TermFun & f, ATerm e) -{ - if (ATgetType(e) == AT_APPL) { - AFun fun = ATgetAFun(e); - int arity = ATgetArity(fun); - ATermList args = ATempty; - - for (int i = arity - 1; i >= 0; i--) - args = ATinsert(args, bottomupRewrite(f, ATgetArgument(e, i))); - - e = (ATerm) ATmakeApplList(fun, args); - } - - else if (ATgetType(e) == AT_LIST) { - ATermList in = (ATermList) e; - ATermList out = ATempty; - - for (ATermIterator i(in); i; ++i) - out = ATinsert(out, bottomupRewrite(f, *i)); - - e = (ATerm) ATreverse(out); - } - - return f(e); -} - - -void queryAllAttrs(Expr e, ATermMap & attrs) -{ - ATMatcher m; - ATermList bnds; - if (!(atMatch(m, e) >> "Attrs" >> bnds)) - throw badTerm("expected attribute set", e); - - for (ATermIterator i(bnds); i; ++i) { - string s; - Expr e; - if (!(atMatch(m, *i) >> "Bind" >> s >> e)) - abort(); /* can't happen */ - attrs.set(s, e); - } -} - - -Expr queryAttr(Expr e, const string & name) -{ - ATermMap attrs; - queryAllAttrs(e, attrs); - return attrs.get(name); -} - - -Expr makeAttrs(const ATermMap & attrs) -{ - ATermList bnds = ATempty; - for (ATermIterator i(attrs.keys()); i; ++i) - bnds = ATinsert(bnds, - ATmake("Bind(, )", *i, attrs.get(*i))); - return ATmake("Attrs()", ATreverse(bnds)); -} - - -Expr substitute(const ATermMap & subs, Expr e) -{ - ATMatcher m; - string s; - - if (atMatch(m, e) >> "Var" >> s) { - Expr sub = subs.get(s); - return sub ? sub : e; - } - - /* In case of a function, filter out all variables bound by this - function. */ - ATermList formals; - ATerm body; - if (atMatch(m, e) >> "Function" >> formals >> body) { - ATermMap subs2(subs); - for (ATermIterator i(formals); i; ++i) { - Expr def; - if (!(atMatch(m, *i) >> "NoDefFormal" >> s) && - !(atMatch(m, *i) >> "DefFormal" >> s >> def)) - abort(); - subs2.remove(s); - } - return ATmake("Function(, )", formals, - substitute(subs2, body)); - } - - /* Idem for a mutually recursive attribute set. */ - ATermList bindings; - if (atMatch(m, e) >> "Rec" >> bindings) { - ATermMap subs2(subs); - for (ATermIterator i(bindings); i; ++i) { - Expr e; - if (!(atMatch(m, *i) >> "Bind" >> s >> e)) - abort(); /* can't happen */ - subs2.remove(s); - } - return ATmake("Rec()", substitute(subs2, (ATerm) bindings)); - } - - if (ATgetType(e) == AT_APPL) { - AFun fun = ATgetAFun(e); - int arity = ATgetArity(fun); - ATermList args = ATempty; - - for (int i = arity - 1; i >= 0; i--) - args = ATinsert(args, substitute(subs, ATgetArgument(e, i))); - - return (ATerm) ATmakeApplList(fun, args); - } - - if (ATgetType(e) == AT_LIST) { - ATermList out = ATempty; - for (ATermIterator i((ATermList) e); i; ++i) - out = ATinsert(out, substitute(subs, *i)); - return (ATerm) ATreverse(out); - } - - return e; -} - - -Expr makeBool(bool b) -{ - return b ? ATmake("Bool(True)") : ATmake("Bool(False)"); -} diff --git a/src/fix-ng/fixexpr.hh b/src/fix-ng/fixexpr.hh deleted file mode 100644 index 6c1e51d9ccd6..000000000000 --- a/src/fix-ng/fixexpr.hh +++ /dev/null @@ -1,75 +0,0 @@ -#ifndef __FIXEXPR_H -#define __FIXEXPR_H - -#include - -#include - -#include "util.hh" - - -/* Fix expressions are represented as ATerms. The maximal sharing - property of the ATerm library allows us to implement caching of - normals forms efficiently. */ -typedef ATerm Expr; - - -/* Mappings from ATerms to ATerms. This is just a wrapper around - ATerm tables. */ -class ATermMap -{ -private: - unsigned int maxLoadPct; - ATermTable table; - -public: - ATermMap(unsigned int initialSize = 16, unsigned int maxLoadPct = 75); - ATermMap(const ATermMap & map); - ~ATermMap(); - - void set(ATerm key, ATerm value); - void set(const string & key, ATerm value); - - ATerm get(ATerm key) const; - ATerm get(const string & key) const; - - void remove(ATerm key); - void remove(const string & key); - - ATermList keys() const; -}; - - -/* Convert a string to an ATerm (i.e., a quoted nullary function - applicaton). */ -ATerm string2ATerm(const string & s); -string aterm2String(ATerm t); - -/* Generic bottomup traversal over ATerms. The traversal first - recursively descends into subterms, and then applies the given term - function to the resulting term. */ -struct TermFun -{ - virtual ATerm operator () (ATerm e) = 0; -}; -ATerm bottomupRewrite(TermFun & f, ATerm e); - -/* Query all attributes in an attribute set expression. The - expression must be in normal form. */ -void queryAllAttrs(Expr e, ATermMap & attrs); - -/* Query a specific attribute from an attribute set expression. The - expression must be in normal form. */ -Expr queryAttr(Expr e, const string & name); - -/* Create an attribute set expression from an Attrs value. */ -Expr makeAttrs(const ATermMap & attrs); - -/* Perform a set of substitutions on an expression. */ -Expr substitute(const ATermMap & subs, Expr e); - -/* Create an expression representing a boolean. */ -Expr makeBool(bool b); - - -#endif /* !__FIXEXPR_H */ diff --git a/src/fix-ng/parser.cc b/src/fix-ng/parser.cc deleted file mode 100644 index eaa41b3963ef..000000000000 --- a/src/fix-ng/parser.cc +++ /dev/null @@ -1,165 +0,0 @@ -#include - -#include -#include -#include -#include - -extern "C" { -#include -#include -} - -#include "aterm.hh" -#include "parser.hh" -#include "shared.hh" -#include "fixexpr.hh" -#include "parse-table.h" - - -/* Cleanup cleans up an imploded parse tree into an actual abstract - syntax tree that we can evaluate. It removes quotes around - strings, converts integer literals into actual integers, and - absolutises paths relative to the directory containing the input - file. */ -struct Cleanup : TermFun -{ - string basePath; - - virtual ATerm operator () (ATerm e) - { - ATMatcher m; - string s; - - if (atMatch(m, e) >> "Str" >> s) { - return ATmake("Str()", - string(s, 1, s.size() - 2).c_str()); - } - - if (atMatch(m, e) >> "Path" >> s) { - if (s[0] != '/') - s = basePath + "/" + s; - return ATmake("Path()", canonPath(s).c_str()); - } - - if (atMatch(m, e) >> "Int" >> s) { - istringstream s2(s); - int n; - s2 >> n; - return ATmake("Int()", n); - } - - if (atMatch(m, e) >> "Bool" >> "true") - return ATmake("Bool(True)"); - - if (atMatch(m, e) >> "Bool" >> "false") - return ATmake("Bool(False)"); - - if (atMatch(m, e) >> "ExprNil") - return (ATerm) ATempty; - - ATerm e1; - ATermList e2; - if (atMatch(m, e) >> "ExprCons" >> e1 >> e2) - return (ATerm) ATinsert(e2, e1); - - return e; - } -}; - - -Expr parseExprFromFile(Path path) -{ -#if 0 - /* Perhaps this is already an imploded parse tree? */ - Expr e = ATreadFromNamedFile(path.c_str()); - if (e) return e; -#endif - - /* If `path' refers to a directory, append `/default.fix'. */ - struct stat st; - if (stat(path.c_str(), &st)) - throw SysError(format("getting status of `%1%'") % path); - if (S_ISDIR(st.st_mode)) - path = canonPath(path + "/default.fix"); - - /* Initialise the SDF libraries. */ - static bool initialised = false; - static ATerm parseTable = 0; - static language lang = 0; - - if (!initialised) { - PT_initMEPTApi(); - PT_initAsFix2Api(); - SGinitParser(ATfalse); - - ATprotect(&parseTable); - parseTable = ATreadFromBinaryString( - (char *) fixParseTable, sizeof fixParseTable); - if (!parseTable) - throw Error(format("cannot construct parse table term")); - - ATprotect(&lang); - lang = ATmake("Fix"); - if (!SGopenLanguageFromTerm( - (char *) programId.c_str(), lang, parseTable)) - throw Error(format("cannot open language")); - - SG_STARTSYMBOL_ON(); - SG_OUTPUT_ON(); - SG_ASFIX2ME_ON(); - SG_AMBIGUITY_ERROR_ON(); - SG_FILTER_OFF(); - - initialised = true; - } - - /* Read the input file. We can't use SGparseFile() because it's - broken, so we read the input ourselves and call - SGparseString(). */ - AutoCloseFD fd = open(path.c_str(), O_RDONLY); - if (fd == -1) throw SysError(format("opening `%1%'") % path); - - if (fstat(fd, &st) == -1) - throw SysError(format("statting `%1%'") % path); - - char text[st.st_size + 1]; - readFull(fd, (unsigned char *) text, st.st_size); - text[st.st_size] = 0; - - /* Parse it. */ - ATerm result = SGparseString(lang, "Expr", text); - if (!result) - throw SysError(format("parse failed in `%1%'") % path); - if (SGisParseError(result)) - throw Error(format("parse error in `%1%': %2%") - % path % result); - - /* Implode it. */ - PT_ParseTree tree = PT_makeParseTreeFromTerm(result); - if (!tree) - throw Error(format("cannot create parse tree")); - - ATerm imploded = PT_implodeParseTree(tree, - ATtrue, - ATtrue, - ATtrue, - ATtrue, - ATtrue, - ATtrue, - ATfalse, - ATtrue, - ATtrue, - ATtrue, - ATfalse); - if (!imploded) - throw Error(format("cannot implode parse tree")); - - debug(format("imploded parse tree of `%1%': %2%") - % path % imploded); - - /* Finally, clean it up. */ - Cleanup cleanup; - cleanup.basePath = dirOf(path); - return bottomupRewrite(cleanup, imploded); -} diff --git a/src/fix-ng/parser.hh b/src/fix-ng/parser.hh deleted file mode 100644 index e44987dd014b..000000000000 --- a/src/fix-ng/parser.hh +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef __PARSER_H -#define __PARSER_H - -#include "fixexpr.hh" - - -Expr parseExprFromFile(Path path); - - -#endif /* !__PARSER_H */ diff --git a/src/fix-ng/primops.cc b/src/fix-ng/primops.cc deleted file mode 100644 index 097933115342..000000000000 --- a/src/fix-ng/primops.cc +++ /dev/null @@ -1,246 +0,0 @@ -#include "primops.hh" -#include "normalise.hh" -#include "globals.hh" - - -Expr primImport(EvalState & state, Expr arg) -{ - ATMatcher m; - string path; - if (!(atMatch(m, arg) >> "Path" >> path)) - throw badTerm("path expected", arg); - return evalFile(state, path); -} - - -static PathSet storeExprRootsCached(EvalState & state, const Path & nePath) -{ - DrvPaths::iterator i = state.drvPaths.find(nePath); - if (i != state.drvPaths.end()) - return i->second; - else { - PathSet paths = storeExprRoots(nePath); - state.drvPaths[nePath] = paths; - return paths; - } -} - - -static Hash hashDerivation(EvalState & state, StoreExpr ne) -{ - if (ne.type == StoreExpr::neDerivation) { - PathSet inputs2; - for (PathSet::iterator i = ne.derivation.inputs.begin(); - i != ne.derivation.inputs.end(); i++) - { - DrvHashes::iterator j = state.drvHashes.find(*i); - if (j == state.drvHashes.end()) - throw Error(format("don't know expression `%1%'") % (string) *i); - inputs2.insert(j->second); - } - ne.derivation.inputs = inputs2; - } - return hashTerm(unparseStoreExpr(ne)); -} - - -static Path copyAtom(EvalState & state, const Path & srcPath) -{ - /* !!! should be cached */ - Path dstPath(addToStore(srcPath)); - - ClosureElem elem; - StoreExpr ne; - ne.type = StoreExpr::neClosure; - ne.closure.roots.insert(dstPath); - ne.closure.elems[dstPath] = elem; - - Hash drvHash = hashDerivation(state, ne); - Path drvPath = writeTerm(unparseStoreExpr(ne), ""); - state.drvHashes[drvPath] = drvHash; - - printMsg(lvlChatty, format("copied `%1%' -> closure `%2%'") - % srcPath % drvPath); - return drvPath; -} - - -static string addInput(EvalState & state, - Path & nePath, StoreExpr & ne) -{ - PathSet paths = storeExprRootsCached(state, nePath); - if (paths.size() != 1) abort(); - Path path = *(paths.begin()); - ne.derivation.inputs.insert(nePath); - return path; -} - - -static string processBinding(EvalState & state, Expr e, StoreExpr & ne) -{ - e = evalExpr(state, e); - - ATMatcher m; - string s; - ATermList es; - - if (atMatch(m, e) >> "Str" >> s) return s; - if (atMatch(m, e) >> "Uri" >> s) return s; - if (atMatch(m, e) >> "Bool" >> "True") return "1"; - if (atMatch(m, e) >> "Bool" >> "False") return ""; - - if (atMatch(m, e) >> "Attrs" >> es) { - Expr a = queryAttr(e, "type"); - if (a && evalString(state, a) == "derivation") { - a = queryAttr(e, "drvPath"); - if (a) { - Path drvPath = evalPath(state, a); - return addInput(state, drvPath, ne); - } - } - } - - if (atMatch(m, e) >> "Path" >> s) { - Path drvPath = copyAtom(state, s); - return addInput(state, drvPath, ne); - } - - if (atMatch(m, e) >> "List" >> es) { - string s; - bool first = true; - for (ATermIterator i(es); i; ++i) { - startNest(nest, lvlVomit, format("processing list element")); - if (!first) s = s + " "; else first = false; - s += processBinding(state, evalExpr(state, *i), ne); - } - return s; - } - - if (atMatch(m, e) >> "Null") return ""; - - throw badTerm("invalid derivation binding", e); -} - - -Expr primDerivation(EvalState & state, Expr args) -{ - startNest(nest, lvlVomit, "evaluating derivation"); - - ATermMap attrs; - args = evalExpr(state, args); - queryAllAttrs(args, attrs); - - /* Build the derivation expression by processing the attributes. */ - StoreExpr ne; - ne.type = StoreExpr::neDerivation; - - string drvName; - Path outPath; - Hash outHash; - bool outHashGiven = false; - - for (ATermIterator i(attrs.keys()); i; ++i) { - string key = aterm2String(*i); - Expr value = attrs.get(key); - startNest(nest, lvlVomit, format("processing attribute `%1%'") % key); - - /* The `args' attribute is special: it supplies the - command-line arguments to the builder. */ - if (key == "args") { - throw Error("args not implemented"); -#if 0 - ATermList args; - if (!(ATmatch(value, "[]", &args)) - throw badTerm("list expected", value); - while (!ATisEmpty(args)) { - Expr arg = evalExpr(state, ATgetFirst(args)); - ne.derivation.args.push_back(processBinding(state, arg, ne)); - args = ATgetNext(args); - } -#endif - } - - /* All other attributes are passed to the builder through the - environment. */ - else { - string s = processBinding(state, value, ne); - ne.derivation.env[key] = s; - if (key == "builder") ne.derivation.builder = s; - else if (key == "system") ne.derivation.platform = s; - else if (key == "name") drvName = s; - else if (key == "outPath") outPath = s; - else if (key == "id") { - outHash = parseHash(s); - outHashGiven = true; - } - } - } - - /* Do we have all required attributes? */ - if (ne.derivation.builder == "") - throw badTerm("required attribute `builder' missing", args); - if (ne.derivation.platform == "") - throw badTerm("required attribute `system' missing", args); - if (drvName == "") - throw badTerm("required attribute `name' missing", args); - - /* Determine the output path. */ - if (!outHashGiven) outHash = hashDerivation(state, ne); - if (outPath == "") - /* Hash the Nix expression with no outputs to produce a - unique but deterministic path name for this derivation. */ - outPath = canonPath(nixStore + "/" + - ((string) outHash).c_str() + "-" + drvName); - ne.derivation.env["out"] = outPath; - ne.derivation.outputs.insert(outPath); - - /* Write the resulting term into the Nix store directory. */ - Hash drvHash = outHashGiven - ? hashString((string) outHash + outPath) - : hashDerivation(state, ne); - Path drvPath = writeTerm(unparseStoreExpr(ne), "-d-" + drvName); - state.drvHashes[drvPath] = drvHash; - - printMsg(lvlChatty, format("instantiated `%1%' -> `%2%'") - % drvName % drvPath); - - attrs.set("outPath", ATmake("Path()", outPath.c_str())); - attrs.set("drvPath", ATmake("Path()", drvPath.c_str())); - attrs.set("type", ATmake("Str(\"derivation\")")); - - return makeAttrs(attrs); -} - - -Expr primBaseNameOf(EvalState & state, Expr arg) -{ - string s = evalString(state, arg); - return ATmake("Str()", baseNameOf(s).c_str()); -} - - -Expr primToString(EvalState & state, Expr arg) -{ - arg = evalExpr(state, arg); - ATMatcher m; - string s; - if (atMatch(m, arg) >> "Str" >> s || - atMatch(m, arg) >> "Path" >> s || - atMatch(m, arg) >> "Uri" >> s) - return ATmake("Str()", s.c_str()); - else throw badTerm("cannot coerce to string", arg); -} - - -Expr primNull(EvalState & state) -{ - return ATmake("Null"); -} - - -Expr primIsNull(EvalState & state, Expr arg) -{ - arg = evalExpr(state, arg); - ATMatcher m; - return makeBool(atMatch(m, arg) >> "Null"); -} diff --git a/src/fix-ng/primops.hh b/src/fix-ng/primops.hh deleted file mode 100644 index 76d587afdb1e..000000000000 --- a/src/fix-ng/primops.hh +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef __PRIMOPS_H -#define __PRIMOPS_H - -#include "eval.hh" - - -/* Load and evaluate an expression from path specified by the - argument. */ -Expr primImport(EvalState & state, Expr arg); - -/* Construct (as a unobservable) side effect) a Nix derivation - expression that performs the derivation described by the argument - set. Returns the original set extended with the following - attributes: `outPath' containing the primary output path of the - derivation; `drvPath' containing the path of the Nix expression; - and `type' set to `derivation' to indicate that this is a - derivation. */ -Expr primDerivation(EvalState & state, Expr args); - -/* Return the base name of the given string, i.e., everything - following the last slash. */ -Expr primBaseNameOf(EvalState & state, Expr arg); - -/* Convert the argument (which can be a path or a uri) to a string. */ -Expr primToString(EvalState & state, Expr arg); - -/* Return the null value. */ -Expr primNull(EvalState & state); - -/* Determine whether the argument is the null value. */ -Expr primIsNull(EvalState & state, Expr arg); - - -#endif /* !__PRIMOPS_H */ diff --git a/src/nix-instantiate/Makefile.am b/src/nix-instantiate/Makefile.am new file mode 100644 index 000000000000..cdaec1390d9e --- /dev/null +++ b/src/nix-instantiate/Makefile.am @@ -0,0 +1,26 @@ +bin_PROGRAMS = nix-instantiate + +nix_instantiate_SOURCES = fixexpr.cc parser.cc eval.cc primops.cc fix.cc +nix_instantiate_LDADD = ../libmain/libmain.a ../libstore/libstore.a ../libutil/libutil.a \ + ../boost/format/libformat.a -L../../externals/inst/lib -ldb_cxx \ + -lsglr -lATB -lconversion -lasfix2 -lmept -lATerm + +AM_CXXFLAGS = \ + -I.. -I../../externals/inst/include -I../libutil -I../libstore -I../libmain + + +# Parse table generation. + +parser.o: parse-table.h + +parse-table.h: fix.tbl bin2c + ./bin2c fixParseTable < $< > $@ || (rm $@ && exit 1) + +noinst_PROGRAMS = bin2c + +bin2c_SOURCES = bin2c.c + +%.tbl: %.sdf + ../../externals/inst/bin/sdf2table -i $< -o $@ + +CLEANFILES = parse-table.h fix.tbl diff --git a/src/nix-instantiate/bin2c.c b/src/nix-instantiate/bin2c.c new file mode 100644 index 000000000000..18bf81d69e25 --- /dev/null +++ b/src/nix-instantiate/bin2c.c @@ -0,0 +1,23 @@ +#include +#include +#include + +void print(const char *format, ...) +{ + va_list ap; + va_start(ap, format); + if (vprintf(format, ap) < 0) abort(); + va_end(ap); +} + +int main(int argc, char * * argv) +{ + int c; + if (argc != 2) abort(); + print("static unsigned char %s[] = {", argv[1]); + while ((c = getchar()) != EOF) { + print("0x%02x, ", (unsigned char) c); + } + print("};\n"); + return 0; +} diff --git a/src/nix-instantiate/eval.cc b/src/nix-instantiate/eval.cc new file mode 100644 index 000000000000..b110c3a4a41c --- /dev/null +++ b/src/nix-instantiate/eval.cc @@ -0,0 +1,265 @@ +#include "eval.hh" +#include "parser.hh" +#include "primops.hh" + + +EvalState::EvalState() + : normalForms(32768, 75) +{ + blackHole = ATmake("BlackHole()"); + if (!blackHole) throw Error("cannot build black hole"); + nrEvaluated = nrCached = 0; +} + + +/* Substitute an argument set into the body of a function. */ +static Expr substArgs(Expr body, ATermList formals, Expr arg) +{ + ATMatcher m; + ATermMap subs; + Expr undefined = ATmake("Undefined"); + + /* Get the formal arguments. */ + for (ATermIterator i(formals); i; ++i) { + Expr name, def; + if (atMatch(m, *i) >> "NoDefFormal" >> name) + subs.set(name, undefined); + else if (atMatch(m, *i) >> "DefFormal" >> name >> def) + subs.set(name, def); + else abort(); /* can't happen */ + } + + /* Get the actual arguments, and check that they match with the + formals. */ + ATermMap args; + queryAllAttrs(arg, args); + for (ATermIterator i(args.keys()); i; ++i) { + Expr key = *i; + Expr cur = subs.get(key); + if (!cur) + throw badTerm(format("function has no formal argument `%1%'") + % aterm2String(key), arg); + subs.set(key, args.get(key)); + } + + /* Check that all arguments are defined. */ + for (ATermIterator i(subs.keys()); i; ++i) + if (subs.get(*i) == undefined) + throw badTerm(format("formal argument `%1%' missing") + % aterm2String(*i), arg); + + return substitute(subs, body); +} + + +/* Transform a mutually recursive set into a non-recursive set. Each + attribute is transformed into an expression that has all references + to attributes substituted with selection expressions on the + original set. E.g., e = `rec {x = f x y, y = x}' becomes `{x = f + (e.x) (e.y), y = e.x}'. */ +ATerm expandRec(ATerm e, ATermList bnds) +{ + ATMatcher m; + + /* Create the substitution list. */ + ATermMap subs; + for (ATermIterator i(bnds); i; ++i) { + string s; + Expr e2; + if (!(atMatch(m, *i) >> "Bind" >> s >> e2)) + abort(); /* can't happen */ + subs.set(s, ATmake("Select(, )", e, s.c_str())); + } + + /* Create the non-recursive set. */ + ATermMap as; + for (ATermIterator i(bnds); i; ++i) { + string s; + Expr e2; + if (!(atMatch(m, *i) >> "Bind" >> s >> e2)) + abort(); /* can't happen */ + as.set(s, substitute(subs, e2)); + } + + return makeAttrs(as); +} + + +string evalString(EvalState & state, Expr e) +{ + e = evalExpr(state, e); + ATMatcher m; + string s; + if (!(atMatch(m, e) >> "Str" >> s)) + throw badTerm("string expected", e); + return s; +} + + +Path evalPath(EvalState & state, Expr e) +{ + e = evalExpr(state, e); + ATMatcher m; + string s; + if (!(atMatch(m, e) >> "Path" >> s)) + throw badTerm("path expected", e); + return s; +} + + +bool evalBool(EvalState & state, Expr e) +{ + e = evalExpr(state, e); + ATMatcher m; + if (atMatch(m, e) >> "Bool" >> "True") return true; + else if (atMatch(m, e) >> "Bool" >> "False") return false; + else throw badTerm("expecting a boolean", e); +} + + +Expr evalExpr2(EvalState & state, Expr e) +{ + ATMatcher m; + Expr e1, e2, e3, e4; + string s1; + + /* Normal forms. */ + if (atMatch(m, e) >> "Str" || + atMatch(m, e) >> "Path" || + atMatch(m, e) >> "Uri" || + atMatch(m, e) >> "Bool" || + atMatch(m, e) >> "Function" || + atMatch(m, e) >> "Attrs" || + atMatch(m, e) >> "List") + return e; + + /* Any encountered variables must be undeclared or primops. */ + if (atMatch(m, e) >> "Var" >> s1) { + if (s1 == "null") return primNull(state); + return e; + } + + /* Function application. */ + if (atMatch(m, e) >> "Call" >> e1 >> e2) { + + ATermList formals; + + /* Evaluate the left-hand side. */ + e1 = evalExpr(state, e1); + + /* Is it a primop or a function? */ + if (atMatch(m, e1) >> "Var" >> s1) { + if (s1 == "import") return primImport(state, e2); + if (s1 == "derivation") return primDerivation(state, e2); + if (s1 == "toString") return primToString(state, e2); + if (s1 == "baseNameOf") return primBaseNameOf(state, e2); + if (s1 == "isNull") return primIsNull(state, e2); + else throw badTerm("undefined variable/primop", e1); + } + + else if (atMatch(m, e1) >> "Function" >> formals >> e4) + return evalExpr(state, + substArgs(e4, formals, evalExpr(state, e2))); + + else throw badTerm("expecting a function or primop", e1); + } + + /* Attribute selection. */ + if (atMatch(m, e) >> "Select" >> e1 >> s1) { + Expr a = queryAttr(evalExpr(state, e1), s1); + if (!a) throw badTerm(format("missing attribute `%1%'") % s1, e); + return evalExpr(state, a); + } + + /* Mutually recursive sets. */ + ATermList bnds; + if (atMatch(m, e) >> "Rec" >> bnds) + return expandRec(e, bnds); + + /* Let expressions `let {..., body = ...}' are just desugared + into `(rec {..., body = ...}).body'. */ + if (atMatch(m, e) >> "LetRec" >> bnds) + return evalExpr(state, ATmake("Select(Rec(), \"body\")", bnds)); + + /* Conditionals. */ + if (atMatch(m, e) >> "If" >> e1 >> e2 >> e3) { + if (evalBool(state, e1)) + return evalExpr(state, e2); + else + return evalExpr(state, e3); + } + + /* Assertions. */ + if (atMatch(m, e) >> "Assert" >> e1 >> e2) { + if (!evalBool(state, e1)) throw badTerm("guard failed", e); + return evalExpr(state, e2); + } + + /* Generic equality. */ + if (atMatch(m, e) >> "OpEq" >> e1 >> e2) + return makeBool(evalExpr(state, e1) == evalExpr(state, e2)); + + /* Generic inequality. */ + if (atMatch(m, e) >> "OpNEq" >> e1 >> e2) + return makeBool(evalExpr(state, e1) != evalExpr(state, e2)); + + /* Negation. */ + if (atMatch(m, e) >> "OpNot" >> e1) + return makeBool(!evalBool(state, e1)); + + /* Implication. */ + if (atMatch(m, e) >> "OpImpl" >> e1 >> e2) + return makeBool(!evalBool(state, e1) || evalBool(state, e2)); + + /* Conjunction (logical AND). */ + if (atMatch(m, e) >> "OpAnd" >> e1 >> e2) + return makeBool(evalBool(state, e1) && evalBool(state, e2)); + + /* Disjunction (logical OR). */ + if (atMatch(m, e) >> "OpOr" >> e1 >> e2) + return makeBool(evalBool(state, e1) || evalBool(state, e2)); + + /* Barf. */ + throw badTerm("invalid expression", e); +} + + +Expr evalExpr(EvalState & state, Expr e) +{ + startNest(nest, lvlVomit, + format("evaluating expression: %1%") % e); + + state.nrEvaluated++; + + /* Consult the memo table to quickly get the normal form of + previously evaluated expressions. */ + Expr nf = state.normalForms.get(e); + if (nf) { + if (nf == state.blackHole) + throw badTerm("infinite recursion", e); + state.nrCached++; + return nf; + } + + /* Otherwise, evaluate and memoize. */ + state.normalForms.set(e, state.blackHole); + nf = evalExpr2(state, e); + state.normalForms.set(e, nf); + return nf; +} + + +Expr evalFile(EvalState & state, const Path & path) +{ + startNest(nest, lvlTalkative, format("evaluating file `%1%'") % path); + Expr e = parseExprFromFile(path); + return evalExpr(state, e); +} + + +void printEvalStats(EvalState & state) +{ + debug(format("evaluated %1% expressions, %2% cache hits, %3%%% efficiency") + % state.nrEvaluated % state.nrCached + % ((float) state.nrCached / (float) state.nrEvaluated * 100)); +} diff --git a/src/nix-instantiate/eval.hh b/src/nix-instantiate/eval.hh new file mode 100644 index 000000000000..061c840a7576 --- /dev/null +++ b/src/nix-instantiate/eval.hh @@ -0,0 +1,42 @@ +#ifndef __EVAL_H +#define __EVAL_H + +#include + +#include "aterm.hh" +#include "hash.hh" +#include "fixexpr.hh" + + +typedef map DrvPaths; +typedef map DrvHashes; + +struct EvalState +{ + ATermMap normalForms; + DrvPaths drvPaths; + DrvHashes drvHashes; /* normalised derivation hashes */ + Expr blackHole; + + unsigned int nrEvaluated; + unsigned int nrCached; + + EvalState(); +}; + + +/* Evaluate an expression to normal form. */ +Expr evalExpr(EvalState & state, Expr e); + +/* Evaluate an expression read from the given file to normal form. */ +Expr evalFile(EvalState & state, const Path & path); + +/* Specific results. */ +string evalString(EvalState & state, Expr e); +Path evalPath(EvalState & state, Expr e); + +/* Print statistics. */ +void printEvalStats(EvalState & state); + + +#endif /* !__EVAL_H */ diff --git a/src/nix-instantiate/fix-expr.cc b/src/nix-instantiate/fix-expr.cc new file mode 100644 index 000000000000..e9c5a3ba633f --- /dev/null +++ b/src/nix-instantiate/fix-expr.cc @@ -0,0 +1,215 @@ +#include "fix-expr.hh" +#include "expr.hh" + + +ATermMap::ATermMap(unsigned int initialSize, unsigned int maxLoadPct) +{ + table = ATtableCreate(initialSize, maxLoadPct); + if (!table) throw Error("cannot create ATerm table"); +} + + +ATermMap::ATermMap(const ATermMap & map) + : table(0) +{ + ATermList keys = map.keys(); + + /* !!! adjust allocation for load pct */ + table = ATtableCreate(ATgetLength(keys), map.maxLoadPct); + if (!table) throw Error("cannot create ATerm table"); + + for (ATermIterator i(keys); i; ++i) + set(*i, map.get(*i)); +} + + +ATermMap::~ATermMap() +{ + if (table) ATtableDestroy(table); +} + + +void ATermMap::set(ATerm key, ATerm value) +{ + return ATtablePut(table, key, value); +} + + +void ATermMap::set(const string & key, ATerm value) +{ + set(string2ATerm(key), value); +} + + +ATerm ATermMap::get(ATerm key) const +{ + return ATtableGet(table, key); +} + + +ATerm ATermMap::get(const string & key) const +{ + return get(string2ATerm(key)); +} + + +void ATermMap::remove(ATerm key) +{ + ATtableRemove(table, key); +} + + +void ATermMap::remove(const string & key) +{ + remove(string2ATerm(key)); +} + + +ATermList ATermMap::keys() const +{ + ATermList keys = ATtableKeys(table); + if (!keys) throw Error("cannot query aterm map keys"); + return keys; +} + + +ATerm string2ATerm(const string & s) +{ + return (ATerm) ATmakeAppl0(ATmakeAFun((char *) s.c_str(), 0, ATtrue)); +} + + +string aterm2String(ATerm t) +{ + return ATgetName(ATgetAFun(t)); +} + + +ATerm bottomupRewrite(TermFun & f, ATerm e) +{ + if (ATgetType(e) == AT_APPL) { + AFun fun = ATgetAFun(e); + int arity = ATgetArity(fun); + ATermList args = ATempty; + + for (int i = arity - 1; i >= 0; i--) + args = ATinsert(args, bottomupRewrite(f, ATgetArgument(e, i))); + + e = (ATerm) ATmakeApplList(fun, args); + } + + else if (ATgetType(e) == AT_LIST) { + ATermList in = (ATermList) e; + ATermList out = ATempty; + + for (ATermIterator i(in); i; ++i) + out = ATinsert(out, bottomupRewrite(f, *i)); + + e = (ATerm) ATreverse(out); + } + + return f(e); +} + + +void queryAllAttrs(Expr e, ATermMap & attrs) +{ + ATMatcher m; + ATermList bnds; + if (!(atMatch(m, e) >> "Attrs" >> bnds)) + throw badTerm("expected attribute set", e); + + for (ATermIterator i(bnds); i; ++i) { + string s; + Expr e; + if (!(atMatch(m, *i) >> "Bind" >> s >> e)) + abort(); /* can't happen */ + attrs.set(s, e); + } +} + + +Expr queryAttr(Expr e, const string & name) +{ + ATermMap attrs; + queryAllAttrs(e, attrs); + return attrs.get(name); +} + + +Expr makeAttrs(const ATermMap & attrs) +{ + ATermList bnds = ATempty; + for (ATermIterator i(attrs.keys()); i; ++i) + bnds = ATinsert(bnds, + ATmake("Bind(, )", *i, attrs.get(*i))); + return ATmake("Attrs()", ATreverse(bnds)); +} + + +Expr substitute(const ATermMap & subs, Expr e) +{ + ATMatcher m; + string s; + + if (atMatch(m, e) >> "Var" >> s) { + Expr sub = subs.get(s); + return sub ? sub : e; + } + + /* In case of a function, filter out all variables bound by this + function. */ + ATermList formals; + ATerm body; + if (atMatch(m, e) >> "Function" >> formals >> body) { + ATermMap subs2(subs); + for (ATermIterator i(formals); i; ++i) { + Expr def; + if (!(atMatch(m, *i) >> "NoDefFormal" >> s) && + !(atMatch(m, *i) >> "DefFormal" >> s >> def)) + abort(); + subs2.remove(s); + } + return ATmake("Function(, )", formals, + substitute(subs2, body)); + } + + /* Idem for a mutually recursive attribute set. */ + ATermList bindings; + if (atMatch(m, e) >> "Rec" >> bindings) { + ATermMap subs2(subs); + for (ATermIterator i(bindings); i; ++i) { + Expr e; + if (!(atMatch(m, *i) >> "Bind" >> s >> e)) + abort(); /* can't happen */ + subs2.remove(s); + } + return ATmake("Rec()", substitute(subs2, (ATerm) bindings)); + } + + if (ATgetType(e) == AT_APPL) { + AFun fun = ATgetAFun(e); + int arity = ATgetArity(fun); + ATermList args = ATempty; + + for (int i = arity - 1; i >= 0; i--) + args = ATinsert(args, substitute(subs, ATgetArgument(e, i))); + + return (ATerm) ATmakeApplList(fun, args); + } + + if (ATgetType(e) == AT_LIST) { + ATermList out = ATempty; + for (ATermIterator i((ATermList) e); i; ++i) + out = ATinsert(out, substitute(subs, *i)); + return (ATerm) ATreverse(out); + } + + return e; +} + + +Expr makeBool(bool b) +{ + return b ? ATmake("Bool(True)") : ATmake("Bool(False)"); +} diff --git a/src/nix-instantiate/fix-expr.hh b/src/nix-instantiate/fix-expr.hh new file mode 100644 index 000000000000..6c1e51d9ccd6 --- /dev/null +++ b/src/nix-instantiate/fix-expr.hh @@ -0,0 +1,75 @@ +#ifndef __FIXEXPR_H +#define __FIXEXPR_H + +#include + +#include + +#include "util.hh" + + +/* Fix expressions are represented as ATerms. The maximal sharing + property of the ATerm library allows us to implement caching of + normals forms efficiently. */ +typedef ATerm Expr; + + +/* Mappings from ATerms to ATerms. This is just a wrapper around + ATerm tables. */ +class ATermMap +{ +private: + unsigned int maxLoadPct; + ATermTable table; + +public: + ATermMap(unsigned int initialSize = 16, unsigned int maxLoadPct = 75); + ATermMap(const ATermMap & map); + ~ATermMap(); + + void set(ATerm key, ATerm value); + void set(const string & key, ATerm value); + + ATerm get(ATerm key) const; + ATerm get(const string & key) const; + + void remove(ATerm key); + void remove(const string & key); + + ATermList keys() const; +}; + + +/* Convert a string to an ATerm (i.e., a quoted nullary function + applicaton). */ +ATerm string2ATerm(const string & s); +string aterm2String(ATerm t); + +/* Generic bottomup traversal over ATerms. The traversal first + recursively descends into subterms, and then applies the given term + function to the resulting term. */ +struct TermFun +{ + virtual ATerm operator () (ATerm e) = 0; +}; +ATerm bottomupRewrite(TermFun & f, ATerm e); + +/* Query all attributes in an attribute set expression. The + expression must be in normal form. */ +void queryAllAttrs(Expr e, ATermMap & attrs); + +/* Query a specific attribute from an attribute set expression. The + expression must be in normal form. */ +Expr queryAttr(Expr e, const string & name); + +/* Create an attribute set expression from an Attrs value. */ +Expr makeAttrs(const ATermMap & attrs); + +/* Perform a set of substitutions on an expression. */ +Expr substitute(const ATermMap & subs, Expr e); + +/* Create an expression representing a boolean. */ +Expr makeBool(bool b); + + +#endif /* !__FIXEXPR_H */ diff --git a/src/nix-instantiate/fix.cc b/src/nix-instantiate/fix.cc new file mode 100644 index 000000000000..e407aaf44346 --- /dev/null +++ b/src/nix-instantiate/fix.cc @@ -0,0 +1,117 @@ +#include +#include + +#include "globals.hh" +#include "normalise.hh" +#include "shared.hh" +#include "eval.hh" + + +#if 0 +static Path searchPath(const Paths & searchDirs, const Path & relPath) +{ + if (string(relPath, 0, 1) == "/") return relPath; + + for (Paths::const_iterator i = searchDirs.begin(); + i != searchDirs.end(); i++) + { + Path path = *i + "/" + relPath; + if (pathExists(path)) return path; + } + + throw Error( + format("path `%1%' not found in any of the search directories") + % relPath); +} +#endif + + +static Expr evalStdin(EvalState & state) +{ + startNest(nest, lvlTalkative, format("evaluating standard input")); + Expr e = ATreadFromFile(stdin); + if (!e) + throw Error(format("unable to read a term from stdin")); + return evalExpr(state, e); +} + + +static void printNixExpr(EvalState & state, Expr e) +{ + ATMatcher m; + ATermList es; + + if (atMatch(m, e) >> "Attrs" >> es) { + Expr a = queryAttr(e, "type"); + if (a && evalString(state, a) == "derivation") { + a = queryAttr(e, "drvPath"); + if (a) { + cout << format("%1%\n") % evalPath(state, a); + return; + } + } + } + + if (ATgetType(e) == AT_LIST) { + for (ATermIterator i((ATermList) e); i; ++i) + printNixExpr(state, evalExpr(state, *i)); + return; + } + + throw badTerm("top level does not evaluate to one or more Nix expressions", e); +} + + +void run(Strings args) +{ + EvalState state; + Strings files; + bool readStdin = false; + +#if 0 + state.searchDirs.push_back("."); + state.searchDirs.push_back(nixDataDir + "/fix"); +#endif + + for (Strings::iterator it = args.begin(); + it != args.end(); ) + { + string arg = *it++; + +#if 0 + if (arg == "--includedir" || arg == "-I") { + if (it == args.end()) + throw UsageError(format("argument required in `%1%'") % arg); + state.searchDirs.push_back(*it++); + } + else +#endif + if (arg == "--verbose" || arg == "-v") + verbosity = (Verbosity) ((int) verbosity + 1); + else if (arg == "-") + readStdin = true; + else if (arg[0] == '-') + throw UsageError(format("unknown flag `%1%`") % arg); + else + files.push_back(arg); + } + + openDB(); + + if (readStdin) { + Expr e = evalStdin(state); + printNixExpr(state, e); + } + + for (Strings::iterator it = files.begin(); + it != files.end(); it++) + { + Expr e = evalFile(state, absPath(*it)); + printNixExpr(state, e); + } + + printEvalStats(state); +} + + +string programId = "fix"; diff --git a/src/nix-instantiate/fix.sdf b/src/nix-instantiate/fix.sdf new file mode 100644 index 000000000000..54f5d5266f79 --- /dev/null +++ b/src/nix-instantiate/fix.sdf @@ -0,0 +1,203 @@ +definition + +module Main +imports Fix + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% Top level syntax. + +module Fix +imports Fix-Exprs Fix-Layout + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% Expressions. + +module Fix-Exprs +imports Fix-Lexicals URI +exports + sorts Expr Bind Formal + context-free syntax + + Id -> Expr {cons("Var")} + + Int -> Expr {cons("Int")} + + Str -> Expr {cons("Str")} + + Uri -> Expr {cons("Uri")} + + Path -> Expr {cons("Path")} + + "(" Expr ")" -> Expr {bracket} + + Expr Expr -> Expr {cons("Call"), left} + + "{" {Formal ","}* "}" ":" Expr -> Expr {cons("Function"), right} + Id -> Formal {cons("NoDefFormal")} + Id "?" Expr -> Formal {cons("DefFormal")} + + "assert" Expr ";" Expr -> Expr {cons("Assert"), right} + + "rec" "{" Binds "}" -> Expr {cons("Rec")} + "let" "{" Binds "}" -> Expr {cons("LetRec")} + "{" Binds "}" -> Expr {cons("Attrs")} + + Id "=" Expr -> Bind {cons("Bind")} + {Bind ";"}* -> Binds + Bind ";" -> BindSemi + BindSemi* -> Binds + + "[" ExprList "]" -> Expr {cons("List")} + "" -> ExprList {cons("ExprNil")} + Expr ExprList -> ExprList {cons("ExprCons")} + + Expr "." Id -> Expr {cons("Select")} + + "if" Expr "then" Expr "else" Expr -> Expr {cons("If")} + + Expr "==" Expr -> Expr {cons("OpEq"), non-assoc} + Expr "!=" Expr -> Expr {cons("OpNEq"), non-assoc} + + "!" Expr -> Expr {cons("OpNot")} + Expr "&&" Expr -> Expr {cons("OpAnd"), right} + Expr "||" Expr -> Expr {cons("OpOr"), right} + Expr "->" Expr -> Expr {cons("OpImpl"), right} + + Bool -> Expr {cons("Bool")} + + context-free priorities + + Expr "." Id -> Expr + > Expr ExprList -> ExprList + > Expr Expr -> Expr + > "!" Expr -> Expr + > Expr "==" Expr -> Expr + > Expr "!=" Expr -> Expr + > Expr "&&" Expr -> Expr + > Expr "||" Expr -> Expr + > Expr "->" Expr -> Expr + > "assert" Expr ";" Expr -> Expr + > "{" {Formal ","}* "}" ":" Expr -> Expr + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% Lexical syntax. + +module Fix-Lexicals +exports + sorts Id Path + lexical syntax + [a-zA-Z\_][a-zA-Z0-9\_\']* -> Id + "rec" -> Id {reject} + "let" -> Id {reject} + "if" -> Id {reject} + "then" -> Id {reject} + "else" -> Id {reject} + "true" -> Id {reject} + "false" -> Id {reject} + "assert" -> Id {reject} + + [0-9]+ -> Int + + "\"" ~[\n\"]* "\"" -> Str + + PathComp ("/" PathComp)+ -> Path + [a-zA-Z0-9\.\_\-\+]+ -> PathComp + + "true" -> Bool + "false" -> Bool + + lexical restrictions + Id -/- [a-zA-Z0-9\_\'] + Int -/- [0-9] + Path -/- [a-zA-Z0-9\.\_\-\+\/] + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% URIs (RFC 2396, appendix A). + +module URI +exports + sorts Uri + lexical syntax + Uscheme ":" (Uhierpath | Uopaquepath) -> Uri + + (Unetpath | Uabspath) ("?" Uquery)? -> Uhierpath + Uuricnoslash Uuric* -> Uopaquepath + + Uunreserved | Uescaped | [\;\?\:\@\&\=\+\$\,] -> Uuricnoslash + + "//" Uauthority Uabspath? -> Unetpath + "/" Upathsegments -> Uabspath + "//" Uuric* -> Uabspath {reject} + Urelsegment Uabspath? -> Urelpath + + (Uunreserved | Uescaped | [\;\@\&\=\+\$\,])+ -> Urelsegment + + Ualpha (Ualpha | Udigit | [\+\-\.])* -> Uscheme + + Userver | Uregname -> Uauthority + + (Uunreserved | Uescaped | [\$\,\;\:\@\&\=\+])+ -> Uregname + + ((Uuserinfo "@") Uhostport) -> Userver + (Uunreserved | Uescaped | [\;\:\&\=\+\$\,])* -> Uuserinfo + + Uhost (":" Uport)? -> Uhostport + Uhostname | UIPv4address -> Uhost + (Udomainlabel ".")+ Utoplabel "."? -> Uhostname + Ualphanum | Ualphanum (Ualphanum | "-")* Ualphanum -> Udomainlabel + Ualpha | Ualpha (Ualphanum | "-")* Ualphanum -> Utoplabel + Udigit+ "." Udigit+ "." Udigit+ "." Udigit+ -> UIPv4address + Udigit* -> Uport + + Uabspath | Uopaquepart -> Upath + Usegment ("/" Usegment)* -> Upathsegments + Upchar* (";" Uparam)* -> Usegment + Upchar* -> Uparam + Uunreserved | Uescaped | [\:\@\&\=\+\$\,] -> Upchar + + Uuric* -> Uquery + + Uuric* -> Ufragment + + Ureserved | Uunreserved | Uescaped -> Uuric + [\;\/\?\:\@\&\=\+\$\,] -> Ureserved + Ualphanum | Umark -> Uunreserved + [\-\_\.\!\~\*\'\(\)] -> Umark + + "%" Uhex Uhex -> Uescaped + Udigit | [A-Fa-f] -> Uhex + + Ualpha | Udigit -> Ualphanum + Ulowalpha | Uupalpha -> Ualpha + + [a-z] -> Ulowalpha + [A-Z] -> Uupalpha + [0-9] -> Udigit + + lexical restrictions + Uri -/- [a-zA-Z0-9\-\_\.\!\~\*\'\(\)] + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% Layout. + +module Fix-Layout +exports + lexical syntax + [\ \t\n] -> LAYOUT + HashComment -> LAYOUT + Comment -> LAYOUT + "#" ~[\n]* [\n] -> HashComment + "//" ~[\n]* [\n] -> HashComment + "/*" ( ~[\*] | Asterisk )* "*/" -> Comment + [\*] -> Asterisk + lexical restrictions + Asterisk -/- [\/] + context-free restrictions + LAYOUT? -/- [\ \t\n] | [\#] + syntax + HashComment -> diff --git a/src/nix-instantiate/fixexpr.cc b/src/nix-instantiate/fixexpr.cc new file mode 100644 index 000000000000..721fa8afa5f2 --- /dev/null +++ b/src/nix-instantiate/fixexpr.cc @@ -0,0 +1,215 @@ +#include "fixexpr.hh" +#include "storeexpr.hh" + + +ATermMap::ATermMap(unsigned int initialSize, unsigned int maxLoadPct) +{ + table = ATtableCreate(initialSize, maxLoadPct); + if (!table) throw Error("cannot create ATerm table"); +} + + +ATermMap::ATermMap(const ATermMap & map) + : table(0) +{ + ATermList keys = map.keys(); + + /* !!! adjust allocation for load pct */ + table = ATtableCreate(ATgetLength(keys), map.maxLoadPct); + if (!table) throw Error("cannot create ATerm table"); + + for (ATermIterator i(keys); i; ++i) + set(*i, map.get(*i)); +} + + +ATermMap::~ATermMap() +{ + if (table) ATtableDestroy(table); +} + + +void ATermMap::set(ATerm key, ATerm value) +{ + return ATtablePut(table, key, value); +} + + +void ATermMap::set(const string & key, ATerm value) +{ + set(string2ATerm(key), value); +} + + +ATerm ATermMap::get(ATerm key) const +{ + return ATtableGet(table, key); +} + + +ATerm ATermMap::get(const string & key) const +{ + return get(string2ATerm(key)); +} + + +void ATermMap::remove(ATerm key) +{ + ATtableRemove(table, key); +} + + +void ATermMap::remove(const string & key) +{ + remove(string2ATerm(key)); +} + + +ATermList ATermMap::keys() const +{ + ATermList keys = ATtableKeys(table); + if (!keys) throw Error("cannot query aterm map keys"); + return keys; +} + + +ATerm string2ATerm(const string & s) +{ + return (ATerm) ATmakeAppl0(ATmakeAFun((char *) s.c_str(), 0, ATtrue)); +} + + +string aterm2String(ATerm t) +{ + return ATgetName(ATgetAFun(t)); +} + + +ATerm bottomupRewrite(TermFun & f, ATerm e) +{ + if (ATgetType(e) == AT_APPL) { + AFun fun = ATgetAFun(e); + int arity = ATgetArity(fun); + ATermList args = ATempty; + + for (int i = arity - 1; i >= 0; i--) + args = ATinsert(args, bottomupRewrite(f, ATgetArgument(e, i))); + + e = (ATerm) ATmakeApplList(fun, args); + } + + else if (ATgetType(e) == AT_LIST) { + ATermList in = (ATermList) e; + ATermList out = ATempty; + + for (ATermIterator i(in); i; ++i) + out = ATinsert(out, bottomupRewrite(f, *i)); + + e = (ATerm) ATreverse(out); + } + + return f(e); +} + + +void queryAllAttrs(Expr e, ATermMap & attrs) +{ + ATMatcher m; + ATermList bnds; + if (!(atMatch(m, e) >> "Attrs" >> bnds)) + throw badTerm("expected attribute set", e); + + for (ATermIterator i(bnds); i; ++i) { + string s; + Expr e; + if (!(atMatch(m, *i) >> "Bind" >> s >> e)) + abort(); /* can't happen */ + attrs.set(s, e); + } +} + + +Expr queryAttr(Expr e, const string & name) +{ + ATermMap attrs; + queryAllAttrs(e, attrs); + return attrs.get(name); +} + + +Expr makeAttrs(const ATermMap & attrs) +{ + ATermList bnds = ATempty; + for (ATermIterator i(attrs.keys()); i; ++i) + bnds = ATinsert(bnds, + ATmake("Bind(, )", *i, attrs.get(*i))); + return ATmake("Attrs()", ATreverse(bnds)); +} + + +Expr substitute(const ATermMap & subs, Expr e) +{ + ATMatcher m; + string s; + + if (atMatch(m, e) >> "Var" >> s) { + Expr sub = subs.get(s); + return sub ? sub : e; + } + + /* In case of a function, filter out all variables bound by this + function. */ + ATermList formals; + ATerm body; + if (atMatch(m, e) >> "Function" >> formals >> body) { + ATermMap subs2(subs); + for (ATermIterator i(formals); i; ++i) { + Expr def; + if (!(atMatch(m, *i) >> "NoDefFormal" >> s) && + !(atMatch(m, *i) >> "DefFormal" >> s >> def)) + abort(); + subs2.remove(s); + } + return ATmake("Function(, )", formals, + substitute(subs2, body)); + } + + /* Idem for a mutually recursive attribute set. */ + ATermList bindings; + if (atMatch(m, e) >> "Rec" >> bindings) { + ATermMap subs2(subs); + for (ATermIterator i(bindings); i; ++i) { + Expr e; + if (!(atMatch(m, *i) >> "Bind" >> s >> e)) + abort(); /* can't happen */ + subs2.remove(s); + } + return ATmake("Rec()", substitute(subs2, (ATerm) bindings)); + } + + if (ATgetType(e) == AT_APPL) { + AFun fun = ATgetAFun(e); + int arity = ATgetArity(fun); + ATermList args = ATempty; + + for (int i = arity - 1; i >= 0; i--) + args = ATinsert(args, substitute(subs, ATgetArgument(e, i))); + + return (ATerm) ATmakeApplList(fun, args); + } + + if (ATgetType(e) == AT_LIST) { + ATermList out = ATempty; + for (ATermIterator i((ATermList) e); i; ++i) + out = ATinsert(out, substitute(subs, *i)); + return (ATerm) ATreverse(out); + } + + return e; +} + + +Expr makeBool(bool b) +{ + return b ? ATmake("Bool(True)") : ATmake("Bool(False)"); +} diff --git a/src/nix-instantiate/fixexpr.hh b/src/nix-instantiate/fixexpr.hh new file mode 100644 index 000000000000..6c1e51d9ccd6 --- /dev/null +++ b/src/nix-instantiate/fixexpr.hh @@ -0,0 +1,75 @@ +#ifndef __FIXEXPR_H +#define __FIXEXPR_H + +#include + +#include + +#include "util.hh" + + +/* Fix expressions are represented as ATerms. The maximal sharing + property of the ATerm library allows us to implement caching of + normals forms efficiently. */ +typedef ATerm Expr; + + +/* Mappings from ATerms to ATerms. This is just a wrapper around + ATerm tables. */ +class ATermMap +{ +private: + unsigned int maxLoadPct; + ATermTable table; + +public: + ATermMap(unsigned int initialSize = 16, unsigned int maxLoadPct = 75); + ATermMap(const ATermMap & map); + ~ATermMap(); + + void set(ATerm key, ATerm value); + void set(const string & key, ATerm value); + + ATerm get(ATerm key) const; + ATerm get(const string & key) const; + + void remove(ATerm key); + void remove(const string & key); + + ATermList keys() const; +}; + + +/* Convert a string to an ATerm (i.e., a quoted nullary function + applicaton). */ +ATerm string2ATerm(const string & s); +string aterm2String(ATerm t); + +/* Generic bottomup traversal over ATerms. The traversal first + recursively descends into subterms, and then applies the given term + function to the resulting term. */ +struct TermFun +{ + virtual ATerm operator () (ATerm e) = 0; +}; +ATerm bottomupRewrite(TermFun & f, ATerm e); + +/* Query all attributes in an attribute set expression. The + expression must be in normal form. */ +void queryAllAttrs(Expr e, ATermMap & attrs); + +/* Query a specific attribute from an attribute set expression. The + expression must be in normal form. */ +Expr queryAttr(Expr e, const string & name); + +/* Create an attribute set expression from an Attrs value. */ +Expr makeAttrs(const ATermMap & attrs); + +/* Perform a set of substitutions on an expression. */ +Expr substitute(const ATermMap & subs, Expr e); + +/* Create an expression representing a boolean. */ +Expr makeBool(bool b); + + +#endif /* !__FIXEXPR_H */ diff --git a/src/nix-instantiate/parser.cc b/src/nix-instantiate/parser.cc new file mode 100644 index 000000000000..eaa41b3963ef --- /dev/null +++ b/src/nix-instantiate/parser.cc @@ -0,0 +1,165 @@ +#include + +#include +#include +#include +#include + +extern "C" { +#include +#include +} + +#include "aterm.hh" +#include "parser.hh" +#include "shared.hh" +#include "fixexpr.hh" +#include "parse-table.h" + + +/* Cleanup cleans up an imploded parse tree into an actual abstract + syntax tree that we can evaluate. It removes quotes around + strings, converts integer literals into actual integers, and + absolutises paths relative to the directory containing the input + file. */ +struct Cleanup : TermFun +{ + string basePath; + + virtual ATerm operator () (ATerm e) + { + ATMatcher m; + string s; + + if (atMatch(m, e) >> "Str" >> s) { + return ATmake("Str()", + string(s, 1, s.size() - 2).c_str()); + } + + if (atMatch(m, e) >> "Path" >> s) { + if (s[0] != '/') + s = basePath + "/" + s; + return ATmake("Path()", canonPath(s).c_str()); + } + + if (atMatch(m, e) >> "Int" >> s) { + istringstream s2(s); + int n; + s2 >> n; + return ATmake("Int()", n); + } + + if (atMatch(m, e) >> "Bool" >> "true") + return ATmake("Bool(True)"); + + if (atMatch(m, e) >> "Bool" >> "false") + return ATmake("Bool(False)"); + + if (atMatch(m, e) >> "ExprNil") + return (ATerm) ATempty; + + ATerm e1; + ATermList e2; + if (atMatch(m, e) >> "ExprCons" >> e1 >> e2) + return (ATerm) ATinsert(e2, e1); + + return e; + } +}; + + +Expr parseExprFromFile(Path path) +{ +#if 0 + /* Perhaps this is already an imploded parse tree? */ + Expr e = ATreadFromNamedFile(path.c_str()); + if (e) return e; +#endif + + /* If `path' refers to a directory, append `/default.fix'. */ + struct stat st; + if (stat(path.c_str(), &st)) + throw SysError(format("getting status of `%1%'") % path); + if (S_ISDIR(st.st_mode)) + path = canonPath(path + "/default.fix"); + + /* Initialise the SDF libraries. */ + static bool initialised = false; + static ATerm parseTable = 0; + static language lang = 0; + + if (!initialised) { + PT_initMEPTApi(); + PT_initAsFix2Api(); + SGinitParser(ATfalse); + + ATprotect(&parseTable); + parseTable = ATreadFromBinaryString( + (char *) fixParseTable, sizeof fixParseTable); + if (!parseTable) + throw Error(format("cannot construct parse table term")); + + ATprotect(&lang); + lang = ATmake("Fix"); + if (!SGopenLanguageFromTerm( + (char *) programId.c_str(), lang, parseTable)) + throw Error(format("cannot open language")); + + SG_STARTSYMBOL_ON(); + SG_OUTPUT_ON(); + SG_ASFIX2ME_ON(); + SG_AMBIGUITY_ERROR_ON(); + SG_FILTER_OFF(); + + initialised = true; + } + + /* Read the input file. We can't use SGparseFile() because it's + broken, so we read the input ourselves and call + SGparseString(). */ + AutoCloseFD fd = open(path.c_str(), O_RDONLY); + if (fd == -1) throw SysError(format("opening `%1%'") % path); + + if (fstat(fd, &st) == -1) + throw SysError(format("statting `%1%'") % path); + + char text[st.st_size + 1]; + readFull(fd, (unsigned char *) text, st.st_size); + text[st.st_size] = 0; + + /* Parse it. */ + ATerm result = SGparseString(lang, "Expr", text); + if (!result) + throw SysError(format("parse failed in `%1%'") % path); + if (SGisParseError(result)) + throw Error(format("parse error in `%1%': %2%") + % path % result); + + /* Implode it. */ + PT_ParseTree tree = PT_makeParseTreeFromTerm(result); + if (!tree) + throw Error(format("cannot create parse tree")); + + ATerm imploded = PT_implodeParseTree(tree, + ATtrue, + ATtrue, + ATtrue, + ATtrue, + ATtrue, + ATtrue, + ATfalse, + ATtrue, + ATtrue, + ATtrue, + ATfalse); + if (!imploded) + throw Error(format("cannot implode parse tree")); + + debug(format("imploded parse tree of `%1%': %2%") + % path % imploded); + + /* Finally, clean it up. */ + Cleanup cleanup; + cleanup.basePath = dirOf(path); + return bottomupRewrite(cleanup, imploded); +} diff --git a/src/nix-instantiate/parser.hh b/src/nix-instantiate/parser.hh new file mode 100644 index 000000000000..e44987dd014b --- /dev/null +++ b/src/nix-instantiate/parser.hh @@ -0,0 +1,10 @@ +#ifndef __PARSER_H +#define __PARSER_H + +#include "fixexpr.hh" + + +Expr parseExprFromFile(Path path); + + +#endif /* !__PARSER_H */ diff --git a/src/nix-instantiate/primops.cc b/src/nix-instantiate/primops.cc new file mode 100644 index 000000000000..097933115342 --- /dev/null +++ b/src/nix-instantiate/primops.cc @@ -0,0 +1,246 @@ +#include "primops.hh" +#include "normalise.hh" +#include "globals.hh" + + +Expr primImport(EvalState & state, Expr arg) +{ + ATMatcher m; + string path; + if (!(atMatch(m, arg) >> "Path" >> path)) + throw badTerm("path expected", arg); + return evalFile(state, path); +} + + +static PathSet storeExprRootsCached(EvalState & state, const Path & nePath) +{ + DrvPaths::iterator i = state.drvPaths.find(nePath); + if (i != state.drvPaths.end()) + return i->second; + else { + PathSet paths = storeExprRoots(nePath); + state.drvPaths[nePath] = paths; + return paths; + } +} + + +static Hash hashDerivation(EvalState & state, StoreExpr ne) +{ + if (ne.type == StoreExpr::neDerivation) { + PathSet inputs2; + for (PathSet::iterator i = ne.derivation.inputs.begin(); + i != ne.derivation.inputs.end(); i++) + { + DrvHashes::iterator j = state.drvHashes.find(*i); + if (j == state.drvHashes.end()) + throw Error(format("don't know expression `%1%'") % (string) *i); + inputs2.insert(j->second); + } + ne.derivation.inputs = inputs2; + } + return hashTerm(unparseStoreExpr(ne)); +} + + +static Path copyAtom(EvalState & state, const Path & srcPath) +{ + /* !!! should be cached */ + Path dstPath(addToStore(srcPath)); + + ClosureElem elem; + StoreExpr ne; + ne.type = StoreExpr::neClosure; + ne.closure.roots.insert(dstPath); + ne.closure.elems[dstPath] = elem; + + Hash drvHash = hashDerivation(state, ne); + Path drvPath = writeTerm(unparseStoreExpr(ne), ""); + state.drvHashes[drvPath] = drvHash; + + printMsg(lvlChatty, format("copied `%1%' -> closure `%2%'") + % srcPath % drvPath); + return drvPath; +} + + +static string addInput(EvalState & state, + Path & nePath, StoreExpr & ne) +{ + PathSet paths = storeExprRootsCached(state, nePath); + if (paths.size() != 1) abort(); + Path path = *(paths.begin()); + ne.derivation.inputs.insert(nePath); + return path; +} + + +static string processBinding(EvalState & state, Expr e, StoreExpr & ne) +{ + e = evalExpr(state, e); + + ATMatcher m; + string s; + ATermList es; + + if (atMatch(m, e) >> "Str" >> s) return s; + if (atMatch(m, e) >> "Uri" >> s) return s; + if (atMatch(m, e) >> "Bool" >> "True") return "1"; + if (atMatch(m, e) >> "Bool" >> "False") return ""; + + if (atMatch(m, e) >> "Attrs" >> es) { + Expr a = queryAttr(e, "type"); + if (a && evalString(state, a) == "derivation") { + a = queryAttr(e, "drvPath"); + if (a) { + Path drvPath = evalPath(state, a); + return addInput(state, drvPath, ne); + } + } + } + + if (atMatch(m, e) >> "Path" >> s) { + Path drvPath = copyAtom(state, s); + return addInput(state, drvPath, ne); + } + + if (atMatch(m, e) >> "List" >> es) { + string s; + bool first = true; + for (ATermIterator i(es); i; ++i) { + startNest(nest, lvlVomit, format("processing list element")); + if (!first) s = s + " "; else first = false; + s += processBinding(state, evalExpr(state, *i), ne); + } + return s; + } + + if (atMatch(m, e) >> "Null") return ""; + + throw badTerm("invalid derivation binding", e); +} + + +Expr primDerivation(EvalState & state, Expr args) +{ + startNest(nest, lvlVomit, "evaluating derivation"); + + ATermMap attrs; + args = evalExpr(state, args); + queryAllAttrs(args, attrs); + + /* Build the derivation expression by processing the attributes. */ + StoreExpr ne; + ne.type = StoreExpr::neDerivation; + + string drvName; + Path outPath; + Hash outHash; + bool outHashGiven = false; + + for (ATermIterator i(attrs.keys()); i; ++i) { + string key = aterm2String(*i); + Expr value = attrs.get(key); + startNest(nest, lvlVomit, format("processing attribute `%1%'") % key); + + /* The `args' attribute is special: it supplies the + command-line arguments to the builder. */ + if (key == "args") { + throw Error("args not implemented"); +#if 0 + ATermList args; + if (!(ATmatch(value, "[]", &args)) + throw badTerm("list expected", value); + while (!ATisEmpty(args)) { + Expr arg = evalExpr(state, ATgetFirst(args)); + ne.derivation.args.push_back(processBinding(state, arg, ne)); + args = ATgetNext(args); + } +#endif + } + + /* All other attributes are passed to the builder through the + environment. */ + else { + string s = processBinding(state, value, ne); + ne.derivation.env[key] = s; + if (key == "builder") ne.derivation.builder = s; + else if (key == "system") ne.derivation.platform = s; + else if (key == "name") drvName = s; + else if (key == "outPath") outPath = s; + else if (key == "id") { + outHash = parseHash(s); + outHashGiven = true; + } + } + } + + /* Do we have all required attributes? */ + if (ne.derivation.builder == "") + throw badTerm("required attribute `builder' missing", args); + if (ne.derivation.platform == "") + throw badTerm("required attribute `system' missing", args); + if (drvName == "") + throw badTerm("required attribute `name' missing", args); + + /* Determine the output path. */ + if (!outHashGiven) outHash = hashDerivation(state, ne); + if (outPath == "") + /* Hash the Nix expression with no outputs to produce a + unique but deterministic path name for this derivation. */ + outPath = canonPath(nixStore + "/" + + ((string) outHash).c_str() + "-" + drvName); + ne.derivation.env["out"] = outPath; + ne.derivation.outputs.insert(outPath); + + /* Write the resulting term into the Nix store directory. */ + Hash drvHash = outHashGiven + ? hashString((string) outHash + outPath) + : hashDerivation(state, ne); + Path drvPath = writeTerm(unparseStoreExpr(ne), "-d-" + drvName); + state.drvHashes[drvPath] = drvHash; + + printMsg(lvlChatty, format("instantiated `%1%' -> `%2%'") + % drvName % drvPath); + + attrs.set("outPath", ATmake("Path()", outPath.c_str())); + attrs.set("drvPath", ATmake("Path()", drvPath.c_str())); + attrs.set("type", ATmake("Str(\"derivation\")")); + + return makeAttrs(attrs); +} + + +Expr primBaseNameOf(EvalState & state, Expr arg) +{ + string s = evalString(state, arg); + return ATmake("Str()", baseNameOf(s).c_str()); +} + + +Expr primToString(EvalState & state, Expr arg) +{ + arg = evalExpr(state, arg); + ATMatcher m; + string s; + if (atMatch(m, arg) >> "Str" >> s || + atMatch(m, arg) >> "Path" >> s || + atMatch(m, arg) >> "Uri" >> s) + return ATmake("Str()", s.c_str()); + else throw badTerm("cannot coerce to string", arg); +} + + +Expr primNull(EvalState & state) +{ + return ATmake("Null"); +} + + +Expr primIsNull(EvalState & state, Expr arg) +{ + arg = evalExpr(state, arg); + ATMatcher m; + return makeBool(atMatch(m, arg) >> "Null"); +} diff --git a/src/nix-instantiate/primops.hh b/src/nix-instantiate/primops.hh new file mode 100644 index 000000000000..76d587afdb1e --- /dev/null +++ b/src/nix-instantiate/primops.hh @@ -0,0 +1,34 @@ +#ifndef __PRIMOPS_H +#define __PRIMOPS_H + +#include "eval.hh" + + +/* Load and evaluate an expression from path specified by the + argument. */ +Expr primImport(EvalState & state, Expr arg); + +/* Construct (as a unobservable) side effect) a Nix derivation + expression that performs the derivation described by the argument + set. Returns the original set extended with the following + attributes: `outPath' containing the primary output path of the + derivation; `drvPath' containing the path of the Nix expression; + and `type' set to `derivation' to indicate that this is a + derivation. */ +Expr primDerivation(EvalState & state, Expr args); + +/* Return the base name of the given string, i.e., everything + following the last slash. */ +Expr primBaseNameOf(EvalState & state, Expr arg); + +/* Convert the argument (which can be a path or a uri) to a string. */ +Expr primToString(EvalState & state, Expr arg); + +/* Return the null value. */ +Expr primNull(EvalState & state); + +/* Determine whether the argument is the null value. */ +Expr primIsNull(EvalState & state, Expr arg); + + +#endif /* !__PRIMOPS_H */ diff --git a/src/nix-store/Makefile.am b/src/nix-store/Makefile.am new file mode 100644 index 000000000000..a39d1e2ad985 --- /dev/null +++ b/src/nix-store/Makefile.am @@ -0,0 +1,27 @@ +bin_PROGRAMS = nix-store + +nix_store_SOURCES = nix.cc dotgraph.cc +nix_store_LDADD = ../libmain/libmain.a ../libstore/libstore.a ../libutil/libutil.a \ + ../boost/format/libformat.a -L../../externals/inst/lib -ldb_cxx -lATerm + +nix.o: nix-help.txt.hh + +%.hh: % + echo -n '"' > $@ + sed 's|\(.*\)|\1\\n\\|' < $< >> $@ + echo '"' >> $@ + +AM_CXXFLAGS = \ + -I.. -I../../externals/inst/include -I../libutil -I../libstore -I../libmain + +install-data-local: + $(INSTALL) -d $(localstatedir)/nix + $(INSTALL) -d $(localstatedir)/nix/db + $(INSTALL) -d $(localstatedir)/nix/links + rm -f $(prefix)/current + ln -sf $(localstatedir)/nix/links/current $(prefix)/current + $(INSTALL) -d $(localstatedir)/log/nix + $(INSTALL) -d $(prefix)/store + $(bindir)/nix-store --init + +EXTRA_DIST = *.hh diff --git a/src/nix-store/dotgraph.cc b/src/nix-store/dotgraph.cc new file mode 100644 index 000000000000..c670bf19e4c5 --- /dev/null +++ b/src/nix-store/dotgraph.cc @@ -0,0 +1,135 @@ +#include "dotgraph.hh" +#include "normalise.hh" + + +static string dotQuote(const string & s) +{ + return "\"" + s + "\""; +} + + +static string nextColour() +{ + static int n = 0; + static string colours[] = + { "black", "red", "green", "blue" + , "magenta", "burlywood" }; + return colours[n++ % (sizeof(colours) / sizeof(string))]; +} + + +static string makeEdge(const string & src, const string & dst) +{ + format f = format("%1% -> %2% [color = %3%];\n") + % dotQuote(src) % dotQuote(dst) % dotQuote(nextColour()); + return f.str(); +} + + +static string makeNode(const string & id, const string & label, + const string & colour) +{ + format f = format("%1% [label = %2%, shape = box, " + "style = filled, fillcolor = %3%];\n") + % dotQuote(id) % dotQuote(label) % dotQuote(colour); + return f.str(); +} + + +static string symbolicName(const string & path) +{ + string p = baseNameOf(path); + if (isHash(string(p, 0, Hash::hashSize * 2)) && + p[Hash::hashSize * 2] == '-') + p = string(p, Hash::hashSize * 2 + 1); + return p; +} + + +string pathLabel(const Path & nePath, const string & elemPath) +{ + return (string) nePath + "-" + elemPath; +} + + +void printClosure(const Path & nePath, const StoreExpr & fs) +{ + PathSet workList(fs.closure.roots); + PathSet doneSet; + + for (PathSet::iterator i = workList.begin(); i != workList.end(); i++) { + cout << makeEdge(pathLabel(nePath, *i), nePath); + } + + while (!workList.empty()) { + Path path = *(workList.begin()); + workList.erase(path); + + if (doneSet.find(path) == doneSet.end()) { + doneSet.insert(path); + + ClosureElems::const_iterator elem = fs.closure.elems.find(path); + if (elem == fs.closure.elems.end()) + throw Error(format("bad closure, missing path `%1%'") % path); + + for (StringSet::const_iterator i = elem->second.refs.begin(); + i != elem->second.refs.end(); i++) + { + workList.insert(*i); + cout << makeEdge(pathLabel(nePath, *i), pathLabel(nePath, path)); + } + + cout << makeNode(pathLabel(nePath, path), + symbolicName(path), "#ff0000"); + } + } +} + + +void printDotGraph(const PathSet & roots) +{ + PathSet workList(roots); + PathSet doneSet; + + cout << "digraph G {\n"; + + while (!workList.empty()) { + Path nePath = *(workList.begin()); + workList.erase(nePath); + + if (doneSet.find(nePath) == doneSet.end()) { + doneSet.insert(nePath); + + StoreExpr ne = storeExprFromPath(nePath); + + string label, colour; + + if (ne.type == StoreExpr::neDerivation) { + for (PathSet::iterator i = ne.derivation.inputs.begin(); + i != ne.derivation.inputs.end(); i++) + { + workList.insert(*i); + cout << makeEdge(*i, nePath); + } + + label = "derivation"; + colour = "#00ff00"; + for (StringPairs::iterator i = ne.derivation.env.begin(); + i != ne.derivation.env.end(); i++) + if (i->first == "name") label = i->second; + } + + else if (ne.type == StoreExpr::neClosure) { + label = ""; + colour = "#00ffff"; + printClosure(nePath, ne); + } + + else abort(); + + cout << makeNode(nePath, label, colour); + } + } + + cout << "}\n"; +} diff --git a/src/nix-store/dotgraph.hh b/src/nix-store/dotgraph.hh new file mode 100644 index 000000000000..ef389b30d353 --- /dev/null +++ b/src/nix-store/dotgraph.hh @@ -0,0 +1,8 @@ +#ifndef __DOTGRAPH_H +#define __DOTGRAPH_H + +#include "storeexpr.hh" + +void printDotGraph(const PathSet & roots); + +#endif /* !__DOTGRAPH_H */ diff --git a/src/nix-store/nix-help.txt b/src/nix-store/nix-help.txt new file mode 100644 index 000000000000..d7f977025440 --- /dev/null +++ b/src/nix-store/nix-help.txt @@ -0,0 +1,34 @@ +nix-store [OPTIONS...] [ARGUMENTS...] + +`nix-store' is a tool to manipulate the Nix store. + +Operations: + + --realise / -r: realise a Nix expression + --delete / -d: delete paths from the Nix store + --add / -A: copy a path to the Nix store + --query / -q: query information + + --successor: register a successor expression + --substitute: register a substitute expression + + --dump: dump a path as a Nix archive + --restore: restore a path from a Nix archive + + --init: initialise the Nix database + --verify: verify Nix structures + + --version: output version information + --help: display help + +Query flags: + + --list / -l: query the output paths (roots) of a Nix expression (default) + --requisites / -R: print all paths necessary to realise expression + --predecessors: print predecessors of a Nix expression + --graph: print a dot graph rooted at given ids + +Options: + + --verbose / -v: verbose operation (may be repeated) + --keep-failed / -K: keep temporary directories of failed builds diff --git a/src/nix-store/nix.cc b/src/nix-store/nix.cc new file mode 100644 index 000000000000..d1766de39b7e --- /dev/null +++ b/src/nix-store/nix.cc @@ -0,0 +1,304 @@ +#include +#include + +#include "globals.hh" +#include "normalise.hh" +#include "archive.hh" +#include "shared.hh" +#include "dotgraph.hh" + + +typedef void (* Operation) (Strings opFlags, Strings opArgs); + + +static void printHelp() +{ + cout << +#include "nix-help.txt.hh" + ; + exit(0); +} + + + +static Path checkPath(const Path & arg) +{ + return arg; /* !!! check that arg is in the store */ +} + + +/* Realise paths from the given store expressions. */ +static void opRealise(Strings opFlags, Strings opArgs) +{ + if (!opFlags.empty()) throw UsageError("unknown flag"); + + for (Strings::iterator i = opArgs.begin(); + i != opArgs.end(); i++) + { + Path nfPath = normaliseStoreExpr(checkPath(*i)); + realiseClosure(nfPath); + cout << format("%1%\n") % (string) nfPath; + } +} + + +/* Delete a path in the Nix store directory. */ +static void opDelete(Strings opFlags, Strings opArgs) +{ + if (!opFlags.empty()) throw UsageError("unknown flag"); + + for (Strings::iterator it = opArgs.begin(); + it != opArgs.end(); it++) + deleteFromStore(checkPath(*it)); +} + + +/* Add paths to the Nix values directory and print the hashes of those + paths. */ +static void opAdd(Strings opFlags, Strings opArgs) +{ + if (!opFlags.empty()) throw UsageError("unknown flag"); + + for (Strings::iterator i = opArgs.begin(); i != opArgs.end(); i++) + cout << format("%1%\n") % addToStore(*i); +} + + +Path maybeNormalise(const Path & ne, bool normalise) +{ + return normalise ? normaliseStoreExpr(ne) : ne; +} + + +/* Perform various sorts of queries. */ +static void opQuery(Strings opFlags, Strings opArgs) +{ + enum { qList, qRequisites, qPredecessors, qGraph + } query = qList; + bool normalise = false; + bool includeExprs = true; + bool includeSuccessors = false; + + for (Strings::iterator i = opFlags.begin(); + i != opFlags.end(); i++) + if (*i == "--list" || *i == "-l") query = qList; + else if (*i == "--requisites" || *i == "-R") query = qRequisites; + else if (*i == "--predecessors") query = qPredecessors; + else if (*i == "--graph") query = qGraph; + else if (*i == "--normalise" || *i == "-n") normalise = true; + else if (*i == "--exclude-exprs") includeExprs = false; + else if (*i == "--include-successors") includeSuccessors = true; + else throw UsageError(format("unknown flag `%1%'") % *i); + + switch (query) { + + case qList: { + for (Strings::iterator i = opArgs.begin(); + i != opArgs.end(); i++) + { + StringSet paths = storeExprRoots( + maybeNormalise(checkPath(*i), normalise)); + for (StringSet::iterator j = paths.begin(); + j != paths.end(); j++) + cout << format("%s\n") % *j; + } + break; + } + + case qRequisites: { + StringSet paths; + for (Strings::iterator i = opArgs.begin(); + i != opArgs.end(); i++) + { + StringSet paths2 = storeExprRequisites( + maybeNormalise(checkPath(*i), normalise), + includeExprs, includeSuccessors); + paths.insert(paths2.begin(), paths2.end()); + } + for (StringSet::iterator i = paths.begin(); + i != paths.end(); i++) + cout << format("%s\n") % *i; + break; + } + + case qPredecessors: { + for (Strings::iterator i = opArgs.begin(); + i != opArgs.end(); i++) + { + Paths preds = queryPredecessors(checkPath(*i)); + for (Paths::iterator j = preds.begin(); + j != preds.end(); j++) + cout << format("%s\n") % *j; + } + break; + } + + case qGraph: { + PathSet roots; + for (Strings::iterator i = opArgs.begin(); + i != opArgs.end(); i++) + roots.insert(maybeNormalise(checkPath(*i), normalise)); + printDotGraph(roots); + break; + } + + default: + abort(); + } +} + + +static void opSuccessor(Strings opFlags, Strings opArgs) +{ + if (!opFlags.empty()) throw UsageError("unknown flag"); + if (opArgs.size() % 2) throw UsageError("expecting even number of arguments"); + + Transaction txn; + createStoreTransaction(txn); + for (Strings::iterator i = opArgs.begin(); + i != opArgs.end(); ) + { + Path path1 = checkPath(*i++); + Path path2 = checkPath(*i++); + registerSuccessor(txn, path1, path2); + } + txn.commit(); +} + + +static void opSubstitute(Strings opFlags, Strings opArgs) +{ + if (!opFlags.empty()) throw UsageError("unknown flag"); + if (opArgs.size() % 2) throw UsageError("expecting even number of arguments"); + + for (Strings::iterator i = opArgs.begin(); + i != opArgs.end(); ) + { + Path src = checkPath(*i++); + Path sub = checkPath(*i++); + registerSubstitute(src, sub); + } +} + + +/* A sink that writes dump output to stdout. */ +struct StdoutSink : DumpSink +{ + virtual void operator () + (const unsigned char * data, unsigned int len) + { + writeFull(STDOUT_FILENO, data, len); + } +}; + + +/* Dump a path as a Nix archive. The archive is written to standard + output. */ +static void opDump(Strings opFlags, Strings opArgs) +{ + if (!opFlags.empty()) throw UsageError("unknown flag"); + if (opArgs.size() != 1) throw UsageError("only one argument allowed"); + + StdoutSink sink; + string path = *opArgs.begin(); + dumpPath(path, sink); +} + + +/* A source that read restore intput to stdin. */ +struct StdinSource : RestoreSource +{ + virtual void operator () (unsigned char * data, unsigned int len) + { + readFull(STDIN_FILENO, data, len); + } +}; + + +/* Restore a value from a Nix archive. The archive is written to + standard input. */ +static void opRestore(Strings opFlags, Strings opArgs) +{ + if (!opFlags.empty()) throw UsageError("unknown flag"); + if (opArgs.size() != 1) throw UsageError("only one argument allowed"); + + StdinSource source; + restorePath(*opArgs.begin(), source); +} + + +/* Initialise the Nix databases. */ +static void opInit(Strings opFlags, Strings opArgs) +{ + if (!opFlags.empty()) throw UsageError("unknown flag"); + if (!opArgs.empty()) + throw UsageError("--init does not have arguments"); + initDB(); +} + + +/* Verify the consistency of the Nix environment. */ +static void opVerify(Strings opFlags, Strings opArgs) +{ + verifyStore(); +} + + +/* Scan the arguments; find the operation, set global flags, put all + other flags in a list, and put all other arguments in another + list. */ +void run(Strings args) +{ + Strings opFlags, opArgs; + Operation op = 0; + + for (Strings::iterator it = args.begin(); it != args.end(); ) + { + string arg = *it++; + + Operation oldOp = op; + + if (arg == "--realise" || arg == "-r") + op = opRealise; + else if (arg == "--delete" || arg == "-d") + op = opDelete; + else if (arg == "--add" || arg == "-A") + op = opAdd; + else if (arg == "--query" || arg == "-q") + op = opQuery; + else if (arg == "--successor") + op = opSuccessor; + else if (arg == "--substitute") + op = opSubstitute; + else if (arg == "--dump") + op = opDump; + else if (arg == "--restore") + op = opRestore; + else if (arg == "--init") + op = opInit; + else if (arg == "--verify") + op = opVerify; + else if (arg == "--verbose" || arg == "-v") + verbosity = (Verbosity) ((int) verbosity + 1); + else if (arg == "--keep-failed" || arg == "-K") + keepFailed = true; + else if (arg == "--help") + printHelp(); + else if (arg[0] == '-') + opFlags.push_back(arg); + else + opArgs.push_back(arg); + + if (oldOp && oldOp != op) + throw UsageError("only one operation may be specified"); + } + + if (!op) throw UsageError("no operation specified"); + + openDB(); + + op(opFlags, opArgs); +} + + +string programId = "nix"; diff --git a/src/nix/Makefile.am b/src/nix/Makefile.am deleted file mode 100644 index 8a52be364f1a..000000000000 --- a/src/nix/Makefile.am +++ /dev/null @@ -1,27 +0,0 @@ -bin_PROGRAMS = nix - -nix_SOURCES = nix.cc dotgraph.cc -nix_LDADD = ../libmain/libmain.a ../libstore/libstore.a ../libutil/libutil.a \ - ../boost/format/libformat.a -L../../externals/inst/lib -ldb_cxx -lATerm - -nix.o: nix-help.txt.hh - -%.hh: % - echo -n '"' > $@ - sed 's|\(.*\)|\1\\n\\|' < $< >> $@ - echo '"' >> $@ - -AM_CXXFLAGS = \ - -I.. -I../../externals/inst/include -I../libutil -I../libstore -I../libmain - -install-data-local: - $(INSTALL) -d $(localstatedir)/nix - $(INSTALL) -d $(localstatedir)/nix/db - $(INSTALL) -d $(localstatedir)/nix/links - rm -f $(prefix)/current - ln -sf $(localstatedir)/nix/links/current $(prefix)/current - $(INSTALL) -d $(localstatedir)/log/nix - $(INSTALL) -d $(prefix)/store - $(bindir)/nix --init - -EXTRA_DIST = *.hh diff --git a/src/nix/dotgraph.cc b/src/nix/dotgraph.cc deleted file mode 100644 index c670bf19e4c5..000000000000 --- a/src/nix/dotgraph.cc +++ /dev/null @@ -1,135 +0,0 @@ -#include "dotgraph.hh" -#include "normalise.hh" - - -static string dotQuote(const string & s) -{ - return "\"" + s + "\""; -} - - -static string nextColour() -{ - static int n = 0; - static string colours[] = - { "black", "red", "green", "blue" - , "magenta", "burlywood" }; - return colours[n++ % (sizeof(colours) / sizeof(string))]; -} - - -static string makeEdge(const string & src, const string & dst) -{ - format f = format("%1% -> %2% [color = %3%];\n") - % dotQuote(src) % dotQuote(dst) % dotQuote(nextColour()); - return f.str(); -} - - -static string makeNode(const string & id, const string & label, - const string & colour) -{ - format f = format("%1% [label = %2%, shape = box, " - "style = filled, fillcolor = %3%];\n") - % dotQuote(id) % dotQuote(label) % dotQuote(colour); - return f.str(); -} - - -static string symbolicName(const string & path) -{ - string p = baseNameOf(path); - if (isHash(string(p, 0, Hash::hashSize * 2)) && - p[Hash::hashSize * 2] == '-') - p = string(p, Hash::hashSize * 2 + 1); - return p; -} - - -string pathLabel(const Path & nePath, const string & elemPath) -{ - return (string) nePath + "-" + elemPath; -} - - -void printClosure(const Path & nePath, const StoreExpr & fs) -{ - PathSet workList(fs.closure.roots); - PathSet doneSet; - - for (PathSet::iterator i = workList.begin(); i != workList.end(); i++) { - cout << makeEdge(pathLabel(nePath, *i), nePath); - } - - while (!workList.empty()) { - Path path = *(workList.begin()); - workList.erase(path); - - if (doneSet.find(path) == doneSet.end()) { - doneSet.insert(path); - - ClosureElems::const_iterator elem = fs.closure.elems.find(path); - if (elem == fs.closure.elems.end()) - throw Error(format("bad closure, missing path `%1%'") % path); - - for (StringSet::const_iterator i = elem->second.refs.begin(); - i != elem->second.refs.end(); i++) - { - workList.insert(*i); - cout << makeEdge(pathLabel(nePath, *i), pathLabel(nePath, path)); - } - - cout << makeNode(pathLabel(nePath, path), - symbolicName(path), "#ff0000"); - } - } -} - - -void printDotGraph(const PathSet & roots) -{ - PathSet workList(roots); - PathSet doneSet; - - cout << "digraph G {\n"; - - while (!workList.empty()) { - Path nePath = *(workList.begin()); - workList.erase(nePath); - - if (doneSet.find(nePath) == doneSet.end()) { - doneSet.insert(nePath); - - StoreExpr ne = storeExprFromPath(nePath); - - string label, colour; - - if (ne.type == StoreExpr::neDerivation) { - for (PathSet::iterator i = ne.derivation.inputs.begin(); - i != ne.derivation.inputs.end(); i++) - { - workList.insert(*i); - cout << makeEdge(*i, nePath); - } - - label = "derivation"; - colour = "#00ff00"; - for (StringPairs::iterator i = ne.derivation.env.begin(); - i != ne.derivation.env.end(); i++) - if (i->first == "name") label = i->second; - } - - else if (ne.type == StoreExpr::neClosure) { - label = ""; - colour = "#00ffff"; - printClosure(nePath, ne); - } - - else abort(); - - cout << makeNode(nePath, label, colour); - } - } - - cout << "}\n"; -} diff --git a/src/nix/dotgraph.hh b/src/nix/dotgraph.hh deleted file mode 100644 index ef389b30d353..000000000000 --- a/src/nix/dotgraph.hh +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef __DOTGRAPH_H -#define __DOTGRAPH_H - -#include "storeexpr.hh" - -void printDotGraph(const PathSet & roots); - -#endif /* !__DOTGRAPH_H */ diff --git a/src/nix/nix-help.txt b/src/nix/nix-help.txt deleted file mode 100644 index a2d9958ffe49..000000000000 --- a/src/nix/nix-help.txt +++ /dev/null @@ -1,32 +0,0 @@ -nix [OPTIONS...] [ARGUMENTS...] - -Operations: - - --realise / -r: realise a Nix expression - --delete / -d: delete paths from the Nix store - --add / -A: copy a path to the Nix store - --query / -q: query information - - --successor: register a successor expression - --substitute: register a substitute expression - - --dump: dump a path as a Nix archive - --restore: restore a path from a Nix archive - - --init: initialise the Nix database - --verify: verify Nix structures - - --version: output version information - --help: display help - -Query flags: - - --list / -l: query the output paths (roots) of a Nix expression (default) - --requisites / -R: print all paths necessary to realise expression - --predecessors: print predecessors of a Nix expression - --graph: print a dot graph rooted at given ids - -Options: - - --verbose / -v: verbose operation (may be repeated) - --keep-failed / -K: keep temporary directories of failed builds diff --git a/src/nix/nix.cc b/src/nix/nix.cc deleted file mode 100644 index d1766de39b7e..000000000000 --- a/src/nix/nix.cc +++ /dev/null @@ -1,304 +0,0 @@ -#include -#include - -#include "globals.hh" -#include "normalise.hh" -#include "archive.hh" -#include "shared.hh" -#include "dotgraph.hh" - - -typedef void (* Operation) (Strings opFlags, Strings opArgs); - - -static void printHelp() -{ - cout << -#include "nix-help.txt.hh" - ; - exit(0); -} - - - -static Path checkPath(const Path & arg) -{ - return arg; /* !!! check that arg is in the store */ -} - - -/* Realise paths from the given store expressions. */ -static void opRealise(Strings opFlags, Strings opArgs) -{ - if (!opFlags.empty()) throw UsageError("unknown flag"); - - for (Strings::iterator i = opArgs.begin(); - i != opArgs.end(); i++) - { - Path nfPath = normaliseStoreExpr(checkPath(*i)); - realiseClosure(nfPath); - cout << format("%1%\n") % (string) nfPath; - } -} - - -/* Delete a path in the Nix store directory. */ -static void opDelete(Strings opFlags, Strings opArgs) -{ - if (!opFlags.empty()) throw UsageError("unknown flag"); - - for (Strings::iterator it = opArgs.begin(); - it != opArgs.end(); it++) - deleteFromStore(checkPath(*it)); -} - - -/* Add paths to the Nix values directory and print the hashes of those - paths. */ -static void opAdd(Strings opFlags, Strings opArgs) -{ - if (!opFlags.empty()) throw UsageError("unknown flag"); - - for (Strings::iterator i = opArgs.begin(); i != opArgs.end(); i++) - cout << format("%1%\n") % addToStore(*i); -} - - -Path maybeNormalise(const Path & ne, bool normalise) -{ - return normalise ? normaliseStoreExpr(ne) : ne; -} - - -/* Perform various sorts of queries. */ -static void opQuery(Strings opFlags, Strings opArgs) -{ - enum { qList, qRequisites, qPredecessors, qGraph - } query = qList; - bool normalise = false; - bool includeExprs = true; - bool includeSuccessors = false; - - for (Strings::iterator i = opFlags.begin(); - i != opFlags.end(); i++) - if (*i == "--list" || *i == "-l") query = qList; - else if (*i == "--requisites" || *i == "-R") query = qRequisites; - else if (*i == "--predecessors") query = qPredecessors; - else if (*i == "--graph") query = qGraph; - else if (*i == "--normalise" || *i == "-n") normalise = true; - else if (*i == "--exclude-exprs") includeExprs = false; - else if (*i == "--include-successors") includeSuccessors = true; - else throw UsageError(format("unknown flag `%1%'") % *i); - - switch (query) { - - case qList: { - for (Strings::iterator i = opArgs.begin(); - i != opArgs.end(); i++) - { - StringSet paths = storeExprRoots( - maybeNormalise(checkPath(*i), normalise)); - for (StringSet::iterator j = paths.begin(); - j != paths.end(); j++) - cout << format("%s\n") % *j; - } - break; - } - - case qRequisites: { - StringSet paths; - for (Strings::iterator i = opArgs.begin(); - i != opArgs.end(); i++) - { - StringSet paths2 = storeExprRequisites( - maybeNormalise(checkPath(*i), normalise), - includeExprs, includeSuccessors); - paths.insert(paths2.begin(), paths2.end()); - } - for (StringSet::iterator i = paths.begin(); - i != paths.end(); i++) - cout << format("%s\n") % *i; - break; - } - - case qPredecessors: { - for (Strings::iterator i = opArgs.begin(); - i != opArgs.end(); i++) - { - Paths preds = queryPredecessors(checkPath(*i)); - for (Paths::iterator j = preds.begin(); - j != preds.end(); j++) - cout << format("%s\n") % *j; - } - break; - } - - case qGraph: { - PathSet roots; - for (Strings::iterator i = opArgs.begin(); - i != opArgs.end(); i++) - roots.insert(maybeNormalise(checkPath(*i), normalise)); - printDotGraph(roots); - break; - } - - default: - abort(); - } -} - - -static void opSuccessor(Strings opFlags, Strings opArgs) -{ - if (!opFlags.empty()) throw UsageError("unknown flag"); - if (opArgs.size() % 2) throw UsageError("expecting even number of arguments"); - - Transaction txn; - createStoreTransaction(txn); - for (Strings::iterator i = opArgs.begin(); - i != opArgs.end(); ) - { - Path path1 = checkPath(*i++); - Path path2 = checkPath(*i++); - registerSuccessor(txn, path1, path2); - } - txn.commit(); -} - - -static void opSubstitute(Strings opFlags, Strings opArgs) -{ - if (!opFlags.empty()) throw UsageError("unknown flag"); - if (opArgs.size() % 2) throw UsageError("expecting even number of arguments"); - - for (Strings::iterator i = opArgs.begin(); - i != opArgs.end(); ) - { - Path src = checkPath(*i++); - Path sub = checkPath(*i++); - registerSubstitute(src, sub); - } -} - - -/* A sink that writes dump output to stdout. */ -struct StdoutSink : DumpSink -{ - virtual void operator () - (const unsigned char * data, unsigned int len) - { - writeFull(STDOUT_FILENO, data, len); - } -}; - - -/* Dump a path as a Nix archive. The archive is written to standard - output. */ -static void opDump(Strings opFlags, Strings opArgs) -{ - if (!opFlags.empty()) throw UsageError("unknown flag"); - if (opArgs.size() != 1) throw UsageError("only one argument allowed"); - - StdoutSink sink; - string path = *opArgs.begin(); - dumpPath(path, sink); -} - - -/* A source that read restore intput to stdin. */ -struct StdinSource : RestoreSource -{ - virtual void operator () (unsigned char * data, unsigned int len) - { - readFull(STDIN_FILENO, data, len); - } -}; - - -/* Restore a value from a Nix archive. The archive is written to - standard input. */ -static void opRestore(Strings opFlags, Strings opArgs) -{ - if (!opFlags.empty()) throw UsageError("unknown flag"); - if (opArgs.size() != 1) throw UsageError("only one argument allowed"); - - StdinSource source; - restorePath(*opArgs.begin(), source); -} - - -/* Initialise the Nix databases. */ -static void opInit(Strings opFlags, Strings opArgs) -{ - if (!opFlags.empty()) throw UsageError("unknown flag"); - if (!opArgs.empty()) - throw UsageError("--init does not have arguments"); - initDB(); -} - - -/* Verify the consistency of the Nix environment. */ -static void opVerify(Strings opFlags, Strings opArgs) -{ - verifyStore(); -} - - -/* Scan the arguments; find the operation, set global flags, put all - other flags in a list, and put all other arguments in another - list. */ -void run(Strings args) -{ - Strings opFlags, opArgs; - Operation op = 0; - - for (Strings::iterator it = args.begin(); it != args.end(); ) - { - string arg = *it++; - - Operation oldOp = op; - - if (arg == "--realise" || arg == "-r") - op = opRealise; - else if (arg == "--delete" || arg == "-d") - op = opDelete; - else if (arg == "--add" || arg == "-A") - op = opAdd; - else if (arg == "--query" || arg == "-q") - op = opQuery; - else if (arg == "--successor") - op = opSuccessor; - else if (arg == "--substitute") - op = opSubstitute; - else if (arg == "--dump") - op = opDump; - else if (arg == "--restore") - op = opRestore; - else if (arg == "--init") - op = opInit; - else if (arg == "--verify") - op = opVerify; - else if (arg == "--verbose" || arg == "-v") - verbosity = (Verbosity) ((int) verbosity + 1); - else if (arg == "--keep-failed" || arg == "-K") - keepFailed = true; - else if (arg == "--help") - printHelp(); - else if (arg[0] == '-') - opFlags.push_back(arg); - else - opArgs.push_back(arg); - - if (oldOp && oldOp != op) - throw UsageError("only one operation may be specified"); - } - - if (!op) throw UsageError("no operation specified"); - - openDB(); - - op(opFlags, opArgs); -} - - -string programId = "nix"; -- cgit 1.4.1