]> git.saurik.com Git - apt.git/blob - cmdline/apt-config.cc
CMake: test/libapt: Use a prebuilt GTest library if available
[apt.git] / cmdline / apt-config.cc
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
6 APT Config - Program to manipulate APT configuration files
7
8 This program will parse a config file and then do something with it.
9
10 Commands:
11 shell - Shell mode. After this a series of word pairs should occur.
12 The first is the environment var to set and the second is
13 the key to set it from. Use like:
14 eval `apt-config shell QMode apt::QMode`
15
16 ##################################################################### */
17 /*}}}*/
18 // Include Files /*{{{*/
19 #include<config.h>
20
21 #include <apt-pkg/cmndline.h>
22 #include <apt-pkg/error.h>
23 #include <apt-pkg/init.h>
24 #include <apt-pkg/strutl.h>
25 #include <apt-pkg/configuration.h>
26 #include <apt-pkg/aptconfiguration.h>
27 #include <apt-pkg/pkgsystem.h>
28
29 #include <iostream>
30 #include <string>
31 #include <vector>
32 #include <map>
33 #include <string.h>
34
35 #include <apt-private/private-cmndline.h>
36 #include <apt-private/private-main.h>
37
38 #include <apti18n.h>
39 /*}}}*/
40 using namespace std;
41
42 // DoShell - Handle the shell command /*{{{*/
43 // ---------------------------------------------------------------------
44 /* */
45 static bool DoShell(CommandLine &CmdL)
46 {
47 for (const char **I = CmdL.FileList + 1; *I != 0; I += 2)
48 {
49 if (I[1] == 0 || strlen(I[1]) == 0)
50 return _error->Error(_("Arguments not in pairs"));
51
52 string key = I[1];
53 if (key.end()[-1] == '/') // old directory format
54 key.append("d");
55
56 if (_config->ExistsAny(key.c_str()))
57 cout << *I << "='" <<
58 SubstVar(_config->FindAny(key.c_str()),"'","'\\''") << '\'' << endl;
59
60 }
61
62 return true;
63 }
64 /*}}}*/
65 // DoDump - Dump the configuration space /*{{{*/
66 // ---------------------------------------------------------------------
67 /* */
68 static bool DoDump(CommandLine &CmdL)
69 {
70 bool const empty = _config->FindB("APT::Config::Dump::EmptyValue", true);
71 std::string const format = _config->Find("APT::Config::Dump::Format", "%f \"%v\";\n");
72 if (CmdL.FileSize() == 1)
73 _config->Dump(cout, NULL, format.c_str(), empty);
74 else
75 for (const char **I = CmdL.FileList + 1; *I != 0; ++I)
76 _config->Dump(cout, *I, format.c_str(), empty);
77 return true;
78 }
79 /*}}}*/
80 static bool ShowHelp(CommandLine &) /*{{{*/
81 {
82 std::cout <<
83 _("Usage: apt-config [options] command\n"
84 "\n"
85 "apt-config is an interface to the configuration settings used by\n"
86 "all APT tools, mainly intended for debugging and shell scripting.\n");
87 return true;
88 }
89 /*}}}*/
90 static std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
91 {
92 return {
93 {"shell", &DoShell, _("get configuration values via shell evaluation")},
94 {"dump", &DoDump, _("show the active configuration setting")},
95 {nullptr, nullptr, nullptr}
96 };
97 }
98 /*}}}*/
99 int main(int argc,const char *argv[]) /*{{{*/
100 {
101 // Parse the command line and initialize the package library
102 CommandLine CmdL;
103 auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_CONFIG, &_config, &_system, argc, argv, &ShowHelp, &GetCommands);
104
105 std::vector<std::string> const langs = APT::Configuration::getLanguages(true);
106 _config->Clear("Acquire::Languages");
107 for (std::vector<std::string>::const_iterator l = langs.begin(); l != langs.end(); ++l)
108 _config->Set("Acquire::Languages::", *l);
109
110 std::vector<std::string> const archs = APT::Configuration::getArchitectures();
111 _config->Clear("APT::Architectures");
112 for (std::vector<std::string>::const_iterator a = archs.begin(); a != archs.end(); ++a)
113 _config->Set("APT::Architectures::", *a);
114
115 string const conf = "APT::Compressor::";
116 std::map<std::string,std::string> CompressorNames;
117 for (auto && key : _config->FindVector("APT::Compressor", "", true))
118 {
119 auto const comp = conf + key + "::Name";
120 CompressorNames.emplace(_config->Find(comp, key), key);
121 }
122 std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors();
123 _config->Clear("APT::Compressor");
124 for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressors.begin(); c != compressors.end(); ++c)
125 {
126 auto const n = CompressorNames.find(c->Name);
127 string comp = conf + (n == CompressorNames.end() ? c->Name : n->second) + "::";
128 _config->Set(comp + "Name", c->Name);
129 _config->Set(comp + "Extension", c->Extension);
130 _config->Set(comp + "Binary", c->Binary);
131 _config->Set(std::string(comp + "Cost").c_str(), c->Cost);
132 for (std::vector<std::string>::const_iterator a = c->CompressArgs.begin(); a != c->CompressArgs.end(); ++a)
133 _config->Set(comp + "CompressArg::", *a);
134 for (std::vector<std::string>::const_iterator a = c->UncompressArgs.begin(); a != c->UncompressArgs.end(); ++a)
135 _config->Set(comp + "UncompressArg::", *a);
136 }
137
138 std::vector<std::string> const profiles = APT::Configuration::getBuildProfiles();
139 _config->Clear("APT::Build-Profiles");
140 for (std::vector<std::string>::const_iterator p = profiles.begin(); p != profiles.end(); ++p)
141 _config->Set("APT::Build-Profiles::", *p);
142
143 return DispatchCommandLine(CmdL, Cmds);
144 }
145 /*}}}*/