aboutsummaryrefslogtreecommitdiff
path: root/paludis/elike_use_requirement.cc
blob: 1aabf3345566addde0ac7759424644893b38e321 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2008 Ciaran McCreesh
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <paludis/elike_use_requirement.hh>
#include <paludis/util/options.hh>
#include <paludis/util/make_shared_ptr.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/join.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/util/log.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/tribool.hh>
#include <paludis/dep_spec.hh>
#include <paludis/name.hh>
#include <paludis/environment.hh>
#include <paludis/package_id.hh>
#include <paludis/metadata_key.hh>
#include <paludis/choice.hh>
#include <vector>
#include <functional>
#include <algorithm>

using namespace paludis;

namespace
{
    bool icky_use_query(const ChoiceNameWithPrefix & f, const PackageID & id, Tribool default_value = Tribool(indeterminate))
    {
        if (! id.choices_key())
        {
            Log::get_instance()->message("elike_use_requirement.query", ll_warning, lc_context) <<
                "ID '" << id << "' has no choices, so couldn't get the state of flag '" << f << "'";
            return false;
        }

        const std::tr1::shared_ptr<const ChoiceValue> v(id.choices_key()->value()->find_by_name_with_prefix(f));
        if (v)
            return v->enabled();

        if (default_value.is_indeterminate() && ! id.choices_key()->value()->has_matching_contains_every_value_prefix(f))
            Log::get_instance()->message("elike_use_requirement.query", ll_warning, lc_context) <<
                "ID '" << id << "' has no flag named '" << f << "'";
        return default_value.is_true();
    }

    class UseRequirement
    {
        private:
            const ChoiceNameWithPrefix _name;
            const Tribool _default_value;

        public:
            UseRequirement(const ChoiceNameWithPrefix & n, Tribool d) :
                _name(n),
                _default_value(d)
            {
            }
            virtual ~UseRequirement() { }

            const ChoiceNameWithPrefix flag() const PALUDIS_ATTRIBUTE((warn_unused_result))
            {
                return _name;
            }

            const Tribool default_value() const PALUDIS_ATTRIBUTE((warn_unused_result))
            {
                return _default_value;
            }

            virtual bool requirement_met(const Environment * const, const PackageID &) const PALUDIS_ATTRIBUTE((warn_unused_result)) = 0;
            virtual const std::string as_human_string() const PALUDIS_ATTRIBUTE((warn_unused_result)) = 0;

            const std::string default_value_human_string_fragment() const PALUDIS_ATTRIBUTE((warn_unused_result))
            {
                if (_default_value.is_true())
                    return ", assuming enabled if missing";
                else if (_default_value.is_false())
                    return ", assuming disabled if missing";
                else
                    return "";
            }
    };

    class PALUDIS_VISIBLE EnabledUseRequirement :
        public UseRequirement
    {
        public:
            EnabledUseRequirement(const ChoiceNameWithPrefix & n, Tribool d) :
                UseRequirement(n, d)
            {
            }

            virtual bool requirement_met(const Environment * const, const PackageID & pkg) const
            {
                return icky_use_query(flag(), pkg, default_value());
            }

            virtual const std::string as_human_string() const
            {
                return "Flag '" + stringify(flag()) + "' enabled" + default_value_human_string_fragment();
            }
    };

    class PALUDIS_VISIBLE DisabledUseRequirement :
        public UseRequirement
    {
        public:
            DisabledUseRequirement(const ChoiceNameWithPrefix & n, Tribool d) :
                UseRequirement(n, d)
            {
            }

            virtual bool requirement_met(const Environment * const, const PackageID & pkg) const
            {
                return ! icky_use_query(flag(), pkg, default_value());
            }

            virtual const std::string as_human_string() const
            {
                return "Flag '" + stringify(flag()) + "' disabled" + default_value_human_string_fragment();
            }
    };

    class PALUDIS_VISIBLE ConditionalUseRequirement :
        public UseRequirement
    {
        private:
            const std::tr1::shared_ptr<const PackageID> _id;

        public:
            ConditionalUseRequirement(const ChoiceNameWithPrefix & n, const std::tr1::shared_ptr<const PackageID> & i, Tribool d) :
                UseRequirement(n, d),
                _id(i)
            {
            }

            const std::tr1::shared_ptr<const PackageID> package_id() const PALUDIS_ATTRIBUTE((warn_unused_result))
            {
                return _id;
            }
    };

    class PALUDIS_VISIBLE IfMineThenUseRequirement :
        public ConditionalUseRequirement
    {
        public:
            IfMineThenUseRequirement(const ChoiceNameWithPrefix & n, const std::tr1::shared_ptr<const PackageID> & i, Tribool d) :
                ConditionalUseRequirement(n, i, d)
            {
            }

            virtual bool requirement_met(const Environment * const, const PackageID & pkg) const
            {
                return ! icky_use_query(flag(), *package_id()) || icky_use_query(flag(), pkg, default_value());
            }

            virtual const std::string as_human_string() const
            {
                return "Flag '" + stringify(flag()) + "' enabled if it is enabled for '" + stringify(*package_id()) + "'" + default_value_human_string_fragment();
            }
    };

    class PALUDIS_VISIBLE IfNotMineThenUseRequirement :
        public ConditionalUseRequirement
    {
        public:
            IfNotMineThenUseRequirement(const ChoiceNameWithPrefix & n, const std::tr1::shared_ptr<const PackageID> & i, Tribool d) :
                ConditionalUseRequirement(n, i, d)
            {
            }

            virtual bool requirement_met(const Environment * const, const PackageID & pkg) const
            {
                return icky_use_query(flag(), *package_id()) || icky_use_query(flag(), pkg, default_value());
            }

            virtual const std::string as_human_string() const
            {
                return "Flag '" + stringify(flag()) + "' enabled if it is disabled for '" + stringify(*package_id()) + "'" + default_value_human_string_fragment();
            }
    };

    class PALUDIS_VISIBLE IfMineThenNotUseRequirement :
        public ConditionalUseRequirement
    {
        public:
            IfMineThenNotUseRequirement(const ChoiceNameWithPrefix & n, const std::tr1::shared_ptr<const PackageID> & i, Tribool d) :
                ConditionalUseRequirement(n, i, d)
            {
            }

            virtual bool requirement_met(const Environment * const, const PackageID & pkg) const
            {
                return ! icky_use_query(flag(), *package_id()) || ! icky_use_query(flag(), pkg, default_value());
            }

            virtual const std::string as_human_string() const
            {
                return "Flag '" + stringify(flag()) + "' disabled if it is enabled for '" + stringify(*package_id()) + "'" + default_value_human_string_fragment();
            }
    };

    class PALUDIS_VISIBLE IfNotMineThenNotUseRequirement :
        public ConditionalUseRequirement
    {
        public:
            IfNotMineThenNotUseRequirement(const ChoiceNameWithPrefix & n, const std::tr1::shared_ptr<const PackageID> & i, Tribool d) :
                ConditionalUseRequirement(n, i, d)
            {
            }

            virtual bool requirement_met(const Environment * const, const PackageID & pkg) const
            {
                return icky_use_query(flag(), *package_id()) || ! icky_use_query(flag(), pkg, default_value());
            }

            virtual const std::string as_human_string() const
            {
                return "Flag '" + stringify(flag()) + "' disabled if it is disabled for '" + stringify(*package_id()) + "'" + default_value_human_string_fragment();
            }
    };

    class PALUDIS_VISIBLE EqualUseRequirement :
        public ConditionalUseRequirement
    {
        public:
            EqualUseRequirement(const ChoiceNameWithPrefix & n, const std::tr1::shared_ptr<const PackageID> & i, Tribool d) :
                ConditionalUseRequirement(n, i, d)
            {
            }

            virtual bool requirement_met(const Environment * const, const PackageID & pkg) const
            {
                return icky_use_query(flag(), pkg, default_value()) == icky_use_query(flag(), *package_id());
            }

            virtual const std::string as_human_string() const
            {
                return "Flag '" + stringify(flag()) + "' enabled or disabled like it is for '" + stringify(*package_id()) + "'" + default_value_human_string_fragment();
            }
    };

    class PALUDIS_VISIBLE NotEqualUseRequirement :
        public ConditionalUseRequirement
    {
        public:
            NotEqualUseRequirement(const ChoiceNameWithPrefix & n, const std::tr1::shared_ptr<const PackageID> & i, Tribool d) :
                ConditionalUseRequirement(n, i, d)
            {
            }

            virtual bool requirement_met(const Environment * const, const PackageID & pkg) const
            {
                return icky_use_query(flag(), pkg, default_value()) != icky_use_query(flag(), *package_id());
            }

            virtual const std::string as_human_string() const
            {
                return "Flag '" + stringify(flag()) + "' enabled or disabled opposite to how it is for '" + stringify(*package_id()) + "'" + default_value_human_string_fragment();
            }
    };

    class UseRequirements :
        public AdditionalPackageDepSpecRequirement
    {
        private:
            std::string _raw;
            std::vector<std::tr1::shared_ptr<const UseRequirement> > _reqs;

        public:
            UseRequirements(const std::string & r) :
                _raw(r)
            {
            }

            virtual bool requirement_met(const Environment * const env, const PackageID & id) const
            {
                using namespace std::tr1::placeholders;
                return _reqs.end() == std::find_if(_reqs.begin(), _reqs.end(), std::tr1::bind(
                        std::logical_not<bool>(), std::tr1::bind(
                             &UseRequirement::requirement_met, _1, env, std::tr1::cref(id))));
            }

            virtual const std::string as_human_string() const
            {
                return join(_reqs.begin(), _reqs.end(), "; ", std::tr1::mem_fn(&UseRequirement::as_human_string));
            }

            virtual const std::string as_raw_string() const
            {
                return _raw;
            }

            void add_requirement(const std::tr1::shared_ptr<const UseRequirement> & req)
            {
                _reqs.push_back(req);
            }
    };

    template <typename T_>
    std::tr1::shared_ptr<const UseRequirement>
    make_unconditional_requirement(const ChoiceNameWithPrefix & n, const std::tr1::shared_ptr<const PackageID > &, Tribool d)
    {
        return make_shared_ptr(new T_(n, d));
    }

    template <typename T_>
    std::tr1::shared_ptr<const UseRequirement>
    make_conditional_requirement(const ChoiceNameWithPrefix & n, const std::tr1::shared_ptr<const PackageID > & i, Tribool d)
    {
        return make_shared_ptr(new T_(n, i, d));
    }

    std::tr1::shared_ptr<const UseRequirement>
    parse_one_use_requirement(const std::string & s, std::string & flag,
            const std::tr1::shared_ptr<const PackageID> & id, const ELikeUseRequirementOptions & options)
    {
        typedef std::tr1::shared_ptr<const UseRequirement> (* Factory)(const ChoiceNameWithPrefix &, const std::tr1::shared_ptr<const PackageID> &, Tribool);
        Factory factory;

        if (flag.empty())
            throw ELikeUseRequirementError(s, "Invalid [] contents");

        if ('=' == flag.at(flag.length() - 1))
        {
            if ((! options[euro_allow_self_deps]) || (! id))
                throw ELikeUseRequirementError(s, "Cannot use [use=] here");

            flag.erase(flag.length() - 1);
            if (flag.empty())
                throw ELikeUseRequirementError(s, "Invalid [] contents");

            if ('!' == flag.at(flag.length() - 1))
            {
                if (options[euro_portage_syntax] && ! options[euro_both_syntaxes])
                {
                    if (options[euro_strict_parsing])
                        throw ELikeUseRequirementError(s, "[use!=] not safe for use here");
                    else
                        Log::get_instance()->message("e.use_requirement.flag_not_equal_not_allowed", ll_warning, lc_context)
                            << "[use!=] not safe for use here";
                }

                flag.erase(flag.length() - 1, 1);
                if (flag.empty())
                    throw ELikeUseRequirementError(s, "Invalid [] contents");
                factory = make_conditional_requirement<NotEqualUseRequirement>;
            }
            else if ('!' == flag.at(0))
            {
                if (! options[euro_portage_syntax] && ! options[euro_both_syntaxes])
                {
                    if (options[euro_strict_parsing])
                        throw ELikeUseRequirementError(s, "[!use=] not safe for use here");
                    else
                        Log::get_instance()->message("e.use_requirement.not_flag_equal_not_allowed", ll_warning, lc_context)
                            << "[!use=] not safe for use here";
                }

                flag.erase(0, 1);
                if (flag.empty())
                    throw ELikeUseRequirementError(s, "Invalid [] contents");
                factory = make_conditional_requirement<NotEqualUseRequirement>;
            }
            else
                factory = make_conditional_requirement<EqualUseRequirement>;
        }
        else if ('?' == flag.at(flag.length() - 1))
        {
            if ((! options[euro_allow_self_deps]) || (! id))
                throw ELikeUseRequirementError(s, "Cannot use [use?] here");

            flag.erase(flag.length() - 1);
            if (flag.empty())
                throw ELikeUseRequirementError(s, "Invalid [] contents");

            if ('!' == flag.at(flag.length() - 1))
            {
                flag.erase(flag.length() - 1, 1);
                if (flag.empty())
                    throw ELikeUseRequirementError(s, "Invalid [] contents");

                if ('-' == flag.at(0))
                {
                    if (options[euro_portage_syntax] && ! options[euro_both_syntaxes])
                    {
                        if (options[euro_strict_parsing])
                            throw ELikeUseRequirementError(s, "[-use!?] not safe for use here");
                        else
                            Log::get_instance()->message("e.use_requirement.minus_flag_not_question_not_allowed", ll_warning, lc_context)
                                << "[-use!?] not safe for use here";
                    }

                    flag.erase(0, 1);
                    if (flag.empty())
                        throw ELikeUseRequirementError(s, "Invalid [] contents");

                    factory = make_conditional_requirement<IfNotMineThenNotUseRequirement>;
                }
                else
                {
                    if (options[euro_portage_syntax] && ! options[euro_both_syntaxes])
                    {
                        if (options[euro_strict_parsing])
                            throw ELikeUseRequirementError(s, "[use!?] not safe for use here");
                        else
                            Log::get_instance()->message("e.use_requirement.flag_not_question_not_allowed", ll_warning, lc_context)
                                << "[use!?] not safe for use here";
                    }

                    factory = make_conditional_requirement<IfNotMineThenUseRequirement>;
                }
            }
            else if ('!' == flag.at(0))
            {
                if (! options[euro_portage_syntax] && ! options[euro_both_syntaxes])
                {
                    if (options[euro_strict_parsing])
                        throw ELikeUseRequirementError(s, "[!use?] not safe for use here");
                    else
                        Log::get_instance()->message("e.use_requirement.not_flag_question_not_allowed", ll_warning, lc_context)
                            << "[!use?] not safe for use here";
                }

                flag.erase(0, 1);
                if (flag.empty())
                    throw ELikeUseRequirementError(s, "Invalid [] contents");
                factory = make_conditional_requirement<IfNotMineThenNotUseRequirement>;
            }
            else
            {
                if ('-' == flag.at(0))
                {
                    if (options[euro_portage_syntax] && ! options[euro_both_syntaxes])
                    {
                        if (options[euro_strict_parsing])
                            throw ELikeUseRequirementError(s, "[-use?] not safe for use here");
                        else
                            Log::get_instance()->message("e.use_requirement.minus_flag_question_not_allowed", ll_warning, lc_context)
                                << "[-use?] not safe for use here";
                    }

                    flag.erase(0, 1);
                    if (flag.empty())
                        throw ELikeUseRequirementError(s, "Invalid [] contents");

                    factory = make_conditional_requirement<IfMineThenNotUseRequirement>;
                }
                else
                    factory = make_conditional_requirement<IfMineThenUseRequirement>;
            }
        }
        else if ('-' == flag.at(0))
        {
            flag.erase(0, 1);
            if (flag.empty())
                throw ELikeUseRequirementError(s, "Invalid [] contents");
            factory = make_unconditional_requirement<DisabledUseRequirement>;
        }
        else
            factory = make_unconditional_requirement<EnabledUseRequirement>;

        if ('+' == flag.at(flag.length() - 1))
        {
            if (! options[euro_allow_default_values])
            {
                if (options[euro_strict_parsing])
                    throw ELikeUseRequirementError(s, "[use+] not safe for use here");
                else
                    Log::get_instance()->message("e.use_requirement.flag_not_equal_not_allowed", ll_warning, lc_context)
                        << "[use+] not safe for use here";
            }

            flag.erase(flag.length() - 1, 1);
            if (flag.empty())
                throw ELikeUseRequirementError(s, "Invalid [] contents");
            return factory(ChoiceNameWithPrefix(flag), id, Tribool(true));
        }
        else if ('-' == flag.at(flag.length() - 1))
        {
            if (! options[euro_allow_default_values])
            {
                if (options[euro_strict_parsing])
                    throw ELikeUseRequirementError(s, "[use-] not safe for use here");
                else
                    Log::get_instance()->message("e.use_requirement.flag_not_equal_not_allowed", ll_warning, lc_context)
                        << "[use-] not safe for use here";
            }

            flag.erase(flag.length() - 1, 1);
            if (flag.empty())
                throw ELikeUseRequirementError(s, "Invalid [] contents");
            return factory(ChoiceNameWithPrefix(flag), id, Tribool(false));
        }
        else
            return factory(ChoiceNameWithPrefix(flag), id, Tribool(indeterminate));
    }
}
ELikeUseRequirementError::ELikeUseRequirementError(const std::string & s, const std::string & m) throw () :
    Exception("Error parsing use requirement '" + s + "': " + m)
{
}

std::tr1::shared_ptr<const AdditionalPackageDepSpecRequirement>
paludis::parse_elike_use_requirement(const std::string & s,
        const std::tr1::shared_ptr<const PackageID> & id, const ELikeUseRequirementOptions & options)
{
    Context context("When parsing use requirement '" + s + "':");

    std::tr1::shared_ptr<UseRequirements> result(new UseRequirements("[" + s + "]"));
    std::string::size_type pos(0);
    for (;;)
    {
        std::string::size_type comma(s.find(',', pos));
        std::string flag(s.substr(pos, std::string::npos == comma ? comma : comma - pos));
        result->add_requirement(parse_one_use_requirement(s, flag, id, options));
        if (std::string::npos == comma)
            break;

        if (! options[euro_portage_syntax] && ! options[euro_both_syntaxes])
        {
            if (options[euro_strict_parsing])
                throw ELikeUseRequirementError(s, "[use,use] not safe for use here");
            else
                Log::get_instance()->message("e.use_requirement.comma_separated_not_allowed", ll_warning, lc_context)
                    << "[use,use] not safe for use here";
        }

        pos = comma + 1;
    }

    return result;
}