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