Allow alternates to be listed for packages
[mspang/inapt.git] / inapt.cc
index 3e6ac19..f2edb46 100644 (file)
--- a/inapt.cc
+++ b/inapt.cc
@@ -183,15 +183,84 @@ static void usage() {
     exit(2);
 }
 
+static bool test_macro(const char *macro, set<string> *defines) {
+    return (*macro != '!' && defines->find(macro) != defines->end())
+            || (*macro == '!' && defines->find(macro + 1) == defines->end());
+}
+
+static pkgCache::PkgIterator eval_pkg(inapt_action *action, pkgCacheFile &cachef) {
+    pkgCache::PkgIterator pkg;
+
+    pkgCache *cache = cachef;
+
+    if (!pkg.end()) fatal("omg"); /* TODO */
+
+    for (std::vector<std::string>::iterator i = action->alternates.begin(); i != action->alternates.end(); i++) {
+        pkg = cache->FindPkg(*i);
+
+        /* no such package */
+        if (pkg.end())
+            continue;
+
+        /* real package */
+        if (cachef[pkg].CandidateVer)
+            break;
+
+        /* virtual package */
+        if (pkg->ProvidesList) {
+            if (!pkg.ProvidesList()->NextProvides) {
+                pkgCache::PkgIterator tmp = pkg.ProvidesList().OwnerPkg();
+                if (action->action == inapt_action::INSTALL) {
+                    debug("selecting %s instead of %s", tmp.Name(), pkg.Name());
+                    pkg = tmp;
+                    break;
+                } else {
+                    debug("will not remove %s instead of virtual package %s", tmp.Name(), pkg.Name());
+                }
+            } else {
+                debug("%s is a virtual package", pkg.Name());
+            }
+        } else {
+            debug("%s is a virtual packages with no provides", pkg.Name());
+        }
+    }
+
+    if (pkg.end()) {
+        /* todo: report all errors at the end */
+        if (action->alternates.size() == 1) {
+            fatal("%s:%d: No such package: %s", action->filename, action->linenum, action->alternates[0].c_str());
+        } else {
+            std::vector<std::string>::iterator i = action->alternates.begin();
+            std::string message = *(i++);
+            while (i != action->alternates.end()) {
+                message.append(", ");
+                message.append(*(i++));
+            }
+            fatal("%s:%d: No alternative available: %s", action->filename, action->linenum, message.c_str());
+        }
+    }
+
+    return pkg;
+}
+
 static void eval_block(inapt_block *block, set<string> *defines, std::vector<inapt_action *> *final_actions) {
     if (!block)
         return;
 
-    for (vector<inapt_action *>::iterator i = block->actions.begin(); i < block->actions.end(); i++)
-        final_actions->push_back(*i);
+    for (vector<inapt_action *>::iterator i = block->actions.begin(); i < block->actions.end(); i++) {
+        bool ok = true;
+        for (vector<const char *>::iterator j = (*i)->predicates.begin(); j < (*i)->predicates.end(); j++) {
+            if (!test_macro(*j, defines)) {
+                ok = false;
+                break;
+            }
+        }
+        if (ok)
+            final_actions->push_back(*i);
+    }
 
     for (vector<inapt_conditional *>::iterator i = block->children.begin(); i < block->children.end(); i++) {
-        if (defines->find((*i)->condition) != defines->end())
+        if (test_macro((*i)->condition, defines))
             eval_block((*i)->then_block, defines, final_actions);
         else
             eval_block((*i)->else_block, defines, final_actions);
@@ -216,36 +285,15 @@ static void exec_actions(std::vector<inapt_action *> *final_actions) {
     pkgCache *cache = cachef;
     pkgDepCache *DCache = cachef;
 
-    for (vector<inapt_action *>::iterator i = final_actions->begin(); i < final_actions->end(); i++) {
-        pkgCache::PkgIterator pkg = cache->FindPkg((*i)->package);
-        if (pkg.end())
-            fatal("%s:%d: No such package: %s", (*i)->filename, (*i)->linenum, (*i)->package);
-        (*i)->pkg = pkg;
-        if (!cachef[pkg].CandidateVer) {
-            if (pkg->ProvidesList) {
-                if (!pkg.ProvidesList()->NextProvides) {
-                    pkgCache::PkgIterator tmp = pkg.ProvidesList().OwnerPkg();
-                    if ((*i)->action == inapt_action::INSTALL) {
-                        debug("selecting %s instead of %s", tmp.Name(), pkg.Name());
-                        (*i)->pkg = tmp;
-                    } else {
-                        debug("will not remove %s instead of virtual package %s", tmp.Name(), pkg.Name());
-                    }
-                } else {
-                    fatal("%s is a virtual package", pkg.Name());
-                }
-            } else {
-                fatal("%s is a virtual packages with no provides", pkg.Name());
-            }
-        }
-    }
+    for (vector<inapt_action *>::iterator i = final_actions->begin(); i < final_actions->end(); i++)
+        (*i)->pkg = eval_pkg(*i, cachef);
 
     for (vector<inapt_action *>::iterator i = final_actions->begin(); i < final_actions->end(); i++) {
         pkgCache::PkgIterator j = (*i)->pkg;
         switch ((*i)->action) {
             case inapt_action::INSTALL:
-                if (!j.CurVersion() || cachef[j].Delete()) {
-                    printf("preinstall %s %s:%d\n", (*i)->package, (*i)->filename, (*i)->linenum);
+                if (!j.CurrentVer() || cachef[j].Delete()) {
+                    printf("preinstall %s %s:%d\n", (*i)->pkg.Name(), (*i)->filename, (*i)->linenum);
                     DCache->MarkInstall(j, true);
                 }
                 break;
@@ -260,19 +308,15 @@ static void exec_actions(std::vector<inapt_action *> *final_actions) {
         pkgCache::PkgIterator j = (*i)->pkg;
         switch ((*i)->action) {
             case inapt_action::INSTALL:
-                if (!j.CurVersion() || cachef[j].Delete()) {
-                    printf("install %s %s:%d\n", (*i)->package, (*i)->filename, (*i)->linenum);
+                if ((!j.CurrentVer() && !cachef[j].Install()) || cachef[j].Delete()) {
+                    printf("install %s %s:%d\n", (*i)->pkg.Name(), (*i)->filename, (*i)->linenum);
                     DCache->MarkInstall(j, false);
-                } else {
-                    //printf("install %s %s:%d NTD\n", (*i)->package, (*i)->filename, (*i)->linenum);
                 }
                 break;
             case inapt_action::REMOVE:
-                if (j.CurVersion() || cachef[j].Install()) {
-                    printf("remove %s %s:%d %s\n", (*i)->package, (*i)->filename, (*i)->linenum, j.CurVersion());
+                if ((j.CurrentVer() && !cachef[j].Delete()) || cachef[j].Install()) {
+                    printf("remove %s %s:%d\n", (*i)->pkg.Name(), (*i)->filename, (*i)->linenum);
                     DCache->MarkDelete(j, false);
-                } else {
-                    //printf("remove %s %s:%d NTD\n", (*i)->package, (*i)->filename, (*i)->linenum);
                 }
                 break;
             default:
@@ -308,9 +352,7 @@ static void exec_actions(std::vector<inapt_action *> *final_actions) {
     pkgProblemResolver fix (DCache);
 
     for (vector<inapt_action *>::iterator i = final_actions->begin(); i < final_actions->end(); i++)
-           fix.Protect(cache->FindPkg((*i)->package));
-    for (vector<inapt_action *>::iterator i = final_actions->begin(); i < final_actions->end(); i++)
-           fix.Protect(cache->FindPkg((*i)->package));
+           fix.Protect((*i)->pkg);
     fix.Resolve();
 
     fprintf(stderr, "\n");
@@ -340,14 +382,11 @@ int main(int argc, char *argv[]) {
     set<string> defines;
 
     prog = xstrdup(basename(argv[0]));
-    while ((opt = getopt_long(argc, argv, "D:U:", opts, NULL)) != -1) {
+    while ((opt = getopt_long(argc, argv, "D:", opts, NULL)) != -1) {
         switch (opt) {
             case 'D':
                 defines.insert(optarg);
                 break;
-            case 'U':
-                defines.erase(optarg);
-                break;
             case '?':
                 usage();
                 break;