configuration.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: configuration.cc,v 1.8 1998/10/22 04:56:45 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::Item::FullTag - Return the fully scoped tag /*{{{*/
  199. // ---------------------------------------------------------------------
  200. /* */
  201. string Configuration::Item::FullTag() const
  202. {
  203. if (Parent == 0 || Parent->Parent == 0)
  204. return Tag;
  205. return Parent->FullTag() + "::" + Tag;
  206. }
  207. /*}}}*/
  208. // ReadConfigFile - Read a configuration file /*{{{*/
  209. // ---------------------------------------------------------------------
  210. /* The configuration format is very much like the named.conf format
  211. used in bind8, in fact this routine can parse most named.conf files. */
  212. bool ReadConfigFile(Configuration &Conf,string FName)
  213. {
  214. // Open the stream for reading
  215. ifstream F(FName.c_str(),ios::in | ios::nocreate);
  216. if (!F != 0)
  217. return _error->Errno("ifstream::ifstream","Opening configuration file %s",FName.c_str());
  218. char Buffer[300];
  219. string LineBuffer;
  220. // Parser state
  221. string ParentTag;
  222. int CurLine = 0;
  223. bool InComment = false;
  224. while (F.eof() == false)
  225. {
  226. F.getline(Buffer,sizeof(Buffer));
  227. CurLine++;
  228. _strtabexpand(Buffer,sizeof(Buffer));
  229. _strstrip(Buffer);
  230. // Multi line comment
  231. if (InComment == true)
  232. {
  233. for (const char *I = Buffer; *I != 0; I++)
  234. {
  235. if (*I == '*' && I[1] == '/')
  236. {
  237. memmove(Buffer,I+2,strlen(I+2) + 1);
  238. InComment = false;
  239. break;
  240. }
  241. }
  242. if (InComment == true)
  243. continue;
  244. }
  245. // Discard single line comments
  246. for (char *I = Buffer; *I != 0; I++)
  247. {
  248. if (*I == '/' && I[1] == '/')
  249. {
  250. *I = 0;
  251. break;
  252. }
  253. }
  254. // Look for multi line comments
  255. for (char *I = Buffer; *I != 0; I++)
  256. {
  257. if (*I == '/' && I[1] == '*')
  258. {
  259. InComment = true;
  260. for (char *J = Buffer; *J != 0; J++)
  261. {
  262. if (*J == '*' && J[1] == '/')
  263. {
  264. memmove(I,J+2,strlen(J+2) + 1);
  265. InComment = false;
  266. break;
  267. }
  268. }
  269. if (InComment == true)
  270. {
  271. *I = 0;
  272. break;
  273. }
  274. }
  275. }
  276. // Blank
  277. if (Buffer[0] == 0)
  278. continue;
  279. // We now have a valid line fragment
  280. for (char *I = Buffer; *I != 0;)
  281. {
  282. if (*I == '{' || *I == ';' || *I == '}')
  283. {
  284. // Put the last fragement into the buffer
  285. char *Start = Buffer;
  286. char *Stop = I;
  287. for (; Start != I && isspace(*Start) != 0; Start++);
  288. for (; Stop != Start && isspace(Stop[-1]) != 0; Stop--);
  289. if (LineBuffer.empty() == false && Stop - Start != 0)
  290. LineBuffer += ' ';
  291. LineBuffer += string(Start,Stop - Start);
  292. // Remove the fragment
  293. char TermChar = *I;
  294. memmove(Buffer,I + 1,strlen(I + 1) + 1);
  295. I = Buffer;
  296. // Move up a tag
  297. if (TermChar == '}')
  298. {
  299. string::size_type Pos = ParentTag.rfind("::");
  300. if (Pos == string::npos)
  301. ParentTag = string();
  302. else
  303. ParentTag = string(ParentTag,0,Pos);
  304. }
  305. // Syntax Error
  306. if (TermChar == '{' && LineBuffer.empty() == true)
  307. return _error->Error("Syntax error %s:%u: Block starts with no name.",FName.c_str(),CurLine);
  308. if (LineBuffer.empty() == true)
  309. continue;
  310. // Parse off the tag
  311. string::size_type Pos = LineBuffer.find(' ');
  312. if (Pos == string::npos)
  313. {
  314. if (TermChar == '{')
  315. Pos = LineBuffer.length();
  316. else
  317. return _error->Error("Syntax error %s:%u: Tag with no value",FName.c_str(),CurLine);
  318. }
  319. string Tag = string(LineBuffer,0,Pos);
  320. // Go down a level
  321. if (TermChar == '{')
  322. {
  323. if (ParentTag.empty() == true)
  324. ParentTag = Tag;
  325. else
  326. ParentTag += string("::") + Tag;
  327. Tag = string();
  328. }
  329. // We dont have a value to set
  330. if (Pos == LineBuffer.length())
  331. {
  332. LineBuffer = string();
  333. continue;
  334. }
  335. // Parse off the word
  336. string Word;
  337. if (ParseCWord(LineBuffer.c_str()+Pos,Word) == false)
  338. return _error->Error("Syntax error %s:%u: Malformed value",FName.c_str(),CurLine);
  339. // Generate the item name
  340. string Item;
  341. if (ParentTag.empty() == true)
  342. Item = Tag;
  343. else
  344. {
  345. if (Tag.empty() == true)
  346. Item = ParentTag;
  347. else
  348. Item = ParentTag + "::" + Tag;
  349. }
  350. // Set the item in the configuration class
  351. Conf.Set(Item,Word);
  352. // Empty the buffer
  353. LineBuffer = string();
  354. }
  355. else
  356. I++;
  357. }
  358. // Store the fragment
  359. const char *Stripd = _strstrip(Buffer);
  360. if (*Stripd != 0 && LineBuffer.empty() == false)
  361. LineBuffer += " ";
  362. LineBuffer += Stripd;
  363. }
  364. return true;
  365. }
  366. /*}}}*/