configuration.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: configuration.cc,v 1.9 1998/10/30 07:53:42 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. for (char *I = Buffer; *I != 0; I++)
  270. {
  271. if (*I == '/' && I[1] == '/')
  272. {
  273. *I = 0;
  274. break;
  275. }
  276. }
  277. // Look for multi line comments
  278. for (char *I = Buffer; *I != 0; I++)
  279. {
  280. if (*I == '/' && I[1] == '*')
  281. {
  282. InComment = true;
  283. for (char *J = Buffer; *J != 0; J++)
  284. {
  285. if (*J == '*' && J[1] == '/')
  286. {
  287. memmove(I,J+2,strlen(J+2) + 1);
  288. InComment = false;
  289. break;
  290. }
  291. }
  292. if (InComment == true)
  293. {
  294. *I = 0;
  295. break;
  296. }
  297. }
  298. }
  299. // Blank
  300. if (Buffer[0] == 0)
  301. continue;
  302. // We now have a valid line fragment
  303. for (char *I = Buffer; *I != 0;)
  304. {
  305. if (*I == '{' || *I == ';' || *I == '}')
  306. {
  307. // Put the last fragement into the buffer
  308. char *Start = Buffer;
  309. char *Stop = I;
  310. for (; Start != I && isspace(*Start) != 0; Start++);
  311. for (; Stop != Start && isspace(Stop[-1]) != 0; Stop--);
  312. if (LineBuffer.empty() == false && Stop - Start != 0)
  313. LineBuffer += ' ';
  314. LineBuffer += string(Start,Stop - Start);
  315. // Remove the fragment
  316. char TermChar = *I;
  317. memmove(Buffer,I + 1,strlen(I + 1) + 1);
  318. I = Buffer;
  319. // Move up a tag
  320. if (TermChar == '}')
  321. {
  322. string::size_type Pos = ParentTag.rfind("::");
  323. if (Pos == string::npos)
  324. ParentTag = string();
  325. else
  326. ParentTag = string(ParentTag,0,Pos);
  327. }
  328. // Syntax Error
  329. if (TermChar == '{' && LineBuffer.empty() == true)
  330. return _error->Error("Syntax error %s:%u: Block starts with no name.",FName.c_str(),CurLine);
  331. if (LineBuffer.empty() == true)
  332. continue;
  333. // Parse off the tag
  334. string::size_type Pos = LineBuffer.find(' ');
  335. if (Pos == string::npos)
  336. {
  337. if (TermChar == '{')
  338. Pos = LineBuffer.length();
  339. else
  340. return _error->Error("Syntax error %s:%u: Tag with no value",FName.c_str(),CurLine);
  341. }
  342. string Tag = string(LineBuffer,0,Pos);
  343. // Go down a level
  344. if (TermChar == '{')
  345. {
  346. if (ParentTag.empty() == true)
  347. ParentTag = Tag;
  348. else
  349. ParentTag += string("::") + Tag;
  350. Tag = string();
  351. }
  352. // We dont have a value to set
  353. if (Pos == LineBuffer.length())
  354. {
  355. LineBuffer = string();
  356. continue;
  357. }
  358. // Parse off the word
  359. string Word;
  360. if (ParseCWord(LineBuffer.c_str()+Pos,Word) == false)
  361. return _error->Error("Syntax error %s:%u: Malformed value",FName.c_str(),CurLine);
  362. // Generate the item name
  363. string Item;
  364. if (ParentTag.empty() == true)
  365. Item = Tag;
  366. else
  367. {
  368. if (Tag.empty() == true)
  369. Item = ParentTag;
  370. else
  371. Item = ParentTag + "::" + Tag;
  372. }
  373. // Set the item in the configuration class
  374. Conf.Set(Item,Word);
  375. // Empty the buffer
  376. LineBuffer = string();
  377. }
  378. else
  379. I++;
  380. }
  381. // Store the fragment
  382. const char *Stripd = _strstrip(Buffer);
  383. if (*Stripd != 0 && LineBuffer.empty() == false)
  384. LineBuffer += " ";
  385. LineBuffer += Stripd;
  386. }
  387. return true;
  388. }
  389. /*}}}*/