apt-config.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 <apti18n.h>
  29. /*}}}*/
  30. using namespace std;
  31. // DoShell - Handle the shell command /*{{{*/
  32. // ---------------------------------------------------------------------
  33. /* */
  34. static bool DoShell(CommandLine &CmdL)
  35. {
  36. for (const char **I = CmdL.FileList + 1; *I != 0; I += 2)
  37. {
  38. if (I[1] == 0 || strlen(I[1]) == 0)
  39. return _error->Error(_("Arguments not in pairs"));
  40. string key = I[1];
  41. if (key.end()[-1] == '/') // old directory format
  42. key.append("d");
  43. if (_config->ExistsAny(key.c_str()))
  44. cout << *I << "='" <<
  45. SubstVar(_config->FindAny(key.c_str()),"'","'\\''") << '\'' << endl;
  46. }
  47. return true;
  48. }
  49. /*}}}*/
  50. // DoDump - Dump the configuration space /*{{{*/
  51. // ---------------------------------------------------------------------
  52. /* */
  53. static bool DoDump(CommandLine &CmdL)
  54. {
  55. bool const empty = _config->FindB("APT::Config::Dump::EmptyValue", true);
  56. std::string const format = _config->Find("APT::Config::Dump::Format", "%f \"%v\";\n");
  57. if (CmdL.FileSize() == 1)
  58. _config->Dump(cout, NULL, format.c_str(), empty);
  59. else
  60. for (const char **I = CmdL.FileList + 1; *I != 0; ++I)
  61. _config->Dump(cout, *I, format.c_str(), empty);
  62. return true;
  63. }
  64. /*}}}*/
  65. // ShowHelp - Show the help screen /*{{{*/
  66. // ---------------------------------------------------------------------
  67. /* */
  68. static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
  69. {
  70. ioprintf(cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
  71. if (_config->FindB("version") == true)
  72. return true;
  73. std::cout <<
  74. _("Usage: apt-config [options] command\n"
  75. "\n"
  76. "apt-config is a simple tool to read the APT config file\n")
  77. << std::endl
  78. << _("Commands:") << std::endl;
  79. for (; Cmds->Handler != nullptr; ++Cmds)
  80. {
  81. if (Cmds->Help == nullptr)
  82. continue;
  83. std::cout << " " << Cmds->Match << " - " << Cmds->Help << std::endl;
  84. }
  85. std::cout << std::endl <<
  86. _("Options:\n"
  87. " -h This help text.\n"
  88. " -c=? Read this configuration file\n"
  89. " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
  90. return true;
  91. }
  92. /*}}}*/
  93. int main(int argc,const char *argv[]) /*{{{*/
  94. {
  95. CommandLine::DispatchWithHelp Cmds[] = {
  96. {"shell", &DoShell, _("get configuration values via shell evaluation")},
  97. {"dump", &DoDump, _("show the active configuration setting")},
  98. {nullptr, nullptr, nullptr}
  99. };
  100. std::vector<CommandLine::Args> Args = getCommandArgs("apt-config", CommandLine::GetCommand(Cmds, argc, argv));
  101. // Set up gettext support
  102. setlocale(LC_ALL,"");
  103. textdomain(PACKAGE);
  104. // Parse the command line and initialize the package library
  105. CommandLine CmdL;
  106. ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
  107. std::vector<std::string> const langs = APT::Configuration::getLanguages(true);
  108. _config->Clear("Acquire::Languages");
  109. for (std::vector<std::string>::const_iterator l = langs.begin(); l != langs.end(); ++l)
  110. _config->Set("Acquire::Languages::", *l);
  111. std::vector<std::string> const archs = APT::Configuration::getArchitectures();
  112. _config->Clear("APT::Architectures");
  113. for (std::vector<std::string>::const_iterator a = archs.begin(); a != archs.end(); ++a)
  114. _config->Set("APT::Architectures::", *a);
  115. std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors();
  116. _config->Clear("APT::Compressor");
  117. string conf = "APT::Compressor::";
  118. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressors.begin(); c != compressors.end(); ++c)
  119. {
  120. string comp = conf + c->Name + "::";
  121. _config->Set(comp + "Name", c->Name);
  122. _config->Set(comp + "Extension", c->Extension);
  123. _config->Set(comp + "Binary", c->Binary);
  124. _config->Set(std::string(comp + "Cost").c_str(), c->Cost);
  125. for (std::vector<std::string>::const_iterator a = c->CompressArgs.begin(); a != c->CompressArgs.end(); ++a)
  126. _config->Set(comp + "CompressArg::", *a);
  127. for (std::vector<std::string>::const_iterator a = c->UncompressArgs.begin(); a != c->UncompressArgs.end(); ++a)
  128. _config->Set(comp + "UncompressArg::", *a);
  129. }
  130. std::vector<std::string> const profiles = APT::Configuration::getBuildProfiles();
  131. _config->Clear("APT::Build-Profiles");
  132. for (std::vector<std::string>::const_iterator p = profiles.begin(); p != profiles.end(); ++p)
  133. _config->Set("APT::Build-Profiles::", *p);
  134. // Match the operation
  135. CmdL.DispatchArg(Cmds);
  136. // Print any errors or warnings found during parsing
  137. if (_error->empty() == false)
  138. {
  139. bool Errors = _error->PendingError();
  140. _error->DumpErrors();
  141. return Errors == true?100:0;
  142. }
  143. return 0;
  144. }
  145. /*}}}*/