deblistparser.cc 15 KB

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