configuration.cc 24 KB

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