]> git.saurik.com Git - apt.git/blob - cmdline/apt-config.cc
e0383e0197978aadf6e3542aef659ac7a7cdeadb
[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 <string.h>
33
34 #include <apt-private/private-cmndline.h>
35
36 #include <apti18n.h>
37 /*}}}*/
38 using namespace std;
39
40 // DoShell - Handle the shell command /*{{{*/
41 // ---------------------------------------------------------------------
42 /* */
43 static bool DoShell(CommandLine &CmdL)
44 {
45 for (const char **I = CmdL.FileList + 1; *I != 0; I += 2)
46 {
47 if (I[1] == 0 || strlen(I[1]) == 0)
48 return _error->Error(_("Arguments not in pairs"));
49
50 string key = I[1];
51 if (key.end()[-1] == '/') // old directory format
52 key.append("d");
53
54 if (_config->ExistsAny(key.c_str()))
55 cout << *I << "='" <<
56 SubstVar(_config->FindAny(key.c_str()),"'","'\\''") << '\'' << endl;
57
58 }
59
60 return true;
61 }
62 /*}}}*/
63 // DoDump - Dump the configuration space /*{{{*/
64 // ---------------------------------------------------------------------
65 /* */
66 static bool DoDump(CommandLine &CmdL)
67 {
68 bool const empty = _config->FindB("APT::Config::Dump::EmptyValue", true);
69 std::string const format = _config->Find("APT::Config::Dump::Format", "%f \"%v\";\n");
70 if (CmdL.FileSize() == 1)
71 _config->Dump(cout, NULL, format.c_str(), empty);
72 else
73 for (const char **I = CmdL.FileList + 1; *I != 0; ++I)
74 _config->Dump(cout, *I, format.c_str(), empty);
75 return true;
76 }
77 /*}}}*/
78 // ShowHelp - Show the help screen /*{{{*/
79 // ---------------------------------------------------------------------
80 /* */
81 static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
82 {
83 ioprintf(cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
84 if (_config->FindB("version") == true)
85 return true;
86
87 std::cout <<
88 _("Usage: apt-config [options] command\n"
89 "\n"
90 "apt-config is a simple tool to read the APT config file\n")
91 << std::endl
92 << _("Commands:") << std::endl;
93 for (; Cmds->Handler != nullptr; ++Cmds)
94 {
95 if (Cmds->Help == nullptr)
96 continue;
97 std::cout << " " << Cmds->Match << " - " << Cmds->Help << std::endl;
98 }
99
100 std::cout << std::endl <<
101 _("Options:\n"
102 " -h This help text.\n"
103 " -c=? Read this configuration file\n"
104 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
105 return true;
106 }
107 /*}}}*/
108 int main(int argc,const char *argv[]) /*{{{*/
109 {
110 CommandLine::DispatchWithHelp Cmds[] = {
111 {"shell", &DoShell, _("get configuration values via shell evaluation")},
112 {"dump", &DoDump, _("show the active configuration setting")},
113 {nullptr, nullptr, nullptr}
114 };
115
116 std::vector<CommandLine::Args> Args = getCommandArgs("apt-config", CommandLine::GetCommand(Cmds, argc, argv));
117
118 // Set up gettext support
119 setlocale(LC_ALL,"");
120 textdomain(PACKAGE);
121
122 // Parse the command line and initialize the package library
123 CommandLine CmdL;
124 ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
125
126 std::vector<std::string> const langs = APT::Configuration::getLanguages(true);
127 _config->Clear("Acquire::Languages");
128 for (std::vector<std::string>::const_iterator l = langs.begin(); l != langs.end(); ++l)
129 _config->Set("Acquire::Languages::", *l);
130
131 std::vector<std::string> const archs = APT::Configuration::getArchitectures();
132 _config->Clear("APT::Architectures");
133 for (std::vector<std::string>::const_iterator a = archs.begin(); a != archs.end(); ++a)
134 _config->Set("APT::Architectures::", *a);
135
136 std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors();
137 _config->Clear("APT::Compressor");
138 string conf = "APT::Compressor::";
139 for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressors.begin(); c != compressors.end(); ++c)
140 {
141 string comp = conf + c->Name + "::";
142 _config->Set(comp + "Name", c->Name);
143 _config->Set(comp + "Extension", c->Extension);
144 _config->Set(comp + "Binary", c->Binary);
145 _config->Set(std::string(comp + "Cost").c_str(), c->Cost);
146 for (std::vector<std::string>::const_iterator a = c->CompressArgs.begin(); a != c->CompressArgs.end(); ++a)
147 _config->Set(comp + "CompressArg::", *a);
148 for (std::vector<std::string>::const_iterator a = c->UncompressArgs.begin(); a != c->UncompressArgs.end(); ++a)
149 _config->Set(comp + "UncompressArg::", *a);
150 }
151
152 std::vector<std::string> const profiles = APT::Configuration::getBuildProfiles();
153 _config->Clear("APT::Build-Profiles");
154 for (std::vector<std::string>::const_iterator p = profiles.begin(); p != profiles.end(); ++p)
155 _config->Set("APT::Build-Profiles::", *p);
156
157 // Match the operation
158 CmdL.DispatchArg(Cmds);
159
160 // Print any errors or warnings found during parsing
161 if (_error->empty() == false)
162 {
163 bool Errors = _error->PendingError();
164 _error->DumpErrors();
165 return Errors == true?100:0;
166 }
167
168 return 0;
169 }
170 /*}}}*/