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