deblistparser.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: deblistparser.cc,v 1.14 1999/01/27 02:48:53 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 <apt-pkg/deblistparser.h>
  11. #include <apt-pkg/error.h>
  12. #include <apt-pkg/configuration.h>
  13. #include <apt-pkg/strutl.h>
  14. #include <system.h>
  15. /*}}}*/
  16. // ListParser::debListParser - Constructor /*{{{*/
  17. // ---------------------------------------------------------------------
  18. /* */
  19. debListParser::debListParser(FileFd &File) : Tags(File)
  20. {
  21. }
  22. /*}}}*/
  23. // ListParser::UniqFindTagWrite - Find the tag and write a unq string /*{{{*/
  24. // ---------------------------------------------------------------------
  25. /* */
  26. unsigned long debListParser::UniqFindTagWrite(const char *Tag)
  27. {
  28. const char *Start;
  29. const char *Stop;
  30. if (Section.Find(Tag,Start,Stop) == false)
  31. return 0;
  32. return WriteUniqString(Start,Stop - Start);
  33. }
  34. /*}}}*/
  35. // ListParser::Package - Return the package name /*{{{*/
  36. // ---------------------------------------------------------------------
  37. /* This is to return the name of the package this section describes */
  38. string debListParser::Package()
  39. {
  40. string Result = Section.FindS("Package");
  41. if (Result.empty() == true)
  42. _error->Error("Encoutered a section with no Package: header");
  43. return Result;
  44. }
  45. /*}}}*/
  46. // ListParser::Version - Return the version string /*{{{*/
  47. // ---------------------------------------------------------------------
  48. /* This is to return the string describing the version in debian form,
  49. epoch:upstream-release. If this returns the blank string then the
  50. entry is assumed to only describe package properties */
  51. string debListParser::Version()
  52. {
  53. return Section.FindS("Version");
  54. }
  55. /*}}}*/
  56. // ListParser::NewVersion - Fill in the version structure /*{{{*/
  57. // ---------------------------------------------------------------------
  58. /* */
  59. bool debListParser::NewVersion(pkgCache::VerIterator Ver)
  60. {
  61. // Parse the section
  62. Ver->Section = UniqFindTagWrite("Section");
  63. // Archive Size
  64. Ver->Size = (unsigned)Section.FindI("Size");
  65. // Unpacked Size (in K)
  66. Ver->InstalledSize = (unsigned)Section.FindI("Installed-Size");
  67. Ver->InstalledSize *= 1024;
  68. // Priority
  69. const char *Start;
  70. const char *Stop;
  71. if (Section.Find("Priority",Start,Stop) == true)
  72. {
  73. WordList PrioList[] = {{"important",pkgCache::State::Important},
  74. {"required",pkgCache::State::Required},
  75. {"standard",pkgCache::State::Standard},
  76. {"optional",pkgCache::State::Optional},
  77. {"extra",pkgCache::State::Extra}};
  78. if (GrabWord(string(Start,Stop-Start),PrioList,
  79. _count(PrioList),Ver->Priority) == false)
  80. return _error->Error("Malformed Priority line");
  81. }
  82. if (ParseDepends(Ver,"Depends",pkgCache::Dep::Depends) == false)
  83. return false;
  84. if (ParseDepends(Ver,"Pre-Depends",pkgCache::Dep::PreDepends) == false)
  85. return false;
  86. if (ParseDepends(Ver,"Suggests",pkgCache::Dep::Suggests) == false)
  87. return false;
  88. if (ParseDepends(Ver,"Recommends",pkgCache::Dep::Recommends) == false)
  89. return false;
  90. if (ParseDepends(Ver,"Conflicts",pkgCache::Dep::Conflicts) == false)
  91. return false;
  92. if (ParseDepends(Ver,"Replaces",pkgCache::Dep::Replaces) == false)
  93. return false;
  94. if (ParseProvides(Ver) == false)
  95. return false;
  96. return true;
  97. }
  98. /*}}}*/
  99. // ListParser::UsePackage - Update a package structure /*{{{*/
  100. // ---------------------------------------------------------------------
  101. /* This is called to update the package with any new information
  102. that might be found in the section */
  103. bool debListParser::UsePackage(pkgCache::PkgIterator Pkg,
  104. pkgCache::VerIterator Ver)
  105. {
  106. if (Pkg->Section == 0)
  107. Pkg->Section = UniqFindTagWrite("Section");
  108. if (Section.FindFlag("Essential",Pkg->Flags,pkgCache::Flag::Essential) == false)
  109. return false;
  110. if (Section.FindFlag("Immediate-Configure",Pkg->Flags,pkgCache::Flag::ImmediateConf) == false)
  111. return false;
  112. if (ParseStatus(Pkg,Ver) == false)
  113. return false;
  114. return true;
  115. }
  116. /*}}}*/
  117. // ListParser::ParseStatus - Parse the status field /*{{{*/
  118. // ---------------------------------------------------------------------
  119. /* Status lines are of the form,
  120. Status: want flag status
  121. want = unknown, install, hold, deinstall, purge
  122. flag = ok, reinstreq, hold, hold-reinstreq
  123. status = not-installed, unpacked, half-configured, uninstalled,
  124. half-installed, config-files, post-inst-failed,
  125. removal-failed, installed
  126. Some of the above are obsolete (I think?) flag = hold-* and
  127. status = post-inst-failed, removal-failed at least.
  128. */
  129. bool debListParser::ParseStatus(pkgCache::PkgIterator Pkg,
  130. pkgCache::VerIterator Ver)
  131. {
  132. const char *Start;
  133. const char *Stop;
  134. if (Section.Find("Status",Start,Stop) == false)
  135. return true;
  136. // Isolate the first word
  137. const char *I = Start;
  138. for(; I < Stop && *I != ' '; I++);
  139. if (I >= Stop || *I != ' ')
  140. return _error->Error("Malformed Status line");
  141. // Process the want field
  142. WordList WantList[] = {{"unknown",pkgCache::State::Unknown},
  143. {"install",pkgCache::State::Install},
  144. {"hold",pkgCache::State::Hold},
  145. {"deinstall",pkgCache::State::DeInstall},
  146. {"purge",pkgCache::State::Purge}};
  147. if (GrabWord(string(Start,I-Start),WantList,
  148. _count(WantList),Pkg->SelectedState) == false)
  149. return _error->Error("Malformed 1st word in the Status line");
  150. // Isloate the next word
  151. I++;
  152. Start = I;
  153. for(; I < Stop && *I != ' '; I++);
  154. if (I >= Stop || *I != ' ')
  155. return _error->Error("Malformed status line, no 2nd word");
  156. // Process the flag field
  157. WordList FlagList[] = {{"ok",pkgCache::State::Ok},
  158. {"reinstreq",pkgCache::State::ReInstReq},
  159. {"hold",pkgCache::State::HoldInst},
  160. {"hold-reinstreq",pkgCache::State::HoldReInstReq}};
  161. if (GrabWord(string(Start,I-Start),FlagList,
  162. _count(FlagList),Pkg->InstState) == false)
  163. return _error->Error("Malformed 2nd word in the Status line");
  164. // Isloate the last word
  165. I++;
  166. Start = I;
  167. for(; I < Stop && *I != ' '; I++);
  168. if (I != Stop)
  169. return _error->Error("Malformed Status line, no 3rd word");
  170. // Process the flag field
  171. WordList StatusList[] = {{"not-installed",pkgCache::State::NotInstalled},
  172. {"unpacked",pkgCache::State::UnPacked},
  173. {"half-configured",pkgCache::State::HalfConfigured},
  174. {"installed",pkgCache::State::Installed},
  175. {"uninstalled",pkgCache::State::UnInstalled},
  176. {"half-installed",pkgCache::State::HalfInstalled},
  177. {"config-files",pkgCache::State::ConfigFiles},
  178. {"post-inst-failed",pkgCache::State::HalfConfigured},
  179. {"removal-failed",pkgCache::State::HalfInstalled}};
  180. if (GrabWord(string(Start,I-Start),StatusList,
  181. _count(StatusList),Pkg->CurrentState) == false)
  182. return _error->Error("Malformed 3rd word in the Status line");
  183. /* A Status line marks the package as indicating the current
  184. version as well. Only if it is actually installed.. Otherwise
  185. the interesting dpkg handling of the status file creates bogus
  186. entries. */
  187. if (!(Pkg->CurrentState == pkgCache::State::NotInstalled ||
  188. Pkg->CurrentState == pkgCache::State::ConfigFiles))
  189. {
  190. if (Ver.end() == true)
  191. _error->Warning("Encountered status field in a non-version description");
  192. else
  193. Pkg->CurrentVer = Ver.Index();
  194. }
  195. return true;
  196. }
  197. /*}}}*/
  198. // ListParser::ParseDepends - Parse a dependency element /*{{{*/
  199. // ---------------------------------------------------------------------
  200. /* This parses the dependency elements out of a standard string in place,
  201. bit by bit. */
  202. const char *debListParser::ParseDepends(const char *Start,const char *Stop,
  203. string &Package,string &Ver,
  204. unsigned int &Op)
  205. {
  206. // Strip off leading space
  207. for (;Start != Stop && isspace(*Start) != 0; Start++);
  208. // Parse off the package name
  209. const char *I = Start;
  210. for (;I != Stop && isspace(*I) == 0 && *I != '(' && *I != ')' &&
  211. *I != ',' && *I != '|'; I++);
  212. // Malformed, no '('
  213. if (I != Stop && *I == ')')
  214. return 0;
  215. if (I == Start)
  216. return 0;
  217. // Stash the package name
  218. Package.assign(Start,I - Start);
  219. // Skip white space to the '('
  220. for (;I != Stop && isspace(*I) != 0 ; I++);
  221. // Parse a version
  222. if (I != Stop && *I == '(')
  223. {
  224. // Skip the '('
  225. for (I++; I != Stop && isspace(*I) != 0 ; I++);
  226. if (I + 3 >= Stop)
  227. return 0;
  228. // Determine the operator
  229. switch (*I)
  230. {
  231. case '<':
  232. I++;
  233. if (*I == '=')
  234. {
  235. I++;
  236. Op = pkgCache::Dep::LessEq;
  237. break;
  238. }
  239. if (*I == '<')
  240. {
  241. I++;
  242. Op = pkgCache::Dep::Less;
  243. break;
  244. }
  245. // < is the same as <= and << is really Cs < for some reason
  246. Op = pkgCache::Dep::LessEq;
  247. break;
  248. case '>':
  249. I++;
  250. if (*I == '=')
  251. {
  252. I++;
  253. Op = pkgCache::Dep::GreaterEq;
  254. break;
  255. }
  256. if (*I == '>')
  257. {
  258. I++;
  259. Op = pkgCache::Dep::Greater;
  260. break;
  261. }
  262. // > is the same as >= and >> is really Cs > for some reason
  263. Op = pkgCache::Dep::GreaterEq;
  264. break;
  265. case '=':
  266. Op = pkgCache::Dep::Equals;
  267. I++;
  268. break;
  269. // HACK around bad package definitions
  270. default:
  271. Op = pkgCache::Dep::Equals;
  272. break;
  273. }
  274. // Skip whitespace
  275. for (;I != Stop && isspace(*I) != 0; I++);
  276. Start = I;
  277. for (;I != Stop && *I != ')'; I++);
  278. if (I == Stop || Start == I)
  279. return 0;
  280. Ver = string(Start,I-Start);
  281. I++;
  282. }
  283. else
  284. {
  285. Ver = string();
  286. Op = pkgCache::Dep::NoOp;
  287. }
  288. // Skip whitespace
  289. for (;I != Stop && isspace(*I) != 0; I++);
  290. if (I != Stop && *I == '|')
  291. Op |= pkgCache::Dep::Or;
  292. if (I == Stop || *I == ',' || *I == '|')
  293. {
  294. if (I != Stop)
  295. for (I++; I != Stop && isspace(*I) != 0; I++);
  296. return I;
  297. }
  298. return 0;
  299. }
  300. /*}}}*/
  301. // ListParser::ParseDepends - Parse a dependency list /*{{{*/
  302. // ---------------------------------------------------------------------
  303. /* This is the higher level depends parser. It takes a tag and generates
  304. a complete depends tree for the given version. */
  305. bool debListParser::ParseDepends(pkgCache::VerIterator Ver,
  306. const char *Tag,unsigned int Type)
  307. {
  308. const char *Start;
  309. const char *Stop;
  310. if (Section.Find(Tag,Start,Stop) == false)
  311. return true;
  312. string Package;
  313. string Version;
  314. unsigned int Op;
  315. while (1)
  316. {
  317. Start = ParseDepends(Start,Stop,Package,Version,Op);
  318. if (Start == 0)
  319. return _error->Error("Problem parsing dependency %s",Tag);
  320. if (NewDepends(Ver,Package,Version,Op,Type) == false)
  321. return false;
  322. if (Start == Stop)
  323. break;
  324. }
  325. return true;
  326. }
  327. /*}}}*/
  328. // ListParser::ParseProvides - Parse the provides list /*{{{*/
  329. // ---------------------------------------------------------------------
  330. /* */
  331. bool debListParser::ParseProvides(pkgCache::VerIterator Ver)
  332. {
  333. const char *Start;
  334. const char *Stop;
  335. if (Section.Find("Provides",Start,Stop) == false)
  336. return true;
  337. string Package;
  338. string Version;
  339. unsigned int Op;
  340. while (1)
  341. {
  342. Start = ParseDepends(Start,Stop,Package,Version,Op);
  343. if (Start == 0)
  344. return _error->Error("Problem parsing Provides line");
  345. if (Op != pkgCache::Dep::NoOp)
  346. return _error->Error("Malformed provides line");
  347. if (NewProvides(Ver,Package,Version) == false)
  348. return false;
  349. if (Start == Stop)
  350. break;
  351. }
  352. return true;
  353. }
  354. /*}}}*/
  355. // ListParser::GrabWord - Matches a word and returns /*{{{*/
  356. // ---------------------------------------------------------------------
  357. /* Looks for a word in a list of words - for ParseStatus */
  358. bool debListParser::GrabWord(string Word,WordList *List,int Count,
  359. unsigned char &Out)
  360. {
  361. for (int C = 0; C != Count; C++)
  362. {
  363. if (strcasecmp(Word.c_str(),List[C].Str) == 0)
  364. {
  365. Out = List[C].Val;
  366. return true;
  367. }
  368. }
  369. return false;
  370. }
  371. /*}}}*/
  372. // ListParser::Step - Move to the next section in the file /*{{{*/
  373. // ---------------------------------------------------------------------
  374. /* This has to be carefull to only process the correct architecture */
  375. bool debListParser::Step()
  376. {
  377. iOffset = Tags.Offset();
  378. string Arch = _config->Find("APT::architecture");
  379. while (Tags.Step(Section) == true)
  380. {
  381. /* See if this is the correct Architecture, if it isnt then we
  382. drop the whole section */
  383. const char *Start;
  384. const char *Stop;
  385. if (Section.Find("Architecture",Start,Stop) == false)
  386. return true;
  387. if (stringcmp(Start,Stop,Arch.begin(),Arch.end()) == 0)
  388. return true;
  389. if (stringcmp(Start,Stop,"all") == 0)
  390. return true;
  391. iOffset = Tags.Offset();
  392. }
  393. return false;
  394. }
  395. /*}}}*/
  396. // ListParser::LoadReleaseInfo - Load the release information /*{{{*/
  397. // ---------------------------------------------------------------------
  398. /* */
  399. bool debListParser::LoadReleaseInfo(pkgCache::PkgFileIterator FileI,
  400. FileFd &File)
  401. {
  402. pkgTagFile Tags(File);
  403. pkgTagSection Section;
  404. if (Tags.Step(Section) == false)
  405. return false;
  406. const char *Start;
  407. const char *Stop;
  408. if (Section.Find("Archive",Start,Stop) == true)
  409. FileI->Archive = WriteUniqString(Start,Stop - Start);
  410. if (Section.Find("Component",Start,Stop) == true)
  411. FileI->Component = WriteUniqString(Start,Stop - Start);
  412. if (Section.Find("Version",Start,Stop) == true)
  413. FileI->Version = WriteUniqString(Start,Stop - Start);
  414. if (Section.Find("Origin",Start,Stop) == true)
  415. FileI->Origin = WriteUniqString(Start,Stop - Start);
  416. if (Section.Find("Label",Start,Stop) == true)
  417. FileI->Label = WriteUniqString(Start,Stop - Start);
  418. if (Section.Find("Architecture",Start,Stop) == true)
  419. FileI->Architecture = WriteUniqString(Start,Stop - Start);
  420. if (Section.FindFlag("NotAutomatic",FileI->Flags,
  421. pkgCache::Flag::NotAutomatic) == false)
  422. _error->Warning("Bad NotAutomatic flag");
  423. return !_error->PendingError();
  424. }
  425. /*}}}*/