apt-config.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 &)
  69. {
  70. ioprintf(cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
  71. if (_config->FindB("version") == true)
  72. return true;
  73. 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. "\n"
  78. "Commands:\n"
  79. " shell - Shell mode\n"
  80. " dump - Show the configuration\n"
  81. "\n"
  82. "Options:\n"
  83. " -h This help text.\n"
  84. " -c=? Read this configuration file\n"
  85. " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
  86. return true;
  87. }
  88. /*}}}*/
  89. int main(int argc,const char *argv[]) /*{{{*/
  90. {
  91. CommandLine::Dispatch Cmds[] = {{"shell",&DoShell},
  92. {"dump",&DoDump},
  93. {"help",&ShowHelp},
  94. {0,0}};
  95. std::vector<CommandLine::Args> Args = getCommandArgs("apt-config", CommandLine::GetCommand(Cmds, argc, argv));
  96. // Set up gettext support
  97. setlocale(LC_ALL,"");
  98. textdomain(PACKAGE);
  99. // Parse the command line and initialize the package library
  100. CommandLine CmdL;
  101. ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
  102. std::vector<std::string> const langs = APT::Configuration::getLanguages(true);
  103. _config->Clear("Acquire::Languages");
  104. for (std::vector<std::string>::const_iterator l = langs.begin(); l != langs.end(); ++l)
  105. _config->Set("Acquire::Languages::", *l);
  106. std::vector<std::string> const archs = APT::Configuration::getArchitectures();
  107. _config->Clear("APT::Architectures");
  108. for (std::vector<std::string>::const_iterator a = archs.begin(); a != archs.end(); ++a)
  109. _config->Set("APT::Architectures::", *a);
  110. std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors();
  111. _config->Clear("APT::Compressor");
  112. string conf = "APT::Compressor::";
  113. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressors.begin(); c != compressors.end(); ++c)
  114. {
  115. string comp = conf + c->Name + "::";
  116. _config->Set(comp + "Name", c->Name);
  117. _config->Set(comp + "Extension", c->Extension);
  118. _config->Set(comp + "Binary", c->Binary);
  119. _config->Set(std::string(comp + "Cost").c_str(), c->Cost);
  120. for (std::vector<std::string>::const_iterator a = c->CompressArgs.begin(); a != c->CompressArgs.end(); ++a)
  121. _config->Set(comp + "CompressArg::", *a);
  122. for (std::vector<std::string>::const_iterator a = c->UncompressArgs.begin(); a != c->UncompressArgs.end(); ++a)
  123. _config->Set(comp + "UncompressArg::", *a);
  124. }
  125. std::vector<std::string> const profiles = APT::Configuration::getBuildProfiles();
  126. _config->Clear("APT::Build-Profiles");
  127. for (std::vector<std::string>::const_iterator p = profiles.begin(); p != profiles.end(); ++p)
  128. _config->Set("APT::Build-Profiles::", *p);
  129. // Match the operation
  130. CmdL.DispatchArg(Cmds);
  131. // Print any errors or warnings found during parsing
  132. if (_error->empty() == false)
  133. {
  134. bool Errors = _error->PendingError();
  135. _error->DumpErrors();
  136. return Errors == true?100:0;
  137. }
  138. return 0;
  139. }
  140. /*}}}*/