apt-config.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: apt-config.cc,v 1.1 1998/11/22 23:37:07 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. // Parse the command line and initialize the package library
  65. CommandLine CmdL(Args,_config);
  66. if (pkgInitialize(*_config) == false ||
  67. CmdL.Parse(argc,argv) == false)
  68. {
  69. _error->DumpErrors();
  70. return 100;
  71. }
  72. // See if the help should be shown
  73. if (_config->FindB("help") == true ||
  74. CmdL.FileSize() == 0)
  75. return ShowHelp();
  76. // Match the operation
  77. struct
  78. {
  79. const char *Match;
  80. bool (*Handler)(CommandLine &);
  81. } Map[] = {{"shell",&DoShell},
  82. {0,0}};
  83. int I;
  84. for (I = 0; Map[I].Match != 0; I++)
  85. {
  86. if (strcmp(CmdL.FileList[0],Map[I].Match) == 0)
  87. {
  88. if (Map[I].Handler(CmdL) == false && _error->PendingError() == false)
  89. _error->Error("Handler silently failed");
  90. break;
  91. }
  92. }
  93. // No matching name
  94. if (Map[I].Match == 0)
  95. _error->Error("Invalid operation %s", CmdL.FileList[0]);
  96. // Print any errors or warnings found during parsing
  97. if (_error->empty() == false)
  98. {
  99. bool Errors = _error->PendingError();
  100. _error->DumpErrors();
  101. return Errors == true?100:0;
  102. }
  103. return 0;
  104. }