configuration.cc 20 KB

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