apt-config.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 <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. InitLocale();
  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. std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors();
  100. _config->Clear("APT::Compressor");
  101. string conf = "APT::Compressor::";
  102. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressors.begin(); c != compressors.end(); ++c)
  103. {
  104. string comp = conf + c->Name + "::";
  105. _config->Set(comp + "Name", c->Name);
  106. _config->Set(comp + "Extension", c->Extension);
  107. _config->Set(comp + "Binary", c->Binary);
  108. _config->Set(std::string(comp + "Cost").c_str(), c->Cost);
  109. for (std::vector<std::string>::const_iterator a = c->CompressArgs.begin(); a != c->CompressArgs.end(); ++a)
  110. _config->Set(comp + "CompressArg::", *a);
  111. for (std::vector<std::string>::const_iterator a = c->UncompressArgs.begin(); a != c->UncompressArgs.end(); ++a)
  112. _config->Set(comp + "UncompressArg::", *a);
  113. }
  114. std::vector<std::string> const profiles = APT::Configuration::getBuildProfiles();
  115. _config->Clear("APT::Build-Profiles");
  116. for (std::vector<std::string>::const_iterator p = profiles.begin(); p != profiles.end(); ++p)
  117. _config->Set("APT::Build-Profiles::", *p);
  118. return DispatchCommandLine(CmdL, Cmds);
  119. }
  120. /*}}}*/