You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

145 lines
5.2 KiB

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