1 : #include <map>
2 : #include <iostream>
3 :
4 : #include "globals.hh"
5 : #include "build.hh"
6 : #include "gc.hh"
7 : #include "shared.hh"
8 : #include "eval.hh"
9 : #include "parser.hh"
10 : #include "get-drvs.hh"
11 : #include "attr-path.hh"
12 : #include "expr-to-xml.hh"
13 : #include "util.hh"
14 : #include "store.hh"
15 : #include "help.txt.hh"
16 :
17 :
18 : using namespace nix;
19 :
20 :
21 : void printHelp()
22 1 : {
23 1 : std::cout << string((char *) helpText, sizeof helpText);
24 : }
25 :
26 :
27 : static Expr parseStdin(EvalState & state)
28 12 : {
29 938 : startNest(nest, lvlTalkative, format("parsing standard input"));
30 12 : string s, s2;
31 141 : while (getline(std::cin, s2)) s += s2 + "\n";
32 12 : return parseExprFromString(state, s, absPath("."));
33 : }
34 :
35 :
36 138 : static Path gcRoot;
37 : static int rootNr = 0;
38 : static bool indirectRoot = false;
39 :
40 :
41 : static void printResult(EvalState & state, Expr e,
42 : bool evalOnly, bool xmlOutput, const ATermMap & autoArgs)
43 53 : {
44 53 : ATermList context;
45 :
46 53 : if (evalOnly)
47 30 : if (xmlOutput)
48 1 : printTermAsXML(e, std::cout, context);
49 : else
50 29 : std::cout << format("%1%\n") % e;
51 :
52 : else {
53 69 : DrvInfos drvs;
54 23 : getDerivations(state, e, "", autoArgs, drvs);
55 56 : for (DrvInfos::iterator i = drvs.begin(); i != drvs.end(); ++i) {
56 35 : Path drvPath = i->queryDrvPath(state);
57 33 : if (gcRoot == "")
58 32 : printGCWarning();
59 : else
60 1 : drvPath = addPermRoot(drvPath,
61 : makeRootName(gcRoot, rootNr),
62 : indirectRoot);
63 33 : std::cout << format("%1%\n") % drvPath;
64 : }
65 : }
66 : }
67 :
68 :
69 : Expr doEval(EvalState & state, string attrPath, bool parseOnly, bool strict,
70 : const ATermMap & autoArgs, Expr e)
71 61 : {
72 61 : e = findAlongAttrPath(state, attrPath, autoArgs, e);
73 61 : if (!parseOnly)
74 56 : if (strict)
75 1 : e = strictEvalExpr(state, e);
76 : else
77 55 : e = evalExpr(state, e);
78 53 : return e;
79 : }
80 :
81 :
82 : void run(Strings args)
83 68 : {
84 1666 : EvalState state;
85 3820 : Strings files;
86 68 : bool readStdin = false;
87 68 : bool evalOnly = false;
88 68 : bool parseOnly = false;
89 68 : bool xmlOutput = false;
90 68 : bool strict = false;
91 68 : string attrPath;
92 68 : ATermMap autoArgs(128);
93 :
94 190 : for (Strings::iterator i = args.begin();
95 : i != args.end(); )
96 : {
97 122 : string arg = *i++;
98 :
99 122 : if (arg == "-")
100 12 : readStdin = true;
101 110 : else if (arg == "--eval-only") {
102 33 : readOnlyMode = true;
103 33 : evalOnly = true;
104 : }
105 77 : else if (arg == "--parse-only") {
106 12 : readOnlyMode = true;
107 12 : parseOnly = evalOnly = true;
108 : }
109 65 : else if (arg == "--attr" || arg == "-A") {
110 5 : if (i == args.end())
111 0 : throw UsageError("`--attr' requires an argument");
112 5 : attrPath = *i++;
113 : }
114 60 : else if (arg == "--arg") {
115 0 : if (i == args.end())
116 0 : throw UsageError("`--arg' requires two arguments");
117 0 : string name = *i++;
118 0 : if (i == args.end())
119 0 : throw UsageError("`--arg' requires two arguments");
120 0 : Expr value = parseExprFromString(state, *i++, absPath("."));
121 0 : autoArgs.set(toATerm(name), value);
122 : }
123 60 : else if (arg == "--add-root") {
124 1 : if (i == args.end())
125 0 : throw UsageError("`--add-root' requires an argument");
126 1 : gcRoot = absPath(*i++);
127 : }
128 59 : else if (arg == "--indirect")
129 1 : indirectRoot = true;
130 58 : else if (arg == "--xml")
131 1 : xmlOutput = true;
132 57 : else if (arg == "--strict")
133 1 : strict = true;
134 56 : else if (arg[0] == '-')
135 0 : throw UsageError(format("unknown flag `%1%'") % arg);
136 : else
137 56 : files.push_back(arg);
138 : }
139 :
140 68 : openDB();
141 :
142 68 : if (readStdin) {
143 12 : Expr e = parseStdin(state);
144 5 : e = doEval(state, attrPath, parseOnly, strict, autoArgs, e);
145 5 : printResult(state, e, evalOnly, xmlOutput, autoArgs);
146 : }
147 :
148 107 : for (Strings::iterator i = files.begin();
149 : i != files.end(); i++)
150 : {
151 56 : Path path = absPath(*i);
152 56 : Expr e = parseExprFromFile(state, path);
153 56 : e = doEval(state, attrPath, parseOnly, strict, autoArgs, e);
154 48 : printResult(state, e, evalOnly, xmlOutput, autoArgs);
155 : }
156 :
157 51 : printEvalStats(state);
158 : }
159 :
160 :
161 138 : string programId = "nix-instantiate";
|