aboutsummaryrefslogtreecommitdiff
path: root/paludis/util/config_file.cc
blob: 3a3d5e41411f081b3f198bc8500a05f95a263696 (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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006, 2007, 2008 Ciaran McCreesh
 * Copyright (c) 2006 Danny van Dyk
 *
 * 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/util/config_file.hh>
#include <paludis/util/exception.hh>
#include <paludis/util/fs_entry.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/wrapped_forward_iterator-impl.hh>
#include <paludis/util/private_implementation_pattern-impl.hh>
#include <paludis/util/options.hh>
#include <paludis/util/simple_parser.hh>
#include <paludis/util/log.hh>

#include <fstream>
#include <istream>
#include <list>
#include <map>

using namespace paludis;

#include <paludis/util/config_file-se.cc>

template class WrappedForwardIterator<LineConfigFile::ConstIteratorTag, const std::string>;
template class WrappedForwardIterator<KeyValueConfigFile::ConstIteratorTag, const std::pair<const std::string, std::string> >;

ConfigFileError::ConfigFileError(const std::string & filename, const std::string & m) throw () :
    ConfigurationError(filename.empty() ? m : "In file '" + filename + "': " + m)
{
}

ConfigFileError::ConfigFileError(const std::string & m) throw () :
    ConfigurationError(m)
{
}

namespace paludis
{
    template <>
    struct Implementation<ConfigFile::Source>
    {
        std::string filename;
        std::tr1::shared_ptr<const std::string> text;

        Implementation(const FSEntry & f) :
            filename(stringify(f))
        {
            std::ifstream ff(stringify(f).c_str());
            if (! ff)
                throw ConfigFileError(filename, "Could not read file");
            std::tr1::shared_ptr<std::string> t(new std::string);
            std::copy((std::istreambuf_iterator<char>(ff)), std::istreambuf_iterator<char>(), std::back_inserter(*t));
            text = t;
        }

        Implementation(const std::string & s) :
            text(new std::string(s))
        {
        }

        Implementation(std::istream & ff)
        {
            std::tr1::shared_ptr<std::string> t(new std::string);
            std::copy((std::istreambuf_iterator<char>(ff)), std::istreambuf_iterator<char>(), std::back_inserter(*t));
            text = t;
        }

        Implementation(const std::string & f, const std::tr1::shared_ptr<const std::string> & t) :
            filename(f),
            text(t)
        {
        }
    };
}

ConfigFile::Source::Source(const FSEntry & f) :
    PrivateImplementationPattern<ConfigFile::Source>(new Implementation<ConfigFile::Source>(f))
{
}

ConfigFile::Source::Source(const std::string & f) :
    PrivateImplementationPattern<ConfigFile::Source>(new Implementation<ConfigFile::Source>(f))
{
}

ConfigFile::Source::Source(std::istream & f) :
    PrivateImplementationPattern<ConfigFile::Source>(new Implementation<ConfigFile::Source>(f))
{
}

ConfigFile::Source::Source(const ConfigFile::Source & f) :
    PrivateImplementationPattern<ConfigFile::Source>(new Implementation<ConfigFile::Source>(f._imp->filename, f._imp->text))
{
}

const ConfigFile::Source &
ConfigFile::Source::operator= (const ConfigFile::Source & other)
{
    if (this != &other)
    {
        _imp->filename = other._imp->filename;
        _imp->text = other._imp->text;
    }
    return *this;
}

ConfigFile::Source::~Source()
{
}

const std::string &
ConfigFile::Source::text() const
{
    return *_imp->text;
}

const std::string &
ConfigFile::Source::filename() const
{
    return _imp->filename;
}

template class PrivateImplementationPattern<ConfigFile::Source>;

ConfigFile::~ConfigFile()
{
}

namespace paludis
{
    template <>
    struct Implementation<LineConfigFile>
    {
        const LineConfigFileOptions options;
        std::list<std::string> lines;

        Implementation(const LineConfigFileOptions & o) :
            options(o)
        {
        }
    };
}

namespace
{
    void parse_after_continuation(const ConfigFile::Source & sr, SimpleParser & parser, const bool recognise_comments)
    {
        if (parser.eof())
            throw ConfigFileError(sr.filename(), "EOF after continuation near line " + stringify(parser.current_line_number()));
        else if (recognise_comments && parser.lookahead(*simple_parser::any_of(" \t") & simple_parser::exact("#")))
            throw ConfigFileError(sr.filename(),
                    "Comment not allowed immediately after after continuation near line " + stringify(parser.current_line_number()));
    }
}

LineConfigFile::LineConfigFile(const Source & sr, const LineConfigFileOptions & o) :
    PrivateImplementationPattern<LineConfigFile>(new Implementation<LineConfigFile>(o))
{
    Context context("When parsing line-based configuration file '" + (sr.filename().empty() ? "?" : sr.filename()) + "':");

    SimpleParser parser(sr.text());
    while (! parser.eof())
    {
        /* is it a comment? */
        if (! _imp->options[lcfo_disallow_comments])
        {
            if (parser.consume(*simple_parser::any_of(" \t") & simple_parser::exact("#") &
                        *simple_parser::any_except("\n")))
            {
                /* expect newline, but handle eof without final newline */
                if (! parser.consume(simple_parser::exact("\n")))
                {
                    if (parser.eof())
                    {
                        Log::get_instance()->message("line_config_file.no_trailing_newline", ll_warning, lc_context)
                            << "No newline at end of file";
                        break;
                    }
                    else
                        throw ConfigFileError(sr.filename(),
                                "Something is very strange at line '" + stringify(parser.current_line_number() + "'"));
                }
                continue;
            }
        }

        if (! _imp->options[lcfo_preserve_whitespace])
            if (! parser.consume(*simple_parser::any_of(" \t")))
                throw InternalError(PALUDIS_HERE, "failed to consume a zero width match");

        if (parser.eof())
        {
            Log::get_instance()->message("line_config_file.no_trailing_newline", ll_warning, lc_context)
                << "No newline at end of file";
            break;
        }

        /* is it a blank line? */
        if (! _imp->options[lcfo_no_skip_blank_lines])
        {
            if (parser.consume(simple_parser::exact("\n")))
                continue;
        }

        /* normal line, or lines with continuation */
        std::string line, word, space;
        bool need_single_space_unless_eol(false);
        while (true)
        {
            if (parser.eof())
            {
                Log::get_instance()->message("line_config_file.no_trailing_newline", ll_warning, lc_context)
                    << "No newline at end of file";
                break;
            }
            else if (parser.consume(+simple_parser::any_of(" \t") >> space))
            {
                if (_imp->options[lcfo_preserve_whitespace])
                    line.append(space);
                else if (! line.empty())
                    need_single_space_unless_eol = true;
            }
            else if (parser.consume(simple_parser::exact("\n") >> space))
                break;
            else if ((! _imp->options[lcfo_disallow_continuations]) && parser.consume(simple_parser::exact("\\\n")))
            {
                parse_after_continuation(sr, parser, ! _imp->options[lcfo_disallow_comments]);
            }
            else if (parser.consume(simple_parser::exact("\\") >> word))
            {
                if (need_single_space_unless_eol)
                {
                    need_single_space_unless_eol = false;
                    line.append(" ");
                }
                line.append(word);
            }
            else if ((! line.empty()) && (_imp->options[lcfo_allow_inline_comments]) && parser.consume(simple_parser::exact("#") &
                        *simple_parser::any_except("\n")))
            {
                if (! parser.consume(simple_parser::exact("\n")))
                    if (! parser.eof())
                        throw ConfigFileError(sr.filename(),
                                "Something is very strange at line '" + stringify(parser.current_line_number() + "'"));
            }
            else if (parser.consume(simple_parser::exact("#") >> word))
            {
                if (need_single_space_unless_eol)
                {
                    need_single_space_unless_eol = false;
                    line.append(" ");
                }
                line.append(word);
            }
            else if (parser.consume(+simple_parser::any_except(" \t\n\\#") >> word))
            {
                if (need_single_space_unless_eol)
                {
                    need_single_space_unless_eol = false;
                    line.append(" ");
                }
                line.append(word);
            }
            else
                throw ConfigFileError(sr.filename(), "Unparsable text in line " + stringify(parser.current_line_number()));
        }
        _imp->lines.push_back(line);
    }
}

LineConfigFile::~LineConfigFile()
{
}

LineConfigFile::ConstIterator
LineConfigFile::begin() const
{
    return ConstIterator(_imp->lines.begin());
}

LineConfigFile::ConstIterator
LineConfigFile::end() const
{
    return ConstIterator(_imp->lines.end());
}

template class PrivateImplementationPattern<LineConfigFile>;

namespace paludis
{
    template <>
    struct Implementation<KeyValueConfigFile>
    {
        const KeyValueConfigFileOptions options;
        const KeyValueConfigFile::DefaultFunction default_function;
        const KeyValueConfigFile::TransformationFunction transformation_function;

        std::map<std::string, std::string> values;

        Implementation(
                const KeyValueConfigFileOptions & o,
                const KeyValueConfigFile::DefaultFunction & d,
                const KeyValueConfigFile::TransformationFunction & i) :
            options(o),
            default_function(d),
            transformation_function(i)
        {
        }
    };
}

namespace
{
    bool parse_value(const KeyValueConfigFile & k, const ConfigFile::Source & sr, SimpleParser & parser, std::string & result)
        PALUDIS_ATTRIBUTE((warn_unused_result));
    bool parse_single_quoted_value(const KeyValueConfigFile & k, const ConfigFile::Source & sr, SimpleParser & parser, std::string & result)
        PALUDIS_ATTRIBUTE((warn_unused_result));
    bool parse_double_quoted_value(const KeyValueConfigFile & k, const ConfigFile::Source & sr, SimpleParser & parser, std::string & result)
        PALUDIS_ATTRIBUTE((warn_unused_result));
    bool parse_unquoted_value(const KeyValueConfigFile & k, const ConfigFile::Source & sr, SimpleParser & parser, std::string & result)
        PALUDIS_ATTRIBUTE((warn_unused_result));
    bool parse_variable(const KeyValueConfigFile & k, const ConfigFile::Source & sr, SimpleParser & parser, std::string & var)
        PALUDIS_ATTRIBUTE((warn_unused_result));

    bool parse_value(const KeyValueConfigFile & k, const ConfigFile::Source & sr, SimpleParser & parser, std::string & result)
    {
        if ((k.options()[kvcfo_allow_inline_comments] && parser.lookahead(simple_parser::exact("#"))))
            return true;

        if ((! k.options()[kvcfo_disallow_single_quoted_strings]) && parser.consume(simple_parser::exact("'")))
            return parse_single_quoted_value(k, sr, parser, result);

        if ((! k.options()[kvcfo_disallow_double_quoted_strings]) && parser.consume(simple_parser::exact("\"")))
            return parse_double_quoted_value(k, sr, parser, result);

        if (! k.options()[kvcfo_disallow_unquoted_values])
            return parse_unquoted_value(k, sr, parser, result);

        return false;
    }

    bool parse_single_quoted_value(const KeyValueConfigFile & k, const ConfigFile::Source & sr, SimpleParser & parser, std::string & result)
    {
        while (true)
        {
            if (parser.eof())
                throw ConfigFileError(sr.filename(), "Unterminated single quote at line " + stringify(parser.current_line_number()));

            std::string s;

            if ((! k.options()[kvcfo_disallow_continuations]) && parser.consume(simple_parser::exact("\\\n")))
            {
                parse_after_continuation(sr, parser, ! k.options()[kvcfo_disallow_comments]);
                continue;
            }
            else if (parser.consume(simple_parser::exact("'")))
                break;
            else if (parser.consume((simple_parser::any_except("") & *simple_parser::any_except("\\'")) >> s))
                result.append(s);
            else
                throw ConfigFileError(sr.filename(), "Can't parse single quoted string at line " + stringify(parser.current_line_number()));
        }

        return true;
    }

    bool parse_double_quoted_value(const KeyValueConfigFile & k, const ConfigFile::Source & sr, SimpleParser & parser, std::string & result)
    {
        while (true)
        {
            if (parser.eof())
                throw ConfigFileError(sr.filename(), "Unterminated double quote at line " + stringify(parser.current_line_number()));

            std::string s;

            if ((! k.options()[kvcfo_disallow_continuations]) && parser.consume(simple_parser::exact("\\\n")))
            {
                parse_after_continuation(sr, parser, ! k.options()[kvcfo_disallow_comments]);
                continue;
            }
            else if (parser.consume(simple_parser::exact("\\t")))
                result.append("\t");
            else if (parser.consume(simple_parser::exact("\\n")))
                result.append("\n");
            else if (parser.consume(simple_parser::exact("\\") & simple_parser::any_except("") >> s))
                result.append(s);
            else if ((! k.options()[kvcfo_disallow_variables]) && parser.consume(simple_parser::exact("$")))
            {
                std::string var;
                if (! parse_variable(k, sr, parser, var))
                    throw ConfigFileError(sr.filename(), "Bad variable at line " + stringify(parser.current_line_number()));
                result.append(k.get(var));
            }
            else if (parser.consume(simple_parser::exact("\"")))
                break;
            else if (parser.consume((simple_parser::any_except("") & *simple_parser::any_except("\\\"$\t\n")) >> s))
                result.append(s);
            else
                throw ConfigFileError(sr.filename(), "Can't parse double quoted string at line " + stringify(parser.current_line_number()));
        }

        return true;
    }

    bool parse_variable(const KeyValueConfigFile &, const ConfigFile::Source &, SimpleParser & parser, std::string & var)
    {
        const std::string var_name_chars(
                "abcdefghijklmnopqrstuvwxyz"
                "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                "0123456789_"
                );
        return
            parser.consume(simple_parser::exact("{") & +simple_parser::any_of(var_name_chars) >> var & simple_parser::exact("}")) ||
            parser.consume(+simple_parser::any_of(var_name_chars) >> var);
    }

    bool parse_unquoted_value(const KeyValueConfigFile & k, const ConfigFile::Source & sr, SimpleParser & parser, std::string & result)
    {
        bool need_single_space_unless_eol(false);
        while (true)
        {
            std::string w;
            if (parser.eof() || parser.lookahead(simple_parser::exact("\n")))
                break;
            else if (parser.consume(+simple_parser::any_of(" \t") >> w))
            {
                if (k.options()[kvcfo_disallow_space_inside_unquoted_values])
                    throw ConfigFileError(sr.filename(), "Not allowed space inside unquoted values at line "
                            + stringify(parser.current_line_number()));
                else if (k.options()[kvcfo_preserve_whitespace])
                    result.append(w);
                else
                    need_single_space_unless_eol = true;
            }
            else if ((! k.options()[kvcfo_allow_inline_comments]) && parser.consume(simple_parser::exact("#")))
            {
                if (! parser.consume(*simple_parser::any_except("\n")))
                    throw InternalError(PALUDIS_HERE, "failed to consume a zero width match");
                break;
            }
            else if ((! k.options()[kvcfo_disallow_variables]) && parser.consume(simple_parser::exact("$")))
            {
                if (need_single_space_unless_eol)
                {
                    result.append(" ");
                    need_single_space_unless_eol = false;
                }

                std::string var;
                if (! parse_variable(k, sr, parser, var))
                    throw ConfigFileError(sr.filename(), "Bad variable at line " + stringify(parser.current_line_number()));
                result.append(k.get(var));
            }
            else if ((! k.options()[kvcfo_disallow_continuations]) && parser.consume(simple_parser::exact("\\\n")))
            {
                parse_after_continuation(sr, parser, ! k.options()[kvcfo_disallow_comments]);
            }
            else if (parser.consume(simple_parser::exact("\\") & simple_parser::any_except("") >> w))
            {
                if (need_single_space_unless_eol)
                {
                    result.append(" ");
                    need_single_space_unless_eol = false;
                }

                result.append(w);
            }
            else if (parser.consume((simple_parser::any_except("") & *simple_parser::any_except("\\\"$#\n\t ")) >> w))
            {
                if (need_single_space_unless_eol)
                {
                    result.append(" ");
                    need_single_space_unless_eol = false;
                }

                result.append(w);
            }
            else
                throw ConfigFileError(sr.filename(), "Can't parse unquoted string at line " + stringify(parser.current_line_number()));
        }

        return true;
    }

    std::string default_from_kv(const KeyValueConfigFile & f, const KeyValueConfigFile &, const std::string & s)
    {
        return f.get(s);
    }
}

KeyValueConfigFile::KeyValueConfigFile(
        const Source & sr,
        const KeyValueConfigFileOptions & o,
        const KeyValueConfigFile::DefaultFunction & f,
        const KeyValueConfigFile::TransformationFunction & i) :
    PrivateImplementationPattern<KeyValueConfigFile>(new Implementation<KeyValueConfigFile>(o, f, i))
{
    Context context("When parsing key=value-based configuration file '" + (sr.filename().empty() ? "?" : sr.filename()) + "':");

    SimpleParser parser(sr.text());
    while (! parser.eof())
    {
        /* is it a comment? */
        if (! _imp->options[kvcfo_disallow_comments])
        {
            if (parser.consume(*simple_parser::any_of(" \t") & simple_parser::exact("#") &
                        *simple_parser::any_except("\n")))
            {
                /* expect newline, but handle eof without final newline */
                if (! parser.consume(simple_parser::exact("\n")))
                {
                    if (parser.eof())
                    {
                        Log::get_instance()->message("key_value_config_file.no_trailing_newline", ll_warning, lc_context)
                            << "No newline at end of file";
                        break;
                    }
                    else
                        throw ConfigFileError(sr.filename(),
                                "Something is very strange at line '" + stringify(parser.current_line_number() + "'"));
                }
                continue;
            }
        }

        if (! parser.consume(*simple_parser::any_of(" \t")))
            throw InternalError(PALUDIS_HERE, "failed to consume a zero width match");

        if (parser.eof())
        {
            Log::get_instance()->message("key_value_config_file.no_trailing_newline", ll_warning, lc_context)
                << "No newline at end of file";
            break;
        }

        /* is it a blank line? */
        if (parser.consume(simple_parser::exact("\n")))
            continue;

        /* is it a comment? */
        if ((! _imp->options[kvcfo_disallow_comments]) && parser.consume(simple_parser::exact("#") &
                    *simple_parser::any_except("\n")))
        {
            if (! parser.consume(simple_parser::exact("\n")))
            {
                if (parser.eof())
                    Log::get_instance()->message("key_value_config_file.no_trailing_newline", ll_warning, lc_context)
                        << "No newline at end of file";
                else
                    throw ConfigFileError(sr.filename(),
                            "Something is very strange at line '" + stringify(parser.current_line_number() + "'"));
            }
            continue;
        }

        /* is it a source command? */
        if ((! _imp->options[kvcfo_disallow_source]) && parser.consume(simple_parser::exact("source") &
                    +simple_parser::any_of(" \t")))
        {
            std::string filename;
            if (! parse_value(*this, sr, parser, filename))
                throw ConfigFileError(sr.filename(), "Unparsable 'source' command in line " + stringify(parser.current_line_number()));

            if (filename.empty())
                throw ConfigFileError(sr.filename(), "Empty filename for 'source' command in line " + stringify(parser.current_line_number()));

            if (! parser.consume(*simple_parser::any_of(" \t")))
                throw InternalError(PALUDIS_HERE, "failed to consume a zero width match");

            if (_imp->options[kvcfo_allow_inline_comments] && parser.consume(simple_parser::exact("#") &
                        *simple_parser::any_except("\n")))
                /* skippity skippity */ ;

            if (! parser.consume(simple_parser::exact("\n")))
            {
                if (parser.eof())
                    Log::get_instance()->message("key_value_config_file.no_trailing_newline", ll_warning, lc_context)
                        << "No newline at end of file";
                else
                    throw ConfigFileError(sr.filename(), "Expected newline after source command in line " + stringify(parser.current_line_number()));
            }

            Context local_context("When following 'source '" + filename + "' statement:");
            KeyValueConfigFile kv(FSEntry(filename), o,
                    std::tr1::bind(&default_from_kv, std::tr1::cref(*this), std::tr1::placeholders::_1, std::tr1::placeholders::_2), i);
            for (KeyValueConfigFile::ConstIterator k(kv.begin()), k_end(kv.end()) ;
                    k != k_end ; ++k)
                _imp->values[k->first] = k->second;


            continue;
        }

        /* ignore export, if appropriate */
        if (_imp->options[kvcfo_ignore_export] && parser.consume(simple_parser::exact("export") &
                    +simple_parser::any_of(" \t")))
        {
        }

        /* is it superman? */
        std::string key, value;

        if (! parser.consume(+simple_parser::any_except(" \t\n$#\"'=\\") >> key))
            throw ConfigFileError(sr.filename(), "Couldn't find a key in line " + stringify(parser.current_line_number()));

        while (! parser.eof())
        {
            if (! _imp->options[kvcfo_disallow_space_around_equals])
                if (! parser.consume(*simple_parser::any_of(" \t")))
                    throw InternalError(PALUDIS_HERE, "failed to consume a zero width match");

            if ((! _imp->options[kvcfo_disallow_continuations]) && parser.consume(simple_parser::exact("\\\n")))
            {
                parse_after_continuation(sr, parser, ! _imp->options[kvcfo_disallow_comments]);
            }
            else
                break;
        }

        if (! parser.consume(simple_parser::exact("=")))
            throw ConfigFileError(sr.filename(), "Expected an = at line " + stringify(parser.current_line_number()));

        while (! parser.eof())
        {
            if (parser.consume(+simple_parser::any_of(" \t")))
                if (_imp->options[kvcfo_disallow_space_around_equals])
                    throw ConfigFileError(sr.filename(), "Space not allowed after = at line " + stringify(parser.current_line_number()));

            if ((! _imp->options[kvcfo_disallow_continuations]) && parser.consume(simple_parser::exact("\\\n")))
            {
                parse_after_continuation(sr, parser, ! _imp->options[kvcfo_disallow_comments]);
            }
            else
                break;
        }

        if (! parse_value(*this, sr, parser, value))
            throw ConfigFileError(sr.filename(), "Couldn't find a value at line " + stringify(parser.current_line_number()));

        while (! parser.eof())
        {
            std::string s;
            if (parser.consume(+simple_parser::any_of(" \t") >> s))
                if (_imp->options[kvcfo_preserve_whitespace])
                    value += s;

            if ((_imp->options[kvcfo_allow_inline_comments]) && parser.consume(
                        simple_parser::exact("#") & *simple_parser::any_except("\n")))
            {
                if (! parser.consume(simple_parser::exact("\n")))
                {
                    if (parser.eof())
                        Log::get_instance()->message("key_value_config_file.no_trailing_newline", ll_warning, lc_context)
                            << "No newline at end of file";
                    else
                        throw ConfigFileError(sr.filename(),
                                "Something is very strange at line '" + stringify(parser.current_line_number() + "'"));
                }
                break;
            }

            if ((! _imp->options[kvcfo_disallow_continuations]) && parser.consume(simple_parser::exact("\\\n")))
            {
                parse_after_continuation(sr, parser, ! _imp->options[kvcfo_disallow_comments]);
            }
            else
                break;
        }

        _imp->values[key] = transformation_function()(*this, key, get(key), value);
    }
}

KeyValueConfigFile::~KeyValueConfigFile()
{
}

KeyValueConfigFile::ConstIterator
KeyValueConfigFile::begin() const
{
    return ConstIterator(_imp->values.begin());
}

KeyValueConfigFile::ConstIterator
KeyValueConfigFile::end() const
{
    return ConstIterator(_imp->values.end());
}

std::string
KeyValueConfigFile::get(const std::string & s) const
{
    std::map<std::string, std::string>::const_iterator f(_imp->values.find(s));
    if (_imp->values.end() == f)
        return _imp->default_function(*this, s);
    else
        return f->second;
}

std::string
KeyValueConfigFile::no_defaults(const KeyValueConfigFile &, const std::string &)
{
    return "";
}

std::string
KeyValueConfigFile::no_transformation(const KeyValueConfigFile &, const std::string &, const std::string &, const std::string & n)
{
    return n;
}

const KeyValueConfigFileOptions &
KeyValueConfigFile::options() const
{
    return _imp->options;
}

const KeyValueConfigFile::DefaultFunction &
KeyValueConfigFile::default_function() const
{
    return _imp->default_function;
}

const KeyValueConfigFile::TransformationFunction &
KeyValueConfigFile::transformation_function() const
{
    return _imp->transformation_function;
}

template class PrivateImplementationPattern<KeyValueConfigFile>;