apt-config.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 <apt-pkg/cmndline.h>
  16. #include <apt-pkg/error.h>
  17. #include <apt-pkg/init.h>
  18. #include <apt-pkg/strutl.h>
  19. #include <apt-pkg/configuration.h>
  20. #include <apt-pkg/aptconfiguration.h>
  21. #include <config.h>
  22. #include <apti18n.h>
  23. #include <locale.h>
  24. #include <iostream>
  25. #include <string>
  26. #include <vector>
  27. /*}}}*/
  28. using namespace std;
  29. // DoShell - Handle the shell command /*{{{*/
  30. // ---------------------------------------------------------------------
  31. /* */
  32. bool DoShell(CommandLine &CmdL)
  33. {
  34. for (const char **I = CmdL.FileList + 1; *I != 0; I += 2)
  35. {
  36. if (I[1] == 0 || strlen(I[1]) == 0)
  37. return _error->Error(_("Arguments not in pairs"));
  38. string key = I[1];
  39. if (key.end()[-1] == '/') // old directory format
  40. key.append("d");
  41. if (_config->ExistsAny(key.c_str()))
  42. cout << *I << "='" <<
  43. SubstVar(_config->FindAny(key.c_str()),"'","'\\''") << '\'' << endl;
  44. }
  45. return true;
  46. }
  47. /*}}}*/
  48. // DoDump - Dump the configuration space /*{{{*/
  49. // ---------------------------------------------------------------------
  50. /* */
  51. bool DoDump(CommandLine &CmdL)
  52. {
  53. _config->Dump(cout);
  54. return true;
  55. }
  56. /*}}}*/
  57. // ShowHelp - Show the help screen /*{{{*/
  58. // ---------------------------------------------------------------------
  59. /* */
  60. int ShowHelp()
  61. {
  62. ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
  63. COMMON_ARCH,__DATE__,__TIME__);
  64. if (_config->FindB("version") == true)
  65. return 0;
  66. cout <<
  67. _("Usage: apt-config [options] command\n"
  68. "\n"
  69. "apt-config is a simple tool to read the APT config file\n"
  70. "\n"
  71. "Commands:\n"
  72. " shell - Shell mode\n"
  73. " dump - Show the configuration\n"
  74. "\n"
  75. "Options:\n"
  76. " -h This help text.\n"
  77. " -c=? Read this configuration file\n"
  78. " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
  79. return 0;
  80. }
  81. /*}}}*/
  82. int main(int argc,const char *argv[]) /*{{{*/
  83. {
  84. CommandLine::Args Args[] = {
  85. {'h',"help","help",0},
  86. {'v',"version","version",0},
  87. {'c',"config-file",0,CommandLine::ConfigFile},
  88. {'o',"option",0,CommandLine::ArbItem},
  89. {0,0,0,0}};
  90. CommandLine::Dispatch Cmds[] = {{"shell",&DoShell},
  91. {"dump",&DoDump},
  92. {0,0}};
  93. // Set up gettext support
  94. setlocale(LC_ALL,"");
  95. textdomain(PACKAGE);
  96. // Parse the command line and initialize the package library
  97. CommandLine CmdL(Args,_config);
  98. if (pkgInitConfig(*_config) == false ||
  99. CmdL.Parse(argc,argv) == false ||
  100. pkgInitSystem(*_config,_system) == false)
  101. {
  102. _error->DumpErrors();
  103. return 100;
  104. }
  105. // See if the help should be shown
  106. if (_config->FindB("help") == true ||
  107. CmdL.FileSize() == 0)
  108. return ShowHelp();
  109. std::vector<std::string> const langs = APT::Configuration::getLanguages(true);
  110. _config->Clear("Acquire::Languages");
  111. for (std::vector<std::string>::const_iterator l = langs.begin(); l != langs.end(); ++l)
  112. _config->Set("Acquire::Languages::", *l);
  113. std::vector<std::string> const archs = APT::Configuration::getArchitectures();
  114. _config->Clear("APT::Architectures");
  115. for (std::vector<std::string>::const_iterator a = archs.begin(); a != archs.end(); ++a)
  116. _config->Set("APT::Architectures::", *a);
  117. // Match the operation
  118. CmdL.DispatchArg(Cmds);
  119. // Print any errors or warnings found during parsing
  120. if (_error->empty() == false)
  121. {
  122. bool Errors = _error->PendingError();
  123. _error->DumpErrors();
  124. return Errors == true?100:0;
  125. }
  126. return 0;
  127. }
  128. /*}}}*/