You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

136 lines
3.7 KiB

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