configuration.cc 23 KB

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