apt-config.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 occure.
  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 <apti18n.h>
  28. /*}}}*/
  29. using namespace std;
  30. // DoShell - Handle the shell command /*{{{*/
  31. // ---------------------------------------------------------------------
  32. /* */
  33. bool DoShell(CommandLine &CmdL)
  34. {
  35. for (const char **I = CmdL.FileList + 1; *I != 0; I += 2)
  36. {
  37. if (I[1] == 0 || strlen(I[1]) == 0)
  38. return _error->Error(_("Arguments not in pairs"));
  39. string key = I[1];
  40. if (key.end()[-1] == '/') // old directory format
  41. key.append("d");
  42. if (_config->ExistsAny(key.c_str()))
  43. cout << *I << "='" <<
  44. SubstVar(_config->FindAny(key.c_str()),"'","'\\''") << '\'' << endl;
  45. }
  46. return true;
  47. }
  48. /*}}}*/
  49. // DoDump - Dump the configuration space /*{{{*/
  50. // ---------------------------------------------------------------------
  51. /* */
  52. bool DoDump(CommandLine &CmdL)
  53. {
  54. bool const empty = _config->FindB("APT::Config::Dump::EmptyValue", true);
  55. std::string const format = _config->Find("APT::Config::Dump::Format", "%f \"%v\";\n");
  56. if (CmdL.FileSize() == 1)
  57. _config->Dump(cout, NULL, format.c_str(), empty);
  58. else
  59. for (const char **I = CmdL.FileList + 1; *I != 0; ++I)
  60. _config->Dump(cout, *I, format.c_str(), empty);
  61. return true;
  62. }
  63. /*}}}*/
  64. // ShowHelp - Show the help screen /*{{{*/
  65. // ---------------------------------------------------------------------
  66. /* */
  67. int ShowHelp()
  68. {
  69. ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
  70. COMMON_ARCH,__DATE__,__TIME__);
  71. if (_config->FindB("version") == true)
  72. return 0;
  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 0;
  87. }
  88. /*}}}*/
  89. int main(int argc,const char *argv[]) /*{{{*/
  90. {
  91. CommandLine::Args Args[] = {
  92. {'h',"help","help",0},
  93. {'v',"version","version",0},
  94. {'c',"config-file",0,CommandLine::ConfigFile},
  95. {'o',"option",0,CommandLine::ArbItem},
  96. {0,"empty","APT::Config::Dump::EmptyValue",CommandLine::Boolean},
  97. {0,"format","APT::Config::Dump::Format",CommandLine::HasArg},
  98. {0,0,0,0}};
  99. CommandLine::Dispatch Cmds[] = {{"shell",&DoShell},
  100. {"dump",&DoDump},
  101. {0,0}};
  102. // Set up gettext support
  103. setlocale(LC_ALL,"");
  104. textdomain(PACKAGE);
  105. // Parse the command line and initialize the package library
  106. CommandLine CmdL(Args,_config);
  107. if (pkgInitConfig(*_config) == false ||
  108. CmdL.Parse(argc,argv) == false ||
  109. pkgInitSystem(*_config,_system) == false)
  110. {
  111. _error->DumpErrors();
  112. return 100;
  113. }
  114. // See if the help should be shown
  115. if (_config->FindB("help") == true ||
  116. CmdL.FileSize() == 0)
  117. return ShowHelp();
  118. std::vector<std::string> const langs = APT::Configuration::getLanguages(true);
  119. _config->Clear("Acquire::Languages");
  120. for (std::vector<std::string>::const_iterator l = langs.begin(); l != langs.end(); ++l)
  121. _config->Set("Acquire::Languages::", *l);
  122. std::vector<std::string> const archs = APT::Configuration::getArchitectures();
  123. _config->Clear("APT::Architectures");
  124. for (std::vector<std::string>::const_iterator a = archs.begin(); a != archs.end(); ++a)
  125. _config->Set("APT::Architectures::", *a);
  126. std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors();
  127. _config->Clear("APT::Compressor");
  128. string conf = "APT::Compressor::";
  129. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressors.begin(); c != compressors.end(); ++c)
  130. {
  131. string comp = conf + c->Name + "::";
  132. _config->Set(comp + "Name", c->Name);
  133. _config->Set(comp + "Extension", c->Extension);
  134. _config->Set(comp + "Binary", c->Binary);
  135. _config->Set(std::string(comp + "Cost").c_str(), c->Cost);
  136. for (std::vector<std::string>::const_iterator a = c->CompressArgs.begin(); a != c->CompressArgs.end(); ++a)
  137. _config->Set(comp + "CompressArg::", *a);
  138. for (std::vector<std::string>::const_iterator a = c->UncompressArgs.begin(); a != c->UncompressArgs.end(); ++a)
  139. _config->Set(comp + "UncompressArg::", *a);
  140. }
  141. // Match the operation
  142. CmdL.DispatchArg(Cmds);
  143. // Print any errors or warnings found during parsing
  144. if (_error->empty() == false)
  145. {
  146. bool Errors = _error->PendingError();
  147. _error->DumpErrors();
  148. return Errors == true?100:0;
  149. }
  150. return 0;
  151. }
  152. /*}}}*/