about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2008-07-01T10·10+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2008-07-01T10·10+0000
commitd567baabbd99fdb92e67295a77aef76ef970e65c (patch)
treed31c1737c52da705c17da78edb517bc2e56b841a /src
parentb3b0b2a29e2842784f80cf839f84af18b0b83e90 (diff)
* Export the nix-env derivation name parsing and version comparison
  logic through the `parseDrvName' and `compareVersions' primops.
  This will allow expressions to easily check whether some dependency
  is a specific needed version or falls in some version range.  See
  tests/lang/eval-okay-versions.nix for examples.

Diffstat (limited to 'src')
-rw-r--r--src/libexpr/Makefile.am6
-rw-r--r--src/libexpr/names.cc (renamed from src/nix-env/names.cc)19
-rw-r--r--src/libexpr/names.hh (renamed from src/nix-env/names.hh)0
-rw-r--r--src/libexpr/primops.cc63
-rw-r--r--src/nix-env/Makefile.am3
5 files changed, 54 insertions, 37 deletions
diff --git a/src/libexpr/Makefile.am b/src/libexpr/Makefile.am
index b08c079f4d30..aacc1dcc3b16 100644
--- a/src/libexpr/Makefile.am
+++ b/src/libexpr/Makefile.am
@@ -2,11 +2,13 @@ pkglib_LTLIBRARIES = libexpr.la
 
 libexpr_la_SOURCES = \
  nixexpr.cc eval.cc primops.cc lexer-tab.cc parser-tab.cc \
- get-drvs.cc attr-path.cc expr-to-xml.cc common-opts.cc
+ get-drvs.cc attr-path.cc expr-to-xml.cc common-opts.cc \
+ names.cc
 
 pkginclude_HEADERS = \
  nixexpr.hh eval.hh parser.hh lexer-tab.hh parser-tab.hh \
- get-drvs.hh attr-path.hh expr-to-xml.hh common-opts.hh
+ get-drvs.hh attr-path.hh expr-to-xml.hh common-opts.hh \
+ names.hh
 
 libexpr_la_LIBADD = ../libutil/libutil.la ../libstore/libstore.la \
  ../boost/format/libformat.la
diff --git a/src/nix-env/names.cc b/src/libexpr/names.cc
index a425b8d0762c..29c7ddb4d70f 100644
--- a/src/nix-env/names.cc
+++ b/src/libexpr/names.cc
@@ -92,25 +92,6 @@ int compareVersions(const string & v1, const string & v2)
 }
 
 
-static void testCompareVersions()
-{
-#define TEST(v1, v2, n) assert( \
-    compareVersions(v1, v2) == n && compareVersions(v2, v1) == -n)
-    TEST("1.0", "2.3", -1);
-    TEST("2.1", "2.3", -1);
-    TEST("2.3", "2.3", 0);
-    TEST("2.5", "2.3", 1);
-    TEST("3.1", "2.3", 1);
-    TEST("2.3.1", "2.3", 1);
-    TEST("2.3.1", "2.3a", 1);
-    TEST("2.3pre1", "2.3", -1);
-    TEST("2.3pre3", "2.3pre12", -1);
-    TEST("2.3a", "2.3c", -1);
-    TEST("2.3pre1", "2.3c", -1);
-    TEST("2.3pre1", "2.3q", -1);
-}
-
-
 DrvNames drvNamesFromArgs(const Strings & opArgs)
 {
     DrvNames result;
diff --git a/src/nix-env/names.hh b/src/libexpr/names.hh
index e189302d6d94..e189302d6d94 100644
--- a/src/nix-env/names.hh
+++ b/src/libexpr/names.hh
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc
index 864292d4a4b8..fbef227223dc 100644
--- a/src/libexpr/primops.cc
+++ b/src/libexpr/primops.cc
@@ -7,6 +7,7 @@
 #include "expr-to-xml.hh"
 #include "nixexpr-ast.hh"
 #include "parser.hh"
+#include "names.hh"
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -817,7 +818,7 @@ static Expr prim_removeAttrs(EvalState & state, const ATermVector & args)
 }
 
 
-/* Determine whether the argument is a list. */
+/* Determine whether the argument is an attribute set. */
 static Expr prim_isAttrs(EvalState & state, const ATermVector & args)
 {
     ATermList list;
@@ -950,23 +951,54 @@ static Expr prim_unsafeDiscardStringContext(EvalState & state, const ATermVector
     return makeStr(s, PathSet());
 }
 
-/* Expression serialization/deserialization */ 
 
-static Expr prim_ExprToString ( EvalState & state, const ATermVector & args)
+/* Expression serialization/deserialization */
+
+
+static Expr prim_exprToString(EvalState & state, const ATermVector & args)
 {
-	return makeStr ( atPrint ( evalExpr ( state, args [ 0 ] ) ) );
+    /* !!! this disregards context */
+    return makeStr(atPrint(evalExpr(state, args[0])));
 }
 
-static Expr prim_StringToExpr ( EvalState & state, const ATermVector & args)
+
+static Expr prim_stringToExpr(EvalState & state, const ATermVector & args)
 {
-	string s;
-	PathSet l;
-	if (! matchStr ( evalExpr ( state, args[0] ), s, l )) {
-		throw EvalError("__stringToExpr needs string argument!");
-	}
-	return ATreadFromString(s.c_str());
+    /* !!! this can introduce arbitrary garbage terms in the
+       evaluator! */;
+    string s;
+    PathSet l;
+    if (!matchStr(evalExpr(state, args[0]), s, l))
+        throw EvalError("stringToExpr needs string argument!");
+    return ATreadFromString(s.c_str());
 }
 
+
+/*************************************************************
+ * Versions
+ *************************************************************/
+
+
+static Expr prim_parseDrvName(EvalState & state, const ATermVector & args)
+{
+    string name = evalStringNoCtx(state, args[0]);
+    DrvName parsed(name);
+    ATermMap attrs(2);
+    attrs.set(toATerm("name"), makeAttrRHS(makeStr(parsed.name), makeNoPos()));
+    attrs.set(toATerm("version"), makeAttrRHS(makeStr(parsed.version), makeNoPos()));
+    return makeAttrs(attrs);
+}
+
+
+static Expr prim_compareVersions(EvalState & state, const ATermVector & args)
+{
+    string version1 = evalStringNoCtx(state, args[0]);
+    string version2 = evalStringNoCtx(state, args[1]);
+    int d = compareVersions(version1, version2);
+    return makeInt(d);
+}
+
+
 /*************************************************************
  * Primop registration
  *************************************************************/
@@ -994,8 +1026,8 @@ void EvalState::addPrimOps()
     addPrimOp("__trace", 2, prim_trace);
     
     // Expr <-> String
-    addPrimOp("__exprToString", 1, prim_ExprToString);
-    addPrimOp("__stringToExpr", 1, prim_StringToExpr);
+    addPrimOp("__exprToString", 1, prim_exprToString);
+    addPrimOp("__stringToExpr", 1, prim_stringToExpr);
 
     addPrimOp("relativise", 2, prim_relativise);
 
@@ -1039,7 +1071,10 @@ void EvalState::addPrimOps()
     addPrimOp("__substring", 3, prim_substring);
     addPrimOp("__stringLength", 1, prim_stringLength);
     addPrimOp("__unsafeDiscardStringContext", 1, prim_unsafeDiscardStringContext);
-    
+
+    // Versions
+    addPrimOp("__parseDrvName", 1, prim_parseDrvName);
+    addPrimOp("__compareVersions", 2, prim_compareVersions);
 }
 
 
diff --git a/src/nix-env/Makefile.am b/src/nix-env/Makefile.am
index a1e58cf006bc..4720dfe8b780 100644
--- a/src/nix-env/Makefile.am
+++ b/src/nix-env/Makefile.am
@@ -1,7 +1,6 @@
 bin_PROGRAMS = nix-env
 
-nix_env_SOURCES = nix-env.cc names.cc names.hh \
- profiles.cc profiles.hh help.txt
+nix_env_SOURCES = nix-env.cc profiles.cc profiles.hh help.txt
 nix_env_LDADD = ../libmain/libmain.la ../libexpr/libexpr.la \
  ../libstore/libstore.la ../libutil/libutil.la \
  ../boost/format/libformat.la ${bdb_lib} ${aterm_lib}