apt-config.cc 5.6 KB

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