apt-config.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: apt-config.cc,v 1.2 1998/11/27 01:52:57 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 "config.h"
  19. #include <iostream>
  20. /*}}}*/
  21. // DoShell - Handle the shell command /*{{{*/
  22. // ---------------------------------------------------------------------
  23. /* */
  24. bool DoShell(CommandLine &CmdL)
  25. {
  26. for (const char **I = CmdL.FileList + 1; *I != 0; I += 2)
  27. {
  28. if (I[1] == 0)
  29. return _error->Error("Arguments not in pairs");
  30. if (_config->Exists(I[1]) == true)
  31. cout << *I << "=\"" << _config->Find(I[1]) << '"' << endl;
  32. }
  33. return true;
  34. }
  35. /*}}}*/
  36. // ShowHelp - Show the help screen /*{{{*/
  37. // ---------------------------------------------------------------------
  38. /* */
  39. int ShowHelp()
  40. {
  41. cout << PACKAGE << ' ' << VERSION << " for " << ARCHITECTURE <<
  42. " compiled on " << __DATE__ << " " << __TIME__ << endl;
  43. cout << "Usage: apt-config [options] command" << endl;
  44. cout << endl;
  45. cout << "apt-config is a simple tool to read the APT config file" << endl;
  46. cout << endl;
  47. cout << "Commands:" << endl;
  48. cout << " shell - Shell mode" << endl;
  49. cout << endl;
  50. cout << "Options:" << endl;
  51. cout << " -h This help text." << endl;
  52. cout << " -c=? Read this configuration file" << endl;
  53. cout << " -o=? Set an arbitary configuration option, ie -o dir::cache=/tmp" << endl;
  54. return 100;
  55. }
  56. /*}}}*/
  57. int main(int argc,const char *argv[])
  58. {
  59. CommandLine::Args Args[] = {
  60. {'h',"help","help",0},
  61. {'c',"config-file",0,CommandLine::ConfigFile},
  62. {'o',"option",0,CommandLine::ArbItem},
  63. {0,0,0,0}};
  64. CommandLine::Dispatch Cmds[] = {{"shell",&DoShell},
  65. {0,0}};
  66. // Parse the command line and initialize the package library
  67. CommandLine CmdL(Args,_config);
  68. if (pkgInitialize(*_config) == false ||
  69. CmdL.Parse(argc,argv) == false)
  70. {
  71. _error->DumpErrors();
  72. return 100;
  73. }
  74. // See if the help should be shown
  75. if (_config->FindB("help") == true ||
  76. CmdL.FileSize() == 0)
  77. return ShowHelp();
  78. // Match the operation
  79. CmdL.DispatchArg(Cmds);
  80. // Print any errors or warnings found during parsing
  81. if (_error->empty() == false)
  82. {
  83. bool Errors = _error->PendingError();
  84. _error->DumpErrors();
  85. return Errors == true?100:0;
  86. }
  87. return 0;
  88. }