]> git.saurik.com Git - apt.git/blobdiff - apt-private/private-output.cc
apt-private/private-output.cc: fix cppcheck output
[apt.git] / apt-private / private-output.cc
index e9b8037daa7d718747945af7ce29e57a0165c260..8f190a5516f2ab24833d0f12064fe5e5b2d82611 100644 (file)
@@ -7,15 +7,23 @@
 #include <apt-pkg/cachefile.h>
 #include <apt-pkg/pkgrecords.h>
 #include <apt-pkg/policy.h>
+#include <apt-pkg/depcache.h>
+#include <apt-pkg/pkgcache.h>
+#include <apt-pkg/cacheiterators.h>
 
+#include <apt-private/private-output.h>
+#include <apt-private/private-cachefile.h>
+
+#include <regex.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <iomanip>
 #include <iostream>
-#include <locale.h>
 #include <langinfo.h>
 #include <unistd.h>
-
-#include "private-output.h"
-#include "private-cachefile.h"
+#include <signal.h>
+#include <sys/ioctl.h>
 
 #include <apti18n.h>
                                                                        /*}}}*/
@@ -26,10 +34,29 @@ std::ostream c0out(0);
 std::ostream c1out(0);
 std::ostream c2out(0);
 std::ofstream devnull("/dev/null");
+
+
 unsigned int ScreenWidth = 80 - 1; /* - 1 for the cursor */
 
+// SigWinch - Window size change signal handler                                /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+static void SigWinch(int)
+{
+   // Riped from GNU ls
+#ifdef TIOCGWINSZ
+   struct winsize ws;
+  
+   if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col >= 5)
+      ScreenWidth = ws.ws_col - 1;
+#endif
+}
+                                                                       /*}}}*/
 bool InitOutput()                                                      /*{{{*/
 {
+   if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
+      _config->Set("quiet","1");
+
    c0out.rdbuf(cout.rdbuf());
    c1out.rdbuf(cout.rdbuf());
    c2out.rdbuf(cout.rdbuf());
@@ -38,6 +65,10 @@ bool InitOutput()                                                    /*{{{*/
    if (_config->FindI("quiet",0) > 1)
       c1out.rdbuf(devnull.rdbuf());
 
+   // deal with window size changes
+   signal(SIGWINCH,SigWinch);
+   SigWinch(0);
+
    if(!isatty(1))
    {
       _config->Set("APT::Color", "false");
@@ -60,10 +91,10 @@ bool InitOutput()                                                   /*{{{*/
    return true;
 }
                                                                        /*}}}*/
-std::string GetArchiveSuite(pkgCacheFile &CacheFile, pkgCache::VerIterator ver) /*{{{*/
+static std::string GetArchiveSuite(pkgCacheFile &/*CacheFile*/, pkgCache::VerIterator ver) /*{{{*/
 {
    std::string suite = "";
-   if (ver && ver.FileList() && ver.FileList())
+   if (ver && ver.FileList())
    {
       pkgCache::VerFileIterator VF = ver.FileList();
       for (; VF.end() == false ; ++VF)
@@ -79,7 +110,7 @@ std::string GetArchiveSuite(pkgCacheFile &CacheFile, pkgCache::VerIterator ver)
    return suite;
 }
                                                                        /*}}}*/
-std::string GetFlagsStr(pkgCacheFile &CacheFile, pkgCache::PkgIterator P)/*{{{*/
+static std::string GetFlagsStr(pkgCacheFile &CacheFile, pkgCache::PkgIterator P)/*{{{*/
 {
    pkgDepCache *DepCache = CacheFile.GetDepCache();
    pkgDepCache::StateCache &state = (*DepCache)[P];
@@ -96,7 +127,7 @@ std::string GetFlagsStr(pkgCacheFile &CacheFile, pkgCache::PkgIterator P)/*{{{*/
    return flags_str;
 }
                                                                        /*}}}*/
-std::string GetCandidateVersion(pkgCacheFile &CacheFile, pkgCache::PkgIterator P)/*{{{*/
+static std::string GetCandidateVersion(pkgCacheFile &CacheFile, pkgCache::PkgIterator P)/*{{{*/
 {
    pkgPolicy *policy = CacheFile.GetPolicy();
    pkgCache::VerIterator cand = policy->GetCandidateVer(P);
@@ -104,14 +135,14 @@ std::string GetCandidateVersion(pkgCacheFile &CacheFile, pkgCache::PkgIterator P
    return cand ? cand.VerStr() : "(none)";
 }
                                                                        /*}}}*/
-std::string GetInstalledVersion(pkgCacheFile &CacheFile, pkgCache::PkgIterator P)/*{{{*/
+static std::string GetInstalledVersion(pkgCacheFile &/*CacheFile*/, pkgCache::PkgIterator P)/*{{{*/
 {
    pkgCache::VerIterator inst = P.CurrentVer();
 
    return inst ? inst.VerStr() : "(none)";
 }
                                                                        /*}}}*/
-std::string GetVersion(pkgCacheFile &CacheFile, pkgCache::VerIterator V)/*{{{*/
+static std::string GetVersion(pkgCacheFile &/*CacheFile*/, pkgCache::VerIterator V)/*{{{*/
 {
    pkgCache::PkgIterator P = V.ParentPkg();
    if (V == P.CurrentVer())
@@ -131,16 +162,20 @@ std::string GetVersion(pkgCacheFile &CacheFile, pkgCache::VerIterator V)/*{{{*/
    return "(none)";
 }
                                                                        /*}}}*/
-std::string GetArchitecture(pkgCacheFile &CacheFile, pkgCache::PkgIterator P)/*{{{*/
+static std::string GetArchitecture(pkgCacheFile &CacheFile, pkgCache::PkgIterator P)/*{{{*/
 {
    pkgPolicy *policy = CacheFile.GetPolicy();
    pkgCache::VerIterator inst = P.CurrentVer();
    pkgCache::VerIterator cand = policy->GetCandidateVer(P);
-   
+
+   // this may happen for packages in dpkg "deinstall ok config-file" state
+   if (inst.IsGood() == false && cand.IsGood() == false)
+      return P.VersionList().Arch();
+
    return inst ? inst.Arch() : cand.Arch();
 }
                                                                        /*}}}*/
-std::string GetShortDescription(pkgCacheFile &CacheFile, pkgRecords &records, pkgCache::PkgIterator P)/*{{{*/
+static std::string GetShortDescription(pkgCacheFile &CacheFile, pkgRecords &records, pkgCache::PkgIterator P)/*{{{*/
 {
    pkgPolicy *policy = CacheFile.GetPolicy();
 
@@ -190,45 +225,44 @@ void ListSingleVersion(pkgCacheFile &CacheFile, pkgRecords &records,      /*{{{*/
       // raring/linux-kernel version [upradable: new-version]
       //    description
       pkgPolicy *policy = CacheFile.GetPolicy();
-      out << std::setiosflags(std::ios::left)
-          << _config->Find("APT::Color::Highlight", "")
-          << name_str 
-          << _config->Find("APT::Color::Neutral", "")
-          << "/" << suite
-          << " ";
+      std::string VersionStr = GetVersion(CacheFile, V);
+      std::string CandidateVerStr = GetCandidateVersion(CacheFile, P);
+      std::string InstalledVerStr = GetInstalledVersion(CacheFile, P);
+      std::string StatusStr;
       if(P.CurrentVer() == V && state.Upgradable()) {
-         out << GetVersion(CacheFile, V)
-             << " "
-             << "[" << _("installed,upgradable to: ")
-             << GetCandidateVersion(CacheFile, P) << "]";
+         strprintf(StatusStr, _("[installed,upgradable to: %s]"),
+                   CandidateVerStr.c_str());
       } else if (P.CurrentVer() == V) {
-         out << GetVersion(CacheFile, V)
-             << " ";
          if(!V.Downloadable())
-            out << _("[installed,local]");
+            StatusStr = _("[installed,local]");
          else
             if(V.Automatic() && state.Garbage)
-                  out << _("[installed,auto-removable]");
+               StatusStr = _("[installed,auto-removable]");
             else if (state.Flags & pkgCache::Flag::Auto)
-               out << _("[installed,automatic]");
+               StatusStr = _("[installed,automatic]");
             else
-               out << _("[installed]");
+               StatusStr = _("[installed]");
       } else if (P.CurrentVer() && 
                  policy->GetCandidateVer(P) == V && 
                  state.Upgradable()) {
-         out << GetVersion(CacheFile, V)
-             << " "
-             << _("[upgradable from: ")
-             << GetInstalledVersion(CacheFile, P) << "]";
+            strprintf(StatusStr, _("[upgradable from: %s]"),
+                      InstalledVerStr.c_str());
       } else {
          if (V.ParentPkg()->CurrentState == pkgCache::State::ConfigFiles)
-            out << GetVersion(CacheFile, V) 
-                << " "
-                << _("[residual-config]");
+            StatusStr = _("[residual-config]");
          else
-            out << GetVersion(CacheFile, V);
+            StatusStr = "";
       }
-      out << " " << GetArchitecture(CacheFile, P);
+      out << std::setiosflags(std::ios::left)
+          << _config->Find("APT::Color::Highlight", "")
+          << name_str 
+          << _config->Find("APT::Color::Neutral", "")
+          << "/" << suite
+          << " "
+          << VersionStr << " " 
+          << GetArchitecture(CacheFile, P);
+      if (StatusStr != "") 
+         out << " " << StatusStr;
       if (include_summary)
       {
          out << std::endl