configuration.cc 22 KB

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