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