configuration.cc 11 KB

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