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