deblistparser.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: deblistparser.cc,v 1.4 1998/07/07 04:17:16 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::NewVersion - Fill in the version structure /*{{{*/
  120. // ---------------------------------------------------------------------
  121. /* */
  122. bool debListParser::NewVersion(pkgCache::VerIterator Ver)
  123. {
  124. // Parse the section
  125. if ((Ver->Section = UniqFindTagWrite("Section")) == 0)
  126. return _error->Warning("Missing Section tag");
  127. // Archive Size
  128. if ((Ver->Size = (unsigned)FindTagI("Size")) == 0)
  129. return _error->Error("Unparsable Size field");
  130. // Unpacked Size (in K)
  131. if ((Ver->InstalledSize = (unsigned)FindTagI("Installed-Size")) == 0)
  132. return _error->Error("Unparsable Installed-Size field");
  133. Ver->InstalledSize *= 1024;
  134. // Priority
  135. const char *Start;
  136. const char *Stop;
  137. if (Section.Find("Priority",Start,Stop) == true)
  138. {
  139. WordList PrioList[] = {{"important",pkgCache::State::Important},
  140. {"required",pkgCache::State::Required},
  141. {"standard",pkgCache::State::Standard},
  142. {"optional",pkgCache::State::Optional},
  143. {"extra",pkgCache::State::Extra}};
  144. if (GrabWord(string(Start,Stop-Start),PrioList,
  145. _count(PrioList),Ver->Priority) == false)
  146. return _error->Error("Malformed Priority line");
  147. }
  148. if (ParseDepends(Ver,"Depends",pkgCache::Dep::Depends) == false)
  149. return false;
  150. if (ParseDepends(Ver,"PreDepends",pkgCache::Dep::PreDepends) == false)
  151. return false;
  152. if (ParseDepends(Ver,"Suggests",pkgCache::Dep::Suggests) == false)
  153. return false;
  154. if (ParseDepends(Ver,"Recommends",pkgCache::Dep::Recommends) == false)
  155. return false;
  156. if (ParseDepends(Ver,"Conflicts",pkgCache::Dep::Conflicts) == false)
  157. return false;
  158. if (ParseDepends(Ver,"Replaces",pkgCache::Dep::Depends) == false)
  159. return false;
  160. if (ParseProvides(Ver) == false)
  161. return false;
  162. return true;
  163. }
  164. /*}}}*/
  165. // ListParser::UsePackage - Update a package structure /*{{{*/
  166. // ---------------------------------------------------------------------
  167. /* This is called to update the package with any new information
  168. that might be found in the section */
  169. bool debListParser::UsePackage(pkgCache::PkgIterator Pkg,
  170. pkgCache::VerIterator Ver)
  171. {
  172. if (Pkg->Section == 0)
  173. if ((Pkg->Section = UniqFindTagWrite("Section")) == 0)
  174. return false;
  175. if (HandleFlag("Essential",Pkg->Flags,pkgCache::Flag::Essential) == false)
  176. return false;
  177. if (HandleFlag("Immediate-Configure",Pkg->Flags,pkgCache::Flag::ImmediateConf) == false)
  178. return false;
  179. if (ParseStatus(Pkg,Ver) == false)
  180. return false;
  181. return true;
  182. }
  183. /*}}}*/
  184. // ListParser::ParseStatus - Parse the status field /*{{{*/
  185. // ---------------------------------------------------------------------
  186. /* Status lines are of the form,
  187. Status: want flag status
  188. want = unknown, install, hold, deinstall, purge
  189. flag = ok, reinstreq, hold, hold-reinstreq
  190. status = not-installed, unpacked, half-configured, uninstalled,
  191. half-installed, config-files, post-inst-failed,
  192. removal-failed, installed
  193. Some of the above are obsolete (I think?) flag = hold-* and
  194. status = post-inst-failed, removal-failed at least.
  195. */
  196. bool debListParser::ParseStatus(pkgCache::PkgIterator Pkg,
  197. pkgCache::VerIterator Ver)
  198. {
  199. const char *Start;
  200. const char *Stop;
  201. if (Section.Find("Status",Start,Stop) == false)
  202. return true;
  203. // Isolate the first word
  204. const char *I = Start;
  205. for(; I < Stop && *I != ' '; I++);
  206. if (I >= Stop || *I != ' ')
  207. return _error->Error("Malformed Status line");
  208. // Process the want field
  209. WordList WantList[] = {{"unknown",pkgCache::State::Unknown},
  210. {"install",pkgCache::State::Install},
  211. {"hold",pkgCache::State::Hold},
  212. {"deinstall",pkgCache::State::DeInstall},
  213. {"purge",pkgCache::State::Purge}};
  214. if (GrabWord(string(Start,I-Start),WantList,
  215. _count(WantList),Pkg->SelectedState) == false)
  216. return _error->Error("Malformed 1st word in the Status line");
  217. // Isloate the next word
  218. I++;
  219. Start = I;
  220. for(; I < Stop && *I != ' '; I++);
  221. if (I >= Stop || *I != ' ')
  222. return _error->Error("Malformed status line, no 2nd word");
  223. // Process the flag field
  224. WordList FlagList[] = {{"ok",pkgCache::State::Ok},
  225. {"reinstreq",pkgCache::State::ReInstReq},
  226. {"hold",pkgCache::State::HoldInst},
  227. {"hold-reinstreq",pkgCache::State::HoldReInstReq}};
  228. if (GrabWord(string(Start,I-Start),FlagList,
  229. _count(FlagList),Pkg->InstState) == false)
  230. return _error->Error("Malformed 2nd word in the Status line");
  231. // Isloate the last word
  232. I++;
  233. Start = I;
  234. for(; I < Stop && *I != ' '; I++);
  235. if (I != Stop)
  236. return _error->Error("Malformed Status line, no 3rd word");
  237. // Process the flag field
  238. WordList StatusList[] = {{"not-installed",pkgCache::State::NotInstalled},
  239. {"unpacked",pkgCache::State::UnPacked},
  240. {"half-configured",pkgCache::State::HalfConfigured},
  241. {"installed",pkgCache::State::Installed},
  242. {"uninstalled",pkgCache::State::UnInstalled},
  243. {"half-installed",pkgCache::State::HalfInstalled},
  244. {"config-files",pkgCache::State::ConfigFiles},
  245. {"post-inst-failed",pkgCache::State::HalfConfigured},
  246. {"removal-failed",pkgCache::State::HalfInstalled}};
  247. if (GrabWord(string(Start,I-Start),StatusList,
  248. _count(StatusList),Pkg->CurrentState) == false)
  249. return _error->Error("Malformed 3rd word in the Status line");
  250. /* A Status line marks the package as indicating the current
  251. version as well. Only if it is actually installed.. Otherwise
  252. the interesting dpkg handling of the status file creates bogus
  253. entries. */
  254. if (!(Pkg->CurrentState == pkgCache::State::NotInstalled ||
  255. Pkg->CurrentState == pkgCache::State::ConfigFiles))
  256. {
  257. if (Ver.end() == true)
  258. _error->Warning("Encountered status field in a non-version description");
  259. else
  260. Pkg->CurrentVer = Ver.Index();
  261. }
  262. return true;
  263. }
  264. /*}}}*/
  265. // ListParser::ParseDepends - Parse a dependency element /*{{{*/
  266. // ---------------------------------------------------------------------
  267. /* This parses the dependency elements out of a standard string in place,
  268. bit by bit. */
  269. const char *debListParser::ParseDepends(const char *Start,const char *Stop,
  270. string &Package,string &Ver,
  271. unsigned int &Op)
  272. {
  273. // Strip off leading space
  274. for (;Start != Stop && isspace(*Start) != 0; Start++);
  275. // Parse off the package name
  276. const char *I = Start;
  277. for (;I != Stop && isspace(*I) == 0 && *I != '(' && *I != ')' &&
  278. *I != ',' && *I != '|'; I++);
  279. // Malformed, no '('
  280. if (I != Stop && *I == ')')
  281. return 0;
  282. if (I == Start)
  283. return 0;
  284. // Stash the package name
  285. Package.assign(Start,I - Start);
  286. // Skip white space to the '('
  287. for (;I != Stop && isspace(*I) != 0 ; I++);
  288. // Parse a version
  289. if (I != Stop && *I == '(')
  290. {
  291. // Skip the '('
  292. for (I++; I != Stop && isspace(*I) != 0 ; I++);
  293. if (I + 3 >= Stop)
  294. return 0;
  295. // Determine the operator
  296. switch (*I)
  297. {
  298. case '<':
  299. I++;
  300. if (*I == '=')
  301. {
  302. I++;
  303. Op = pkgCache::Dep::LessEq;
  304. break;
  305. }
  306. if (*I == '<')
  307. {
  308. I++;
  309. Op = pkgCache::Dep::Less;
  310. break;
  311. }
  312. // < is the same as <= and << is really Cs < for some reason
  313. Op = pkgCache::Dep::LessEq;
  314. break;
  315. case '>':
  316. I++;
  317. if (*I == '=')
  318. {
  319. I++;
  320. Op = pkgCache::Dep::GreaterEq;
  321. break;
  322. }
  323. if (*I == '>')
  324. {
  325. I++;
  326. Op = pkgCache::Dep::Greater;
  327. break;
  328. }
  329. // > is the same as >= and >> is really Cs > for some reason
  330. Op = pkgCache::Dep::GreaterEq;
  331. break;
  332. case '=':
  333. Op = pkgCache::Dep::Equals;
  334. I++;
  335. break;
  336. // HACK around bad package definitions
  337. default:
  338. Op = pkgCache::Dep::Equals;
  339. break;
  340. }
  341. // Skip whitespace
  342. for (;I != Stop && isspace(*I) != 0; I++);
  343. Start = I;
  344. for (;I != Stop && *I != ')'; I++);
  345. if (I == Stop || Start == I)
  346. return 0;
  347. Ver = string(Start,I-Start);
  348. I++;
  349. }
  350. else
  351. {
  352. Ver = string();
  353. Op = pkgCache::Dep::NoOp;
  354. }
  355. // Skip whitespace
  356. for (;I != Stop && isspace(*I) != 0; I++);
  357. if (I != Stop && *I == '|')
  358. Op |= pkgCache::Dep::Or;
  359. if (I == Stop || *I == ',' || *I == '|')
  360. {
  361. if (I != Stop)
  362. for (I++; I != Stop && isspace(*I) != 0; I++);
  363. return I;
  364. }
  365. return 0;
  366. }
  367. /*}}}*/
  368. // ListParser::ParseDepends - Parse a dependency list /*{{{*/
  369. // ---------------------------------------------------------------------
  370. /* This is the higher level depends parser. It takes a tag and generates
  371. a complete depends tree for the given version. */
  372. bool debListParser::ParseDepends(pkgCache::VerIterator Ver,
  373. const char *Tag,unsigned int Type)
  374. {
  375. const char *Start;
  376. const char *Stop;
  377. if (Section.Find(Tag,Start,Stop) == false)
  378. return true;
  379. string Package;
  380. string Version;
  381. unsigned int Op;
  382. while ((Start = ParseDepends(Start,Stop,Package,Version,Op)) != Stop)
  383. {
  384. if (Start == 0)
  385. return _error->Error("Problem parsing dependency %s",Tag);
  386. if (NewDepends(Ver,Package,Version,Op,Type) == false)
  387. return false;
  388. }
  389. return true;
  390. }
  391. /*}}}*/
  392. // ListParser::ParseProvides - Parse the provides list /*{{{*/
  393. // ---------------------------------------------------------------------
  394. /* */
  395. bool debListParser::ParseProvides(pkgCache::VerIterator Ver)
  396. {
  397. const char *Start;
  398. const char *Stop;
  399. if (Section.Find("Provides",Start,Stop) == false)
  400. return true;
  401. string Package;
  402. string Version;
  403. unsigned int Op;
  404. while (1)
  405. {
  406. Start = ParseDepends(Start,Stop,Package,Version,Op);
  407. if (Start == 0)
  408. return _error->Error("Problem parsing Provides line");
  409. if (Op != pkgCache::Dep::NoOp)
  410. return _error->Error("Malformed provides line");
  411. if (NewProvides(Ver,Package,Version) == false)
  412. return false;
  413. if (Start == Stop)
  414. break;
  415. }
  416. return true;
  417. }
  418. /*}}}*/
  419. // ListParser::GrabWord - Matches a word and returns /*{{{*/
  420. // ---------------------------------------------------------------------
  421. /* Looks for a word in a list of words - for ParseStatus */
  422. bool debListParser::GrabWord(string Word,WordList *List,int Count,
  423. unsigned char &Out)
  424. {
  425. for (int C = 0; C != Count; C++)
  426. {
  427. if (strcasecmp(Word.c_str(),List[C].Str) == 0)
  428. {
  429. Out = List[C].Val;
  430. return true;
  431. }
  432. }
  433. return false;
  434. }
  435. /*}}}*/
  436. // ListParser::Step - Move to the next section in the file /*{{{*/
  437. // ---------------------------------------------------------------------
  438. /* This has to be carefull to only process the correct architecture */
  439. bool debListParser::Step()
  440. {
  441. iOffset = Tags.Offset();
  442. while (Tags.Step(Section) == true)
  443. {
  444. /* See if this is the correct Architecture, if it isnt then we
  445. drop the whole section */
  446. const char *Start;
  447. const char *Stop;
  448. if (Section.Find("Architecture",Start,Stop) == false)
  449. return true;
  450. if (strncmp(Start,"i386",Stop - Start) == 0 &&
  451. strlen("i386") == (unsigned)(Stop - Start))
  452. return true;
  453. if (strncmp(Start,"all",Stop - Start) == 0 &&
  454. 3 == (unsigned)(Stop - Start))
  455. return true;
  456. iOffset = Tags.Offset();
  457. }
  458. return false;
  459. }
  460. /*}}}*/