configuration.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: configuration.cc,v 1.13 1999/07/02 23:17:00 jgg 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. ##################################################################### */
  10. /*}}}*/
  11. // Include files /*{{{*/
  12. #ifdef __GNUG__
  13. #pragma implementation "apt-pkg/configuration.h"
  14. #endif
  15. #include <apt-pkg/configuration.h>
  16. #include <apt-pkg/error.h>
  17. #include <apt-pkg/strutl.h>
  18. #include <stdio.h>
  19. #include <fstream.h>
  20. /*}}}*/
  21. Configuration *_config = new Configuration;
  22. // Configuration::Configuration - Constructor /*{{{*/
  23. // ---------------------------------------------------------------------
  24. /* */
  25. Configuration::Configuration()
  26. {
  27. Root = new Item;
  28. }
  29. /*}}}*/
  30. // Configuration::Lookup - Lookup a single item /*{{{*/
  31. // ---------------------------------------------------------------------
  32. /* This will lookup a single item by name below another item. It is a
  33. helper function for the main lookup function */
  34. Configuration::Item *Configuration::Lookup(Item *Head,const char *S,
  35. unsigned long Len,bool Create)
  36. {
  37. int Res = 1;
  38. Item *I = Head->Child;
  39. Item **Last = &Head->Child;
  40. // Empty strings match nothing. They are used for lists.
  41. if (Len != 0)
  42. {
  43. for (; I != 0; Last = &I->Next, I = I->Next)
  44. if ((Res = stringcasecmp(I->Tag.begin(),I->Tag.end(),S,S + Len)) == 0)
  45. break;
  46. }
  47. else
  48. for (; I != 0; Last = &I->Next, I = I->Next);
  49. if (Res == 0)
  50. return I;
  51. if (Create == false)
  52. return 0;
  53. I = new Item;
  54. I->Tag = string(S,Len);
  55. I->Next = *Last;
  56. I->Parent = Head;
  57. *Last = I;
  58. return I;
  59. }
  60. /*}}}*/
  61. // Configuration::Lookup - Lookup a fully scoped item /*{{{*/
  62. // ---------------------------------------------------------------------
  63. /* This performs a fully scoped lookup of a given name, possibly creating
  64. new items */
  65. Configuration::Item *Configuration::Lookup(const char *Name,bool Create)
  66. {
  67. if (Name == 0)
  68. return Root->Child;
  69. const char *Start = Name;
  70. const char *End = Start + strlen(Name);
  71. const char *TagEnd = Name;
  72. Item *Itm = Root;
  73. for (; End - TagEnd >= 2; TagEnd++)
  74. {
  75. if (TagEnd[0] == ':' && TagEnd[1] == ':')
  76. {
  77. Itm = Lookup(Itm,Start,TagEnd - Start,Create);
  78. if (Itm == 0)
  79. return 0;
  80. TagEnd = Start = TagEnd + 2;
  81. }
  82. }
  83. // This must be a trailing ::, we create unique items in a list
  84. if (End - Start == 0)
  85. {
  86. if (Create == false)
  87. return 0;
  88. }
  89. Itm = Lookup(Itm,Start,End - Start,Create);
  90. return Itm;
  91. }
  92. /*}}}*/
  93. // Configuration::Find - Find a value /*{{{*/
  94. // ---------------------------------------------------------------------
  95. /* */
  96. string Configuration::Find(const char *Name,const char *Default)
  97. {
  98. Item *Itm = Lookup(Name,false);
  99. if (Itm == 0 || Itm->Value.empty() == true)
  100. {
  101. if (Default == 0)
  102. return string();
  103. else
  104. return Default;
  105. }
  106. return Itm->Value;
  107. }
  108. /*}}}*/
  109. // Configuration::FindFile - Find a Filename /*{{{*/
  110. // ---------------------------------------------------------------------
  111. /* Directories are stored as the base dir in the Parent node and the
  112. sub directory in sub nodes with the final node being the end filename
  113. */
  114. string Configuration::FindFile(const char *Name,const char *Default)
  115. {
  116. Item *Itm = Lookup(Name,false);
  117. if (Itm == 0 || Itm->Value.empty() == true)
  118. {
  119. if (Default == 0)
  120. return string();
  121. else
  122. return Default;
  123. }
  124. // Absolute path
  125. if (Itm->Value[0] == '/' || Itm->Parent == 0)
  126. return Itm->Value;
  127. // ./ is also considered absolute as is anything with ~ in it
  128. if (Itm->Value[0] != 0 &&
  129. ((Itm->Value[0] == '.' && Itm->Value[1] == '/') ||
  130. (Itm->Value[0] == '~' && Itm->Value[1] == '/')))
  131. return Itm->Value;
  132. if (Itm->Parent->Value.end()[-1] == '/')
  133. return Itm->Parent->Value + Itm->Value;
  134. else
  135. return Itm->Parent->Value + '/' + Itm->Value;
  136. }
  137. /*}}}*/
  138. // Configuration::FindDir - Find a directory name /*{{{*/
  139. // ---------------------------------------------------------------------
  140. /* This is like findfile execept the result is terminated in a / */
  141. string Configuration::FindDir(const char *Name,const char *Default)
  142. {
  143. string Res = FindFile(Name,Default);
  144. if (Res.end()[-1] != '/')
  145. return Res + '/';
  146. return Res;
  147. }
  148. /*}}}*/
  149. // Configuration::FindI - Find an integer value /*{{{*/
  150. // ---------------------------------------------------------------------
  151. /* */
  152. int Configuration::FindI(const char *Name,int Default)
  153. {
  154. Item *Itm = Lookup(Name,false);
  155. if (Itm == 0 || Itm->Value.empty() == true)
  156. return Default;
  157. char *End;
  158. int Res = strtol(Itm->Value.c_str(),&End,0);
  159. if (End == Itm->Value.c_str())
  160. return Default;
  161. return Res;
  162. }
  163. /*}}}*/
  164. // Configuration::FindB - Find a boolean type /*{{{*/
  165. // ---------------------------------------------------------------------
  166. /* */
  167. bool Configuration::FindB(const char *Name,bool Default)
  168. {
  169. Item *Itm = Lookup(Name,false);
  170. if (Itm == 0 || Itm->Value.empty() == true)
  171. return Default;
  172. return StringToBool(Itm->Value,Default);
  173. }
  174. /*}}}*/
  175. // Configuration::Set - Set a value /*{{{*/
  176. // ---------------------------------------------------------------------
  177. /* */
  178. void Configuration::Set(const char *Name,string Value)
  179. {
  180. Item *Itm = Lookup(Name,true);
  181. if (Itm == 0)
  182. return;
  183. Itm->Value = Value;
  184. }
  185. /*}}}*/
  186. // Configuration::Set - Set an integer value /*{{{*/
  187. // ---------------------------------------------------------------------
  188. /* */
  189. void Configuration::Set(const char *Name,int Value)
  190. {
  191. Item *Itm = Lookup(Name,true);
  192. if (Itm == 0)
  193. return;
  194. char S[300];
  195. snprintf(S,sizeof(S),"%i",Value);
  196. Itm->Value = S;
  197. }
  198. /*}}}*/
  199. // Configuration::Exists - Returns true if the Name exists /*{{{*/
  200. // ---------------------------------------------------------------------
  201. /* */
  202. bool Configuration::Exists(const char *Name)
  203. {
  204. Item *Itm = Lookup(Name,false);
  205. if (Itm == 0)
  206. return false;
  207. return true;
  208. }
  209. /*}}}*/
  210. // Configuration::Dump - Dump the config /*{{{*/
  211. // ---------------------------------------------------------------------
  212. /* Dump the entire configuration space */
  213. void Configuration::Dump()
  214. {
  215. /* Write out all of the configuration directives by walking the
  216. configuration tree */
  217. const Configuration::Item *Top = _config->Tree(0);
  218. for (; Top != 0;)
  219. {
  220. clog << Top->FullTag() << " \"" << Top->Value << "\";" << endl;
  221. if (Top->Child != 0)
  222. {
  223. Top = Top->Child;
  224. continue;
  225. }
  226. while (Top != 0 && Top->Next == 0)
  227. Top = Top->Parent;
  228. if (Top != 0)
  229. Top = Top->Next;
  230. }
  231. }
  232. /*}}}*/
  233. // Configuration::Item::FullTag - Return the fully scoped tag /*{{{*/
  234. // ---------------------------------------------------------------------
  235. /* */
  236. string Configuration::Item::FullTag() const
  237. {
  238. if (Parent == 0 || Parent->Parent == 0)
  239. return Tag;
  240. return Parent->FullTag() + "::" + Tag;
  241. }
  242. /*}}}*/
  243. // ReadConfigFile - Read a configuration file /*{{{*/
  244. // ---------------------------------------------------------------------
  245. /* The configuration format is very much like the named.conf format
  246. used in bind8, in fact this routine can parse most named.conf files. */
  247. bool ReadConfigFile(Configuration &Conf,string FName)
  248. {
  249. // Open the stream for reading
  250. ifstream F(FName.c_str(),ios::in | ios::nocreate);
  251. if (!F != 0)
  252. return _error->Errno("ifstream::ifstream","Opening configuration file %s",FName.c_str());
  253. char Buffer[300];
  254. string LineBuffer;
  255. string Stack[100];
  256. unsigned int StackPos = 0;
  257. // Parser state
  258. string ParentTag;
  259. int CurLine = 0;
  260. bool InComment = false;
  261. while (F.eof() == false)
  262. {
  263. F.getline(Buffer,sizeof(Buffer));
  264. CurLine++;
  265. _strtabexpand(Buffer,sizeof(Buffer));
  266. _strstrip(Buffer);
  267. // Multi line comment
  268. if (InComment == true)
  269. {
  270. for (const char *I = Buffer; *I != 0; I++)
  271. {
  272. if (*I == '*' && I[1] == '/')
  273. {
  274. memmove(Buffer,I+2,strlen(I+2) + 1);
  275. InComment = false;
  276. break;
  277. }
  278. }
  279. if (InComment == true)
  280. continue;
  281. }
  282. // Discard single line comments
  283. bool InQuote = false;
  284. for (char *I = Buffer; *I != 0; I++)
  285. {
  286. if (*I == '"')
  287. InQuote = !InQuote;
  288. if (InQuote == true)
  289. continue;
  290. if (*I == '/' && I[1] == '/')
  291. {
  292. *I = 0;
  293. break;
  294. }
  295. }
  296. // Look for multi line comments
  297. for (char *I = Buffer; *I != 0; I++)
  298. {
  299. if (*I == '"')
  300. InQuote = !InQuote;
  301. if (InQuote == true)
  302. continue;
  303. if (*I == '/' && I[1] == '*')
  304. {
  305. InComment = true;
  306. for (char *J = Buffer; *J != 0; J++)
  307. {
  308. if (*J == '*' && J[1] == '/')
  309. {
  310. memmove(I,J+2,strlen(J+2) + 1);
  311. InComment = false;
  312. break;
  313. }
  314. }
  315. if (InComment == true)
  316. {
  317. *I = 0;
  318. break;
  319. }
  320. }
  321. }
  322. // Blank
  323. if (Buffer[0] == 0)
  324. continue;
  325. // We now have a valid line fragment
  326. for (char *I = Buffer; *I != 0;)
  327. {
  328. if (*I == '{' || *I == ';' || *I == '}')
  329. {
  330. // Put the last fragement into the buffer
  331. char *Start = Buffer;
  332. char *Stop = I;
  333. for (; Start != I && isspace(*Start) != 0; Start++);
  334. for (; Stop != Start && isspace(Stop[-1]) != 0; Stop--);
  335. if (LineBuffer.empty() == false && Stop - Start != 0)
  336. LineBuffer += ' ';
  337. LineBuffer += string(Start,Stop - Start);
  338. // Remove the fragment
  339. char TermChar = *I;
  340. memmove(Buffer,I + 1,strlen(I + 1) + 1);
  341. I = Buffer;
  342. // Move up a tag
  343. if (TermChar == '}')
  344. {
  345. if (StackPos == 0)
  346. ParentTag = string();
  347. else
  348. ParentTag = Stack[--StackPos];
  349. }
  350. // Syntax Error
  351. if (TermChar == '{' && LineBuffer.empty() == true)
  352. return _error->Error("Syntax error %s:%u: Block starts with no name.",FName.c_str(),CurLine);
  353. if (LineBuffer.empty() == true)
  354. continue;
  355. // Parse off the tag
  356. string Tag;
  357. const char *Pos = LineBuffer.c_str();
  358. if (ParseQuoteWord(Pos,Tag) == false)
  359. return _error->Error("Syntax error %s:%u: Malformed Tag",FName.c_str(),CurLine);
  360. // Go down a level
  361. if (TermChar == '{')
  362. {
  363. if (StackPos <= 100)
  364. Stack[StackPos++] = ParentTag;
  365. if (ParentTag.empty() == true)
  366. ParentTag = Tag;
  367. else
  368. ParentTag += string("::") + Tag;
  369. Tag = string();
  370. }
  371. // Parse off the word
  372. string Word;
  373. if (ParseCWord(Pos,Word) == false)
  374. {
  375. if (TermChar != '{')
  376. {
  377. Word = Tag;
  378. Tag = "";
  379. }
  380. }
  381. // Generate the item name
  382. string Item;
  383. if (ParentTag.empty() == true)
  384. Item = Tag;
  385. else
  386. {
  387. if (TermChar != '{' || Tag.empty() == false)
  388. Item = ParentTag + "::" + Tag;
  389. else
  390. Item = ParentTag;
  391. }
  392. // Set the item in the configuration class
  393. Conf.Set(Item,Word);
  394. // Empty the buffer
  395. LineBuffer = string();
  396. }
  397. else
  398. I++;
  399. }
  400. // Store the fragment
  401. const char *Stripd = _strstrip(Buffer);
  402. if (*Stripd != 0 && LineBuffer.empty() == false)
  403. LineBuffer += " ";
  404. LineBuffer += Stripd;
  405. }
  406. return true;
  407. }
  408. /*}}}*/