aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/e/package_dep_spec.cc
blob: 4a27a12b134dcd5aaacf4a9779cc4c63a383432b (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2007, 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/repositories/e/package_dep_spec.hh>
#include <paludis/repositories/e/eapi.hh>
#include <paludis/repositories/e/use_requirements.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/log.hh>
#include <paludis/util/kc.hh>
#include <paludis/util/make_shared_ptr.hh>
#include <paludis/util/visitor-impl.hh>
#include <paludis/dep_spec.hh>
#include <paludis/version_operator.hh>
#include <paludis/version_spec.hh>
#include <paludis/version_requirements.hh>

using namespace paludis;
using namespace paludis::erepository;

PartiallyMadePackageDepSpec
paludis::erepository::partial_parse_e_package_dep_spec(
        const std::string & ss, const EAPI & eapi, const tr1::shared_ptr<const PackageID> & id)
{
    Context context("When parsing package dep spec '" + ss + "' with eapi '" + stringify(eapi[k::name()]) + "':");

    if (ss.empty())
        throw PackageDepSpecError("Got empty dep spec");

    if (! eapi[k::supported()])
        throw PackageDepSpecError("Don't know how to parse dep specs using EAPI '" + eapi[k::name()] + "'");

    PartiallyMadePackageDepSpec result;
    std::string s(ss);
    bool had_bracket_version_requirements(false);

    std::string::size_type use_group_p;
    while (std::string::npos != ((use_group_p = s.rfind('['))))
    {
        if (! (*eapi[k::supported()])[k::package_dep_spec_parse_options()][pdspo_allow_square_bracket_deps])
        {
            if ((*eapi[k::supported()])[k::package_dep_spec_parse_options()][pdspo_strict_parsing])
                throw PackageDepSpecError("[] dependencies not safe for use with this EAPI");
            else
                Log::get_instance()->message(ll_warning, lc_context, "[] dependencies not safe for use with this EAPI");
        }

        if (s.at(s.length() - 1) != ']')
            throw PackageDepSpecError("Mismatched []");

        std::string flag(s.substr(use_group_p + 1));
        if (flag.length() < 2)
            throw PackageDepSpecError("Invalid [] contents");

        flag.erase(flag.length() - 1);

        switch (flag.at(0))
        {
            case '<':
            case '>':
            case '=':
            case '~':
                {
                    char needed_mode(0);

                    while (! flag.empty())
                    {
                        Context cc("When parsing [] segment '" + flag + "':");

                        std::string op;
                        std::string::size_type opos(0);
                        while (opos < flag.length())
                            if (std::string::npos == std::string("><=~").find(flag.at(opos)))
                                break;
                            else
                                ++opos;

                        op = flag.substr(0, opos);
                        flag.erase(0, opos);

                        if (op.empty())
                            throw PackageDepSpecError("Missing operator inside []");

                        VersionOperator vop(op);

                        std::string ver;
                        opos = flag.find_first_of("|&");
                        if (std::string::npos == opos)
                        {
                            ver = flag;
                            flag.clear();
                        }
                        else
                        {
                            if (0 == needed_mode)
                                needed_mode = flag.at(opos);
                            else if (needed_mode != flag.at(opos))
                                throw PackageDepSpecError("Mixed & and | inside []");

                            result.version_requirements_mode((flag.at(opos) == '|' ? vr_or : vr_and));
                            ver = flag.substr(0, opos++);
                            flag.erase(0, opos);
                        }

                        if (ver.empty())
                            throw PackageDepSpecError("Missing version after operator '" + stringify(vop) + " inside []");

                        if ('*' == ver.at(ver.length() - 1))
                        {
                            ver.erase(ver.length() - 1);
                            if (vop == vo_equal)
                                vop = vo_equal_star;
                            else
                                throw PackageDepSpecError("Invalid use of * with operator '" + stringify(vop) + " inside []");
                        }

                        VersionSpec vs(ver);
                        result.version_requirement(VersionRequirement(vop, vs));
                        had_bracket_version_requirements = true;
                    }
                }
                break;

            default:
                {
                    std::string raw_flag("[" + flag + "]");

                    tr1::shared_ptr<const AdditionalPackageDepSpecRequirement> req;
                    if ('=' == flag.at(flag.length() - 1))
                    {
                        if (! id)
                            throw PackageDepSpecError("Cannot use [use=] without an associated ID");

                        flag.erase(flag.length() - 1);
                        if (flag.empty())
                            throw PackageDepSpecError("Invalid [] contents");
                        if ('!' == flag.at(flag.length() - 1))
                        {
                            flag.erase(flag.length() - 1);
                            if (flag.empty())
                                throw PackageDepSpecError("Invalid [] contents");
                            req.reset(new NotEqualUseRequirement(raw_flag, UseFlagName(flag), id));
                        }
                        else
                            req.reset(new EqualUseRequirement(raw_flag, UseFlagName(flag), id));
                    }
                    else if ('?' == flag.at(flag.length() - 1))
                    {
                        if (! id)
                            throw PackageDepSpecError("Cannot use [use?] without an associated ID");

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

                                req.reset(new IfNotMineThenNotUseRequirement(raw_flag, UseFlagName(flag), id));
                            }
                            else
                                req.reset(new IfNotMineThenUseRequirement(raw_flag, UseFlagName(flag), id));
                        }
                        else
                        {
                            if ('-' == flag.at(0))
                            {
                                flag.erase(0, 1);
                                if (flag.empty())
                                    throw PackageDepSpecError("Invalid [] contents");

                                req.reset(new IfMineThenNotUseRequirement(raw_flag, UseFlagName(flag), id));
                            }
                            else
                                req.reset(new IfMineThenUseRequirement(raw_flag, UseFlagName(flag), id));
                        }
                    }
                    else if ('-' == flag.at(0))
                    {
                        flag.erase(0, 1);
                        if (flag.empty())
                            throw PackageDepSpecError("Invalid [] contents");
                        req.reset(new DisabledUseRequirement(raw_flag, UseFlagName(flag)));
                    }
                    else
                        req.reset(new EnabledUseRequirement(raw_flag, UseFlagName(flag)));
                    result.additional_requirement(req);
                }
                break;
        };

        s.erase(use_group_p);
    }

    std::string::size_type repo_p;
    if (std::string::npos != ((repo_p = s.rfind("::"))))
    {
        if (! (*eapi[k::supported()])[k::package_dep_spec_parse_options()][pdspo_allow_repository_deps])
        {
            if ((*eapi[k::supported()])[k::package_dep_spec_parse_options()][pdspo_strict_parsing])
                throw PackageDepSpecError("Repository dependencies not safe for use with this EAPI");
            else
                Log::get_instance()->message(ll_warning, lc_context, "Repository dependencies not safe for use with this EAPI");
        }

        result.repository(RepositoryName(s.substr(repo_p + 2)));
        s.erase(repo_p);
    }

    std::string::size_type slot_p;
    if (std::string::npos != ((slot_p = s.rfind(':'))))
    {
        std::string match(s.substr(slot_p + 1));
        if (match.empty())
            throw PackageDepSpecError("Empty slot dependency specified");

        if ("*" == match)
        {
            if (! (*eapi[k::supported()])[k::package_dep_spec_parse_options()][pdspo_allow_slot_star_deps])
            {
                if ((*eapi[k::supported()])[k::package_dep_spec_parse_options()][pdspo_strict_parsing])
                    throw PackageDepSpecError("Slot '*' dependencies not safe for use with this EAPI");
                else
                    Log::get_instance()->message(ll_warning, lc_context, "Slot '*' dependencies not safe for use with this EAPI");
            }
            result.slot_requirement(make_shared_ptr(new ESlotAnyUnlockedRequirement));
        }
        else if ('=' == match.at(0))
        {
            if (! (*eapi[k::supported()])[k::package_dep_spec_parse_options()][pdspo_allow_slot_equal_deps])
            {
                if ((*eapi[k::supported()])[k::package_dep_spec_parse_options()][pdspo_strict_parsing])
                    throw PackageDepSpecError("Slot '=' dependencies not safe for use with this EAPI");
                else
                    Log::get_instance()->message(ll_warning, lc_context, "Slot '=' dependencies not safe for use with this EAPI");
            }

            if (1 == match.length())
                result.slot_requirement(make_shared_ptr(new ESlotAnyLockedRequirement));
            else
                result.slot_requirement(make_shared_ptr(new ESlotExactRequirement(SlotName(s.substr(slot_p + 2)), true)));
        }
        else
        {
            if (! (*eapi[k::supported()])[k::package_dep_spec_parse_options()][pdspo_allow_slot_deps])
            {
                if ((*eapi[k::supported()])[k::package_dep_spec_parse_options()][pdspo_strict_parsing])
                    throw PackageDepSpecError("Slot dependencies not safe for use with this EAPI");
                else
                    Log::get_instance()->message(ll_warning, lc_context, "Slot dependencies not safe for use with this EAPI");
            }
            result.slot_requirement(make_shared_ptr(new ESlotExactRequirement(SlotName(s.substr(slot_p + 1)), false)));
        }
        s.erase(slot_p);
    }

    if (std::string::npos != std::string("<>=~").find(s.at(0)))
    {
        if (had_bracket_version_requirements)
            throw PackageDepSpecError("Cannot mix [] and traditional version specifications");

        std::string::size_type p(1);
        if (s.length() > 1 && std::string::npos != std::string("<>=~").find(s.at(1)))
            ++p;
        VersionOperator op(s.substr(0, p));

        if (op == vo_tilde_greater)
            if (! (*eapi[k::supported()])[k::package_dep_spec_parse_options()][pdspo_allow_tilde_greater_deps])
            {
                if ((*eapi[k::supported()])[k::package_dep_spec_parse_options()][pdspo_strict_parsing])
                    throw PackageDepSpecError("~> dependencies not safe for use with this EAPI");
                else
                    Log::get_instance()->message(ll_warning, lc_context, "~> dependencies not safe for use with this EAPI");
            }

        std::string::size_type q(p);

        while (true)
        {
            if (p >= s.length())
                throw PackageDepSpecError("Couldn't parse dep spec '" + ss + "'");
            q = s.find('-', q + 1);
            if ((std::string::npos == q) || (++q >= s.length()))
                throw PackageDepSpecError("Couldn't parse dep spec '" + ss + "'");
            if ((s.at(q) >= '0' && s.at(q) <= '9') || (0 == s.compare(q, 3, "scm")))
                break;
        }

        std::string::size_type new_q(q);
        while (true)
        {
            if (new_q >= s.length())
                break;
            new_q = s.find('-', new_q + 1);
            if ((std::string::npos == new_q) || (++new_q >= s.length()))
                break;
            if (s.at(new_q) >= '0' && s.at(new_q) <= '9')
                q = new_q;
        }

        std::string t(s.substr(p, q - p - 1));
        if (t.length() >= 3 && (0 == t.compare(0, 2, "*/")))
        {
            throw PackageDepSpecError("Wildcard '*' not allowed in '" + stringify(ss) + "' with eapi '"
                    + stringify(eapi[k::name()]) + "'");

            if (0 != t.compare(t.length() - 2, 2, "/*"))
                result.package_name_part(PackageNamePart(t.substr(2)));
        }
        else if (t.length() >= 3 && (0 == t.compare(t.length() - 2, 2, "/*")))
        {
            throw PackageDepSpecError("Wildcard '*' not allowed in '" + stringify(ss) + "' with eapi '"
                    + stringify(eapi[k::name()]) + "'");

            result.category_name_part(CategoryNamePart(t.substr(0, t.length() - 2)));
        }
        else
            result.package(QualifiedPackageName(t));

        if ('*' == s.at(s.length() - 1))
        {
            if (op != vo_equal)
            {
                if (! (*eapi[k::supported()])[k::package_dep_spec_parse_options()][pdspo_strict_star_operator])
                {
                    if ((*eapi[k::supported()])[k::package_dep_spec_parse_options()][pdspo_strict_parsing])
                        throw PackageDepSpecError(
                                "Package dep spec '" + ss + "' uses * "
                                "with operator '" + stringify(op) + "'");
                    else
                        Log::get_instance()->message(ll_qa, lc_context,
                                "Package dep spec '" + ss + "' uses * "
                                "with operator '" + stringify(op) +
                                "', pretending it uses the equals operator instead");
                }
            }
            op = vo_equal_star;

            result.version_requirement(VersionRequirement(op, VersionSpec(s.substr(q, s.length() - q - 1))));
        }
        else
            result.version_requirement(VersionRequirement(op, VersionSpec(s.substr(q))));
    }
    else
    {
        if (s.length() >= 3 && (0 == s.compare(0, 2, "*/")))
        {
            throw PackageDepSpecError("Wildcard '*' not allowed in '" + stringify(ss) + "' with parse eapi '"
                    + stringify(eapi[k::name()]) + "'");

            if (0 != s.compare(s.length() - 2, 2, "/*"))
                result.package_name_part(PackageNamePart(s.substr(2)));
        }
        else if (s.length() >= 3 && (0 == s.compare(s.length() - 2, 2, "/*")))
        {
            throw PackageDepSpecError("Wildcard '*' not allowed in '" + stringify(ss) + "' with EAPI '"
                    + stringify(eapi[k::name()]) + "'");

            result.category_name_part(CategoryNamePart(s.substr(0, s.length() - 2)));
        }
        else
            result.package(QualifiedPackageName(s));
    }

    return result;
}

PackageDepSpec
paludis::erepository::parse_e_package_dep_spec(const std::string & ss, const EAPI & eapi, const tr1::shared_ptr<const PackageID> & id)
{
    return partial_parse_e_package_dep_spec(ss, eapi, id);
}

ESlotExactRequirement::ESlotExactRequirement(const SlotName & s, const bool e) :
    _s(s),
    _e(e)
{
}

const std::string
ESlotExactRequirement::as_string() const
{
    return ":" + std::string(_e ? "=" : "") + stringify(_s);
}

const SlotName
ESlotExactRequirement::slot() const
{
    return _s;
}

const std::string
ESlotAnyUnlockedRequirement::as_string() const
{
    return ":*";
}

const std::string
ESlotAnyLockedRequirement::as_string() const
{
    return ":=";
}