1 : #include <map>
2 : #include <iostream>
3 :
4 : #include "globals.hh"
5 : #include "normalise.hh"
6 : #include "shared.hh"
7 : #include "eval.hh"
8 : #include "parser.hh"
9 : #include "nixexpr-ast.hh"
10 : #include "help.txt.hh"
11 :
12 :
13 : void printHelp()
14 0 : {
15 0 : cout << string((char *) helpText, sizeof helpText);
16 : }
17 :
18 :
19 : static Expr evalStdin(EvalState & state, bool parseOnly)
20 12 : {
21 102 : startNest(nest, lvlTalkative, format("evaluating standard input"));
22 12 : string s, s2;
23 105 : while (getline(cin, s2)) s += s2 + "\n";
24 12 : Expr e = parseExprFromString(state, s, absPath("."));
25 11 : return parseOnly ? e : evalExpr(state, e);
26 : }
27 :
28 :
29 : static void printDrvPaths(EvalState & state, Expr e)
30 11 : {
31 11 : ATermList es;
32 :
33 : /* !!! duplication w.r.t. parseDerivations in nix-env */
34 :
35 11 : if (matchAttrs(e, es)) {
36 11 : Expr a = queryAttr(e, "type");
37 11 : if (a && evalString(state, a) == "derivation") {
38 11 : a = queryAttr(e, "drvPath");
39 11 : if (a) {
40 11 : cout << format("%1%\n") % evalPath(state, a);
41 11 : return;
42 : }
43 0 : throw Error("bad derivation");
44 : } else {
45 0 : ATermMap drvMap;
46 0 : queryAllAttrs(e, drvMap);
47 0 : for (ATermIterator i(drvMap.keys()); i; ++i)
48 0 : printDrvPaths(state, evalExpr(state, drvMap.get(*i)));
49 0 : return;
50 : }
51 : }
52 :
53 0 : if (matchList(e, es)) {
54 0 : for (ATermIterator i(es); i; ++i)
55 0 : printDrvPaths(state, evalExpr(state, *i));
56 0 : return;
57 : }
58 :
59 0 : throw Error("expression does not evaluate to one or more derivations");
60 : }
61 :
62 :
63 : static void printResult(EvalState & state, Expr e, bool evalOnly)
64 21 : {
65 21 : if (evalOnly)
66 10 : cout << format("%1%\n") % e;
67 : else
68 11 : printDrvPaths(state, e);
69 : }
70 :
71 :
72 : void run(Strings args)
73 23 : {
74 161 : EvalState state;
75 831 : Strings files;
76 23 : bool readStdin = false;
77 23 : bool evalOnly = false;
78 23 : bool parseOnly = false;
79 :
80 58 : for (Strings::iterator it = args.begin();
81 : it != args.end(); )
82 : {
83 35 : string arg = *it++;
84 :
85 35 : if (arg == "-")
86 12 : readStdin = true;
87 23 : else if (arg == "--eval-only") {
88 7 : readOnlyMode = true;
89 7 : evalOnly = true;
90 : }
91 16 : else if (arg == "--parse-only") {
92 5 : readOnlyMode = true;
93 5 : parseOnly = evalOnly = true;
94 : }
95 11 : else if (arg[0] == '-')
96 0 : throw UsageError(format("unknown flag `%1%`") % arg);
97 : else
98 11 : files.push_back(arg);
99 : }
100 :
101 23 : openDB();
102 :
103 23 : if (readStdin) {
104 12 : Expr e = evalStdin(state, parseOnly);
105 10 : printResult(state, e, evalOnly);
106 : }
107 :
108 32 : for (Strings::iterator it = files.begin();
109 : it != files.end(); it++)
110 : {
111 11 : Expr e = evalFile(state, absPath(*it));
112 : /* !!! parseOnly ignored */
113 11 : printResult(state, e, evalOnly);
114 : }
115 :
116 21 : printEvalStats(state);
117 : }
118 :
119 :
120 46 : string programId = "nix-instantiate";
|