apt-config.cc 3.7 KB

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