configuration.cc 24 KB

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