apt-config.cc 3.6 KB

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