configuration.cc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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 an entire tree /*{{{*/
  368. // ---------------------------------------------------------------------
  369. /* */
  370. void Configuration::Clear(string const &Name)
  371. {
  372. Item *Top = Lookup(Name.c_str(),false);
  373. if (Top == 0)
  374. return;
  375. Top->Value.clear();
  376. Item *Stop = Top;
  377. Top = Top->Child;
  378. Stop->Child = 0;
  379. for (; Top != 0;)
  380. {
  381. if (Top->Child != 0)
  382. {
  383. Top = Top->Child;
  384. continue;
  385. }
  386. while (Top != 0 && Top->Next == 0)
  387. {
  388. Item *Tmp = Top;
  389. Top = Top->Parent;
  390. delete Tmp;
  391. if (Top == Stop)
  392. return;
  393. }
  394. Item *Tmp = Top;
  395. if (Top != 0)
  396. Top = Top->Next;
  397. delete Tmp;
  398. }
  399. }
  400. /*}}}*/
  401. // Configuration::Exists - Returns true if the Name exists /*{{{*/
  402. // ---------------------------------------------------------------------
  403. /* */
  404. bool Configuration::Exists(const char *Name) const
  405. {
  406. const Item *Itm = Lookup(Name);
  407. if (Itm == 0)
  408. return false;
  409. return true;
  410. }
  411. /*}}}*/
  412. // Configuration::ExistsAny - Returns true if the Name, possibly /*{{{*/
  413. // ---------------------------------------------------------------------
  414. /* qualified by /[fdbi] exists */
  415. bool Configuration::ExistsAny(const char *Name) const
  416. {
  417. string key = Name;
  418. if (key.size() > 2 && key.end()[-2] == '/')
  419. {
  420. if (key.find_first_of("fdbi",key.size()-1) < key.size())
  421. {
  422. key.resize(key.size() - 2);
  423. if (Exists(key.c_str()))
  424. return true;
  425. }
  426. else
  427. {
  428. _error->Warning(_("Unrecognized type abbreviation: '%c'"), key.end()[-3]);
  429. }
  430. }
  431. return Exists(Name);
  432. }
  433. /*}}}*/
  434. // Configuration::Dump - Dump the config /*{{{*/
  435. // ---------------------------------------------------------------------
  436. /* Dump the entire configuration space */
  437. void Configuration::Dump(ostream& str)
  438. {
  439. Dump(str, NULL, "%f \"%v\";\n", true);
  440. }
  441. void Configuration::Dump(ostream& str, char const * const root,
  442. char const * const formatstr, bool const emptyValue)
  443. {
  444. const Configuration::Item* Top = Tree(root);
  445. if (Top == 0)
  446. return;
  447. const Configuration::Item* const Root = (root == NULL) ? NULL : Top;
  448. std::vector<std::string> const format = VectorizeString(formatstr, '%');
  449. /* Write out all of the configuration directives by walking the
  450. configuration tree */
  451. do {
  452. if (emptyValue == true || Top->Value.empty() == emptyValue)
  453. {
  454. std::vector<std::string>::const_iterator f = format.begin();
  455. str << *f;
  456. for (++f; f != format.end(); ++f)
  457. {
  458. if (f->empty() == true)
  459. {
  460. ++f;
  461. str << '%' << *f;
  462. continue;
  463. }
  464. char const type = (*f)[0];
  465. if (type == 'f')
  466. str << Top->FullTag();
  467. else if (type == 't')
  468. str << Top->Tag;
  469. else if (type == 'v')
  470. str << Top->Value;
  471. else if (type == 'F')
  472. str << QuoteString(Top->FullTag(), "=\"\n");
  473. else if (type == 'T')
  474. str << QuoteString(Top->Tag, "=\"\n");
  475. else if (type == 'V')
  476. str << QuoteString(Top->Value, "=\"\n");
  477. else if (type == 'n')
  478. str << "\n";
  479. else if (type == 'N')
  480. str << "\t";
  481. else
  482. str << '%' << type;
  483. str << f->c_str() + 1;
  484. }
  485. }
  486. if (Top->Child != 0)
  487. {
  488. Top = Top->Child;
  489. continue;
  490. }
  491. while (Top != 0 && Top->Next == 0)
  492. Top = Top->Parent;
  493. if (Top != 0)
  494. Top = Top->Next;
  495. if (Root != NULL)
  496. {
  497. const Configuration::Item* I = Top;
  498. while(I != 0)
  499. {
  500. if (I == Root)
  501. break;
  502. else
  503. I = I->Parent;
  504. }
  505. if (I == 0)
  506. break;
  507. }
  508. } while (Top != 0);
  509. }
  510. /*}}}*/
  511. // Configuration::Item::FullTag - Return the fully scoped tag /*{{{*/
  512. // ---------------------------------------------------------------------
  513. /* Stop sets an optional max recursion depth if this item is being viewed as
  514. part of a sub tree. */
  515. string Configuration::Item::FullTag(const Item *Stop) const
  516. {
  517. if (Parent == 0 || Parent->Parent == 0 || Parent == Stop)
  518. return Tag;
  519. return Parent->FullTag(Stop) + "::" + Tag;
  520. }
  521. /*}}}*/
  522. // ReadConfigFile - Read a configuration file /*{{{*/
  523. // ---------------------------------------------------------------------
  524. /* The configuration format is very much like the named.conf format
  525. used in bind8, in fact this routine can parse most named.conf files.
  526. Sectional config files are like bind's named.conf where there are
  527. sections like 'zone "foo.org" { .. };' This causes each section to be
  528. added in with a tag like "zone::foo.org" instead of being split
  529. tag/value. AsSectional enables Sectional parsing.*/
  530. bool ReadConfigFile(Configuration &Conf,const string &FName,bool const &AsSectional,
  531. unsigned const &Depth)
  532. {
  533. // Open the stream for reading
  534. ifstream F(FName.c_str(),ios::in);
  535. if (!F != 0)
  536. return _error->Errno("ifstream::ifstream",_("Opening configuration file %s"),FName.c_str());
  537. string LineBuffer;
  538. string Stack[100];
  539. unsigned int StackPos = 0;
  540. // Parser state
  541. string ParentTag;
  542. int CurLine = 0;
  543. bool InComment = false;
  544. while (F.eof() == false)
  545. {
  546. // The raw input line.
  547. std::string Input;
  548. // The input line with comments stripped.
  549. std::string Fragment;
  550. // Grab the next line of F and place it in Input.
  551. do
  552. {
  553. char *Buffer = new char[1024];
  554. F.clear();
  555. F.getline(Buffer,sizeof(Buffer) / 2);
  556. Input += Buffer;
  557. delete[] Buffer;
  558. }
  559. while (F.fail() && !F.eof());
  560. // Expand tabs in the input line and remove leading and trailing
  561. // whitespace.
  562. {
  563. const int BufferSize = Input.size() * 8 + 1;
  564. char *Buffer = new char[BufferSize];
  565. try
  566. {
  567. memcpy(Buffer, Input.c_str(), Input.size() + 1);
  568. _strtabexpand(Buffer, BufferSize);
  569. _strstrip(Buffer);
  570. Input = Buffer;
  571. }
  572. catch(...)
  573. {
  574. delete[] Buffer;
  575. throw;
  576. }
  577. delete[] Buffer;
  578. }
  579. CurLine++;
  580. // Now strip comments; if the whole line is contained in a
  581. // comment, skip this line.
  582. // The first meaningful character in the current fragment; will
  583. // be adjusted below as we remove bytes from the front.
  584. std::string::const_iterator Start = Input.begin();
  585. // The last meaningful character in the current fragment.
  586. std::string::const_iterator End = Input.end();
  587. // Multi line comment
  588. if (InComment == true)
  589. {
  590. for (std::string::const_iterator I = Start;
  591. I != End; ++I)
  592. {
  593. if (*I == '*' && I + 1 != End && I[1] == '/')
  594. {
  595. Start = I + 2;
  596. InComment = false;
  597. break;
  598. }
  599. }
  600. if (InComment == true)
  601. continue;
  602. }
  603. // Discard single line comments
  604. bool InQuote = false;
  605. for (std::string::const_iterator I = Start;
  606. I != End; ++I)
  607. {
  608. if (*I == '"')
  609. InQuote = !InQuote;
  610. if (InQuote == true)
  611. continue;
  612. if ((*I == '/' && I + 1 != End && I[1] == '/') ||
  613. (*I == '#' && strcmp(string(I,I+6).c_str(),"#clear") != 0 &&
  614. strcmp(string(I,I+8).c_str(),"#include") != 0))
  615. {
  616. End = I;
  617. break;
  618. }
  619. }
  620. // Look for multi line comments and build up the
  621. // fragment.
  622. Fragment.reserve(End - Start);
  623. InQuote = false;
  624. for (std::string::const_iterator I = Start;
  625. I != End; ++I)
  626. {
  627. if (*I == '"')
  628. InQuote = !InQuote;
  629. if (InQuote == true)
  630. Fragment.push_back(*I);
  631. else if (*I == '/' && I + 1 != End && I[1] == '*')
  632. {
  633. InComment = true;
  634. for (std::string::const_iterator J = I;
  635. J != End; ++J)
  636. {
  637. if (*J == '*' && J + 1 != End && J[1] == '/')
  638. {
  639. // Pretend we just finished walking over the
  640. // comment, and don't add anything to the output
  641. // fragment.
  642. I = J + 1;
  643. InComment = false;
  644. break;
  645. }
  646. }
  647. if (InComment == true)
  648. break;
  649. }
  650. else
  651. Fragment.push_back(*I);
  652. }
  653. // Skip blank lines.
  654. if (Fragment.empty())
  655. continue;
  656. // The line has actual content; interpret what it means.
  657. InQuote = false;
  658. Start = Fragment.begin();
  659. End = Fragment.end();
  660. for (std::string::const_iterator I = Start;
  661. I != End; ++I)
  662. {
  663. if (*I == '"')
  664. InQuote = !InQuote;
  665. if (InQuote == false && (*I == '{' || *I == ';' || *I == '}'))
  666. {
  667. // Put the last fragment into the buffer
  668. std::string::const_iterator NonWhitespaceStart = Start;
  669. std::string::const_iterator NonWhitespaceStop = I;
  670. for (; NonWhitespaceStart != I && isspace(*NonWhitespaceStart) != 0; ++NonWhitespaceStart)
  671. ;
  672. for (; NonWhitespaceStop != NonWhitespaceStart && isspace(NonWhitespaceStop[-1]) != 0; --NonWhitespaceStop)
  673. ;
  674. if (LineBuffer.empty() == false && NonWhitespaceStop - NonWhitespaceStart != 0)
  675. LineBuffer += ' ';
  676. LineBuffer += string(NonWhitespaceStart, NonWhitespaceStop);
  677. // Drop this from the input string, saving the character
  678. // that terminated the construct we just closed. (i.e., a
  679. // brace or a semicolon)
  680. char TermChar = *I;
  681. Start = I + 1;
  682. // Syntax Error
  683. if (TermChar == '{' && LineBuffer.empty() == true)
  684. return _error->Error(_("Syntax error %s:%u: Block starts with no name."),FName.c_str(),CurLine);
  685. // No string on this line
  686. if (LineBuffer.empty() == true)
  687. {
  688. if (TermChar == '}')
  689. {
  690. if (StackPos == 0)
  691. ParentTag = string();
  692. else
  693. ParentTag = Stack[--StackPos];
  694. }
  695. continue;
  696. }
  697. // Parse off the tag
  698. string Tag;
  699. const char *Pos = LineBuffer.c_str();
  700. if (ParseQuoteWord(Pos,Tag) == false)
  701. return _error->Error(_("Syntax error %s:%u: Malformed tag"),FName.c_str(),CurLine);
  702. // Parse off the word
  703. string Word;
  704. bool NoWord = false;
  705. if (ParseCWord(Pos,Word) == false &&
  706. ParseQuoteWord(Pos,Word) == false)
  707. {
  708. if (TermChar != '{')
  709. {
  710. Word = Tag;
  711. Tag = "";
  712. }
  713. else
  714. NoWord = true;
  715. }
  716. if (strlen(Pos) != 0)
  717. return _error->Error(_("Syntax error %s:%u: Extra junk after value"),FName.c_str(),CurLine);
  718. // Go down a level
  719. if (TermChar == '{')
  720. {
  721. if (StackPos <= 100)
  722. Stack[StackPos++] = ParentTag;
  723. /* Make sectional tags incorperate the section into the
  724. tag string */
  725. if (AsSectional == true && Word.empty() == false)
  726. {
  727. Tag += "::" ;
  728. Tag += Word;
  729. Word = "";
  730. }
  731. if (ParentTag.empty() == true)
  732. ParentTag = Tag;
  733. else
  734. ParentTag += string("::") + Tag;
  735. Tag = string();
  736. }
  737. // Generate the item name
  738. string Item;
  739. if (ParentTag.empty() == true)
  740. Item = Tag;
  741. else
  742. {
  743. if (TermChar != '{' || Tag.empty() == false)
  744. Item = ParentTag + "::" + Tag;
  745. else
  746. Item = ParentTag;
  747. }
  748. // Specials
  749. if (Tag.length() >= 1 && Tag[0] == '#')
  750. {
  751. if (ParentTag.empty() == false)
  752. return _error->Error(_("Syntax error %s:%u: Directives can only be done at the top level"),FName.c_str(),CurLine);
  753. Tag.erase(Tag.begin());
  754. if (Tag == "clear")
  755. Conf.Clear(Word);
  756. else if (Tag == "include")
  757. {
  758. if (Depth > 10)
  759. return _error->Error(_("Syntax error %s:%u: Too many nested includes"),FName.c_str(),CurLine);
  760. if (Word.length() > 2 && Word.end()[-1] == '/')
  761. {
  762. if (ReadConfigDir(Conf,Word,AsSectional,Depth+1) == false)
  763. return _error->Error(_("Syntax error %s:%u: Included from here"),FName.c_str(),CurLine);
  764. }
  765. else
  766. {
  767. if (ReadConfigFile(Conf,Word,AsSectional,Depth+1) == false)
  768. return _error->Error(_("Syntax error %s:%u: Included from here"),FName.c_str(),CurLine);
  769. }
  770. }
  771. else
  772. return _error->Error(_("Syntax error %s:%u: Unsupported directive '%s'"),FName.c_str(),CurLine,Tag.c_str());
  773. }
  774. else if (Tag.empty() == true && NoWord == false && Word == "#clear")
  775. return _error->Error(_("Syntax error %s:%u: clear directive requires an option tree as argument"),FName.c_str(),CurLine);
  776. else
  777. {
  778. // Set the item in the configuration class
  779. if (NoWord == false)
  780. Conf.Set(Item,Word);
  781. }
  782. // Empty the buffer
  783. LineBuffer.clear();
  784. // Move up a tag, but only if there is no bit to parse
  785. if (TermChar == '}')
  786. {
  787. if (StackPos == 0)
  788. ParentTag.clear();
  789. else
  790. ParentTag = Stack[--StackPos];
  791. }
  792. }
  793. }
  794. // Store the remaining text, if any, in the current line buffer.
  795. // NB: could change this to use string-based operations; I'm
  796. // using strstrip now to ensure backwards compatibility.
  797. // -- dburrows 2008-04-01
  798. {
  799. char *Buffer = new char[End - Start + 1];
  800. try
  801. {
  802. std::copy(Start, End, Buffer);
  803. Buffer[End - Start] = '\0';
  804. const char *Stripd = _strstrip(Buffer);
  805. if (*Stripd != 0 && LineBuffer.empty() == false)
  806. LineBuffer += " ";
  807. LineBuffer += Stripd;
  808. }
  809. catch(...)
  810. {
  811. delete[] Buffer;
  812. throw;
  813. }
  814. delete[] Buffer;
  815. }
  816. }
  817. if (LineBuffer.empty() == false)
  818. return _error->Error(_("Syntax error %s:%u: Extra junk at end of file"),FName.c_str(),CurLine);
  819. return true;
  820. }
  821. /*}}}*/
  822. // ReadConfigDir - Read a directory of config files /*{{{*/
  823. // ---------------------------------------------------------------------
  824. /* */
  825. bool ReadConfigDir(Configuration &Conf,const string &Dir,
  826. bool const &AsSectional, unsigned const &Depth)
  827. {
  828. vector<string> const List = GetListOfFilesInDir(Dir, "conf", true, true);
  829. // Read the files
  830. for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
  831. if (ReadConfigFile(Conf,*I,AsSectional,Depth) == false)
  832. return false;
  833. return true;
  834. }
  835. /*}}}*/
  836. // MatchAgainstConfig Constructor /*{{{*/
  837. Configuration::MatchAgainstConfig::MatchAgainstConfig(char const * Config)
  838. {
  839. std::vector<std::string> const strings = _config->FindVector(Config);
  840. for (std::vector<std::string>::const_iterator s = strings.begin();
  841. s != strings.end(); ++s)
  842. {
  843. regex_t *p = new regex_t;
  844. if (regcomp(p, s->c_str(), REG_EXTENDED | REG_ICASE | REG_NOSUB) == 0)
  845. patterns.push_back(p);
  846. else
  847. {
  848. regfree(p);
  849. delete p;
  850. _error->Warning("Invalid regular expression '%s' in configuration "
  851. "option '%s' will be ignored.",
  852. s->c_str(), Config);
  853. continue;
  854. }
  855. }
  856. if (strings.size() == 0)
  857. patterns.push_back(NULL);
  858. }
  859. /*}}}*/
  860. // MatchAgainstConfig Destructor /*{{{*/
  861. Configuration::MatchAgainstConfig::~MatchAgainstConfig()
  862. {
  863. clearPatterns();
  864. }
  865. void Configuration::MatchAgainstConfig::clearPatterns()
  866. {
  867. for(std::vector<regex_t *>::const_iterator p = patterns.begin();
  868. p != patterns.end(); ++p)
  869. {
  870. if (*p == NULL) continue;
  871. regfree(*p);
  872. delete *p;
  873. }
  874. patterns.clear();
  875. }
  876. /*}}}*/
  877. // MatchAgainstConfig::Match - returns true if a pattern matches /*{{{*/
  878. bool Configuration::MatchAgainstConfig::Match(char const * str) const
  879. {
  880. for(std::vector<regex_t *>::const_iterator p = patterns.begin();
  881. p != patterns.end(); ++p)
  882. if (*p != NULL && regexec(*p, str, 0, 0, 0) == 0)
  883. return true;
  884. return false;
  885. }
  886. /*}}}*/