apt-config.cc 5.2 KB

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