]>
git.saurik.com Git - apt.git/blob - apt-pkg/aptconfiguration.cc
9fd51ad5a95b6645ed8826f245d5365abe2d373f
1 // -*- mode: cpp; mode: fold -*-
3 /* ######################################################################
5 Provide access methods to various configuration settings,
6 setup defaults and returns validate settings.
8 ##################################################################### */
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>
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) {
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");
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");
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
);
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)
55 // ignore types we have no method ready to use
56 if (_config
->Exists(string("Acquire::CompressionTypes::").append(*o
)) == false)
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)
68 // move again over the option tree to add all missing compression types
69 ::Configuration::Item
const *Types
= _config
->Tree("Acquire::CompressionTypes");
73 for (; Types
!= 0; Types
= Types
->Next
) {
74 if (Types
->Tag
== "Order" || Types
->Tag
.empty() == true)
76 // ignore types we already have in the vector
77 if (std::find(types
.begin(),types
.end(),Types
->Tag
) != types
.end())
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)
86 types
.push_back(Types
->Tag
);
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
) {
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
;
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)
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);
129 string envLong
= envMsg
.substr(0,lenLong
);
130 string
const envShort
= envLong
.substr(0,lenShort
);
131 bool envLongIncluded
= true;
133 // first cornercase: LANG=C, so we use only "en" Translation
134 if (envLong
== "C") {
135 codes
.push_back("en");
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;
152 // we don't add the long code, but we allow the user to do so
153 if (envLongIncluded
== true)
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
);
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")
182 if (*e
== envLong
|| *e
== envShort
)
184 if (std::find(environment
.begin(), environment
.end(), *e
) != environment
.end())
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
)
197 environment
.push_back(*e
);
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") {
207 } else if (forceLang
!= "none")
208 codes
.push_back(forceLang
);
213 std::vector
<string
> const lang
= _config
->FindVector("Acquire::Languages");
214 // the default setting -> "environment, en"
215 if (lang
.empty() == true) {
217 if (envShort
!= "en")
218 codes
.push_back("en");
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())
233 if (noneSeen
== false)
235 allCodes
.push_back(*e
);
238 } else if (*l
== "none") {
241 } else if (std::find(allCodes
.begin(), allCodes
.end(), *l
) != allCodes
.end())
244 if (noneSeen
== false)
246 allCodes
.push_back(*l
);