deblistparser.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: deblistparser.cc,v 1.2 1998/07/04 22:32:17 jgg Exp $
  4. /* ######################################################################
  5. Package Cache Generator - Generator for the cache structure.
  6. This builds the cache structure from the abstract package list parser.
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #include <pkglib/deblistparser.h>
  11. #include <pkglib/error.h>
  12. #include <system.h>
  13. /*}}}*/
  14. // ListParser::debListParser - Constructor /*{{{*/
  15. // ---------------------------------------------------------------------
  16. /* */
  17. debListParser::debListParser(File &File) : Tags(File)
  18. {
  19. }
  20. /*}}}*/
  21. // ListParser::FindTag - Find the tag and return a string /*{{{*/
  22. // ---------------------------------------------------------------------
  23. /* */
  24. string debListParser::FindTag(const char *Tag)
  25. {
  26. const char *Start;
  27. const char *Stop;
  28. if (Section.Find(Tag,Start,Stop) == false)
  29. return string();
  30. return string(Start,Stop - Start);
  31. }
  32. /*}}}*/
  33. // ListParser::FindTagI - Find the tag and return an int /*{{{*/
  34. // ---------------------------------------------------------------------
  35. /* */
  36. signed long debListParser::FindTagI(const char *Tag,signed long Default)
  37. {
  38. const char *Start;
  39. const char *Stop;
  40. if (Section.Find(Tag,Start,Stop) == false)
  41. return Default;
  42. // Copy it into a temp buffer so we can use strtol
  43. char S[300];
  44. if ((unsigned)(Stop - Start) >= sizeof(S))
  45. return Default;
  46. strncpy(S,Start,Stop-Start);
  47. S[Stop - Start] = 0;
  48. char *End;
  49. signed long Result = strtol(S,&End,10);
  50. if (S == End)
  51. return Default;
  52. return Result;
  53. }
  54. /*}}}*/
  55. // ListParser::UniqFindTagWrite - Find the tag and write a unq string /*{{{*/
  56. // ---------------------------------------------------------------------
  57. /* */
  58. unsigned long debListParser::UniqFindTagWrite(const char *Tag)
  59. {
  60. const char *Start;
  61. const char *Stop;
  62. if (Section.Find(Tag,Start,Stop) == false)
  63. return 0;
  64. return WriteUniqString(Start,Stop - Start);
  65. }
  66. /*}}}*/
  67. // ListParser::HandleFlag - Sets a flag variable based on a tag /*{{{*/
  68. // ---------------------------------------------------------------------
  69. /* This checks the tag for true/false yes/no etc */
  70. bool debListParser::HandleFlag(const char *Tag,unsigned long &Flags,
  71. unsigned long Flag)
  72. {
  73. const char *Start;
  74. const char *Stop;
  75. if (Section.Find(Tag,Start,Stop) == false)
  76. return true;
  77. int Set = 2;
  78. if (strncasecmp(Start,"yes",Stop - Start) == 0)
  79. Set = 1;
  80. if (strncasecmp(Start,"true",Stop - Start) == 0)
  81. Set = 1;
  82. if (strncasecmp(Start,"no",Stop - Start) == 0)
  83. Set = 0;
  84. if (strncasecmp(Start,"false",Stop - Start) == 0)
  85. Set = 0;
  86. if (Set == 2)
  87. {
  88. _error->Warning("Unknown flag value");
  89. return true;
  90. }
  91. if (Set == 0)
  92. Flags &= ~Flag;
  93. if (Set == 1)
  94. Flags |= Flag;
  95. return true;
  96. }
  97. /*}}}*/
  98. // ListParser::Package - Return the package name /*{{{*/
  99. // ---------------------------------------------------------------------
  100. /* This is to return the name of the package this section describes */
  101. string debListParser::Package()
  102. {
  103. string Result = FindTag("Package");
  104. if (Result.empty() == true)
  105. _error->Error("Encoutered a section with no Package: header");
  106. return Result;
  107. }
  108. /*}}}*/
  109. // ListParser::Version - Return the version string /*{{{*/
  110. // ---------------------------------------------------------------------
  111. /* This is to return the string describing the version in debian form,
  112. epoch:upstream-release. If this returns the blank string then the
  113. entry is assumed to only describe package properties */
  114. string debListParser::Version()
  115. {
  116. return FindTag("Version");
  117. }
  118. /*}}}*/
  119. // ListParser::NewPackage - Fill in the package structure /*{{{*/
  120. // ---------------------------------------------------------------------
  121. /* This is called when a new package structure is created. It must fill
  122. in the static package information. */
  123. bool debListParser::NewPackage(pkgCache::PkgIterator Pkg)
  124. {
  125. // Debian doesnt have anything, everything is condionally megered
  126. return true;
  127. }
  128. /*}}}*/
  129. // ListParser::NewVersion - Fill in the version structure /*{{{*/
  130. // ---------------------------------------------------------------------
  131. /* */
  132. bool debListParser::NewVersion(pkgCache::VerIterator Ver)
  133. {
  134. // Parse the section
  135. if ((Ver->Section = UniqFindTagWrite("Section")) == 0)
  136. return _error->Warning("Missing Section tag");
  137. // Archive Size
  138. if ((Ver->Size = (unsigned)FindTagI("Size")) == 0)
  139. return _error->Error("Unparsable Size field");
  140. // Unpacked Size (in K)
  141. if ((Ver->InstalledSize = (unsigned)FindTagI("Installed-Size")) == 0)
  142. return _error->Error("Unparsable Installed-Size field");
  143. Ver->InstalledSize *= 1024;
  144. // Priority
  145. const char *Start;
  146. const char *Stop;
  147. if (Section.Find("Priority",Start,Stop) == true)
  148. {
  149. WordList PrioList[] = {{"important",pkgCache::Important},
  150. {"required",pkgCache::Required},
  151. {"standard",pkgCache::Standard},
  152. {"optional",pkgCache::Optional},
  153. {"extra",pkgCache::Extra}};
  154. if (GrabWord(string(Start,Stop-Start),PrioList,
  155. _count(PrioList),Ver->Priority) == false)
  156. return _error->Error("Malformed Priority line");
  157. }
  158. return true;
  159. }
  160. /*}}}*/
  161. // ListParser::UsePackage - Update a package structure /*{{{*/
  162. // ---------------------------------------------------------------------
  163. /* This is called to update the package with any new information
  164. that might be found in the section */
  165. bool debListParser::UsePackage(pkgCache::PkgIterator Pkg,
  166. pkgCache::VerIterator Ver)
  167. {
  168. if (Pkg->Section == 0)
  169. if ((Pkg->Section = UniqFindTagWrite("Section")) == 0)
  170. return false;
  171. if (HandleFlag("Essential",Pkg->Flags,pkgCache::Essential) == false)
  172. return false;
  173. if (HandleFlag("Immediate-Configure",Pkg->Flags,pkgCache::ImmediateConf) == false)
  174. return false;
  175. if (ParseStatus(Pkg,Ver) == false)
  176. return false;
  177. return true;
  178. }
  179. /*}}}*/
  180. // ListParser::ParseStatus - Parse the status feild /*{{{*/
  181. // ---------------------------------------------------------------------
  182. /* Status lines are of the form,
  183. Status: want flag status
  184. want = unknown, install, hold, deinstall, purge
  185. flag = ok, reinstreq, hold, hold-reinstreq
  186. status = not-installed, unpacked, half-configured, uninstalled,
  187. half-installed, config-files, post-inst-failed,
  188. removal-failed, installed
  189. Some of the above are obsolete (I think?) flag = hold-* and
  190. status = post-inst-failed, removal-failed at least.
  191. */
  192. bool debListParser::ParseStatus(pkgCache::PkgIterator Pkg,
  193. pkgCache::VerIterator Ver)
  194. {
  195. const char *Start;
  196. const char *Stop;
  197. if (Section.Find("Status",Start,Stop) == false)
  198. return true;
  199. // Isolate the first word
  200. const char *I = Start;
  201. for(; I < Stop && *I != ' '; I++);
  202. if (I >= Stop || *I != ' ')
  203. return _error->Error("Malformed Status line");
  204. // Process the want field
  205. WordList WantList[] = {{"unknown",pkgCache::Unknown},
  206. {"install",pkgCache::Install},
  207. {"hold",pkgCache::Hold},
  208. {"deinstall",pkgCache::DeInstall},
  209. {"purge",pkgCache::Purge}};
  210. if (GrabWord(string(Start,I-Start),WantList,
  211. _count(WantList),Pkg->SelectedState) == false)
  212. return _error->Error("Malformed 1st word in the Status line");
  213. // Isloate the next word
  214. I++;
  215. Start = I;
  216. for(; I < Stop && *I != ' '; I++);
  217. if (I >= Stop || *I != ' ')
  218. return _error->Error("Malformed status line, no 2nd word");
  219. // Process the flag field
  220. WordList FlagList[] = {{"ok",pkgCache::Ok},
  221. {"reinstreq",pkgCache::ReInstReq},
  222. {"hold",pkgCache::HoldInst},
  223. {"hold-reinstreq",pkgCache::HoldReInstReq}};
  224. if (GrabWord(string(Start,I-Start),FlagList,
  225. _count(FlagList),Pkg->InstState) == false)
  226. return _error->Error("Malformed 2nd word in the Status line");
  227. // Isloate the last word
  228. I++;
  229. Start = I;
  230. for(; I < Stop && *I != ' '; I++);
  231. if (I != Stop)
  232. return _error->Error("Malformed Status line, no 3rd word");
  233. // Process the flag field
  234. WordList StatusList[] = {{"not-installed",pkgCache::NotInstalled},
  235. {"unpacked",pkgCache::UnPacked},
  236. {"half-configured",pkgCache::HalfConfigured},
  237. {"installed",pkgCache::Installed},
  238. {"uninstalled",pkgCache::UnInstalled},
  239. {"half-installed",pkgCache::HalfInstalled},
  240. {"config-files",pkgCache::ConfigFiles},
  241. {"post-inst-failed",pkgCache::HalfConfigured},
  242. {"removal-failed",pkgCache::HalfInstalled}};
  243. if (GrabWord(string(Start,I-Start),StatusList,
  244. _count(StatusList),Pkg->CurrentState) == false)
  245. return _error->Error("Malformed 3rd word in the Status line");
  246. /* A Status line marks the package as indicating the current
  247. version as well. Only if it is actually installed.. Otherwise
  248. the interesting dpkg handling of the status file creates bogus
  249. entries. */
  250. if (!(Pkg->CurrentState == pkgCache::NotInstalled ||
  251. Pkg->CurrentState == pkgCache::ConfigFiles))
  252. {
  253. if (Ver.end() == true)
  254. _error->Warning("Encountered status field in a non-version description");
  255. else
  256. Pkg->CurrentVer = Ver.Index();
  257. }
  258. return true;
  259. }
  260. /*}}}*/
  261. // ListParser::GrabWord - Matches a word and returns /*{{{*/
  262. // ---------------------------------------------------------------------
  263. /* Looks for a word in a list of words - for ParseStatus */
  264. bool debListParser::GrabWord(string Word,WordList *List,int Count,
  265. unsigned char &Out)
  266. {
  267. for (int C = 0; C != Count; C++)
  268. {
  269. if (strcasecmp(Word.c_str(),List[C].Str) == 0)
  270. {
  271. Out = List[C].Val;
  272. return true;
  273. }
  274. }
  275. return false;
  276. }
  277. /*}}}*/
  278. // ListParser::Step - Move to the next section in the file /*{{{*/
  279. // ---------------------------------------------------------------------
  280. /* This has to be carefull to only process the correct architecture */
  281. bool debListParser::Step()
  282. {
  283. while (Tags.Step(Section) == true)
  284. {
  285. /* See if this is the correct Architecture, if it isnt then we
  286. drop the whole section */
  287. const char *Start;
  288. const char *Stop;
  289. if (Section.Find("Architecture",Start,Stop) == false)
  290. return true;
  291. if (strncmp(Start,"i386",Stop - Start) == 0 &&
  292. strlen("i386") == (unsigned)(Stop - Start))
  293. return true;
  294. if (strncmp(Start,"all",Stop - Start) == 0 &&
  295. 3 == (unsigned)(Stop - Start))
  296. return true;
  297. }
  298. return false;
  299. }
  300. /*}}}*/