configuration.cc 32.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// -*- mode: cpp; mode: fold -*-
// Description								/*{{{*/
/* ######################################################################

   Configuration Class
   
   This class provides a configuration file and command line parser
   for a tree-oriented configuration environment. All runtime configuration
   is stored in here.

   This source is placed in the Public Domain, do with it what you will
   It was originally written by Jason Gunthorpe <jgg@debian.org>.
   
   ##################################################################### */
									/*}}}*/
// Include files							/*{{{*/
#include <config.h>

#include <apt-pkg/configuration.h>
#include <apt-pkg/error.h>
#include <apt-pkg/fileutl.h>
#include <apt-pkg/macros.h>
#include <apt-pkg/strutl.h>
24
#include <apt-pkg/string_view.h>
25
26
27
28
29
30
31
32
33

#include <ctype.h>
#include <regex.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
34
#include <array>
35
36
#include <fstream>
#include <iterator>
37
#include <numeric>
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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#include <apti18n.h>

using namespace std;
									/*}}}*/

Configuration *_config = new Configuration;

/* TODO: This config verification shouldn't be using a static variable
   but a Cnf-member – but that would need ABI breaks and stuff and for now
   that really is an apt-dev-only tool, so it isn't that bad that it is
   unusable and allaround a bit strange */
enum class APT_HIDDEN ConfigType { UNDEFINED, INT, BOOL, STRING, STRING_OR_BOOL, STRING_OR_LIST, FILE, DIR, LIST, PROGRAM_PATH = FILE };
APT_HIDDEN std::unordered_map<std::string, ConfigType> apt_known_config {};
static std::string getConfigTypeString(ConfigType const type)		/*{{{*/
{
   switch (type)
   {
      case ConfigType::UNDEFINED: return "UNDEFINED";
      case ConfigType::INT: return "INT";
      case ConfigType::BOOL: return "BOOL";
      case ConfigType::STRING: return "STRING";
      case ConfigType::STRING_OR_BOOL: return "STRING_OR_BOOL";
      case ConfigType::FILE: return "FILE";
      case ConfigType::DIR: return "DIR";
      case ConfigType::LIST: return "LIST";
      case ConfigType::STRING_OR_LIST: return "STRING_OR_LIST";
   }
   return "UNKNOWN";
}
									/*}}}*/
static ConfigType getConfigType(std::string const &type)		/*{{{*/
{
   if (type == "<INT>")
      return ConfigType::INT;
   else if (type == "<BOOL>")
      return ConfigType::BOOL;
   else if (type == "<STRING>")
      return ConfigType::STRING;
   else if (type == "<STRING_OR_BOOL>")
      return ConfigType::STRING_OR_BOOL;
   else if (type == "<FILE>")
      return ConfigType::FILE;
   else if (type == "<DIR>")
      return ConfigType::DIR;
   else if (type == "<LIST>")
      return ConfigType::LIST;
   else if (type == "<STRING_OR_LIST>")
      return ConfigType::STRING_OR_LIST;
   else if (type == "<PROGRAM_PATH>")
      return ConfigType::PROGRAM_PATH;
   return ConfigType::UNDEFINED;
}
									/*}}}*/
// checkFindConfigOptionType - workhorse of option checking		/*{{{*/
static void checkFindConfigOptionTypeInternal(std::string name, ConfigType const type)
{
   std::transform(name.begin(), name.end(), name.begin(), ::tolower);
   auto known = apt_known_config.find(name);
   if (known == apt_known_config.cend())
   {
      auto const rcolon = name.rfind(':');
      if (rcolon != std::string::npos)
      {
	 known = apt_known_config.find(name.substr(0, rcolon) + ":*");
	 if (known == apt_known_config.cend())
	 {
	    auto const parts = StringSplit(name, "::");
	    size_t psize = parts.size();
	    if (psize > 1)
	    {
	       for (size_t max = psize; max != 1; --max)
	       {
		  std::ostringstream os;
		  std::copy(parts.begin(), parts.begin() + max, std::ostream_iterator<std::string>(os, "::"));
		  os << "**";
		  known = apt_known_config.find(os.str());
		  if (known != apt_known_config.cend() && known->second == ConfigType::UNDEFINED)
		     return;
	       }
	       for (size_t max = psize - 1; max != 1; --max)
	       {
		  std::ostringstream os;
		  std::copy(parts.begin(), parts.begin() + max - 1, std::ostream_iterator<std::string>(os, "::"));
		  os << "*::";
		  std::copy(parts.begin() + max + 1, parts.end() - 1, std::ostream_iterator<std::string>(os, "::"));
		  os << *(parts.end() - 1);
		  known = apt_known_config.find(os.str());
		  if (known != apt_known_config.cend())
		     break;
	       }
	    }
	 }
      }
   }
   if (known == apt_known_config.cend())
      _error->Warning("Using unknown config option »%s« of type %s",
	    name.c_str(), getConfigTypeString(type).c_str());
   else if (known->second != type)
   {
      if (known->second == ConfigType::DIR && type == ConfigType::FILE)
	 ; // implementation detail
      else if (type == ConfigType::STRING && (known->second == ConfigType::FILE || known->second == ConfigType::DIR))
	 ; // TODO: that might be an error or not, we will figure this out later
      else if (known->second == ConfigType::STRING_OR_BOOL && (type == ConfigType::BOOL || type == ConfigType::STRING))
	 ;
      else if (known->second == ConfigType::STRING_OR_LIST && (type == ConfigType::LIST || type == ConfigType::STRING))
	 ;
      else
	 _error->Warning("Using config option »%s« of type %s as a type %s",
	       name.c_str(), getConfigTypeString(known->second).c_str(), getConfigTypeString(type).c_str());
   }
}
static void checkFindConfigOptionType(char const * const name, ConfigType const type)
{
   if (apt_known_config.empty())
      return;
   checkFindConfigOptionTypeInternal(name, type);
}
									/*}}}*/
static bool LoadConfigurationIndex(std::string const &filename)		/*{{{*/
{
   apt_known_config.clear();
   if (filename.empty())
      return true;
   Configuration Idx;
   if (ReadConfigFile(Idx, filename) == false)
      return false;

   Configuration::Item const * Top = Idx.Tree(nullptr);
   if (unlikely(Top == nullptr))
      return false;

   do {
      if (Top->Value.empty() == false)
      {
	 std::string fulltag = Top->FullTag();
	 std::transform(fulltag.begin(), fulltag.end(), fulltag.begin(), ::tolower);
	 apt_known_config.emplace(std::move(fulltag), getConfigType(Top->Value));
      }

      if (Top->Child != nullptr)
      {
	 Top = Top->Child;
	 continue;
      }

      while (Top != nullptr && Top->Next == nullptr)
	 Top = Top->Parent;
      if (Top != nullptr)
	 Top = Top->Next;
   } while (Top != nullptr);

   return true;
}
									/*}}}*/

// Configuration::Configuration - Constructor				/*{{{*/
// ---------------------------------------------------------------------
/* */
Configuration::Configuration() : ToFree(true)
{
   Root = new Item;
}
Configuration::Configuration(const Item *Root) : Root((Item *)Root), ToFree(false)
{
}
									/*}}}*/
// Configuration::~Configuration - Destructor				/*{{{*/
// ---------------------------------------------------------------------
/* */
Configuration::~Configuration()
{
   if (ToFree == false)
      return;
   
   Item *Top = Root;
   for (; Top != 0;)
   {
      if (Top->Child != 0)
      {
	 Top = Top->Child;
	 continue;
      }
            
      while (Top != 0 && Top->Next == 0)
      {
	 Item *Parent = Top->Parent;
	 delete Top;
	 Top = Parent;
      }      
      if (Top != 0)
      {
	 Item *Next = Top->Next;
	 delete Top;
	 Top = Next;
      }
   }
}
									/*}}}*/
// Configuration::Lookup - Lookup a single item				/*{{{*/
// ---------------------------------------------------------------------
/* This will lookup a single item by name below another item. It is a 
   helper function for the main lookup function */
Configuration::Item *Configuration::Lookup(Item *Head,const char *S,
					   unsigned long const &Len,bool const &Create)
{
   int Res = 1;
   Item *I = Head->Child;
   Item **Last = &Head->Child;
   
   // Empty strings match nothing. They are used for lists.
   if (Len != 0)
   {
      for (; I != 0; Last = &I->Next, I = I->Next)
	 if (Len == I->Tag.length() && (Res = stringcasecmp(I->Tag,S,S + Len)) == 0)
	    break;
   }
   else
      for (; I != 0; Last = &I->Next, I = I->Next);
      
   if (Res == 0)
      return I;
   if (Create == false)
      return 0;
   
   I = new Item;
   I->Tag.assign(S,Len);
   I->Next = *Last;
   I->Parent = Head;
   *Last = I;
   return I;
}
									/*}}}*/
// Configuration::Lookup - Lookup a fully scoped item			/*{{{*/
// ---------------------------------------------------------------------
/* This performs a fully scoped lookup of a given name, possibly creating
   new items */
Configuration::Item *Configuration::Lookup(const char *Name,bool const &Create)
{
   if (Name == 0)
      return Root->Child;
   
   const char *Start = Name;
   const char *End = Start + strlen(Name);
   const char *TagEnd = Name;
   Item *Itm = Root;
   for (; End - TagEnd >= 2; TagEnd++)
   {
      if (TagEnd[0] == ':' && TagEnd[1] == ':')
      {
	 Itm = Lookup(Itm,Start,TagEnd - Start,Create);
	 if (Itm == 0)
	    return 0;
	 TagEnd = Start = TagEnd + 2;	 
      }
   }   

   // This must be a trailing ::, we create unique items in a list
   if (End - Start == 0)
   {
      if (Create == false)
	 return 0;
   }
   
   Itm = Lookup(Itm,Start,End - Start,Create);
   return Itm;
}
									/*}}}*/
// Configuration::Find - Find a value					/*{{{*/
// ---------------------------------------------------------------------
/* */
string Configuration::Find(const char *Name,const char *Default) const
{
   checkFindConfigOptionType(Name, ConfigType::STRING);
   const Item *Itm = Lookup(Name);
   if (Itm == 0 || Itm->Value.empty() == true)
   {
      if (Default == 0)
	 return "";
      else
	 return Default;
   }
   
   return Itm->Value;
}
									/*}}}*/
// Configuration::FindFile - Find a Filename				/*{{{*/
// ---------------------------------------------------------------------
/* Directories are stored as the base dir in the Parent node and the
   sub directory in sub nodes with the final node being the end filename
 */
string Configuration::FindFile(const char *Name,const char *Default) const
{
   checkFindConfigOptionType(Name, ConfigType::FILE);
   const Item *RootItem = Lookup("RootDir");
   std::string result =  (RootItem == 0) ? "" : RootItem->Value;
   if(result.empty() == false && result[result.size() - 1] != '/')
     result.push_back('/');

   const Item *Itm = Lookup(Name);
   if (Itm == 0 || Itm->Value.empty() == true)
   {
      if (Default != 0)
	 result.append(Default);
   }
   else
   {
      string val = Itm->Value;
      while (Itm->Parent != 0)
      {
	 if (Itm->Parent->Value.empty() == true)
	 {
	    Itm = Itm->Parent;
	    continue;
	 }

	 // Absolute
	 if (val.length() >= 1 && val[0] == '/')
	 {
	    if (val.compare(0, 9, "/dev/null") == 0)
	       val.erase(9);
	    break;
	 }

	 // ~/foo or ./foo
	 if (val.length() >= 2 && (val[0] == '~' || val[0] == '.') && val[1] == '/')
	    break;

	 // ../foo
	 if (val.length() >= 3 && val[0] == '.' && val[1] == '.' && val[2] == '/')
	    break;

	 if (Itm->Parent->Value.end()[-1] != '/')
	    val.insert(0, "/");

	 val.insert(0, Itm->Parent->Value);
	 Itm = Itm->Parent;
      }
      result.append(val);
   }
   return flNormalize(result);
}
									/*}}}*/
// Configuration::FindDir - Find a directory name			/*{{{*/
// ---------------------------------------------------------------------
/* This is like findfile execept the result is terminated in a / */
string Configuration::FindDir(const char *Name,const char *Default) const
{
   checkFindConfigOptionType(Name, ConfigType::DIR);
   string Res = FindFile(Name,Default);
   if (Res.end()[-1] != '/')
   {
      size_t const found = Res.rfind("/dev/null");
      if (found != string::npos && found == Res.size() - 9)
	 return Res; // /dev/null returning
      return Res + '/';
   }
   return Res;
}
									/*}}}*/
// Configuration::FindVector - Find a vector of values			/*{{{*/
// ---------------------------------------------------------------------
/* Returns a vector of config values under the given item */
vector<string> Configuration::FindVector(const char *Name, std::string const &Default, bool const Keys) const
{
   checkFindConfigOptionType(Name, ConfigType::LIST);
   vector<string> Vec;
   const Item *Top = Lookup(Name);
   if (Top == NULL)
      return VectorizeString(Default, ',');

   if (Top->Value.empty() == false)
      return VectorizeString(Top->Value, ',');

   Item *I = Top->Child;
   while(I != NULL)
   {
      Vec.push_back(Keys ? I->Tag : I->Value);
      I = I->Next;
   }
   if (Vec.empty() == true)
      return VectorizeString(Default, ',');

   return Vec;
}
									/*}}}*/
// Configuration::FindI - Find an integer value				/*{{{*/
// ---------------------------------------------------------------------
/* */
int Configuration::FindI(const char *Name,int const &Default) const
{
   checkFindConfigOptionType(Name, ConfigType::INT);
   const Item *Itm = Lookup(Name);
   if (Itm == 0 || Itm->Value.empty() == true)
      return Default;
   
   char *End;
   int Res = strtol(Itm->Value.c_str(),&End,0);
   if (End == Itm->Value.c_str())
      return Default;
   
   return Res;
}
									/*}}}*/
// Configuration::FindB - Find a boolean type				/*{{{*/
// ---------------------------------------------------------------------
/* */
bool Configuration::FindB(const char *Name,bool const &Default) const
{
   checkFindConfigOptionType(Name, ConfigType::BOOL);
   const Item *Itm = Lookup(Name);
   if (Itm == 0 || Itm->Value.empty() == true)
      return Default;
   
   return StringToBool(Itm->Value,Default);
}
									/*}}}*/
// Configuration::FindAny - Find an arbitrary type			/*{{{*/
// ---------------------------------------------------------------------
/* a key suffix of /f, /d, /b or /i calls Find{File,Dir,B,I} */
string Configuration::FindAny(const char *Name,const char *Default) const
{
   string key = Name;
   char type = 0;

   if (key.size() > 2 && key.end()[-2] == '/')
   {
      type = key.end()[-1];
      key.resize(key.size() - 2);
   }

   switch (type)
   {
      // file
      case 'f': 
      return FindFile(key.c_str(), Default);
      
      // directory
      case 'd': 
      return FindDir(key.c_str(), Default);
      
      // bool
      case 'b': 
      return FindB(key, Default) ? "true" : "false";
      
      // int
      case 'i': 
      {
	 char buf[16];
	 snprintf(buf, sizeof(buf)-1, "%d", FindI(key, Default ? atoi(Default) : 0 ));
	 return buf;
      }
   }

   // fallback
   return Find(Name, Default);
}
									/*}}}*/
// Configuration::CndSet - Conditinal Set a value			/*{{{*/
// ---------------------------------------------------------------------
/* This will not overwrite */
void Configuration::CndSet(const char *Name,const string &Value)
{
   Item *Itm = Lookup(Name,true);
   if (Itm == 0)
      return;
   if (Itm->Value.empty() == true)
      Itm->Value = Value;
}
									/*}}}*/
// Configuration::Set - Set an integer value				/*{{{*/
// ---------------------------------------------------------------------
/* */
void Configuration::CndSet(const char *Name,int const Value)
{
   Item *Itm = Lookup(Name,true);
   if (Itm == 0 || Itm->Value.empty() == false)
      return;
   char S[300];
   snprintf(S,sizeof(S),"%i",Value);
   Itm->Value = S;
}
									/*}}}*/
// Configuration::Set - Set a value					/*{{{*/
// ---------------------------------------------------------------------
/* */
void Configuration::Set(const char *Name,const string &Value)
{
   Item *Itm = Lookup(Name,true);
   if (Itm == 0)
      return;
   Itm->Value = Value;
}
									/*}}}*/
// Configuration::Set - Set an integer value				/*{{{*/
// ---------------------------------------------------------------------
/* */
void Configuration::Set(const char *Name,int const &Value)
{
   Item *Itm = Lookup(Name,true);
   if (Itm == 0)
      return;
   char S[300];
   snprintf(S,sizeof(S),"%i",Value);
   Itm->Value = S;
}
									/*}}}*/
// Configuration::Clear - Clear an single value from a list	        /*{{{*/
// ---------------------------------------------------------------------
/* */
void Configuration::Clear(string const &Name, int const &Value)
{
   char S[300];
   snprintf(S,sizeof(S),"%i",Value);
   Clear(Name, S);
}
									/*}}}*/
// Configuration::Clear - Clear an single value from a list	        /*{{{*/
// ---------------------------------------------------------------------
/* */
void Configuration::Clear(string const &Name, string const &Value)
{
   Item *Top = Lookup(Name.c_str(),false);
   if (Top == 0 || Top->Child == 0)
      return;

   Item *Tmp, *Prev, *I;
   Prev = I = Top->Child;

   while(I != NULL)
   {
      if(I->Value == Value)
      {
	 Tmp = I;
	 // was first element, point parent to new first element
	 if(Top->Child == Tmp)
	    Top->Child = I->Next;
	 I = I->Next;
	 Prev->Next = I;
	 delete Tmp;
      } else {
	 Prev = I;
	 I = I->Next;
      }
   }
     
}
									/*}}}*/
// Configuration::Clear - Clear everything				/*{{{*/
// ---------------------------------------------------------------------
void Configuration::Clear()
{
   const Configuration::Item *Top = Tree(0);
   while( Top != 0 )
   {
      Clear(Top->FullTag());
      Top = Top->Next;
   }
}
									/*}}}*/
// Configuration::Clear - Clear an entire tree				/*{{{*/
// ---------------------------------------------------------------------
/* */
void Configuration::Clear(string const &Name)
{
   Item *Top = Lookup(Name.c_str(),false);
   if (Top == 0) 
      return;

   Top->Value.clear();
   Item *Stop = Top;
   Top = Top->Child;
   Stop->Child = 0;
   for (; Top != 0;)
   {
      if (Top->Child != 0)
      {
	 Top = Top->Child;
	 continue;
      }

      while (Top != 0 && Top->Next == 0)
      {
	 Item *Tmp = Top;
	 Top = Top->Parent;
	 delete Tmp;
	 
	 if (Top == Stop)
	    return;
      }
      
      Item *Tmp = Top;
      if (Top != 0)
	 Top = Top->Next;
      delete Tmp;
   }
}
									/*}}}*/
void Configuration::MoveSubTree(char const * const OldRootName, char const * const NewRootName)/*{{{*/
{
   // prevent NewRoot being a subtree of OldRoot
   if (OldRootName == nullptr)
      return;
   if (NewRootName != nullptr)
   {
      if (strcmp(OldRootName, NewRootName) == 0)
	 return;
      std::string const oldroot = std::string(OldRootName) + "::";
      if (strcasestr(NewRootName, oldroot.c_str()) != NULL)
	 return;
   }

   Item * Top;
   Item const * const OldRoot = Top = Lookup(OldRootName, false);
   if (Top == nullptr)
      return;
   std::string NewRoot;
   if (NewRootName != nullptr)
      NewRoot.append(NewRootName).append("::");

   Top->Value.clear();
   Item * const Stop = Top;
   Top = Top->Child;
   Stop->Child = 0;
   for (; Top != 0;)
   {
      if (Top->Child != 0)
      {
	 Top = Top->Child;
	 continue;
      }

      while (Top != 0 && Top->Next == 0)
      {
	 Set(NewRoot + Top->FullTag(OldRoot), Top->Value);
	 Item const * const Tmp = Top;
	 Top = Top->Parent;
	 delete Tmp;

	 if (Top == Stop)
	    return;
      }

      Set(NewRoot + Top->FullTag(OldRoot), Top->Value);
      Item const * const Tmp = Top;
      if (Top != 0)
	 Top = Top->Next;
      delete Tmp;
   }
}
									/*}}}*/
// Configuration::Exists - Returns true if the Name exists		/*{{{*/
// ---------------------------------------------------------------------
/* */
bool Configuration::Exists(const char *Name) const
{
   const Item *Itm = Lookup(Name);
   if (Itm == 0)
      return false;
   return true;
}
									/*}}}*/
// Configuration::ExistsAny - Returns true if the Name, possibly	/*{{{*/
// ---------------------------------------------------------------------
/* qualified by /[fdbi] exists */
bool Configuration::ExistsAny(const char *Name) const
{
   string key = Name;

   if (key.size() > 2 && key.end()[-2] == '/')
   {
      if (key.find_first_of("fdbi",key.size()-1) < key.size())
      {
         key.resize(key.size() - 2);
         if (Exists(key.c_str()))
            return true;
      }
      else
      {
         _error->Warning(_("Unrecognized type abbreviation: '%c'"), key.end()[-3]);
      }
   }
   return Exists(Name);
}
									/*}}}*/
// Configuration::Dump - Dump the config				/*{{{*/
// ---------------------------------------------------------------------
/* Dump the entire configuration space */
void Configuration::Dump(ostream& str)
{
   Dump(str, NULL, "%F \"%v\";\n", true);
}
void Configuration::Dump(ostream& str, char const * const root,
			 char const * const formatstr, bool const emptyValue)
{
   const Configuration::Item* Top = Tree(root);
   if (Top == 0)
      return;
   const Configuration::Item* const Root = (root == NULL) ? NULL : Top;
   std::vector<std::string> const format = VectorizeString(formatstr, '%');

   /* Write out all of the configuration directives by walking the
      configuration tree */
   do {
      if (emptyValue == true || Top->Value.empty() == emptyValue)
      {
	 std::vector<std::string>::const_iterator f = format.begin();
	 str << *f;
	 for (++f; f != format.end(); ++f)
	 {
	    if (f->empty() == true)
	    {
	       ++f;
	       str << '%' << *f;
	       continue;
	    }
	    char const type = (*f)[0];
	    if (type == 'f')
	       str << Top->FullTag();
	    else if (type == 't')
	       str << Top->Tag;
	    else if (type == 'v')
	       str << Top->Value;
	    else if (type == 'F')
	       str << QuoteString(Top->FullTag(), "=\"\n");
	    else if (type == 'T')
	       str << QuoteString(Top->Tag, "=\"\n");
	    else if (type == 'V')
	       str << QuoteString(Top->Value, "=\"\n");
	    else if (type == 'n')
	       str << "\n";
	    else if (type == 'N')
	       str << "\t";
	    else
	       str << '%' << type;
	    str << f->c_str() + 1;
	 }
      }

      if (Top->Child != 0)
      {
	 Top = Top->Child;
	 continue;
      }

      while (Top != 0 && Top->Next == 0)
	 Top = Top->Parent;
      if (Top != 0)
	 Top = Top->Next;

      if (Root != NULL)
      {
	 const Configuration::Item* I = Top;
	 while(I != 0)
	 {
	    if (I == Root)
	       break;
	    else
	       I = I->Parent;
	 }
	 if (I == 0)
	    break;
      }
   } while (Top != 0);
}
									/*}}}*/

// Configuration::Item::FullTag - Return the fully scoped tag		/*{{{*/
// ---------------------------------------------------------------------
/* Stop sets an optional max recursion depth if this item is being viewed as
   part of a sub tree. */
string Configuration::Item::FullTag(const Item *Stop) const
{
   if (Parent == 0 || Parent->Parent == 0 || Parent == Stop)
      return Tag;
   return Parent->FullTag(Stop) + "::" + Tag;
}
									/*}}}*/

// ReadConfigFile - Read a configuration file				/*{{{*/
// ---------------------------------------------------------------------
/* The configuration format is very much like the named.conf format
   used in bind8, in fact this routine can parse most named.conf files. 
   Sectional config files are like bind's named.conf where there are 
   sections like 'zone "foo.org" { .. };' This causes each section to be
   added in with a tag like "zone::foo.org" instead of being split 
   tag/value. AsSectional enables Sectional parsing.*/
static void leaveCurrentScope(std::stack<std::string> &Stack, std::string &ParentTag)
{
   if (Stack.empty())
      ParentTag.clear();
   else
   {
      ParentTag = Stack.top();
      Stack.pop();
   }
}
bool ReadConfigFile(Configuration &Conf,const string &FName,bool const &AsSectional,
		    unsigned const &Depth)
{
   // Open the stream for reading
   FileFd F;
   if (OpenConfigurationFileFd(FName, F) == false)
      return false;

   string LineBuffer;
   std::stack<std::string> Stack;

   // Parser state
   string ParentTag;

   int CurLine = 0;
   bool InComment = false;
   while (F.Eof() == false)
   {
      // The raw input line.
      std::string Input;
      if (F.ReadLine(Input) == false)
	 Input.clear();
      // The input line with comments stripped.
      std::string Fragment;

866
867
      // Expand tabs in the input line and remove leading and trailing whitespace.
      Input = APT::String::Strip(SubstVar(Input, "\t", "        "));
868
869
870
871
      CurLine++;

      // Now strip comments; if the whole line is contained in a
      // comment, skip this line.
872
      APT::StringView Line{Input.data(), Input.size()};
873

874
875
      // continued Multi line comment
      if (InComment)
876
      {
877
878
	 size_t end = Line.find("*/");
	 if (end != APT::StringView::npos)
879
	 {
880
881
	    Line.remove_prefix(end + 2);
	    InComment = false;
882
	 }
883
	 else
884
885
886
887
888
	    continue;
      }

      // Discard single line comments
      {
889
890
	 size_t start = 0;
	 while ((start = Line.find("//", start)) != APT::StringView::npos)
891
	 {
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
	    if (std::count(Line.begin(), Line.begin() + start, '"') % 2 != 0)
	    {
	       ++start;
	       continue;
	    }
	    Line.remove_suffix(Line.length() - start);
	    break;
	 }
	 using APT::operator""_sv;
	 constexpr std::array<APT::StringView, 3> magicComments { "clear"_sv, "include"_sv, "x-apt-configure-index"_sv };
	 start = 0;
	 while ((start = Line.find('#', start)) != APT::StringView::npos)
	 {
	    if (std::count(Line.begin(), Line.begin() + start, '"') % 2 != 0 ||
		std::any_of(magicComments.begin(), magicComments.end(), [&](auto const m) { return Line.compare(start+1, m.length(), m) == 0; }))
	    {
	       ++start;
	       continue;
	    }
	    Line.remove_suffix(Line.length() - start);
912
913
914
915
916
917
	    break;
	 }
      }

      // Look for multi line comments and build up the
      // fragment.
918
      Fragment.reserve(Line.length());
919
      {
920
921
922
923
	 size_t start = 0;
	 while ((start = Line.find("/*", start)) != APT::StringView::npos)
	 {
	    if (std::count(Line.begin(), Line.begin() + start, '"') % 2 != 0)
924
	    {
925
926
	       start += 2;
	       continue;
927
	    }
928
929
930
931
932
933
934
935
936
937
938
	    Fragment.append(Line.data(), start);
	    auto const end = Line.find("*/", start + 2);
	    if (end == APT::StringView::npos)
	    {
	       Line.clear();
	       InComment = true;
	       break;
	    }
	    else
	       Line.remove_prefix(end + 2);
	    start = 0;
939
	 }
940
941
	 if (not Line.empty())
	    Fragment.append(Line.data(), Line.length());
942
943
944
945
946
947
948
      }

      // Skip blank lines.
      if (Fragment.empty())
	 continue;

      // The line has actual content; interpret what it means.
949
950
951
      bool InQuote = false;
      auto Start = Fragment.cbegin();
      auto End = Fragment.cend();
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
      for (std::string::const_iterator I = Start;
	   I != End; ++I)
      {
	 if (*I == '"')
	    InQuote = !InQuote;

	 if (InQuote == false && (*I == '{' || *I == ';' || *I == '}'))
	 {
	    // Put the last fragment into the buffer
	    std::string::const_iterator NonWhitespaceStart = Start;
	    std::string::const_iterator NonWhitespaceStop = I;
	    for (; NonWhitespaceStart != I && isspace(*NonWhitespaceStart) != 0; ++NonWhitespaceStart)
	      ;
	    for (; NonWhitespaceStop != NonWhitespaceStart && isspace(NonWhitespaceStop[-1]) != 0; --NonWhitespaceStop)
	      ;
	    if (LineBuffer.empty() == false && NonWhitespaceStop - NonWhitespaceStart != 0)
	       LineBuffer += ' ';
	    LineBuffer += string(NonWhitespaceStart, NonWhitespaceStop);

	    // Drop this from the input string, saving the character
	    // that terminated the construct we just closed. (i.e., a
	    // brace or a semicolon)
	    char TermChar = *I;
	    Start = I + 1;

	    // Syntax Error
	    if (TermChar == '{' && LineBuffer.empty() == true)
	       return _error->Error(_("Syntax error %s:%u: Block starts with no name."),FName.c_str(),CurLine);

	    // No string on this line
	    if (LineBuffer.empty() == true)
	    {
	       if (TermChar == '}')
		  leaveCurrentScope(Stack, ParentTag);
	       continue;
	    }

	    // Parse off the tag
	    string Tag;
	    const char *Pos = LineBuffer.c_str();
	    if (ParseQuoteWord(Pos,Tag) == false)
	       return _error->Error(_("Syntax error %s:%u: Malformed tag"),FName.c_str(),CurLine);

	    // Parse off the word
	    string Word;
	    bool NoWord = false;
	    if (ParseCWord(Pos,Word) == false &&
		ParseQuoteWord(Pos,Word) == false)
	    {
	       if (TermChar != '{')
	       {
		  Word = Tag;
		  Tag = "";
	       }
	       else
		  NoWord = true;
	    }
	    if (strlen(Pos) != 0)
	       return _error->Error(_("Syntax error %s:%u: Extra junk after value"),FName.c_str(),CurLine);

	    // Go down a level
	    if (TermChar == '{')
	    {
	       Stack.push(ParentTag);

	       /* Make sectional tags incorporate the section into the
	          tag string */
	       if (AsSectional == true && Word.empty() == false)
	       {
		  Tag.append("::").append(Word);
		  Word.clear();
	       }

	       if (ParentTag.empty() == true)
		  ParentTag = Tag;
	       else
		  ParentTag.append("::").append(Tag);
	       Tag.clear();
	    }

	    // Generate the item name
	    string Item;
	    if (ParentTag.empty() == true)
	       Item = Tag;
	    else
	    {
	       if (TermChar != '{' || Tag.empty() == false)
		  Item = ParentTag + "::" + Tag;
	       else
		  Item = ParentTag;
	    }

	    // Specials
	    if (Tag.length() >= 1 && Tag[0] == '#')
	    {
	       if (ParentTag.empty() == false)
		  return _error->Error(_("Syntax error %s:%u: Directives can only be done at the top level"),FName.c_str(),CurLine);
	       Tag.erase(Tag.begin());
	       if (Tag == "clear")
		  Conf.Clear(Word);
	       else if (Tag == "include")
	       {
		  if (Depth > 10)
		     return _error->Error(_("Syntax error %s:%u: Too many nested includes"),FName.c_str(),CurLine);
		  if (Word.length() > 2 && Word.end()[-1] == '/')
		  {
		     if (ReadConfigDir(Conf,Word,AsSectional,Depth+1) == false)
			return _error->Error(_("Syntax error %s:%u: Included from here"),FName.c_str(),CurLine);
		  }
		  else
		  {
		     if (ReadConfigFile(Conf,Word,AsSectional,Depth+1) == false)
			return _error->Error(_("Syntax error %s:%u: Included from here"),FName.c_str(),CurLine);
		  }
	       }
	       else if (Tag == "x-apt-configure-index")
	       {
		  if (LoadConfigurationIndex(Word) == false)
		     return _error->Warning("Loading the configure index %s in file %s:%u failed!", Word.c_str(), FName.c_str(), CurLine);
	       }
	       else
		  return _error->Error(_("Syntax error %s:%u: Unsupported directive '%s'"),FName.c_str(),CurLine,Tag.c_str());
	    }
	    else if (Tag.empty() == true && NoWord == false && Word == "#clear")
	       return _error->Error(_("Syntax error %s:%u: clear directive requires an option tree as argument"),FName.c_str(),CurLine);
	    else
	    {
	       // Set the item in the configuration class
	       if (NoWord == false)
		  Conf.Set(Item,Word);
	    }

	    // Empty the buffer
	    LineBuffer.clear();

	    // Move up a tag, but only if there is no bit to parse
	    if (TermChar == '}')
	       leaveCurrentScope(Stack, ParentTag);
	 }
      }

      // Store the remaining text, if any, in the current line buffer.

      // NB: could change this to use string-based operations; I'm
      // using strstrip now to ensure backwards compatibility.
      //   -- dburrows 2008-04-01
      {
	char *Buffer = new char[End - Start + 1];
	try
	  {
	    std::copy(Start, End, Buffer);
	    Buffer[End - Start] = '\0';

	    const char *Stripd = _strstrip(Buffer);
	    if (*Stripd != 0 && LineBuffer.empty() == false)
	      LineBuffer += " ";
	    LineBuffer += Stripd;
	  }
	catch(...)
	  {
	    delete[] Buffer;
	    throw;
	  }
	delete[] Buffer;
      }
   }

   if (LineBuffer.empty() == false)
      return _error->Error(_("Syntax error %s:%u: Extra junk at end of file"),FName.c_str(),CurLine);
   return true;
}
									/*}}}*/
// ReadConfigDir - Read a directory of config files			/*{{{*/
// ---------------------------------------------------------------------
/* */
bool ReadConfigDir(Configuration &Conf,const string &Dir,
		   bool const &AsSectional, unsigned const &Depth)
{
1130
   _error->PushToStack();
1131
   auto const files = GetListOfFilesInDir(Dir, "conf", true, true);
1132
1133
   auto const successfulList = not _error->PendingError();
   _error->MergeWithStack();
1134
1135
   return std::accumulate(files.cbegin(), files.cend(), true, [&](bool good, auto const &file) {
      return ReadConfigFile(Conf, file, AsSectional, Depth) && good;
1136
   }) && successfulList;
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
}
									/*}}}*/
// MatchAgainstConfig Constructor					/*{{{*/
Configuration::MatchAgainstConfig::MatchAgainstConfig(char const * Config)
{
   std::vector<std::string> const strings = _config->FindVector(Config);
   for (std::vector<std::string>::const_iterator s = strings.begin();
	s != strings.end(); ++s)
   {
      regex_t *p = new regex_t;
      if (regcomp(p, s->c_str(), REG_EXTENDED | REG_ICASE | REG_NOSUB) == 0)
	 patterns.push_back(p);
      else
      {
	 regfree(p);
	 delete p;
	 _error->Warning("Invalid regular expression '%s' in configuration "
                         "option '%s' will be ignored.",
                         s->c_str(), Config);
	 continue;
      }
   }
   if (strings.empty() == true)
      patterns.push_back(NULL);
}
									/*}}}*/
// MatchAgainstConfig Destructor					/*{{{*/
Configuration::MatchAgainstConfig::~MatchAgainstConfig()
{
   clearPatterns();
}
void Configuration::MatchAgainstConfig::clearPatterns()
{
   for(std::vector<regex_t *>::const_iterator p = patterns.begin();
	p != patterns.end(); ++p)
   {
      if (*p == NULL) continue;
      regfree(*p);
      delete *p;
   }
   patterns.clear();
}
									/*}}}*/
// MatchAgainstConfig::Match - returns true if a pattern matches	/*{{{*/
bool Configuration::MatchAgainstConfig::Match(char const * str) const
{
   for(std::vector<regex_t *>::const_iterator p = patterns.begin();
	p != patterns.end(); ++p)
      if (*p != NULL && regexec(*p, str, 0, 0, 0) == 0)
	 return true;

   return false;
}
									/*}}}*/