apt-config.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: apt-config.cc,v 1.6 1999/06/06 05:52:37 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. // DoDump - Dump the configuration space /*{{{*/
  46. // ---------------------------------------------------------------------
  47. /* */
  48. bool DoDump(CommandLine &CmdL)
  49. {
  50. _config->Dump();
  51. return true;
  52. }
  53. /*}}}*/
  54. // ShowHelp - Show the help screen /*{{{*/
  55. // ---------------------------------------------------------------------
  56. /* */
  57. int ShowHelp()
  58. {
  59. cout << PACKAGE << ' ' << VERSION << " for " << ARCHITECTURE <<
  60. " compiled on " << __DATE__ << " " << __TIME__ << endl;
  61. if (_config->FindB("version") == true)
  62. return 100;
  63. cout << "Usage: apt-config [options] command" << endl;
  64. cout << endl;
  65. cout << "apt-config is a simple tool to read the APT config file" << endl;
  66. cout << endl;
  67. cout << "Commands:" << endl;
  68. cout << " shell - Shell mode" << endl;
  69. cout << " dump - Show the configuration" << endl;
  70. cout << endl;
  71. cout << "Options:" << endl;
  72. cout << " -h This help text." << endl;
  73. cout << " -c=? Read this configuration file" << endl;
  74. cout << " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp" << endl;
  75. return 100;
  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. // Parse the command line and initialize the package library
  90. CommandLine CmdL(Args,_config);
  91. if (pkgInitialize(*_config) == false ||
  92. CmdL.Parse(argc,argv) == 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. }