apt-sortpkgs.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: apt-sortpkgs.cc,v 1.4 2002/02/15 03:40:00 jgg Exp $
  4. /* ######################################################################
  5. APT Sort Packages - Program to sort Package and Source files
  6. This program is quite simple, it just sorts the package files by
  7. package and sorts the fields inside by the internal APT sort order.
  8. Input is taken from a named file and sent to stdout.
  9. ##################################################################### */
  10. /*}}}*/
  11. // Include Files /*{{{*/
  12. #include <apt-pkg/tagfile.h>
  13. #include <apt-pkg/error.h>
  14. #include <apt-pkg/configuration.h>
  15. #include <apt-pkg/cmndline.h>
  16. #include <apt-pkg/init.h>
  17. #include <apt-pkg/strutl.h>
  18. #include <config.h>
  19. #include <apti18n.h>
  20. #include <vector>
  21. #include <algorithm>
  22. #include <locale.h>
  23. #include <unistd.h>
  24. /*}}}*/
  25. struct PkgName
  26. {
  27. string Name;
  28. string Ver;
  29. string Arch;
  30. unsigned long Offset;
  31. unsigned long Length;
  32. inline int Compare3(const PkgName &x) const
  33. {
  34. int A = stringcasecmp(Name,x.Name);
  35. if (A == 0)
  36. {
  37. A = stringcasecmp(Ver,x.Ver);
  38. if (A == 0)
  39. A = stringcasecmp(Arch,x.Arch);
  40. }
  41. return A;
  42. }
  43. bool operator <(const PkgName &x) const {return Compare3(x) < 0;};
  44. bool operator >(const PkgName &x) const {return Compare3(x) > 0;};
  45. bool operator ==(const PkgName &x) const {return Compare3(x) == 0;};
  46. };
  47. // DoIt - Sort a single file /*{{{*/
  48. // ---------------------------------------------------------------------
  49. /* */
  50. bool DoIt(string InFile)
  51. {
  52. FileFd Fd(InFile,FileFd::ReadOnly);
  53. pkgTagFile Tags(&Fd);
  54. if (_error->PendingError() == true)
  55. return false;
  56. // Parse.
  57. vector<PkgName> List;
  58. pkgTagSection Section;
  59. unsigned long Largest = 0;
  60. unsigned long Offset = Tags.Offset();
  61. bool Source = _config->FindB("APT::SortPkgs::Source",false);
  62. while (Tags.Step(Section) == true)
  63. {
  64. PkgName Tmp;
  65. /* Fetch the name, auto-detecting if this is a source file or a
  66. package file */
  67. Tmp.Name = Section.FindS("Package");
  68. Tmp.Ver = Section.FindS("Version");
  69. Tmp.Arch = Section.FindS("Architecture");
  70. if (Tmp.Name.empty() == true)
  71. return _error->Error(_("Unknown package record!"));
  72. Tmp.Offset = Offset;
  73. Tmp.Length = Section.size();
  74. if (Largest < Tmp.Length)
  75. Largest = Tmp.Length;
  76. List.push_back(Tmp);
  77. Offset = Tags.Offset();
  78. }
  79. if (_error->PendingError() == true)
  80. return false;
  81. // Sort it
  82. sort(List.begin(),List.end());
  83. const char **Order = TFRewritePackageOrder;
  84. if (Source == true)
  85. Order = TFRewriteSourceOrder;
  86. // Emit
  87. unsigned char *Buffer = new unsigned char[Largest+1];
  88. for (vector<PkgName>::iterator I = List.begin(); I != List.end(); I++)
  89. {
  90. // Read in the Record.
  91. if (Fd.Seek(I->Offset) == false || Fd.Read(Buffer,I->Length) == false)
  92. {
  93. delete [] Buffer;
  94. return false;
  95. }
  96. Buffer[I->Length] = '\n';
  97. if (Section.Scan((char *)Buffer,I->Length+1) == false)
  98. {
  99. delete [] Buffer;
  100. return _error->Error("Internal error, failed to scan buffer");
  101. }
  102. // Sort the section
  103. if (TFRewrite(stdout,Section,Order,0) == false)
  104. {
  105. delete [] Buffer;
  106. return _error->Error("Internal error, failed to sort fields");
  107. }
  108. fputc('\n',stdout);
  109. }
  110. delete [] Buffer;
  111. return true;
  112. }
  113. /*}}}*/
  114. // ShowHelp - Show the help text /*{{{*/
  115. // ---------------------------------------------------------------------
  116. /* */
  117. int ShowHelp()
  118. {
  119. ioprintf(cout,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE,VERSION,
  120. COMMON_OS,COMMON_CPU,__DATE__,__TIME__);
  121. if (_config->FindB("version") == true)
  122. return 0;
  123. cout <<
  124. _("Usage: apt-sortpkgs [options] file1 [file2 ...]\n"
  125. "\n"
  126. "apt-sortpkgs is a simple tool to sort package files. The -s option is used\n"
  127. "to indicate what kind of file it is.\n"
  128. "\n"
  129. "Options:\n"
  130. " -h This help text\n"
  131. " -s Use source file sorting\n"
  132. " -c=? Read this configuration file\n"
  133. " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp\n");
  134. return 0;
  135. }
  136. /*}}}*/
  137. int main(unsigned int argc,const char *argv[])
  138. {
  139. CommandLine::Args Args[] = {
  140. {'h',"help","help",0},
  141. {'v',"version","version",0},
  142. {'s',"source","APT::SortPkgs::Source",0},
  143. {'c',"config-file",0,CommandLine::ConfigFile},
  144. {'o',"option",0,CommandLine::ArbItem},
  145. {0,0,0,0}};
  146. // Set up gettext support
  147. setlocale(LC_ALL,"");
  148. textdomain(PACKAGE);
  149. // Parse the command line and initialize the package library
  150. CommandLine CmdL(Args,_config);
  151. if (pkgInitConfig(*_config) == false ||
  152. CmdL.Parse(argc,argv) == false ||
  153. pkgInitSystem(*_config,_system) == false)
  154. {
  155. _error->DumpErrors();
  156. return 100;
  157. }
  158. // See if the help should be shown
  159. if (_config->FindB("help") == true ||
  160. CmdL.FileSize() == 0)
  161. return ShowHelp();
  162. // Match the operation
  163. for (unsigned int I = 0; I != CmdL.FileSize(); I++)
  164. if (DoIt(CmdL.FileList[I]) == false)
  165. break;
  166. // Print any errors or warnings found during parsing
  167. if (_error->empty() == false)
  168. {
  169. bool Errors = _error->PendingError();
  170. _error->DumpErrors();
  171. return Errors == true?100:0;
  172. }
  173. return 0;
  174. }