configuration.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: configuration.cc,v 1.28 2004/04/30 04:00:15 mdz Exp $
  4. /* ######################################################################
  5. Configuration Class
  6. This class provides a configuration file and command line parser
  7. for a tree-oriented configuration environment. All runtime configuration
  8. is stored in here.
  9. This source is placed in the Public Domain, do with it what you will
  10. It was originally written by Jason Gunthorpe <jgg@debian.org>.
  11. ##################################################################### */
  12. /*}}}*/
  13. // Include files /*{{{*/
  14. #include <apt-pkg/configuration.h>
  15. #include <apt-pkg/error.h>
  16. #include <apt-pkg/strutl.h>
  17. #include <apt-pkg/fileutl.h>
  18. #include <apti18n.h>
  19. #include <vector>
  20. #include <algorithm>
  21. #include <fstream>
  22. #include <iostream>
  23. #include <stdio.h>
  24. #include <dirent.h>
  25. #include <sys/stat.h>
  26. #include <unistd.h>
  27. using namespace std;
  28. /*}}}*/
  29. Configuration *_config = new Configuration;
  30. // Configuration::Configuration - Constructor /*{{{*/
  31. // ---------------------------------------------------------------------
  32. /* */
  33. Configuration::Configuration() : ToFree(true)
  34. {
  35. Root = new Item;
  36. }
  37. Configuration::Configuration(const Item *Root) : Root((Item *)Root), ToFree(false)
  38. {
  39. };
  40. /*}}}*/
  41. // Configuration::~Configuration - Destructor /*{{{*/
  42. // ---------------------------------------------------------------------
  43. /* */
  44. Configuration::~Configuration()
  45. {
  46. if (ToFree == false)
  47. return;
  48. Item *Top = Root;
  49. for (; Top != 0;)
  50. {
  51. if (Top->Child != 0)
  52. {
  53. Top = Top->Child;
  54. continue;
  55. }
  56. while (Top != 0 && Top->Next == 0)
  57. {
  58. Item *Parent = Top->Parent;
  59. delete Top;
  60. Top = Parent;
  61. }
  62. if (Top != 0)
  63. {
  64. Item *Next = Top->Next;
  65. delete Top;
  66. Top = Next;
  67. }
  68. }
  69. }
  70. /*}}}*/
  71. // Configuration::Lookup - Lookup a single item /*{{{*/
  72. // ---------------------------------------------------------------------
  73. /* This will lookup a single item by name below another item. It is a
  74. helper function for the main lookup function */
  75. Configuration::Item *Configuration::Lookup(Item *Head,const char *S,
  76. unsigned long Len,bool Create)
  77. {
  78. int Res = 1;
  79. Item *I = Head->Child;
  80. Item **Last = &Head->Child;
  81. // Empty strings match nothing. They are used for lists.
  82. if (Len != 0)
  83. {
  84. for (; I != 0; Last = &I->Next, I = I->Next)
  85. if ((Res = stringcasecmp(I->Tag,S,S + Len)) == 0)
  86. break;
  87. }
  88. else
  89. for (; I != 0; Last = &I->Next, I = I->Next);
  90. if (Res == 0)
  91. return I;
  92. if (Create == false)
  93. return 0;
  94. I = new Item;
  95. I->Tag.assign(S,Len);
  96. I->Next = *Last;
  97. I->Parent = Head;
  98. *Last = I;
  99. return I;
  100. }
  101. /*}}}*/
  102. // Configuration::Lookup - Lookup a fully scoped item /*{{{*/
  103. // ---------------------------------------------------------------------
  104. /* This performs a fully scoped lookup of a given name, possibly creating
  105. new items */
  106. Configuration::Item *Configuration::Lookup(const char *Name,bool Create)
  107. {
  108. if (Name == 0)
  109. return Root->Child;
  110. const char *Start = Name;
  111. const char *End = Start + strlen(Name);
  112. const char *TagEnd = Name;
  113. Item *Itm = Root;
  114. for (; End - TagEnd >= 2; TagEnd++)
  115. {
  116. if (TagEnd[0] == ':' && TagEnd[1] == ':')
  117. {
  118. Itm = Lookup(Itm,Start,TagEnd - Start,Create);
  119. if (Itm == 0)
  120. return 0;
  121. TagEnd = Start = TagEnd + 2;
  122. }
  123. }
  124. // This must be a trailing ::, we create unique items in a list
  125. if (End - Start == 0)
  126. {
  127. if (Create == false)
  128. return 0;
  129. }
  130. Itm = Lookup(Itm,Start,End - Start,Create);
  131. return Itm;
  132. }
  133. /*}}}*/
  134. // Configuration::Find - Find a value /*{{{*/
  135. // ---------------------------------------------------------------------
  136. /* */
  137. string Configuration::Find(const char *Name,const char *Default) const
  138. {
  139. const Item *Itm = Lookup(Name);
  140. if (Itm == 0 || Itm->Value.empty() == true)
  141. {
  142. if (Default == 0)
  143. return "";
  144. else
  145. return Default;
  146. }
  147. return Itm->Value;
  148. }
  149. /*}}}*/
  150. // Configuration::FindFile - Find a Filename /*{{{*/
  151. // ---------------------------------------------------------------------
  152. /* Directories are stored as the base dir in the Parent node and the
  153. sub directory in sub nodes with the final node being the end filename
  154. */
  155. string Configuration::FindFile(const char *Name,const char *Default) const
  156. {
  157. const Item *Itm = Lookup(Name);
  158. if (Itm == 0 || Itm->Value.empty() == true)
  159. {
  160. if (Default == 0)
  161. return "";
  162. else
  163. return Default;
  164. }
  165. string val = Itm->Value;
  166. while (Itm->Parent != 0 && Itm->Parent->Value.empty() == false)
  167. {
  168. // Absolute
  169. if (val.length() >= 1 && val[0] == '/')
  170. break;
  171. // ~/foo or ./foo
  172. if (val.length() >= 2 && (val[0] == '~' || val[0] == '.') && val[1] == '/')
  173. break;
  174. // ../foo
  175. if (val.length() >= 3 && val[0] == '.' && val[1] == '.' && val[2] == '/')
  176. break;
  177. if (Itm->Parent->Value.end()[-1] != '/')
  178. val.insert(0, "/");
  179. val.insert(0, Itm->Parent->Value);
  180. Itm = Itm->Parent;
  181. }
  182. return val;
  183. }
  184. /*}}}*/
  185. // Configuration::FindDir - Find a directory name /*{{{*/
  186. // ---------------------------------------------------------------------
  187. /* This is like findfile execept the result is terminated in a / */
  188. string Configuration::FindDir(const char *Name,const char *Default) const
  189. {
  190. string Res = FindFile(Name,Default);
  191. if (Res.end()[-1] != '/')
  192. return Res + '/';
  193. return Res;
  194. }
  195. /*}}}*/
  196. // Configuration::FindI - Find an integer value /*{{{*/
  197. // ---------------------------------------------------------------------
  198. /* */
  199. int Configuration::FindI(const char *Name,int Default) const
  200. {
  201. const Item *Itm = Lookup(Name);
  202. if (Itm == 0 || Itm->Value.empty() == true)
  203. return Default;
  204. char *End;
  205. int Res = strtol(Itm->Value.c_str(),&End,0);
  206. if (End == Itm->Value.c_str())
  207. return Default;
  208. return Res;
  209. }
  210. /*}}}*/
  211. // Configuration::FindB - Find a boolean type /*{{{*/
  212. // ---------------------------------------------------------------------
  213. /* */
  214. bool Configuration::FindB(const char *Name,bool Default) const
  215. {
  216. const Item *Itm = Lookup(Name);
  217. if (Itm == 0 || Itm->Value.empty() == true)
  218. return Default;
  219. return StringToBool(Itm->Value,Default);
  220. }
  221. /*}}}*/
  222. // Configuration::FindAny - Find an arbitrary type /*{{{*/
  223. // ---------------------------------------------------------------------
  224. /* a key suffix of /f, /d, /b or /i calls Find{File,Dir,B,I} */
  225. string Configuration::FindAny(const char *Name,const char *Default) const
  226. {
  227. string key = Name;
  228. char type = 0;
  229. if (key.size() > 2 && key.end()[-2] == '/')
  230. {
  231. type = key.end()[-1];
  232. key.resize(key.size() - 2);
  233. }
  234. switch (type)
  235. {
  236. // file
  237. case 'f':
  238. return FindFile(key.c_str(), Default);
  239. // directory
  240. case 'd':
  241. return FindDir(key.c_str(), Default);
  242. // bool
  243. case 'b':
  244. return FindB(key, Default) ? "true" : "false";
  245. // int
  246. case 'i':
  247. {
  248. char buf[16];
  249. snprintf(buf, sizeof(buf)-1, "%d", FindI(key, Default ? atoi(Default) : 0 ));
  250. return buf;
  251. }
  252. }
  253. // fallback
  254. return Find(Name, Default);
  255. }
  256. /*}}}*/
  257. // Configuration::CndSet - Conditinal Set a value /*{{{*/
  258. // ---------------------------------------------------------------------
  259. /* This will not overwrite */
  260. void Configuration::CndSet(const char *Name,const string &Value)
  261. {
  262. Item *Itm = Lookup(Name,true);
  263. if (Itm == 0)
  264. return;
  265. if (Itm->Value.empty() == true)
  266. Itm->Value = Value;
  267. }
  268. /*}}}*/
  269. // Configuration::Set - Set a value /*{{{*/
  270. // ---------------------------------------------------------------------
  271. /* */
  272. void Configuration::Set(const char *Name,const string &Value)
  273. {
  274. Item *Itm = Lookup(Name,true);
  275. if (Itm == 0)
  276. return;
  277. Itm->Value = Value;
  278. }
  279. /*}}}*/
  280. // Configuration::Set - Set an integer value /*{{{*/
  281. // ---------------------------------------------------------------------
  282. /* */
  283. void Configuration::Set(const char *Name,int Value)
  284. {
  285. Item *Itm = Lookup(Name,true);
  286. if (Itm == 0)
  287. return;
  288. char S[300];
  289. snprintf(S,sizeof(S),"%i",Value);
  290. Itm->Value = S;
  291. }
  292. /*}}}*/
  293. // Configuration::Clear - Clear an single value from a list /*{{{*/
  294. // ---------------------------------------------------------------------
  295. /* */
  296. void Configuration::Clear(const string Name, int Value)
  297. {
  298. char S[300];
  299. snprintf(S,sizeof(S),"%i",Value);
  300. Clear(Name, S);
  301. }
  302. /*}}}*/
  303. // Configuration::Clear - Clear an single value from a list /*{{{*/
  304. // ---------------------------------------------------------------------
  305. /* */
  306. void Configuration::Clear(const string Name, string Value)
  307. {
  308. Item *Top = Lookup(Name.c_str(),false);
  309. if (Top == 0 || Top->Child == 0)
  310. return;
  311. Item *Tmp, *Prev, *I;
  312. Prev = I = Top->Child;
  313. while(I != NULL)
  314. {
  315. if(I->Value == Value)
  316. {
  317. Tmp = I;
  318. // was first element, point parent to new first element
  319. if(Top->Child == Tmp)
  320. Top->Child = I->Next;
  321. I = I->Next;
  322. Prev->Next = I;
  323. delete Tmp;
  324. } else {
  325. Prev = I;
  326. I = I->Next;
  327. }
  328. }
  329. }
  330. /*}}}*/
  331. // Configuration::Clear - Clear an entire tree /*{{{*/
  332. // ---------------------------------------------------------------------
  333. /* */
  334. void Configuration::Clear(string Name)
  335. {
  336. Item *Top = Lookup(Name.c_str(),false);
  337. if (Top == 0)
  338. return;
  339. Top->Value.clear();
  340. Item *Stop = Top;
  341. Top = Top->Child;
  342. Stop->Child = 0;
  343. for (; Top != 0;)
  344. {
  345. if (Top->Child != 0)
  346. {
  347. Top = Top->Child;
  348. continue;
  349. }
  350. while (Top != 0 && Top->Next == 0)
  351. {
  352. Item *Tmp = Top;
  353. Top = Top->Parent;
  354. delete Tmp;
  355. if (Top == Stop)
  356. return;
  357. }
  358. Item *Tmp = Top;
  359. if (Top != 0)
  360. Top = Top->Next;
  361. delete Tmp;
  362. }
  363. }
  364. /*}}}*/
  365. // Configuration::Exists - Returns true if the Name exists /*{{{*/
  366. // ---------------------------------------------------------------------
  367. /* */
  368. bool Configuration::Exists(const char *Name) const
  369. {
  370. const Item *Itm = Lookup(Name);
  371. if (Itm == 0)
  372. return false;
  373. return true;
  374. }
  375. /*}}}*/
  376. // Configuration::ExistsAny - Returns true if the Name, possibly /*{{{*/
  377. // ---------------------------------------------------------------------
  378. /* qualified by /[fdbi] exists */
  379. bool Configuration::ExistsAny(const char *Name) const
  380. {
  381. string key = Name;
  382. if (key.size() > 2 && key.end()[-2] == '/')
  383. {
  384. if (key.find_first_of("fdbi",key.size()-1) < key.size())
  385. {
  386. key.resize(key.size() - 2);
  387. if (Exists(key.c_str()))
  388. return true;
  389. }
  390. else
  391. {
  392. _error->Warning(_("Unrecognized type abbreviation: '%c'"), key.end()[-3]);
  393. }
  394. }
  395. return Exists(Name);
  396. }
  397. /*}}}*/
  398. // Configuration::Dump - Dump the config /*{{{*/
  399. // ---------------------------------------------------------------------
  400. /* Dump the entire configuration space */
  401. void Configuration::Dump(ostream& str)
  402. {
  403. /* Write out all of the configuration directives by walking the
  404. configuration tree */
  405. const Configuration::Item *Top = Tree(0);
  406. for (; Top != 0;)
  407. {
  408. str << Top->FullTag() << " \"" << Top->Value << "\";" << endl;
  409. if (Top->Child != 0)
  410. {
  411. Top = Top->Child;
  412. continue;
  413. }
  414. while (Top != 0 && Top->Next == 0)
  415. Top = Top->Parent;
  416. if (Top != 0)
  417. Top = Top->Next;
  418. }
  419. }
  420. /*}}}*/
  421. // Configuration::Item::FullTag - Return the fully scoped tag /*{{{*/
  422. // ---------------------------------------------------------------------
  423. /* Stop sets an optional max recursion depth if this item is being viewed as
  424. part of a sub tree. */
  425. string Configuration::Item::FullTag(const Item *Stop) const
  426. {
  427. if (Parent == 0 || Parent->Parent == 0 || Parent == Stop)
  428. return Tag;
  429. return Parent->FullTag(Stop) + "::" + Tag;
  430. }
  431. /*}}}*/
  432. // ReadConfigFile - Read a configuration file /*{{{*/
  433. // ---------------------------------------------------------------------
  434. /* The configuration format is very much like the named.conf format
  435. used in bind8, in fact this routine can parse most named.conf files.
  436. Sectional config files are like bind's named.conf where there are
  437. sections like 'zone "foo.org" { .. };' This causes each section to be
  438. added in with a tag like "zone::foo.org" instead of being split
  439. tag/value. AsSectional enables Sectional parsing.*/
  440. bool ReadConfigFile(Configuration &Conf,const string &FName,bool AsSectional,
  441. unsigned Depth)
  442. {
  443. // Open the stream for reading
  444. ifstream F(FName.c_str(),ios::in);
  445. if (!F != 0)
  446. return _error->Errno("ifstream::ifstream",_("Opening configuration file %s"),FName.c_str());
  447. char Buffer[1024];
  448. string LineBuffer;
  449. string Stack[100];
  450. unsigned int StackPos = 0;
  451. // Parser state
  452. string ParentTag;
  453. int CurLine = 0;
  454. bool InComment = false;
  455. while (F.eof() == false)
  456. {
  457. F.getline(Buffer,sizeof(Buffer));
  458. CurLine++;
  459. // This should be made to work instead, but this is better than looping
  460. if (F.fail() && !F.eof())
  461. return _error->Error(_("Line %d too long (max %lu)"), CurLine, sizeof(Buffer));
  462. _strtabexpand(Buffer,sizeof(Buffer));
  463. _strstrip(Buffer);
  464. // Multi line comment
  465. if (InComment == true)
  466. {
  467. for (const char *I = Buffer; *I != 0; I++)
  468. {
  469. if (*I == '*' && I[1] == '/')
  470. {
  471. memmove(Buffer,I+2,strlen(I+2) + 1);
  472. InComment = false;
  473. break;
  474. }
  475. }
  476. if (InComment == true)
  477. continue;
  478. }
  479. // Discard single line comments
  480. bool InQuote = false;
  481. for (char *I = Buffer; *I != 0; I++)
  482. {
  483. if (*I == '"')
  484. InQuote = !InQuote;
  485. if (InQuote == true)
  486. continue;
  487. if (*I == '/' && I[1] == '/')
  488. {
  489. *I = 0;
  490. break;
  491. }
  492. }
  493. // Look for multi line comments
  494. InQuote = false;
  495. for (char *I = Buffer; *I != 0; I++)
  496. {
  497. if (*I == '"')
  498. InQuote = !InQuote;
  499. if (InQuote == true)
  500. continue;
  501. if (*I == '/' && I[1] == '*')
  502. {
  503. InComment = true;
  504. for (char *J = Buffer; *J != 0; J++)
  505. {
  506. if (*J == '*' && J[1] == '/')
  507. {
  508. memmove(I,J+2,strlen(J+2) + 1);
  509. InComment = false;
  510. break;
  511. }
  512. }
  513. if (InComment == true)
  514. {
  515. *I = 0;
  516. break;
  517. }
  518. }
  519. }
  520. // Blank
  521. if (Buffer[0] == 0)
  522. continue;
  523. // We now have a valid line fragment
  524. InQuote = false;
  525. for (char *I = Buffer; *I != 0;)
  526. {
  527. if (*I == '"')
  528. InQuote = !InQuote;
  529. if (InQuote == false && (*I == '{' || *I == ';' || *I == '}'))
  530. {
  531. // Put the last fragment into the buffer
  532. char *Start = Buffer;
  533. char *Stop = I;
  534. for (; Start != I && isspace(*Start) != 0; Start++);
  535. for (; Stop != Start && isspace(Stop[-1]) != 0; Stop--);
  536. if (LineBuffer.empty() == false && Stop - Start != 0)
  537. LineBuffer += ' ';
  538. LineBuffer += string(Start,Stop - Start);
  539. // Remove the fragment
  540. char TermChar = *I;
  541. memmove(Buffer,I + 1,strlen(I + 1) + 1);
  542. I = Buffer;
  543. // Syntax Error
  544. if (TermChar == '{' && LineBuffer.empty() == true)
  545. return _error->Error(_("Syntax error %s:%u: Block starts with no name."),FName.c_str(),CurLine);
  546. // No string on this line
  547. if (LineBuffer.empty() == true)
  548. {
  549. if (TermChar == '}')
  550. {
  551. if (StackPos == 0)
  552. ParentTag = string();
  553. else
  554. ParentTag = Stack[--StackPos];
  555. }
  556. continue;
  557. }
  558. // Parse off the tag
  559. string Tag;
  560. const char *Pos = LineBuffer.c_str();
  561. if (ParseQuoteWord(Pos,Tag) == false)
  562. return _error->Error(_("Syntax error %s:%u: Malformed tag"),FName.c_str(),CurLine);
  563. // Parse off the word
  564. string Word;
  565. bool NoWord = false;
  566. if (ParseCWord(Pos,Word) == false &&
  567. ParseQuoteWord(Pos,Word) == false)
  568. {
  569. if (TermChar != '{')
  570. {
  571. Word = Tag;
  572. Tag = "";
  573. }
  574. else
  575. NoWord = true;
  576. }
  577. if (strlen(Pos) != 0)
  578. return _error->Error(_("Syntax error %s:%u: Extra junk after value"),FName.c_str(),CurLine);
  579. // Go down a level
  580. if (TermChar == '{')
  581. {
  582. if (StackPos <= 100)
  583. Stack[StackPos++] = ParentTag;
  584. /* Make sectional tags incorperate the section into the
  585. tag string */
  586. if (AsSectional == true && Word.empty() == false)
  587. {
  588. Tag += "::" ;
  589. Tag += Word;
  590. Word = "";
  591. }
  592. if (ParentTag.empty() == true)
  593. ParentTag = Tag;
  594. else
  595. ParentTag += string("::") + Tag;
  596. Tag = string();
  597. }
  598. // Generate the item name
  599. string Item;
  600. if (ParentTag.empty() == true)
  601. Item = Tag;
  602. else
  603. {
  604. if (TermChar != '{' || Tag.empty() == false)
  605. Item = ParentTag + "::" + Tag;
  606. else
  607. Item = ParentTag;
  608. }
  609. // Specials
  610. if (Tag.length() >= 1 && Tag[0] == '#')
  611. {
  612. if (ParentTag.empty() == false)
  613. return _error->Error(_("Syntax error %s:%u: Directives can only be done at the top level"),FName.c_str(),CurLine);
  614. Tag.erase(Tag.begin());
  615. if (Tag == "clear")
  616. Conf.Clear(Word);
  617. else if (Tag == "include")
  618. {
  619. if (Depth > 10)
  620. return _error->Error(_("Syntax error %s:%u: Too many nested includes"),FName.c_str(),CurLine);
  621. if (Word.length() > 2 && Word.end()[-1] == '/')
  622. {
  623. if (ReadConfigDir(Conf,Word,AsSectional,Depth+1) == false)
  624. return _error->Error(_("Syntax error %s:%u: Included from here"),FName.c_str(),CurLine);
  625. }
  626. else
  627. {
  628. if (ReadConfigFile(Conf,Word,AsSectional,Depth+1) == false)
  629. return _error->Error(_("Syntax error %s:%u: Included from here"),FName.c_str(),CurLine);
  630. }
  631. }
  632. else
  633. return _error->Error(_("Syntax error %s:%u: Unsupported directive '%s'"),FName.c_str(),CurLine,Tag.c_str());
  634. }
  635. else
  636. {
  637. // Set the item in the configuration class
  638. if (NoWord == false)
  639. Conf.Set(Item,Word);
  640. }
  641. // Empty the buffer
  642. LineBuffer.clear();
  643. // Move up a tag, but only if there is no bit to parse
  644. if (TermChar == '}')
  645. {
  646. if (StackPos == 0)
  647. ParentTag.clear();
  648. else
  649. ParentTag = Stack[--StackPos];
  650. }
  651. }
  652. else
  653. I++;
  654. }
  655. // Store the fragment
  656. const char *Stripd = _strstrip(Buffer);
  657. if (*Stripd != 0 && LineBuffer.empty() == false)
  658. LineBuffer += " ";
  659. LineBuffer += Stripd;
  660. }
  661. if (LineBuffer.empty() == false)
  662. return _error->Error(_("Syntax error %s:%u: Extra junk at end of file"),FName.c_str(),CurLine);
  663. return true;
  664. }
  665. /*}}}*/
  666. // ReadConfigDir - Read a directory of config files /*{{{*/
  667. // ---------------------------------------------------------------------
  668. /* */
  669. bool ReadConfigDir(Configuration &Conf,const string &Dir,bool AsSectional,
  670. unsigned Depth)
  671. {
  672. DIR *D = opendir(Dir.c_str());
  673. if (D == 0)
  674. return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
  675. vector<string> List;
  676. for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D))
  677. {
  678. if (Ent->d_name[0] == '.')
  679. continue;
  680. // Skip bad file names ala run-parts
  681. const char *C = Ent->d_name;
  682. for (; *C != 0; C++)
  683. if (isalpha(*C) == 0 && isdigit(*C) == 0 && *C != '_' && *C != '-')
  684. break;
  685. if (*C != 0)
  686. continue;
  687. // Make sure it is a file and not something else
  688. string File = flCombine(Dir,Ent->d_name);
  689. struct stat St;
  690. if (stat(File.c_str(),&St) != 0 || S_ISREG(St.st_mode) == 0)
  691. continue;
  692. List.push_back(File);
  693. }
  694. closedir(D);
  695. sort(List.begin(),List.end());
  696. // Read the files
  697. for (vector<string>::const_iterator I = List.begin(); I != List.end(); I++)
  698. if (ReadConfigFile(Conf,*I,AsSectional,Depth) == false)
  699. return false;
  700. return true;
  701. }
  702. /*}}}*/