aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2007-11-21 13:49:59 +0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2007-11-21 13:49:59 +0000
commit06f95dd07ce691a1f12c8ec7fa5ad84858d81cd0 (patch)
treebca2f5bb29b4a6844eb8f35c04d4e9bda7025128
parentc370755583d7350f4b96136eb0a6a8a7b08551b1 (diff)
* New primop `readFile' to get the contents of a file as a string.
-rw-r--r--doc/manual/builtins.xml9
-rw-r--r--src/libexpr/primops.cc12
-rw-r--r--tests/lang/eval-okay-readfile.exp1
-rw-r--r--tests/lang/eval-okay-readfile.nix1
4 files changed, 23 insertions, 0 deletions
diff --git a/doc/manual/builtins.xml b/doc/manual/builtins.xml
index b6c886199..4c8e3163b 100644
--- a/doc/manual/builtins.xml
+++ b/doc/manual/builtins.xml
@@ -441,6 +441,15 @@ in config.someSetting</programlisting>
-->
+ <varlistentry><term><function>builtins.readFile</function>
+ <replaceable>path</replaceable></term>
+
+ <listitem><para>Return the contents of the file
+ <replaceable>path</replaceable> as a string.</para></listitem>
+
+ </varlistentry>
+
+
<varlistentry><term><function>removeAttrs</function>
<replaceable>attrs</replaceable> <replaceable>list</replaceable></term>
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc
index 2a96e25a8..bb9190579 100644
--- a/src/libexpr/primops.cc
+++ b/src/libexpr/primops.cc
@@ -622,6 +622,17 @@ static Expr prim_dirOf(EvalState & state, const ATermVector & args)
}
+/* Return the contents of a file as a string. */
+static Expr prim_readFile(EvalState & state, const ATermVector & args)
+{
+ PathSet context;
+ Path path = coerceToPath(state, args[0], context);
+ if (!context.empty())
+ throw EvalError(format("string `%1%' cannot refer to other paths") % path);
+ return makeStr(readFile(path));
+}
+
+
/*************************************************************
* Creating files
*************************************************************/
@@ -968,6 +979,7 @@ void EvalState::addPrimOps()
addPrimOp("__pathExists", 1, prim_pathExists);
addPrimOp("baseNameOf", 1, prim_baseNameOf);
addPrimOp("dirOf", 1, prim_dirOf);
+ addPrimOp("__readFile", 1, prim_readFile);
// Creating files
addPrimOp("__toXML", 1, prim_toXML);
diff --git a/tests/lang/eval-okay-readfile.exp b/tests/lang/eval-okay-readfile.exp
new file mode 100644
index 000000000..979771c7a
--- /dev/null
+++ b/tests/lang/eval-okay-readfile.exp
@@ -0,0 +1 @@
+Str("builtins.readFile ./eval-okay-readfile.nix\n",[])
diff --git a/tests/lang/eval-okay-readfile.nix b/tests/lang/eval-okay-readfile.nix
new file mode 100644
index 000000000..82f7cb174
--- /dev/null
+++ b/tests/lang/eval-okay-readfile.nix
@@ -0,0 +1 @@
+builtins.readFile ./eval-okay-readfile.nix