]> git.saurik.com Git - apt.git/blobdiff - cmdline/apt-cache.cc
Fixed error reporting for empty host strings and invali...
[apt.git] / cmdline / apt-cache.cc
index a235ad9a17f82dbaf58f8ad9902736a99e70fac8..5928676de8aa62dcb2b0a04f713cea912c51e7b2 100644 (file)
@@ -1,6 +1,6 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
-// $Id: apt-cache.cc,v 1.30 1999/03/18 04:52:51 jgg Exp $
+// $Id: apt-cache.cc,v 1.43 2000/05/12 04:00:59 jgg Exp $
 /* ######################################################################
    
    apt-cache - Manages the cache files
@@ -8,7 +8,7 @@
    apt-cache provides some functions fo manipulating the cache files.
    It uses the command line interface common to all the APT tools. The
    only really usefull function right now is dumpavail which is used
-   by the dselect method. Everything else is ment as a debug aide.
+   by the dselect method. Everything else is meant as a debug aide.
    
    Returns 100 on failure, 0 on success.
    
 #include <apt-pkg/cmndline.h>
 #include <apt-pkg/strutl.h>
 #include <apt-pkg/pkgrecords.h>
+#include <apt-pkg/srcrecords.h>
+#include <apt-pkg/version.h>
 #include <config.h>
 
 #include <iostream.h>
 #include <unistd.h>
 #include <errno.h>
 #include <regex.h>
+#include <stdio.h>
                                                                        /*}}}*/
 
 pkgCache *GCache = 0;
@@ -176,9 +179,7 @@ bool DumpPackage(CommandLine &CmdL)
       }
       cout << "Reverse Provides: " << endl;
       for (pkgCache::PrvIterator Prv = Pkg.ProvidesList(); Prv.end() != true; Prv++)
-        cout << Prv.OwnerPkg().Name() << " " << Prv.OwnerVer().VerStr();
-      cout << endl;
-            
+        cout << Prv.OwnerPkg().Name() << " " << Prv.OwnerVer().VerStr() << endl;            
    }
 
    return true;
@@ -331,30 +332,6 @@ bool Dump(CommandLine &Cmd)
    return true;
 }
                                                                        /*}}}*/
-// GetCandidateVer - Returns the Candidate install version             /*{{{*/
-// ---------------------------------------------------------------------
-/* This should really use the DepCache or something.. Copied from there. 
-   Allow current is required to filter out the status file when emitting
-   an available file. */
-static pkgCache::VerIterator GetCandidateVer(pkgCache::PkgIterator Pkg,
-                                            bool AllowCurrent = true)
-{
-   /* Not source/not automatic versions cannot be a candidate version 
-    unless they are already installed */
-   for (pkgCache::VerIterator I = Pkg.VersionList(); I.end() == false; I++)
-   {
-      if (Pkg.CurrentVer() == I && AllowCurrent == true)
-        return I;
-      
-      for (pkgCache::VerFileIterator J = I.FileList(); J.end() == false; J++)
-        if ((J.File()->Flags & pkgCache::Flag::NotSource) == 0 &&
-            (J.File()->Flags & pkgCache::Flag::NotAutomatic) == 0)
-           return I;
-   }
-   
-   return pkgCache::VerIterator(*GCache,0);
-}
-                                                                       /*}}}*/
 // DumpAvail - Print out the available list                            /*{{{*/
 // ---------------------------------------------------------------------
 /* This is needed to make dpkg --merge happy */
@@ -386,7 +363,7 @@ bool DumpAvail(CommandLine &Cmd)
       for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
       {
         // Find the proper version to use. We should probably use the DepCache.
-        pkgCache::VerIterator V = GetCandidateVer(P,false);
+        pkgCache::VerIterator V = Cache.GetCandidateVer(P,false);
 
         if (V.end() == true || V.FileList().File() != I)
            continue;
@@ -402,6 +379,270 @@ bool DumpAvail(CommandLine &Cmd)
       }
    }
    
+   return true;
+}
+                                                                       /*}}}*/
+// Depends - Print out a dependency tree                               /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool Depends(CommandLine &CmdL)
+{
+   pkgCache &Cache = *GCache;
+   
+   for (const char **I = CmdL.FileList + 1; *I != 0; I++)
+   {
+      pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
+      if (Pkg.end() == true)
+      {
+        _error->Warning("Unable to locate package %s",*I);
+        continue;
+      }
+      
+      pkgCache::VerIterator Ver = Pkg.VersionList();
+      if (Ver.end() == true)
+      {
+        cout << '<' << Pkg.Name() << '>' << endl;
+        continue;
+      }
+
+      cout << Pkg.Name() << endl;
+      
+      for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
+      {
+        if ((D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or)
+           cout << " |";
+        else
+           cout << "  ";
+        
+        // Show the package
+        pkgCache::PkgIterator Trg = D.TargetPkg();
+        if (Trg->VersionList == 0)
+           cout << D.DepType() << ": <" << Trg.Name() << ">" << endl;
+        else
+           cout << D.DepType() << ": " << Trg.Name() << endl;
+           
+        // Display all solutions
+        pkgCache::Version **List = D.AllTargets();
+        for (pkgCache::Version **I = List; *I != 0; I++)
+        {
+           pkgCache::VerIterator V(Cache,*I);
+           if (V != Cache.VerP + V.ParentPkg()->VersionList || 
+               V->ParentPkg == D->Package)
+              continue;
+           cout << "    " << V.ParentPkg().Name() << endl;
+        }
+        delete [] List;
+      }
+   }   
+   
+   return true;
+}
+                                                                       /*}}}*/
+// Dotty - Generate a graph for Dotty                                  /*{{{*/
+// ---------------------------------------------------------------------
+/* Dotty is the graphvis program for generating graphs. It is a fairly
+   simple queuing algorithm that just writes dependencies and nodes. 
+   http://www.research.att.com/sw/tools/graphviz/ */
+bool Dotty(CommandLine &CmdL)
+{
+   pkgCache &Cache = *GCache;
+   bool GivenOnly = _config->FindB("APT::Cache::GivenOnly",false);
+   
+   /* Normal packages are boxes
+      Pure Provides are triangles
+      Mixed are diamonds
+      Hexagons are missing packages*/
+   const char *Shapes[] = {"hexagon","triangle","box","diamond"};
+   
+   /* Initialize the list of packages to show.
+      1 = To Show
+      2 = To Show no recurse
+      3 = Emitted no recurse
+      4 = Emitted
+      0 = None */
+   enum States {None=0, ToShow, ToShowNR, DoneNR, Done};
+   enum TheFlags {ForceNR=(1<<0)};
+   unsigned char *Show = new unsigned char[Cache.Head().PackageCount];
+   unsigned char *Flags = new unsigned char[Cache.Head().PackageCount];
+   unsigned char *ShapeMap = new unsigned char[Cache.Head().PackageCount];
+   
+   // Show everything if no arguments given
+   if (CmdL.FileList[1] == 0)
+      for (unsigned long I = 0; I != Cache.Head().PackageCount; I++)
+        Show[I] = ToShow;
+   else
+      for (unsigned long I = 0; I != Cache.Head().PackageCount; I++)
+        Show[I] = None;
+   memset(Flags,0,sizeof(*Flags)*Cache.Head().PackageCount);
+   
+   // Map the shapes
+   for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
+   {   
+      if (Pkg->VersionList == 0)
+      {
+        // Missing
+        if (Pkg->ProvidesList == 0)
+           ShapeMap[Pkg->ID] = 0;
+        else
+           ShapeMap[Pkg->ID] = 1;
+      }
+      else
+      {
+        // Normal
+        if (Pkg->ProvidesList == 0)
+           ShapeMap[Pkg->ID] = 2;
+        else
+           ShapeMap[Pkg->ID] = 3;
+      }
+   }
+   
+   // Load the list of packages from the command line into the show list
+   for (const char **I = CmdL.FileList + 1; *I != 0; I++)
+   {
+      // Process per-package flags
+      string P = *I;
+      bool Force = false;
+      if (P.length() > 3)
+      {
+        if (P.end()[-1] == '^')
+        {
+           Force = true;
+           P.erase(P.end()-1);
+        }
+        
+        if (P.end()[-1] == ',')
+           P.erase(P.end()-1);
+      }
+      
+      // Locate the package
+      pkgCache::PkgIterator Pkg = Cache.FindPkg(P);
+      if (Pkg.end() == true)
+      {
+        _error->Warning("Unable to locate package %s",*I);
+        continue;
+      }
+      Show[Pkg->ID] = ToShow;
+      
+      if (Force == true)
+        Flags[Pkg->ID] |= ForceNR;
+   }
+   
+   // Little header
+   printf("digraph packages {\n");
+   printf("concentrate=true;\n");
+   printf("size=\"30,40\";\n");
+   
+   bool Act = true;
+   while (Act == true)
+   {
+      Act = false;
+      for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
+      {
+        // See we need to show this package
+        if (Show[Pkg->ID] == None || Show[Pkg->ID] >= DoneNR)
+           continue;
+        
+        // Colour as done
+        if (Show[Pkg->ID] == ToShowNR || (Flags[Pkg->ID] & ForceNR) == ForceNR)
+        {
+           // Pure Provides and missing packages have no deps!
+           if (ShapeMap[Pkg->ID] == 0 || ShapeMap[Pkg->ID] == 1)
+              Show[Pkg->ID] = Done;
+           else
+              Show[Pkg->ID] = DoneNR;
+        }       
+        else
+           Show[Pkg->ID] = Done;
+        Act = true;
+
+        // No deps to map out
+        if (Pkg->VersionList == 0 || Show[Pkg->ID] == DoneNR)
+           continue;
+        
+        pkgCache::VerIterator Ver = Pkg.VersionList();
+        for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
+        {
+           // See if anything can meet this dep
+           // Walk along the actual package providing versions
+           bool Hit = false;
+           pkgCache::PkgIterator DPkg = D.TargetPkg();
+           for (pkgCache::VerIterator I = DPkg.VersionList();
+                     I.end() == false && Hit == false; I++)
+           {
+              if (pkgCheckDep(D.TargetVer(),I.VerStr(),D->CompareOp) == true)
+                 Hit = true;
+           }
+           
+           // Follow all provides
+           for (pkgCache::PrvIterator I = DPkg.ProvidesList(); 
+                     I.end() == false && Hit == false; I++)
+           {
+              if (pkgCheckDep(D.TargetVer(),I.ProvideVersion(),D->CompareOp) == false)
+                 Hit = true;
+           }
+           
+           // Only graph critical deps     
+           if (D.IsCritical() == true)
+           {
+              printf("\"%s\" -> \"%s\"",Pkg.Name(),D.TargetPkg().Name());
+              
+              // Colour the node for recursion
+              if (Show[D.TargetPkg()->ID] <= DoneNR)
+              {
+                 /* If a conflicts does not meet anything in the database
+                    then show the relation but do not recurse */
+                 if (Hit == false && D->Type == pkgCache::Dep::Conflicts)
+                 {
+                    if (Show[D.TargetPkg()->ID] == None && 
+                        Show[D.TargetPkg()->ID] != ToShow)
+                       Show[D.TargetPkg()->ID] = ToShowNR;
+                 }               
+                 else
+                 {
+                    if (GivenOnly == true && Show[D.TargetPkg()->ID] != ToShow)
+                       Show[D.TargetPkg()->ID] = ToShowNR;
+                    else
+                       Show[D.TargetPkg()->ID] = ToShow;
+                 }
+              }
+              
+              // Edge colour
+              switch(D->Type)
+              {
+                 case pkgCache::Dep::Conflicts:
+                 printf("[color=springgreen];\n");
+                 break;
+                 
+                 case pkgCache::Dep::PreDepends:
+                 printf("[color=blue];\n");
+                 break;
+                 
+                 default:
+                 printf(";\n");
+                 break;
+              }               
+           }       
+        }
+      }
+   }   
+   
+   /* Draw the box colours after the fact since we can not tell what colour
+      they should be until everything is finished drawing */
+   for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
+   {
+      if (Show[Pkg->ID] < DoneNR)
+        continue;
+      
+      // Orange box for early recursion stoppage
+      if (Show[Pkg->ID] == DoneNR)
+        printf("\"%s\" [color=orange,shape=%s];\n",Pkg.Name(),
+               Shapes[ShapeMap[Pkg->ID]]);
+      else
+        printf("\"%s\" [shape=%s];\n",Pkg.Name(),
+               Shapes[ShapeMap[Pkg->ID]]);
+   }
+   
+   printf("}\n");
    return true;
 }
                                                                        /*}}}*/
@@ -521,7 +762,7 @@ bool Search(CommandLine &CmdL)
    for (;I.end() != true; I++)
    {
       // We search against the install version as that makes the most sense..
-      pkgCache::VerIterator V = GetCandidateVer(I);
+      pkgCache::VerIterator V = Cache.GetCandidateVer(I);
       if (V.end() == true)
         continue;
 
@@ -558,12 +799,82 @@ bool ShowPackage(CommandLine &CmdL)
       }
       
       // Find the proper version to use. We should probably use the DepCache.
-      pkgCache::VerIterator V = GetCandidateVer(Pkg);
-      if (V.end() == true || V.FileList().end() == true)
+      if (_config->FindB("APT::Cache::AllVersions","true") == true)
+      {
+        pkgCache::VerIterator V;
+        for (V = Pkg.VersionList(); V.end() == false; V++)
+        {
+           if (DisplayRecord(V) == false)
+              return false;
+        }
+      }
+      else
+      {
+        pkgCache::VerIterator V = Cache.GetCandidateVer(Pkg);
+        if (V.end() == true || V.FileList().end() == true)
+           continue;
+        if (DisplayRecord(V) == false)
+           return false;
+      }      
+   }
+   return true;
+}
+                                                                       /*}}}*/
+// ShowPkgNames - Show package names                                   /*{{{*/
+// ---------------------------------------------------------------------
+/* This does a prefix match on the first argument */
+bool ShowPkgNames(CommandLine &CmdL)
+{
+   pkgCache &Cache = *GCache;
+   pkgCache::PkgIterator I = Cache.PkgBegin();
+   bool All = _config->FindB("APT::Cache::AllNames","false");
+   
+   if (CmdL.FileList[1] != 0)
+   {
+      for (;I.end() != true; I++)
+      {
+        if (All == false && I->VersionList == 0)
+           continue;
+        
+        if (strncmp(I.Name(),CmdL.FileList[1],strlen(CmdL.FileList[1])) == 0)
+           cout << I.Name() << endl;
+      }
+
+      return true;
+   }
+   
+   // Show all pkgs
+   for (;I.end() != true; I++)
+   {
+      if (All == false && I->VersionList == 0)
         continue;
-      if (DisplayRecord(V) == false)
-        return false;
+      cout << I.Name() << endl;
    }
+   
+   return true;
+}
+                                                                       /*}}}*/
+// ShowSrcPackage - Show source package records                                /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool ShowSrcPackage(CommandLine &CmdL)
+{
+   pkgSourceList List;
+   List.ReadMainList();
+   
+   // Create the text record parsers
+   pkgSrcRecords SrcRecs(List);
+   if (_error->PendingError() == true)
+      return false;
+
+   for (const char **I = CmdL.FileList + 1; *I != 0; I++)
+   {
+      SrcRecs.Restart();
+      
+      pkgSrcRecords::Parser *Parse;
+      while ((Parse = SrcRecs.Find(*I,false)) != 0)
+        cout << Parse->AsStr();
+   }      
    return true;
 }
                                                                        /*}}}*/
@@ -595,7 +906,7 @@ bool ShowHelp(CommandLine &Cmd)
    cout << endl;
    cout << "apt-cache is a low-level tool used to manipulate APT's binary" << endl;
    cout << "cache files stored in " << _config->FindFile("Dir::Cache") << endl;
-   cout << "It is not ment for ordinary use only as a debug aide." << endl;
+   cout << "It is not meant for ordinary use only as a debug aide." << endl;
    cout << endl;
    cout << "Commands:" << endl;
    cout << "   add - Add an package file to the source cache" << endl;
@@ -608,6 +919,9 @@ bool ShowHelp(CommandLine &Cmd)
    cout << "   check - Check the cache a bit" << endl;
    cout << "   search - Search the package list for a regex pattern" << endl;
    cout << "   show - Show a readable record for the package" << endl;
+   cout << "   depends - Show raw dependency information for a package" << endl;
+   cout << "   pkgnames - List the names of all packages" << endl;
+   cout << "   dotty - Generate package graphs for GraphVis" << endl;
    cout << endl;
    cout << "Options:" << endl;
    cout << "  -h   This help text." << endl;
@@ -616,7 +930,7 @@ bool ShowHelp(CommandLine &Cmd)
    cout << "  -q   Disable progress indicator." << endl;
    cout << "  -i   Show only important deps for the unmet command." << endl;
    cout << "  -c=? Read this configuration file" << endl;
-   cout << "  -o=? Set an arbitary configuration option, ie -o dir::cache=/tmp" << endl;
+   cout << "  -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp" << endl;
    cout << "See the apt-cache(8) and apt.conf(5) manual pages for more information." << endl;
    return 100;
 }
@@ -641,13 +955,17 @@ int main(int argc,const char *argv[])
       {'q',"quiet","quiet",CommandLine::IntLevel},
       {'i',"important","APT::Cache::Important",0},
       {'f',"full","APT::Cache::ShowFull",0},
+      {'g',"no-generate","APT::Cache::NoGenerate",0},
+      {'a',"all-versions","APT::Cache::AllVersions",0},
       {0,"names-only","APT::Cache::NamesOnly",0},
+      {0,"all-names","APT::Cache::AllNames",0},
       {'c',"config-file",0,CommandLine::ConfigFile},
       {'o',"option",0,CommandLine::ArbItem},
       {0,0,0,0}};
    CommandLine::Dispatch CmdsA[] = {{"help",&ShowHelp},
                                     {"add",&DoAdd},
                                     {"gencaches",&GenCaches},
+                                    {"showsrc",&ShowSrcPackage},
                                     {0,0}};
    CommandLine::Dispatch CmdsB[] = {{"showpkg",&DumpPackage},
                                     {"stats",&Stats},
@@ -656,7 +974,10 @@ int main(int argc,const char *argv[])
                                     {"unmet",&UnMet},
                                     {"check",&Check},
                                     {"search",&Search},
+                                    {"depends",&Depends},
+                                    {"dotty",&Dotty},
                                     {"show",&ShowPackage},
+                                    {"pkgnames",&ShowPkgNames},
                                     {0,0}};
 
    CacheInitialize();
@@ -675,18 +996,37 @@ int main(int argc,const char *argv[])
        CmdL.FileSize() == 0)
       return ShowHelp(CmdL);
 
+   // Deal with stdout not being a tty
+   if (ttyname(STDOUT_FILENO) == 0 && _config->FindI("quiet",0) < 1)
+      _config->Set("quiet","1");
+
    if (CmdL.DispatchArg(CmdsA,false) == false && _error->PendingError() == false)
-   {      
-      // Open the cache file
-      FileFd CacheF(_config->FindFile("Dir::Cache::pkgcache"),FileFd::ReadOnly);
-      MMap Map(CacheF,MMap::Public | MMap::ReadOnly);
+   { 
+      MMap *Map;
+      if (_config->FindB("APT::Cache::NoGenerate",false) == true)
+      {
+        Map = new MMap(*new FileFd(_config->FindFile("Dir::Cache::pkgcache"),
+                                   FileFd::ReadOnly),MMap::Public|MMap::ReadOnly);
+      }
+      else
+      {
+        // Open the cache file
+        pkgSourceList List;
+        List.ReadMainList();
+
+        // Generate it and map it
+        OpProgress Prog;
+        Map = pkgMakeStatusCacheMem(List,Prog);
+      }
+      
       if (_error->PendingError() == false)
       {
-        pkgCache Cache(Map);   
+        pkgCache Cache(*Map);   
         GCache = &Cache;
         if (_error->PendingError() == false)
            CmdL.DispatchArg(CmdsB);
-      }      
+      }
+      delete Map;
    }
    
    // Print any errors or warnings found during parsing