apt  2.2.4
About: Apt (Advanced Package Tool) is a management system for software packages (Debian/Ubuntu). Release series 2.2.
  Fossies Dox: apt-2.2.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

apt-config.cc
Go to the documentation of this file.
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4 
5  APT Config - Program to manipulate APT configuration files
6 
7  This program will parse a config file and then do something with it.
8 
9  Commands:
10  shell - Shell mode. After this a series of word pairs should occur.
11  The first is the environment var to set and the second is
12  the key to set it from. Use like:
13  eval `apt-config shell QMode apt::QMode`
14 
15  ##################################################################### */
16  /*}}}*/
17 // Include Files /*{{{*/
18 #include <config.h>
19 
21 #include <apt-pkg/cmndline.h>
22 #include <apt-pkg/configuration.h>
23 #include <apt-pkg/error.h>
24 #include <apt-pkg/init.h>
25 #include <apt-pkg/pkgsystem.h>
26 #include <apt-pkg/strutl.h>
27 
28 #include <iostream>
29 #include <map>
30 #include <string>
31 #include <vector>
32 #include <string.h>
33 
36 
37 #include <apti18n.h>
38  /*}}}*/
39 using namespace std;
40 
41 // DoShell - Handle the shell command /*{{{*/
42 // ---------------------------------------------------------------------
43 /* */
44 static bool DoShell(CommandLine &CmdL)
45 {
46  for (const char **I = CmdL.FileList + 1; *I != 0; I += 2)
47  {
48  if (I[1] == 0 || strlen(I[1]) == 0)
49  return _error->Error(_("Arguments not in pairs"));
50 
51  string key = I[1];
52  if (key.end()[-1] == '/') // old directory format
53  key.append("d");
54 
55  if (_config->ExistsAny(key.c_str()))
56  cout << *I << "='" <<
57  SubstVar(_config->FindAny(key.c_str()),"'","'\\''") << '\'' << endl;
58 
59  }
60 
61  return true;
62 }
63  /*}}}*/
64 // DoDump - Dump the configuration space /*{{{*/
65 // ---------------------------------------------------------------------
66 /* */
67 static bool DoDump(CommandLine &CmdL)
68 {
69  bool const empty = _config->FindB("APT::Config::Dump::EmptyValue", true);
70  std::string const format = _config->Find("APT::Config::Dump::Format", "%F \"%v\";\n");
71  if (CmdL.FileSize() == 1)
72  _config->Dump(cout, NULL, format.c_str(), empty);
73  else
74  for (const char **I = CmdL.FileList + 1; *I != 0; ++I)
75  _config->Dump(cout, *I, format.c_str(), empty);
76  return true;
77 }
78  /*}}}*/
79 static bool ShowHelp(CommandLine &) /*{{{*/
80 {
81  std::cout <<
82  _("Usage: apt-config [options] command\n"
83  "\n"
84  "apt-config is an interface to the configuration settings used by\n"
85  "all APT tools, mainly intended for debugging and shell scripting.\n");
86  return true;
87 }
88  /*}}}*/
89 static std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
90 {
91  return {
92  {"shell", &DoShell, _("get configuration values via shell evaluation")},
93  {"dump", &DoDump, _("show the active configuration setting")},
94  {nullptr, nullptr, nullptr}
95  };
96 }
97  /*}}}*/
98 int main(int argc,const char *argv[]) /*{{{*/
99 {
100  // Parse the command line and initialize the package library
101  CommandLine CmdL;
102  auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_CONFIG, &_config, &_system, argc, argv, &ShowHelp, &GetCommands);
103 
104  std::vector<std::string> const langs = APT::Configuration::getLanguages(true);
105  _config->Clear("Acquire::Languages");
106  for (std::vector<std::string>::const_iterator l = langs.begin(); l != langs.end(); ++l)
107  _config->Set("Acquire::Languages::", *l);
108 
109  std::vector<std::string> const archs = APT::Configuration::getArchitectures();
110  _config->Clear("APT::Architectures");
111  for (std::vector<std::string>::const_iterator a = archs.begin(); a != archs.end(); ++a)
112  _config->Set("APT::Architectures::", *a);
113 
114  string const conf = "APT::Compressor::";
115  std::map<std::string,std::string> CompressorNames;
116  for (auto && key : _config->FindVector("APT::Compressor", "", true))
117  {
118  auto const comp = conf + key + "::Name";
119  CompressorNames.emplace(_config->Find(comp, key), key);
120  }
121  std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors();
122  _config->Clear("APT::Compressor");
123  for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressors.begin(); c != compressors.end(); ++c)
124  {
125  auto const n = CompressorNames.find(c->Name);
126  string comp = conf + (n == CompressorNames.end() ? c->Name : n->second) + "::";
127  _config->Set(comp + "Name", c->Name);
128  _config->Set(comp + "Extension", c->Extension);
129  _config->Set(comp + "Binary", c->Binary);
130  _config->Set((comp + "Cost").c_str(), c->Cost);
131  for (std::vector<std::string>::const_iterator a = c->CompressArgs.begin(); a != c->CompressArgs.end(); ++a)
132  _config->Set(comp + "CompressArg::", *a);
133  for (std::vector<std::string>::const_iterator a = c->UncompressArgs.begin(); a != c->UncompressArgs.end(); ++a)
134  _config->Set(comp + "UncompressArg::", *a);
135  }
136 
137  std::vector<std::string> const profiles = APT::Configuration::getBuildProfiles();
138  _config->Clear("APT::Build-Profiles");
139  for (std::vector<std::string>::const_iterator p = profiles.begin(); p != profiles.end(); ++p)
140  _config->Set("APT::Build-Profiles::", *p);
141 
142  return DispatchCommandLine(CmdL, Cmds);
143 }
144  /*}}}*/
static bool std::string const metaIndex const *const pkgAcqMetaClearSig *const pkgAcquire::Item *const I
static bool ShowHelp(CommandLine &)
Definition: apt-config.cc:79
static std::vector< aptDispatchWithHelp > GetCommands()
Definition: apt-config.cc:89
static bool DoDump(CommandLine &CmdL)
Definition: apt-config.cc:67
static bool DoShell(CommandLine &CmdL)
Definition: apt-config.cc:44
int main(int argc, const char *argv[])
Definition: apt-config.cc:98
const char ** FileList
Definition: cmndline.h:78
unsigned int FileSize() const APT_PURE
Definition: cmndline.cc:353
Configuration * _config
APT_PUBLIC std::vector< std::string > const getArchitectures(bool const &Cached=true)
Returns a vector of Architectures we support.
APT_PUBLIC std::vector< Compressor > const getCompressors(bool const Cached=true)
Return a vector of Compressors supported for data.tar's.
APT_PUBLIC std::vector< std::string > const getLanguages(bool const &All=false, bool const &Cached=true, char const **const Locale=0)
Returns a vector of Language Codes.
APT_PUBLIC std::vector< std::string > const getBuildProfiles()
pkgSystem * _system
Definition: pkgsystem.cc:24
std::vector< CommandLine::Dispatch > ParseCommandLine(CommandLine &CmdL, APT_CMD const Binary, Configuration *const *const Cnf, pkgSystem **const Sys, int const argc, const char *argv[], bool(*ShowHelp)(CommandLine &), std::vector< aptDispatchWithHelp >(*GetCommands)(void))
unsigned short DispatchCommandLine(CommandLine &CmdL, std::vector< CommandLine::Dispatch > const &Cmds)
string SubstVar(const string &Str, const string &Subst, const string &Contents)
Definition: strutl.cc:502