configuration.cc 10 KB

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