apt-sortpkgs.cc 5.4 KB

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