configuration.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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 *RootItem = Lookup("RootDir");
  158. std::string rootDir = (RootItem == 0) ? "" : RootItem->Value;
  159. if(rootDir.size() > 0 && rootDir[rootDir.size() - 1] != '/')
  160. rootDir.push_back('/');
  161. const Item *Itm = Lookup(Name);
  162. if (Itm == 0 || Itm->Value.empty() == true)
  163. {
  164. if (Default == 0)
  165. return "";
  166. else
  167. return Default;
  168. }
  169. string val = Itm->Value;
  170. while (Itm->Parent != 0 && Itm->Parent->Value.empty() == false)
  171. {
  172. // Absolute
  173. if (val.length() >= 1 && val[0] == '/')
  174. break;
  175. // ~/foo or ./foo
  176. if (val.length() >= 2 && (val[0] == '~' || val[0] == '.') && val[1] == '/')
  177. break;
  178. // ../foo
  179. if (val.length() >= 3 && val[0] == '.' && val[1] == '.' && val[2] == '/')
  180. break;
  181. if (Itm->Parent->Value.end()[-1] != '/')
  182. val.insert(0, "/");
  183. val.insert(0, Itm->Parent->Value);
  184. Itm = Itm->Parent;
  185. }
  186. return rootDir + val;
  187. }
  188. /*}}}*/
  189. // Configuration::FindDir - Find a directory name /*{{{*/
  190. // ---------------------------------------------------------------------
  191. /* This is like findfile execept the result is terminated in a / */
  192. string Configuration::FindDir(const char *Name,const char *Default) const
  193. {
  194. string Res = FindFile(Name,Default);
  195. if (Res.end()[-1] != '/')
  196. return Res + '/';
  197. return Res;
  198. }
  199. /*}}}*/
  200. // Configuration::FindI - Find an integer value /*{{{*/
  201. // ---------------------------------------------------------------------
  202. /* */
  203. int Configuration::FindI(const char *Name,int Default) const
  204. {
  205. const Item *Itm = Lookup(Name);
  206. if (Itm == 0 || Itm->Value.empty() == true)
  207. return Default;
  208. char *End;
  209. int Res = strtol(Itm->Value.c_str(),&End,0);
  210. if (End == Itm->Value.c_str())
  211. return Default;
  212. return Res;
  213. }
  214. /*}}}*/
  215. // Configuration::FindB - Find a boolean type /*{{{*/
  216. // ---------------------------------------------------------------------
  217. /* */
  218. bool Configuration::FindB(const char *Name,bool Default) const
  219. {
  220. const Item *Itm = Lookup(Name);
  221. if (Itm == 0 || Itm->Value.empty() == true)
  222. return Default;
  223. return StringToBool(Itm->Value,Default);
  224. }
  225. /*}}}*/
  226. // Configuration::FindAny - Find an arbitrary type /*{{{*/
  227. // ---------------------------------------------------------------------
  228. /* a key suffix of /f, /d, /b or /i calls Find{File,Dir,B,I} */
  229. string Configuration::FindAny(const char *Name,const char *Default) const
  230. {
  231. string key = Name;
  232. char type = 0;
  233. if (key.size() > 2 && key.end()[-2] == '/')
  234. {
  235. type = key.end()[-1];
  236. key.resize(key.size() - 2);
  237. }
  238. switch (type)
  239. {
  240. // file
  241. case 'f':
  242. return FindFile(key.c_str(), Default);
  243. // directory
  244. case 'd':
  245. return FindDir(key.c_str(), Default);
  246. // bool
  247. case 'b':
  248. return FindB(key, Default) ? "true" : "false";
  249. // int
  250. case 'i':
  251. {
  252. char buf[16];
  253. snprintf(buf, sizeof(buf)-1, "%d", FindI(key, Default ? atoi(Default) : 0 ));
  254. return buf;
  255. }
  256. }
  257. // fallback
  258. return Find(Name, Default);
  259. }
  260. /*}}}*/
  261. // Configuration::CndSet - Conditinal Set a value /*{{{*/
  262. // ---------------------------------------------------------------------
  263. /* This will not overwrite */
  264. void Configuration::CndSet(const char *Name,const string &Value)
  265. {
  266. Item *Itm = Lookup(Name,true);
  267. if (Itm == 0)
  268. return;
  269. if (Itm->Value.empty() == true)
  270. Itm->Value = Value;
  271. }
  272. /*}}}*/
  273. // Configuration::Set - Set a value /*{{{*/
  274. // ---------------------------------------------------------------------
  275. /* */
  276. void Configuration::Set(const char *Name,const string &Value)
  277. {
  278. Item *Itm = Lookup(Name,true);
  279. if (Itm == 0)
  280. return;
  281. Itm->Value = Value;
  282. }
  283. /*}}}*/
  284. // Configuration::Set - Set an integer value /*{{{*/
  285. // ---------------------------------------------------------------------
  286. /* */
  287. void Configuration::Set(const char *Name,int Value)
  288. {
  289. Item *Itm = Lookup(Name,true);
  290. if (Itm == 0)
  291. return;
  292. char S[300];
  293. snprintf(S,sizeof(S),"%i",Value);
  294. Itm->Value = S;
  295. }
  296. /*}}}*/
  297. // Configuration::Clear - Clear an single value from a list /*{{{*/
  298. // ---------------------------------------------------------------------
  299. /* */
  300. void Configuration::Clear(const string Name, int Value)
  301. {
  302. char S[300];
  303. snprintf(S,sizeof(S),"%i",Value);
  304. Clear(Name, S);
  305. }
  306. /*}}}*/
  307. // Configuration::Clear - Clear an single value from a list /*{{{*/
  308. // ---------------------------------------------------------------------
  309. /* */
  310. void Configuration::Clear(const string Name, string Value)
  311. {
  312. Item *Top = Lookup(Name.c_str(),false);
  313. if (Top == 0 || Top->Child == 0)
  314. return;
  315. Item *Tmp, *Prev, *I;
  316. Prev = I = Top->Child;
  317. while(I != NULL)
  318. {
  319. if(I->Value == Value)
  320. {
  321. Tmp = I;
  322. // was first element, point parent to new first element
  323. if(Top->Child == Tmp)
  324. Top->Child = I->Next;
  325. I = I->Next;
  326. Prev->Next = I;
  327. delete Tmp;
  328. } else {
  329. Prev = I;
  330. I = I->Next;
  331. }
  332. }
  333. }
  334. /*}}}*/
  335. // Configuration::Clear - Clear an entire tree /*{{{*/
  336. // ---------------------------------------------------------------------
  337. /* */
  338. void Configuration::Clear(string Name)
  339. {
  340. Item *Top = Lookup(Name.c_str(),false);
  341. if (Top == 0)
  342. return;
  343. Top->Value.clear();
  344. Item *Stop = Top;
  345. Top = Top->Child;
  346. Stop->Child = 0;
  347. for (; Top != 0;)
  348. {
  349. if (Top->Child != 0)
  350. {
  351. Top = Top->Child;
  352. continue;
  353. }
  354. while (Top != 0 && Top->Next == 0)
  355. {
  356. Item *Tmp = Top;
  357. Top = Top->Parent;
  358. delete Tmp;
  359. if (Top == Stop)
  360. return;
  361. }
  362. Item *Tmp = Top;
  363. if (Top != 0)
  364. Top = Top->Next;
  365. delete Tmp;
  366. }
  367. }
  368. /*}}}*/
  369. // Configuration::Exists - Returns true if the Name exists /*{{{*/
  370. // ---------------------------------------------------------------------
  371. /* */
  372. bool Configuration::Exists(const char *Name) const
  373. {
  374. const Item *Itm = Lookup(Name);
  375. if (Itm == 0)
  376. return false;
  377. return true;
  378. }
  379. /*}}}*/
  380. // Configuration::ExistsAny - Returns true if the Name, possibly /*{{{*/
  381. // ---------------------------------------------------------------------
  382. /* qualified by /[fdbi] exists */
  383. bool Configuration::ExistsAny(const char *Name) const
  384. {
  385. string key = Name;
  386. if (key.size() > 2 && key.end()[-2] == '/')
  387. {
  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. }
  399. return Exists(Name);
  400. }
  401. /*}}}*/
  402. // Configuration::Dump - Dump the config /*{{{*/
  403. // ---------------------------------------------------------------------
  404. /* Dump the entire configuration space */
  405. void Configuration::Dump(ostream& str)
  406. {
  407. /* Write out all of the configuration directives by walking the
  408. configuration tree */
  409. const Configuration::Item *Top = Tree(0);
  410. for (; Top != 0;)
  411. {
  412. str << Top->FullTag() << " \"" << Top->Value << "\";" << endl;
  413. if (Top->Child != 0)
  414. {
  415. Top = Top->Child;
  416. continue;
  417. }
  418. while (Top != 0 && Top->Next == 0)
  419. Top = Top->Parent;
  420. if (Top != 0)
  421. Top = Top->Next;
  422. }
  423. }
  424. /*}}}*/
  425. // Configuration::Item::FullTag - Return the fully scoped tag /*{{{*/
  426. // ---------------------------------------------------------------------
  427. /* Stop sets an optional max recursion depth if this item is being viewed as
  428. part of a sub tree. */
  429. string Configuration::Item::FullTag(const Item *Stop) const
  430. {
  431. if (Parent == 0 || Parent->Parent == 0 || Parent == Stop)
  432. return Tag;
  433. return Parent->FullTag(Stop) + "::" + Tag;
  434. }
  435. /*}}}*/
  436. // ReadConfigFile - Read a configuration file /*{{{*/
  437. // ---------------------------------------------------------------------
  438. /* The configuration format is very much like the named.conf format
  439. used in bind8, in fact this routine can parse most named.conf files.
  440. Sectional config files are like bind's named.conf where there are
  441. sections like 'zone "foo.org" { .. };' This causes each section to be
  442. added in with a tag like "zone::foo.org" instead of being split
  443. tag/value. AsSectional enables Sectional parsing.*/
  444. bool ReadConfigFile(Configuration &Conf,const string &FName,bool AsSectional,
  445. unsigned Depth)
  446. {
  447. // Open the stream for reading
  448. ifstream F(FName.c_str(),ios::in);
  449. if (!F != 0)
  450. return _error->Errno("ifstream::ifstream",_("Opening configuration file %s"),FName.c_str());
  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. // The raw input line.
  461. std::string Input;
  462. // The input line with comments stripped.
  463. std::string Fragment;
  464. // Grab the next line of F and place it in Input.
  465. do
  466. {
  467. char *Buffer = new char[1024];
  468. F.clear();
  469. F.getline(Buffer,sizeof(Buffer) / 2);
  470. Input += Buffer;
  471. }
  472. while (F.fail() && !F.eof());
  473. // Expand tabs in the input line and remove leading and trailing
  474. // whitespace.
  475. {
  476. const int BufferSize = Input.size() * 8 + 1;
  477. char *Buffer = new char[BufferSize];
  478. try
  479. {
  480. memcpy(Buffer, Input.c_str(), Input.size() + 1);
  481. _strtabexpand(Buffer, BufferSize);
  482. _strstrip(Buffer);
  483. Input = Buffer;
  484. }
  485. catch(...)
  486. {
  487. delete[] Buffer;
  488. throw;
  489. }
  490. delete[] Buffer;
  491. }
  492. CurLine++;
  493. // Now strip comments; if the whole line is contained in a
  494. // comment, skip this line.
  495. // The first meaningful character in the current fragment; will
  496. // be adjusted below as we remove bytes from the front.
  497. std::string::const_iterator Start = Input.begin();
  498. // The last meaningful character in the current fragment.
  499. std::string::const_iterator End = Input.end();
  500. // Multi line comment
  501. if (InComment == true)
  502. {
  503. for (std::string::const_iterator I = Start;
  504. I != End; ++I)
  505. {
  506. if (*I == '*' && I + 1 != End && I[1] == '/')
  507. {
  508. Start = I + 2;
  509. InComment = false;
  510. break;
  511. }
  512. }
  513. if (InComment == true)
  514. continue;
  515. }
  516. // Discard single line comments
  517. bool InQuote = false;
  518. for (std::string::const_iterator I = Start;
  519. I != End; ++I)
  520. {
  521. if (*I == '"')
  522. InQuote = !InQuote;
  523. if (InQuote == true)
  524. continue;
  525. if (*I == '/' && I + 1 != End && I[1] == '/')
  526. {
  527. End = I;
  528. break;
  529. }
  530. }
  531. // Look for multi line comments and build up the
  532. // fragment.
  533. Fragment.reserve(End - Start);
  534. InQuote = false;
  535. for (std::string::const_iterator I = Start;
  536. I != End; ++I)
  537. {
  538. if (*I == '"')
  539. InQuote = !InQuote;
  540. if (InQuote == true)
  541. Fragment.push_back(*I);
  542. else if (*I == '/' && I + 1 != End && I[1] == '*')
  543. {
  544. InComment = true;
  545. for (std::string::const_iterator J = I;
  546. J != End; ++J)
  547. {
  548. if (*J == '*' && J + 1 != End && J[1] == '/')
  549. {
  550. // Pretend we just finished walking over the
  551. // comment, and don't add anything to the output
  552. // fragment.
  553. I = J + 1;
  554. InComment = false;
  555. break;
  556. }
  557. }
  558. if (InComment == true)
  559. break;
  560. }
  561. else
  562. Fragment.push_back(*I);
  563. }
  564. // Skip blank lines.
  565. if (Fragment.empty())
  566. continue;
  567. // The line has actual content; interpret what it means.
  568. InQuote = false;
  569. Start = Fragment.begin();
  570. End = Fragment.end();
  571. for (std::string::const_iterator I = Start;
  572. I != End; ++I)
  573. {
  574. if (*I == '"')
  575. InQuote = !InQuote;
  576. if (InQuote == false && (*I == '{' || *I == ';' || *I == '}'))
  577. {
  578. // Put the last fragment into the buffer
  579. std::string::const_iterator NonWhitespaceStart = Start;
  580. std::string::const_iterator NonWhitespaceStop = I;
  581. for (; NonWhitespaceStart != I && isspace(*NonWhitespaceStart) != 0; NonWhitespaceStart++)
  582. ;
  583. for (; NonWhitespaceStop != NonWhitespaceStart && isspace(NonWhitespaceStop[-1]) != 0; NonWhitespaceStop--)
  584. ;
  585. if (LineBuffer.empty() == false && NonWhitespaceStop - NonWhitespaceStart != 0)
  586. LineBuffer += ' ';
  587. LineBuffer += string(NonWhitespaceStart, NonWhitespaceStop);
  588. // Drop this from the input string, saving the character
  589. // that terminated the construct we just closed. (i.e., a
  590. // brace or a semicolon)
  591. char TermChar = *I;
  592. Start = I + 1;
  593. // Syntax Error
  594. if (TermChar == '{' && LineBuffer.empty() == true)
  595. return _error->Error(_("Syntax error %s:%u: Block starts with no name."),FName.c_str(),CurLine);
  596. // No string on this line
  597. if (LineBuffer.empty() == true)
  598. {
  599. if (TermChar == '}')
  600. {
  601. if (StackPos == 0)
  602. ParentTag = string();
  603. else
  604. ParentTag = Stack[--StackPos];
  605. }
  606. continue;
  607. }
  608. // Parse off the tag
  609. string Tag;
  610. const char *Pos = LineBuffer.c_str();
  611. if (ParseQuoteWord(Pos,Tag) == false)
  612. return _error->Error(_("Syntax error %s:%u: Malformed tag"),FName.c_str(),CurLine);
  613. // Parse off the word
  614. string Word;
  615. bool NoWord = false;
  616. if (ParseCWord(Pos,Word) == false &&
  617. ParseQuoteWord(Pos,Word) == false)
  618. {
  619. if (TermChar != '{')
  620. {
  621. Word = Tag;
  622. Tag = "";
  623. }
  624. else
  625. NoWord = true;
  626. }
  627. if (strlen(Pos) != 0)
  628. return _error->Error(_("Syntax error %s:%u: Extra junk after value"),FName.c_str(),CurLine);
  629. // Go down a level
  630. if (TermChar == '{')
  631. {
  632. if (StackPos <= 100)
  633. Stack[StackPos++] = ParentTag;
  634. /* Make sectional tags incorperate the section into the
  635. tag string */
  636. if (AsSectional == true && Word.empty() == false)
  637. {
  638. Tag += "::" ;
  639. Tag += Word;
  640. Word = "";
  641. }
  642. if (ParentTag.empty() == true)
  643. ParentTag = Tag;
  644. else
  645. ParentTag += string("::") + Tag;
  646. Tag = string();
  647. }
  648. // Generate the item name
  649. string Item;
  650. if (ParentTag.empty() == true)
  651. Item = Tag;
  652. else
  653. {
  654. if (TermChar != '{' || Tag.empty() == false)
  655. Item = ParentTag + "::" + Tag;
  656. else
  657. Item = ParentTag;
  658. }
  659. // Specials
  660. if (Tag.length() >= 1 && Tag[0] == '#')
  661. {
  662. if (ParentTag.empty() == false)
  663. return _error->Error(_("Syntax error %s:%u: Directives can only be done at the top level"),FName.c_str(),CurLine);
  664. Tag.erase(Tag.begin());
  665. if (Tag == "clear")
  666. Conf.Clear(Word);
  667. else if (Tag == "include")
  668. {
  669. if (Depth > 10)
  670. return _error->Error(_("Syntax error %s:%u: Too many nested includes"),FName.c_str(),CurLine);
  671. if (Word.length() > 2 && Word.end()[-1] == '/')
  672. {
  673. if (ReadConfigDir(Conf,Word,AsSectional,Depth+1) == false)
  674. return _error->Error(_("Syntax error %s:%u: Included from here"),FName.c_str(),CurLine);
  675. }
  676. else
  677. {
  678. if (ReadConfigFile(Conf,Word,AsSectional,Depth+1) == false)
  679. return _error->Error(_("Syntax error %s:%u: Included from here"),FName.c_str(),CurLine);
  680. }
  681. }
  682. else
  683. return _error->Error(_("Syntax error %s:%u: Unsupported directive '%s'"),FName.c_str(),CurLine,Tag.c_str());
  684. }
  685. else
  686. {
  687. // Set the item in the configuration class
  688. if (NoWord == false)
  689. Conf.Set(Item,Word);
  690. }
  691. // Empty the buffer
  692. LineBuffer.clear();
  693. // Move up a tag, but only if there is no bit to parse
  694. if (TermChar == '}')
  695. {
  696. if (StackPos == 0)
  697. ParentTag.clear();
  698. else
  699. ParentTag = Stack[--StackPos];
  700. }
  701. }
  702. }
  703. // Store the remaining text, if any, in the current line buffer.
  704. // NB: could change this to use string-based operations; I'm
  705. // using strstrip now to ensure backwards compatibility.
  706. // -- dburrows 2008-04-01
  707. {
  708. char *Buffer = new char[End - Start + 1];
  709. try
  710. {
  711. std::copy(Start, End, Buffer);
  712. Buffer[End - Start] = '\0';
  713. const char *Stripd = _strstrip(Buffer);
  714. if (*Stripd != 0 && LineBuffer.empty() == false)
  715. LineBuffer += " ";
  716. LineBuffer += Stripd;
  717. }
  718. catch(...)
  719. {
  720. delete[] Buffer;
  721. throw;
  722. }
  723. delete[] Buffer;
  724. }
  725. }
  726. if (LineBuffer.empty() == false)
  727. return _error->Error(_("Syntax error %s:%u: Extra junk at end of file"),FName.c_str(),CurLine);
  728. return true;
  729. }
  730. /*}}}*/
  731. // ReadConfigDir - Read a directory of config files /*{{{*/
  732. // ---------------------------------------------------------------------
  733. /* */
  734. bool ReadConfigDir(Configuration &Conf,const string &Dir,bool AsSectional,
  735. unsigned Depth)
  736. {
  737. DIR *D = opendir(Dir.c_str());
  738. if (D == 0)
  739. return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
  740. vector<string> List;
  741. for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D))
  742. {
  743. if (Ent->d_name[0] == '.')
  744. continue;
  745. // Skip bad file names ala run-parts
  746. const char *C = Ent->d_name;
  747. for (; *C != 0; C++)
  748. if (isalpha(*C) == 0 && isdigit(*C) == 0 && *C != '_' && *C != '-')
  749. break;
  750. if (*C != 0)
  751. continue;
  752. // Make sure it is a file and not something else
  753. string File = flCombine(Dir,Ent->d_name);
  754. struct stat St;
  755. if (stat(File.c_str(),&St) != 0 || S_ISREG(St.st_mode) == 0)
  756. continue;
  757. List.push_back(File);
  758. }
  759. closedir(D);
  760. sort(List.begin(),List.end());
  761. // Read the files
  762. for (vector<string>::const_iterator I = List.begin(); I != List.end(); I++)
  763. if (ReadConfigFile(Conf,*I,AsSectional,Depth) == false)
  764. return false;
  765. return true;
  766. }
  767. /*}}}*/