]> git.saurik.com Git - apt.git/blob - apt-pkg/aptconfiguration.cc
9fd51ad5a95b6645ed8826f245d5365abe2d373f
[apt.git] / apt-pkg / aptconfiguration.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4
5 Provide access methods to various configuration settings,
6 setup defaults and returns validate settings.
7
8 ##################################################################### */
9 /*}}}*/
10 // Include Files /*{{{*/
11 #include <apt-pkg/aptconfiguration.h>
12 #include <apt-pkg/configuration.h>
13 #include <apt-pkg/fileutl.h>
14 #include <apt-pkg/macros.h>
15 #include <apt-pkg/strutl.h>
16
17 #include <vector>
18 #include <string>
19 #include <algorithm>
20 /*}}}*/
21 namespace APT {
22 // getCompressionTypes - Return Vector of usbale compressiontypes /*{{{*/
23 // ---------------------------------------------------------------------
24 /* return a vector of compression types in the prefered order. */
25 std::vector<std::string>
26 const Configuration::getCompressionTypes(bool const &Cached) {
27 static std::vector<std::string> types;
28 if (types.empty() == false) {
29 if (Cached == true)
30 return types;
31 else
32 types.clear();
33 }
34
35 // setup the defaults for the compressiontypes => method mapping
36 _config->CndSet("Acquire::CompressionTypes::bz2","bzip2");
37 _config->CndSet("Acquire::CompressionTypes::lzma","lzma");
38 _config->CndSet("Acquire::CompressionTypes::gz","gzip");
39
40 // Set default application paths to check for optional compression types
41 _config->CndSet("Dir::Bin::lzma", "/usr/bin/lzma");
42 _config->CndSet("Dir::Bin::bzip2", "/bin/bzip2");
43
44 // accept non-list order as override setting for config settings on commandline
45 std::string const overrideOrder = _config->Find("Acquire::CompressionTypes::Order","");
46 if (overrideOrder.empty() == false)
47 types.push_back(overrideOrder);
48
49 // load the order setting into our vector
50 std::vector<std::string> const order = _config->FindVector("Acquire::CompressionTypes::Order");
51 for (std::vector<std::string>::const_iterator o = order.begin();
52 o != order.end(); o++) {
53 if ((*o).empty() == true)
54 continue;
55 // ignore types we have no method ready to use
56 if (_config->Exists(string("Acquire::CompressionTypes::").append(*o)) == false)
57 continue;
58 // ignore types we have no app ready to use
59 string const appsetting = string("Dir::Bin::").append(*o);
60 if (_config->Exists(appsetting) == true) {
61 std::string const app = _config->FindFile(appsetting.c_str(), "");
62 if (app.empty() == false && FileExists(app) == false)
63 continue;
64 }
65 types.push_back(*o);
66 }
67
68 // move again over the option tree to add all missing compression types
69 ::Configuration::Item const *Types = _config->Tree("Acquire::CompressionTypes");
70 if (Types != 0)
71 Types = Types->Child;
72
73 for (; Types != 0; Types = Types->Next) {
74 if (Types->Tag == "Order" || Types->Tag.empty() == true)
75 continue;
76 // ignore types we already have in the vector
77 if (std::find(types.begin(),types.end(),Types->Tag) != types.end())
78 continue;
79 // ignore types we have no app ready to use
80 string const appsetting = string("Dir::Bin::").append(Types->Value);
81 if (appsetting.empty() == false && _config->Exists(appsetting) == true) {
82 std::string const app = _config->FindFile(appsetting.c_str(), "");
83 if (app.empty() == false && FileExists(app) == false)
84 continue;
85 }
86 types.push_back(Types->Tag);
87 }
88
89 return types;
90 }
91 /*}}}*/
92 // GetLanguages - Return Vector of Language Codes /*{{{*/
93 // ---------------------------------------------------------------------
94 /* return a vector of language codes in the prefered order.
95 the special word "environment" will be replaced with the long and the short
96 code of the local settings and it will be insured that this will not add
97 duplicates. So in an german local the setting "environment, de_DE, en, de"
98 will result in "de_DE, de, en".
99 The special word "none" is the stopcode for the not-All code vector */
100 std::vector<std::string> const Configuration::getLanguages(bool const &All,
101 bool const &Cached, char const ** const Locale) {
102 using std::string;
103
104 // The detection is boring and has a lot of cornercases,
105 // so we cache the results to calculated it only once.
106 std::vector<string> static allCodes;
107 std::vector<string> static codes;
108
109 // we have something in the cache
110 if (codes.empty() == false || allCodes.empty() == false) {
111 if (Cached == true) {
112 if(All == true && allCodes.empty() == false)
113 return allCodes;
114 else
115 return codes;
116 } else {
117 allCodes.clear();
118 codes.clear();
119 }
120 }
121
122 // get the environment language codes: LC_MESSAGES (and later LANGUAGE)
123 // we extract both, a long and a short code and then we will
124 // check if we actually need both (rare) or if the short is enough
125 string const envMsg = string(Locale == 0 ? std::setlocale(LC_MESSAGES, NULL) : *Locale);
126 size_t const lenShort = (envMsg.find('_') != string::npos) ? envMsg.find('_') : 2;
127 size_t const lenLong = (envMsg.find_first_of(".@") != string::npos) ? envMsg.find_first_of(".@") : (lenShort + 3);
128
129 string envLong = envMsg.substr(0,lenLong);
130 string const envShort = envLong.substr(0,lenShort);
131 bool envLongIncluded = true;
132
133 // first cornercase: LANG=C, so we use only "en" Translation
134 if (envLong == "C") {
135 codes.push_back("en");
136 allCodes = codes;
137 return codes;
138 }
139
140 // to save the servers from unneeded queries, we only try also long codes
141 // for languages it is realistic to have a long code translation fileā€¦
142 // TODO: Improve translation acquire system to drop them dynamic
143 char const *needLong[] = { "cs", "en", "pt", "sv", "zh", NULL };
144 if (envLong != envShort) {
145 for (char const **l = needLong; *l != NULL; l++)
146 if (envShort.compare(*l) == 0) {
147 envLongIncluded = false;
148 break;
149 }
150 }
151
152 // we don't add the long code, but we allow the user to do so
153 if (envLongIncluded == true)
154 envLong.clear();
155
156 // FIXME: Remove support for the old APT::Acquire::Translation
157 // it was undocumented and so it should be not very widthly used
158 string const oldAcquire = _config->Find("APT::Acquire::Translation","");
159 if (oldAcquire.empty() == false && oldAcquire != "environment") {
160 if (oldAcquire != "none")
161 codes.push_back(oldAcquire);
162 allCodes = codes;
163 return codes;
164 }
165
166 // It is very likely we will need to environment codes later,
167 // so let us generate them now from LC_MESSAGES and LANGUAGE
168 std::vector<string> environment;
169 // take care of LC_MESSAGES
170 if (envLongIncluded == false)
171 environment.push_back(envLong);
172 environment.push_back(envShort);
173 // take care of LANGUAGE
174 string envLang = Locale == 0 ? getenv("LANGUAGE") : *(Locale+1);
175 if (envLang.empty() == false) {
176 std::vector<string> env = ExplodeString(envLang,':');
177 short addedLangs = 0; // add a maximum of 3 fallbacks from the environment
178 for (std::vector<string>::const_iterator e = env.begin();
179 e != env.end() && addedLangs < 3; ++e) {
180 if (unlikely(e->empty() == true) || *e == "en")
181 continue;
182 if (*e == envLong || *e == envShort)
183 continue;
184 if (std::find(environment.begin(), environment.end(), *e) != environment.end())
185 continue;
186 if (e->find('_') != string::npos) {
187 // Drop LongCodes here - ShortCodes are also included
188 string const shorty = e->substr(0, e->find('_'));
189 char const **n = needLong;
190 for (; *n != NULL; ++n)
191 if (shorty == *n)
192 break;
193 if (*n == NULL)
194 continue;
195 }
196 ++addedLangs;
197 environment.push_back(*e);
198 }
199 }
200
201 // Support settings like Acquire::Translation=none on the command line to
202 // override the configuration settings vector of languages.
203 string const forceLang = _config->Find("Acquire::Languages","");
204 if (forceLang.empty() == false) {
205 if (forceLang == "environment") {
206 codes = environment;
207 } else if (forceLang != "none")
208 codes.push_back(forceLang);
209 allCodes = codes;
210 return codes;
211 }
212
213 std::vector<string> const lang = _config->FindVector("Acquire::Languages");
214 // the default setting -> "environment, en"
215 if (lang.empty() == true) {
216 codes = environment;
217 if (envShort != "en")
218 codes.push_back("en");
219 allCodes = codes;
220 return codes;
221 }
222
223 // the configs define the order, so add the environment
224 // then needed and ensure the codes are not listed twice.
225 bool noneSeen = false;
226 for (std::vector<string>::const_iterator l = lang.begin();
227 l != lang.end(); l++) {
228 if (*l == "environment") {
229 for (std::vector<string>::const_iterator e = environment.begin();
230 e != environment.end(); ++e) {
231 if (std::find(allCodes.begin(), allCodes.end(), *e) != allCodes.end())
232 continue;
233 if (noneSeen == false)
234 codes.push_back(*e);
235 allCodes.push_back(*e);
236 }
237 continue;
238 } else if (*l == "none") {
239 noneSeen = true;
240 continue;
241 } else if (std::find(allCodes.begin(), allCodes.end(), *l) != allCodes.end())
242 continue;
243
244 if (noneSeen == false)
245 codes.push_back(*l);
246 allCodes.push_back(*l);
247 }
248 if (All == true)
249 return allCodes;
250 else
251 return codes;
252 }
253 /*}}}*/
254 }