configuration.cc 10 KB

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