]> git.saurik.com Git - apt.git/blame - apt-pkg/deb/deblistparser.cc
fix build-dependency parser to understand [!arch] flags...
[apt.git] / apt-pkg / deb / deblistparser.cc
CommitLineData
f55a958f
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
9e10ad8a 3// $Id: deblistparser.cc,v 1.28 2001/10/02 03:03:47 tausq Exp $
f55a958f
AL
4/* ######################################################################
5
6 Package Cache Generator - Generator for the cache structure.
7
8 This builds the cache structure from the abstract package list parser.
9
10 ##################################################################### */
11 /*}}}*/
12// Include Files /*{{{*/
094a497d
AL
13#include <apt-pkg/deblistparser.h>
14#include <apt-pkg/error.h>
15#include <apt-pkg/configuration.h>
cdcc6d34 16#include <apt-pkg/strutl.h>
204fbdcc 17#include <apt-pkg/crc-16.h>
9c14e3d6 18
e7b470ee
AL
19#include <ctype.h>
20
f55a958f
AL
21#include <system.h>
22 /*}}}*/
23
b2e465d6
AL
24static debListParser::WordList PrioList[] = {{"important",pkgCache::State::Important},
25 {"required",pkgCache::State::Required},
26 {"standard",pkgCache::State::Standard},
27 {"optional",pkgCache::State::Optional},
28 {"extra",pkgCache::State::Extra},
29 {}};
30
f55a958f
AL
31// ListParser::debListParser - Constructor /*{{{*/
32// ---------------------------------------------------------------------
33/* */
b2e465d6 34debListParser::debListParser(FileFd *File) : Tags(File)
f55a958f 35{
ddc1d8d0 36 Arch = _config->Find("APT::architecture");
0149949b
AL
37}
38 /*}}}*/
f55a958f
AL
39// ListParser::UniqFindTagWrite - Find the tag and write a unq string /*{{{*/
40// ---------------------------------------------------------------------
41/* */
42unsigned long debListParser::UniqFindTagWrite(const char *Tag)
43{
44 const char *Start;
45 const char *Stop;
46 if (Section.Find(Tag,Start,Stop) == false)
47 return 0;
48 return WriteUniqString(Start,Stop - Start);
49}
50 /*}}}*/
f55a958f
AL
51// ListParser::Package - Return the package name /*{{{*/
52// ---------------------------------------------------------------------
53/* This is to return the name of the package this section describes */
54string debListParser::Package()
55{
b0b4efb9 56 string Result = Section.FindS("Package");
f55a958f 57 if (Result.empty() == true)
65a1e968 58 _error->Error("Encountered a section with no Package: header");
f55a958f
AL
59 return Result;
60}
61 /*}}}*/
62// ListParser::Version - Return the version string /*{{{*/
63// ---------------------------------------------------------------------
64/* This is to return the string describing the version in debian form,
65 epoch:upstream-release. If this returns the blank string then the
66 entry is assumed to only describe package properties */
67string debListParser::Version()
68{
b0b4efb9 69 return Section.FindS("Version");
f55a958f
AL
70}
71 /*}}}*/
f55a958f
AL
72// ListParser::NewVersion - Fill in the version structure /*{{{*/
73// ---------------------------------------------------------------------
74/* */
75bool debListParser::NewVersion(pkgCache::VerIterator Ver)
0149949b
AL
76{
77 // Parse the section
b35d2f5f 78 Ver->Section = UniqFindTagWrite("Section");
17caf1b1 79 Ver->Arch = UniqFindTagWrite("Architecture");
0149949b
AL
80
81 // Archive Size
b0b4efb9 82 Ver->Size = (unsigned)Section.FindI("Size");
0149949b
AL
83
84 // Unpacked Size (in K)
b0b4efb9 85 Ver->InstalledSize = (unsigned)Section.FindI("Installed-Size");
0149949b
AL
86 Ver->InstalledSize *= 1024;
87
88 // Priority
89 const char *Start;
90 const char *Stop;
91 if (Section.Find("Priority",Start,Stop) == true)
b2e465d6
AL
92 {
93 if (GrabWord(string(Start,Stop-Start),PrioList,Ver->Priority) == false)
421c8d10 94 Ver->Priority = pkgCache::State::Extra;
0149949b 95 }
dcb79bae 96
6c139d6e 97 if (ParseDepends(Ver,"Depends",pkgCache::Dep::Depends) == false)
dcb79bae 98 return false;
8efa2a3b 99 if (ParseDepends(Ver,"Pre-Depends",pkgCache::Dep::PreDepends) == false)
dcb79bae 100 return false;
6c139d6e 101 if (ParseDepends(Ver,"Suggests",pkgCache::Dep::Suggests) == false)
dcb79bae 102 return false;
6c139d6e 103 if (ParseDepends(Ver,"Recommends",pkgCache::Dep::Recommends) == false)
dcb79bae 104 return false;
6c139d6e 105 if (ParseDepends(Ver,"Conflicts",pkgCache::Dep::Conflicts) == false)
dcb79bae 106 return false;
f55ece0e 107 if (ParseDepends(Ver,"Replaces",pkgCache::Dep::Replaces) == false)
dcb79bae
AL
108 return false;
109
b2e465d6
AL
110 // Obsolete.
111 if (ParseDepends(Ver,"Optional",pkgCache::Dep::Suggests) == false)
112 return false;
113
dcb79bae
AL
114 if (ParseProvides(Ver) == false)
115 return false;
0149949b 116
f55a958f
AL
117 return true;
118}
119 /*}}}*/
120// ListParser::UsePackage - Update a package structure /*{{{*/
121// ---------------------------------------------------------------------
122/* This is called to update the package with any new information
123 that might be found in the section */
124bool debListParser::UsePackage(pkgCache::PkgIterator Pkg,
125 pkgCache::VerIterator Ver)
126{
127 if (Pkg->Section == 0)
b35d2f5f 128 Pkg->Section = UniqFindTagWrite("Section");
b0b4efb9 129 if (Section.FindFlag("Essential",Pkg->Flags,pkgCache::Flag::Essential) == false)
f55a958f 130 return false;
138d4b3d 131 if (Section.FindFlag("Important",Pkg->Flags,pkgCache::Flag::Important) == false)
f55a958f 132 return false;
138d4b3d
AL
133
134 if (strcmp(Pkg.Name(),"apt") == 0)
135 Pkg->Flags |= pkgCache::Flag::Important;
136
f55a958f
AL
137 if (ParseStatus(Pkg,Ver) == false)
138 return false;
139 return true;
140}
141 /*}}}*/
204fbdcc
AL
142// ListParser::VersionHash - Compute a unique hash for this version /*{{{*/
143// ---------------------------------------------------------------------
144/* */
145unsigned short debListParser::VersionHash()
146{
147 const char *Sections[] ={"Installed-Size",
148 "Depends",
149 "Pre-Depends",
f78439bf
AL
150// "Suggests",
151// "Recommends",
204fbdcc
AL
152 "Conflicts",
153 "Replaces",0};
154 unsigned long Result = INIT_FCS;
418a471f 155 char S[1024];
204fbdcc
AL
156 for (const char **I = Sections; *I != 0; I++)
157 {
158 const char *Start;
159 const char *End;
160 if (Section.Find(*I,Start,End) == false || End - Start >= (signed)sizeof(S))
161 continue;
162
163 /* Strip out any spaces from the text, this undoes dpkgs reformatting
421c8d10
AL
164 of certain fields. dpkg also has the rather interesting notion of
165 reformatting depends operators < -> <= */
204fbdcc
AL
166 char *I = S;
167 for (; Start != End; Start++)
421c8d10 168 {
204fbdcc 169 if (isspace(*Start) == 0)
ddc1d8d0 170 *I++ = tolower(*Start);
421c8d10
AL
171 if (*Start == '<' && Start[1] != '<' && Start[1] != '=')
172 *I++ = '=';
173 if (*Start == '>' && Start[1] != '>' && Start[1] != '=')
174 *I++ = '=';
175 }
418a471f 176
204fbdcc
AL
177 Result = AddCRC16(Result,S,I - S);
178 }
179
180 return Result;
181}
182 /*}}}*/
dcb79bae 183// ListParser::ParseStatus - Parse the status field /*{{{*/
f55a958f
AL
184// ---------------------------------------------------------------------
185/* Status lines are of the form,
186 Status: want flag status
187 want = unknown, install, hold, deinstall, purge
188 flag = ok, reinstreq, hold, hold-reinstreq
a005475e 189 status = not-installed, unpacked, half-configured,
f55a958f
AL
190 half-installed, config-files, post-inst-failed,
191 removal-failed, installed
192
193 Some of the above are obsolete (I think?) flag = hold-* and
194 status = post-inst-failed, removal-failed at least.
195 */
196bool 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
204 // Isolate the first word
205 const char *I = Start;
206 for(; I < Stop && *I != ' '; I++);
207 if (I >= Stop || *I != ' ')
208 return _error->Error("Malformed Status line");
209
210 // Process the want field
6c139d6e
AL
211 WordList WantList[] = {{"unknown",pkgCache::State::Unknown},
212 {"install",pkgCache::State::Install},
213 {"hold",pkgCache::State::Hold},
214 {"deinstall",pkgCache::State::DeInstall},
b2e465d6
AL
215 {"purge",pkgCache::State::Purge},
216 {}};
217 if (GrabWord(string(Start,I-Start),WantList,Pkg->SelectedState) == false)
f55a958f
AL
218 return _error->Error("Malformed 1st word in the Status line");
219
220 // Isloate the next word
221 I++;
222 Start = I;
223 for(; I < Stop && *I != ' '; I++);
224 if (I >= Stop || *I != ' ')
225 return _error->Error("Malformed status line, no 2nd word");
226
227 // Process the flag field
6c139d6e
AL
228 WordList FlagList[] = {{"ok",pkgCache::State::Ok},
229 {"reinstreq",pkgCache::State::ReInstReq},
230 {"hold",pkgCache::State::HoldInst},
b2e465d6
AL
231 {"hold-reinstreq",pkgCache::State::HoldReInstReq},
232 {}};
233 if (GrabWord(string(Start,I-Start),FlagList,Pkg->InstState) == false)
f55a958f
AL
234 return _error->Error("Malformed 2nd word in the Status line");
235
236 // Isloate the last word
237 I++;
238 Start = I;
239 for(; I < Stop && *I != ' '; I++);
240 if (I != Stop)
241 return _error->Error("Malformed Status line, no 3rd word");
242
243 // Process the flag field
6c139d6e
AL
244 WordList StatusList[] = {{"not-installed",pkgCache::State::NotInstalled},
245 {"unpacked",pkgCache::State::UnPacked},
246 {"half-configured",pkgCache::State::HalfConfigured},
247 {"installed",pkgCache::State::Installed},
6c139d6e
AL
248 {"half-installed",pkgCache::State::HalfInstalled},
249 {"config-files",pkgCache::State::ConfigFiles},
250 {"post-inst-failed",pkgCache::State::HalfConfigured},
b2e465d6
AL
251 {"removal-failed",pkgCache::State::HalfInstalled},
252 {}};
253 if (GrabWord(string(Start,I-Start),StatusList,Pkg->CurrentState) == false)
f55a958f
AL
254 return _error->Error("Malformed 3rd word in the Status line");
255
256 /* A Status line marks the package as indicating the current
257 version as well. Only if it is actually installed.. Otherwise
258 the interesting dpkg handling of the status file creates bogus
259 entries. */
6c139d6e
AL
260 if (!(Pkg->CurrentState == pkgCache::State::NotInstalled ||
261 Pkg->CurrentState == pkgCache::State::ConfigFiles))
f55a958f
AL
262 {
263 if (Ver.end() == true)
264 _error->Warning("Encountered status field in a non-version description");
265 else
266 Pkg->CurrentVer = Ver.Index();
267 }
268
dcb79bae
AL
269 return true;
270}
271 /*}}}*/
272// ListParser::ParseDepends - Parse a dependency element /*{{{*/
273// ---------------------------------------------------------------------
274/* This parses the dependency elements out of a standard string in place,
275 bit by bit. */
b2e465d6
AL
276const char *debListParser::ConvertRelation(const char *I,unsigned int &Op)
277{
278 // Determine the operator
279 switch (*I)
280 {
281 case '<':
282 I++;
283 if (*I == '=')
284 {
285 I++;
286 Op = pkgCache::Dep::LessEq;
287 break;
288 }
289
290 if (*I == '<')
291 {
292 I++;
293 Op = pkgCache::Dep::Less;
294 break;
295 }
296
297 // < is the same as <= and << is really Cs < for some reason
298 Op = pkgCache::Dep::LessEq;
299 break;
300
301 case '>':
302 I++;
303 if (*I == '=')
304 {
305 I++;
306 Op = pkgCache::Dep::GreaterEq;
307 break;
308 }
309
310 if (*I == '>')
311 {
312 I++;
313 Op = pkgCache::Dep::Greater;
314 break;
315 }
316
317 // > is the same as >= and >> is really Cs > for some reason
318 Op = pkgCache::Dep::GreaterEq;
319 break;
320
321 case '=':
322 Op = pkgCache::Dep::Equals;
323 I++;
324 break;
325
326 // HACK around bad package definitions
327 default:
328 Op = pkgCache::Dep::Equals;
329 break;
330 }
331 return I;
332}
333
dcb79bae
AL
334const char *debListParser::ParseDepends(const char *Start,const char *Stop,
335 string &Package,string &Ver,
b2e465d6 336 unsigned int &Op, bool ParseArchFlags)
dcb79bae
AL
337{
338 // Strip off leading space
339 for (;Start != Stop && isspace(*Start) != 0; Start++);
340
341 // Parse off the package name
342 const char *I = Start;
343 for (;I != Stop && isspace(*I) == 0 && *I != '(' && *I != ')' &&
344 *I != ',' && *I != '|'; I++);
345
346 // Malformed, no '('
347 if (I != Stop && *I == ')')
348 return 0;
349
350 if (I == Start)
351 return 0;
352
353 // Stash the package name
354 Package.assign(Start,I - Start);
355
356 // Skip white space to the '('
357 for (;I != Stop && isspace(*I) != 0 ; I++);
358
359 // Parse a version
360 if (I != Stop && *I == '(')
361 {
362 // Skip the '('
363 for (I++; I != Stop && isspace(*I) != 0 ; I++);
364 if (I + 3 >= Stop)
365 return 0;
b2e465d6 366 I = ConvertRelation(I,Op);
dcb79bae
AL
367
368 // Skip whitespace
369 for (;I != Stop && isspace(*I) != 0; I++);
370 Start = I;
371 for (;I != Stop && *I != ')'; I++);
372 if (I == Stop || Start == I)
373 return 0;
374
02f000a9
AL
375 // Skip trailing whitespace
376 const char *End = I;
377 for (; End > Start && isspace(End[-1]); End--);
378
379 Ver = string(Start,End-Start);
dcb79bae
AL
380 I++;
381 }
382 else
383 {
384 Ver = string();
6c139d6e 385 Op = pkgCache::Dep::NoOp;
dcb79bae
AL
386 }
387
388 // Skip whitespace
389 for (;I != Stop && isspace(*I) != 0; I++);
b2e465d6
AL
390
391 if (ParseArchFlags == true)
392 {
393 string arch = _config->Find("APT::Architecture");
9e10ad8a 394
b2e465d6
AL
395 // Parse an architecture
396 if (I != Stop && *I == '[')
397 {
398 // malformed
399 I++;
400 if (I == Stop)
401 return 0;
402
403 const char *End = I;
404 bool Found = false;
9e10ad8a 405 bool NegArch = false;
b2e465d6
AL
406 while (I != Stop)
407 {
408 // look for whitespace or ending ']'
409 while (End != Stop && !isspace(*End) && *End != ']')
410 End++;
411
412 if (End == Stop)
413 return 0;
9e10ad8a
AL
414
415 if (*I == '!')
416 {
417 NegArch = true;
418 I++;
419 }
420
3e18cc75 421 if (stringcmp(arch,I,End) == 0)
b2e465d6
AL
422 Found = true;
423
424 if (*End++ == ']') {
425 I = End;
426 break;
427 }
428
429 I = End;
430 for (;I != Stop && isspace(*I) != 0; I++);
431 }
9e10ad8a
AL
432
433 if (NegArch)
434 Found = !Found;
b2e465d6 435
9e10ad8a 436 if (Found == false)
b2e465d6
AL
437 Package = ""; /* not for this arch */
438 }
439
440 // Skip whitespace
441 for (;I != Stop && isspace(*I) != 0; I++);
442 }
443
dcb79bae 444 if (I != Stop && *I == '|')
6c139d6e 445 Op |= pkgCache::Dep::Or;
dcb79bae
AL
446
447 if (I == Stop || *I == ',' || *I == '|')
448 {
449 if (I != Stop)
450 for (I++; I != Stop && isspace(*I) != 0; I++);
451 return I;
452 }
453
454 return 0;
455}
456 /*}}}*/
457// ListParser::ParseDepends - Parse a dependency list /*{{{*/
458// ---------------------------------------------------------------------
459/* This is the higher level depends parser. It takes a tag and generates
460 a complete depends tree for the given version. */
461bool debListParser::ParseDepends(pkgCache::VerIterator Ver,
462 const char *Tag,unsigned int Type)
463{
464 const char *Start;
465 const char *Stop;
466 if (Section.Find(Tag,Start,Stop) == false)
467 return true;
468
469 string Package;
470 string Version;
471 unsigned int Op;
472
8efa2a3b 473 while (1)
dcb79bae 474 {
8efa2a3b 475 Start = ParseDepends(Start,Stop,Package,Version,Op);
dcb79bae
AL
476 if (Start == 0)
477 return _error->Error("Problem parsing dependency %s",Tag);
8efa2a3b 478
dcb79bae
AL
479 if (NewDepends(Ver,Package,Version,Op,Type) == false)
480 return false;
8efa2a3b
AL
481 if (Start == Stop)
482 break;
dcb79bae
AL
483 }
484 return true;
485}
486 /*}}}*/
487// ListParser::ParseProvides - Parse the provides list /*{{{*/
488// ---------------------------------------------------------------------
489/* */
490bool debListParser::ParseProvides(pkgCache::VerIterator Ver)
491{
492 const char *Start;
493 const char *Stop;
494 if (Section.Find("Provides",Start,Stop) == false)
495 return true;
496
497 string Package;
498 string Version;
499 unsigned int Op;
500
501 while (1)
502 {
503 Start = ParseDepends(Start,Stop,Package,Version,Op);
504 if (Start == 0)
505 return _error->Error("Problem parsing Provides line");
6c139d6e 506 if (Op != pkgCache::Dep::NoOp)
dcb79bae
AL
507 return _error->Error("Malformed provides line");
508
509 if (NewProvides(Ver,Package,Version) == false)
510 return false;
511
512 if (Start == Stop)
513 break;
514 }
515
f55a958f
AL
516 return true;
517}
518 /*}}}*/
519// ListParser::GrabWord - Matches a word and returns /*{{{*/
520// ---------------------------------------------------------------------
521/* Looks for a word in a list of words - for ParseStatus */
b2e465d6 522bool debListParser::GrabWord(string Word,WordList *List,unsigned char &Out)
f55a958f 523{
b2e465d6 524 for (unsigned int C = 0; List[C].Str != 0; C++)
f55a958f
AL
525 {
526 if (strcasecmp(Word.c_str(),List[C].Str) == 0)
527 {
528 Out = List[C].Val;
529 return true;
530 }
531 }
532 return false;
533}
534 /*}}}*/
535// ListParser::Step - Move to the next section in the file /*{{{*/
536// ---------------------------------------------------------------------
0149949b 537/* This has to be carefull to only process the correct architecture */
f55a958f
AL
538bool debListParser::Step()
539{
dcb79bae 540 iOffset = Tags.Offset();
0149949b 541 while (Tags.Step(Section) == true)
ddc1d8d0
AL
542 {
543 /* See if this is the correct Architecture, if it isn't then we
544 drop the whole section. A missing arch tag only happens (in theory)
545 inside the Status file, so that is a positive return */
0149949b
AL
546 const char *Start;
547 const char *Stop;
548 if (Section.Find("Architecture",Start,Stop) == false)
549 return true;
9c14e3d6 550
3e18cc75 551 if (stringcmp(Arch,Start,Stop) == 0)
0149949b
AL
552 return true;
553
9c14e3d6 554 if (stringcmp(Start,Stop,"all") == 0)
0149949b 555 return true;
dcb79bae
AL
556
557 iOffset = Tags.Offset();
0149949b
AL
558 }
559 return false;
f55a958f
AL
560}
561 /*}}}*/
b0b4efb9
AL
562// ListParser::LoadReleaseInfo - Load the release information /*{{{*/
563// ---------------------------------------------------------------------
564/* */
565bool debListParser::LoadReleaseInfo(pkgCache::PkgFileIterator FileI,
566 FileFd &File)
567{
b2e465d6 568 pkgTagFile Tags(&File);
b0b4efb9
AL
569 pkgTagSection Section;
570 if (Tags.Step(Section) == false)
571 return false;
572
573 const char *Start;
574 const char *Stop;
575 if (Section.Find("Archive",Start,Stop) == true)
576 FileI->Archive = WriteUniqString(Start,Stop - Start);
577 if (Section.Find("Component",Start,Stop) == true)
578 FileI->Component = WriteUniqString(Start,Stop - Start);
579 if (Section.Find("Version",Start,Stop) == true)
580 FileI->Version = WriteUniqString(Start,Stop - Start);
581 if (Section.Find("Origin",Start,Stop) == true)
582 FileI->Origin = WriteUniqString(Start,Stop - Start);
583 if (Section.Find("Label",Start,Stop) == true)
584 FileI->Label = WriteUniqString(Start,Stop - Start);
585 if (Section.Find("Architecture",Start,Stop) == true)
586 FileI->Architecture = WriteUniqString(Start,Stop - Start);
0dbb95d8 587
3c124dde
AL
588 if (Section.FindFlag("NotAutomatic",FileI->Flags,
589 pkgCache::Flag::NotAutomatic) == false)
0dbb95d8 590 _error->Warning("Bad NotAutomatic flag");
0dbb95d8 591
b0b4efb9
AL
592 return !_error->PendingError();
593}
594 /*}}}*/
b2e465d6
AL
595// ListParser::GetPrio - Convert the priority from a string /*{{{*/
596// ---------------------------------------------------------------------
597/* */
598unsigned char debListParser::GetPrio(string Str)
599{
600 unsigned char Out;
601 if (GrabWord(Str,PrioList,Out) == false)
602 Out = pkgCache::State::Extra;
603
604 return Out;
605}
606 /*}}}*/