apt-sortpkgs.cc 5.6 KB

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