apt-config.cc 3.4 KB

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